-
Notifications
You must be signed in to change notification settings - Fork 606
Stefano - C-Web-Server #309
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
4c84155
0199b0d
54b34ea
4f77b70
2366a52
ac57b4e
14246da
e26f8e3
ed27e12
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,4 +72,4 @@ void file_free(struct file_data *filedata) | |
{ | ||
free(filedata->data); | ||
free(filedata); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,12 +52,30 @@ int send_response(int fd, char *header, char *content_type, void *body, int cont | |
{ | ||
const int max_response_size = 262144; | ||
char response[max_response_size]; | ||
|
||
|
||
// Generate date | ||
time_t rawtime; | ||
struct tm *current_time; | ||
time(&rawtime); | ||
current_time = localtime(&rawtime); | ||
|
||
// Build HTTP response and store it in response | ||
|
||
/////////////////// | ||
// IMPLEMENT ME! // | ||
/////////////////// | ||
sprintf(response, | ||
"%s\n" | ||
"Date: %s" // Example: Wed Dec 20 13:05:11 PST 2017 | ||
"Connection: close\n" | ||
"Content-Length: %d\n" | ||
"Content-Type: %s\n" | ||
"\n" | ||
"%s", | ||
header, | ||
asctime(current_time), | ||
content_length, | ||
content_type, | ||
body | ||
); | ||
|
||
int response_length = strlen(response); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can also set |
||
|
||
// Send it all! | ||
int rv = send(fd, response, response_length, 0); | ||
|
@@ -76,16 +94,13 @@ int send_response(int fd, char *header, char *content_type, void *body, int cont | |
void get_d20(int fd) | ||
{ | ||
// Generate a random number between 1 and 20 inclusive | ||
srand(time(NULL)); | ||
char random_number_string[20]; | ||
int random_number = rand() % 20; | ||
sprintf(random_number_string, "%d", random_number); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's smart to print this to a string because of what |
||
|
||
/////////////////// | ||
// IMPLEMENT ME! // | ||
/////////////////// | ||
|
||
// Use send_response() to send it back as text/plain data | ||
|
||
/////////////////// | ||
// IMPLEMENT ME! // | ||
/////////////////// | ||
send_response(fd, "HTTP/1.1 200 OK", "text/plain", random_number_string, strlen(random_number_string)); | ||
} | ||
|
||
/** | ||
|
@@ -94,7 +109,7 @@ void get_d20(int fd) | |
void resp_404(int fd) | ||
{ | ||
char filepath[4096]; | ||
struct file_data *filedata; | ||
struct file_data *filedata; | ||
char *mime_type; | ||
|
||
// Fetch the 404.html file | ||
|
@@ -119,9 +134,26 @@ void resp_404(int fd) | |
*/ | ||
void get_file(int fd, struct cache *cache, char *request_path) | ||
{ | ||
/////////////////// | ||
// IMPLEMENT ME! // | ||
/////////////////// | ||
char filepath[4096]; | ||
struct file_data *filedata; | ||
char *mime_type; | ||
struct cache_entry *cached_file = cache_get(cache, request_path); | ||
|
||
if (cached_file != NULL) { | ||
send_response(fd, "HTTP/1.1 200 OK", cached_file->content_type, cached_file->content, cached_file->content_length); | ||
} else { | ||
sprintf(filepath, "./serverroot%s", request_path); | ||
|
||
mime_type = mime_type_get(filepath); | ||
|
||
filedata = file_load(filepath); | ||
|
||
send_response(fd, "HTTP/1.1 200 OK", mime_type, filedata->data, filedata->size); | ||
|
||
cache_put(cache, request_path, mime_type, filedata->data, filedata->size); | ||
|
||
file_free(filedata); | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -144,6 +176,8 @@ void handle_http_request(int fd, struct cache *cache) | |
{ | ||
const int request_buffer_size = 65536; // 64K | ||
char request[request_buffer_size]; | ||
char method[200]; | ||
char path[8192]; | ||
|
||
// Read request | ||
int bytes_recvd = recv(fd, request, request_buffer_size - 1, 0); | ||
|
@@ -153,19 +187,22 @@ void handle_http_request(int fd, struct cache *cache) | |
return; | ||
} | ||
|
||
|
||
/////////////////// | ||
// IMPLEMENT ME! // | ||
/////////////////// | ||
|
||
// Read the three components of the first request line | ||
|
||
sscanf(request, "%s %s", method, path); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You'll also want to add an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the feedback Grant! I'll try to incorporate it today. |
||
|
||
// If GET, handle the get endpoints | ||
|
||
// Check if it's /d20 and handle that special case | ||
// Otherwise serve the requested file by calling get_file() | ||
|
||
|
||
if (strcmp(method, "GET") == 0) { | ||
if (strcmp(path, "/d20") == 0) { | ||
// Check if it's /d20 and handle that special case | ||
get_d20(fd); | ||
} else if (path != NULL) { | ||
// Otherwise serve the requested file by calling get_file() | ||
get_file(fd, cache, path); | ||
} else { | ||
resp_404(fd); | ||
} | ||
} | ||
|
||
// (Stretch) If POST, handle the post request | ||
} | ||
|
||
|
@@ -187,7 +224,7 @@ int main(void) | |
fprintf(stderr, "webserver: fatal error getting listening socket\n"); | ||
exit(1); | ||
} | ||
|
||
printf("webserver: waiting for connections on port %s...\n", PORT); | ||
|
||
// This is the main loop that accepts incoming connections and | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is pretty much exactly what I have. Well done.