Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

692 рядки
18 KiB

  1. /* Copyright (c) 2014, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <openssl/base.h>
  15. #include <string>
  16. #include <vector>
  17. #include <errno.h>
  18. #include <limits.h>
  19. #include <stddef.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <sys/types.h>
  23. #if !defined(OPENSSL_WINDOWS)
  24. #include <arpa/inet.h>
  25. #include <fcntl.h>
  26. #include <netdb.h>
  27. #include <netinet/in.h>
  28. #include <sys/select.h>
  29. #include <sys/socket.h>
  30. #include <unistd.h>
  31. #else
  32. #include <io.h>
  33. OPENSSL_MSVC_PRAGMA(warning(push, 3))
  34. #include <winsock2.h>
  35. #include <ws2tcpip.h>
  36. OPENSSL_MSVC_PRAGMA(warning(pop))
  37. typedef int ssize_t;
  38. OPENSSL_MSVC_PRAGMA(comment(lib, "Ws2_32.lib"))
  39. #endif
  40. #include <openssl/err.h>
  41. #include <openssl/ssl.h>
  42. #include <openssl/x509.h>
  43. #include "../crypto/internal.h"
  44. #include "internal.h"
  45. #include "transport_common.h"
  46. #if !defined(OPENSSL_WINDOWS)
  47. static int closesocket(int sock) {
  48. return close(sock);
  49. }
  50. #endif
  51. bool InitSocketLibrary() {
  52. #if defined(OPENSSL_WINDOWS)
  53. WSADATA wsaData;
  54. int err = WSAStartup(MAKEWORD(2, 2), &wsaData);
  55. if (err != 0) {
  56. fprintf(stderr, "WSAStartup failed with error %d\n", err);
  57. return false;
  58. }
  59. #endif
  60. return true;
  61. }
  62. static void SplitHostPort(std::string *out_hostname, std::string *out_port,
  63. const std::string &hostname_and_port) {
  64. size_t colon_offset = hostname_and_port.find_last_of(':');
  65. const size_t bracket_offset = hostname_and_port.find_last_of(']');
  66. std::string hostname, port;
  67. // An IPv6 literal may have colons internally, guarded by square brackets.
  68. if (bracket_offset != std::string::npos &&
  69. colon_offset != std::string::npos && bracket_offset > colon_offset) {
  70. colon_offset = std::string::npos;
  71. }
  72. if (colon_offset == std::string::npos) {
  73. *out_hostname = hostname_and_port;
  74. *out_port = "443";
  75. } else {
  76. *out_hostname = hostname_and_port.substr(0, colon_offset);
  77. *out_port = hostname_and_port.substr(colon_offset + 1);
  78. }
  79. }
  80. // Connect sets |*out_sock| to be a socket connected to the destination given
  81. // in |hostname_and_port|, which should be of the form "www.example.com:123".
  82. // It returns true on success and false otherwise.
  83. bool Connect(int *out_sock, const std::string &hostname_and_port) {
  84. std::string hostname, port;
  85. SplitHostPort(&hostname, &port, hostname_and_port);
  86. // Handle IPv6 literals.
  87. if (hostname.size() >= 2 && hostname[0] == '[' &&
  88. hostname[hostname.size() - 1] == ']') {
  89. hostname = hostname.substr(1, hostname.size() - 2);
  90. }
  91. struct addrinfo hint, *result;
  92. OPENSSL_memset(&hint, 0, sizeof(hint));
  93. hint.ai_family = AF_UNSPEC;
  94. hint.ai_socktype = SOCK_STREAM;
  95. int ret = getaddrinfo(hostname.c_str(), port.c_str(), &hint, &result);
  96. if (ret != 0) {
  97. fprintf(stderr, "getaddrinfo returned: %s\n", gai_strerror(ret));
  98. return false;
  99. }
  100. bool ok = false;
  101. char buf[256];
  102. *out_sock =
  103. socket(result->ai_family, result->ai_socktype, result->ai_protocol);
  104. if (*out_sock < 0) {
  105. perror("socket");
  106. goto out;
  107. }
  108. switch (result->ai_family) {
  109. case AF_INET: {
  110. struct sockaddr_in *sin =
  111. reinterpret_cast<struct sockaddr_in *>(result->ai_addr);
  112. fprintf(stderr, "Connecting to %s:%d\n",
  113. inet_ntop(result->ai_family, &sin->sin_addr, buf, sizeof(buf)),
  114. ntohs(sin->sin_port));
  115. break;
  116. }
  117. case AF_INET6: {
  118. struct sockaddr_in6 *sin6 =
  119. reinterpret_cast<struct sockaddr_in6 *>(result->ai_addr);
  120. fprintf(stderr, "Connecting to [%s]:%d\n",
  121. inet_ntop(result->ai_family, &sin6->sin6_addr, buf, sizeof(buf)),
  122. ntohs(sin6->sin6_port));
  123. break;
  124. }
  125. }
  126. if (connect(*out_sock, result->ai_addr, result->ai_addrlen) != 0) {
  127. perror("connect");
  128. goto out;
  129. }
  130. ok = true;
  131. out:
  132. freeaddrinfo(result);
  133. return ok;
  134. }
  135. Listener::~Listener() {
  136. if (server_sock_ >= 0) {
  137. closesocket(server_sock_);
  138. }
  139. }
  140. bool Listener::Init(const std::string &port) {
  141. if (server_sock_ >= 0) {
  142. return false;
  143. }
  144. struct sockaddr_in6 addr;
  145. OPENSSL_memset(&addr, 0, sizeof(addr));
  146. addr.sin6_family = AF_INET6;
  147. // Windows' IN6ADDR_ANY_INIT does not have enough curly braces for clang-cl
  148. // (https://crbug.com/772108), while other platforms like NaCl are missing
  149. // in6addr_any, so use a mix of both.
  150. #if defined(OPENSSL_WINDOWS)
  151. addr.sin6_addr = in6addr_any;
  152. #else
  153. addr.sin6_addr = IN6ADDR_ANY_INIT;
  154. #endif
  155. addr.sin6_port = htons(atoi(port.c_str()));
  156. #if defined(OPENSSL_WINDOWS)
  157. const BOOL enable = TRUE;
  158. #else
  159. const int enable = 1;
  160. #endif
  161. server_sock_ = socket(addr.sin6_family, SOCK_STREAM, 0);
  162. if (server_sock_ < 0) {
  163. perror("socket");
  164. return false;
  165. }
  166. if (setsockopt(server_sock_, SOL_SOCKET, SO_REUSEADDR, (const char *)&enable,
  167. sizeof(enable)) < 0) {
  168. perror("setsockopt");
  169. return false;
  170. }
  171. if (bind(server_sock_, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
  172. perror("connect");
  173. return false;
  174. }
  175. listen(server_sock_, SOMAXCONN);
  176. return true;
  177. }
  178. bool Listener::Accept(int *out_sock) {
  179. struct sockaddr_in6 addr;
  180. socklen_t addr_len = sizeof(addr);
  181. *out_sock = accept(server_sock_, (struct sockaddr *)&addr, &addr_len);
  182. return *out_sock >= 0;
  183. }
  184. bool VersionFromString(uint16_t *out_version, const std::string &version) {
  185. if (version == "ssl3") {
  186. *out_version = SSL3_VERSION;
  187. return true;
  188. } else if (version == "tls1" || version == "tls1.0") {
  189. *out_version = TLS1_VERSION;
  190. return true;
  191. } else if (version == "tls1.1") {
  192. *out_version = TLS1_1_VERSION;
  193. return true;
  194. } else if (version == "tls1.2") {
  195. *out_version = TLS1_2_VERSION;
  196. return true;
  197. } else if (version == "tls1.3") {
  198. *out_version = TLS1_3_VERSION;
  199. return true;
  200. }
  201. return false;
  202. }
  203. void PrintConnectionInfo(BIO *bio, const SSL *ssl) {
  204. const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
  205. BIO_printf(bio, " Version: %s\n", SSL_get_version(ssl));
  206. BIO_printf(bio, " Resumed session: %s\n",
  207. SSL_session_reused(ssl) ? "yes" : "no");
  208. BIO_printf(bio, " Cipher: %s\n", SSL_CIPHER_standard_name(cipher));
  209. uint16_t curve = SSL_get_curve_id(ssl);
  210. if (curve != 0) {
  211. BIO_printf(bio, " ECDHE curve: %s\n", SSL_get_curve_name(curve));
  212. }
  213. uint16_t sigalg = SSL_get_peer_signature_algorithm(ssl);
  214. if (sigalg != 0) {
  215. BIO_printf(bio, " Signature algorithm: %s\n",
  216. SSL_get_signature_algorithm_name(
  217. sigalg, SSL_version(ssl) != TLS1_2_VERSION));
  218. }
  219. BIO_printf(bio, " Secure renegotiation: %s\n",
  220. SSL_get_secure_renegotiation_support(ssl) ? "yes" : "no");
  221. BIO_printf(bio, " Extended master secret: %s\n",
  222. SSL_get_extms_support(ssl) ? "yes" : "no");
  223. const uint8_t *next_proto;
  224. unsigned next_proto_len;
  225. SSL_get0_next_proto_negotiated(ssl, &next_proto, &next_proto_len);
  226. BIO_printf(bio, " Next protocol negotiated: %.*s\n", next_proto_len,
  227. next_proto);
  228. const uint8_t *alpn;
  229. unsigned alpn_len;
  230. SSL_get0_alpn_selected(ssl, &alpn, &alpn_len);
  231. BIO_printf(bio, " ALPN protocol: %.*s\n", alpn_len, alpn);
  232. const char *host_name = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
  233. if (host_name != nullptr && SSL_is_server(ssl)) {
  234. BIO_printf(bio, " Client sent SNI: %s\n", host_name);
  235. }
  236. if (!SSL_is_server(ssl)) {
  237. const uint8_t *ocsp_staple;
  238. size_t ocsp_staple_len;
  239. SSL_get0_ocsp_response(ssl, &ocsp_staple, &ocsp_staple_len);
  240. BIO_printf(bio, " OCSP staple: %s\n", ocsp_staple_len > 0 ? "yes" : "no");
  241. const uint8_t *sct_list;
  242. size_t sct_list_len;
  243. SSL_get0_signed_cert_timestamp_list(ssl, &sct_list, &sct_list_len);
  244. BIO_printf(bio, " SCT list: %s\n", sct_list_len > 0 ? "yes" : "no");
  245. }
  246. BIO_printf(
  247. bio, " Early data: %s\n",
  248. (SSL_early_data_accepted(ssl) || SSL_in_early_data(ssl)) ? "yes" : "no");
  249. // Print the server cert subject and issuer names.
  250. bssl::UniquePtr<X509> peer(SSL_get_peer_certificate(ssl));
  251. if (peer != nullptr) {
  252. BIO_printf(bio, " Cert subject: ");
  253. X509_NAME_print_ex(bio, X509_get_subject_name(peer.get()), 0,
  254. XN_FLAG_ONELINE);
  255. BIO_printf(bio, "\n Cert issuer: ");
  256. X509_NAME_print_ex(bio, X509_get_issuer_name(peer.get()), 0,
  257. XN_FLAG_ONELINE);
  258. BIO_printf(bio, "\n");
  259. }
  260. }
  261. bool SocketSetNonBlocking(int sock, bool is_non_blocking) {
  262. bool ok;
  263. #if defined(OPENSSL_WINDOWS)
  264. u_long arg = is_non_blocking;
  265. ok = 0 == ioctlsocket(sock, FIONBIO, &arg);
  266. #else
  267. int flags = fcntl(sock, F_GETFL, 0);
  268. if (flags < 0) {
  269. return false;
  270. }
  271. if (is_non_blocking) {
  272. flags |= O_NONBLOCK;
  273. } else {
  274. flags &= ~O_NONBLOCK;
  275. }
  276. ok = 0 == fcntl(sock, F_SETFL, flags);
  277. #endif
  278. if (!ok) {
  279. fprintf(stderr, "Failed to set socket non-blocking.\n");
  280. }
  281. return ok;
  282. }
  283. static bool SocketSelect(int sock, bool stdin_open, bool *socket_ready,
  284. bool *stdin_ready) {
  285. #if !defined(OPENSSL_WINDOWS)
  286. fd_set read_fds;
  287. FD_ZERO(&read_fds);
  288. if (stdin_open) {
  289. FD_SET(0, &read_fds);
  290. }
  291. FD_SET(sock, &read_fds);
  292. if (select(sock + 1, &read_fds, NULL, NULL, NULL) <= 0) {
  293. perror("select");
  294. return false;
  295. }
  296. if (FD_ISSET(0, &read_fds)) {
  297. *stdin_ready = true;
  298. }
  299. if (FD_ISSET(sock, &read_fds)) {
  300. *socket_ready = true;
  301. }
  302. return true;
  303. #else
  304. WSAEVENT socket_handle = WSACreateEvent();
  305. if (socket_handle == WSA_INVALID_EVENT ||
  306. WSAEventSelect(sock, socket_handle, FD_READ) != 0) {
  307. WSACloseEvent(socket_handle);
  308. return false;
  309. }
  310. HANDLE read_fds[2];
  311. read_fds[0] = socket_handle;
  312. read_fds[1] = GetStdHandle(STD_INPUT_HANDLE);
  313. switch (
  314. WaitForMultipleObjects(stdin_open ? 2 : 1, read_fds, FALSE, INFINITE)) {
  315. case WAIT_OBJECT_0 + 0:
  316. *socket_ready = true;
  317. break;
  318. case WAIT_OBJECT_0 + 1:
  319. *stdin_ready = true;
  320. break;
  321. case WAIT_TIMEOUT:
  322. break;
  323. default:
  324. WSACloseEvent(socket_handle);
  325. return false;
  326. }
  327. WSACloseEvent(socket_handle);
  328. return true;
  329. #endif
  330. }
  331. // PrintErrorCallback is a callback function from OpenSSL's
  332. // |ERR_print_errors_cb| that writes errors to a given |FILE*|.
  333. int PrintErrorCallback(const char *str, size_t len, void *ctx) {
  334. fwrite(str, len, 1, reinterpret_cast<FILE*>(ctx));
  335. return 1;
  336. }
  337. bool TransferData(SSL *ssl, int sock) {
  338. if (!SocketSetNonBlocking(sock, true)) {
  339. return false;
  340. }
  341. bool stdin_open = true;
  342. for (;;) {
  343. bool socket_ready = false;
  344. bool stdin_ready = false;
  345. if (!SocketSelect(sock, stdin_open, &socket_ready, &stdin_ready)) {
  346. return false;
  347. }
  348. if (stdin_ready) {
  349. uint8_t buffer[512];
  350. ssize_t n;
  351. do {
  352. n = BORINGSSL_READ(0, buffer, sizeof(buffer));
  353. } while (n == -1 && errno == EINTR);
  354. if (n == 0) {
  355. stdin_open = false;
  356. #if !defined(OPENSSL_WINDOWS)
  357. shutdown(sock, SHUT_WR);
  358. #else
  359. shutdown(sock, SD_SEND);
  360. #endif
  361. continue;
  362. } else if (n < 0) {
  363. perror("read from stdin");
  364. return false;
  365. }
  366. // On Windows, SocketSelect ends up setting sock to non-blocking.
  367. #if !defined(OPENSSL_WINDOWS)
  368. if (!SocketSetNonBlocking(sock, false)) {
  369. return false;
  370. }
  371. #endif
  372. int ssl_ret = SSL_write(ssl, buffer, n);
  373. if (!SocketSetNonBlocking(sock, true)) {
  374. return false;
  375. }
  376. if (ssl_ret <= 0) {
  377. int ssl_err = SSL_get_error(ssl, ssl_ret);
  378. fprintf(stderr, "Error while writing: %d\n", ssl_err);
  379. ERR_print_errors_cb(PrintErrorCallback, stderr);
  380. return false;
  381. } else if (ssl_ret != n) {
  382. fprintf(stderr, "Short write from SSL_write.\n");
  383. return false;
  384. }
  385. }
  386. if (socket_ready) {
  387. uint8_t buffer[512];
  388. int ssl_ret = SSL_read(ssl, buffer, sizeof(buffer));
  389. if (ssl_ret < 0) {
  390. int ssl_err = SSL_get_error(ssl, ssl_ret);
  391. if (ssl_err == SSL_ERROR_WANT_READ) {
  392. continue;
  393. }
  394. fprintf(stderr, "Error while reading: %d\n", ssl_err);
  395. ERR_print_errors_cb(PrintErrorCallback, stderr);
  396. return false;
  397. } else if (ssl_ret == 0) {
  398. return true;
  399. }
  400. ssize_t n;
  401. do {
  402. n = BORINGSSL_WRITE(1, buffer, ssl_ret);
  403. } while (n == -1 && errno == EINTR);
  404. if (n != ssl_ret) {
  405. fprintf(stderr, "Short write to stderr.\n");
  406. return false;
  407. }
  408. }
  409. }
  410. }
  411. // SocketLineReader wraps a small buffer around a socket for line-orientated
  412. // protocols.
  413. class SocketLineReader {
  414. public:
  415. explicit SocketLineReader(int sock) : sock_(sock) {}
  416. // Next reads a '\n'- or '\r\n'-terminated line from the socket and, on
  417. // success, sets |*out_line| to it and returns true. Otherwise it returns
  418. // false.
  419. bool Next(std::string *out_line) {
  420. for (;;) {
  421. for (size_t i = 0; i < buf_len_; i++) {
  422. if (buf_[i] != '\n') {
  423. continue;
  424. }
  425. size_t length = i;
  426. if (i > 0 && buf_[i - 1] == '\r') {
  427. length--;
  428. }
  429. out_line->assign(buf_, length);
  430. buf_len_ -= i + 1;
  431. OPENSSL_memmove(buf_, &buf_[i + 1], buf_len_);
  432. return true;
  433. }
  434. if (buf_len_ == sizeof(buf_)) {
  435. fprintf(stderr, "Received line too long!\n");
  436. return false;
  437. }
  438. ssize_t n;
  439. do {
  440. n = recv(sock_, &buf_[buf_len_], sizeof(buf_) - buf_len_, 0);
  441. } while (n == -1 && errno == EINTR);
  442. if (n < 0) {
  443. fprintf(stderr, "Read error from socket\n");
  444. return false;
  445. }
  446. buf_len_ += n;
  447. }
  448. }
  449. // ReadSMTPReply reads one or more lines that make up an SMTP reply. On
  450. // success, it sets |*out_code| to the reply's code (e.g. 250) and
  451. // |*out_content| to the body of the reply (e.g. "OK") and returns true.
  452. // Otherwise it returns false.
  453. //
  454. // See https://tools.ietf.org/html/rfc821#page-48
  455. bool ReadSMTPReply(unsigned *out_code, std::string *out_content) {
  456. out_content->clear();
  457. // kMaxLines is the maximum number of lines that we'll accept in an SMTP
  458. // reply.
  459. static const unsigned kMaxLines = 512;
  460. for (unsigned i = 0; i < kMaxLines; i++) {
  461. std::string line;
  462. if (!Next(&line)) {
  463. return false;
  464. }
  465. if (line.size() < 4) {
  466. fprintf(stderr, "Short line from SMTP server: %s\n", line.c_str());
  467. return false;
  468. }
  469. const std::string code_str = line.substr(0, 3);
  470. char *endptr;
  471. const unsigned long code = strtoul(code_str.c_str(), &endptr, 10);
  472. if (*endptr || code > UINT_MAX) {
  473. fprintf(stderr, "Failed to parse code from line: %s\n", line.c_str());
  474. return false;
  475. }
  476. if (i == 0) {
  477. *out_code = code;
  478. } else if (code != *out_code) {
  479. fprintf(stderr,
  480. "Reply code varied within a single reply: was %u, now %u\n",
  481. *out_code, static_cast<unsigned>(code));
  482. return false;
  483. }
  484. if (line[3] == ' ') {
  485. // End of reply.
  486. *out_content += line.substr(4, std::string::npos);
  487. return true;
  488. } else if (line[3] == '-') {
  489. // Another line of reply will follow this one.
  490. *out_content += line.substr(4, std::string::npos);
  491. out_content->push_back('\n');
  492. } else {
  493. fprintf(stderr, "Bad character after code in SMTP reply: %s\n",
  494. line.c_str());
  495. return false;
  496. }
  497. }
  498. fprintf(stderr, "Rejected SMTP reply of more then %u lines\n", kMaxLines);
  499. return false;
  500. }
  501. private:
  502. const int sock_;
  503. char buf_[512];
  504. size_t buf_len_ = 0;
  505. };
  506. // SendAll writes |data_len| bytes from |data| to |sock|. It returns true on
  507. // success and false otherwise.
  508. static bool SendAll(int sock, const char *data, size_t data_len) {
  509. size_t done = 0;
  510. while (done < data_len) {
  511. ssize_t n;
  512. do {
  513. n = send(sock, &data[done], data_len - done, 0);
  514. } while (n == -1 && errno == EINTR);
  515. if (n < 0) {
  516. fprintf(stderr, "Error while writing to socket\n");
  517. return false;
  518. }
  519. done += n;
  520. }
  521. return true;
  522. }
  523. bool DoSMTPStartTLS(int sock) {
  524. SocketLineReader line_reader(sock);
  525. unsigned code_220 = 0;
  526. std::string reply_220;
  527. if (!line_reader.ReadSMTPReply(&code_220, &reply_220)) {
  528. return false;
  529. }
  530. if (code_220 != 220) {
  531. fprintf(stderr, "Expected 220 line from SMTP server but got code %u\n",
  532. code_220);
  533. return false;
  534. }
  535. static const char kHelloLine[] = "EHLO BoringSSL\r\n";
  536. if (!SendAll(sock, kHelloLine, sizeof(kHelloLine) - 1)) {
  537. return false;
  538. }
  539. unsigned code_250 = 0;
  540. std::string reply_250;
  541. if (!line_reader.ReadSMTPReply(&code_250, &reply_250)) {
  542. return false;
  543. }
  544. if (code_250 != 250) {
  545. fprintf(stderr, "Expected 250 line after EHLO but got code %u\n", code_250);
  546. return false;
  547. }
  548. // https://tools.ietf.org/html/rfc1869#section-4.3
  549. if (("\n" + reply_250 + "\n").find("\nSTARTTLS\n") == std::string::npos) {
  550. fprintf(stderr, "Server does not support STARTTLS\n");
  551. return false;
  552. }
  553. static const char kSTARTTLSLine[] = "STARTTLS\r\n";
  554. if (!SendAll(sock, kSTARTTLSLine, sizeof(kSTARTTLSLine) - 1)) {
  555. return false;
  556. }
  557. if (!line_reader.ReadSMTPReply(&code_220, &reply_220)) {
  558. return false;
  559. }
  560. if (code_220 != 220) {
  561. fprintf(
  562. stderr,
  563. "Expected 220 line from SMTP server after STARTTLS, but got code %u\n",
  564. code_220);
  565. return false;
  566. }
  567. return true;
  568. }
  569. bool DoHTTPTunnel(int sock, const std::string &hostname_and_port) {
  570. std::string hostname, port;
  571. SplitHostPort(&hostname, &port, hostname_and_port);
  572. fprintf(stderr, "Establishing HTTP tunnel to %s:%s.\n", hostname.c_str(),
  573. port.c_str());
  574. char buf[1024];
  575. snprintf(buf, sizeof(buf), "CONNECT %s:%s HTTP/1.0\r\n\r\n", hostname.c_str(),
  576. port.c_str());
  577. if (!SendAll(sock, buf, strlen(buf))) {
  578. return false;
  579. }
  580. SocketLineReader line_reader(sock);
  581. // Read until an empty line, signaling the end of the HTTP response.
  582. std::string line;
  583. for (;;) {
  584. if (!line_reader.Next(&line)) {
  585. return false;
  586. }
  587. if (line.empty()) {
  588. return true;
  589. }
  590. fprintf(stderr, "%s\n", line.c_str());
  591. }
  592. }