aboutsummaryrefslogtreecommitdiffstats
path: root/src/blogc-runserver
diff options
context:
space:
mode:
Diffstat (limited to 'src/blogc-runserver')
-rw-r--r--src/blogc-runserver/httpd-utils.c21
-rw-r--r--src/blogc-runserver/httpd.c32
-rw-r--r--src/blogc-runserver/main.c17
-rw-r--r--src/blogc-runserver/mime.c6
4 files changed, 40 insertions, 36 deletions
diff --git a/src/blogc-runserver/httpd-utils.c b/src/blogc-runserver/httpd-utils.c
index 9d374ae..a920556 100644
--- a/src/blogc-runserver/httpd-utils.c
+++ b/src/blogc-runserver/httpd-utils.c
@@ -9,14 +9,15 @@
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
-#include "../common/utils.h"
+#include <squareball.h>
+
#include "httpd-utils.h"
char*
br_readline(int socket)
{
- bc_string_t *rv = bc_string_new();
+ sb_string_t *rv = sb_string_new();
char buffer[READLINE_BUFFER_SIZE];
ssize_t len;
bool end = false;
@@ -30,7 +31,7 @@ br_readline(int socket)
end = true;
break;
}
- bc_string_append_c(rv, buffer[i]);
+ sb_string_append_c(rv, buffer[i]);
}
}
if (len < READLINE_BUFFER_SIZE) {
@@ -38,7 +39,7 @@ br_readline(int socket)
}
}
- return bc_string_free(rv, false);
+ return sb_string_free(rv, false);
}
@@ -58,7 +59,7 @@ br_hextoi(const char c)
char*
br_urldecode(const char *str)
{
- bc_string_t *rv = bc_string_new();
+ sb_string_t *rv = sb_string_new();
for (size_t i = 0; i < strlen(str); i++) {
switch (str[i]) {
@@ -67,22 +68,22 @@ br_urldecode(const char *str)
int p1 = br_hextoi(str[i + 1]) * 16;
int p2 = br_hextoi(str[i + 2]);
if (p1 >= 0 && p2 >= 0) {
- bc_string_append_c(rv, p1 + p2);
+ sb_string_append_c(rv, p1 + p2);
i += 2;
continue;
}
}
- bc_string_append_c(rv, '%');
+ sb_string_append_c(rv, '%');
break;
case '+':
- bc_string_append_c(rv, ' ');
+ sb_string_append_c(rv, ' ');
break;
default:
- bc_string_append_c(rv, str[i]);
+ sb_string_append_c(rv, str[i]);
}
}
- return bc_string_free(rv, false);
+ return sb_string_free(rv, false);
}
diff --git a/src/blogc-runserver/httpd.c b/src/blogc-runserver/httpd.c
index 513c4b0..a78b888 100644
--- a/src/blogc-runserver/httpd.c
+++ b/src/blogc-runserver/httpd.c
@@ -19,11 +19,11 @@
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
-#include "../common/error.h"
-#include "../common/file.h"
-#include "../common/utils.h"
+#include <squareball.h>
+
#include "mime.h"
#include "httpd-utils.h"
+#include "httpd.h"
#define LISTEN_BACKLOG 100
@@ -43,7 +43,7 @@ typedef struct {
static void
error(int socket, int status_code, const char *error)
{
- char *str = bc_strdup_printf(
+ char *str = sb_strdup_printf(
"HTTP/1.0 %d %s\r\n"
"Content-Type: text/html\r\n"
"Content-Length: %zu\r\n"
@@ -74,8 +74,8 @@ handle_request(void *arg)
unsigned short status_code = 200;
- char **pieces = bc_str_split(conn_line, ' ', 3);
- if (bc_strv_length(pieces) != 3) {
+ char **pieces = sb_str_split(conn_line, ' ', 3);
+ if (sb_strv_length(pieces) != 3) {
status_code = 400;
error(client_socket, 400, "Bad Request");
goto point1;
@@ -87,9 +87,9 @@ handle_request(void *arg)
goto point1;
}
- char **pieces2 = bc_str_split(pieces[1], '?', 2);
+ char **pieces2 = sb_str_split(pieces[1], '?', 2);
char *path = br_urldecode(pieces2[0]);
- bc_strv_free(pieces2);
+ sb_strv_free(pieces2);
if (path == NULL) {
status_code = 400;
@@ -97,7 +97,7 @@ handle_request(void *arg)
goto point2;
}
- char *abs_path = bc_strdup_printf("%s/%s", docroot, path);
+ char *abs_path = sb_strdup_printf("%s/%s", docroot, path);
char *real_path = realpath(abs_path, NULL);
free(abs_path);
@@ -161,7 +161,7 @@ handle_request(void *arg)
if (add_slash) {
// production webservers usually returns 301 in such cases, but 302 is
// better for development/testing.
- char *tmp = bc_strdup_printf(
+ char *tmp = sb_strdup_printf(
"HTTP/1.0 302 Found\r\n"
"Location: %s/\r\n"
"Content-Length: 0\r\n"
@@ -177,16 +177,16 @@ handle_request(void *arg)
}
size_t len;
- bc_error_t *err = NULL;
- char* contents = bc_file_get_contents(real_path, false, &len, &err);
+ sb_error_t *err = NULL;
+ char* contents = sb_file_get_contents(real_path, &len, &err);
if (err != NULL) {
status_code = 500;
error(client_socket, 500, "Internal Server Error");
- bc_error_free(err);
+ sb_error_free(err);
goto point4;
}
- char *out = bc_strdup_printf(
+ char *out = sb_strdup_printf(
"HTTP/1.0 200 OK\r\n"
"Content-Type: %s\r\n"
"Content-Length: %zu\r\n"
@@ -213,7 +213,7 @@ point1:
fprintf(stderr, "[Thread-%zu] %s - - \"%s\" %d\n", thread_id + 1,
ip, conn_line, status_code);
free(conn_line);
- bc_strv_free(pieces);
+ sb_strv_free(pieces);
point0:
free(ip);
close(client_socket);
@@ -233,7 +233,7 @@ br_httpd_get_ip(int af, const struct sockaddr *addr)
struct sockaddr_in *a = (struct sockaddr_in*) addr;
inet_ntop(af, &(a->sin_addr), host, INET6_ADDRSTRLEN);
}
- return bc_strdup(host);
+ return sb_strdup(host);
}
diff --git a/src/blogc-runserver/main.c b/src/blogc-runserver/main.c
index 1c5be29..b108137 100644
--- a/src/blogc-runserver/main.c
+++ b/src/blogc-runserver/main.c
@@ -13,7 +13,8 @@
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
-#include "../common/utils.h"
+#include <squareball.h>
+
#include "httpd.h"
@@ -63,9 +64,9 @@ main(int argc, char **argv)
char *endptr;
char *tmp_host = getenv("BLOGC_RUNSERVER_DEFAULT_HOST");
- char *default_host = bc_strdup(tmp_host != NULL ? tmp_host : "127.0.0.1");
+ char *default_host = sb_strdup(tmp_host != NULL ? tmp_host : "127.0.0.1");
char *tmp_port = getenv("BLOGC_RUNSERVER_DEFAULT_PORT");
- char *default_port = bc_strdup(tmp_port != NULL ? tmp_port : "8080");
+ char *default_port = sb_strdup(tmp_port != NULL ? tmp_port : "8080");
size_t args = 0;
@@ -80,15 +81,15 @@ main(int argc, char **argv)
goto cleanup;
case 't':
if (argv[i][2] != '\0')
- host = bc_strdup(argv[i] + 2);
+ host = sb_strdup(argv[i] + 2);
else
- host = bc_strdup(argv[++i]);
+ host = sb_strdup(argv[++i]);
break;
case 'p':
if (argv[i][2] != '\0')
- port = bc_strdup(argv[i] + 2);
+ port = sb_strdup(argv[i] + 2);
else
- port = bc_strdup(argv[++i]);
+ port = sb_strdup(argv[++i]);
break;
case 'm':
if (argv[i][2] != '\0')
@@ -117,7 +118,7 @@ main(int argc, char **argv)
goto cleanup;
}
args++;
- docroot = bc_strdup(argv[i]);
+ docroot = sb_strdup(argv[i]);
}
}
diff --git a/src/blogc-runserver/mime.c b/src/blogc-runserver/mime.c
index 636c496..a6228cf 100644
--- a/src/blogc-runserver/mime.c
+++ b/src/blogc-runserver/mime.c
@@ -9,8 +9,10 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
-#include "../common/utils.h"
+#include <squareball.h>
+
#include "httpd-utils.h"
+#include "mime.h"
// mime types with index should be in the begin of the list. first NULL
@@ -153,7 +155,7 @@ br_mime_guess_index(const char *path)
{
char *found = NULL;
for (size_t i = 0; content_types[i].index != NULL; i++) {
- char *f = bc_strdup_printf("%s/%s", path, content_types[i].index);
+ char *f = sb_strdup_printf("%s/%s", path, content_types[i].index);
if (0 == access(f, F_OK)) {
found = f;
break;