From 779f97ddbb8b2dfe86fcdcdcd2d60dfac4968fd6 Mon Sep 17 00:00:00 2001 From: Nayan <33187059+GShadow5@users.noreply.github.com> Date: Thu, 8 May 2025 17:04:45 -0400 Subject: [PATCH] update request handler to use response builder --- request_handler.c | 55 ++++++++++++++++++++++++++++++++--------------- request_handler.h | 1 + 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/request_handler.c b/request_handler.c index 1b66671..95c5647 100644 --- a/request_handler.c +++ b/request_handler.c @@ -20,32 +20,53 @@ void handle_request(char* request, int length, char* response, print_http_request(req); printf("---------\n"); // 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; + switch (req->method) { + case GET: + // Build response + http_response* res = create_http_response(); + response_handle_get(req, res); + // 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); + // Set response length + *response_length = strlen(response_string); + // Free response + free_http_response(res); + break; - // default: - // break; - // } + case POST: + /* code */ + break; + + case PUT: + /* code */ + break; + + case DELETE: + /* code */ + break; + } // Create response // Create reponse 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); + // 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); + // printf("Response --------\n"); + // printf("%s\n--------\n", response); - *response_length = strlen(response); + // *response_length = strlen(response); + + free_http_request(req); } void parse_http_request(char* request, int length, struct http_request* req) { diff --git a/request_handler.h b/request_handler.h index dc75936..8f9d0c1 100644 --- a/request_handler.h +++ b/request_handler.h @@ -6,6 +6,7 @@ #include #include "http_stuff.h" +#include "response_builder.h" void handle_request(char* request, int length, char* response, size_t* response_length);