pre-srfi / http-1.1

HTTP/1.1 reader and writer
0 stars 1 forks source link

is (http-request-write) intended to be used with a list-accumulator? #3

Open wanderer opened 3 years ago

wanderer commented 3 years ago

Hello im trying to use http-request-write but I'm not sure how to use it. It does with a list-accumulator, but I get a list of bytevectors, one bv for the statusline and one for each header, then the body is appended as a list of Ints. I'm not sure if this is intended or not.

amirouche commented 3 years ago

@wanderer ping me later this week-end if I forget.

amirouche commented 3 years ago

Here is an example use of http-request-write:

(import (scheme base))
(import (arew network socket))
(import (arew network http))

(define (ref alist symbol)
  (let loop ((alist alist))
    (if (null? alist)
        (error "Oops")
        (if (symbol=? (caar alist) symbol)
            (cdar alist)
            (loop (cdr alist))))))

(define sock (socket 'inet 'stream 'ip))

(define-values (ok addressinfos) (getaddrinfo "httpbin.org" "http" #f))
(connect sock (ref (car addressinfos) 'address))

(define accumulator (socket-accumulator sock))
(define generator (socket-generator sock))

(http-request-write accumulator
                    "GET"
                    "http://httpbin.org/html"
                    '(1 . 1)
                    '(("Host" . "httpbin.org"))
                    "")

(call-with-values (lambda () (http-response-read generator)) pk)
(close sock)

Here is the definition socket-accumulator that returns the accumulator passed to http-request-write:

  (define (socket-accumulator fd)
    ;; There is no buffering.
    (lambda (something)
      (if (fixnum? something)
          (%write fd (bytevector something) 0 1)
          (let loop ((index 0))
            (unless (fx=? index (bytevector-length something))
              (let ((sent (%write fd something index (bytevector-length something))))
                (loop (fx+ index sent))))))))

That is the accumulator expect as argument:

I remember I added the ability to pass a single byte to the accumulator, that may be a mistake in http-request-write. What do you think?

Also prolly, we need a port->accumulator procedure.

wanderer commented 3 years ago

@amirouche just coming back around to this.

I remember I added the ability to pass a single byte to the accumulator, that may be a mistake in http-request-write. What do you think?

yeah I thought it was a bit weird that the headers where all bvs and then the body was written as individual bytes. I think it would be easier to use if we where dealing with a uniform type.

amirouche commented 3 years ago

yeah I thought it was a bit weird that the headers where all bvs and then the body was written as individual bytes. I think it would be easier to use if we where dealing with a uniform type.

You are right, I will rework the implementation to do what you say.

@wanderer I have beginning of plan to create a portable implementation of this library that is tested. What is your favorite(s) scheme implementation(s)?

wanderer commented 3 years ago

@amirouche im using gauche atm