- /* Connect to proxy server or directly to server. */
- if (NULL != use_proxy_host && NULL != use_proxy_port) {
- server_socket = connect_to_host(use_proxy_host, use_proxy_port);
- } else {
- server_socket = connect_to_host(host, port);
- }
-
- if (-1 == server_socket) {
- send_close_forwarding_failure(client_fd);
- return;
- }
- server_fd = fdopen(server_socket, "a+");
- if (NULL == server_fd) {
- send_close_forwarding_failure(client_fd);
- return;
- }
-
- /* Connect to proxy if requested (command line option). */
- if (NULL != use_proxy_host && NULL != use_proxy_port) {
- fprintf(server_fd, "CONNECT %s:%s HTTP/1.0\r\n", host, port);
- fprintf(server_fd, "\r\n");
-
- /* Read response line from proxy server. */
- result = read_http_request(server_fd, buffer, sizeof(buffer));
- if (result == -1) {
- /* Read error. */
- send_close_forwarding_failure(client_fd);
- return;
- } else if (result == -2) {
- /* EOF */
- fclose(server_fd);
- send_close_forwarding_failure(client_fd);
- return;
- }
-
- /* Check response of proxy server. */
- if (0 != strncmp(buffer, "HTTP/1.0 200", 12)) {
-#ifdef DEBUG
- printf(" bad proxy response\n");
-#endif
- fclose(server_fd);
- send_close_forwarding_failure(client_fd);
- return;
- }
- }