-
Notifications
You must be signed in to change notification settings - Fork 605
commit #311
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
Open
DavidBenavidez123
wants to merge
1
commit into
bloominstituteoftechnology:master
Choose a base branch
from
DavidBenavidez123:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
commit #311
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,7 @@ | |
#include "mime.h" | ||
#include "cache.h" | ||
|
||
#define PORT "3490" // the port users will be connecting to | ||
#define PORT "3490" // the port users will be connecting to | ||
|
||
#define SERVER_FILES "./serverfiles" | ||
#define SERVER_ROOT "./serverroot" | ||
|
@@ -52,31 +52,51 @@ 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]; | ||
time_t time_res; | ||
struct tm *timestamp; | ||
char buffer[50]; | ||
time(&time_res); | ||
|
||
timestamp = localtime(&time_res); | ||
strftime(buffer, 50, "%a %b %d %T %Z %Y", timestamp); | ||
|
||
// Build HTTP response and store it in response | ||
|
||
/////////////////// | ||
// IMPLEMENT ME! // | ||
/////////////////// | ||
char *new_body = body; | ||
|
||
sprintf(response, "%s\n" | ||
"Date: %s\n" | ||
"Connection: close\n" | ||
"Content-Length: %d\n" | ||
"Content-Type: %s\n" | ||
"\n" | ||
"%s\n", | ||
header, buffer, content_length, content_type, new_body); | ||
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. This is looking good! You have the time in there, too, well done. |
||
|
||
int response_length = strlen(response); | ||
|
||
printf("Response: %s\n", response); | ||
// Send it all! | ||
int rv = send(fd, response, response_length, 0); | ||
|
||
if (rv < 0) { | ||
if (rv < 0) | ||
{ | ||
perror("send"); | ||
} | ||
|
||
return rv; | ||
} | ||
|
||
|
||
/** | ||
* Send a /d20 endpoint response | ||
*/ | ||
void get_d20(int fd) | ||
{ | ||
// Generate a random number between 1 and 20 inclusive | ||
|
||
/////////////////// | ||
// IMPLEMENT ME! // | ||
/////////////////// | ||
|
@@ -94,14 +114,15 @@ 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 | ||
snprintf(filepath, sizeof filepath, "%s/404.html", SERVER_FILES); | ||
filedata = file_load(filepath); | ||
|
||
if (filedata == NULL) { | ||
if (filedata == NULL) | ||
{ | ||
// TODO: make this non-fatal | ||
fprintf(stderr, "cannot find system 404 file\n"); | ||
exit(3); | ||
|
@@ -148,12 +169,12 @@ void handle_http_request(int fd, struct cache *cache) | |
// Read request | ||
int bytes_recvd = recv(fd, request, request_buffer_size - 1, 0); | ||
|
||
if (bytes_recvd < 0) { | ||
if (bytes_recvd < 0) | ||
{ | ||
perror("recv"); | ||
return; | ||
} | ||
|
||
|
||
/////////////////// | ||
// IMPLEMENT ME! // | ||
/////////////////// | ||
|
@@ -165,7 +186,6 @@ void handle_http_request(int fd, struct cache *cache) | |
// Check if it's /d20 and handle that special case | ||
// Otherwise serve the requested file by calling get_file() | ||
|
||
|
||
// (Stretch) If POST, handle the post request | ||
} | ||
|
||
|
@@ -174,7 +194,7 @@ void handle_http_request(int fd, struct cache *cache) | |
*/ | ||
int main(void) | ||
{ | ||
int newfd; // listen on sock_fd, new connection on newfd | ||
int newfd; // listen on sock_fd, new connection on newfd | ||
struct sockaddr_storage their_addr; // connector's address information | ||
char s[INET6_ADDRSTRLEN]; | ||
|
||
|
@@ -183,7 +203,8 @@ int main(void) | |
// Get a listening socket | ||
int listenfd = get_listener_socket(PORT); | ||
|
||
if (listenfd < 0) { | ||
if (listenfd < 0) | ||
{ | ||
fprintf(stderr, "webserver: fatal error getting listening socket\n"); | ||
exit(1); | ||
} | ||
|
@@ -193,24 +214,26 @@ int main(void) | |
// This is the main loop that accepts incoming connections and | ||
// forks a handler process to take care of it. The main parent | ||
// process then goes back to waiting for new connections. | ||
|
||
while(1) { | ||
|
||
while (1) | ||
{ | ||
socklen_t sin_size = sizeof their_addr; | ||
|
||
// Parent process will block on the accept() call until someone | ||
// makes a new connection: | ||
newfd = accept(listenfd, (struct sockaddr *)&their_addr, &sin_size); | ||
if (newfd == -1) { | ||
if (newfd == -1) | ||
{ | ||
perror("accept"); | ||
continue; | ||
} | ||
|
||
// Print out a message that we got the connection | ||
inet_ntop(their_addr.ss_family, | ||
get_in_addr((struct sockaddr *)&their_addr), | ||
s, sizeof s); | ||
get_in_addr((struct sockaddr *)&their_addr), | ||
s, sizeof s); | ||
printf("server: got connection from %s\n", s); | ||
|
||
// newfd is a new socket descriptor for the new connection. | ||
// listenfd is still listening for new connections. | ||
|
||
|
@@ -223,4 +246,3 @@ int main(void) | |
|
||
return 0; | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It's crazy to me how many lines it takes to get a time or timestamp.
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.
yeah it's kind of dumb haha