initial commit. tentatively implements response header parsing.
This commit is contained in:
43
gem300.rkt
Normal file
43
gem300.rkt
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
#lang racket
|
||||||
|
|
||||||
|
(require openssl)
|
||||||
|
(require net/url)
|
||||||
|
|
||||||
|
(define (request url-str)
|
||||||
|
(define url (string->url url-str))
|
||||||
|
(define-values (c-in c-out)
|
||||||
|
(ssl-connect (url-host url)
|
||||||
|
(or (url-port url) 1965)))
|
||||||
|
|
||||||
|
(write-string url-str c-out)
|
||||||
|
(write-string "\r\n" c-out)
|
||||||
|
|
||||||
|
(define-values (status header)
|
||||||
|
(read-response c-in))
|
||||||
|
|
||||||
|
(println status)
|
||||||
|
(println header))
|
||||||
|
|
||||||
|
(define (read-response (c-in (current-input-port)))
|
||||||
|
(define maxlen 1027)
|
||||||
|
|
||||||
|
(let ([header (peek-string maxlen 0 c-in)])
|
||||||
|
|
||||||
|
(if (not (string-contains? header "\r\n"))
|
||||||
|
(error "header exceeds maximum length")
|
||||||
|
|
||||||
|
(let ([header (read-line c-in 'return-linefeed)])
|
||||||
|
(define-values (status meta)
|
||||||
|
(let ([status-meta (string-split header " ")])
|
||||||
|
(values (list-ref status-meta 0)
|
||||||
|
(list-ref status-meta 1))))
|
||||||
|
|
||||||
|
(cond
|
||||||
|
[(> (string-length status) 2)
|
||||||
|
(error "status code exceeds maximum length")]
|
||||||
|
|
||||||
|
[(andmap (compose not char-numeric?) (string->list status))
|
||||||
|
(error "status code is not numeric")]
|
||||||
|
|
||||||
|
[else
|
||||||
|
(values (string->number status) meta)])))))
|
Reference in New Issue
Block a user