@@ -351,20 +351,10 @@ bool Socket::socks5_handshake() {
351
351
}
352
352
353
353
bool Socket::http_proxy_handshake () {
354
- #define HTTP_PROXY_FMT \
355
- " CONNECT %.*s:%d HTTP/1.1\r\n " \
356
- " Host: %.*s:%d\r\n " \
357
- " User-Agent: Swoole/" SWOOLE_VERSION " \r\n " \
358
- " Proxy-Connection: Keep-Alive\r\n "
359
-
360
- // CONNECT
361
- int n;
362
- const char *host = http_proxy->target_host .c_str ();
363
- int host_len = http_proxy->target_host .length ();
354
+ const std::string *real_host = &http_proxy->target_host ;
364
355
#ifdef SW_USE_OPENSSL
365
356
if (ssl_context && !ssl_context->tls_host_name .empty ()) {
366
- host = ssl_context->tls_host_name .c_str ();
367
- host_len = ssl_context->tls_host_name .length ();
357
+ real_host = &ssl_context->tls_host_name ;
368
358
}
369
359
#endif
370
360
@@ -373,34 +363,11 @@ bool Socket::http_proxy_handshake() {
373
363
send_buffer->clear ();
374
364
};
375
365
376
- if (!http_proxy->password .empty ()) {
377
- auto auth_str = http_proxy->get_auth_str ();
378
- n = sw_snprintf (send_buffer->str ,
379
- send_buffer->size ,
380
- HTTP_PROXY_FMT " Proxy-Authorization: Basic %s\r\n\r\n " ,
381
- (int ) http_proxy->target_host .length (),
382
- http_proxy->target_host .c_str (),
383
- http_proxy->target_port ,
384
- host_len,
385
- host,
386
- http_proxy->target_port ,
387
- auth_str.c_str ());
388
- } else {
389
- n = sw_snprintf (send_buffer->str ,
390
- send_buffer->size ,
391
- HTTP_PROXY_FMT " \r\n " ,
392
- (int ) http_proxy->target_host .length (),
393
- http_proxy->target_host .c_str (),
394
- http_proxy->target_port ,
395
- host_len,
396
- host,
397
- http_proxy->target_port );
398
- }
399
-
400
- swoole_trace_log (SW_TRACE_HTTP_CLIENT, " proxy request: <<EOF\n %.*sEOF" , n, send_buffer->str );
401
-
366
+ size_t n = http_proxy->pack (send_buffer, real_host);
402
367
send_buffer->length = n;
403
- if (send (send_buffer->str , n) != n) {
368
+ swoole_trace_log (SW_TRACE_HTTP_CLIENT, " proxy request: <<EOF\n %.*sEOF" , (int ) n, send_buffer->str );
369
+
370
+ if (send (send_buffer->str , n) != (ssize_t ) n) {
404
371
return false ;
405
372
}
406
373
@@ -415,61 +382,20 @@ bool Socket::http_proxy_handshake() {
415
382
protocol.package_eof_len = sizeof (" \r\n\r\n " ) - 1 ;
416
383
memcpy (protocol.package_eof , SW_STRS (" \r\n\r\n " ));
417
384
418
- n = recv_packet ();
419
- if (n <= 0 ) {
385
+ if (recv_packet () <= 0 ) {
420
386
return false ;
421
387
}
422
388
423
389
swoole_trace_log (SW_TRACE_HTTP_CLIENT, " proxy response: <<EOF\n %.*sEOF" , n, recv_buffer->str );
424
390
425
- bool ret = false ;
426
- char *buf = recv_buffer->str ;
427
- int len = n;
428
- int state = 0 ;
429
- char *p = buf;
430
- char *pe = buf + len;
431
- for (; p < buf + len; p++) {
432
- if (state == 0 ) {
433
- if (SW_STR_ISTARTS_WITH (p, pe - p, " HTTP/1.1" ) || SW_STR_ISTARTS_WITH (p, pe - p, " HTTP/1.0" )) {
434
- state = 1 ;
435
- p += sizeof (" HTTP/1.x" ) - 1 ;
436
- } else {
437
- break ;
438
- }
439
- } else if (state == 1 ) {
440
- if (isspace (*p)) {
441
- continue ;
442
- } else {
443
- if (SW_STR_ISTARTS_WITH (p, pe - p, " 200" )) {
444
- state = 2 ;
445
- p += sizeof (" 200" ) - 1 ;
446
- } else {
447
- break ;
448
- }
449
- }
450
- } else if (state == 2 ) {
451
- ret = true ;
452
- break ;
453
- #if 0
454
- if (isspace(*p)) {
455
- continue;
456
- } else {
457
- if (SW_STR_ISTARTS_WITH(p, pe - p, "Connection established")) {
458
- ret = true;
459
- }
460
- break;
461
- }
462
- #endif
463
- }
464
- }
465
-
466
- if (!ret) {
391
+ if (!http_proxy->handshake (recv_buffer)) {
467
392
set_err (SW_ERROR_HTTP_PROXY_BAD_RESPONSE,
468
393
std::string (" wrong http_proxy response received, \n [Request]: " ) + send_buffer->to_std_string () +
469
- " \n [Response]: " + std::string (buf, len));
394
+ " \n [Response]: " + send_buffer->to_std_string ());
395
+ return false ;
470
396
}
471
397
472
- return ret ;
398
+ return true ;
473
399
}
474
400
475
401
void Socket::init_sock_type (SocketType _type) {
@@ -1830,19 +1756,4 @@ Socket::~Socket() {
1830
1756
}
1831
1757
1832
1758
} // namespace coroutine
1833
-
1834
- std::string HttpProxy::get_auth_str () {
1835
- char auth_buf[256 ];
1836
- char encode_buf[512 ];
1837
- size_t n = sw_snprintf (auth_buf,
1838
- sizeof (auth_buf),
1839
- " %.*s:%.*s" ,
1840
- (int ) username.length (),
1841
- username.c_str (),
1842
- (int ) password.length (),
1843
- password.c_str ());
1844
- base64_encode ((unsigned char *) auth_buf, n, encode_buf);
1845
- return std::string (encode_buf);
1846
- }
1847
-
1848
1759
} // namespace swoole
0 commit comments