mirror of
https://github.com/opus-tango/http-server-in-c.git
synced 2026-03-20 03:55:25 +00:00
fix string and pointer bugs to actually send 404
This commit is contained in:
@@ -28,11 +28,11 @@ void* client_handler(void* args) {
|
||||
buffer[bytes_read] = '\0';
|
||||
|
||||
// Allocate space for response
|
||||
char* response = (char*)malloc(BUFFER_SIZE);
|
||||
char* response = NULL;
|
||||
size_t response_length = BUFFER_SIZE;
|
||||
|
||||
// Handle request
|
||||
handle_request(buffer, bytes_read, response, &response_length);
|
||||
handle_request(buffer, bytes_read, &response, &response_length);
|
||||
|
||||
// Send response
|
||||
send(client, response, response_length, 0);
|
||||
|
||||
@@ -144,6 +144,7 @@ char* response_to_string(http_response* res) {
|
||||
|
||||
char* response = (char*)malloc(total_length);
|
||||
strcpy(response, res->status_line);
|
||||
strcat(response, "\r\n");
|
||||
for (int i = 0; i < res->num_headers; i++) {
|
||||
strcat(response, res->headers[i].key);
|
||||
strcat(response, ": ");
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
void parse_http_request(char* request, int length, struct http_request* req);
|
||||
void debug_print_request(char* request);
|
||||
|
||||
void handle_request(char* request, int length, char* response,
|
||||
void handle_request(char* request, int length, char** response,
|
||||
size_t* response_length) {
|
||||
// Terminate request with EOF so strtok stops at end of string
|
||||
request[length] = EOF;
|
||||
@@ -13,12 +13,12 @@ void handle_request(char* request, int length, char* response,
|
||||
|
||||
// debug_print_request(request);
|
||||
|
||||
printf("parsing request ---------\n");
|
||||
// printf("parsing request ---------\n");
|
||||
// Parse request into struct
|
||||
http_request* req = create_http_request();
|
||||
parse_http_request(request, length, req);
|
||||
print_http_request(req);
|
||||
printf("---------\n");
|
||||
// print_http_request(req);
|
||||
// printf("---------\n");
|
||||
// request_info_print(req);
|
||||
// free_http_request(req);
|
||||
|
||||
@@ -31,8 +31,9 @@ void handle_request(char* request, int length, char* response,
|
||||
// Convert response to string
|
||||
char* response_string = response_to_string(res);
|
||||
// Copy string to response
|
||||
response = (char*)malloc(strlen(response_string) + 1);
|
||||
strcpy(response, response_string);
|
||||
char* responsestr = (char*)malloc(strlen(response_string) + 1);
|
||||
strcpy(responsestr, response_string);
|
||||
*response = responsestr;
|
||||
// Set response length
|
||||
*response_length = strlen(response_string);
|
||||
// Free response
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#include "http_stuff.h"
|
||||
#include "response_builder.h"
|
||||
|
||||
void handle_request(char* request, int length, char* response,
|
||||
void handle_request(char* request, int length, char** response,
|
||||
size_t* response_length);
|
||||
|
||||
#endif
|
||||
@@ -9,6 +9,7 @@ void response_handle_get(http_request* req, http_response* res) {
|
||||
printf("%s\n", file_path);
|
||||
|
||||
serve_404(res);
|
||||
return;
|
||||
|
||||
// Determine the file type
|
||||
char* ptr = file_path + strlen(file_path) - 1;
|
||||
|
||||
Reference in New Issue
Block a user