CS3700-project4/smol-http/util.rkt

45 lines
1.1 KiB
Racket

#lang racket/base
(provide http-request-method?
http-response-code?
http-response-code-name
->bytes
string->int)
(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"]
[400 . "Invalid Request"]
[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
(string->bytes/utf-8 (format "~a" x))))
(define (string->int s)
(define n (string->number s))
(and (exact-integer? n) n))
(module+ test
(let ([b #"hello"]) (check-eq? (->bytes b) b))
(check-equal? (->bytes 5) #"5"))