made request ready for zeps
This commit is contained in:
32
request.zp
32
request.zp
@@ -4,6 +4,7 @@
|
||||
(list "set-default-headers!" set-default-headers)
|
||||
(list "set-follow-redirects!" set-follow-redirects)
|
||||
(list "request" request)
|
||||
(list "head" head)
|
||||
(list "get" get)
|
||||
(list "put" put)
|
||||
(list "post" post)
|
||||
@@ -55,7 +56,7 @@
|
||||
(let* ((body (req :body))
|
||||
(split (string:split body "\r\n"))
|
||||
(num (car split))
|
||||
(size (string->number (++ "#x" num)))
|
||||
(size (string->number (++ "#x" (if (truthy? num) num "0"))))
|
||||
(diff (- size (- (length body) (+ (length num) 4)))))
|
||||
(if (eq? size 0)
|
||||
(end-request req sock)
|
||||
@@ -71,13 +72,13 @@
|
||||
(next (substring next (+ (length num) 2) (length next)))
|
||||
(size (+ (string->number (++ "#x" num)) 2)))
|
||||
(if (eq? size 2)
|
||||
(end-request req sock)
|
||||
(end-request (hash:set req :body (++ acc chunk)) sock)
|
||||
(loop (++ acc chunk) next size)))
|
||||
(loop acc
|
||||
(++ cur (byte-vector->string (net:recv sock (make-small (- size (length cur))))))
|
||||
size)))))))
|
||||
|
||||
(request (lambda (scheme path . req-headers)
|
||||
(request (lambda (scheme path opts)
|
||||
(let* ((path (if (string:starts-with path "http://") (substring path 7 (length path)) path))
|
||||
(path (if (string:starts-with path "https://") (substring path 8 (length path)) path))
|
||||
(split (string:split path #\/))
|
||||
@@ -86,12 +87,14 @@
|
||||
(split (string:split host+port #\:))
|
||||
(host (car split))
|
||||
(port (get-from split 1 "80"))
|
||||
(req-headers (get-from req-headers 0 DFLT-HEADERS))
|
||||
(req-headers (get-from opts :headers DFLT-HEADERS))
|
||||
(body (get-from opts :body ""))
|
||||
(headers (hash:kv-reduce
|
||||
(lambda (acc kv) (++ acc (->string (car kv)) ": " (->string (cadr kv)) "\r\n"))
|
||||
""
|
||||
req-headers))
|
||||
(text (++ scheme " /" location " HTTP/1.1\r\nHost: " host+port "\r\n" headers "\r\n\r\n"))
|
||||
(text (++ scheme " /" location " HTTP/1.1\r\nHost: " host+port "\r\n" headers "\r\n\r\n"
|
||||
(if (truthy? body) (++ body "\r\n\r\n") body)))
|
||||
(sock (net:socket "stream")))
|
||||
(begin
|
||||
(net:connect sock (net:get-addr-info host port))
|
||||
@@ -115,14 +118,17 @@
|
||||
(parse-request (byte-vector->string (++ bytes recvd)) scheme path req-headers #t sock)
|
||||
(loop (net:recv sock (min BSIZE (- len cur))) (++ bytes recvd) ncur len)))))))))
|
||||
|
||||
(get (lambda (path . headers)
|
||||
(request "GET" path (get-from headers 0 DFLT-HEADERS))))
|
||||
(head (lambda (path . opts)
|
||||
(request "HEAD" path (get-from opts 0 #{}))))
|
||||
|
||||
(post (lambda (path . headers)
|
||||
(request "POST" path) (get-from headers 0 DFLT-HEADERS)))
|
||||
(get (lambda (path . opts)
|
||||
(request "GET" path (get-from opts 0 #{}))))
|
||||
|
||||
(put (lambda (path . headers)
|
||||
(request "PUT" path) (get-from headers 0 DFLT-HEADERS)))
|
||||
(post (lambda (path . opts)
|
||||
(request "POST" path (get-from opts 0 #{}))))
|
||||
|
||||
(delete (lambda (path . headers)
|
||||
(request "DELETE" path) (get-from headers 0 DFLT-HEADERS))))
|
||||
(put (lambda (path . opts)
|
||||
(request "PUT" path (get-from opts 0 #{}))))
|
||||
|
||||
(delete (lambda (path . opts)
|
||||
(request "DELETE" path (get-from opts 0 #{})))))
|
||||
|
Reference in New Issue
Block a user