4 * Copyright (C) 2011 Simon Ruderich
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "connection.h"
34 /* Maximum length of a HTTP request line. Longer request lines are aborted
35 * with an error. The standard doesn't specify a maximum line length but this
36 * should be a good limit to make processing simpler. As HTTPS is used this
37 * doesn't limit long GET requests. */
38 #define MAX_REQUEST_LINE 4096
40 /* Format string used to send HTTP/1.0 error responses to the client.
42 * %s is used 4 times, first three are the error code (no %n$s!), the last is
44 #define HTTP_RESPONSE_FORMAT "HTTP/1.0 %s\r\n\
45 Content-Type: text/html; charset=US-ASCII\r\n\
47 <!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n\
49 <head><title>%s</title></head>\n\
57 static int initialize_tls_session_client(int peer_socket,
59 gnutls_session_t *session,
60 gnutls_certificate_credentials_t *x509_cred);
61 static int initialize_tls_session_server(int peer_socket,
62 gnutls_session_t *session,
63 gnutls_certificate_credentials_t *x509_cred);
65 static int read_http_request(FILE *client_fd, char *request, size_t length);
66 static void send_bad_request(FILE *client_fd);
67 static void send_forwarding_failure(FILE *client_fd);
68 static void tls_send_invalid_cert_message(gnutls_session_t session);
70 static void transfer_data(int client, int server);
71 static int read_from_write_to(int from, int to);
72 static void transfer_data_tls(int client, int server,
73 gnutls_session_t client_session,
74 gnutls_session_t server_session);
75 static int read_from_write_to_tls(gnutls_session_t from, gnutls_session_t to);
77 static int connect_to_host(const char *hostname, const char *port);
79 static int parse_request(const char *buffer, char *host, char *port,
83 void handle_connection(int client_socket) {
85 FILE *client_fd, *server_fd;
87 char buffer[MAX_REQUEST_LINE];
88 char host[MAX_REQUEST_LINE];
91 int version_minor; /* HTTP/1.x */
94 /* client_x509_cred is used when talking to the client (acting as a TSL
95 * server), server_x509_cred is used when talking to the server (acting as
97 gnutls_certificate_credentials_t client_x509_cred, server_x509_cred;
99 gnutls_session_t client_session, server_session;
100 /* initialize_tls_session_*() called? - used for goto out */
101 int client_session_init, server_session_init;
102 /* gnutls_handshake() called? - used for goto out */
103 int client_session_started, server_session_started;
104 /* Validation failed? If yes we need to send the special "invalid"
106 int validation_failed;
108 LOG(LOG_DEBUG, "new connection");
113 client_session_init = 0;
114 server_session_init = 0;
115 client_session_started = 0;
116 server_session_started = 0;
117 validation_failed = 0;
119 client_fd = fdopen(client_socket, "a+");
120 if (NULL == client_fd) {
121 LOG_PERROR(LOG_WARNING, "fdopen(): client failed");
125 /* Read request line (CONNECT ..) and headers (they are discarded). */
126 result = read_http_request(client_fd, buffer, sizeof(buffer));
129 LOG(LOG_WARNING, "read_http_request(): client read error");
131 } else if (-2 == result) {
133 LOG(LOG_WARNING, "read_http_request(): client EOF");
134 send_bad_request(client_fd);
138 if (0 != parse_request(buffer, host, port, &version_minor)) {
139 LOG(LOG_WARNING, "bad request: %s", buffer);
140 send_bad_request(client_fd);
144 LOG(LOG_DEBUG, "target: %s:%s (HTTP 1.%d)", host, port, version_minor);
146 /* Connect to proxy server or directly to server. */
147 if (NULL != global_proxy_host && NULL != global_proxy_port) {
148 LOG(LOG_DEBUG, "connecting to %s:%s", global_proxy_host,
150 server_socket = connect_to_host(global_proxy_host, global_proxy_port);
152 LOG(LOG_DEBUG, "connecting to %s:%s", host, port);
153 server_socket = connect_to_host(host, port);
156 if (-1 == server_socket) {
157 LOG(LOG_WARNING, "failed to connect to server");
158 send_forwarding_failure(client_fd);
161 server_fd = fdopen(server_socket, "a+");
162 if (NULL == server_fd) {
163 LOG_PERROR(LOG_WARNING, "fdopen(): server failed");
164 send_forwarding_failure(client_fd);
168 /* Connect to proxy if requested (command line option). */
169 if (NULL != global_proxy_host && NULL != global_proxy_port) {
170 fprintf(server_fd, "CONNECT %s:%s HTTP/1.0\r\n", host, port);
171 fprintf(server_fd, "\r\n");
173 /* Read response line from proxy server. */
174 result = read_http_request(server_fd, buffer, sizeof(buffer));
177 LOG(LOG_WARNING, "read_http_request(): proxy read error");
178 send_forwarding_failure(client_fd);
180 } else if (-2 == result) {
182 LOG(LOG_WARNING, "read_http_request(): proxy EOF");
183 send_forwarding_failure(client_fd);
187 /* Check response of proxy server. */
188 if (0 != strncmp(buffer, "HTTP/1.0 200", 12)) {
189 LOG(LOG_WARNING, "bad proxy response: %s", buffer);
190 send_forwarding_failure(client_fd);
195 LOG(LOG_DEBUG, "connection to server established");
197 /* If the -u option is used and we don't know this hostname's server
198 * certificate then just pass through the connection and let the client
199 * verify the server certificate. */
200 if (global_passthrough_unknown) {
204 if (-2 == server_certificate_path(&file, host, path, sizeof(path))) {
205 /* We've established a connection, tell the client. */
206 fprintf(client_fd, "HTTP/1.0 200 Connection established\r\n");
207 fprintf(client_fd, "\r\n");
210 LOG(LOG_DEBUG, "transferring data");
212 /* Proxy data between client and server until one side is done
214 transfer_data(client_socket, server_socket);
216 LOG(LOG_DEBUG, "finished transferring data");
220 /* server_certificate_path() may have opened the file, close it. */
226 /* Initialize TLS client credentials to talk to the server. */
227 result = initialize_tls_session_server(server_socket, &server_session,
230 LOG(LOG_WARNING, "initialize_tls_session_server() failed");
231 send_forwarding_failure(client_fd);
234 server_session_init = 1;
236 LOG(LOG_DEBUG, "starting server TLS handshake");
238 /* Try to establish TLS handshake between us and server. */
239 result = gnutls_handshake(server_session);
240 if (GNUTLS_E_SUCCESS != result) {
241 LOG(LOG_WARNING, "server TLS handshake failed: %s",
242 gnutls_strerror(result));
243 send_forwarding_failure(client_fd);
246 server_session_started = 1;
248 LOG(LOG_DEBUG, "server TLS handshake finished");
250 /* Make sure the server certificate is valid and known. */
251 if (0 != verify_tls_connection(server_session, host)) {
252 LOG(LOG_ERROR, "server certificate validation failed!");
253 /* We send the error message over our TLS connection to the client,
254 * but with an invalid certificate. No data is transfered from/to the
256 validation_failed = 1;
259 /* Initialize TLS server credentials to talk to the client. */
260 result = initialize_tls_session_client(client_socket,
261 /* use a special host if the server
262 * certificate was invalid */
263 (validation_failed) ? "invalid"
268 LOG(LOG_WARNING, "initialize_tls_session_client() failed");
269 send_forwarding_failure(client_fd);
272 client_session_init = 1;
274 /* We've established a connection, tell the client. */
275 fprintf(client_fd, "HTTP/1.0 200 Connection established\r\n");
276 fprintf(client_fd, "\r\n");
279 LOG(LOG_DEBUG, "starting client TLS handshake");
281 /* Try to establish TLS handshake between client and us. */
282 result = gnutls_handshake(client_session);
283 if (GNUTLS_E_SUCCESS != result) {
284 LOG(LOG_WARNING, "client TLS handshake failed: %s",
285 gnutls_strerror(result));
286 send_forwarding_failure(client_fd);
289 client_session_started = 1;
291 LOG(LOG_DEBUG, "client TLS handshake finished");
293 /* Tell the client that the verification failed. Shouldn't be necessary as
294 * the client should terminate the connection because he received the
295 * invalid certificate but better be sure. */
296 if (validation_failed) {
297 tls_send_invalid_cert_message(client_session);
301 LOG(LOG_DEBUG, "transferring TLS data");
303 /* Proxy data between client and server until one side is done (EOF or
305 transfer_data_tls(client_socket, server_socket,
306 client_session, server_session);
308 LOG(LOG_DEBUG, "finished transferring TLS data");
311 /* Close TLS sessions if necessary. Use GNUTLS_SHUT_RDWR so the data is
312 * reliable transmitted. */
313 if (0 != server_session_started) {
314 gnutls_bye(server_session, GNUTLS_SHUT_RDWR);
316 if (0 != client_session_started) {
317 gnutls_bye(client_session, GNUTLS_SHUT_RDWR);
319 if (0 != server_session_init) {
320 gnutls_deinit(server_session);
321 gnutls_certificate_free_credentials(server_x509_cred);
323 if (0 != client_session_init) {
324 gnutls_deinit(client_session);
325 gnutls_certificate_free_cas(client_x509_cred);
326 gnutls_certificate_free_keys(client_x509_cred);
327 gnutls_certificate_free_credentials(client_x509_cred);
330 /* Close connection to server/proxy. */
331 if (NULL != server_fd) {
333 } else if (-1 != server_socket) {
334 close(server_socket);
336 LOG(LOG_DEBUG, "connection to server closed");
337 /* Close connection to client. */
338 if (NULL != client_fd) {
341 close(client_socket);
343 LOG(LOG_DEBUG, "connection to client closed");
345 LOG(LOG_DEBUG, "connection finished");
349 static int initialize_tls_session_client(int peer_socket,
350 const char *hostname,
351 gnutls_session_t *session,
352 gnutls_certificate_credentials_t *x509_cred) {
354 int use_invalid_cert;
357 /* The "invalid" hostname is special. If it's used we send an invalid
358 * certificate to let the client know something is wrong. */
359 use_invalid_cert = 0 == strcmp(hostname, "invalid");
361 /* Hostname too long. */
362 if (sizeof(path) - strlen(PROXY_SERVER_CERT_FORMAT) <= strlen(hostname)) {
364 "initialize_tls_session_client(): hostname too long: '%s'",
368 /* Try to prevent path traversals in hostnames. */
369 if (NULL != strstr(hostname, "..")) {
371 "initialize_tls_session_client(): possible path traversal: '%s'",
375 result = snprintf(path, sizeof(path), PROXY_SERVER_CERT_FORMAT, hostname);
377 LOG_PERROR(LOG_ERROR,
378 "initialize_tls_session_client(): snprintf failed");
380 } else if ((size_t)result >= sizeof(path)) {
382 "initialize_tls_session_client(): snprintf buffer too short");
386 result = gnutls_certificate_allocate_credentials(x509_cred);
387 if (GNUTLS_E_SUCCESS != result) {
389 "initialize_tls_session_client(): \
390 gnutls_certificate_allocate_credentials(): %s",
391 gnutls_strerror(result));
395 /* Load proxy CA file, this CA "list" is send to the client. */
396 if (!use_invalid_cert) {
397 result = gnutls_certificate_set_x509_trust_file(*x509_cred,
399 GNUTLS_X509_FMT_PEM);
402 "initialize_tls_session_client(): can't read CA file: '%s'",
404 gnutls_certificate_free_credentials(*x509_cred);
408 /* If the invalid hostname was specified do nothing, we use a self-signed
409 * certificate in this case. */
411 /* And certificate for this website and proxy's private key. */
412 if (!use_invalid_cert) {
413 result = gnutls_certificate_set_x509_key_file(*x509_cred,
414 path, PROXY_KEY_FILE,
415 GNUTLS_X509_FMT_PEM);
416 /* If the invalid hostname was specified load our special "invalid"
419 result = gnutls_certificate_set_x509_key_file(*x509_cred,
420 PROXY_INVALID_CERT_FILE,
422 GNUTLS_X509_FMT_PEM);
424 if (GNUTLS_E_SUCCESS != result) {
426 "initialize_tls_session_client(): \
427 can't read server certificate ('%s') or key file ('%s'): %s",
428 path, PROXY_KEY_FILE, gnutls_strerror(result));
429 gnutls_certificate_free_credentials(*x509_cred);
430 /* Could be a missing certificate. */
434 gnutls_certificate_set_dh_params(*x509_cred, global_tls_dh_params);
436 result = gnutls_init(session, GNUTLS_SERVER);
437 if (GNUTLS_E_SUCCESS != result) {
439 "initialize_tls_session_client(): gnutls_init(): %s",
440 gnutls_strerror(result));
441 gnutls_certificate_free_credentials(*x509_cred);
444 result = gnutls_priority_set(*session, global_tls_priority_cache);
445 if (GNUTLS_E_SUCCESS != result) {
447 "initialize_tls_session_client(): gnutls_priority_set(): %s",
448 gnutls_strerror(result));
449 gnutls_deinit(*session);
450 gnutls_certificate_free_credentials(*x509_cred);
453 result = gnutls_credentials_set(*session,
454 GNUTLS_CRD_CERTIFICATE, *x509_cred);
455 if (GNUTLS_E_SUCCESS != result) {
457 "initialize_tls_session_client(): gnutls_credentials_set(): %s",
458 gnutls_strerror(result));
459 gnutls_deinit(*session);
460 gnutls_certificate_free_credentials(*x509_cred);
464 gnutls_transport_set_ptr(*session, (gnutls_transport_ptr_t)peer_socket);
468 static int initialize_tls_session_server(int peer_socket,
469 gnutls_session_t *session,
470 gnutls_certificate_credentials_t *x509_cred) {
473 result = gnutls_certificate_allocate_credentials(x509_cred);
474 if (GNUTLS_E_SUCCESS != result) {
476 "initialize_tls_session_server(): \
477 gnutls_certificate_allocate_credentials(): %s",
478 gnutls_strerror(result));
482 result = gnutls_init(session, GNUTLS_CLIENT);
483 if (GNUTLS_E_SUCCESS != result) {
485 "initialize_tls_session_server(): gnutls_init(): %s",
486 gnutls_strerror(result));
487 gnutls_certificate_free_credentials(*x509_cred);
490 gnutls_priority_set(*session, global_tls_priority_cache);
491 if (GNUTLS_E_SUCCESS != result) {
493 "initialize_tls_session_server(): gnutls_priority_set(): %s",
494 gnutls_strerror(result));
495 gnutls_deinit(*session);
496 gnutls_certificate_free_credentials(*x509_cred);
499 result = gnutls_credentials_set(*session,
500 GNUTLS_CRD_CERTIFICATE, *x509_cred);
501 if (GNUTLS_E_SUCCESS != result) {
503 "initialize_tls_session_server(): gnutls_credentials_set(): %s",
504 gnutls_strerror(result));
505 gnutls_deinit(*session);
506 gnutls_certificate_free_credentials(*x509_cred);
510 gnutls_transport_set_ptr(*session, (gnutls_transport_ptr_t)peer_socket);
516 /* Read HTTP request line and headers (ignored).
518 * On success 0 is returned, -1 on client error, -2 on unexpected EOF.
520 static int read_http_request(FILE *client_fd, char *request, size_t length) {
521 char buffer[MAX_REQUEST_LINE];
523 if (NULL == fgets(request, (int)length, client_fd)) {
524 if (ferror(client_fd)) {
525 LOG_PERROR(LOG_WARNING, "read_http_request(): fgets()");
532 while (NULL != fgets(buffer, MAX_REQUEST_LINE, client_fd)) {
534 if (0 == strcmp(buffer, "\n") || 0 == strcmp(buffer, "\r\n")) {
538 if (ferror(client_fd)) {
539 LOG_PERROR(LOG_WARNING, "read_http_request(): fgets()");
546 static void send_bad_request(FILE *client_fd) {
547 #define RESPONSE_ERROR "400 Bad Request"
548 #define RESPONSE_MSG "Your browser sent an invalid request."
549 fprintf(client_fd, HTTP_RESPONSE_FORMAT,
550 RESPONSE_ERROR, RESPONSE_ERROR, RESPONSE_ERROR,
552 #undef RESPONSE_ERROR
555 static void send_forwarding_failure(FILE *client_fd) {
556 #define RESPONSE_ERROR "503 Forwarding failure"
557 #define RESPONSE_MSG "Failed to connect to server, check logs."
558 fprintf(client_fd, HTTP_RESPONSE_FORMAT,
559 RESPONSE_ERROR, RESPONSE_ERROR, RESPONSE_ERROR,
561 #undef RESPONSE_ERROR
564 static void tls_send_invalid_cert_message(gnutls_session_t session) {
565 #define RESPONSE_ERROR "500 Internal Server Error"
566 #define RESPONSE_MSG "Server certificate validation failed, check logs."
569 char buffer[sizeof(HTTP_RESPONSE_FORMAT) - 1 /* '\0' */
570 - 4 * 2 /* four %s */
571 + (sizeof(RESPONSE_ERROR) - 1 /* '\0' */) * 3
572 + sizeof(RESPONSE_MSG) - 1 /* '\0' */
575 result = snprintf(buffer, sizeof(buffer),
576 HTTP_RESPONSE_FORMAT,
577 RESPONSE_ERROR, RESPONSE_ERROR, RESPONSE_ERROR,
580 LOG_PERROR(LOG_ERROR,
581 "tls_send_invalid_cert_message(): snprintf failed");
583 } else if ((size_t)result >= sizeof(buffer)) {
585 "tls_send_invalid_cert_message(): snprintf buffer too short");
589 gnutls_record_send(session, buffer, sizeof(buffer) - 1);
590 /* don't send trailing '\0' */
591 #undef RESPONSE_ERROR
596 /* Transfer data between client and server sockets until one closes the
598 static void transfer_data(int client, int server) {
599 struct pollfd fds[2];
601 fds[0].events = POLLIN | POLLPRI | POLLHUP | POLLERR;
604 fds[1].events = POLLIN | POLLPRI | POLLHUP | POLLERR;
608 int result = poll(fds, 2, -1 /* no timeout */);
610 LOG_PERROR(LOG_ERROR, "transfer_data(): poll()");
614 /* Data available from client. */
615 if (fds[0].revents & POLLIN || fds[0].revents & POLLPRI) {
616 if (0 != read_from_write_to(client, server)) {
617 /* EOF (or other error) */
621 /* Data available from server. */
622 if (fds[1].revents & POLLIN || fds[1].revents & POLLPRI) {
623 if (0 != read_from_write_to(server, client)) {
624 /* EOF (or other error) */
629 /* Client closed connection. */
630 if (fds[0].revents & POLLERR || fds[0].revents & POLLHUP) {
633 /* Server closed connection. */
634 if (fds[1].revents & POLLERR || fds[1].revents & POLLHUP) {
640 /* Read available data from socket from and write it to socket to. At maximum
641 * 4096 bytes are read/written. */
642 static int read_from_write_to(int from, int to) {
644 ssize_t size_written;
647 LOG(LOG_DEBUG, "read_from_write_to(): %d -> %d", from, to);
649 size_read = read(from, buffer, sizeof(buffer));
651 LOG_PERROR(LOG_WARNING, "read_from_write_to(): read()");
655 if (0 == size_read) {
659 size_written = write(to, buffer, (size_t)size_read);
660 if (0 > size_written) {
661 LOG_PERROR(LOG_WARNING, "read_from_write_to(): write()");
664 if (size_read != size_written) {
665 LOG(LOG_ERROR, "read_from_write_to(): only written %ld of %ld bytes!",
666 (long int)size_written, (long int)size_read);
673 /* Transfer data between client and server TLS connection until one closes the
675 static void transfer_data_tls(int client, int server,
676 gnutls_session_t client_session,
677 gnutls_session_t server_session) {
678 struct pollfd fds[2];
680 fds[0].events = POLLIN | POLLPRI | POLLHUP | POLLERR;
683 fds[1].events = POLLIN | POLLPRI | POLLHUP | POLLERR;
687 int result = poll(fds, 2, -1 /* no timeout */);
689 LOG_PERROR(LOG_ERROR, "transfer_data(): poll()");
693 /* Data available from client. */
694 if (fds[0].revents & POLLIN || fds[0].revents & POLLPRI) {
695 if (0 != read_from_write_to_tls(client_session, server_session)) {
696 /* EOF (or other error) */
700 /* Data available from server. */
701 if (fds[1].revents & POLLIN || fds[1].revents & POLLPRI) {
702 if (0 != read_from_write_to_tls(server_session, client_session)) {
703 /* EOF (or other error) */
708 /* Client closed connection. */
709 if (fds[0].revents & POLLERR || fds[0].revents & POLLHUP) {
712 /* Server closed connection. */
713 if (fds[1].revents & POLLERR || fds[1].revents & POLLHUP) {
719 /* Read available data from session from and write to session to. */
720 static int read_from_write_to_tls(gnutls_session_t from,
721 gnutls_session_t to) {
724 ssize_t size_written;
727 /* Get maximum possible buffer size. */
728 size = gnutls_record_get_max_size(from);
729 LOG(LOG_DEBUG, "read_from_write_to_tls(): suggested buffer size: %ld",
731 if (size > gnutls_record_get_max_size(to)) {
732 size = gnutls_record_get_max_size(to);
734 if (size > sizeof(buffer)) {
735 size = sizeof(buffer);
737 LOG(LOG_DEBUG, "read_from_write_to_tls(): used buffer size: %ld",
740 size_read = gnutls_record_recv(from, buffer, size);
742 LOG(LOG_WARNING, "read_from_write_to_tls(): gnutls_record_recv(): %s",
743 gnutls_strerror((int)size_read));
747 if (0 == size_read) {
751 size_written = gnutls_record_send(to, buffer, (size_t)size_read);
752 if (0 > size_written) {
753 LOG(LOG_WARNING, "read_from_write_to_tls(): gnutls_record_send(): %s",
754 gnutls_strerror((int)size_written));
757 if (size_read != size_written) {
758 LOG(LOG_ERROR, "read_from_write_to_tls(): only written %ld of %ld bytes!",
759 (long int)size_written, (long int)size_read);
767 static int connect_to_host(const char *hostname, const char *port) {
768 struct addrinfo gai_hints;
769 struct addrinfo *gai_result;
773 struct addrinfo *server;
775 if (NULL == hostname || NULL == port) {
779 /* Get IP of hostname server. */
780 memset(&gai_hints, 0, sizeof(gai_hints));
781 gai_hints.ai_family = AF_UNSPEC;
782 gai_hints.ai_socktype = SOCK_STREAM;
783 gai_hints.ai_protocol = 0;
784 gai_hints.ai_flags = AI_NUMERICSERV /* given port is numeric */
785 | AI_ADDRCONFIG /* supported by this computer */
786 | AI_V4MAPPED; /* support IPv4 through IPv6 */
787 gai_return = getaddrinfo(hostname, port, &gai_hints, &gai_result);
788 if (0 != gai_return) {
789 LOG_PERROR(LOG_WARNING, "connect_to_host(): getaddrinfo()");
793 /* Now try to connect to each server returned by getaddrinfo(), use the
794 * first successful connect. */
795 for (server = gai_result; NULL != server; server = server->ai_next) {
796 server_socket = socket(server->ai_family,
798 server->ai_protocol);
799 if (-1 == server_socket) {
800 LOG_PERROR(LOG_DEBUG, "connect_to_host(): socket(), trying next");
804 if (-1 != connect(server_socket, server->ai_addr,
805 server->ai_addrlen)) {
808 LOG_PERROR(LOG_DEBUG, "connect_to_host(): connect(), trying next");
810 close(server_socket);
812 /* Make sure we free the result from getaddrinfo(). */
813 freeaddrinfo(gai_result);
815 if (NULL == server) {
816 LOG_PERROR(LOG_WARNING, "connect_to_host(): no server found, abort");
820 return server_socket;
824 /* Parse HTTP CONNECT request string and save its parameters.
826 * The following format is expected: "CONNECT host:port HTTP/1.x".
828 * request and host must have the same size! port must be at least 6 bytes
831 static int parse_request(const char *request, char *host, char *port,
832 int *version_minor) {
833 int port_unused; /* just used to verify the port is numeric */
836 /* scanf() doesn't check spaces. */
837 if (0 != strncmp(request, "CONNECT ", 8)) {
840 /* Check request and extract data, "host:port" is not yet separated. */
841 if (2 != sscanf(request, "CONNECT %s HTTP/1.%d",
842 host, version_minor)) {
845 /* Make sure ":port" is there. */
846 if (NULL == (position = strchr(host, ':'))) {
849 /* Make sure port is numeric. */
850 if (1 != sscanf(position + 1, "%d", &port_unused)) {
853 /* Store it in *port. */
854 strncpy(port, position + 1, 5);
856 /* And remove port from host. */