This file is indexed.

/usr/share/racket/collects/net/http-client.rkt is in racket-common 6.7-3.

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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#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"
         "osx-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 [(list? ssl?)
           ;; At this point, we have a tunneled socket to the remote host/port: we do not need to
           ;; address it; ignore host-bs, only use port for conn-port-usual?
           (match-define (list ssl-ctx? (? input-port? t:from) (? output-port? t:to) abandon-p) ssl?)
           (set-http-conn-abandon-p! hc abandon-p)
           (set-http-conn-port-usual?! hc (or (and ssl-ctx? (= 443 port))
                                              (and (not ssl-ctx?) (= 80 port))))
           (values t:from t:to)]
          [ssl?
           (set-http-conn-port-usual?! hc (= 443 port))
           (cond
             [(osx-old-openssl?)
              ;; OpenSSL is either not available or too old; use
              ;; native OS X tools
              (set-http-conn-abandon-p! hc osx-ssl-abandon-port)
              (osx-ssl-connect host port ssl-version)]
             [(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 (done) (void))
    (define crlf-bytes (make-bytes 2))
    (let loop ([last-bytes #f])
      (define in-v (read-line ip eol-type))
      (cond
        [(eof-object? in-v)
         (done)]
        [else
         (define size-str (string-trim in-v))
         (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)
             (done)
             (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-CONNECT-tunnel proxy-host proxy-port target-host target-port #:ssl? [ssl? #f])
  (define hc (http-conn-open proxy-host #:port proxy-port #:ssl? #f))
  (define connect-string (format "~a:~a" target-host target-port))
  ; (log-net/url-info "http-conn-CONNECT-tunnel tunnel to ~s for ~s" connect-string (url->string url))
  (http-conn-send! hc #:method "CONNECT" connect-string #:headers
                   (list (format "Host: ~a" connect-string)
                         "Proxy-Connection: Keep-Alive"
                         "Connection: Keep-Alive"))

  (let ((tunnel-status (http-conn-status! hc))
        (tunnel-headers (http-conn-headers! hc)))
    (unless (regexp-match "^HTTP[^ ]* +2" tunnel-status)
      (error 'make-ports "HTTP CONNECT failed: ~a" tunnel-status)))

  ;; SSL secure the ports
  (match-define (http-conn _ _ _ t:to t:from _) hc)
  (cond [(not ssl?) ; it's just a tunnel... no ssl
         (define abandon-p (lambda (p) ((http-conn-abandon-p hc) p)))
         (values ssl? t:from t:to abandon-p)]

        [else ; ssl
          (define ssl-version (if (boolean? ssl?) 'auto ssl?))
          (set-http-conn-port-usual?! hc (= 443 target-port))
          ;; choose between win32 or non-win32 openssl here, then keep code common afterwards
          (define-values (p->ssl-ps ssl-abndn-p)
            (if (or ssl-available? (not win32-ssl-available?))
              (values ports->ssl-ports ssl-abandon-port)
              (values ports->win32-ssl-ports win32-ssl-abandon-port)))

          (define clt-ctx
            (match ssl-version
                   [(? ssl-client-context? ctx) ctx]
                   [(? symbol? protocol) (ssl-make-client-context protocol)]))

          (define-values (r:from r:to) (p->ssl-ps t:from t:to
                                                  #:mode 'connect
                                                  #:context clt-ctx
                                                  #:close-original? #t
                                                  #:hostname target-host))

          ;; The user of the tunnel relies on ports->ssl-ports' #:close-original? to close/abandon the
          ;; underlying ports of the tunnel itself. Therefore the abandon-p sent back to caller is the
          ;; ssl-abandon of the wrapped ports.
          (define abandon-p ssl-abndn-p)
          (values clt-ctx r:from r:to abandon-p)]))

(define (head? method-bss)
  (or (equal? method-bss #"HEAD")
      (equal? method-bss "HEAD")
      (equal? method-bss 'HEAD)))

(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-values (raw-response-port wait-for-close?)
    (cond
      [(head? method-bss) (values (open-input-bytes #"") #f)]
      [(regexp-member #rx#"^(?i:Transfer-Encoding: +chunked)$" headers)
       (values (http-conn-response-port/chunked! hc #:close? #t)
               #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)
         (values (http-conn-response-port/length! hc count #:close? close?)
                 close?))]
      [else
       (values (http-conn-response-port/rest! hc) #t)]))
  (define decoded-response-port
    (cond
      [(head? method-bss) 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)
          (when wait-for-close?
            ;; Wait for an EOF from the raw port before we
            ;; send an output on the decoding pipe:
            (copy-port raw-response-port (open-output-nowhere)))
          (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))
  (begin0 (http-conn-sendrecv! hc url-bs
                               #:version version-bs
                               #:method method-bss
                               #:headers headers-bs
                               #:data data
                               #:content-decode decodes
                               #:close? #t)
    (when (head? method-bss)
      (http-conn-close! hc))))

(define data-procedure/c
  (-> (-> (or/c bytes? string?) void?) any))

(define base-ssl?/c
  (or/c boolean? ssl-client-context? symbol?))

(define base-ssl?-tnl/c
  (or/c base-ssl?/c (list/c base-ssl?/c input-port? output-port? (-> port? void?))))

(provide
 data-procedure/c
 base-ssl?/c
 base-ssl?-tnl/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? base-ssl?-tnl/c
                #: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? base-ssl?-tnl/c
                #:port (between/c 1 65535))
        http-conn?)]
  [http-conn-CONNECT-tunnel
    (->* ((or/c bytes? string?)
          (between/c 1 65535)
          (or/c bytes? string?)
          (between/c 1 65535))
         (#:ssl? base-ssl?/c)
         (values base-ssl?/c input-port? output-port? (-> port? void?)))]
  [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? base-ssl?-tnl/c
                #: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?))]))