From a4c631332faaf7506f8cc9443c22deb7db2b4335 Mon Sep 17 00:00:00 2001 From: Peter Bex Date: Tue, 15 Mar 2022 08:08:46 +0100 Subject: Try to determine multipart content length for string ports This makes it slightly more reliable for certain servers which absolutely require content length. An alternative would be to have an explicit API for sending static string contents. This could be in addition to this approach, because this should always be a net win. --- http-client.scm | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to 'http-client.scm') diff --git a/http-client.scm b/http-client.scm index 1eef99d..e9c181b 100644 --- a/http-client.scm +++ b/http-client.scm @@ -756,6 +756,14 @@ entry)) entries)) +(define (maybe-string-port-length port) + (and (eq? (##sys#slot port 7) 'string) ; type + (let ((size (##sys#slot port 11)) + (string (##sys#slot port 12))) + (assert (integer? size)) ; Check our assumptions; this is pretty unsafe code + (assert (string? string)) + size))) + (define (calculate-chunk-size entries) (call/cc (lambda (return) @@ -763,12 +771,16 @@ (fold (lambda (chunk total-size) (if (pair? chunk) (if (eq? 'port (car chunk)) + (let ((str-len (maybe-string-port-length (cdr chunk)))) + (if str-len + (+ total-size str-len) + ;; We can't calculate port lengths + ;; for non-string-ports. Let's just + ;; punt and hope the server won't + ;; return "411 Length Required"... + ;; (TODO: maybe try seeking it?) + (return #f))) ;; Should be a file otherwise. - ;; We can't calculate port lengths. - ;; Let's just punt and hope the server - ;; won't return "411 Length Required"... - ;; (TODO: maybe try seeking it?) - (return #f) (+ total-size (file-size (cdr chunk)))) (+ total-size (string-length chunk)))) total-size -- cgit v1.2.3