Files

239 lines
7.2 KiB
C
Raw Permalink Normal View History

2026-05-25 17:25:50 -07:00
#include "http.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
2026-05-26 20:33:31 -07:00
static const char *EMPTY_STRING = "";
2026-05-25 17:25:50 -07:00
2026-05-26 20:33:31 -07:00
HTTPHeaderList *http_header_list_push(HTTPHeaderList *list, const char *key, const char *value) {
HTTPHeaderList *new = malloc(sizeof(HTTPHeaderList));
2026-05-25 17:25:50 -07:00
if (!new) {
return NULL;
}
new->key_length = strlen(key);
new->key = malloc(new->key_length + 1);
if (!new->key) {
free(new);
return NULL;
}
memcpy(new->key, key, new->key_length + 1);
new->value_length = strlen(value);
new->value = malloc(new->value_length + 1);
if (!new->value) {
free(new->key);
free(new);
return NULL;
}
memcpy(new->value, value, new->value_length + 1);
new->next = list;
return new;
}
2026-05-26 20:33:31 -07:00
void free_http_header_list(HTTPHeaderList *list) {
2026-05-25 17:25:50 -07:00
while (list) {
2026-05-26 20:33:31 -07:00
HTTPHeaderList *next = list->next;
2026-05-25 17:25:50 -07:00
free(list->key);
free(list->value);
free(list);
list = next;
}
}
2026-05-26 20:33:31 -07:00
const char *http_header_list_search(HTTPHeaderList *list, const char *key, const char *def) {
2026-05-25 17:25:50 -07:00
while (list) {
if (strcmp(list->key, key) == 0) {
return list->value;
}
list = list->next;
}
return def;
}
2026-05-26 20:33:31 -07:00
#define MAX_REQUEST_LENGTH 16384
2026-05-27 06:24:36 -07:00
#define MAX_METHOD_LENGTH 8
#define MAX_URI_LENGTH 64
2026-05-26 20:33:31 -07:00
#define MAX_VERSION_LENGTH 3
2026-05-27 06:24:36 -07:00
#define MAX_HEADER_KEY_LENGTH 128
#define MAX_HEADER_VALUE_LENGTH 128
2026-05-25 17:25:50 -07:00
2026-05-26 20:33:31 -07:00
#define RETURN_IF_READ_ERROR(s) \
if (ferror((s))) { \
return HRPR_READ_FAILED; \
2026-05-25 17:25:50 -07:00
}
// if an error occured, req->uri is not allocated
2026-05-25 23:13:27 -07:00
static HTTPRequestParseResult parse_method_uri_line(
2026-05-26 20:33:31 -07:00
FILE *stream, size_t *restrict bytes_read, HTTPRequest *restrict req) {
2026-05-25 17:25:50 -07:00
// allow for some leeway in passing incorrect methods
char method[MAX_METHOD_LENGTH + 1];
char uri[MAX_URI_LENGTH + 1];
char version_str[MAX_VERSION_LENGTH + 1];
2026-05-26 20:33:31 -07:00
char whitespace[4] = { 0, 0, 0, 0 };
2026-05-25 17:25:50 -07:00
ssize_t signed_bytes_read;
#define S1(s) #s
2026-05-26 20:33:31 -07:00
#define S(s) S1(s)
2026-05-25 23:13:27 -07:00
int nconv = fscanf(stream,
2026-05-25 17:25:50 -07:00
// clang-format off
2026-05-27 06:24:36 -07:00
"%" S(MAX_METHOD_LENGTH) "[a-zA-Z]"
2026-05-25 17:25:50 -07:00
"%c"
"%" S(MAX_URI_LENGTH) "[^ \n\r]"
"%c"
"HTTP/%" S(MAX_VERSION_LENGTH) "[0-9.]"
"%c%c"
"%zn",
// clang-format on
2026-05-25 23:13:27 -07:00
method, &whitespace[0], uri, &whitespace[1], version_str, &whitespace[2], &whitespace[3],
&signed_bytes_read);
2026-05-25 17:25:50 -07:00
#undef S
*bytes_read = signed_bytes_read;
RETURN_IF_READ_ERROR(stream);
2026-05-26 20:33:31 -07:00
if (nconv >= 1 && (nconv == 1 || whitespace[0] == ' ')) {
req->method = strdup(method);
if (!req->method) {
return HRPR_NO_MEM;
}
if (nconv >= 3 && (nconv == 3 || whitespace[1] == ' ')) {
req->uri = strdup(uri);
if (!req->uri) {
return HRPR_NO_MEM;
}
req->path = req->uri;
while (*req->path == '/') {
++req->path;
}
}
}
if (nconv != 7 || *uri != '/' || memcmp(whitespace, " \r\n", 4) != 0) {
2026-05-25 17:25:50 -07:00
return HRPR_BAD_FORMAT;
}
return strcmp(version_str, "1.1") == 0 ? HRPR_OK : HRPR_BAD_VERSION;
}
// return true if there are more headers and no error occurred, false otherwise
// this will *not* free LIST if an error occurs
2026-05-26 20:33:31 -07:00
static bool next_header(FILE *stream, size_t *restrict bytes_read,
HTTPRequestParseResult *restrict res, HTTPHeaderList *restrict *restrict list) {
2026-05-25 17:25:50 -07:00
char c = fgetc(stream);
RETURN_IF_READ_ERROR(stream);
if (c == '\r') {
c = fgetc(stream);
RETURN_IF_READ_ERROR(stream);
if (c != '\n') {
*res = HRPR_BAD_FORMAT;
return false;
}
*bytes_read = 2;
*res = HRPR_OK;
return false;
2026-05-26 20:33:31 -07:00
} else if (feof(stream)) {
*res = HRPR_BAD_FORMAT;
return false;
2026-05-25 17:25:50 -07:00
}
ungetc(c, stream);
char key[MAX_HEADER_KEY_LENGTH + 1];
char value[MAX_HEADER_VALUE_LENGTH + 1];
char whitespace[3];
ssize_t signed_bytes_read;
#define S1(s) #s
2026-05-26 20:33:31 -07:00
#define S(s) S1(s)
2026-05-25 21:59:56 -07:00
int nconv = fscanf(stream,
// clang-format off
2026-05-26 20:33:31 -07:00
"%" S(MAX_HEADER_KEY_LENGTH) "[a-zA-Z0-9.-]"
":%c"
"%" S(MAX_HEADER_VALUE_LENGTH) "[ -~]"
"%c%c"
"%zn",
2026-05-25 21:59:56 -07:00
// clang-format on
2026-05-25 23:13:27 -07:00
key, &whitespace[0], value, &whitespace[1], &whitespace[2], &signed_bytes_read);
2026-05-25 17:25:50 -07:00
#undef S
*bytes_read = signed_bytes_read;
RETURN_IF_READ_ERROR(stream);
if (nconv != 5 || memcmp(whitespace, " \r\n", 3) != 0) {
return HRPR_BAD_FORMAT;
}
*list = http_header_list_push(*list, key, value);
if (!*list) {
*res = HRPR_NO_MEM;
return false;
}
return true;
}
2026-05-26 20:33:31 -07:00
HTTPRequestParseResult parse_http_request(FILE *stream, HTTPRequest *restrict out) {
2026-05-25 17:25:50 -07:00
out->uri = EMPTY_STRING;
out->path = EMPTY_STRING;
out->method = EMPTY_STRING;
2026-05-26 00:54:34 -07:00
out->headers = NULL;
2026-05-25 17:25:50 -07:00
size_t total_bytes;
HTTPRequestParseResult res;
if ((res = parse_method_uri_line(stream, &total_bytes, out)) != HRPR_OK) {
return res;
}
size_t bytes_read;
while (next_header(stream, &bytes_read, &res, &out->headers)) {
if ((total_bytes += bytes_read) > MAX_REQUEST_LENGTH) {
res = HRPR_BAD_FORMAT;
break;
}
}
return res;
}
2026-05-26 20:33:31 -07:00
void free_http_request(HTTPRequest *restrict req) {
2026-05-25 17:25:50 -07:00
if (req->method != EMPTY_STRING) {
2026-05-26 20:33:31 -07:00
free((char *) req->method);
2026-05-25 17:25:50 -07:00
}
if (req->uri != EMPTY_STRING) {
2026-05-26 20:33:31 -07:00
free((char *) req->uri);
2026-05-25 17:25:50 -07:00
}
free_http_header_list(req->headers);
}
2026-05-26 20:33:31 -07:00
const char *status_code_to_message(int status, size_t *restrict length) {
2026-05-25 17:25:50 -07:00
static const struct {
int code;
2026-05-26 20:33:31 -07:00
const char *msg;
2026-05-25 17:25:50 -07:00
size_t size;
} CODES[] = {
2026-05-26 20:33:31 -07:00
// can't get my local copy of clang-format (v18) to work with the autograder
// clang-format off
#define P(s, m) { s, m, sizeof(m) - 1 }
// clang-format on
2026-05-25 17:25:50 -07:00
P(200, "OK"),
P(201, "Created"),
P(400, "Bad Request"),
P(403, "Forbidden"),
P(404, "Not Found"),
P(500, "Internal Server Error"),
P(501, "Not Implemented"),
P(505, "Version Not Supported"),
#undef P
};
static const size_t NCODES = sizeof(CODES) / sizeof(CODES[0]);
for (size_t i = 0; i < NCODES; ++i) {
if (status == CODES[i].code) {
if (length) {
*length = CODES[i].size;
}
return CODES[i].msg;
}
}
return NULL;
}
2026-05-26 20:33:31 -07:00
void format_http_response(FILE *stream, HTTPResponse *restrict resp) {
2026-05-25 17:25:50 -07:00
assert(status_code_to_message(resp->status, NULL));
2026-05-26 00:54:34 -07:00
dprintf(fileno(stream), "HTTP/1.1 %d %s\r\n", resp->status,
status_code_to_message(resp->status, NULL));
dprintf(fileno(stream), "Content-Length: %zu\r\n", resp->body_length);
2026-05-26 20:33:31 -07:00
for (HTTPHeaderList *h = resp->headers; h; h = h->next) {
2026-05-26 00:54:34 -07:00
write(fileno(stream), h->key, h->key_length);
write(fileno(stream), ": ", 2);
write(fileno(stream), h->value, h->value_length);
write(fileno(stream), "\r\n", 2);
2026-05-25 17:25:50 -07:00
}
2026-05-26 00:54:34 -07:00
write(fileno(stream), "\r\n", 2);
2026-05-25 17:25:50 -07:00
}