Reverse proxy in racket

Andrea Giardina <[email protected]>
Newsgroups gmane.comp.lang.racket.user,gmane.lisp.scheme.plt
Message-ID <CAMO+nHu5AhVJwvDc9Hpc_8rg+B409vS5r8x1r=ois1JgbtyzQg@mail.gmail.com>
Hi all,
I'd need to write a very simple reverse proxy and I'm wondering what's
the best way to do it in racket. I don't have any requirements but
performance (not even security and reliability, since I'm writing
basically a testing tool).

I came out with this solution (using http-sendrecv/url). It  seems
working properly for everything but redirects:

(define (handle-file-request req)
    (define uri (request-uri req))
    (define method (request-method req))
    (define post-data (request-post-data/raw req))
    (define proxy-headers
        (map
        (lambda (h) (bytes-append (header-field h) #": " (header-value h)))
        (filter (lambda (h) (not (bytes=? (header-field h) #"Host"))) headers)))

    (let-values
        ([(status headers in)
            (http-sendrecv/url
              (struct-copy url uri [scheme remote-scheme)] [host
remote-host] [port remote-port])
              #:method method
              #:headers proxy-headers
              #:data post-data)])

        (let ([mime (findf identity (map (lambda (h) (extract-field
#"content-type" h)) headers))])
          (response
            200 #"OK"
            (current-seconds) (if mime mime #"text/javascript; charset=UTF-8")
            empty
            (λ (client-out) (write-bytes (port->bytes in) client-out)))))


Do you think is this one the right way or do you think there is a
better (cleaner/more performing) approach? Any idea about how to
manage redirects if not manually?

Thanks,
Andrea

-- 
You received this message because you are subscribed to the Google Groups "Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
For more options, visit https://groups.google.com/d/optout.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.