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

Add easy ability to set status codes and format response body #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
src/*.o
*.o
.cache/
*swp*
33 changes: 33 additions & 0 deletions include/HTTP_Server.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,44 @@
#ifndef HTTP_SERVER_H
#define HTTP_SERVER_H

#include <string.h>
#include <stddef.h>

#define HTTP_STATUS_CODE_LEN 50
#define HTTP_RESPONSE_BODY_LEN 4096

// note: please keep these in the same order as _status_code_text
// as that's the corresponding lookup table
enum http_status_code_e
{
OK = 0,
CREATED,
BAD_REQUEST,
UNAUTHORIZED,
FORBIDDEN,
NOT_FOUND,
INTERNAL_ERROR,
NOT_IMPLEMENTED
};

// defined in HTTP_Server.c
extern const char _status_code_text[8][50];

typedef struct HTTP_Server {
int socket;
int port;
char status_code[HTTP_STATUS_CODE_LEN];
char response[HTTP_RESPONSE_BODY_LEN];
} HTTP_Server;

void init_server(HTTP_Server* http_server, int port);

void http_set_status_code(
HTTP_Server* http_server,
const enum http_status_code_e);

void http_set_response_body(
HTTP_Server* http_server,
const char* body);

#endif
36 changes: 36 additions & 0 deletions src/HTTP_Server.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@
#include <stdio.h>
#include <sys/socket.h>

// pulled these from https://www.w3.org/Protocols/HTTP/HTRESP.html
const char _status_code_text[8][50] = {
"HTTP/1.1 200 OK\r\n\r\n",
"HTTP/1.1 201 CREATED\r\n\r\n",
"HTTP/1.1 400 Bad request\r\n\r\n",
"HTTP/1.1 401 Unauthorized\r\n\r\n",
"HTTP/1.1 403 Forbidden\r\n\r\n",
"HTTP/1.1 404 Not found\r\n\r\n",
"HTTP/1.1 500 Internal Error\r\n\r\n",
"HTTP/1.1 501 Not implemented\r\n\r\n"
};

void init_server(HTTP_Server * http_server, int port) {
http_server->port = port;

Expand All @@ -20,3 +32,27 @@ void init_server(HTTP_Server * http_server, int port) {
http_server->socket = server_socket;
printf("HTTP Server Initialized\nPort: %d\n", http_server->port);
}

void http_set_status_code(
HTTP_Server* http_server,
const enum http_status_code_e status_code)
{
memset(http_server->status_code, 0, HTTP_STATUS_CODE_LEN);
const char* _status = _status_code_text[status_code];
memcpy(http_server->status_code, _status, strlen(_status));
}

void http_set_response_body(
HTTP_Server* http_server,
const char* body)
{
// prepend the status code before adding the body
memset(http_server->response, 0, HTTP_RESPONSE_BODY_LEN);
memcpy(http_server->response, http_server->status_code, strlen(http_server->status_code));

// add response body (while accounting for the space we already took up by the response code
// NOTE: the -5 is for the four extra \r\n\r\n characters and a null terminator
size_t max_len = (size_t)HTTP_RESPONSE_BODY_LEN - strlen(http_server->status_code) - 5;
strncat(http_server->response, body, max_len);
strncat(http_server->response, "\r\n\r\n", 5);
}
1 change: 1 addition & 0 deletions src/Response.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ char * render_static_file(char * fileName) {
temp[i] = ch;
i++;
}
temp[i] = '\0';
fclose(file);
return temp;
}
14 changes: 6 additions & 8 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ int main() {


printf("\n====================================\n");
printf("=========ALL VAILABLE ROUTES========\n");
printf("=========ALL AVAILABLE ROUTES========\n");
// display all available routes
inorder(route);

Expand Down Expand Up @@ -70,26 +70,24 @@ int main() {
if (strstr(urlRoute, "/static/") != NULL) {
//strcat(template, urlRoute+1);
strcat(template, "static/index.css");
http_set_status_code(&http_server, OK);
}else {
struct Route * destination = search(route, urlRoute);
strcat(template, "templates/");

if (destination == NULL) {
strcat(template, "404.html");
http_set_status_code(&http_server, NOT_FOUND);
}else {
strcat(template, destination->value);
http_set_status_code(&http_server, OK);
}
}

char * response_data = render_static_file(template);
http_set_response_body(&http_server, response_data);

char http_header[4096] = "HTTP/1.1 200 OK\r\n\r\n";

strcat(http_header, response_data);
strcat(http_header, "\r\n\r\n");


send(client_socket, http_header, sizeof(http_header), 0);
send(client_socket, http_server.response, sizeof(http_server.response), 0);
close(client_socket);
free(response_data);
}
Expand Down