Modify proxy to handle multiple HTTP requests per connection

- Keep connection open for persistent HTTP connections
- Prevents premature connection closure that causes browser errors
- Loop processing requests until client closes connection
parent df1bc6ab
...@@ -389,10 +389,16 @@ static void *proxy_data_forward(void *arg) { ...@@ -389,10 +389,16 @@ static void *proxy_data_forward(void *arg) {
// Handle incoming HTTP request by proxying to wsssht daemon // Handle incoming HTTP request by proxying to wsssht daemon
static int handle_proxy_request(int client_fd, wssshd_state_t *state, const wssshd_config_t *config) { static int handle_proxy_request(int client_fd, wssshd_state_t *state, const wssshd_config_t *config) {
// Find client once, assume all requests on this connection are for the same hostname
char first_hostname[256] = {0};
client_t *client = NULL;
const char *service_type = NULL;
while (1) {
char request[8192]; char request[8192];
ssize_t bytes_read = recv(client_fd, request, sizeof(request) - 1, 0); ssize_t bytes_read = recv(client_fd, request, sizeof(request) - 1, 0);
if (bytes_read <= 0) { if (bytes_read <= 0) {
return -1; break;
} }
request[bytes_read] = '\0'; request[bytes_read] = '\0';
...@@ -400,13 +406,14 @@ static int handle_proxy_request(int client_fd, wssshd_state_t *state, const wsss ...@@ -400,13 +406,14 @@ static int handle_proxy_request(int client_fd, wssshd_state_t *state, const wsss
char hostname[256]; char hostname[256];
if (parse_host_header(request, hostname, sizeof(hostname)) != 0) { if (parse_host_header(request, hostname, sizeof(hostname)) != 0) {
send_http_error(client_fd, 400, "Bad Request"); send_http_error(client_fd, 400, "Bad Request");
return -1; continue;
} }
printf("[WEB-PROXY] Received request for hostname: %s\n", hostname); printf("[WEB-PROXY] Received request for hostname: %s\n", hostname);
// Find client by hostname // Find client by hostname (only once)
client_t *client = NULL; if (!client || strcmp(first_hostname, hostname) != 0) {
strcpy(first_hostname, hostname);
pthread_mutex_lock(&state->client_mutex); pthread_mutex_lock(&state->client_mutex);
for (size_t i = 0; i < state->clients_count; i++) { for (size_t i = 0; i < state->clients_count; i++) {
client_t *c = &state->clients[i]; client_t *c = &state->clients[i];
...@@ -433,14 +440,13 @@ static int handle_proxy_request(int client_fd, wssshd_state_t *state, const wsss ...@@ -433,14 +440,13 @@ static int handle_proxy_request(int client_fd, wssshd_state_t *state, const wsss
if (!client) { if (!client) {
printf("[WEB-PROXY] No client found for hostname: %s\n", hostname); printf("[WEB-PROXY] No client found for hostname: %s\n", hostname);
send_http_error(client_fd, 404, "Not Found"); send_http_error(client_fd, 404, "Not Found");
return -1; continue;
} }
printf("[WEB-PROXY] Found client: %s\n", client->client_id); printf("[WEB-PROXY] Found client: %s\n", client->client_id);
// Check if client has web/http/https services // Check if client has web/http/https services
bool has_web_service = false; bool has_web_service = false;
const char *service_type = NULL;
char *services_copy = strdup(client->services); char *services_copy = strdup(client->services);
if (services_copy) { if (services_copy) {
char *service_token = strtok(services_copy, ","); char *service_token = strtok(services_copy, ",");
...@@ -474,7 +480,8 @@ static int handle_proxy_request(int client_fd, wssshd_state_t *state, const wsss ...@@ -474,7 +480,8 @@ static int handle_proxy_request(int client_fd, wssshd_state_t *state, const wsss
if (!has_web_service) { if (!has_web_service) {
printf("[WEB-PROXY] Client %s does not have web/http/https services\n", client->client_id); printf("[WEB-PROXY] Client %s does not have web/http/https services\n", client->client_id);
send_http_error(client_fd, 503, "Service Unavailable"); send_http_error(client_fd, 503, "Service Unavailable");
return -1; continue;
}
} }
// Determine if SSL should be used // Determine if SSL should be used
...@@ -494,7 +501,7 @@ static int handle_proxy_request(int client_fd, wssshd_state_t *state, const wsss ...@@ -494,7 +501,7 @@ static int handle_proxy_request(int client_fd, wssshd_state_t *state, const wsss
if (!proc) { if (!proc) {
printf("[WEB-PROXY] Failed to launch wsssht daemon for %s\n", hostname); printf("[WEB-PROXY] Failed to launch wsssht daemon for %s\n", hostname);
send_http_error(client_fd, 502, "Bad Gateway"); send_http_error(client_fd, 502, "Bad Gateway");
return -1; continue;
} }
// Connect to wsssht daemon // Connect to wsssht daemon
...@@ -502,7 +509,7 @@ static int handle_proxy_request(int client_fd, wssshd_state_t *state, const wsss ...@@ -502,7 +509,7 @@ static int handle_proxy_request(int client_fd, wssshd_state_t *state, const wsss
if (tunnel_fd < 0) { if (tunnel_fd < 0) {
perror("[WEB-PROXY] Failed to create socket"); perror("[WEB-PROXY] Failed to create socket");
send_http_error(client_fd, 500, "Internal Server Error"); send_http_error(client_fd, 500, "Internal Server Error");
return -1; continue;
} }
struct sockaddr_in addr; struct sockaddr_in addr;
...@@ -516,7 +523,7 @@ static int handle_proxy_request(int client_fd, wssshd_state_t *state, const wsss ...@@ -516,7 +523,7 @@ static int handle_proxy_request(int client_fd, wssshd_state_t *state, const wsss
perror("[WEB-PROXY] Failed to connect to wsssht"); perror("[WEB-PROXY] Failed to connect to wsssht");
close(tunnel_fd); close(tunnel_fd);
send_http_error(client_fd, 502, "Bad Gateway"); send_http_error(client_fd, 502, "Bad Gateway");
return -1; continue;
} }
printf("[WEB-PROXY] Connected to wsssht\n"); printf("[WEB-PROXY] Connected to wsssht\n");
...@@ -534,7 +541,7 @@ static int handle_proxy_request(int client_fd, wssshd_state_t *state, const wsss ...@@ -534,7 +541,7 @@ static int handle_proxy_request(int client_fd, wssshd_state_t *state, const wsss
} else { } else {
close(tunnel_fd); close(tunnel_fd);
send_http_error(client_fd, 502, "Bad Gateway"); send_http_error(client_fd, 502, "Bad Gateway");
return -1; continue;
} }
} else { } else {
// Set socket non-blocking for SSL timeout // Set socket non-blocking for SSL timeout
...@@ -594,7 +601,7 @@ static int handle_proxy_request(int client_fd, wssshd_state_t *state, const wsss ...@@ -594,7 +601,7 @@ static int handle_proxy_request(int client_fd, wssshd_state_t *state, const wsss
} else { } else {
close(tunnel_fd); close(tunnel_fd);
send_http_error(client_fd, 502, "Bad Gateway"); send_http_error(client_fd, 502, "Bad Gateway");
return -1; continue;
} }
} }
...@@ -620,7 +627,7 @@ static int handle_proxy_request(int client_fd, wssshd_state_t *state, const wsss ...@@ -620,7 +627,7 @@ static int handle_proxy_request(int client_fd, wssshd_state_t *state, const wsss
if (ssl_ctx) SSL_CTX_free(ssl_ctx); if (ssl_ctx) SSL_CTX_free(ssl_ctx);
close(tunnel_fd); close(tunnel_fd);
send_http_error(client_fd, 502, "Bad Gateway"); send_http_error(client_fd, 502, "Bad Gateway");
return -1; continue;
} }
// Read response from wsssht daemon and forward to client // Read response from wsssht daemon and forward to client
...@@ -671,6 +678,8 @@ static int handle_proxy_request(int client_fd, wssshd_state_t *state, const wsss ...@@ -671,6 +678,8 @@ static int handle_proxy_request(int client_fd, wssshd_state_t *state, const wsss
close(tunnel_fd); close(tunnel_fd);
printf("[WEB-PROXY] Request handled for %s (%zd bytes sent)\n", hostname, total_sent); printf("[WEB-PROXY] Request handled for %s (%zd bytes sent)\n", hostname, total_sent);
}
return 0; return 0;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment