Skip to content

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
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 40 additions & 18 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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);

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.

Copy link
Author

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


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);

Choose a reason for hiding this comment

The 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! //
///////////////////
Expand All @@ -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);
Expand Down Expand Up @@ -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! //
///////////////////
Expand All @@ -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
}

Expand All @@ -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];

Expand All @@ -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);
}
Expand All @@ -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.

Expand All @@ -223,4 +246,3 @@ int main(void)

return 0;
}