@@ -111,13 +111,18 @@ export class HttpsProxySocket {
111111 cb ( err , null ) ;
112112 }
113113
114+ const END_OF_HEADERS = '\r\n\r\n' ;
115+
114116 function ondata ( b : Buffer ) {
115117 buffers . push ( b ) ;
116118 buffersLength += b . length ;
117- var buffered = Buffer . concat ( buffers , buffersLength ) ;
118- var str = buffered . toString ( 'ascii' ) ;
119119
120- if ( ! ~ str . indexOf ( '\r\n\r\n' ) ) {
120+ // Headers (including URLs) are generally ISO-8859-1 or ASCII.
121+ // The subset used by an HTTPS proxy should always be safe as ASCII.
122+ const buffered = Buffer . concat ( buffers , buffersLength ) ;
123+ const str = buffered . toString ( 'ascii' ) ;
124+
125+ if ( str . indexOf ( END_OF_HEADERS ) < 0 ) {
121126 // keep buffering
122127 debug ( 'have not received end of HTTP headers yet...' ) ;
123128 if ( socket . read ) {
@@ -128,16 +133,16 @@ export class HttpsProxySocket {
128133 return ;
129134 }
130135
131- var firstLine = str . substring ( 0 , str . indexOf ( '\r\n' ) ) ;
132- var statusCode = + firstLine . split ( ' ' ) [ 1 ] ;
136+ const firstLine = str . substring ( 0 , str . indexOf ( '\r\n' ) ) ;
137+ const statusCode = parseInt ( firstLine . split ( ' ' ) [ 1 ] , 10 ) ;
133138 debug ( 'got proxy server response: %o' , firstLine ) ;
134139
135140 if ( 200 == statusCode ) {
136141 // 200 Connected status code!
137- var sock = socket ;
142+ const sock = socket ;
138143
139144 // nullify the buffered data since we won't be needing it
140- buffers = buffered = null ;
145+ buffers = null ;
141146
142147 cleanup ( ) ;
143148 cb ( null , sock ) ;
@@ -148,7 +153,7 @@ export class HttpsProxySocket {
148153 cleanup ( ) ;
149154
150155 // nullify the buffered data since we won't be needing it
151- buffers = buffered = null ;
156+ buffers = null ;
152157
153158 cleanup ( ) ;
154159 socket . end ( ) ;
0 commit comments