- /* Now try to connect to each server returned by getaddrinfo(), use the
- * first successful connect. */
- for (server = gai_result; NULL != server; server = server->ai_next) {
- server_socket = socket(server->ai_family,
- server->ai_socktype,
- server->ai_protocol);
- if (-1 == server_socket) {
- perror("socket(), trying next");
- continue;
- }
-
- if (-1 != connect(server_socket, server->ai_addr,
- server->ai_addrlen)) {
- break;
- }
- perror("connect(), trying next");
-
- close(server_socket);
- }
- /* Make sure we free the result from getaddrinfo(). */
- freeaddrinfo(gai_result);
-
- if (NULL == server) {
- fprintf(stderr, "no server found, aborting\n");
- return -1;
- }
-
- return server_socket;
-}
-
-
-/* Parse HTTP CONNECT request string and save its parameters.
- *
- * The following format is expected: "CONNECT host:port HTTP/1.y".
- *
- * request and host must have the same size! port must be at least 6 bytes
- * long (5 + '\0').
- */
-static int parse_request(const char *request, char *host, char *port,
- int *version_minor) {
- int port_unused; /* just used to verify the port is numeric */
- char *position;
-
- /* scanf() doesn't check spaces. */
- if (0 != strncmp(request, "CONNECT ", 8)) {
- return -1;
- }
- /* Check request and extract data, "host:port" is not yet separated. */
- if (2 != sscanf(request, "CONNECT %s HTTP/1.%d",
- host, version_minor)) {
- return -1;
- }
- /* Make sure ":port" is there. */
- if (NULL == (position = strchr(host, ':'))) {
- return -1;
- }
- /* Make sure port is numeric. */
- if (1 != sscanf(position + 1, "%d", &port_unused)) {
- return -1;