mirror of
https://github.com/opus-tango/http-server-in-c.git
synced 2026-03-20 12:05:25 +00:00
switched over to logging
This commit is contained in:
@@ -1,12 +1,6 @@
|
||||
#include "request_handler.h"
|
||||
|
||||
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, http_response* response) {
|
||||
// Terminate request with EOF so strtok stops at end of string
|
||||
request[length] = EOF;
|
||||
|
||||
// Parse request into struct
|
||||
http_request* req = create_http_request();
|
||||
parse_http_request(request, length, req);
|
||||
@@ -14,7 +8,6 @@ void handle_request(char* request, int length, http_response* response) {
|
||||
// Switch statement to handle different request types
|
||||
switch (req->method) {
|
||||
case GET:
|
||||
// Build response
|
||||
response_handle_get(req, response);
|
||||
break;
|
||||
|
||||
@@ -31,20 +24,6 @@ void handle_request(char* request, int length, http_response* response) {
|
||||
break;
|
||||
}
|
||||
|
||||
// Create response
|
||||
|
||||
// Create response string
|
||||
// char* ptr_temp = response;
|
||||
// char* temp =
|
||||
// "HTTP/1.1 200 OK\r\nContent-Type: text/html \r\nContent-Length: "
|
||||
// "0\r\n\r\n\0";
|
||||
// strcpy(ptr_temp, temp);
|
||||
|
||||
// printf("Response --------\n");
|
||||
// printf("%s\n--------\n", response);
|
||||
|
||||
// *response_length = strlen(response);
|
||||
|
||||
free_http_request(req);
|
||||
}
|
||||
|
||||
@@ -52,7 +31,7 @@ void parse_http_request(char* request, int length, struct http_request* req) {
|
||||
// Get the end of the first line
|
||||
char* request_line_end = strstr(request, "\r\n");
|
||||
if (request_line_end == NULL) {
|
||||
printf("Invalid packet (end first line)\n");
|
||||
log_message(LOG_ERROR, "Invalid packet (request line), cannot parse");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -60,7 +39,7 @@ void parse_http_request(char* request, int length, struct http_request* req) {
|
||||
char* type_start = request;
|
||||
char* type_end = strstr(type_start, " ");
|
||||
if (type_end == NULL) {
|
||||
printf("Invalid packet (method)\n");
|
||||
log_message(LOG_ERROR, "Invalid packet (method), cannot parse");
|
||||
return;
|
||||
}
|
||||
char* type = (char*)malloc(type_end - type_start + 1);
|
||||
@@ -75,7 +54,7 @@ void parse_http_request(char* request, int length, struct http_request* req) {
|
||||
} else if (strcmp(type, "DELETE") == 0) {
|
||||
req->method = DELETE;
|
||||
} else {
|
||||
printf("Invalid packet (method)\n");
|
||||
log_message(LOG_ERROR, "Invalid packet (method), cannot parse");
|
||||
return;
|
||||
}
|
||||
req->method_str = (char*)malloc(type_end - type_start + 1);
|
||||
@@ -86,7 +65,7 @@ void parse_http_request(char* request, int length, struct http_request* req) {
|
||||
char* url_start = type_end + 1;
|
||||
char* url_end = strstr(url_start, " ");
|
||||
if (url_end == NULL) {
|
||||
printf("Invalid packet (url)\n");
|
||||
log_message(LOG_ERROR, "Invalid packet (url), cannot parse");
|
||||
return;
|
||||
}
|
||||
req->url = (char*)malloc(url_end - url_start + 1);
|
||||
@@ -96,12 +75,12 @@ void parse_http_request(char* request, int length, struct http_request* req) {
|
||||
// Extract headers
|
||||
char* headers_end = strstr(request_line_end + 2, "\r\n\r\n");
|
||||
if (headers_end == NULL) {
|
||||
printf("Invalid packet (headers)\n");
|
||||
log_message(LOG_ERROR, "Invalid packet (headers), cannot parse");
|
||||
return;
|
||||
}
|
||||
char* headers_start = request_line_end + 2;
|
||||
if (headers_start == NULL) {
|
||||
printf("Invalid packet (headers)\n");
|
||||
log_message(LOG_ERROR, "Invalid packet (headers), cannot parse");
|
||||
return;
|
||||
}
|
||||
req->num_headers = 0;
|
||||
@@ -113,7 +92,7 @@ void parse_http_request(char* request, int length, struct http_request* req) {
|
||||
}
|
||||
char* delim = strstr(header_start, ": ");
|
||||
if (delim == NULL) {
|
||||
printf("Invalid packet (headers)\n");
|
||||
log_message(LOG_ERROR, "Invalid packet (headers), cannot parse");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -132,29 +111,4 @@ void parse_http_request(char* request, int length, struct http_request* req) {
|
||||
req->body[length - (body_start - request)] = '\0';
|
||||
|
||||
return;
|
||||
};
|
||||
|
||||
void debug_print_request(char* request) {
|
||||
// Copy request
|
||||
char* request_copy = (char*)malloc(strlen(request) + 1);
|
||||
strcpy(request_copy, request);
|
||||
|
||||
// Iterate through request and print "\\r\\n" before each \r\n
|
||||
// and "\\r\\n\\r\\n" before each \r\n\r\n
|
||||
char* ptr = request_copy;
|
||||
while (*ptr != '\0') {
|
||||
if (*ptr == '\r' && *(ptr + 1) == '\n') {
|
||||
if (*(ptr + 2) == '\r' && *(ptr + 3) == '\n') {
|
||||
printf("\\r\\n\\r\\n\n");
|
||||
ptr += 4;
|
||||
} else {
|
||||
printf("\\r\\n\n");
|
||||
ptr += 2;
|
||||
}
|
||||
} else {
|
||||
printf("%c", *ptr);
|
||||
ptr++;
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user