ecen5273

ECEN5273 Coursework
git clone https://git.pngu.org/ecen5273
Log | Files | Refs | README

server.h (2918B)


// HTTP Web Server
// Jack Sangdahl
// 09 October 2024
#include <arpa/inet.h>
#include <pthread.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>

#define DATA_SIZE 8192
#define CONTENT_TYPE_SIZE 32
#define KEEP_ALIVE_TIMEOUT 10

#define RESP_400                                                               \
  "HTTP/1.1 400 Bad Request\r\n"                                               \
  "Content-Type: text/html\r\n"                                                \
  "Content-Length: 159\r\n\r\n"                                                \
  "<html><body><h1>400 Bad Request</h1><p>The request could not be "           \
  "parsed or is malformed</p></body></html>"

#define RESP_403                                                               \
  "HTTP/1.1 403 Forbidden\r\n"                                                 \
  "Content-Type: text/html\r\n"                                                \
  "Content-Length: 207\r\n\r\n"                                                \
  "<html><body><h1>403 Forbidden</h1><p>The requested file can not "           \
  "be accessed due to afile permission issue</p></body></html>"

#define RESP_404                                                               \
  "HTTP/1.1 404 Not Found\r\n"                                                 \
  "Content-Type: text/html\r\n"                                                \
  "Content-Length: 195\r\n\r\n"                                                \
  "<html><body><h1>404 Not Found</h1><p>The requested file can not "           \
  "be found in the document tree</p></body></html>"

#define RESP_405                                                               \
  "HTTP/1.1 405 Method Not Allowed\r\n"                                        \
  "Content-Type: text/html\r\n"                                                \
  "Content-Length: 194\r\n\r\n"                                                \
  "<html><body><h1>405 Method Not Allowed</h1><p>A method other "              \
  "than GET was requested</p></body></html>"

#define RESP_505                                                               \
  "HTTP/1.1 505 HTTP Version Not Supported\r\n"                                \
  "Content-Type: text/html\r\n"                                                \
  "Content-Length: 224\r\n\r\n"                                                \
  "<html><body><h1>505 HTTP Version Not Supported</h1><p>An HTTP "             \
  "version other than 1.0 or 1.1 was requested</p></body></html>"

struct file_ext_map {
  char *ext;
  char *type;
};

void *handle_connection(void *a);
int handle_request(int connection, char *filename, char *version,
                   bool keep_alive);
int handle_error(int connection, int error_code);
void handle_sigint(int sig);