Update stuff
This commit is contained in:
@@ -7,7 +7,8 @@
|
|||||||
|
|
||||||
static const char* EMPTY_STRING = "";
|
static const char* EMPTY_STRING = "";
|
||||||
|
|
||||||
HTTPHeaderList *http_header_list_push(HTTPHeaderList *list, const char *key, const char *value) {
|
HTTPHeaderList* http_header_list_push(HTTPHeaderList* list, const char* key, const char* value)
|
||||||
|
{
|
||||||
HTTPHeaderList* new = malloc(sizeof(HTTPHeaderList));
|
HTTPHeaderList* new = malloc(sizeof(HTTPHeaderList));
|
||||||
if (!new) {
|
if (!new) {
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -31,7 +32,8 @@ HTTPHeaderList *http_header_list_push(HTTPHeaderList *list, const char *key, con
|
|||||||
return new;
|
return new;
|
||||||
}
|
}
|
||||||
|
|
||||||
void free_http_header_list(HTTPHeaderList *list) {
|
void free_http_header_list(HTTPHeaderList* list)
|
||||||
|
{
|
||||||
while (list) {
|
while (list) {
|
||||||
HTTPHeaderList* next = list->next;
|
HTTPHeaderList* next = list->next;
|
||||||
free(list->key);
|
free(list->key);
|
||||||
@@ -41,7 +43,8 @@ void free_http_header_list(HTTPHeaderList *list) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *http_header_list_search(HTTPHeaderList *list, const char *key, const char *def) {
|
const char* http_header_list_search(HTTPHeaderList* list, const char* key, const char* def)
|
||||||
|
{
|
||||||
while (list) {
|
while (list) {
|
||||||
if (strcmp(list->key, key) == 0) {
|
if (strcmp(list->key, key) == 0) {
|
||||||
return list->value;
|
return list->value;
|
||||||
@@ -65,7 +68,8 @@ const char *http_header_list_search(HTTPHeaderList *list, const char *key, const
|
|||||||
|
|
||||||
// if an error occured, req->uri is not allocated
|
// if an error occured, req->uri is not allocated
|
||||||
static HTTPRequestParseResult parse_method_uri_line(
|
static HTTPRequestParseResult parse_method_uri_line(
|
||||||
FILE *stream, size_t *restrict bytes_read, HTTPRequest *restrict req) {
|
FILE* stream, size_t* restrict bytes_read, HTTPRequest* restrict req)
|
||||||
|
{
|
||||||
// allow for some leeway in passing incorrect methods
|
// allow for some leeway in passing incorrect methods
|
||||||
char method[MAX_METHOD_LENGTH + 1];
|
char method[MAX_METHOD_LENGTH + 1];
|
||||||
char uri[MAX_URI_LENGTH + 1];
|
char uri[MAX_URI_LENGTH + 1];
|
||||||
@@ -110,7 +114,8 @@ static HTTPRequestParseResult parse_method_uri_line(
|
|||||||
// return true if there are more headers and no error occurred, false otherwise
|
// return true if there are more headers and no error occurred, false otherwise
|
||||||
// this will *not* free LIST if an error occurs
|
// this will *not* free LIST if an error occurs
|
||||||
static bool next_header(FILE* stream, size_t* restrict bytes_read,
|
static bool next_header(FILE* stream, size_t* restrict bytes_read,
|
||||||
HTTPRequestParseResult *restrict res, HTTPHeaderList *restrict *restrict list) {
|
HTTPRequestParseResult* restrict res, HTTPHeaderList* restrict* restrict list)
|
||||||
|
{
|
||||||
char c = fgetc(stream);
|
char c = fgetc(stream);
|
||||||
RETURN_IF_READ_ERROR(stream);
|
RETURN_IF_READ_ERROR(stream);
|
||||||
if (c == '\r') {
|
if (c == '\r') {
|
||||||
@@ -154,16 +159,17 @@ static bool next_header(FILE *stream, size_t *restrict bytes_read,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
HTTPRequestParseResult parse_http_request(FILE *stream, HTTPRequest *restrict out) {
|
HTTPRequestParseResult parse_http_request(FILE* stream, HTTPRequest* restrict out)
|
||||||
|
{
|
||||||
out->uri = EMPTY_STRING;
|
out->uri = EMPTY_STRING;
|
||||||
out->path = EMPTY_STRING;
|
out->path = EMPTY_STRING;
|
||||||
out->method = EMPTY_STRING;
|
out->method = EMPTY_STRING;
|
||||||
|
out->headers = NULL;
|
||||||
size_t total_bytes;
|
size_t total_bytes;
|
||||||
HTTPRequestParseResult res;
|
HTTPRequestParseResult res;
|
||||||
if ((res = parse_method_uri_line(stream, &total_bytes, out)) != HRPR_OK) {
|
if ((res = parse_method_uri_line(stream, &total_bytes, out)) != HRPR_OK) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
out->headers = NULL;
|
|
||||||
size_t bytes_read;
|
size_t bytes_read;
|
||||||
while (next_header(stream, &bytes_read, &res, &out->headers)) {
|
while (next_header(stream, &bytes_read, &res, &out->headers)) {
|
||||||
if ((total_bytes += bytes_read) > MAX_REQUEST_LENGTH) {
|
if ((total_bytes += bytes_read) > MAX_REQUEST_LENGTH) {
|
||||||
@@ -174,7 +180,8 @@ HTTPRequestParseResult parse_http_request(FILE *stream, HTTPRequest *restrict ou
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
void free_http_request(HTTPRequest *restrict req) {
|
void free_http_request(HTTPRequest* restrict req)
|
||||||
|
{
|
||||||
if (req->method != EMPTY_STRING) {
|
if (req->method != EMPTY_STRING) {
|
||||||
free((char*)req->method);
|
free((char*)req->method);
|
||||||
}
|
}
|
||||||
@@ -184,7 +191,8 @@ void free_http_request(HTTPRequest *restrict req) {
|
|||||||
free_http_header_list(req->headers);
|
free_http_header_list(req->headers);
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *status_code_to_message(int status, size_t *restrict length) {
|
const char* status_code_to_message(int status, size_t* restrict length)
|
||||||
|
{
|
||||||
static const struct {
|
static const struct {
|
||||||
int code;
|
int code;
|
||||||
const char* msg;
|
const char* msg;
|
||||||
@@ -213,15 +221,17 @@ const char *status_code_to_message(int status, size_t *restrict length) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void format_http_response(FILE *stream, HTTPResponse *restrict resp) {
|
void format_http_response(FILE* stream, HTTPResponse* restrict resp)
|
||||||
|
{
|
||||||
assert(status_code_to_message(resp->status, NULL));
|
assert(status_code_to_message(resp->status, NULL));
|
||||||
fprintf(stream, "HTTP/1.1 %d %s\r\n", resp->status, status_code_to_message(resp->status, NULL));
|
dprintf(fileno(stream), "HTTP/1.1 %d %s\r\n", resp->status,
|
||||||
fprintf(stream, "Content-Length: %zu\r\n", resp->body_length);
|
status_code_to_message(resp->status, NULL));
|
||||||
|
dprintf(fileno(stream), "Content-Length: %zu\r\n", resp->body_length);
|
||||||
for (HTTPHeaderList* h = resp->headers; h; h = h->next) {
|
for (HTTPHeaderList* h = resp->headers; h; h = h->next) {
|
||||||
fwrite(h->key, 1, h->key_length, stream);
|
write(fileno(stream), h->key, h->key_length);
|
||||||
fwrite(": ", 1, 2, stream);
|
write(fileno(stream), ": ", 2);
|
||||||
fwrite(h->value, 1, h->value_length, stream);
|
write(fileno(stream), h->value, h->value_length);
|
||||||
fwrite("\r\n", 1, 2, stream);
|
write(fileno(stream), "\r\n", 2);
|
||||||
}
|
}
|
||||||
fwrite("\r\n", 1, 2, stream);
|
write(fileno(stream), "\r\n", 2);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,10 +16,12 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
|
#include <pthread.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
@@ -40,7 +42,8 @@ static const GlobalFlags DEFAULT_FLAGS = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Return false on failure, true on success
|
// Return false on failure, true on success
|
||||||
static bool parse_uint(const char *str, uintmax_t min, uintmax_t max, uintmax_t *output) {
|
static bool parse_uint(const char* str, uintmax_t min, uintmax_t max, uintmax_t* output)
|
||||||
|
{
|
||||||
if (isspace(*str) || *str == '+' || *str == '-') {
|
if (isspace(*str) || *str == '+' || *str == '-') {
|
||||||
#ifdef BAD_ERROR_REPORTING_FOR_AUTOGRADER
|
#ifdef BAD_ERROR_REPORTING_FOR_AUTOGRADER
|
||||||
fprintf(stderr, "Invalid Port\n");
|
fprintf(stderr, "Invalid Port\n");
|
||||||
@@ -71,7 +74,8 @@ static bool parse_uint(const char *str, uintmax_t min, uintmax_t max, uintmax_t
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void parse_cli_options(int argc, const char **argv, GlobalFlags *flags) {
|
static void parse_cli_options(int argc, const char** argv, GlobalFlags* flags)
|
||||||
|
{
|
||||||
#define MAX_PARALLELISM SIZE_MAX
|
#define MAX_PARALLELISM SIZE_MAX
|
||||||
#define MAX_PORT 65535
|
#define MAX_PORT 65535
|
||||||
opterr = false;
|
opterr = false;
|
||||||
@@ -89,7 +93,9 @@ static void parse_cli_options(int argc, const char **argv, GlobalFlags *flags) {
|
|||||||
#pragma GCC diagnostic pop
|
#pragma GCC diagnostic pop
|
||||||
}
|
}
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 'h': flags->help_flag = true; return;
|
case 'h':
|
||||||
|
flags->help_flag = true;
|
||||||
|
return;
|
||||||
case 'p': {
|
case 'p': {
|
||||||
uintmax_t conv;
|
uintmax_t conv;
|
||||||
if (!parse_uint(optarg, 1, MAX_PARALLELISM, &conv)) {
|
if (!parse_uint(optarg, 1, MAX_PARALLELISM, &conv)) {
|
||||||
@@ -97,7 +103,9 @@ static void parse_cli_options(int argc, const char **argv, GlobalFlags *flags) {
|
|||||||
}
|
}
|
||||||
flags->parallelism = conv;
|
flags->parallelism = conv;
|
||||||
} break;
|
} break;
|
||||||
case 'a': flags->address = optarg; break;
|
case 'a':
|
||||||
|
flags->address = optarg;
|
||||||
|
break;
|
||||||
case ':':
|
case ':':
|
||||||
flags->opt_error = true;
|
flags->opt_error = true;
|
||||||
log_error("flag requires argument: '%s'", pretty_flag);
|
log_error("flag requires argument: '%s'", pretty_flag);
|
||||||
@@ -124,7 +132,8 @@ static void parse_cli_options(int argc, const char **argv, GlobalFlags *flags) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void print_help(FILE *file) {
|
static void print_help(FILE* file)
|
||||||
|
{
|
||||||
fprintf(file, "usage: httpserver [-h] [-a ADDRESS] [-p PARALLELISM] <PORT>\n");
|
fprintf(file, "usage: httpserver [-h] [-a ADDRESS] [-p PARALLELISM] <PORT>\n");
|
||||||
fprintf(file, " -h print this message, then exit\n");
|
fprintf(file, " -h print this message, then exit\n");
|
||||||
fprintf(file, " -p use PARALLELISM threads for processing requests (default: 1)\n");
|
fprintf(file, " -p use PARALLELISM threads for processing requests (default: 1)\n");
|
||||||
@@ -135,14 +144,25 @@ static const int WORKER_BLOCKED_SIGNALS[] = { SIGTERM, SIGINT, SIGHUP };
|
|||||||
static const size_t N_WORKER_BLOCKED_SIGNALS = sizeof(WORKER_BLOCKED_SIGNALS) / sizeof(int);
|
static const size_t N_WORKER_BLOCKED_SIGNALS = sizeof(WORKER_BLOCKED_SIGNALS) / sizeof(int);
|
||||||
static bool shutdown_flag = false;
|
static bool shutdown_flag = false;
|
||||||
|
|
||||||
static void signal_handler(int signal) {
|
static void signal_handler(int signal)
|
||||||
|
{
|
||||||
if (shutdown_flag) {
|
if (shutdown_flag) {
|
||||||
_exit(signal == SIGTERM ? 0 : EXIT_FAILURE);
|
_exit(signal == SIGTERM ? 0 : EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
shutdown_flag = true;
|
shutdown_flag = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void setup_signals() {
|
static void setup_signals()
|
||||||
|
{
|
||||||
|
struct sigaction pipe_act = {
|
||||||
|
.sa_flags = 0,
|
||||||
|
.sa_handler = SIG_IGN,
|
||||||
|
};
|
||||||
|
sigemptyset(&pipe_act.sa_mask);
|
||||||
|
if (sigaction(SIGPIPE, &pipe_act, NULL) < 0) {
|
||||||
|
log_errno("sigaction");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
struct sigaction act = {
|
struct sigaction act = {
|
||||||
.sa_handler = signal_handler,
|
.sa_handler = signal_handler,
|
||||||
.sa_flags = 0,
|
.sa_flags = 0,
|
||||||
@@ -156,18 +176,25 @@ static void setup_signals() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int parse_result_to_status(HTTPRequestParseResult res) {
|
static int parse_result_to_status(HTTPRequestParseResult res)
|
||||||
|
{
|
||||||
switch (res) {
|
switch (res) {
|
||||||
case HRPR_OK: return 200;
|
case HRPR_OK:
|
||||||
|
return 200;
|
||||||
case HRPR_NO_MEM:
|
case HRPR_NO_MEM:
|
||||||
case HRPR_READ_FAILED: return 500;
|
case HRPR_READ_FAILED:
|
||||||
case HRPR_BAD_VERSION: return 505;
|
return 500;
|
||||||
case HRPR_BAD_FORMAT: return 400;
|
case HRPR_BAD_VERSION:
|
||||||
default: abort();
|
return 505;
|
||||||
|
case HRPR_BAD_FORMAT:
|
||||||
|
return 400;
|
||||||
|
default:
|
||||||
|
abort();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void send_simple_response(FILE *stream, int status) {
|
static void send_simple_response(FILE* stream, int status)
|
||||||
|
{
|
||||||
size_t status_msg_len;
|
size_t status_msg_len;
|
||||||
const char* status_msg = status_code_to_message(status, &status_msg_len);
|
const char* status_msg = status_code_to_message(status, &status_msg_len);
|
||||||
HTTPResponse resp = {
|
HTTPResponse resp = {
|
||||||
@@ -176,19 +203,23 @@ static void send_simple_response(FILE *stream, int status) {
|
|||||||
.body_length = status_msg_len + 1,
|
.body_length = status_msg_len + 1,
|
||||||
};
|
};
|
||||||
format_http_response(stream, &resp);
|
format_http_response(stream, &resp);
|
||||||
fwrite(status_msg, 1, status_msg_len, stream);
|
write(fileno(stream), status_msg, status_msg_len);
|
||||||
fputc('\n', stream);
|
write(fileno(stream), "\n", 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void write_audit_log_entry(HTTPRequest *restrict req, int status) {
|
static void write_audit_log_entry(HTTPRequest* restrict req, int status)
|
||||||
|
{
|
||||||
fprintf(stderr, "%s,%s,%d,%s\n", req->method, req->uri, status,
|
fprintf(stderr, "%s,%s,%d,%s\n", req->method, req->uri, status,
|
||||||
http_header_list_search(req->headers, "Request-ID", "0"));
|
http_header_list_search(req->headers, "Request-ID", "0"));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void handle_get_request(FILE *conn, HTTPRequest *restrict req) {
|
static pthread_mutex_t file_access_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||||
|
|
||||||
|
static void handle_get_request(FILE* conn, HTTPRequest* restrict req)
|
||||||
|
{
|
||||||
int status = 200;
|
int status = 200;
|
||||||
struct stat statbuf;
|
struct stat statbuf;
|
||||||
flockfile(stderr);
|
pthread_mutex_lock(&file_access_mutex);
|
||||||
FILE* file_handle = fopen(req->path, "r");
|
FILE* file_handle = fopen(req->path, "r");
|
||||||
if (!file_handle) {
|
if (!file_handle) {
|
||||||
if (errno == ENOENT) {
|
if (errno == ENOENT) {
|
||||||
@@ -203,7 +234,7 @@ static void handle_get_request(FILE *conn, HTTPRequest *restrict req) {
|
|||||||
status = errno == EACCES ? 403 : 500;
|
status = errno == EACCES ? 403 : 500;
|
||||||
}
|
}
|
||||||
write_audit_log_entry(req, status);
|
write_audit_log_entry(req, status);
|
||||||
funlockfile(stderr);
|
pthread_mutex_unlock(&file_access_mutex);
|
||||||
if (status != 200) {
|
if (status != 200) {
|
||||||
if (file_handle) {
|
if (file_handle) {
|
||||||
fclose(file_handle);
|
fclose(file_handle);
|
||||||
@@ -225,13 +256,14 @@ static void handle_get_request(FILE *conn, HTTPRequest *restrict req) {
|
|||||||
}
|
}
|
||||||
size_t count = fread(read_buf, 1, sizeof(read_buf), file_handle);
|
size_t count = fread(read_buf, 1, sizeof(read_buf), file_handle);
|
||||||
if (count) {
|
if (count) {
|
||||||
fwrite(read_buf, 1, count, conn);
|
write(fileno(conn), read_buf, count);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fclose(file_handle);
|
fclose(file_handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
static ssize_t get_content_length(HTTPRequest *restrict req) {
|
static ssize_t get_content_length(HTTPRequest* restrict req)
|
||||||
|
{
|
||||||
const char* text = http_header_list_search(req->headers, "Content-Length", NULL);
|
const char* text = http_header_list_search(req->headers, "Content-Length", NULL);
|
||||||
if (!text) {
|
if (!text) {
|
||||||
return 0;
|
return 0;
|
||||||
@@ -246,7 +278,8 @@ static ssize_t get_content_length(HTTPRequest *restrict req) {
|
|||||||
return conv;
|
return conv;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void handle_put_request(FILE *conn, HTTPRequest *restrict req) {
|
static void handle_put_request(FILE* conn, HTTPRequest* restrict req)
|
||||||
|
{
|
||||||
ssize_t content_length = get_content_length(req);
|
ssize_t content_length = get_content_length(req);
|
||||||
if (content_length < 0) {
|
if (content_length < 0) {
|
||||||
write_audit_log_entry(req, 400);
|
write_audit_log_entry(req, 400);
|
||||||
@@ -279,7 +312,7 @@ static void handle_put_request(FILE *conn, HTTPRequest *restrict req) {
|
|||||||
close(temp_fd);
|
close(temp_fd);
|
||||||
int status;
|
int status;
|
||||||
bool need_unlink = true;
|
bool need_unlink = true;
|
||||||
flockfile(stderr);
|
pthread_mutex_lock(&file_access_mutex);
|
||||||
int creat_fd = open(req->path, O_RDONLY | O_CREAT | O_EXCL, 0644);
|
int creat_fd = open(req->path, O_RDONLY | O_CREAT | O_EXCL, 0644);
|
||||||
if (creat_fd < 0 && errno == EEXIST) {
|
if (creat_fd < 0 && errno == EEXIST) {
|
||||||
// file existed
|
// file existed
|
||||||
@@ -300,14 +333,15 @@ static void handle_put_request(FILE *conn, HTTPRequest *restrict req) {
|
|||||||
need_unlink = false;
|
need_unlink = false;
|
||||||
write_status_and_unlock:
|
write_status_and_unlock:
|
||||||
write_audit_log_entry(req, status);
|
write_audit_log_entry(req, status);
|
||||||
funlockfile(stderr);
|
pthread_mutex_unlock(&file_access_mutex);
|
||||||
send_simple_response(conn, status);
|
send_simple_response(conn, status);
|
||||||
if (need_unlink) {
|
if (need_unlink) {
|
||||||
unlink(temp_file);
|
unlink(temp_file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void handle_connection(void *arg) {
|
static void handle_connection(void* arg)
|
||||||
|
{
|
||||||
FILE* conn = arg;
|
FILE* conn = arg;
|
||||||
HTTPRequest req;
|
HTTPRequest req;
|
||||||
HTTPRequestParseResult res = parse_http_request(conn, &req);
|
HTTPRequestParseResult res = parse_http_request(conn, &req);
|
||||||
@@ -327,14 +361,17 @@ static void handle_connection(void *arg) {
|
|||||||
send_simple_response(conn, 501);
|
send_simple_response(conn, 501);
|
||||||
}
|
}
|
||||||
free_http_request(&req);
|
free_http_request(&req);
|
||||||
|
shutdown(fileno(conn), SHUT_RDWR);
|
||||||
fclose(conn);
|
fclose(conn);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void fclose_free_func(void *file) {
|
static void fclose_free_func(void* file)
|
||||||
|
{
|
||||||
fclose(file);
|
fclose(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, const char **argv) {
|
int main(int argc, const char** argv)
|
||||||
|
{
|
||||||
setenv("POSIXLY_CORRECT", "1", true);
|
setenv("POSIXLY_CORRECT", "1", true);
|
||||||
GlobalFlags flags = DEFAULT_FLAGS;
|
GlobalFlags flags = DEFAULT_FLAGS;
|
||||||
parse_cli_options(argc, argv, &flags);
|
parse_cli_options(argc, argv, &flags);
|
||||||
@@ -369,7 +406,7 @@ int main(int argc, const char **argv) {
|
|||||||
} else if (conn_fd < 0) {
|
} else if (conn_fd < 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
FILE *conn = fdopen(conn_fd, "w+");
|
FILE* conn = fdopen(conn_fd, "r+");
|
||||||
if (!conn) {
|
if (!conn) {
|
||||||
close(conn_fd);
|
close(conn_fd);
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
Reference in New Issue
Block a user