/usr/share/racket/collects/net/http-client.rkt is in racket-common 6.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 | #lang racket/base
(require racket/contract/base
racket/match
racket/list
racket/string
racket/port
(rename-in racket/tcp
[tcp-connect plain-tcp-connect]
[tcp-abandon-port plain-tcp-abandon-port])
openssl
"win32-ssl.rkt"
file/gunzip)
(define tolerant? #t)
(define eol-type
(if tolerant?
'any
'return-linefeed))
;; Lib
(define (->string bs)
(if (bytes? bs)
(bytes->string/utf-8 bs)
bs))
(define (->bytes str)
(cond
[(string? str)
(string->bytes/utf-8 str)]
[(not str)
#""]
[else
str]))
(define (read-bytes-line/not-eof ip kind)
(define bs (read-bytes-line ip kind))
(when (eof-object? bs)
(error 'http-client "Connection ended early"))
bs)
(define (regexp-member rx l)
(ormap (λ (h) (regexp-match rx h)) l))
(define PIPE-SIZE 4096)
;; Core
(struct http-conn (host port port-usual? to from abandon-p) #:mutable)
(define (make-http-conn)
(http-conn #f #f #f #f #f #f))
(define (http-conn-live? hc)
(and (http-conn-to hc)
(http-conn-from hc)
#t))
(define (http-conn-open! hc host-bs #:ssl? [ssl? #f] #:port [port (if ssl? 443 80)])
(http-conn-close! hc)
(define host (->string host-bs))
(define ssl-version (if (boolean? ssl?) 'auto ssl?))
(define-values (from to)
(cond [ssl?
(set-http-conn-port-usual?! hc (= 443 port))
(cond
[(or ssl-available? (not win32-ssl-available?))
(set-http-conn-abandon-p! hc ssl-abandon-port)
(ssl-connect host port ssl-version)]
[else
(set-http-conn-abandon-p! hc win32-ssl-abandon-port)
(win32-ssl-connect host port ssl-version)])]
[else
(set-http-conn-abandon-p! hc plain-tcp-abandon-port)
(set-http-conn-port-usual?! hc (= 80 port))
(plain-tcp-connect host port)]))
(set-http-conn-host! hc host)
(set-http-conn-port! hc port)
;; (define-values (log-i log-o) (make-pipe))
;; (thread (λ () (copy-port log-i to (current-error-port))))
(set-http-conn-to! hc to)
(set-http-conn-from! hc from))
(define (http-conn-close! hc)
(match-define (http-conn host port port-usual? to from abandon) hc)
(set-http-conn-host! hc #f)
(when to
(close-output-port to)
(set-http-conn-to! hc #f))
(when from
(close-input-port from)
(set-http-conn-from! hc #f))
(set-http-conn-abandon-p! hc #f))
(define (http-conn-abandon! hc)
(match-define (http-conn host port port-usual? to from abandon) hc)
(when to
(abandon to)
(set-http-conn-to! hc #f)))
(define (write-chunk out data)
(let ([bytes (->bytes data)])
(define len (bytes-length bytes))
(unless (zero? len)
(fprintf out "~x\r\n~a\r\n" len bytes))))
(define (http-conn-send! hc url-bs
#:version [version-bs #"1.1"]
#:method [method-bss #"GET"]
#:close? [close? #f]
#:headers [headers-bs empty]
#:content-decode [decodes '(gzip)]
#:data [data #f])
(match-define (http-conn host port port-usual? to from _) hc)
(fprintf to "~a ~a HTTP/~a\r\n" method-bss url-bs version-bs)
(unless (regexp-member #rx"^(?i:Host:) +.+$" headers-bs)
(fprintf to "Host: ~a\r\n"
(if port-usual?
host
(format "~a:~a" host port))))
(unless (regexp-member #rx"^(?i:User-Agent:) +.+$" headers-bs)
(fprintf to "User-Agent: Racket/~a (net/http-client)\r\n"
(version)))
(unless (or (not (memq 'gzip decodes))
(regexp-member #rx"^(?i:Accept-Encoding:) +.+$" headers-bs))
(fprintf to "Accept-Encoding: gzip\r\n"))
(define body (->bytes data))
(cond [(procedure? body)
(fprintf to "Transfer-Encoding: chunked\r\n")]
[(and body
(not (regexp-member #rx"^(?i:Content-Length:) +.+$" headers-bs)))
(fprintf to "Content-Length: ~a\r\n" (bytes-length body))])
(when close?
(unless (regexp-member #rx"^(?i:Connection:) +.+$" headers-bs)
(fprintf to "Connection: close\r\n")))
(for ([h (in-list headers-bs)])
(fprintf to "~a\r\n" h))
(fprintf to "\r\n")
(cond [(procedure? body)
(body (λ (data) (write-chunk to data)))
(fprintf to "0\r\n\r\n")]
[body (display body to)])
(flush-output to))
(define (http-conn-status! hc)
(read-bytes-line/not-eof (http-conn-from hc) eol-type))
(define (http-conn-headers! hc)
(define top (read-bytes-line/not-eof (http-conn-from hc) eol-type))
(if (bytes=? top #"")
empty
(cons top (http-conn-headers! hc))))
(define BUFFER-SIZE 1024)
(define (copy-bytes in out count)
(define buffer (make-bytes BUFFER-SIZE))
(let loop ([count count])
(when (positive? count)
(define r
(read-bytes-avail! buffer in 0
(if (< count BUFFER-SIZE)
count
BUFFER-SIZE)))
(unless (eof-object? r)
(write-bytes buffer out 0 r)
(loop (- count r))))))
(define (http-conn-response-port/rest! hc)
(http-conn-response-port/length! hc +inf.0 #:close? #t))
(define (http-conn-response-port/length! hc count #:close? [close? #f])
(define-values (in out) (make-pipe PIPE-SIZE))
(thread
(λ ()
(copy-bytes (http-conn-from hc) out count)
(when close?
(http-conn-close! hc))
(close-output-port out)))
in)
(define (http-conn-response-port/chunked! hc #:close? [close? #f])
(define (http-pipe-chunk ip op)
(define crlf-bytes (make-bytes 2))
(let loop ([last-bytes #f])
(define size-str (string-trim (read-line ip eol-type)))
(define chunk-size (string->number size-str 16))
(unless chunk-size
(error 'http-conn-response/chunked
"Could not parse ~S as hexadecimal number"
size-str))
(define use-last-bytes?
(and last-bytes (<= chunk-size (bytes-length last-bytes))))
(if (zero? chunk-size)
(begin (flush-output op)
(close-output-port op))
(let* ([bs (if use-last-bytes?
(begin
(read-bytes! last-bytes ip 0 chunk-size)
last-bytes)
(read-bytes chunk-size ip))]
[crlf (read-bytes! crlf-bytes ip 0 2)])
(write-bytes bs op 0 chunk-size)
(loop bs)))))
(define-values (in out) (make-pipe PIPE-SIZE))
(define chunk-t
(thread
(λ ()
(http-pipe-chunk (http-conn-from hc) out))))
(thread
(λ ()
(thread-wait chunk-t)
(when close?
(http-conn-close! hc))
(close-output-port out)))
in)
;; Derived
(define (http-conn-open host-bs #:ssl? [ssl? #f] #:port [port (if ssl? 443 80)])
(define hc (make-http-conn))
(http-conn-open! hc host-bs #:ssl? ssl? #:port port)
hc)
(define (http-conn-recv! hc
#:method [method-bss #"GET"]
#:content-decode [decodes '(gzip)]
#:close? [iclose? #f])
(define status (http-conn-status! hc))
(define headers (http-conn-headers! hc))
(define close?
(or iclose?
(regexp-member #rx#"^(?i:Connection: +close)$" headers)))
(when close?
(http-conn-abandon! hc))
(define head?
(or (equal? method-bss #"HEAD")
(equal? method-bss "HEAD")
(equal? method-bss 'HEAD)))
(define raw-response-port
(cond
[head? (open-input-bytes #"")]
[(regexp-member #rx#"^(?i:Transfer-Encoding: +chunked)$" headers)
(http-conn-response-port/chunked! hc #:close? #t)]
[(ormap (λ (h)
(match (regexp-match #rx#"^(?i:Content-Length:) +(.+)$" h)
[#f #f]
[(list _ cl-bs)
(string->number
(bytes->string/utf-8 cl-bs))]))
headers)
=>
(λ (count)
(http-conn-response-port/length! hc count #:close? close?))]
[else
(http-conn-response-port/rest! hc)]))
(define decoded-response-port
(cond
[head? raw-response-port]
[(and (memq 'gzip decodes)
(regexp-member #rx#"^(?i:Content-Encoding: +gzip)$" headers)
(not (eof-object? (peek-byte raw-response-port))))
(define-values (in out) (make-pipe PIPE-SIZE))
(define gunzip-t
(thread
(λ ()
(gunzip-through-ports raw-response-port out))))
(thread
(λ ()
(thread-wait gunzip-t)
(close-output-port out)))
in]
[else
raw-response-port]))
(values status headers decoded-response-port))
(define (http-conn-sendrecv! hc url-bs
#:version [version-bs #"1.1"]
#:method [method-bss #"GET"]
#:headers [headers-bs empty]
#:data [data #f]
#:content-decode [decodes '(gzip)]
#:close? [close? #f])
(http-conn-send! hc url-bs
#:version version-bs
#:method method-bss
#:close? close?
#:headers headers-bs
#:content-decode decodes
#:data data)
(http-conn-recv! hc
#:method method-bss
#:content-decode decodes
#:close? close?))
(define (http-sendrecv host-bs url-bs
#:ssl? [ssl? #f]
#:port [port (if ssl? 443 80)]
#:version [version-bs #"1.1"]
#:method [method-bss #"GET"]
#:headers [headers-bs empty]
#:data [data #f]
#:content-decode [decodes '(gzip)])
(define hc (http-conn-open host-bs #:ssl? ssl? #:port port))
(http-conn-sendrecv! hc url-bs
#:version version-bs
#:method method-bss
#:headers headers-bs
#:data data
#:content-decode decodes
#:close? #t))
(define data-procedure/c
(-> (-> (or/c bytes? string?) void?) any))
(provide
data-procedure/c
(contract-out
[http-conn?
(-> any/c
boolean?)]
[http-conn-live?
(-> any/c
boolean?)]
[rename
make-http-conn http-conn
(-> http-conn?)]
[http-conn-open!
(->* (http-conn? (or/c bytes? string?))
(#:ssl? (or/c boolean? ssl-client-context? symbol?)
#:port (between/c 1 65535))
void?)]
[http-conn-close!
(-> http-conn? void?)]
[http-conn-abandon!
(-> http-conn? void?)]
[http-conn-send!
(->*
(http-conn-live? (or/c bytes? string?))
(#:version (or/c bytes? string?)
#:method (or/c bytes? string? symbol?)
#:close? boolean?
#:headers (listof (or/c bytes? string?))
#:content-decode (listof symbol?)
#:data (or/c false/c bytes? string? data-procedure/c))
void)]
;; Derived
[http-conn-open
(->* ((or/c bytes? string?))
(#:ssl? (or/c boolean? ssl-client-context? symbol?)
#:port (between/c 1 65535))
http-conn?)]
[http-conn-recv!
(->* (http-conn-live?)
(#:content-decode (listof symbol?)
#:method (or/c bytes? string? symbol?)
#:close? boolean?)
(values bytes? (listof bytes?) input-port?))]
[http-conn-sendrecv!
(->* (http-conn-live? (or/c bytes? string?))
(#:version (or/c bytes? string?)
#:method (or/c bytes? string? symbol?)
#:headers (listof (or/c bytes? string?))
#:data (or/c false/c bytes? string? data-procedure/c)
#:content-decode (listof symbol?)
#:close? boolean?)
(values bytes? (listof bytes?) input-port?))]
[http-sendrecv
(->* ((or/c bytes? string?) (or/c bytes? string?))
(#:ssl? (or/c boolean? ssl-client-context? symbol?)
#:port (between/c 1 65535)
#:version (or/c bytes? string?)
#:method (or/c bytes? string? symbol?)
#:headers (listof (or/c bytes? string?))
#:data (or/c false/c bytes? string? data-procedure/c)
#:content-decode (listof symbol?))
(values bytes? (listof bytes?) input-port?))]))
|