@@ -96,6 +96,14 @@ void main(List<String> arguments) async {
9696 ..badCertificateCallback = (cert, host, port) =>
9797 trustedRoots.contains (String .fromCharCodes (cert.sha1));
9898
99+ // Allow SSL debugging
100+ if (config.sslKeyLogFile case final path? ) {
101+ final keyLog = File (path)
102+ ..createSync (recursive: true );
103+ client.keyLog = (line) =>
104+ keyLog.writeAsStringSync (line, mode: FileMode .append);
105+ }
106+
99107 // Apply HTTP proxy
100108 if (config.proxy case final Uri proxy) {
101109 final credentials = proxy.httpClientCredentials;
@@ -127,7 +135,8 @@ void main(List<String> arguments) async {
127135 response
128136 ..contentLength = 0
129137 ..statusCode = HttpStatus .ok
130- ..close ();
138+ ..close ()
139+ .ignore ();
131140 return ;
132141 }
133142
@@ -141,7 +150,8 @@ void main(List<String> arguments) async {
141150 ..headers.add (HttpHeaders .wwwAuthenticateHeader, 'Basic realm=Protected' )
142151 ..headers.contentType = ContentType .text
143152 ..write ('PROXY///ERROR///UNAUTHORIZED' )
144- ..close ();
153+ ..close ()
154+ .ignore ();
145155 return ;
146156 }
147157 }
@@ -159,11 +169,6 @@ void main(List<String> arguments) async {
159169 .then ((requestToRemote) async {
160170 requestToRemote.followRedirects = false ;
161171
162- // Remote server auth
163- final remoteBasicAuth = config.remote.basicAuth;
164- if (remoteBasicAuth != null )
165- requestToRemote.headers.add (HttpHeaders .authorizationHeader, remoteBasicAuth);
166-
167172 request.headers.forEach ((headerName, headerValues) {
168173 // Filter out headers
169174 if (! headersNotToForwardToRemote.contains (headerName)) {
@@ -181,14 +186,19 @@ void main(List<String> arguments) async {
181186 }
182187 });
183188
189+ // Remote server auth
190+ final remoteBasicAuth = config.remote.basicAuth;
191+ if (remoteBasicAuth != null )
192+ requestToRemote.headers.set (HttpHeaders .authorizationHeader, remoteBasicAuth);
193+
184194 // If there's content pipe request body
185195 if (request.contentLength > 0 )
186196 await requestToRemote.addStream (request);
187197
188198 return requestToRemote.close ();
189199 })
190200 .then (
191- (remoteResponse) async {
201+ (remoteResponse) {
192202 stdout.writeln ('[$requestId ] Remote response: ${remoteResponse .statusCode }' );
193203 remoteResponse.headers.forEach ((headerName, headerValues) {
194204 // Filter out headers
@@ -240,8 +250,10 @@ void main(List<String> arguments) async {
240250 ..headers.contentType = ContentType .text
241251 ..writeln ('PROXY///ERROR///INTERNAL' )
242252 ..write (error)
243- ..close ();
253+ ..close ()
254+ .ignore ();
244255 },
245- );
256+ )
257+ .ignore ();
246258 });
247259}
0 commit comments