update request handler to use response builder

This commit is contained in:
Nayan
2025-05-08 17:04:45 -04:00
parent 1a74c87c53
commit 779f97ddbb
2 changed files with 39 additions and 17 deletions

View File

@@ -20,32 +20,53 @@ void handle_request(char* request, int length, char* response,
print_http_request(req); print_http_request(req);
printf("---------\n"); printf("---------\n");
// request_info_print(req); // request_info_print(req);
free_http_request(req); // free_http_request(req);
// Switch statement to handle different request types // Switch statement to handle different request types
// switch (req->method) switch (req->method) {
// { case GET:
// case GET: // Build response
// /* code */ http_response* res = create_http_response();
// break; 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: case POST:
// break; /* code */
// } break;
case PUT:
/* code */
break;
case DELETE:
/* code */
break;
}
// Create response // Create response
// Create reponse string // Create reponse string
char* ptr_temp = response; // char* ptr_temp = response;
char* temp = // char* temp =
"HTTP/1.1 200 OK\r\nContent-Type: text/html \r\nContent-Length: " // "HTTP/1.1 200 OK\r\nContent-Type: text/html \r\nContent-Length: "
"0\r\n\r\n\0"; // "0\r\n\r\n\0";
strcpy(ptr_temp, temp); // strcpy(ptr_temp, temp);
printf("Response --------\n"); // printf("Response --------\n");
printf("%s\n--------\n", response); // 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) { void parse_http_request(char* request, int length, struct http_request* req) {

View File

@@ -6,6 +6,7 @@
#include <sys/types.h> #include <sys/types.h>
#include "http_stuff.h" #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); size_t* response_length);