Skip to content
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

Andre Myrick - C Web Server #305

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
66 changes: 62 additions & 4 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,32 @@ 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);

int response_length = strlen(response);

printf("Response: %s\n", response);
// Send it all!
int rv = send(fd, response, response_length, 0);

Expand All @@ -75,6 +94,10 @@ int send_response(int fd, char *header, char *content_type, void *body, int cont
*/
void get_d20(int fd)
{
char body[3];
int random_int = rand() % 21;
sprintf(body, "%d", random_int);
send_response(fd, "HTTP/1.1 200 OK", "text/plain", body, strlen(body));
// Generate a random number between 1 and 20 inclusive

///////////////////
Expand Down Expand Up @@ -119,9 +142,32 @@ 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;

snprintf(filepath, sizeof filepath, "%s/%s", SERVER_ROOT, request_path);
filedata = file_load(filepath);

if (filedata == NULL & strcmp(request_path, "/") == 0) {
fprintf(stderr, "cannot find %s\n", request_path);
snprintf(filepath, sizeof filepath, "%s/index.html", SERVER_ROOT);
filedata = file_load(filepath);
mime_type = mime_type_get(filepath);
send_response(fd, "HTTP/1.1 200 OK", mime_type, filedata->data, filedata->size);
file_free(filedata);
return;
}
else if (filedata == NULL) {
fprintf(stderr, "cannot find %s\n", request_path);
exit(3);
}

mime_type = mime_type_get(filepath);

send_response(fd, "HTTP/1.1 200 OK", mime_type, filedata->data, filedata->size);

file_free(filedata);
}

/**
Expand Down Expand Up @@ -153,7 +199,20 @@ void handle_http_request(int fd, struct cache *cache)
return;
}

char method[200];
char path[8192];

sscanf(request, "%s %s", method, path);

if(strcmp(method, "GET") == 0 & strcmp(path, "/d20") == 0)
{
get_d20(fd);
}
else
{
get_file(fd, cache, path);
}

///////////////////
// IMPLEMENT ME! //
///////////////////
Expand Down Expand Up @@ -193,7 +252,6 @@ 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) {
socklen_t sin_size = sizeof their_addr;

Expand Down