aboutsummaryrefslogtreecommitdiffstats
path: root/src/blogc-runserver
diff options
context:
space:
mode:
authorRafael G. Martins <rafael@rafaelmartins.eng.br>2018-04-05 22:35:25 +0200
committerRafael G. Martins <rafael@rafaelmartins.eng.br>2018-04-05 22:35:35 +0200
commitc43b487246fdfd2ddc5c794763b18255ac6a318e (patch)
treec5b2b5ef837e9e796842155a6f7943745c61d135 /src/blogc-runserver
parent0a7f6a2659b363e1d68202b9fd863b57f7420f4f (diff)
downloadblogc-c43b487246fdfd2ddc5c794763b18255ac6a318e.tar.gz
blogc-c43b487246fdfd2ddc5c794763b18255ac6a318e.tar.bz2
blogc-c43b487246fdfd2ddc5c794763b18255ac6a318e.zip
*: use squareball
yeah, changed my mind again :)
Diffstat (limited to 'src/blogc-runserver')
-rw-r--r--src/blogc-runserver/httpd-utils.c21
-rw-r--r--src/blogc-runserver/httpd.c31
-rw-r--r--src/blogc-runserver/main.c17
-rw-r--r--src/blogc-runserver/mime.c5
4 files changed, 38 insertions, 36 deletions
diff --git a/src/blogc-runserver/httpd-utils.c b/src/blogc-runserver/httpd-utils.c
index 7a49bf2..c1df2c8 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 9680229..b08a915 100644
--- a/src/blogc-runserver/httpd.c
+++ b/src/blogc-runserver/httpd.c
@@ -19,9 +19,8 @@
#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"
@@ -43,7 +42,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 +73,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 +86,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 +96,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);
@@ -155,7 +154,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"
@@ -171,16 +170,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"
@@ -207,7 +206,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);
@@ -227,7 +226,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 b9b831a..318652d 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"
@@ -66,9 +67,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;
@@ -83,15 +84,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')
@@ -120,7 +121,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 18c787c..554fed9 100644
--- a/src/blogc-runserver/mime.c
+++ b/src/blogc-runserver/mime.c
@@ -9,7 +9,8 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
-#include "../common/utils.h"
+#include <squareball.h>
+
#include "httpd-utils.h"
@@ -153,7 +154,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;