File: status-code.rkt

package info (click to toggle)
racket 7.9%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 178,684 kB
  • sloc: ansic: 282,112; lisp: 234,887; pascal: 70,954; sh: 27,112; asm: 16,268; makefile: 4,613; cpp: 2,715; ada: 1,681; javascript: 1,244; cs: 879; exp: 499; csh: 422; python: 274; xml: 106; perl: 104
file content (98 lines) | stat: -rw-r--r-- 2,426 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#lang racket/base

(require racket/contract
         racket/match
         (only-in racket/string
                  non-empty-string?))

(provide/contract
 [message-for-status-code
  (number? . -> . (or/c false/c non-empty-string?))]
 [DEFAULT-STATUS-MESSAGE
  bytes?]
 [infer-response-message
  (number? (or/c false/c bytes?) . -> . bytes?)])

(module+ test
  (require rackunit))

;; HTTP status codes coming from
;;
;; + Hypertext Transfer Protocol (HTTP/1.1): Semantics and Content
;;   (https://tools.ietf.org/html/rfc7231)
;;
;; + Hypertext Transfer Protocol (HTTP/1.1): Authentication
;;   (https://tools.ietf.org/html/rfc7235)

(define/contract common-http-status-codes&messages
  (and/c (hash/c (integer-in 100 599) non-empty-string?)
         immutable?)
  (hasheq
   100 "Continue"
   101 "Switching Protocols"

   200 "OK"
   201 "Created"
   202 "Accepted"
   203 "Non-Authoritative Information"
   204 "No Content"
   205 "Reset Content"
   206 "Partial Content" ; RFC 7233

   300 "Multiple Choices"
   301 "Moved Permanently"
   302 "Found"
   303 "See Other"
   305 "Use Proxy"
   307 "Temporary Redirect"
   308 "Permanent Redirect" ; RFC 7538

   400 "Bad Request"
   401 "Unauthorized"
   402 "Payment Required"
   403 "Forbidden"
   404 "Not Found"
   405 "Method Not Allowed"
   406 "Not Acceptable"
   407 "Proxy Authentication Required"
   408 "Request Timeout"
   409 "Conflict"
   410 "Gone"
   411 "Length Required"
   413 "Payload Too Large"
   414 "URI Too Long"
   415 "Unsupported Media Type"
   417 "Expectation Failed"
   426 "Upgrade Required"

   500 "Internal Server Error"
   501 "Not Implemented"
   502 "Bad Gateway"
   503 "Service Unavailable"
   504 "Gateway Timeout"
   505 "HTTP Version Not Supported"))

(module+ test
  (check-equal?
   (list 100 101
         200 201 202 203 204 205 206
         300 301 302 303 305 307 308
         400 401 402 403 404 405 406 407 408 409 410 411 413 414 415 417 426
         500 501 502 503 504 505)
   (sort (hash-keys common-http-status-codes&messages) <)))

(define (message-for-status-code code)
  (hash-ref common-http-status-codes&messages code #f))

(define DEFAULT-STATUS-MESSAGE #"OK")

(define (infer-response-message code message)
  (match message
    [(? bytes?)
     message]
    [else
     (match (message-for-status-code code)
       [(? string? s)
        (string->bytes/utf-8 s)]
       [else
        DEFAULT-STATUS-MESSAGE])]))