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);
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) {

View File

@@ -6,6 +6,7 @@
#include <sys/types.h>
#include "http_stuff.h"
#include "response_builder.h"
void handle_request(char* request, int length, char* response,
size_t* response_length);