made request ready for zeps
This commit is contained in:
15
module.zp
Normal file
15
module.zp
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#{:name "request"
|
||||||
|
:version "1.0.0"
|
||||||
|
:license "GPLv2"
|
||||||
|
:tests []
|
||||||
|
:type :library
|
||||||
|
:author #{:name "Veit Heller"
|
||||||
|
:email "veit@veitheller.de"
|
||||||
|
:github "@hellerve"}
|
||||||
|
:location #{:github "hellerve/request"
|
||||||
|
:zpr "request"}
|
||||||
|
:zepto #{:version :any
|
||||||
|
:ez #f
|
||||||
|
:bz #f}
|
||||||
|
:dependencies ()
|
||||||
|
:dev-dependencies ()}
|
32
request.zp
32
request.zp
@@ -4,6 +4,7 @@
|
|||||||
(list "set-default-headers!" set-default-headers)
|
(list "set-default-headers!" set-default-headers)
|
||||||
(list "set-follow-redirects!" set-follow-redirects)
|
(list "set-follow-redirects!" set-follow-redirects)
|
||||||
(list "request" request)
|
(list "request" request)
|
||||||
|
(list "head" head)
|
||||||
(list "get" get)
|
(list "get" get)
|
||||||
(list "put" put)
|
(list "put" put)
|
||||||
(list "post" post)
|
(list "post" post)
|
||||||
@@ -55,7 +56,7 @@
|
|||||||
(let* ((body (req :body))
|
(let* ((body (req :body))
|
||||||
(split (string:split body "\r\n"))
|
(split (string:split body "\r\n"))
|
||||||
(num (car split))
|
(num (car split))
|
||||||
(size (string->number (++ "#x" num)))
|
(size (string->number (++ "#x" (if (truthy? num) num "0"))))
|
||||||
(diff (- size (- (length body) (+ (length num) 4)))))
|
(diff (- size (- (length body) (+ (length num) 4)))))
|
||||||
(if (eq? size 0)
|
(if (eq? size 0)
|
||||||
(end-request req sock)
|
(end-request req sock)
|
||||||
@@ -71,13 +72,13 @@
|
|||||||
(next (substring next (+ (length num) 2) (length next)))
|
(next (substring next (+ (length num) 2) (length next)))
|
||||||
(size (+ (string->number (++ "#x" num)) 2)))
|
(size (+ (string->number (++ "#x" num)) 2)))
|
||||||
(if (eq? size 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 chunk) next size)))
|
||||||
(loop acc
|
(loop acc
|
||||||
(++ cur (byte-vector->string (net:recv sock (make-small (- size (length cur))))))
|
(++ cur (byte-vector->string (net:recv sock (make-small (- size (length cur))))))
|
||||||
size)))))))
|
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))
|
(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))
|
(path (if (string:starts-with path "https://") (substring path 8 (length path)) path))
|
||||||
(split (string:split path #\/))
|
(split (string:split path #\/))
|
||||||
@@ -86,12 +87,14 @@
|
|||||||
(split (string:split host+port #\:))
|
(split (string:split host+port #\:))
|
||||||
(host (car split))
|
(host (car split))
|
||||||
(port (get-from split 1 "80"))
|
(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
|
(headers (hash:kv-reduce
|
||||||
(lambda (acc kv) (++ acc (->string (car kv)) ": " (->string (cadr kv)) "\r\n"))
|
(lambda (acc kv) (++ acc (->string (car kv)) ": " (->string (cadr kv)) "\r\n"))
|
||||||
""
|
""
|
||||||
req-headers))
|
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")))
|
(sock (net:socket "stream")))
|
||||||
(begin
|
(begin
|
||||||
(net:connect sock (net:get-addr-info host port))
|
(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)
|
(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)))))))))
|
(loop (net:recv sock (min BSIZE (- len cur))) (++ bytes recvd) ncur len)))))))))
|
||||||
|
|
||||||
(get (lambda (path . headers)
|
(head (lambda (path . opts)
|
||||||
(request "GET" path (get-from headers 0 DFLT-HEADERS))))
|
(request "HEAD" path (get-from opts 0 #{}))))
|
||||||
|
|
||||||
(post (lambda (path . headers)
|
(get (lambda (path . opts)
|
||||||
(request "POST" path) (get-from headers 0 DFLT-HEADERS)))
|
(request "GET" path (get-from opts 0 #{}))))
|
||||||
|
|
||||||
(put (lambda (path . headers)
|
(post (lambda (path . opts)
|
||||||
(request "PUT" path) (get-from headers 0 DFLT-HEADERS)))
|
(request "POST" path (get-from opts 0 #{}))))
|
||||||
|
|
||||||
(delete (lambda (path . headers)
|
(put (lambda (path . opts)
|
||||||
(request "DELETE" path) (get-from headers 0 DFLT-HEADERS))))
|
(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