diff --git a/chttp.c b/chttp.c index 6b319ca..3c931d8 100644 --- a/chttp.c +++ b/chttp.c @@ -3879,28 +3879,21 @@ void http_request_builder_url(HTTP_RequestBuilder builder, if (conn->state != HTTP_CLIENT_CONN_WAIT_LINE) return; // Request line already written - // Parse the URL to extract components + // Allocate a copy of the URL string so the parsed URL pointers remain valid + char *url_copy = malloc(url.len); + if (url_copy == NULL) + return; // TODO: set error + memcpy(url_copy, url.ptr, url.len); + + // Parse the copied URL HTTP_URL parsed_url; - if (http_parse_url(url.ptr, url.len, &parsed_url) != 1) + if (http_parse_url(url_copy, url.len, &parsed_url) != 1) { + free(url_copy); return; // TODO: set error + } - HTTP_String domain = parsed_url.authority.host.text; - HTTP_String path = parsed_url.path; - char *p = malloc(domain.len + path.len); - if (p == NULL) - return; // TODO: set error - - memcpy(p, domain.ptr, domain.len); - domain.ptr = p; - p += domain.len; - - memcpy(p, path.ptr, path.len); - path.ptr = p; - p += path.len; - - conn->domain = domain; - conn->path = path; - conn->url = parsed_url; + conn->url_buffer = url_copy; + conn->url = parsed_url; // Write method HTTP_String method_str = get_method_string(method); @@ -4065,7 +4058,7 @@ int http_request_builder_send(HTTP_RequestBuilder builder) error: conn->state = HTTP_CLIENT_CONN_FREE; - free(conn->domain.ptr); + free(conn->url_buffer); byte_queue_free(&conn->input); byte_queue_free(&conn->output); client->num_conns--; @@ -4248,8 +4241,8 @@ int http_client_process_events(HTTP_Client *client, save_cookies(&client->cookie_jar, conn->response.headers, conn->response.num_headers, - conn->domain, - conn->path); + conn->url.authority.host.text, + conn->url.path); // TODO: Handle redirects here } @@ -4313,7 +4306,7 @@ void http_free_response(HTTP_Response *response) return; conn->state = HTTP_CLIENT_CONN_FREE; - free(conn->domain.ptr); + free(conn->url_buffer); byte_queue_free(&conn->input); byte_queue_free(&conn->output); client->num_conns--; diff --git a/chttp.h b/chttp.h index bae296c..2b2973c 100644 --- a/chttp.h +++ b/chttp.h @@ -894,12 +894,10 @@ typedef struct { // TODO: comment bool trace_bytes; - // Endpoint domain and path. These must be saved - // in case the server wants to set some cookies. - HTTP_String domain; - HTTP_String path; - // Parsed URL for connection establishment + // The url_buffer contains the allocated copy of the URL string, + // and url.* pointers reference into this buffer + char *url_buffer; HTTP_URL url; // Data received from the server diff --git a/src/client.c b/src/client.c index 2e5bf16..1b77d26 100644 --- a/src/client.c +++ b/src/client.c @@ -218,28 +218,21 @@ void http_request_builder_url(HTTP_RequestBuilder builder, if (conn->state != HTTP_CLIENT_CONN_WAIT_LINE) return; // Request line already written - // Parse the URL to extract components + // Allocate a copy of the URL string so the parsed URL pointers remain valid + char *url_copy = malloc(url.len); + if (url_copy == NULL) + return; // TODO: set error + memcpy(url_copy, url.ptr, url.len); + + // Parse the copied URL HTTP_URL parsed_url; - if (http_parse_url(url.ptr, url.len, &parsed_url) != 1) + if (http_parse_url(url_copy, url.len, &parsed_url) != 1) { + free(url_copy); return; // TODO: set error + } - HTTP_String domain = parsed_url.authority.host.text; - HTTP_String path = parsed_url.path; - char *p = malloc(domain.len + path.len); - if (p == NULL) - return; // TODO: set error - - memcpy(p, domain.ptr, domain.len); - domain.ptr = p; - p += domain.len; - - memcpy(p, path.ptr, path.len); - path.ptr = p; - p += path.len; - - conn->domain = domain; - conn->path = path; - conn->url = parsed_url; + conn->url_buffer = url_copy; + conn->url = parsed_url; // Write method HTTP_String method_str = get_method_string(method); @@ -404,7 +397,7 @@ int http_request_builder_send(HTTP_RequestBuilder builder) error: conn->state = HTTP_CLIENT_CONN_FREE; - free(conn->domain.ptr); + free(conn->url_buffer); byte_queue_free(&conn->input); byte_queue_free(&conn->output); client->num_conns--; @@ -587,8 +580,8 @@ int http_client_process_events(HTTP_Client *client, save_cookies(&client->cookie_jar, conn->response.headers, conn->response.num_headers, - conn->domain, - conn->path); + conn->url.authority.host.text, + conn->url.path); // TODO: Handle redirects here } @@ -652,7 +645,7 @@ void http_free_response(HTTP_Response *response) return; conn->state = HTTP_CLIENT_CONN_FREE; - free(conn->domain.ptr); + free(conn->url_buffer); byte_queue_free(&conn->input); byte_queue_free(&conn->output); client->num_conns--; diff --git a/src/client.h b/src/client.h index f801833..56dc09e 100644 --- a/src/client.h +++ b/src/client.h @@ -75,12 +75,10 @@ typedef struct { // TODO: comment bool trace_bytes; - // Endpoint domain and path. These must be saved - // in case the server wants to set some cookies. - HTTP_String domain; - HTTP_String path; - // Parsed URL for connection establishment + // The url_buffer contains the allocated copy of the URL string, + // and url.* pointers reference into this buffer + char *url_buffer; HTTP_URL url; // Data received from the server