-
- client_fd = fdopen(client_socket, "a+");
- if (NULL == client_fd) {
- perror("fdopen()");
- close(client_socket);
- return;
- }
-
-#ifdef DEBUG
- printf("New connection:\n");
-#endif
-
- /* Read request line (CONNECT ..) and headers (they are discarded). */
- result = read_http_request(client_fd, buffer, sizeof(buffer));
- if (result == -1) {
- /* Read error. */
- return;
- } else if (result == -2) {
- /* EOF */
- send_close_bad_request(client_fd);
- return;
- }
-
-#ifdef DEBUG
- printf(" request: %s", buffer);
-#endif
-
- if (0 != parse_request(buffer, host, port, &version_minor)) {
- send_close_bad_request(client_fd);
-#ifdef DEBUG
- printf(" bad request\n");
-#endif
- return;
- }
-
-#ifdef DEBUG
- printf(" %s:%s (HTTP 1.%d)\n", host, port, version_minor);
-#endif
-
- /* 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;
+/* Recent versions of GnuTLS automatically initialize the cryptography layer
+ * in gnutls_global_init(). */
+#if GNUTLS_VERSION_NUMBER <= 0x020b00
+ gcry_error_t error;
+
+ /* Thread safe setup. Must be called before gnutls_global_init(). */
+ error = gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
+ if (error != 0) {
+ fprintf(stderr, "gcry_control(): %s/%s\n", gcry_strsource(error),
+ gcry_strerror(error));
+ exit(EXIT_FAILURE);