mirror of
https://github.com/opus-tango/http-server-in-c.git
synced 2026-03-20 03:55:25 +00:00
switched over to logging
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
#include "response_builder.h"
|
||||
|
||||
void response_handle_get(http_request* req, http_response* res) {
|
||||
log_message(LOG_INFO, "GET request %s", req->url);
|
||||
// Extract the path from the request URL
|
||||
char* basepath = "./public";
|
||||
char* file_path = (char*)malloc(strlen(req->url) + strlen(basepath) + 1);
|
||||
strcpy(file_path, basepath);
|
||||
strcat(file_path, req->url);
|
||||
printf("%s\n", file_path);
|
||||
log_message(LOG_INFO, "Path: %s", file_path);
|
||||
|
||||
// Determine the file type
|
||||
char* ptr = file_path + strlen(file_path) - 1;
|
||||
@@ -31,22 +32,19 @@ void response_handle_get(http_request* req, http_response* res) {
|
||||
|
||||
void response_handle_post(http_request* req, http_response* res) {
|
||||
// TODO
|
||||
printf("POST request\n");
|
||||
printf("%s\n", req->url);
|
||||
log_message(LOG_INFO, "POST request %s", req->url);
|
||||
serve_501(res);
|
||||
}
|
||||
|
||||
void response_handle_delete(http_request* req, http_response* res) {
|
||||
// TODO
|
||||
printf("DELETE request\n");
|
||||
printf("%s\n", req->url);
|
||||
log_message(LOG_INFO, "DELETE request %s", req->url);
|
||||
serve_501(res);
|
||||
}
|
||||
|
||||
void response_handle_put(http_request* req, http_response* res) {
|
||||
// TODO
|
||||
printf("PUT request\n");
|
||||
printf("%s\n", req->url);
|
||||
log_message(LOG_INFO, "PUT request %s", req->url);
|
||||
serve_501(res);
|
||||
}
|
||||
|
||||
@@ -55,7 +53,7 @@ void response_build_static_file(char* file_path, content_type content_type,
|
||||
// Open the file and verify that the file exists
|
||||
FILE* file = fopen(file_path, "rb");
|
||||
if (file == NULL) {
|
||||
printf("fopen failed to find file %s\n", file_path);
|
||||
log_message(LOG_WARN, "404:fopen failed to find file %s\n", file_path);
|
||||
serve_404(res);
|
||||
return;
|
||||
}
|
||||
@@ -68,13 +66,13 @@ void response_build_static_file(char* file_path, content_type content_type,
|
||||
// Read file into buffer
|
||||
char* file_buffer = (char*)malloc(file_size);
|
||||
if (file_buffer == NULL) {
|
||||
printf("malloc failed\n");
|
||||
log_message(LOG_WARN, "500: malloc for file failed");
|
||||
serve_500(res);
|
||||
return;
|
||||
}
|
||||
size_t bytes_read = fread(file_buffer, file_size, 1, file);
|
||||
if (bytes_read != 1) {
|
||||
printf("fread failed\n");
|
||||
log_message(LOG_WARN, "500: fread for file failed");
|
||||
serve_500(res);
|
||||
return;
|
||||
}
|
||||
@@ -154,14 +152,7 @@ void response_build_static_file(char* file_path, content_type content_type,
|
||||
// Set content type string
|
||||
res->content_type = content_type_str;
|
||||
|
||||
// Add header for close connection
|
||||
header_kv* close_connection = (header_kv*)malloc(sizeof(header_kv));
|
||||
close_connection->key = (char*)malloc(strlen("Connection") + 1);
|
||||
close_connection->value = (char*)malloc(strlen("close") + 1);
|
||||
strcpy(close_connection->key, "Connection");
|
||||
strcpy(close_connection->value, "close");
|
||||
res->headers = close_connection;
|
||||
res->num_headers = 1;
|
||||
log_message(LOG_INFO, "Serving %s\n", file_path);
|
||||
}
|
||||
|
||||
void serve_404(http_response* res) {
|
||||
|
||||
Reference in New Issue
Block a user