feat: serve pastes

c-web-server
Jef Roosens 2023-05-29 21:41:03 +02:00
parent 00fa4a7c8c
commit 7af4be12ff
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
2 changed files with 6 additions and 21 deletions

View File

@ -1,20 +0,0 @@
cmake_minimum_required(VERSION 3.20)
project(lander C CXX)
set(CMAKE_C_STANDARD 17)
set(CMAKE_CXX_STANDARD 17)
include_directories(trie/include)
add_subdirectory(crow)
add_subdirectory(trie)
if(CMAKE_BUILD_TYPE MATCHES "^Test")
enable_testing()
else()
if(CMAKE_BUILD_TYPE STREQUAL Release)
add_compile_options(-O3 -flto)
endif()
add_executable(lander src/main.cpp)
target_link_libraries(lander PUBLIC Crow::Crow trie)
endif()

View File

@ -28,7 +28,7 @@ bool lander_get_entry(event_loop_conn *conn) {
http_loop_ctx *ctx = conn->ctx; http_loop_ctx *ctx = conn->ctx;
const char *key = &ctx->req.path[ctx->req.regex_groups[1].rm_so]; const char *key = &ctx->req.path[ctx->req.regex_groups[1].rm_so];
size_t key_len = int key_len =
ctx->req.regex_groups[1].rm_eo - ctx->req.regex_groups[1].rm_so; ctx->req.regex_groups[1].rm_eo - ctx->req.regex_groups[1].rm_so;
Entry *entry; Entry *entry;
@ -39,6 +39,11 @@ bool lander_get_entry(event_loop_conn *conn) {
} else if (entry->type == Redirect) { } else if (entry->type == Redirect) {
ctx->res.type = http_moved_permanently; ctx->res.type = http_moved_permanently;
http_loop_res_add_header(ctx, http_header_location, entry->string, false); http_loop_res_add_header(ctx, http_header_location, entry->string, false);
} else if (entry->type == Paste) {
char fname[8 + key_len];
sprintf(fname, "pastes/%.*s", key_len, key);
http_loop_res_set_body_file(ctx, fname);
} }
conn->state = event_loop_conn_state_res; conn->state = event_loop_conn_state_res;