CS3700-project4/smol-http/util.rkt

39 lines
934 B
Racket
Raw Normal View History

#lang racket/base
(provide http-request-method?
http-response-code?
http-response-code-name
->bytes)
(module+ test (require rackunit))
;; ---------------------------------------------------------------------------------------
;; any? -> boolean?
(define (http-request-method? x)
(memq x '(GET POST)))
(define http-status-codes
#hasheqv([200 . "OK"]
[301 . "Moved Permanently"]
[403 . "Forbidden"]
[404 . "Not Found"]
[500 . "Internal Server Error"]))
;; any? -> boolean?
(define (http-response-code? x)
(hash-has-key? http-status-codes x))
;; http-status-code? -> string?
(define (http-response-code-name c)
(hash-ref http-status-codes c))
;; any? -> bytes?
(define (->bytes x)
(if (bytes? x)
x
2020-04-10 23:23:14 +00:00
(string->bytes/utf-8 (format "~a" x))))
(module+ test
(let ([b #"hello"]) (check-eq? (->bytes b) b))
(check-equal? (->bytes 5) #"5"))