@@ -2,7 +2,7 @@ use async_std::io::{self, Read};
22use async_std:: prelude:: * ;
33use async_std:: task:: { Context , Poll } ;
44use http_types:: format_err;
5- use http_types:: Request ;
5+ use http_types:: { Method , Request } ;
66
77use std:: pin:: Pin ;
88
@@ -41,6 +41,17 @@ impl Encoder {
4141 url. push_str ( query) ;
4242 }
4343
44+ // A client sending a CONNECT request MUST consists of only the host
45+ // name and port number of the tunnel destination, separated by a colon.
46+ // See: https://tools.ietf.org/html/rfc7231#section-4.3.6
47+ if req. method ( ) == Method :: Connect {
48+ let host = req. url ( ) . host_str ( ) ;
49+ let host = host. ok_or_else ( || format_err ! ( "Missing hostname" ) ) ?;
50+ let port = req. url ( ) . port_or_known_default ( ) ;
51+ let port = port. ok_or_else ( || format_err ! ( "Missing port" ) ) ?;
52+ url = format ! ( "{}:{}" , host, port) ;
53+ }
54+
4455 let val = format ! ( "{} {} HTTP/1.1\r \n " , req. method( ) , url) ;
4556 log:: trace!( "> {}" , & val) ;
4657 buf. write_all ( val. as_bytes ( ) ) . await ?;
@@ -58,6 +69,13 @@ impl Encoder {
5869 log:: trace!( "> {}" , & val) ;
5970 buf. write_all ( val. as_bytes ( ) ) . await ?;
6071
72+ // Insert Proxy-Connection header when method is CONNECT
73+ if req. method ( ) == Method :: Connect {
74+ let val = "proxy-connection: keep-alive\r \n " . to_owned ( ) ;
75+ log:: trace!( "> {}" , & val) ;
76+ buf. write_all ( val. as_bytes ( ) ) . await ?;
77+ }
78+
6179 // If the body isn't streaming, we can set the content-length ahead of time. Else we need to
6280 // send all items in chunks.
6381 if let Some ( len) = req. len ( ) {
0 commit comments