mirror of
https://github.com/opus-tango/http-server-in-c.git
synced 2026-03-20 03:55:25 +00:00
update readme and actually add responses to non-GET requests
This commit is contained in:
34
public/400.html
Normal file
34
public/400.html
Normal 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
34
public/403.html
Normal 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
34
public/501.html
Normal 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>
|
||||
@@ -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.
|
||||
|
||||
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
|
||||
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.
|
||||
|
||||
|
||||
@@ -19,15 +19,15 @@ void handle_request(char* request, int length, http_response* response) {
|
||||
break;
|
||||
|
||||
case POST:
|
||||
/* code */
|
||||
response_handle_post(req, response);
|
||||
break;
|
||||
|
||||
case PUT:
|
||||
/* code */
|
||||
response_handle_put(req, response);
|
||||
break;
|
||||
|
||||
case DELETE:
|
||||
/* code */
|
||||
response_handle_delete(req, response);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@@ -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,
|
||||
status_code status_code, http_response* res) {
|
||||
// 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");
|
||||
break;
|
||||
|
||||
case FORBIDDEN:
|
||||
strcat(status_line, "403 Forbidden");
|
||||
break;
|
||||
|
||||
default:
|
||||
strcat(status_line, "501 Not Implemented");
|
||||
break;
|
||||
@@ -147,4 +172,19 @@ void serve_404(http_response* res) {
|
||||
void serve_500(http_response* res) {
|
||||
char* file_path = "./public/500.html";
|
||||
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);
|
||||
}
|
||||
@@ -20,10 +20,18 @@ typedef enum {
|
||||
NOT_FOUND,
|
||||
BAD_REQUEST,
|
||||
INTERNAL_SERVER_ERROR,
|
||||
FORBIDDEN,
|
||||
NOT_IMPLEMENTED,
|
||||
} status_code;
|
||||
|
||||
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,
|
||||
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_403(http_response* res);
|
||||
|
||||
void serve_501(http_response* res);
|
||||
|
||||
void serve_400(http_response* res);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user