Skip to content
Open
Show file tree
Hide file tree
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
50 changes: 50 additions & 0 deletions GP.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <stdio.h>

/*
GET /foobar HTTP/1.1
Host: www.example.com
Connection: close
X-Header: whatever
*/

int main(void)
{
char *request =
"GET /foobar HTTP/1.1\n"
"Host: www.example.com\n"
"Connection: close\n"
"X-Header: whatever\n"
"\n"; // request ends in blank line

char method[128]; // "GET"
char path[8192]; // "/foobar"

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

printf("%s\n", method); // "GET"
printf("%s\n", path); // "/foobar"
}


#include <stdio.h>
#include <string.h>

int main(void)
{
char response[500000];

char *body = "<h1>Hello, world!</h1>";
int len = strlen(body);

sprintf(response,
"HTTP/1.1 200 OK\n"
"Content-Type: text/html\n"
"Content-Length: %d\n"
"Connection: close\n"
"\n"
"%s",
len, body);

// call send(response)

}
63 changes: 41 additions & 22 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,24 @@ 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];
int response_length;

// Build HTTP response and store it in response

///////////////////
// IMPLEMENT ME! //
///////////////////
response_length = sprintf(response,
"%s\n"
"Content-Type: %s\n"
"Content-Length: %d\n"
"Connection: close\n"
"\n",
header, content_type, content_length);

// Send it all!
int rv = send(fd, response, response_length, 0);

if (rv < 0) {
perror("send");
}
rv = send(fd, body, content_length, 0);
if (rv < 0) {
perror("send");
}
Expand All @@ -77,15 +85,12 @@ void get_d20(int fd)
{
// Generate a random number between 1 and 20 inclusive

///////////////////
// IMPLEMENT ME! //
///////////////////

int random_num = rand() % 20 + 1;
char numString[8];
sprintf(numString, "%d\n", random_num);
// Use send_response() to send it back as text/plain data

///////////////////
// IMPLEMENT ME! //
///////////////////
send_response(fd, "HTTP/1.1 200 OK", "text/plain", numString, strlen(numString));
}

/**
Expand All @@ -98,7 +103,9 @@ void resp_404(int fd)
char *mime_type;

// Fetch the 404.html file

snprintf(filepath, sizeof filepath, "%s/404.html", SERVER_FILES);
// snprintf(filepath, sizeof filepath, "%s/cat.png", SERVER_ROOT);
filedata = file_load(filepath);

if (filedata == NULL) {
Expand All @@ -119,6 +126,9 @@ void resp_404(int fd)
*/
void get_file(int fd, struct cache *cache, char *request_path)
{
(void)fd;
(void)cache;
(void)request_path;
///////////////////
// IMPLEMENT ME! //
///////////////////
Expand All @@ -132,6 +142,8 @@ void get_file(int fd, struct cache *cache, char *request_path)
*/
char *find_start_of_body(char *header)
{
(void)header;
return NULL;
///////////////////
// IMPLEMENT ME! // (Stretch)
///////////////////
Expand All @@ -144,28 +156,35 @@ void handle_http_request(int fd, struct cache *cache)
{
const int request_buffer_size = 65536; // 64K
char request[request_buffer_size];

char method[128];
char path[8192];
// Read request
int bytes_recvd = recv(fd, request, request_buffer_size - 1, 0);

if (bytes_recvd < 0) {
perror("recv");
return;
}


///////////////////
// IMPLEMENT ME! //
///////////////////


// Read the first two components of the first line of the request

sscanf(request, "%s %s", method, path);
printf("%s\n%s\n", method, path);
// If GET, handle the get endpoints

if (strcmp(method, "GET") == 0) {
// Check if it's /d20 and handle that special case
if (strcmp(path, "/d20") == 0) {
get_d20(fd);
} else {
// Otherwise serve the requested file by calling get_file()


// get_file(fd, cache, path);
resp_404(fd);
}
} else if (strcmp(method, "POST") == 0) {
// (Stretch) If POST, handle the post request
} else {
resp_404(fd);
}
resp_404(fd);
// (Stretch) If POST, handle the post request
}

Expand Down
File renamed without changes