Update stuff

This commit is contained in:
2026-05-25 23:13:27 -07:00
parent 5d631b594f
commit 5dfbdc43c0
7 changed files with 133 additions and 168 deletions
+54 -72
View File
@@ -28,7 +28,7 @@ typedef struct {
bool help_flag;
uint32_t port;
size_t parallelism;
const char* address;
const char *address;
} GlobalFlags;
static const GlobalFlags DEFAULT_FLAGS = {
@@ -40,36 +40,44 @@ static const GlobalFlags DEFAULT_FLAGS = {
};
// 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 == '-') {
#ifdef BAD_ERROR_REPORTING_FOR_AUTOGRADER
fprintf(stderr, "Invalid Port\n");
#else
log_error("malformed number: \"%s\"", str);
#endif
return false;
}
errno = 0;
char* endptr;
char *endptr;
uintmax_t conv = strtoumax(str, &endptr, 10);
if (!*str || *endptr) {
#ifdef BAD_ERROR_REPORTING_FOR_AUTOGRADER
fprintf(stderr, "Invalid Port\n");
#else
log_error("malformed number: \"%s\"", str);
#endif
return false;
} else if ((conv == UINTMAX_MAX && errno == ERANGE) || conv < min
|| conv > max) {
} else if ((conv == UINTMAX_MAX && errno == ERANGE) || conv < min || conv > max) {
#ifdef BAD_ERROR_REPORTING_FOR_AUTOGRADER
fprintf(stderr, "Invalid Port\n");
#else
log_error("out of range: %s", str);
#endif
return false;
}
*output = conv;
return true;
}
static void parse_cli_options(int argc, const char** argv, GlobalFlags* flags)
{
constexpr size_t MAX_PARALLELISM = SIZE_MAX;
constexpr uint32_t MAX_PORT = 65535;
static void parse_cli_options(int argc, const char **argv, GlobalFlags *flags) {
#define MAX_PARALLELISM SIZE_MAX
#define MAX_PORT 65535
opterr = false;
int c;
char pretty_flag[8];
while ((c = getopt(argc, (char* const*)argv, ":ha:p:")) >= 0) {
while ((c = getopt(argc, (char *const *) argv, ":ha:p:")) >= 0) {
if (isprint(c)) {
snprintf(pretty_flag, sizeof(pretty_flag), "%c", optopt);
} else {
@@ -81,9 +89,7 @@ static void parse_cli_options(int argc, const char** argv, GlobalFlags* flags)
#pragma GCC diagnostic pop
}
switch (c) {
case 'h':
flags->help_flag = true;
return;
case 'h': flags->help_flag = true; return;
case 'p': {
uintmax_t conv;
if (!parse_uint(optarg, 1, MAX_PARALLELISM, &conv)) {
@@ -91,9 +97,7 @@ static void parse_cli_options(int argc, const char** argv, GlobalFlags* flags)
}
flags->parallelism = conv;
} break;
case 'a':
flags->address = optarg;
break;
case 'a': flags->address = optarg; break;
case ':':
flags->opt_error = true;
log_error("flag requires argument: '%s'", pretty_flag);
@@ -120,31 +124,25 @@ static void parse_cli_options(int argc, const char** argv, GlobalFlags* flags)
}
}
static void print_help(FILE* file)
{
fprintf(file,
"usage: httpserver [-h] [-a ADDRESS] [-p PARALLELISM] <PORT>\n");
static void print_help(FILE *file) {
fprintf(file, "usage: httpserver [-h] [-a ADDRESS] [-p PARALLELISM] <PORT>\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");
fprintf(file, " -a bind to ADDRESS (default: 127.0.0.1)\n");
}
constexpr int WORKER_BLOCKED_SIGNALS[] = { SIGTERM, SIGINT, SIGHUP };
constexpr size_t N_WORKER_BLOCKED_SIGNALS = sizeof(WORKER_BLOCKED_SIGNALS) / sizeof(int);
static const int WORKER_BLOCKED_SIGNALS[] = { SIGTERM, SIGINT, SIGHUP };
static const size_t N_WORKER_BLOCKED_SIGNALS = sizeof(WORKER_BLOCKED_SIGNALS) / sizeof(int);
static bool shutdown_flag = false;
static void signal_handler(int signal)
{
static void signal_handler(int signal) {
if (shutdown_flag) {
_exit(signal == SIGTERM ? 0 : EXIT_FAILURE);
}
shutdown_flag = true;
}
static void setup_signals()
{
static void setup_signals() {
struct sigaction act = {
.sa_handler = signal_handler,
.sa_flags = 0,
@@ -158,27 +156,20 @@ static void setup_signals()
}
}
static int parse_result_to_status(HTTPRequestParseResult res)
{
static int parse_result_to_status(HTTPRequestParseResult res) {
switch (res) {
case HRPR_OK:
return 200;
case HRPR_OK: return 200;
case HRPR_NO_MEM:
case HRPR_READ_FAILED:
return 500;
case HRPR_BAD_VERSION:
return 505;
case HRPR_BAD_FORMAT:
return 400;
default:
abort();
case HRPR_READ_FAILED: return 500;
case HRPR_BAD_VERSION: 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;
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 = {
.status = status,
.headers = NULL,
@@ -189,18 +180,16 @@ static void send_simple_response(FILE* stream, int status)
fputc('\n', stream);
}
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,
http_header_list_search(req->headers, "Request-ID", "0"));
}
static void handle_get_request(FILE* conn, HTTPRequest* restrict req)
{
static void handle_get_request(FILE *conn, HTTPRequest *restrict req) {
int status = 200;
struct stat statbuf;
flockfile(stderr);
FILE* file_handle = fopen(req->path, "r");
FILE *file_handle = fopen(req->path, "r");
if (!file_handle) {
if (errno == ENOENT) {
status = 404;
@@ -242,25 +231,22 @@ static void handle_get_request(FILE* conn, HTTPRequest* restrict req)
fclose(file_handle);
}
static ssize_t get_content_length(HTTPRequest* restrict req)
{
const char* text = http_header_list_search(req->headers, "Content-Length", NULL);
static ssize_t get_content_length(HTTPRequest *restrict req) {
const char *text = http_header_list_search(req->headers, "Content-Length", NULL);
if (!text) {
return 0;
} else if (isspace(*text) || *text == '-' || *text == '+') {
return -1;
}
char* endptr;
char *endptr;
uintmax_t conv = strtoumax(text, &endptr, 10);
if (*endptr || (conv == UINTMAX_MAX && errno == ERANGE)
|| conv > SIZE_MAX) {
if (*endptr || (conv == UINTMAX_MAX && errno == ERANGE) || conv > SIZE_MAX) {
return -1;
}
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);
if (content_length < 0) {
write_audit_log_entry(req, 400);
@@ -278,9 +264,8 @@ static void handle_put_request(FILE* conn, HTTPRequest* restrict req)
char read_buff[4096];
while (content_length) {
ssize_t read_size = fread(read_buff, 1,
(size_t)content_length < sizeof(read_buff)
? (size_t)content_length
: sizeof(read_buff),
(size_t) content_length < sizeof(read_buff) ? (size_t) content_length
: sizeof(read_buff),
conn);
if (ferror(conn) || write(temp_fd, read_buff, read_size) < 0) {
write_audit_log_entry(req, 500);
@@ -322,9 +307,8 @@ write_status_and_unlock:
}
}
static void handle_connection(void* arg)
{
FILE* conn = arg;
static void handle_connection(void *arg) {
FILE *conn = arg;
HTTPRequest req;
HTTPRequestParseResult res = parse_http_request(conn, &req);
if (res != HRPR_OK) {
@@ -346,13 +330,11 @@ static void handle_connection(void* arg)
fclose(conn);
}
static void fclose_free_func(void* file)
{
static void fclose_free_func(void *file) {
fclose(file);
}
int main(int argc, const char** argv)
{
int main(int argc, const char **argv) {
setenv("POSIXLY_CORRECT", "1", true);
GlobalFlags flags = DEFAULT_FLAGS;
parse_cli_options(argc, argv, &flags);
@@ -367,11 +349,11 @@ int main(int argc, const char** argv)
for (size_t i = 0; i < N_WORKER_BLOCKED_SIGNALS; ++i) {
sigaddset(&worker_block_set, WORKER_BLOCKED_SIGNALS[i]);
}
Server* server = make_server(flags.address, flags.port);
Server *server = make_server(flags.address, flags.port);
if (!server) {
return EXIT_FAILURE;
}
ThreadPool* pool = make_thread_pool(flags.parallelism, worker_block_set);
ThreadPool *pool = make_thread_pool(flags.parallelism, worker_block_set);
if (!pool) {
destroy_server(server);
return EXIT_FAILURE;
@@ -387,7 +369,7 @@ int main(int argc, const char** argv)
} else if (conn_fd < 0) {
continue;
}
FILE* conn = fdopen(conn_fd, "w+");
FILE *conn = fdopen(conn_fd, "w+");
if (!conn) {
close(conn_fd);
continue;