aboutsummaryrefslogtreecommitdiff
path: root/src/http.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/http.c')
-rw-r--r--src/http.c118
1 files changed, 70 insertions, 48 deletions
diff --git a/src/http.c b/src/http.c
index 778bf51..4c93a27 100644
--- a/src/http.c
+++ b/src/http.c
@@ -1,10 +1,10 @@
-#include "../beaker.h"
-#include "beaker_globals.h"
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/socket.h>
-#include <unistd.h>
+#include "../beaker.h"
+#include "beaker_globals.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <unistd.h>
static void build_cookie_headers(char *cookie_headers_buffer,
size_t buffer_size) {
@@ -12,37 +12,47 @@ static void build_cookie_headers(char *cookie_headers_buffer,
cookie_headers_buffer[0] = '\0';
for (int i = 0; i < cookies_to_set_count; i++) {
- char single_cookie_header[MAX_VALUE_LEN * 2];
+ char single_cookie_header[MAX_VALUE_LEN * 2];
snprintf(single_cookie_header, sizeof(single_cookie_header),
"Set-Cookie: %s=%s", cookies_to_set[i].name,
cookies_to_set[i].value);
if (strlen(cookies_to_set[i].expires) > 0) {
- strncat(single_cookie_header, "; Expires=", sizeof(single_cookie_header) - strlen(single_cookie_header) - 1);
- strncat(single_cookie_header, cookies_to_set[i].expires, sizeof(single_cookie_header) - strlen(single_cookie_header) - 1);
+ strncat(single_cookie_header, "; Expires=",
+ sizeof(single_cookie_header) - strlen(single_cookie_header) - 1);
+ strncat(single_cookie_header, cookies_to_set[i].expires,
+ sizeof(single_cookie_header) - strlen(single_cookie_header) - 1);
}
if (strlen(cookies_to_set[i].path) > 0) {
- strncat(single_cookie_header, "; Path=", sizeof(single_cookie_header) - strlen(single_cookie_header) - 1);
- strncat(single_cookie_header, cookies_to_set[i].path, sizeof(single_cookie_header) - strlen(single_cookie_header) - 1);
+ strncat(single_cookie_header, "; Path=",
+ sizeof(single_cookie_header) - strlen(single_cookie_header) - 1);
+ strncat(single_cookie_header, cookies_to_set[i].path,
+ sizeof(single_cookie_header) - strlen(single_cookie_header) - 1);
}
if (cookies_to_set[i].http_only) {
- strncat(single_cookie_header, "; HttpOnly", sizeof(single_cookie_header) - strlen(single_cookie_header) - 1);
+ strncat(single_cookie_header, "; HttpOnly",
+ sizeof(single_cookie_header) - strlen(single_cookie_header) - 1);
}
if (cookies_to_set[i].secure) {
- strncat(single_cookie_header, "; Secure", sizeof(single_cookie_header) - strlen(single_cookie_header) - 1);
+ strncat(single_cookie_header, "; Secure",
+ sizeof(single_cookie_header) - strlen(single_cookie_header) - 1);
}
- strncat(single_cookie_header, "\r\n", sizeof(single_cookie_header) - strlen(single_cookie_header) - 1);
+ strncat(single_cookie_header, "\r\n",
+ sizeof(single_cookie_header) - strlen(single_cookie_header) - 1);
- if (strlen(cookie_headers_buffer) + strlen(single_cookie_header) < buffer_size) {
+ if (strlen(cookie_headers_buffer) + strlen(single_cookie_header) <
+ buffer_size) {
strncat(cookie_headers_buffer, single_cookie_header,
buffer_size - strlen(cookie_headers_buffer) - 1);
} else {
- fprintf(stderr, "[WARNING] build_cookie_headers: Cookie headers buffer full, truncating\n");
+ beaker_log(
+ "WARN",
+ "build_cookie_headers: Cookie headers buffer full, truncating\n");
break;
}
}
@@ -50,11 +60,14 @@ static void build_cookie_headers(char *cookie_headers_buffer,
void send_status(const char *status_line) {
if (current_client_socket == -1) {
- fprintf(stderr, "[ERROR] send_status: No client socket set. Cannot send response.\n");
+ beaker_log("ERROR",
+ "send_status: No client socket set. Cannot send response.\n");
return;
}
char http_response[BUFFER_SIZE];
+ current_response_status = atoi(status_line);
+ current_response_size = 0;
snprintf(http_response, sizeof(http_response),
"HTTP/1.1 %s\r\n"
"Content-Length: 0\r\n"
@@ -62,22 +75,26 @@ void send_status(const char *status_line) {
"\r\n",
status_line);
- if (send(current_client_socket, http_response, strlen(http_response), 0) < 0) {
- perror("Error sending HTTP status");
- fprintf(stderr, "[ERROR] send_status: Failed to send HTTP status.\n");
+ if (send(current_client_socket, http_response, strlen(http_response), 0) <
+ 0) {
+ beaker_log_errno_format("ERROR",
+ "send_status: Failed to send HTTP status.\n");
}
}
void send_response(const char *html) {
if (current_client_socket == -1) {
- fprintf(stderr, "[ERROR] send_response: No client socket set. Cannot send response.\n");
+ beaker_log("ERROR",
+ "send_response: No client socket set. Cannot send response.\n");
return;
}
- char http_response_header[BUFFER_SIZE * 2];
- int content_length = strlen(html);
- char cookie_headers[BUFFER_SIZE];
+ char http_response_header[BUFFER_SIZE * 2];
+ int content_length = strlen(html);
+ current_response_status = 200;
+ current_response_size = content_length;
+ char cookie_headers[BUFFER_SIZE];
build_cookie_headers(cookie_headers, sizeof(cookie_headers));
@@ -85,21 +102,21 @@ void send_response(const char *html) {
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html; charset=UTF-8\r\n"
"Content-Length: %d\r\n"
- "%s"
- "Connection: close\r\n"
- "\r\n",
+ "%s"
+ "Connection: close\r\n"
+ "\r\n",
content_length, cookie_headers);
if (send(current_client_socket, http_response_header,
strlen(http_response_header), 0) < 0) {
- perror("Error sending HTTP header");
- fprintf(stderr, "[ERROR] send_response: Failed to send HTTP header.\n");
+ beaker_log_errno_format("ERROR",
+ "send_response: Failed to send HTTP header.\n");
return;
}
if (send(current_client_socket, html, content_length, 0) < 0) {
- perror("Error sending HTML body");
- fprintf(stderr, "[ERROR] send_response: Failed to send HTML body.\n");
+ beaker_log_errno_format("ERROR",
+ "send_response: Failed to send HTML body.\n");
return;
}
@@ -109,11 +126,14 @@ void send_response(const char *html) {
void send_redirect(const char *location) {
if (current_client_socket == -1) {
- fprintf(stderr, "[ERROR] send_redirect: No client socket set. Cannot send redirect.\n");
+ beaker_log("ERROR",
+ "send_redirect: No client socket set. Cannot send redirect.\n");
return;
}
char http_response_header[BUFFER_SIZE * 2];
+ current_response_status = 302;
+ current_response_size = 0;
char cookie_headers[BUFFER_SIZE];
build_cookie_headers(cookie_headers, sizeof(cookie_headers));
@@ -128,8 +148,8 @@ void send_redirect(const char *location) {
if (send(current_client_socket, http_response_header,
strlen(http_response_header), 0) < 0) {
- perror("Error sending redirect header");
- fprintf(stderr, "[ERROR] send_redirect: Failed to send redirect header.\n");
+ beaker_log_errno_format("ERROR",
+ "send_redirect: Failed to send redirect header.\n");
return;
}
@@ -140,9 +160,10 @@ void set_cookie(const char *name, const char *value, const char *expires,
const char *path, bool http_only, bool secure) {
if (cookies_to_set_count >= MAX_COOKIES) {
- fprintf(stderr,
- "[WARNING] set_cookie: Maximum number of cookies to set reached. Cannot set cookie '%s'.\n",
- name);
+ beaker_log("WARN",
+ "set_cookie: Maximum number of cookies to set reached. Cannot "
+ "set cookie '%s'.\n",
+ name);
return;
}
@@ -178,7 +199,7 @@ char *get_cookie(const char *cookie_name) {
char *cookie_header_start = strstr(current_request_buffer, "\r\nCookie: ");
if (cookie_header_start == NULL) {
- return NULL;
+ return NULL;
}
cookie_header_start += strlen("\r\nCookie: ");
@@ -186,22 +207,23 @@ char *get_cookie(const char *cookie_name) {
char *cookie_header_end = strstr(cookie_header_start, "\r\n");
if (cookie_header_end == NULL) {
- cookie_header_end = (char *)(current_request_buffer + strlen(current_request_buffer));
+ cookie_header_end =
+ (char *)(current_request_buffer + strlen(current_request_buffer));
}
size_t cookie_str_len = cookie_header_end - cookie_header_start;
char *cookie_str = (char *)malloc(cookie_str_len + 1);
if (cookie_str == NULL) {
- perror("Failed to allocate memory for raw cookie string");
- fprintf(stderr, "[ERROR] get_cookie: Allocation failed for cookie_str.\n");
+ beaker_log_errno_format("ERROR",
+ "get_cookie: Allocation failed for cookie_str.\n");
return NULL;
}
strncpy(cookie_str, cookie_header_start, cookie_str_len);
- cookie_str[cookie_str_len] = '\0';
+ cookie_str[cookie_str_len] = '\0';
- char *token;
- char *saveptr_cookie;
+ char *token;
+ char *saveptr_cookie;
token = strtok_r(cookie_str, ";", &saveptr_cookie);
while (token != NULL) {
@@ -212,15 +234,15 @@ char *get_cookie(const char *cookie_name) {
char *equals_sign = strchr(token, '=');
if (equals_sign != NULL) {
- size_t name_len = equals_sign - token;
+ size_t name_len = equals_sign - token;
if (name_len == strlen(cookie_name) &&
strncmp(token, cookie_name, name_len) == 0) {
char *cookie_value = strdup(equals_sign + 1);
if (cookie_value == NULL) {
- perror("Failed to duplicate cookie value");
- fprintf(stderr, "[ERROR] get_cookie: Allocation failed for cookie_value.\n");
+ beaker_log_errno_format(
+ "ERROR", "get_cookie: Allocation failed for cookie_value.\n");
}
free(cookie_str);
return cookie_value;