add enum for request method for easy switch statements

This commit is contained in:
Nayan
2025-05-08 00:09:38 -04:00
parent 8c3974aad2
commit df3efbaf9a
3 changed files with 46 additions and 12 deletions

View File

@@ -2,7 +2,7 @@
http_request* create_http_request() { http_request* create_http_request() {
http_request* req = (http_request*)malloc(sizeof(http_request)); http_request* req = (http_request*)malloc(sizeof(http_request));
req->method = NULL; req->method_str = NULL;
req->url = NULL; req->url = NULL;
req->num_headers = 0; req->num_headers = 0;
req->headers = NULL; req->headers = NULL;
@@ -28,9 +28,9 @@ void free_http_request(http_request* req) {
printf("Attempting to free NULL request\n"); printf("Attempting to free NULL request\n");
return; return;
} }
if (req->method != NULL) { if (req->method_str != NULL) {
free(req->method); free(req->method_str);
req->method = NULL; req->method_str = NULL;
} }
if (req->url != NULL) { if (req->url != NULL) {
free(req->url); free(req->url);
@@ -88,9 +88,7 @@ void print_http_request(http_request* req) {
printf("Attempting to print NULL request\n"); printf("Attempting to print NULL request\n");
return; return;
} }
if (req->method == NULL) { printf("Method: %s\n", (req->method_str == NULL) ? "" : req->method_str);
}
printf("Method: %s\n", (req->method == NULL) ? "" : req->method);
printf("URL: %s\n", (req->url == NULL) ? "" : req->url); printf("URL: %s\n", (req->url == NULL) ? "" : req->url);
printf("Headers:\n"); printf("Headers:\n");
for (int i = 0; i < req->num_headers; i++) { for (int i = 0; i < req->num_headers; i++) {

View File

@@ -6,6 +6,13 @@
#include <string.h> #include <string.h>
#include <sys/types.h> #include <sys/types.h>
typedef enum {
GET,
POST,
PUT,
DELETE,
} http_method;
/** /**
* Stores a key-value pair for http headers * Stores a key-value pair for http headers
*/ */
@@ -18,7 +25,8 @@ struct header_kv {
* Stores an HTTP request * Stores an HTTP request
*/ */
struct http_request { struct http_request {
char* method; http_method method;
char* method_str;
char* url; char* url;
int num_headers; int num_headers;
struct header_kv* headers; struct header_kv* headers;

View File

@@ -22,6 +22,19 @@ void handle_request(char* request, int length, char* response,
// request_info_print(req); // request_info_print(req);
free_http_request(req); free_http_request(req);
// Switch statement to handle different request types
// switch (req->method)
// {
// case GET:
// /* code */
// break;
// default:
// break;
// }
// Create response
// Create reponse string // Create reponse string
char* ptr_temp = response; char* ptr_temp = response;
char* temp = char* temp =
@@ -43,16 +56,31 @@ void parse_http_request(char* request, int length, struct http_request* req) {
return; return;
} }
// Extract request type // Extract request method
char* type_start = request; char* type_start = request;
char* type_end = strstr(type_start, " "); char* type_end = strstr(type_start, " ");
if (type_end == NULL) { if (type_end == NULL) {
printf("Invalid packet (method)\n"); printf("Invalid packet (method)\n");
return; return;
} }
req->method = (char*)malloc(type_end - type_start + 1); char* type = (char*)malloc(type_end - type_start + 1);
strncpy(req->method, type_start, type_end - type_start); strncpy(type, type_start, type_end - type_start);
req->method[type_end - type_start] = '\0'; type[type_end - type_start] = '\0';
if (strcmp(type, "GET") == 0) {
req->method = GET;
} else if (strcmp(type, "POST") == 0) {
req->method = POST;
} else if (strcmp(type, "PUT") == 0) {
req->method = PUT;
} else if (strcmp(type, "DELETE") == 0) {
req->method = DELETE;
} else {
printf("Invalid packet (method)\n");
return;
}
req->method_str = (char*)malloc(type_end - type_start + 1);
strncpy(req->method_str, type_start, type_end - type_start);
req->method_str[type_end - type_start] = '\0';
// Extract URL // Extract URL
char* url_start = type_end + 1; char* url_start = type_end + 1;