Opened 7 years ago

Closed 7 years ago

#1392 closed defect (fixed)

Ability to customise http-client headers for each request

Reported by: Caolan McMahon Owned by: sjamaan
Priority: major Milestone: someday
Component: extensions Version: 4.12.0
Keywords: http-client Cc:
Estimated difficulty:

Description

I propose adding a (prepare-request) parameter to the http-client egg. This allows me to add etag and if-modified-since headers to all (even redirected) requests made by http-client, based on the current request URI.

  • http-client.scm

     
    4343     basic-authenticator digest-authenticator
    4444     determine-username/password determine-proxy
    4545     determine-proxy-from-environment determine-proxy-username/password
    46      server-connector default-server-connector)
     46     server-connector default-server-connector
     47     prepare-request default-prepare-request)
    4748
    4849(import chicken scheme lolevel)
    4950(use srfi-1 srfi-13 srfi-18 srfi-69
     
    333334            (when (> (read-string! (string-length buf) buf port) 0)
    334335              (loop #f)))))))
    335336
    336 (define (add-headers req)
     337(define (default-prepare-request req)
    337338  (let* ((uri (request-uri req))
    338339         (cookies (get-cookies-for-uri (request-uri req)))
    339340         (h `(,@(if (not (null? cookies)) `((cookie . ,cookies)) '())
    340               (host ,(cons (uri-host uri) (and (not (uri-default-port? uri))
    341                                                (uri-port uri))))
    342341              ,@(if (and (client-software) (not (null? (client-software))))
    343342                    `((user-agent ,(client-software)))
    344343                    '()))))
     
    345344    (update-request req
    346345                    headers: (headers h (request-headers req)))))
    347346
     347(define prepare-request (make-parameter default-prepare-request))
     348
    348349(define (http-client-error loc msg args specific . rest)
    349350  (raise (make-composite-condition
    350351          (make-property-condition 'exn 'location loc 'message msg 'arguments args)
     
    587588    (let* ((uri (request-uri req))
    588589           (con (ensure-connection! uri)))
    589590      (condition-case
    590           (let* ((req (add-headers (update-request
    591                                     req port: (http-connection-outport con))))
     591          (let* ((req ((prepare-request)
     592                       (update-request
     593                        req
     594                        headers: (headers
     595                                  `((host ,(cons (uri-host uri)
     596                                                 (and (not (uri-default-port? uri))
     597                                                      (uri-port uri)))))
     598                                  (request-headers req))
     599                        port: (http-connection-outport con))))
    592600                 ;; No outgoing URIs should ever contain credentials or fragments
    593601                 (req-uri (update-uri uri fragment: #f username: #f password: #f))
    594602                 ;; RFC1945, 5.1.2: "The absoluteURI form is only allowed

Attachments (1)

prepare-request2.diff (2.5 KB) - added by Caolan McMahon 7 years ago.

Download all attachments as: .zip

Change History (2)

Changed 7 years ago by Caolan McMahon

Attachment: prepare-request2.diff added

comment:1 Changed 7 years ago by sjamaan

Resolution: fixed
Status: newclosed

Implemented in trunk, will be part of 0.13

Note: See TracTickets for help on using tickets.