update readme and actually add responses to non-GET requests

This commit is contained in:
Nayan
2025-05-08 20:07:08 -04:00
parent 8d60863be3
commit 6a17569284
7 changed files with 163 additions and 3 deletions

34
public/400.html Normal file
View File

@@ -0,0 +1,34 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
#root {
width: 100vw;
height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
h1 {
font-size: 3rem;
}
p {
font-size: 1.5rem;
}
a {
font-size: 1.5rem;
}
</style>
</head>
<body>
<div id="root">
<h1>Error 400: Bad Request</h1>
</div>
</body>

34
public/403.html Normal file
View File

@@ -0,0 +1,34 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
#root {
width: 100vw;
height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
h1 {
font-size: 3rem;
}
p {
font-size: 1.5rem;
}
a {
font-size: 1.5rem;
}
</style>
</head>
<body>
<div id="root">
<h1>Error 403: Forbidden</h1>
</div>
</body>

34
public/501.html Normal file
View File

@@ -0,0 +1,34 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
#root {
width: 100vw;
height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
h1 {
font-size: 3rem;
}
p {
font-size: 1.5rem;
}
a {
font-size: 1.5rem;
}
</style>
</head>
<body>
<div id="root">
<h1>Error 501: Not Implemented</h1>
</div>
</body>

View File

@@ -3,6 +3,10 @@ The HTTP server has gotten much larger and more complex than I originally antici
This evening I finally worked through the bugs on handling file serving, and have been able to get it working with arbitrary file types. They are still restricted "for security reasons," but it can theoretically be expanded to any file type. This evening I finally worked through the bugs on handling file serving, and have been able to get it working with arbitrary file types. They are still restricted "for security reasons," but it can theoretically be expanded to any file type.
The server does not handle routes, only raw file paths, and also does not include the templating engine that I was planning to include, but I simply ran out of time. After doing a little research I have also decided to scrap the idea of implementing file uploading, since parsing streamed files is a lot more complex than I can tackle in the time I have left.
I will implement logging to a log file to satisfy the file writing requirement.
## Final Project Plan Update ## Final Project Plan Update
Even though I haven't had time to work on it, I have been turning this project over in my mind for the past two weeks. I have wanted to build my own basic HTTP server for a while, and this is the only piece of homework due this week, so I have decided to make my final project a basic HTTP server. Even though I haven't had time to work on it, I have been turning this project over in my mind for the past two weeks. I have wanted to build my own basic HTTP server for a while, and this is the only piece of homework due this week, so I have decided to make my final project a basic HTTP server.

View File

@@ -19,15 +19,15 @@ void handle_request(char* request, int length, http_response* response) {
break; break;
case POST: case POST:
/* code */ response_handle_post(req, response);
break; break;
case PUT: case PUT:
/* code */ response_handle_put(req, response);
break; break;
case DELETE: case DELETE:
/* code */ response_handle_delete(req, response);
break; break;
} }

View File

@@ -29,6 +29,27 @@ 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);
serve_501(res);
}
void response_handle_delete(http_request* req, http_response* res) {
// TODO
printf("DELETE request\n");
printf("%s\n", 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);
serve_501(res);
}
void response_build_static_file(char* file_path, content_type content_type, void response_build_static_file(char* file_path, content_type content_type,
status_code status_code, http_response* res) { status_code status_code, http_response* res) {
// Open the file and verify that the file exists // Open the file and verify that the file exists
@@ -85,6 +106,10 @@ void response_build_static_file(char* file_path, content_type content_type,
strcat(status_line, "500 Internal Server Error"); strcat(status_line, "500 Internal Server Error");
break; break;
case FORBIDDEN:
strcat(status_line, "403 Forbidden");
break;
default: default:
strcat(status_line, "501 Not Implemented"); strcat(status_line, "501 Not Implemented");
break; break;
@@ -148,3 +173,18 @@ void serve_500(http_response* res) {
char* file_path = "./public/500.html"; char* file_path = "./public/500.html";
response_build_static_file(file_path, HTML, INTERNAL_SERVER_ERROR, res); response_build_static_file(file_path, HTML, INTERNAL_SERVER_ERROR, res);
} }
void serve_403(http_response* res) {
char* file_path = "./public/403.html";
response_build_static_file(file_path, HTML, FORBIDDEN, res);
}
void serve_501(http_response* res) {
char* file_path = "./public/501.html";
response_build_static_file(file_path, HTML, NOT_IMPLEMENTED, res);
}
void serve_400(http_response* res) {
char* file_path = "./public/400.html";
response_build_static_file(file_path, HTML, BAD_REQUEST, res);
}

View File

@@ -20,10 +20,18 @@ typedef enum {
NOT_FOUND, NOT_FOUND,
BAD_REQUEST, BAD_REQUEST,
INTERNAL_SERVER_ERROR, INTERNAL_SERVER_ERROR,
FORBIDDEN,
NOT_IMPLEMENTED,
} status_code; } status_code;
void response_handle_get(http_request* req, http_response* res); void response_handle_get(http_request* req, http_response* res);
void response_handle_post(http_request* req, http_response* res);
void response_handle_delete(http_request* req, http_response* res);
void response_handle_put(http_request* req, http_response* res);
void response_build_static_file(char* file_path, content_type content_type, void response_build_static_file(char* file_path, content_type content_type,
status_code status_code, http_response* res); status_code status_code, http_response* res);
@@ -31,4 +39,10 @@ void serve_404(http_response* res);
void serve_500(http_response* res); void serve_500(http_response* res);
void serve_403(http_response* res);
void serve_501(http_response* res);
void serve_400(http_response* res);
#endif #endif