[web-devel] http-enumerator connecting via a http proxy

Erik de Castro Lopo mle+hs at mega-nerd.com
Sat Jun 25 14:28:30 CEST 2011

Erik de Castro Lopo wrote:

> Michael Snoyman wrote:
> > If I were to take a stab at the best approach, it would be to modify
> > withSslConn to (optionally) read in the HTTP response line from the proxy
> > before handing control off to TLS. I'm not sure I entirely understand the
> > issue, but does that seem like a plausible approach?
> Yes it does. I'll investigate it.

Ok, after a long break, I'm back working on this. Starting with the
example of the withSslConn function I am working on the following
function for the case of an SSL connection through a proxy:

    withSslProxyConn :: MonadIO m
                => ([X509] -> IO TLS.TLSCertificateUsage)
                -> S8.ByteString -- ^ Target host
                -> Int -- ^ Target port
                -> Manager
                -> String -- ^ Proxy host
                -> Int -- ^ Proxy port
                -> Enumerator Blaze.Builder m () -- ^ request
                -> Enumerator S.ByteString m a -- ^ response
    withSslProxyConn checkCert thost tport man phost pport =
        withManagedConn man (phost, pport, True) $
            doConnect >>= TLS.sslClientConn checkCert
        doConnect = do
            h <- connectTo phost (PortNumber $ fromIntegral pport)
            -- Need to send connectRequest and get a HTTP 200 response.
            return h
        connectRequest =
            Blaze.fromByteString "CONNECT "
                `mappend` Blaze.fromByteString thost
                `mappend` Blaze.fromByteString (S8.pack (':' : show tport))
                `mappend` Blaze.fromByteString " HTTP/1.1\r\n\r\n"

The problem is that I'm new to iteratee based coding techniques and I
figure out how to complete the doConnect function. Basically it should

   a) send the connectRequest 
   b) check the HTTP repsonse
   c) if the response is 200 return the handle, otherwise throw an error
Can someone please give me a nudge in the right direction?

Erik de Castro Lopo

More information about the web-devel mailing list