Compare commits
30 Commits
65dcbef0b1
...
4089570293
Author | SHA1 | Date |
---|---|---|
Jef Roosens | 4089570293 | |
Jef Roosens | 452e936956 | |
Jef Roosens | 24f7bba622 | |
Jef Roosens | e91631dd42 | |
Jef Roosens | 222b277eef | |
Jef Roosens | f4d711365d | |
Jef Roosens | 5564e23ceb | |
Jef Roosens | 8c2a7a640d | |
Jef Roosens | 11421dca74 | |
Jef Roosens | 665f5fc63c | |
Jef Roosens | 845ec95bf1 | |
Jef Roosens | f37cfc30af | |
Jef Roosens | c26c8cf18a | |
Jef Roosens | 750bee27c7 | |
Jef Roosens | e058c107e2 | |
Jef Roosens | 0ef51ccada | |
Jef Roosens | ba320a4250 | |
Jef Roosens | af5e519663 | |
Jef Roosens | 0bc8fc8273 | |
Jef Roosens | 95788f22e3 | |
Jef Roosens | 5df1ea8309 | |
Jef Roosens | 675c9b78ff | |
Jef Roosens | 8aad8b2d0f | |
Jef Roosens | ce0abcdc25 | |
Jef Roosens | 94da7584aa | |
Jef Roosens | 44ba4b053e | |
Jef Roosens | 7b195c75b0 | |
Jef Roosens | dc0a3a7349 | |
Jef Roosens | e438bd045c | |
Jef Roosens | 8bdf52da0d |
|
@ -0,0 +1,3 @@
|
|||
[submodule "lnm"]
|
||||
path = lnm
|
||||
url = https://git.rustybever.be/Chewing_Bever/lnm
|
|
@ -1,6 +1,7 @@
|
|||
matrix:
|
||||
PLATFORM:
|
||||
- 'linux/amd64'
|
||||
- 'linux/arm64'
|
||||
|
||||
platform: ${PLATFORM}
|
||||
|
||||
|
@ -34,7 +35,8 @@ pipeline:
|
|||
commands:
|
||||
- apk add --no-cache minio-client
|
||||
- mcli alias set rb 'https://s3.rustybever.be' "$MINIO_ACCESS_KEY" "$MINIO_SECRET_KEY"
|
||||
- mcli cp build/lander landerctl/build/landerctl "rb/lander/commits/$CI_COMMIT_SHA/"
|
||||
- mcli cp build/lander "rb/lander/commits/$CI_COMMIT_SHA/lander-$(echo '${PLATFORM}' | sed 's:/:-:g')"
|
||||
- mcli cp landerctl/build/landerctl "rb/lander/commits/$CI_COMMIT_SHA/landerctl-$(echo '${PLATFORM}' | sed 's:/:-:g')"
|
||||
secrets:
|
||||
- minio_access_key
|
||||
- minio_secret_key
|
||||
|
|
24
CHANGELOG.md
24
CHANGELOG.md
|
@ -9,9 +9,24 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
|
||||
### Added
|
||||
|
||||
* LSM
|
||||
* Binary tree iterators
|
||||
* Trie iterators
|
||||
* Store iterators
|
||||
* LTM - Lander Template Library
|
||||
* Simple string templates
|
||||
* Fully streamable template instances
|
||||
* Lander
|
||||
* Simple paste pages with syntax highlighting
|
||||
* Index pages that group elements under the same namespace into an overview
|
||||
|
||||
## [0.2.1](https://git.rustybever.be/Chewing_Bever/lander/src/tag/0.2.1)
|
||||
|
||||
### Added
|
||||
|
||||
* LNM - Lander Network Module
|
||||
* Rewrite of the event loop & HTTP loop
|
||||
* Fully independent library
|
||||
* Fully independent library, maintained in its own repository
|
||||
* Numerous improvements
|
||||
* Streaming of headers
|
||||
* Allow custom & an arbitrary number of response headers
|
||||
|
@ -20,9 +35,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
* Automatically support HEAD requests for all GET requests
|
||||
* Event loop uses `epoll` instead of `poll`
|
||||
* Configurable multithreading using `epoll`
|
||||
* Trie-based router (no more RegEx)
|
||||
* Landerctl
|
||||
* `-c` flag to use custom config file (useful for testing)
|
||||
|
||||
## Removed
|
||||
|
||||
* Content-Disposition header for files
|
||||
* Secure routes with a specified key (e.g. `/sl/:key`), as these were identical
|
||||
to `/s/:key` routes
|
||||
|
||||
## [0.2.0](https://git.rustybever.be/Chewing_Bever/lander/src/tag/0.2.0)
|
||||
|
||||
### Added
|
||||
|
|
11
Makefile
11
Makefile
|
@ -42,7 +42,11 @@ liblsm:
|
|||
liblnm:
|
||||
$(MAKE) -C lnm
|
||||
|
||||
$(BIN): liblsm liblnm $(OBJS)
|
||||
.PHONY: libltm
|
||||
libltm:
|
||||
$(MAKE) -C ltm
|
||||
|
||||
$(BIN): liblsm liblnm libltm $(OBJS)
|
||||
$(CC) -o $@ $(OBJS) $(_LDFLAGS)
|
||||
|
||||
$(BUILD_DIR)/$(SRC_DIR)/%.c.o: $(SRC_DIR)/%.c
|
||||
|
@ -67,7 +71,7 @@ run: $(BIN)
|
|||
valgrind: $(BIN)
|
||||
LANDER_API_KEY=test \
|
||||
LANDER_DATA_DIR=data \
|
||||
valgrind --track-origins=yes '$(BUILD_DIR)/$(BIN_FILENAME)'
|
||||
valgrind --track-origins=yes --leak-check=full '$(BUILD_DIR)/$(BIN_FILENAME)'
|
||||
|
||||
.PHONY: test
|
||||
test: $(TARGETS_TEST)
|
||||
|
@ -106,14 +110,12 @@ $(BUILD_DIR)/$(TEST_DIR)/%.c.o: $(TEST_DIR)/%.c
|
|||
lint:
|
||||
clang-format -n --Werror $(SRCS) $(SRCS_H) $(SRCS_H_INTERNAL)
|
||||
make -C lsm lint
|
||||
make -C lnm lint
|
||||
make -C landerctl lint
|
||||
|
||||
.PHONY: fmt
|
||||
fmt:
|
||||
clang-format -i $(SRCS) $(SRCS_H) $(SRCS_H_INTERNAL)
|
||||
make -C lsm fmt
|
||||
make -C lnm fmt
|
||||
make -C landerctl fmt
|
||||
|
||||
.PHONY: check
|
||||
|
@ -130,7 +132,6 @@ check:
|
|||
-j$(shell nproc) \
|
||||
$(SRCS)
|
||||
make -C lsm check
|
||||
make -C lnm check
|
||||
make -C landerctl check
|
||||
|
||||
.PHONY: clean
|
||||
|
|
10
config.mk
10
config.mk
|
@ -1,4 +1,4 @@
|
|||
VERSION := 0.2.0
|
||||
VERSION := 0.2.1
|
||||
|
||||
BIN_FILENAME = lander
|
||||
|
||||
|
@ -7,16 +7,16 @@ SRC_DIR = src
|
|||
TEST_DIR = test
|
||||
THIRDPARTY_DIR = thirdparty
|
||||
|
||||
INC_DIRS = include $(THIRDPARTY_DIR)/include lsm/include lnm/include
|
||||
LIBS = lsm lnm
|
||||
LIB_DIRS = ./lsm/build ./lnm/build
|
||||
INC_DIRS = include $(THIRDPARTY_DIR)/include lsm/include lnm/include ltm/include
|
||||
LIBS = lsm lnm ltm
|
||||
LIB_DIRS = ./lsm/build ./lnm/build ./ltm/build
|
||||
|
||||
# -MMD: generate a .d file for every source file. This file can be imported by
|
||||
# make and makes make aware that a header file has been changed, ensuring an
|
||||
# object file is also recompiled if only a header is changed.
|
||||
# -MP: generate a dummy target for every header file (according to the docs it
|
||||
# prevents some errors when removing header files)
|
||||
CFLAGS ?= -MMD -MP -g
|
||||
CFLAGS ?= -MMD -MP -g -Wall
|
||||
|
||||
# When compiling release builds, these flags are better
|
||||
# CLAGS = -O3
|
||||
|
|
|
@ -4,16 +4,22 @@
|
|||
#include "lnm/common.h"
|
||||
#include "lnm/http/loop.h"
|
||||
#include "lsm/store.h"
|
||||
#include "ltm/template.h"
|
||||
|
||||
extern const char lander_key_charset[];
|
||||
|
||||
typedef struct lander_gctx {
|
||||
const char *data_dir;
|
||||
lsm_store *store;
|
||||
struct {
|
||||
ltm_template *paste;
|
||||
ltm_template *index;
|
||||
} templates;
|
||||
} lander_gctx;
|
||||
|
||||
typedef struct lander_ctx {
|
||||
lsm_entry_handle *entry;
|
||||
ltm_instance *instance;
|
||||
} lander_ctx;
|
||||
|
||||
typedef enum lander_attr_type : uint8_t {
|
||||
|
@ -27,6 +33,7 @@ typedef enum lander_entry_type : uint8_t {
|
|||
lander_entry_type_redirect = 0,
|
||||
lander_entry_type_paste = 1,
|
||||
lander_entry_type_file = 2,
|
||||
lander_entry_type_index = 3,
|
||||
} lander_entry_type;
|
||||
|
||||
void *lander_gctx_init();
|
||||
|
@ -37,14 +44,18 @@ void lander_ctx_reset(lander_ctx *ctx);
|
|||
|
||||
void lander_ctx_free(lander_ctx *ctx);
|
||||
|
||||
lnm_http_step_err lander_get_index(lnm_http_conn *conn);
|
||||
lnm_http_step_err lander_get_root(lnm_http_conn *conn);
|
||||
|
||||
lnm_http_step_err lander_get_entry(lnm_http_conn *conn);
|
||||
|
||||
lnm_http_step_err lander_post_redirect(lnm_http_conn *conn);
|
||||
|
||||
lnm_http_step_err lander_post_redirect_secure(lnm_http_conn *conn);
|
||||
|
||||
lnm_http_step_err lander_post_paste(lnm_http_conn *conn);
|
||||
|
||||
lnm_http_step_err lander_post_paste_secure(lnm_http_conn *conn);
|
||||
|
||||
lnm_http_step_err lander_stream_body_to_entry(lnm_http_conn *conn);
|
||||
|
||||
lnm_http_step_err lander_post_redirect_body_to_attr(lnm_http_conn *conn);
|
||||
|
@ -53,6 +64,12 @@ lnm_http_step_err lander_remove_entry(lnm_http_conn *conn);
|
|||
|
||||
lnm_http_step_err lander_post_file(lnm_http_conn *conn);
|
||||
|
||||
lnm_http_step_err lander_post_file_secure(lnm_http_conn *conn);
|
||||
|
||||
lnm_http_step_err lander_post_index(lnm_http_conn *conn);
|
||||
|
||||
lnm_http_step_err lander_post_index_secure(lnm_http_conn *conn);
|
||||
|
||||
/**
|
||||
* Store the requested header as an attribute, if it's present.
|
||||
*/
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
Subproject commit 195eb9eb4832f80ee8ab2372192196018362b252
|
|
@ -1,89 +0,0 @@
|
|||
#ifndef LNM_COMMON
|
||||
#define LNM_COMMON
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define LNM_RES(x) \
|
||||
{ \
|
||||
lnm_err res = x; \
|
||||
if (res != lnm_err_ok) \
|
||||
return res; \
|
||||
}
|
||||
|
||||
#define LNM_RES2(x, e) \
|
||||
{ \
|
||||
lnm_err res = x; \
|
||||
if (res != lnm_err_ok) { \
|
||||
e; \
|
||||
return res; \
|
||||
} \
|
||||
}
|
||||
|
||||
#define LNM_MIN(x, y) ((x) < (y) ? (x) : (y))
|
||||
#define LNM_MAX(x, y) ((x) > (y) ? (x) : (y))
|
||||
|
||||
typedef enum lnm_err {
|
||||
lnm_err_ok = 0,
|
||||
lnm_err_failed_alloc,
|
||||
lnm_err_failed_network,
|
||||
lnm_err_failed_poll,
|
||||
lnm_err_not_setup,
|
||||
lnm_err_bad_regex,
|
||||
lnm_err_not_found,
|
||||
} lnm_err;
|
||||
|
||||
typedef struct lnm_loop lnm_http_loop;
|
||||
|
||||
typedef struct lnm_loop_conn lnm_http_conn;
|
||||
|
||||
typedef struct lnm_http_step lnm_http_step;
|
||||
|
||||
typedef struct lnm_http_route lnm_http_route;
|
||||
|
||||
/**
|
||||
* Returns whether the two strings are equal.
|
||||
*
|
||||
* @param s1 first string to compare
|
||||
* @param s1_len length of `s1`
|
||||
* @param s2 second string to compare
|
||||
* @param s2_len length of `s2`
|
||||
*/
|
||||
bool lnm_strneq(const char *s1, size_t s1_len, const char *s2, size_t s2_len);
|
||||
|
||||
/**
|
||||
* Returns whether the two strings are equal, ignoring capitalisation.
|
||||
*
|
||||
* @param s1 first string to compare
|
||||
* @param s1_len length of `s1`
|
||||
* @param s2 second string to compare
|
||||
* @param s2_len length of `s2`
|
||||
*/
|
||||
bool lnm_strnieq(const char *s1, size_t s1_len, const char *s2, size_t s2_len);
|
||||
|
||||
/**
|
||||
* Calculate integer exponentation.
|
||||
*
|
||||
* @param base
|
||||
* @param power
|
||||
*/
|
||||
uint64_t lnm_ipow(uint64_t base, uint64_t power);
|
||||
|
||||
/**
|
||||
* Parse the given string into a number.
|
||||
*
|
||||
* @param s string to parse
|
||||
* @param len length of s
|
||||
* @return the parsed number, or 0 if the number is invalid
|
||||
*/
|
||||
uint64_t lnm_atoi(const char *s, size_t len);
|
||||
|
||||
/**
|
||||
* Calculate how many base 10 digits the given number consists of.
|
||||
*
|
||||
* @param num number to use
|
||||
*/
|
||||
uint64_t lnm_digits(uint64_t num);
|
||||
|
||||
#endif
|
|
@ -1,98 +0,0 @@
|
|||
#ifndef LNM_HTTP_CONSTS
|
||||
#define LNM_HTTP_CONSTS
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
extern const char *lnm_http_method_names[];
|
||||
extern const size_t lnm_http_method_names_len;
|
||||
|
||||
typedef enum lnm_http_method {
|
||||
lnm_http_method_get = 0,
|
||||
lnm_http_method_post,
|
||||
lnm_http_method_put,
|
||||
lnm_http_method_patch,
|
||||
lnm_http_method_delete,
|
||||
lnm_http_method_head,
|
||||
} lnm_http_method;
|
||||
|
||||
extern const char *lnm_http_status_names[][32];
|
||||
|
||||
typedef enum lnm_http_status {
|
||||
// 1xx
|
||||
lnm_http_status_continue = 100,
|
||||
lnm_http_status_switching_protocols = 101,
|
||||
lnm_http_status_processing = 102,
|
||||
lnm_http_status_early_hints = 103,
|
||||
// 2xx
|
||||
lnm_http_status_ok = 200,
|
||||
lnm_http_status_created = 201,
|
||||
lnm_http_status_accepted = 202,
|
||||
lnm_http_status_non_authoritative_information = 203,
|
||||
lnm_http_status_no_content = 204,
|
||||
lnm_http_status_reset_content = 205,
|
||||
lnm_http_status_partial_content = 206,
|
||||
lnm_http_status_multi_status = 207,
|
||||
lnm_http_status_already_reported = 208,
|
||||
// 3xx
|
||||
lnm_http_status_multiple_choices = 300,
|
||||
lnm_http_status_moved_permanently = 301,
|
||||
lnm_http_status_found = 302,
|
||||
lnm_http_status_see_other = 303,
|
||||
lnm_http_status_not_modified = 304,
|
||||
lnm_http_status_temporary_redirect = 307,
|
||||
lnm_http_status_permanent_redirect = 308,
|
||||
// 4xx
|
||||
lnm_http_status_bad_request = 400,
|
||||
lnm_http_status_unauthorized = 401,
|
||||
lnm_http_status_payment_required = 402,
|
||||
lnm_http_status_forbidden = 403,
|
||||
lnm_http_status_not_found = 404,
|
||||
lnm_http_status_method_not_allowed = 405,
|
||||
lnm_http_status_not_acceptable = 406,
|
||||
lnm_http_status_proxy_authentication_required = 407,
|
||||
lnm_http_status_request_timeout = 408,
|
||||
lnm_http_status_conflict = 409,
|
||||
lnm_http_status_gone = 410,
|
||||
lnm_http_status_length_required = 411,
|
||||
lnm_http_status_precondition_failed = 412,
|
||||
lnm_http_status_content_too_large = 413,
|
||||
lnm_http_status_uri_too_long = 414,
|
||||
lnm_http_status_unsupported_media_type = 415,
|
||||
lnm_http_status_range_not_satisfiable = 416,
|
||||
lnm_http_status_expection_failed = 417,
|
||||
lnm_http_status_im_a_teapot = 418,
|
||||
lnm_http_status_misdirected_request = 421,
|
||||
lnm_http_status_unprocessable_content = 422,
|
||||
lnm_http_status_locked = 423,
|
||||
lnm_http_status_failed_dependency = 424,
|
||||
lnm_http_status_too_early = 425,
|
||||
lnm_http_status_upgrade_required = 426,
|
||||
lnm_http_status_precondition_required = 428,
|
||||
lnm_http_status_too_many_requests = 429,
|
||||
lnm_http_status_request_header_fields_too_large = 431,
|
||||
// 5xx
|
||||
lnm_http_status_internal_server_error = 500,
|
||||
lnm_http_status_method_not_implemented = 501,
|
||||
lnm_http_status_bad_gateway = 502,
|
||||
lnm_http_status_service_unavailable = 503,
|
||||
lnm_http_status_gateway_timeout = 504,
|
||||
lnm_http_status_http_status_version_not_supported = 505,
|
||||
lnm_http_status_variant_also_negotiates = 506,
|
||||
lnm_http_status_insufficient_storage = 507,
|
||||
lnm_http_status_loop_detected = 508,
|
||||
lnm_http_status_not_extended = 510,
|
||||
lnm_http_status_network_authentication_required = 511
|
||||
} lnm_http_status;
|
||||
|
||||
extern const char *lnm_http_header_names[];
|
||||
|
||||
typedef enum lnm_http_header {
|
||||
lnm_http_header_connection = 0,
|
||||
lnm_http_header_location,
|
||||
lnm_http_header_content_type,
|
||||
lnm_http_header_content_disposition,
|
||||
lnm_http_header_server,
|
||||
lnm_http_header_content_length
|
||||
} lnm_http_header;
|
||||
|
||||
#endif
|
|
@ -1,141 +0,0 @@
|
|||
#ifndef LNM_HTTP_LOOP
|
||||
#define LNM_HTTP_LOOP
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "lnm/common.h"
|
||||
#include "lnm/http/req.h"
|
||||
#include "lnm/http/res.h"
|
||||
|
||||
typedef enum lnm_http_step_err {
|
||||
lnm_http_step_err_done = 0,
|
||||
lnm_http_step_err_io_needed,
|
||||
lnm_http_step_err_close,
|
||||
lnm_http_step_err_res,
|
||||
} lnm_http_step_err;
|
||||
|
||||
typedef lnm_http_step_err (*lnm_http_step_fn)(lnm_http_conn *conn);
|
||||
|
||||
typedef lnm_err (*lnm_http_ctx_init_fn)(void **c_ctx, void *gctx);
|
||||
|
||||
typedef void (*lnm_http_ctx_reset_fn)(void *c_ctx);
|
||||
|
||||
typedef void (*lnm_http_ctx_free_fn)(void *c_ctx);
|
||||
|
||||
/**
|
||||
* Initialize a new `lnm_http_loop`.
|
||||
*
|
||||
* @param out where to store pointer to new `lnm_http_loop`
|
||||
*/
|
||||
lnm_err lnm_http_loop_init(lnm_http_loop **out, void *c_gctx,
|
||||
lnm_http_ctx_init_fn ctx_init,
|
||||
lnm_http_ctx_reset_fn ctx_reset,
|
||||
lnm_http_ctx_free_fn ctx_free);
|
||||
|
||||
/**
|
||||
* Initialize a new step.
|
||||
*
|
||||
* @param out where to store pointer to new `lnm_http_step`
|
||||
* @param fn step function
|
||||
*/
|
||||
lnm_err lnm_http_step_init(lnm_http_step **out, lnm_http_step_fn fn);
|
||||
|
||||
/**
|
||||
* Append the given step fn to the step.
|
||||
*
|
||||
* @param out where to store pointer to new `lnm_http_step`
|
||||
* @param step step to append new step to
|
||||
* @param fn step function
|
||||
*/
|
||||
lnm_err lnm_http_step_append(lnm_http_step **out, lnm_http_step *step,
|
||||
lnm_http_step_fn fn);
|
||||
|
||||
/**
|
||||
* Initialize a new route of type literal.
|
||||
*
|
||||
* @param out where to store pointer to new `lnm_http_route`
|
||||
* @param path literal path to match
|
||||
* @param step step to process request with
|
||||
*/
|
||||
lnm_err lnm_http_route_init_literal(lnm_http_route **out,
|
||||
lnm_http_method method, const char *path,
|
||||
lnm_http_step *step);
|
||||
|
||||
/**
|
||||
* Initialize a new route of type regex.
|
||||
*
|
||||
* @param out where to store pointer to new `lnm_http_route`
|
||||
* @param pattern regex pattern
|
||||
* @param regex_group_count how many regex groups are contained in the pattern
|
||||
* @param step step to process request with
|
||||
*/
|
||||
lnm_err lnm_http_route_init_regex(lnm_http_route **out, lnm_http_method method,
|
||||
const char *pattern, int regex_group_count,
|
||||
lnm_http_step *step);
|
||||
|
||||
/**
|
||||
* Add a new route to the HTTP route.
|
||||
*
|
||||
* @param hl HTTP loop to modify
|
||||
* @param route route to add
|
||||
*/
|
||||
lnm_err lnm_http_loop_route_add(lnm_http_loop *hl, lnm_http_route *route);
|
||||
|
||||
lnm_err lnm_http_loop_run(lnm_http_loop *hl, uint16_t port, int thread_count);
|
||||
|
||||
void lnm_http_loop_set_api_key(lnm_http_loop *hl, const char *api_key);
|
||||
|
||||
void lnm_http_loop_set_server(lnm_http_loop *hl, const char *value);
|
||||
|
||||
/**
|
||||
* Represents what state an HTTP loop request is currently in.
|
||||
*/
|
||||
typedef enum lnm_http_loop_state {
|
||||
// Parse the HTTP request
|
||||
lnm_http_loop_state_parse_req = 0,
|
||||
// Route the request
|
||||
lnm_http_loop_state_route,
|
||||
// Parse specific headers (e.g. Content-Length)
|
||||
lnm_http_loop_state_parse_headers,
|
||||
// Execute the various steps defined for the route
|
||||
lnm_http_loop_state_steps,
|
||||
// Add certain automatically added headers
|
||||
lnm_http_loop_state_add_headers,
|
||||
// Write the response status line
|
||||
lnm_http_loop_state_write_status_line,
|
||||
// Write the various response headers
|
||||
lnm_http_loop_state_write_headers,
|
||||
// Write the request body
|
||||
lnm_http_loop_state_write_body,
|
||||
// Clean up the request and reset the state for a next request
|
||||
lnm_http_loop_state_finish,
|
||||
} lnm_http_loop_state;
|
||||
|
||||
typedef struct lnm_http_loop_gctx {
|
||||
struct {
|
||||
lnm_http_route **arr;
|
||||
size_t len;
|
||||
} routes;
|
||||
lnm_http_ctx_init_fn ctx_init;
|
||||
lnm_http_ctx_reset_fn ctx_reset;
|
||||
lnm_http_ctx_free_fn ctx_free;
|
||||
const char *api_key;
|
||||
const char *server;
|
||||
void *c;
|
||||
} lnm_http_loop_gctx;
|
||||
|
||||
typedef struct lnm_http_loop_ctx {
|
||||
lnm_http_loop_state state;
|
||||
lnm_http_req req;
|
||||
lnm_http_res res;
|
||||
lnm_http_route *route;
|
||||
lnm_http_step *cur_step;
|
||||
lnm_http_loop_gctx *g;
|
||||
void *c;
|
||||
} lnm_http_loop_ctx;
|
||||
|
||||
lnm_http_step_err lnm_http_loop_step_body_to_buf(lnm_http_conn *conn);
|
||||
|
||||
lnm_http_step_err lnm_http_loop_step_auth(lnm_http_conn *conn);
|
||||
|
||||
#endif
|
|
@ -1,90 +0,0 @@
|
|||
#ifndef LNM_HTTP_REQ
|
||||
#define LNM_HTTP_REQ
|
||||
|
||||
#include <regex.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "picohttpparser.h"
|
||||
|
||||
#include "lnm/common.h"
|
||||
#include "lnm/http/consts.h"
|
||||
|
||||
#define LNM_HTTP_MAX_REQ_HEADERS 32
|
||||
#define LNM_HTTP_MAX_REGEX_GROUPS 4
|
||||
|
||||
/**
|
||||
* Represents the parsed HTTP request
|
||||
*/
|
||||
typedef struct lnm_http_req {
|
||||
size_t len;
|
||||
int minor_version;
|
||||
lnm_http_method method;
|
||||
struct {
|
||||
const char *s;
|
||||
size_t len;
|
||||
regmatch_t groups[LNM_HTTP_MAX_REGEX_GROUPS];
|
||||
} path;
|
||||
struct {
|
||||
const char *s;
|
||||
size_t len;
|
||||
} query;
|
||||
struct {
|
||||
struct phr_header arr[LNM_HTTP_MAX_REQ_HEADERS];
|
||||
size_t len;
|
||||
} headers;
|
||||
struct {
|
||||
uint64_t expected_len;
|
||||
uint64_t len;
|
||||
char *buf;
|
||||
bool owned;
|
||||
} body;
|
||||
} lnm_http_req;
|
||||
|
||||
typedef enum lnm_http_parse_err {
|
||||
lnm_http_parse_err_ok = 0,
|
||||
lnm_http_parse_err_incomplete,
|
||||
lnm_http_parse_err_invalid,
|
||||
lnm_http_parse_err_unknown_method,
|
||||
} lnm_http_parse_err;
|
||||
|
||||
/**
|
||||
* Try to parse the given buffer into an HTTP request.
|
||||
*
|
||||
* @param req request to store parsed data in
|
||||
* @param buf buffer to parse; might be modified
|
||||
* @param len length of buf
|
||||
*/
|
||||
lnm_http_parse_err lnm_http_req_parse(lnm_http_req *req, char *buf, size_t len);
|
||||
|
||||
/**
|
||||
* Reset the given request object, free'ing all its relevant parts and allowing
|
||||
* it to be reused as a new object.
|
||||
*
|
||||
* @param req object to reset
|
||||
*/
|
||||
void lnm_http_req_reset(lnm_http_req *req);
|
||||
|
||||
/**
|
||||
* Retrieve a specific header from the request.
|
||||
*
|
||||
* @param out where to write pointer to header value
|
||||
* @param out_len where to store length of out value
|
||||
* @param req request to look for header in
|
||||
* @param type type of header to look for
|
||||
*/
|
||||
lnm_err lnm_http_req_header_get(const char **out, size_t *out_len,
|
||||
lnm_http_req *req, lnm_http_header type);
|
||||
|
||||
/**
|
||||
* Retrieve a specific header from the request by specifying its name.
|
||||
*
|
||||
* @param out where to write pointer to header value
|
||||
* @param out_len where to store length of out value
|
||||
* @param req request to look for header in
|
||||
* @param name name of the header; matches case-insensitive
|
||||
*/
|
||||
lnm_err lnm_http_req_header_get_s(const char **out, size_t *out_len,
|
||||
lnm_http_req *req, const char *name);
|
||||
|
||||
#endif
|
|
@ -1,119 +0,0 @@
|
|||
#ifndef LNM_HTTP_RES
|
||||
#define LNM_HTTP_RES
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "lnm/common.h"
|
||||
#include "lnm/http/consts.h"
|
||||
|
||||
typedef lnm_err (*data_fn)(uint64_t *written, char *buf, lnm_http_conn *conn,
|
||||
uint64_t offset, uint64_t len);
|
||||
|
||||
/**
|
||||
* Linked list elements used to store the response headers
|
||||
*/
|
||||
typedef struct lnm_http_res_header {
|
||||
struct {
|
||||
char *s;
|
||||
size_t len;
|
||||
bool owned;
|
||||
} name;
|
||||
struct {
|
||||
char *s;
|
||||
size_t len;
|
||||
bool owned;
|
||||
} value;
|
||||
struct lnm_http_res_header *next;
|
||||
} lnm_http_res_header;
|
||||
|
||||
typedef enum lnm_http_res_body_type {
|
||||
lnm_http_res_body_type_file = 0,
|
||||
lnm_http_res_body_type_buf,
|
||||
lnm_http_res_body_type_fn,
|
||||
} lnm_http_res_body_type;
|
||||
|
||||
typedef struct lnm_http_res {
|
||||
lnm_http_status status;
|
||||
struct {
|
||||
lnm_http_res_header *head;
|
||||
lnm_http_res_header *current;
|
||||
} headers;
|
||||
struct {
|
||||
struct {
|
||||
char *buf;
|
||||
FILE *f;
|
||||
data_fn fn;
|
||||
} data;
|
||||
uint64_t len;
|
||||
bool owned;
|
||||
lnm_http_res_body_type type;
|
||||
} body;
|
||||
// General-purpose; meaning depends on the current state
|
||||
uint64_t written;
|
||||
} lnm_http_res;
|
||||
|
||||
/**
|
||||
* Add a new header of a known type to the response
|
||||
*
|
||||
* @param type type of header
|
||||
* @param value null-terminated string containing the value of the header
|
||||
* @param value_owned whether to take ownership of the value pointer; if false,
|
||||
* free'ing the buffer is the caller's responsibility
|
||||
*/
|
||||
lnm_err lnm_http_res_add_header(lnm_http_res *res, lnm_http_header type,
|
||||
char *value, bool value_owned);
|
||||
|
||||
/**
|
||||
* Add a new header of a known type to the response with a given value length.
|
||||
*
|
||||
* @param type type of header
|
||||
* @param value string of length `value_len` containing the value of the header
|
||||
* @param value_len length of value
|
||||
* @param value_owned whether to take ownership of the value pointer; if false,
|
||||
* free'ing the buffer is the caller's responsibility
|
||||
*/
|
||||
lnm_err lnm_http_res_add_header_len(lnm_http_res *res, lnm_http_header type,
|
||||
char *value, size_t value_len,
|
||||
bool value_owned);
|
||||
|
||||
/**
|
||||
* Set the request body to the given file pointer.
|
||||
*
|
||||
* @param res response to modify
|
||||
* @param f file pointer to use as data
|
||||
* @param len expected length of the file
|
||||
* @param owned whether to take ownership of the file pointer
|
||||
*/
|
||||
void lnm_http_res_body_set_file(lnm_http_res *res, FILE *f, size_t len,
|
||||
bool owned);
|
||||
|
||||
/**
|
||||
* Set the request body to the given buffer.
|
||||
*
|
||||
* @param res response to modify
|
||||
* @param buf buffer to use as data
|
||||
* @param len length of the buffer
|
||||
* @param owned whether to take ownership of the file pointer
|
||||
*/
|
||||
void lnm_http_res_body_set_buf(lnm_http_res *res, char *buf, size_t len,
|
||||
bool owned);
|
||||
|
||||
/**
|
||||
* Set the request body to be read from the given data function.
|
||||
*
|
||||
* @param res response to modify
|
||||
* @param fn data reader function
|
||||
* @param len expected length of the response
|
||||
*/
|
||||
void lnm_http_res_body_set_fn(lnm_http_res *res, data_fn fn, size_t len);
|
||||
|
||||
/**
|
||||
* Reset the given response object, properly free'ing any allocated buffers,
|
||||
* allowing it to be reused for later connections.
|
||||
*
|
||||
* @param res res to reset
|
||||
*/
|
||||
void lnm_http_res_reset(lnm_http_res *res);
|
||||
|
||||
#endif
|
|
@ -1,47 +0,0 @@
|
|||
#ifndef LNM_LOG
|
||||
#define LOG
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
#include "lnm/common.h"
|
||||
|
||||
typedef struct lnm_logger lnm_logger;
|
||||
|
||||
typedef enum lnm_log_level {
|
||||
lnm_log_level_debug = 0,
|
||||
lnm_log_level_info,
|
||||
lnm_log_level_notice,
|
||||
lnm_log_level_warning,
|
||||
lnm_log_level_error,
|
||||
lnm_log_level_critical
|
||||
} lnm_log_level;
|
||||
|
||||
extern const char *lnm_log_level_names[];
|
||||
|
||||
/**
|
||||
* Initialize the global logger.
|
||||
*/
|
||||
lnm_err lnm_log_init_global();
|
||||
|
||||
/**
|
||||
* Register stdout as one of the streams for the global logger.
|
||||
*/
|
||||
lnm_err lnm_log_register_stdout(lnm_log_level level);
|
||||
|
||||
void lnm_log(lnm_log_level level, const char *section, const char *fmt, ...)
|
||||
__attribute__((format(printf, 3, 4)));
|
||||
|
||||
#define lnm_ldebug(section, fmt, ...) \
|
||||
lnm_log(lnm_log_level_debug, section, fmt, __VA_ARGS__)
|
||||
#define lnm_linfo(section, fmt, ...) \
|
||||
lnm_log(lnm_log_level_info, section, fmt, __VA_ARGS__)
|
||||
#define lnm_lnotice(section, fmt, ...) \
|
||||
lnm_log(lnm_log_level_notice, section, fmt, __VA_ARGS__)
|
||||
#define lnm_lwarning(section, fmt, ...) \
|
||||
lnm_log(lnm_log_level_warning, section, fmt, __VA_ARGS__)
|
||||
#define lnm_lerror(section, fmt, ...) \
|
||||
lnm_log(lnm_log_level_error, section, fmt, __VA_ARGS__)
|
||||
#define lnm_lcritical(section, fmt, ...) \
|
||||
lnm_log(lnm_log_level_critical, section, fmt, __VA_ARGS__)
|
||||
|
||||
#endif
|
|
@ -1,54 +0,0 @@
|
|||
#ifndef LNM_LOOP
|
||||
#define LNM_LOOP
|
||||
|
||||
#include <stdatomic.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "lnm/common.h"
|
||||
|
||||
#define LNM_LOOP_BUF_SIZE 2048
|
||||
|
||||
typedef enum {
|
||||
lnm_loop_state_req = 0,
|
||||
lnm_loop_state_res,
|
||||
lnm_loop_state_end,
|
||||
} lnm_loop_state;
|
||||
|
||||
typedef struct lnm_loop_conn {
|
||||
int fd;
|
||||
lnm_loop_state state;
|
||||
void *ctx;
|
||||
struct {
|
||||
char buf[LNM_LOOP_BUF_SIZE];
|
||||
size_t size;
|
||||
size_t read;
|
||||
} r;
|
||||
struct {
|
||||
char buf[LNM_LOOP_BUF_SIZE];
|
||||
size_t size;
|
||||
} w;
|
||||
} lnm_loop_conn;
|
||||
|
||||
typedef struct lnm_loop {
|
||||
int listen_fd;
|
||||
int epoll_fd;
|
||||
atomic_int open;
|
||||
void *gctx;
|
||||
lnm_err (*ctx_init)(void **out, void *gctx);
|
||||
void (*ctx_free)(void *ctx);
|
||||
void (*data_read)(lnm_loop_conn *conn);
|
||||
void (*data_write)(lnm_loop_conn *conn);
|
||||
} lnm_loop;
|
||||
|
||||
lnm_err lnm_loop_init(lnm_loop **out, void *gctx,
|
||||
lnm_err (*ctx_init)(void **out, void *gctx),
|
||||
void (*ctx_free)(void *ctx),
|
||||
void (*data_read)(lnm_loop_conn *conn),
|
||||
void (*data_write)(lnm_loop_conn *conn));
|
||||
|
||||
lnm_err lnm_loop_setup(lnm_loop *l, uint16_t port);
|
||||
|
||||
lnm_err lnm_loop_run(lnm_loop *l, int thread_count);
|
||||
|
||||
#endif
|
|
@ -1,87 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2009-2014 Kazuho Oku, Tokuhiro Matsuno, Daisuke Murase,
|
||||
* Shigeo Mitsunari
|
||||
*
|
||||
* The software is licensed under either the MIT License (below) or the Perl
|
||||
* license.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef picohttpparser_h
|
||||
#define picohttpparser_h
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#define ssize_t intptr_t
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* contains name and value of a header (name == NULL if is a continuing line
|
||||
* of a multiline header */
|
||||
struct phr_header {
|
||||
const char *name;
|
||||
size_t name_len;
|
||||
const char *value;
|
||||
size_t value_len;
|
||||
};
|
||||
|
||||
/* returns number of bytes consumed if successful, -2 if request is partial,
|
||||
* -1 if failed */
|
||||
int phr_parse_request(const char *buf, size_t len, const char **method, size_t *method_len, const char **path, size_t *path_len,
|
||||
int *minor_version, struct phr_header *headers, size_t *num_headers, size_t last_len);
|
||||
|
||||
/* ditto */
|
||||
int phr_parse_response(const char *_buf, size_t len, int *minor_version, int *status, const char **msg, size_t *msg_len,
|
||||
struct phr_header *headers, size_t *num_headers, size_t last_len);
|
||||
|
||||
/* ditto */
|
||||
int phr_parse_headers(const char *buf, size_t len, struct phr_header *headers, size_t *num_headers, size_t last_len);
|
||||
|
||||
/* should be zero-filled before start */
|
||||
struct phr_chunked_decoder {
|
||||
size_t bytes_left_in_chunk; /* number of bytes left in current chunk */
|
||||
char consume_trailer; /* if trailing headers should be consumed */
|
||||
char _hex_count;
|
||||
char _state;
|
||||
};
|
||||
|
||||
/* the function rewrites the buffer given as (buf, bufsz) removing the chunked-
|
||||
* encoding headers. When the function returns without an error, bufsz is
|
||||
* updated to the length of the decoded data available. Applications should
|
||||
* repeatedly call the function while it returns -2 (incomplete) every time
|
||||
* supplying newly arrived data. If the end of the chunked-encoded data is
|
||||
* found, the function returns a non-negative number indicating the number of
|
||||
* octets left undecoded, that starts from the offset returned by `*bufsz`.
|
||||
* Returns -1 on error.
|
||||
*/
|
||||
ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, size_t *bufsz);
|
||||
|
||||
/* returns if the chunked decoder is in middle of chunked data */
|
||||
int phr_decode_chunked_is_in_data(struct phr_chunked_decoder *decoder);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -1,69 +0,0 @@
|
|||
#ifndef LNM_HTTP_LOOP_INTERNAL
|
||||
#define LNM_HTTP_LOOP_INTERNAL
|
||||
|
||||
#include <regex.h>
|
||||
|
||||
#include "lnm/http/loop.h"
|
||||
|
||||
typedef struct lnm_http_step {
|
||||
lnm_http_step_fn fn;
|
||||
struct lnm_http_step *next;
|
||||
} lnm_http_step;
|
||||
|
||||
typedef enum lnm_http_route_type {
|
||||
lnm_http_route_type_literal = 0,
|
||||
lnm_http_route_type_regex,
|
||||
} lnm_http_route_type;
|
||||
|
||||
typedef struct lnm_http_route {
|
||||
union {
|
||||
regex_t *regex;
|
||||
const char *s;
|
||||
} route;
|
||||
lnm_http_method method;
|
||||
lnm_http_route_type type;
|
||||
int regex_group_count;
|
||||
lnm_http_step *step;
|
||||
} lnm_http_route;
|
||||
|
||||
/**
|
||||
* Initialize a new empty route.
|
||||
*
|
||||
* @param out where to store pointer to new `lnm_http_route`
|
||||
*/
|
||||
lnm_err lnm_http_route_init(lnm_http_route **out);
|
||||
|
||||
/**
|
||||
* Initialize a first step.
|
||||
*
|
||||
* @param out where to store pointer to new `lnm_http_step`
|
||||
* @param fn step function associated with the step
|
||||
*/
|
||||
lnm_err lnm_http_step_init(lnm_http_step **out, lnm_http_step_fn fn);
|
||||
|
||||
/**
|
||||
* Initialize a new global context object.
|
||||
*
|
||||
* @param out where to store pointer to new `lnm_http_loop_gctx`
|
||||
*/
|
||||
lnm_err lnm_http_loop_gctx_init(lnm_http_loop_gctx **out, void *c_gctx,
|
||||
lnm_http_ctx_init_fn ctx_init,
|
||||
lnm_http_ctx_reset_fn ctx_reset,
|
||||
lnm_http_ctx_free_fn ctx_free);
|
||||
|
||||
/**
|
||||
* Initialize a new context.
|
||||
*
|
||||
* @param out where to store pointer to new object
|
||||
* @param gctx global context for the loop
|
||||
*/
|
||||
lnm_err lnm_http_loop_ctx_init(lnm_http_loop_ctx **out,
|
||||
lnm_http_loop_gctx *gctx);
|
||||
|
||||
void lnm_http_loop_ctx_reset(lnm_http_loop_ctx *ctx);
|
||||
|
||||
void lnm_http_loop_ctx_free(lnm_http_loop_ctx *ctx);
|
||||
|
||||
void lnm_http_loop_process(lnm_http_conn *conn);
|
||||
|
||||
#endif
|
|
@ -1,26 +0,0 @@
|
|||
#ifndef LNM_LOG_INTERNAL
|
||||
#define LNM_LOG_INTERNAL
|
||||
|
||||
#include "lnm/log.h"
|
||||
|
||||
typedef enum lnm_logger_stream_type {
|
||||
lnm_logger_stream_type_file = 0
|
||||
} lnm_logger_stream_type;
|
||||
|
||||
typedef struct lnm_logger_stream {
|
||||
void *ptr;
|
||||
lnm_logger_stream_type type;
|
||||
lnm_log_level level;
|
||||
} lnm_logger_stream;
|
||||
|
||||
struct lnm_logger {
|
||||
struct {
|
||||
lnm_logger_stream **arr;
|
||||
size_t len;
|
||||
} streams;
|
||||
};
|
||||
|
||||
lnm_err lnm_logger_stream_register(lnm_logger *logger,
|
||||
lnm_logger_stream *stream);
|
||||
|
||||
#endif
|
|
@ -1,14 +0,0 @@
|
|||
#ifndef LNM_LOOP_INTERNAL
|
||||
#define LNM_LOOP_INTERNAL
|
||||
|
||||
#include "lnm/loop.h"
|
||||
|
||||
lnm_err lnm_loop_conn_init(lnm_loop_conn **out, lnm_loop *l);
|
||||
|
||||
void lnm_loop_conn_free(lnm_loop *l, lnm_loop_conn *conn);
|
||||
|
||||
lnm_err lnm_loop_accept(lnm_loop *l);
|
||||
|
||||
void lnm_loop_conn_io(lnm_loop *l, lnm_loop_conn *conn);
|
||||
|
||||
#endif
|
|
@ -1,101 +0,0 @@
|
|||
#include "lnm/http/consts.h"
|
||||
|
||||
const char *lnm_http_method_names[] = {"GET", "POST", "PUT",
|
||||
"PATCH", "DELETE", "HEAD"};
|
||||
const size_t lnm_http_method_names_len =
|
||||
sizeof(lnm_http_method_names) / sizeof(lnm_http_method_names[0]);
|
||||
|
||||
// clang-format off
|
||||
|
||||
const char *lnm_http_status_names[][32] = {
|
||||
// 1xx
|
||||
{
|
||||
"Continue", // 100
|
||||
"Switching Protocols", // 101,
|
||||
"Processing", // 102
|
||||
"Early Hints", // 103
|
||||
},
|
||||
// 2xx
|
||||
{
|
||||
"OK", // 200
|
||||
"Created", // 201
|
||||
"Accepted", // 202
|
||||
"Non-Authoritative Information", // 203
|
||||
"No Content", // 204
|
||||
"Reset Content", // 205
|
||||
"Partial Content", // 206
|
||||
"Multi-Status", // 207
|
||||
"Already Reported", // 208
|
||||
},
|
||||
// 3xx
|
||||
{
|
||||
"Multiple Choices", // 300
|
||||
"Moved Permanently", // 301
|
||||
"Found", // 302
|
||||
"See Other", // 303
|
||||
"Not Modified", // 304
|
||||
NULL, // 305
|
||||
NULL, // 306
|
||||
"Temporary Redirect", // 307
|
||||
"Permanent Redirect", // 308
|
||||
},
|
||||
// 4xx
|
||||
{
|
||||
"Bad Request", // 400
|
||||
"Unauthorized", // 401
|
||||
"Payment Required", // 402
|
||||
"Forbidden", // 403
|
||||
"Not Found", // 404
|
||||
"Method Not Allowed", // 405
|
||||
"Not Acceptable", // 406
|
||||
"Proxy Authentication Required", // 407
|
||||
"Request Timeout", // 408
|
||||
"Conflict", // 409
|
||||
"Gone", // 410
|
||||
"Length Required", // 411
|
||||
"Precondition Failed", // 412
|
||||
"Content Too Large", // 413
|
||||
"URI Too Long", // 414
|
||||
"Unsupported Media Type", // 415
|
||||
"Range Not Satisfiable", // 416
|
||||
"Expectation Failed", // 417
|
||||
"I'm a teapot", // 418
|
||||
NULL, // 419
|
||||
NULL, // 420
|
||||
"Misdirected Request", // 421
|
||||
"Unprocessable Content", // 422
|
||||
"Locked", // 423
|
||||
"Failed Dependency", // 424
|
||||
"Too Early", // 425
|
||||
"Upgrade Required", // 426
|
||||
NULL, // 427
|
||||
"Precondition Required", // 428
|
||||
"Too Many Requests", // 429
|
||||
NULL, // 430
|
||||
"Request Header Fields Too Large", // 431
|
||||
},
|
||||
// 5xx
|
||||
{
|
||||
"Internal Server Error", // 500
|
||||
"Not Implemented", // 501
|
||||
"Bad Gateway", // 502
|
||||
"Service Unavailable", // 503
|
||||
"Gateway Timeout", // 504
|
||||
"HTTP Version Not Supported", // 505
|
||||
"Variant Also Negotiates", // 506
|
||||
"Insufficient Storage", // 507
|
||||
"Loop Detected", // 508
|
||||
NULL, // 509
|
||||
"Not Extended", // 510
|
||||
"Network Authentication Required" // 511
|
||||
},
|
||||
};
|
||||
|
||||
const char *lnm_http_header_names[] = {
|
||||
"Connection",
|
||||
"Location",
|
||||
"Content-Type",
|
||||
"Content-Disposition",
|
||||
"Server",
|
||||
"Content-Length"
|
||||
};
|
|
@ -1,138 +0,0 @@
|
|||
#include <stdlib.h>
|
||||
|
||||
#include "lnm/common.h"
|
||||
#include "lnm/http/loop.h"
|
||||
#include "lnm/http/loop_internal.h"
|
||||
#include "lnm/loop_internal.h"
|
||||
|
||||
lnm_err lnm_http_loop_init(lnm_http_loop **out, void *c_gctx,
|
||||
lnm_http_ctx_init_fn ctx_init,
|
||||
lnm_http_ctx_reset_fn ctx_reset,
|
||||
lnm_http_ctx_free_fn ctx_free) {
|
||||
lnm_http_loop *hl = calloc(1, sizeof(lnm_http_loop));
|
||||
|
||||
if (hl == NULL) {
|
||||
return lnm_err_failed_alloc;
|
||||
}
|
||||
|
||||
LNM_RES2(lnm_http_loop_gctx_init((lnm_http_loop_gctx **)&hl->gctx, c_gctx,
|
||||
ctx_init, ctx_reset, ctx_free),
|
||||
free(hl));
|
||||
|
||||
hl->data_read = lnm_http_loop_process;
|
||||
hl->data_write = lnm_http_loop_process;
|
||||
hl->ctx_init = (lnm_err(*)(void **, void *))lnm_http_loop_ctx_init;
|
||||
hl->ctx_free = (void (*)(void *))lnm_http_loop_ctx_free;
|
||||
*out = hl;
|
||||
|
||||
return lnm_err_ok;
|
||||
}
|
||||
|
||||
lnm_err lnm_http_step_init(lnm_http_step **out, lnm_http_step_fn fn) {
|
||||
lnm_http_step *step = calloc(1, sizeof(lnm_http_step));
|
||||
|
||||
if (step == NULL) {
|
||||
return lnm_err_failed_alloc;
|
||||
}
|
||||
|
||||
step->fn = fn;
|
||||
*out = step;
|
||||
|
||||
return lnm_err_ok;
|
||||
}
|
||||
|
||||
lnm_err lnm_http_step_append(lnm_http_step **out, lnm_http_step *step,
|
||||
lnm_http_step_fn fn) {
|
||||
LNM_RES(lnm_http_step_init(out, fn));
|
||||
|
||||
if (step != NULL) {
|
||||
step->next = *out;
|
||||
}
|
||||
|
||||
return lnm_err_ok;
|
||||
}
|
||||
|
||||
lnm_err lnm_http_route_init(lnm_http_route **out) {
|
||||
lnm_http_route *route = calloc(1, sizeof(lnm_http_route));
|
||||
|
||||
if (route == NULL) {
|
||||
return lnm_err_failed_alloc;
|
||||
}
|
||||
|
||||
*out = route;
|
||||
|
||||
return lnm_err_ok;
|
||||
}
|
||||
|
||||
lnm_err lnm_http_route_init_literal(lnm_http_route **out,
|
||||
lnm_http_method method, const char *path,
|
||||
lnm_http_step *step) {
|
||||
LNM_RES(lnm_http_route_init(out));
|
||||
|
||||
(*out)->type = lnm_http_route_type_literal;
|
||||
(*out)->method = method;
|
||||
(*out)->route.s = path;
|
||||
(*out)->step = step;
|
||||
|
||||
return lnm_err_ok;
|
||||
}
|
||||
|
||||
lnm_err lnm_http_route_init_regex(lnm_http_route **out, lnm_http_method method,
|
||||
const char *pattern, int regex_group_count,
|
||||
lnm_http_step *step) {
|
||||
regex_t *regex = calloc(1, sizeof(regex_t));
|
||||
|
||||
if (regex == NULL) {
|
||||
return lnm_err_failed_alloc;
|
||||
}
|
||||
|
||||
if (regcomp(regex, pattern, REG_EXTENDED) != 0) {
|
||||
free(regex);
|
||||
return lnm_err_bad_regex;
|
||||
}
|
||||
|
||||
LNM_RES2(lnm_http_route_init(out), free(regex));
|
||||
|
||||
(*out)->method = method;
|
||||
(*out)->type = lnm_http_route_type_regex;
|
||||
(*out)->route.regex = regex;
|
||||
(*out)->regex_group_count = regex_group_count;
|
||||
(*out)->step = step;
|
||||
|
||||
return lnm_err_ok;
|
||||
}
|
||||
|
||||
lnm_err lnm_http_loop_route_add(lnm_http_loop *hl, lnm_http_route *route) {
|
||||
lnm_http_loop_gctx *gctx = hl->gctx;
|
||||
|
||||
lnm_http_route **new_routes =
|
||||
gctx->routes.len > 0
|
||||
? realloc(gctx->routes.arr,
|
||||
(gctx->routes.len + 1) * sizeof(lnm_http_route *))
|
||||
: malloc(sizeof(lnm_http_route *));
|
||||
|
||||
if (new_routes == NULL) {
|
||||
return lnm_err_failed_alloc;
|
||||
}
|
||||
|
||||
new_routes[gctx->routes.len] = route;
|
||||
gctx->routes.arr = new_routes;
|
||||
gctx->routes.len++;
|
||||
|
||||
return lnm_err_ok;
|
||||
}
|
||||
|
||||
lnm_err lnm_http_loop_run(lnm_http_loop *hl, uint16_t port, int thread_count) {
|
||||
LNM_RES(lnm_loop_setup(hl, port));
|
||||
return lnm_loop_run(hl, thread_count);
|
||||
}
|
||||
|
||||
void lnm_http_loop_set_api_key(lnm_http_loop *hl, const char *api_key) {
|
||||
lnm_http_loop_gctx *gctx = hl->gctx;
|
||||
gctx->api_key = api_key;
|
||||
}
|
||||
|
||||
void lnm_http_loop_set_server(lnm_http_loop *hl, const char *server) {
|
||||
lnm_http_loop_gctx *gctx = hl->gctx;
|
||||
gctx->server = server;
|
||||
}
|
|
@ -1,55 +0,0 @@
|
|||
#include "lnm/http/loop_internal.h"
|
||||
|
||||
lnm_err lnm_http_loop_gctx_init(lnm_http_loop_gctx **out, void *c_gctx,
|
||||
lnm_http_ctx_init_fn ctx_init,
|
||||
lnm_http_ctx_reset_fn ctx_reset,
|
||||
lnm_http_ctx_free_fn ctx_free) {
|
||||
lnm_http_loop_gctx *gctx = calloc(1, sizeof(lnm_http_loop_gctx));
|
||||
|
||||
if (gctx == NULL) {
|
||||
return lnm_err_failed_alloc;
|
||||
}
|
||||
|
||||
gctx->c = c_gctx;
|
||||
gctx->ctx_init = ctx_init;
|
||||
gctx->ctx_reset = ctx_reset;
|
||||
gctx->ctx_free = ctx_free;
|
||||
|
||||
*out = gctx;
|
||||
|
||||
return lnm_err_ok;
|
||||
}
|
||||
|
||||
lnm_err lnm_http_loop_ctx_init(lnm_http_loop_ctx **out,
|
||||
lnm_http_loop_gctx *gctx) {
|
||||
lnm_http_loop_ctx *ctx = calloc(1, sizeof(lnm_http_loop_ctx));
|
||||
|
||||
if (ctx == NULL) {
|
||||
return lnm_err_failed_alloc;
|
||||
}
|
||||
|
||||
LNM_RES2(gctx->ctx_init(&ctx->c, gctx), free(ctx));
|
||||
|
||||
ctx->g = gctx;
|
||||
*out = ctx;
|
||||
|
||||
return lnm_err_ok;
|
||||
}
|
||||
|
||||
void lnm_http_loop_ctx_reset(lnm_http_loop_ctx *ctx) {
|
||||
ctx->g->ctx_reset(ctx->c);
|
||||
|
||||
lnm_http_req_reset(&ctx->req);
|
||||
lnm_http_res_reset(&ctx->res);
|
||||
|
||||
ctx->route = NULL;
|
||||
ctx->cur_step = NULL;
|
||||
}
|
||||
|
||||
void lnm_http_loop_ctx_free(lnm_http_loop_ctx *ctx) {
|
||||
lnm_http_loop_ctx_reset(ctx);
|
||||
|
||||
ctx->g->ctx_free(ctx->c);
|
||||
|
||||
free(ctx);
|
||||
}
|
|
@ -1,369 +0,0 @@
|
|||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lnm/http/consts.h"
|
||||
#include "lnm/http/loop.h"
|
||||
#include "lnm/http/loop_internal.h"
|
||||
#include "lnm/http/req.h"
|
||||
#include "lnm/log.h"
|
||||
#include "lnm/loop.h"
|
||||
#include "lnm/loop_internal.h"
|
||||
|
||||
static const char *section = "http";
|
||||
|
||||
/* static const lnm_http_loop_state lnm_http_loop_state_first_req =
|
||||
* lnm_http_loop_state_parse_req; */
|
||||
static const lnm_http_loop_state lnm_http_loop_state_first_res =
|
||||
lnm_http_loop_state_add_headers;
|
||||
|
||||
void lnm_http_loop_process_parse_req(lnm_http_conn *conn) {
|
||||
lnm_http_loop_ctx *ctx = conn->ctx;
|
||||
|
||||
lnm_http_parse_err res = lnm_http_req_parse(
|
||||
&ctx->req, &conn->r.buf[conn->r.read], conn->r.size - conn->r.read);
|
||||
|
||||
switch (res) {
|
||||
case lnm_http_parse_err_ok:
|
||||
conn->r.read += ctx->req.len;
|
||||
ctx->state = lnm_http_loop_state_route;
|
||||
|
||||
lnm_linfo(section, "%s %.*s HTTP/1.%i",
|
||||
lnm_http_method_names[ctx->req.method], (int)ctx->req.path.len,
|
||||
ctx->req.path.s, ctx->req.minor_version);
|
||||
break;
|
||||
case lnm_http_parse_err_incomplete:
|
||||
// If the request is already the size of the read buffer, we close the
|
||||
// request. Otherwise, we wait for anything read
|
||||
if (conn->r.size - conn->r.read == LNM_LOOP_BUF_SIZE) {
|
||||
lnm_linfo(section, "Received request larger than buffer (%i bytes)",
|
||||
LNM_LOOP_BUF_SIZE);
|
||||
|
||||
conn->state = lnm_loop_state_end;
|
||||
}
|
||||
break;
|
||||
case lnm_http_parse_err_invalid:
|
||||
lnm_linfo(section, "%s", "Received invalid request");
|
||||
|
||||
conn->state = lnm_loop_state_end;
|
||||
break;
|
||||
case lnm_http_parse_err_unknown_method:
|
||||
ctx->res.status = lnm_http_status_method_not_implemented;
|
||||
ctx->state = lnm_http_loop_state_first_res;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void lnm_http_loop_process_route(lnm_http_conn *conn) {
|
||||
lnm_http_loop_ctx *ctx = conn->ctx;
|
||||
lnm_http_loop_gctx *gctx = ctx->g;
|
||||
|
||||
// 0: no match
|
||||
// 1: matched route, but not method
|
||||
// 2: fully matched route
|
||||
int match_level = 0;
|
||||
lnm_http_route *route;
|
||||
|
||||
for (size_t i = 0; i < gctx->routes.len && match_level < 3; i++) {
|
||||
route = gctx->routes.arr[i];
|
||||
bool matched_path = false;
|
||||
|
||||
switch (route->type) {
|
||||
case lnm_http_route_type_literal:
|
||||
matched_path =
|
||||
strncmp(route->route.s, ctx->req.path.s, ctx->req.path.len) == 0;
|
||||
break;
|
||||
case lnm_http_route_type_regex:
|
||||
matched_path =
|
||||
regexec(route->route.regex, ctx->req.path.s,
|
||||
LNM_HTTP_MAX_REGEX_GROUPS, ctx->req.path.groups, 0) == 0;
|
||||
break;
|
||||
}
|
||||
|
||||
// GET routes also automatically route HEAD requests
|
||||
bool matched_method = route->method == ctx->req.method ||
|
||||
(route->method == lnm_http_method_get &&
|
||||
ctx->req.method == lnm_http_method_head);
|
||||
int new_match_level = 2 * matched_path + matched_method;
|
||||
|
||||
// Remember the previous match levels so we can return the correct status
|
||||
// message
|
||||
match_level = match_level < new_match_level ? new_match_level : match_level;
|
||||
}
|
||||
|
||||
switch (match_level) {
|
||||
case 0:
|
||||
case 1:
|
||||
ctx->res.status = lnm_http_status_not_found;
|
||||
ctx->state = lnm_http_loop_state_first_res;
|
||||
break;
|
||||
case 2:
|
||||
ctx->res.status = lnm_http_status_method_not_allowed;
|
||||
ctx->state = lnm_http_loop_state_first_res;
|
||||
break;
|
||||
case 3:
|
||||
ctx->route = route;
|
||||
ctx->cur_step = route->step;
|
||||
ctx->state = lnm_http_loop_state_parse_headers;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void lnm_http_loop_process_parse_headers(lnm_http_conn *conn) {
|
||||
lnm_http_loop_ctx *ctx = conn->ctx;
|
||||
lnm_http_req *req = &ctx->req;
|
||||
|
||||
const char *value;
|
||||
size_t value_len;
|
||||
if (lnm_http_req_header_get(&value, &value_len, req,
|
||||
lnm_http_header_content_length) == lnm_err_ok) {
|
||||
req->body.expected_len = lnm_atoi(value, value_len);
|
||||
}
|
||||
|
||||
ctx->state = lnm_http_loop_state_steps;
|
||||
}
|
||||
|
||||
void lnm_http_loop_process_steps(lnm_http_conn *conn) {
|
||||
lnm_http_loop_ctx *ctx = conn->ctx;
|
||||
lnm_http_step *step = NULL;
|
||||
|
||||
// Loop until we either:
|
||||
// - reach the end of the chain of steps, indicated by NULL
|
||||
// - have a step that's waiting for I/O
|
||||
while ((ctx->cur_step != NULL) && (step != ctx->cur_step)) {
|
||||
step = ctx->cur_step;
|
||||
|
||||
switch (step->fn(conn)) {
|
||||
case lnm_http_step_err_done:
|
||||
ctx->cur_step = ctx->cur_step->next;
|
||||
break;
|
||||
case lnm_http_step_err_io_needed:
|
||||
break;
|
||||
case lnm_http_step_err_close:
|
||||
conn->state = lnm_loop_state_end;
|
||||
break;
|
||||
case lnm_http_step_err_res:
|
||||
ctx->state = lnm_http_loop_state_first_res;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (ctx->cur_step == NULL) {
|
||||
ctx->state = lnm_http_loop_state_add_headers;
|
||||
}
|
||||
}
|
||||
|
||||
void lnm_http_loop_state_process_add_headers(lnm_http_conn *conn) {
|
||||
lnm_http_loop_ctx *ctx = conn->ctx;
|
||||
lnm_http_res *res = &ctx->res;
|
||||
|
||||
uint64_t digits = lnm_digits(res->body.len);
|
||||
char *buf = malloc(digits + 1);
|
||||
|
||||
if (buf == NULL) {
|
||||
conn->state = lnm_loop_state_end;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
sprintf(buf, "%lu", res->body.len);
|
||||
lnm_http_res_add_header_len(res, lnm_http_header_content_length, buf, digits,
|
||||
true);
|
||||
|
||||
if (ctx->g->server != NULL) {
|
||||
lnm_http_res_add_header(res, lnm_http_header_server, (char *)ctx->g->server,
|
||||
false);
|
||||
}
|
||||
|
||||
if (res->status == 0) {
|
||||
res->status = lnm_http_status_ok;
|
||||
}
|
||||
|
||||
lnm_linfo(section, "%i %s", res->status,
|
||||
lnm_http_status_names[res->status / 100 - 1][res->status % 100]);
|
||||
|
||||
ctx->state = lnm_http_loop_state_write_status_line;
|
||||
}
|
||||
|
||||
// This function is intentionally written inefficiently for now, as it will most
|
||||
// likely only have to run once for each response
|
||||
void lnm_http_loop_process_write_status_line(lnm_http_conn *conn) {
|
||||
lnm_http_loop_ctx *ctx = conn->ctx;
|
||||
lnm_http_res *res = &ctx->res;
|
||||
|
||||
const char *response_type_name =
|
||||
lnm_http_status_names[res->status / 100 - 1][res->status % 100];
|
||||
|
||||
// First we calculate the size of the start of the header
|
||||
size_t buf_size =
|
||||
snprintf(NULL, 0, "HTTP/1.1 %i %s\n", res->status, response_type_name);
|
||||
char buf[buf_size + 1];
|
||||
sprintf(buf, "HTTP/1.1 %i %s\n", res->status, response_type_name);
|
||||
|
||||
size_t to_write =
|
||||
LNM_MIN(buf_size - res->written, LNM_LOOP_BUF_SIZE - conn->w.size);
|
||||
memcpy(&conn->w.buf[conn->w.size], &buf[res->written], to_write);
|
||||
|
||||
conn->w.size += to_write;
|
||||
res->written += to_write;
|
||||
|
||||
if (res->written == buf_size) {
|
||||
res->written = 0;
|
||||
ctx->state = lnm_http_loop_state_write_headers;
|
||||
}
|
||||
}
|
||||
|
||||
void lnm_http_loop_process_write_headers(lnm_http_conn *conn) {
|
||||
lnm_http_loop_ctx *ctx = conn->ctx;
|
||||
lnm_http_res *res = &ctx->res;
|
||||
|
||||
lnm_http_res_header *header;
|
||||
|
||||
// Loop as long as we can still write new data and have headers to write
|
||||
while ((conn->w.size < LNM_LOOP_BUF_SIZE) &&
|
||||
((header = res->headers.current) != NULL)) {
|
||||
size_t buf_len = header->name.len + 2 + header->value.len + 1;
|
||||
|
||||
// Here, we also constantly calculate the entire buffer as we assume each
|
||||
// header will be written in one go
|
||||
char buf[buf_len];
|
||||
memcpy(buf, header->name.s, header->name.len);
|
||||
memcpy(&buf[header->name.len + 2], header->value.s, header->value.len);
|
||||
buf[header->name.len] = ':';
|
||||
buf[header->name.len + 1] = ' ';
|
||||
buf[buf_len - 1] = '\n';
|
||||
|
||||
size_t to_write =
|
||||
LNM_MIN(buf_len - res->written, LNM_LOOP_BUF_SIZE - conn->w.size);
|
||||
memcpy(&conn->w.buf[conn->w.size], &buf[res->written], to_write);
|
||||
|
||||
conn->w.size += to_write;
|
||||
res->written += to_write;
|
||||
|
||||
if (res->written == buf_len) {
|
||||
res->written = 0;
|
||||
res->headers.current = res->headers.current->next;
|
||||
}
|
||||
}
|
||||
|
||||
// The headers should end with an additional newline. If there's no space left
|
||||
// in the write buffer, we don't switch states so we can re-try this write
|
||||
// later
|
||||
if ((res->headers.current == NULL) && (conn->w.size < LNM_LOOP_BUF_SIZE)) {
|
||||
conn->w.buf[conn->w.size] = '\n';
|
||||
conn->w.size++;
|
||||
|
||||
// HEAD requests function exactly the same as GET requests, except that they
|
||||
// skip the body writing part
|
||||
ctx->state =
|
||||
ctx->req.method != lnm_http_method_head && ctx->res.body.len > 0
|
||||
? lnm_http_loop_state_write_body
|
||||
: lnm_http_loop_state_finish;
|
||||
}
|
||||
}
|
||||
|
||||
void lnm_http_loop_process_write_body(lnm_http_conn *conn) {
|
||||
lnm_http_loop_ctx *ctx = conn->ctx;
|
||||
lnm_http_res *res = &ctx->res;
|
||||
|
||||
size_t to_write =
|
||||
LNM_MIN(res->body.len - res->written, LNM_LOOP_BUF_SIZE - conn->w.size);
|
||||
size_t written = 0;
|
||||
|
||||
switch (res->body.type) {
|
||||
case lnm_http_res_body_type_buf:
|
||||
memcpy(&conn->w.buf[conn->w.size], &res->body.data.buf[res->written],
|
||||
to_write);
|
||||
written = to_write;
|
||||
break;
|
||||
case lnm_http_res_body_type_file:
|
||||
written = fread(&conn->w.buf[conn->w.size], 1, to_write, res->body.data.f);
|
||||
|
||||
if ((written == 0) && (ferror(res->body.data.f) != 0)) {
|
||||
ctx->state = lnm_http_loop_state_finish;
|
||||
}
|
||||
break;
|
||||
case lnm_http_res_body_type_fn:
|
||||
if (res->body.data.fn(&written, &conn->w.buf[conn->w.size], conn,
|
||||
res->written, to_write) != lnm_err_ok) {
|
||||
ctx->state = lnm_http_loop_state_finish;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
conn->w.size += written;
|
||||
res->written += written;
|
||||
|
||||
if (res->written == res->body.len) {
|
||||
ctx->state = lnm_http_loop_state_finish;
|
||||
}
|
||||
}
|
||||
|
||||
void lnm_http_loop_process_finish(lnm_http_conn *conn) {
|
||||
// First we ensure the write buffer is fully flushed
|
||||
if (conn->w.size > 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
lnm_http_loop_ctx *ctx = conn->ctx;
|
||||
lnm_http_loop_ctx_reset(ctx);
|
||||
|
||||
ctx->state = lnm_http_loop_state_parse_req;
|
||||
}
|
||||
|
||||
void (*process_fns[])(lnm_http_conn *conn) = {
|
||||
lnm_http_loop_process_parse_req,
|
||||
lnm_http_loop_process_route,
|
||||
lnm_http_loop_process_parse_headers,
|
||||
lnm_http_loop_process_steps,
|
||||
lnm_http_loop_state_process_add_headers,
|
||||
lnm_http_loop_process_write_status_line,
|
||||
lnm_http_loop_process_write_headers,
|
||||
lnm_http_loop_process_write_body,
|
||||
lnm_http_loop_process_finish,
|
||||
};
|
||||
|
||||
lnm_loop_state state_map[] = {
|
||||
// parse_req
|
||||
lnm_loop_state_req,
|
||||
// route
|
||||
lnm_loop_state_req,
|
||||
// parse_headers
|
||||
lnm_loop_state_req,
|
||||
// steps
|
||||
lnm_loop_state_req,
|
||||
// add_headers
|
||||
lnm_loop_state_req,
|
||||
// write_status_line
|
||||
lnm_loop_state_res,
|
||||
// write_headers
|
||||
lnm_loop_state_res,
|
||||
// write_body
|
||||
lnm_loop_state_res,
|
||||
// finish
|
||||
lnm_loop_state_res,
|
||||
};
|
||||
|
||||
void lnm_http_loop_process(lnm_http_conn *conn) {
|
||||
const lnm_http_loop_ctx *ctx = conn->ctx;
|
||||
|
||||
lnm_http_loop_state http_loop_state;
|
||||
lnm_loop_state loop_state = conn->state;
|
||||
|
||||
// We stop processing if:
|
||||
// - the event loop state has been explicitely changed inside the executed
|
||||
// step, as we need to switch to the other I/O loop
|
||||
// - the event loop state needs to be changed because the next step should be
|
||||
// run in another event loop state
|
||||
// - the process fn returned without changing the HTTP loop state, indicating
|
||||
// it's waiting for I/O
|
||||
do {
|
||||
http_loop_state = ctx->state;
|
||||
|
||||
process_fns[http_loop_state](conn);
|
||||
} while ((conn->state == state_map[ctx->state]) &&
|
||||
(http_loop_state != ctx->state));
|
||||
|
||||
// Check required to prevent overwriting manually set event loop state
|
||||
conn->state = conn->state == loop_state ? state_map[ctx->state] : conn->state;
|
||||
}
|
|
@ -1,44 +0,0 @@
|
|||
#include <string.h>
|
||||
|
||||
#include "lnm/http/consts.h"
|
||||
#include "lnm/http/loop.h"
|
||||
#include "lnm/loop.h"
|
||||
|
||||
lnm_http_step_err lnm_http_loop_step_body_to_buf(lnm_http_conn *conn) {
|
||||
lnm_http_loop_ctx *ctx = conn->ctx;
|
||||
|
||||
if (ctx->req.body.buf == NULL) {
|
||||
ctx->req.body.buf = malloc(ctx->req.body.expected_len * sizeof(char));
|
||||
ctx->req.body.len = 0;
|
||||
}
|
||||
|
||||
size_t to_read = LNM_MIN(conn->r.size - conn->r.read,
|
||||
ctx->req.body.expected_len - ctx->req.body.len);
|
||||
memcpy(&ctx->req.body.buf[ctx->req.body.len], &conn->r.buf[conn->r.read],
|
||||
to_read);
|
||||
ctx->req.body.len += to_read;
|
||||
conn->r.read += to_read;
|
||||
|
||||
return ctx->req.body.len == ctx->req.body.expected_len
|
||||
? lnm_http_step_err_done
|
||||
: lnm_http_step_err_io_needed;
|
||||
}
|
||||
|
||||
lnm_http_step_err lnm_http_loop_step_auth(lnm_http_conn *conn) {
|
||||
lnm_http_loop_ctx *ctx = conn->ctx;
|
||||
|
||||
// If there's no API key, requests are always authorized
|
||||
bool authorized = ctx->g->api_key == NULL;
|
||||
|
||||
const char *value;
|
||||
size_t value_len;
|
||||
|
||||
if (!authorized && lnm_http_req_header_get_s(&value, &value_len, &ctx->req,
|
||||
"X-Api-Key") == lnm_err_ok) {
|
||||
authorized = (value_len == strlen(ctx->g->api_key)) &&
|
||||
(memcmp(value, ctx->g->api_key, value_len) == 0);
|
||||
}
|
||||
|
||||
ctx->res.status = authorized ? ctx->res.status : lnm_http_status_unauthorized;
|
||||
return authorized ? lnm_http_step_err_done : lnm_http_step_err_res;
|
||||
}
|
|
@ -1,96 +0,0 @@
|
|||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lnm/common.h"
|
||||
#include "lnm/http/consts.h"
|
||||
#include "lnm/http/loop.h"
|
||||
#include "lnm/http/req.h"
|
||||
|
||||
lnm_http_parse_err lnm_http_req_parse(lnm_http_req *req, char *buf,
|
||||
size_t len) {
|
||||
const char *method;
|
||||
char *path;
|
||||
size_t method_len, path_len;
|
||||
|
||||
req->headers.len = LNM_HTTP_MAX_REQ_HEADERS;
|
||||
|
||||
int req_len = phr_parse_request(
|
||||
buf, len, &method, &method_len, (const char **)&path, &path_len,
|
||||
&req->minor_version, req->headers.arr, &req->headers.len, req->len);
|
||||
|
||||
if (req_len == -1) {
|
||||
req->len = len;
|
||||
|
||||
return lnm_http_parse_err_invalid;
|
||||
} else if (req_len == -2) {
|
||||
return lnm_http_parse_err_incomplete;
|
||||
}
|
||||
|
||||
bool known_method = false;
|
||||
|
||||
for (size_t i = 0; i < lnm_http_method_names_len && !known_method; i++) {
|
||||
if (strncmp(method, lnm_http_method_names[i], method_len) == 0) {
|
||||
req->method = i;
|
||||
known_method = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!known_method) {
|
||||
return lnm_http_parse_err_unknown_method;
|
||||
}
|
||||
|
||||
// Path will always end with a newline, which we can safely set to nul
|
||||
path[path_len] = '\0';
|
||||
char *question_mark = strchr(path, '?');
|
||||
|
||||
// Only store query if the path doesn't simply end with a question mark
|
||||
if ((question_mark != NULL) && (path_len - (question_mark + 1 - path) > 0)) {
|
||||
req->query.s = question_mark + 1;
|
||||
req->query.len = path_len - (question_mark + 1 - path);
|
||||
|
||||
path_len = question_mark - path;
|
||||
|
||||
// All parsed strings should be null-terminated. This character is either a
|
||||
// newline (if at the end of the path), or a question mark (if a query is
|
||||
// present).
|
||||
path[path_len] = '\0';
|
||||
}
|
||||
|
||||
req->path.len = path_len;
|
||||
req->path.s = path;
|
||||
req->len = req_len;
|
||||
|
||||
return lnm_http_parse_err_ok;
|
||||
}
|
||||
|
||||
void lnm_http_req_reset(lnm_http_req *req) {
|
||||
if (req->body.owned) {
|
||||
free(req->body.buf);
|
||||
}
|
||||
|
||||
memset(req, 0, sizeof(lnm_http_req));
|
||||
}
|
||||
|
||||
lnm_err lnm_http_req_header_get(const char **out, size_t *out_len,
|
||||
lnm_http_req *req, lnm_http_header type) {
|
||||
return lnm_http_req_header_get_s(out, out_len, req,
|
||||
lnm_http_header_names[type]);
|
||||
}
|
||||
|
||||
lnm_err lnm_http_req_header_get_s(const char **out, size_t *out_len,
|
||||
lnm_http_req *req, const char *name) {
|
||||
size_t name_len = strlen(name);
|
||||
|
||||
for (size_t i = 0; i < req->headers.len; i++) {
|
||||
const struct phr_header *header = &req->headers.arr[i];
|
||||
|
||||
if (lnm_strnieq(header->name, header->name_len, name, name_len)) {
|
||||
*out = header->value;
|
||||
*out_len = header->value_len;
|
||||
|
||||
return lnm_err_ok;
|
||||
}
|
||||
}
|
||||
|
||||
return lnm_err_not_found;
|
||||
}
|
|
@ -1,99 +0,0 @@
|
|||
#include <string.h>
|
||||
|
||||
#include "lnm/http/res.h"
|
||||
|
||||
lnm_err lnm_http_res_add_header(lnm_http_res *res, lnm_http_header type,
|
||||
char *value, bool value_owned) {
|
||||
return lnm_http_res_add_header_len(res, type, value, strlen(value),
|
||||
value_owned);
|
||||
}
|
||||
|
||||
lnm_err lnm_http_res_add_header_len(lnm_http_res *res, lnm_http_header type,
|
||||
char *value, size_t value_len,
|
||||
bool value_owned) {
|
||||
lnm_http_res_header *header = calloc(1, sizeof(lnm_http_res_header));
|
||||
|
||||
if (header == NULL) {
|
||||
return lnm_err_failed_alloc;
|
||||
}
|
||||
|
||||
lnm_http_res_header **next_ptr = &res->headers.head;
|
||||
|
||||
while ((*next_ptr) != NULL) {
|
||||
next_ptr = &(*next_ptr)->next;
|
||||
}
|
||||
|
||||
*next_ptr = header;
|
||||
|
||||
// Initialize the current pointer to the head of the linked list
|
||||
if (res->headers.current == NULL) {
|
||||
res->headers.current = header;
|
||||
}
|
||||
|
||||
header->name.s = (char *)lnm_http_header_names[type];
|
||||
header->name.len = strlen(lnm_http_header_names[type]);
|
||||
header->name.owned = false;
|
||||
|
||||
header->value.s = value;
|
||||
header->value.len = value_len;
|
||||
header->value.owned = value_owned;
|
||||
|
||||
return lnm_err_ok;
|
||||
}
|
||||
|
||||
void lnm_http_res_body_set_file(lnm_http_res *res, FILE *f, size_t len,
|
||||
bool owned) {
|
||||
res->body.data.f = f;
|
||||
res->body.len = len;
|
||||
res->body.owned = owned;
|
||||
res->body.type = lnm_http_res_body_type_file;
|
||||
}
|
||||
|
||||
void lnm_http_res_body_set_buf(lnm_http_res *res, char *buf, size_t len,
|
||||
bool owned) {
|
||||
res->body.data.buf = buf;
|
||||
res->body.len = len;
|
||||
res->body.owned = owned;
|
||||
res->body.type = lnm_http_res_body_type_buf;
|
||||
}
|
||||
|
||||
void lnm_http_res_body_set_fn(lnm_http_res *res, data_fn fn, size_t len) {
|
||||
res->body.data.fn = fn;
|
||||
res->body.len = len;
|
||||
res->body.type = lnm_http_res_body_type_fn;
|
||||
}
|
||||
|
||||
void lnm_http_res_reset(lnm_http_res *res) {
|
||||
lnm_http_res_header *header = res->headers.head;
|
||||
|
||||
while (header != NULL) {
|
||||
lnm_http_res_header *next = header->next;
|
||||
|
||||
if (header->name.owned) {
|
||||
free(header->name.s);
|
||||
}
|
||||
|
||||
if (header->value.owned) {
|
||||
free(header->value.s);
|
||||
}
|
||||
|
||||
free(header);
|
||||
|
||||
header = next;
|
||||
}
|
||||
|
||||
if (res->body.owned) {
|
||||
switch (res->body.type) {
|
||||
case lnm_http_res_body_type_file:
|
||||
fclose(res->body.data.f);
|
||||
break;
|
||||
case lnm_http_res_body_type_buf:
|
||||
free(res->body.data.buf);
|
||||
break;
|
||||
case lnm_http_res_body_type_fn:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
memset(res, 0, sizeof(lnm_http_res));
|
||||
}
|
|
@ -1,86 +0,0 @@
|
|||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "lnm/common.h"
|
||||
#include "lnm/log_internal.h"
|
||||
|
||||
const char *lnm_log_level_names[] = {"DEBUG ", "INFO ", "NOTICE ",
|
||||
"WARNING ", "ERROR ", "CRITICAL"};
|
||||
|
||||
lnm_logger *global_logger = NULL;
|
||||
|
||||
lnm_err lnm_log_init_global() {
|
||||
global_logger = calloc(1, sizeof(lnm_logger));
|
||||
|
||||
return global_logger == NULL ? lnm_err_failed_alloc : lnm_err_ok;
|
||||
}
|
||||
|
||||
lnm_err lnm_logger_stream_register(lnm_logger *logger,
|
||||
lnm_logger_stream *stream) {
|
||||
lnm_logger_stream **new =
|
||||
logger->streams.len == 0
|
||||
? malloc(sizeof(lnm_logger_stream *))
|
||||
: realloc(logger->streams.arr,
|
||||
(logger->streams.len + 1) * sizeof(lnm_logger_stream *));
|
||||
|
||||
if (new == NULL) {
|
||||
return lnm_err_failed_alloc;
|
||||
}
|
||||
|
||||
new[logger->streams.len] = stream;
|
||||
logger->streams.arr = new;
|
||||
logger->streams.len++;
|
||||
|
||||
return lnm_err_ok;
|
||||
}
|
||||
|
||||
lnm_err lnm_log_register_stdout(lnm_log_level level) {
|
||||
lnm_logger_stream *stream = malloc(sizeof(lnm_logger_stream));
|
||||
|
||||
if (stream == NULL) {
|
||||
return lnm_err_failed_alloc;
|
||||
}
|
||||
|
||||
stream->type = lnm_logger_stream_type_file;
|
||||
stream->ptr = stdout;
|
||||
stream->level = level;
|
||||
|
||||
LNM_RES2(lnm_logger_stream_register(global_logger, stream), free(stream));
|
||||
|
||||
return lnm_err_ok;
|
||||
}
|
||||
|
||||
void lnm_vlog(lnm_log_level level, const char *section, const char *fmt,
|
||||
va_list ap) {
|
||||
char date_str[32];
|
||||
|
||||
time_t now = time(NULL);
|
||||
strftime(date_str, sizeof(date_str) - 1, "%Y-%m-%d %H:%M:%S",
|
||||
localtime(&now));
|
||||
|
||||
for (size_t i = 0; i < global_logger->streams.len; i++) {
|
||||
lnm_logger_stream *stream = global_logger->streams.arr[i];
|
||||
|
||||
if (level < stream->level) {
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (stream->type) {
|
||||
case lnm_logger_stream_type_file:
|
||||
fprintf(stream->ptr, "[%s][%s][%s] ", date_str,
|
||||
lnm_log_level_names[level], section);
|
||||
vfprintf(stream->ptr, fmt, ap);
|
||||
fprintf(stream->ptr, "\n");
|
||||
break;
|
||||
}
|
||||
|
||||
va_end(ap);
|
||||
}
|
||||
}
|
||||
|
||||
void lnm_log(lnm_log_level level, const char *section, const char *fmt, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
lnm_vlog(level, section, fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
|
@ -1,57 +0,0 @@
|
|||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lnm/common.h"
|
||||
|
||||
bool lnm_strneq(const char *s1, size_t s1_len, const char *s2, size_t s2_len) {
|
||||
return (s1_len == s2_len) && (memcmp(s1, s2, s1_len) == 0);
|
||||
}
|
||||
|
||||
bool lnm_strnieq(const char *s1, size_t s1_len, const char *s2, size_t s2_len) {
|
||||
bool equal = s1_len == s2_len;
|
||||
|
||||
for (size_t i = 0; i < s1_len && equal; i++) {
|
||||
equal = s1[i] == s2[i] ||
|
||||
(('a' <= s1[i]) && (s1[i] <= 'z') && (s1[i] - 32 == s2[i]));
|
||||
}
|
||||
|
||||
return equal;
|
||||
}
|
||||
|
||||
uint64_t lnm_ipow(uint64_t base, uint64_t power) {
|
||||
uint64_t res = 1;
|
||||
|
||||
while (power > 0) {
|
||||
res *= base;
|
||||
power--;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
uint64_t lnm_atoi(const char *s, size_t len) {
|
||||
uint64_t res = 0;
|
||||
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
if (s[i] < '0' || '9' < s[i]) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint64_t val = s[i] - '0';
|
||||
res += val * lnm_ipow(10, (len - 1) - i);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
uint64_t lnm_digits(uint64_t num) {
|
||||
int digits = 1;
|
||||
|
||||
while (num > 9) {
|
||||
digits++;
|
||||
|
||||
num /= 10;
|
||||
}
|
||||
|
||||
return digits;
|
||||
}
|
|
@ -1,232 +0,0 @@
|
|||
#include <fcntl.h>
|
||||
#include <netinet/in.h>
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/epoll.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "lnm/common.h"
|
||||
#include "lnm/log.h"
|
||||
#include "lnm/loop.h"
|
||||
#include "lnm/loop_internal.h"
|
||||
|
||||
static const char *section = "loop";
|
||||
|
||||
lnm_err lnm_loop_init(lnm_loop **out, void *gctx,
|
||||
lnm_err (*ctx_init)(void **out, void *gctx),
|
||||
void (*ctx_free)(void *ctx),
|
||||
void (*data_read)(lnm_loop_conn *conn),
|
||||
void (*data_write)(lnm_loop_conn *conn)) {
|
||||
lnm_loop *l = calloc(1, sizeof(lnm_loop));
|
||||
|
||||
if (l == NULL) {
|
||||
return lnm_err_failed_alloc;
|
||||
}
|
||||
|
||||
l->gctx = gctx;
|
||||
l->ctx_init = ctx_init;
|
||||
l->ctx_free = ctx_free;
|
||||
l->data_read = data_read;
|
||||
l->data_write = data_write;
|
||||
|
||||
*out = l;
|
||||
|
||||
return lnm_err_ok;
|
||||
}
|
||||
|
||||
lnm_err lnm_loop_accept(lnm_loop *l) {
|
||||
int conn_fd = accept(l->listen_fd, NULL, NULL);
|
||||
|
||||
if (conn_fd < 0) {
|
||||
lnm_lcritical(section, "accept failed: %i", conn_fd);
|
||||
|
||||
return lnm_err_failed_network;
|
||||
}
|
||||
|
||||
// Set socket to non-blocking
|
||||
int flags = fcntl(conn_fd, F_GETFL);
|
||||
flags |= O_NONBLOCK;
|
||||
fcntl(conn_fd, F_SETFL, flags);
|
||||
|
||||
lnm_loop_conn *conn;
|
||||
LNM_RES2(lnm_loop_conn_init(&conn, l), close(conn_fd));
|
||||
|
||||
conn->fd = conn_fd;
|
||||
conn->state = lnm_loop_state_req;
|
||||
|
||||
struct epoll_event event = {.data.ptr = conn,
|
||||
.events = EPOLLIN | EPOLLET | EPOLLONESHOT};
|
||||
|
||||
epoll_ctl(l->epoll_fd, EPOLL_CTL_ADD, conn_fd, &event);
|
||||
|
||||
l->open++;
|
||||
|
||||
lnm_ldebug(section, "connection opened with fd %i", conn_fd);
|
||||
|
||||
return lnm_err_ok;
|
||||
}
|
||||
|
||||
lnm_err lnm_loop_setup(lnm_loop *l, uint16_t port) {
|
||||
int listen_fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
|
||||
if (listen_fd < 0) {
|
||||
return lnm_err_failed_network;
|
||||
}
|
||||
|
||||
int val = 1;
|
||||
int res = setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(int));
|
||||
|
||||
if (res < 0) {
|
||||
return lnm_err_failed_network;
|
||||
}
|
||||
|
||||
struct sockaddr_in addr = {.sin_family = AF_INET,
|
||||
.sin_port = ntohs(port),
|
||||
.sin_addr.s_addr = ntohl(0)};
|
||||
|
||||
res = bind(listen_fd, (const struct sockaddr *)&addr, sizeof(addr));
|
||||
|
||||
if (res < 0) {
|
||||
return lnm_err_failed_network;
|
||||
}
|
||||
|
||||
res = listen(listen_fd, SOMAXCONN);
|
||||
|
||||
if (res < 0) {
|
||||
return lnm_err_failed_network;
|
||||
}
|
||||
|
||||
int flags = fcntl(listen_fd, F_GETFL);
|
||||
fcntl(listen_fd, F_SETFL, flags | O_NONBLOCK);
|
||||
|
||||
int epoll_fd = epoll_create1(0);
|
||||
|
||||
if (epoll_fd < 0) {
|
||||
return lnm_err_failed_network;
|
||||
}
|
||||
|
||||
struct epoll_event event = {
|
||||
// The listening socket is marked using a NULL data field
|
||||
.data.ptr = NULL,
|
||||
.events = EPOLLIN | EPOLLET | EPOLLONESHOT};
|
||||
|
||||
res = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, listen_fd, &event);
|
||||
|
||||
if (res < 0) {
|
||||
return lnm_err_failed_network;
|
||||
}
|
||||
|
||||
l->listen_fd = listen_fd;
|
||||
l->epoll_fd = epoll_fd;
|
||||
|
||||
return lnm_err_ok;
|
||||
}
|
||||
|
||||
typedef struct lnm_loop_thread_args {
|
||||
lnm_loop *l;
|
||||
int id;
|
||||
int thread_count;
|
||||
} lnm_loop_thread_args;
|
||||
|
||||
lnm_err lnm_loop_run_thread(lnm_loop_thread_args *args) {
|
||||
lnm_loop *l = args->l;
|
||||
int thread_id = args->id;
|
||||
int thread_count = args->thread_count;
|
||||
|
||||
struct epoll_event *events = calloc(1, sizeof(struct epoll_event));
|
||||
int events_cap = 1;
|
||||
|
||||
if (events == NULL) {
|
||||
return lnm_err_failed_alloc;
|
||||
}
|
||||
|
||||
lnm_linfo(section, "thread %i started", thread_id);
|
||||
|
||||
struct epoll_event listen_event = {
|
||||
.data.ptr = NULL, .events = EPOLLIN | EPOLLET | EPOLLONESHOT};
|
||||
|
||||
while (1) {
|
||||
int polled = epoll_wait(l->epoll_fd, events, events_cap, -1);
|
||||
lnm_ldebug(section, "polled (thread %i): %i", thread_id, polled);
|
||||
|
||||
if (polled < 0) {
|
||||
return lnm_err_failed_poll;
|
||||
}
|
||||
|
||||
for (int i = 0; i < polled; i++) {
|
||||
if (events[i].data.ptr == NULL) {
|
||||
lnm_loop_accept(l);
|
||||
|
||||
epoll_ctl(l->epoll_fd, EPOLL_CTL_MOD, l->listen_fd, &listen_event);
|
||||
} else {
|
||||
lnm_loop_conn *conn = events[i].data.ptr;
|
||||
lnm_loop_conn_io(l, conn);
|
||||
|
||||
if (conn->state == lnm_loop_state_end) {
|
||||
int conn_fd = conn->fd;
|
||||
|
||||
lnm_loop_conn_free(l, conn);
|
||||
close(conn_fd);
|
||||
l->open--;
|
||||
|
||||
epoll_ctl(l->epoll_fd, EPOLL_CTL_DEL, conn_fd, NULL);
|
||||
|
||||
lnm_ldebug(section, "connection closed with fd %i", conn_fd);
|
||||
} else {
|
||||
struct epoll_event event = {
|
||||
.data.ptr = conn,
|
||||
.events =
|
||||
(conn->state == lnm_loop_state_req ? EPOLLIN : EPOLLOUT) |
|
||||
EPOLLET | EPOLLONESHOT};
|
||||
epoll_ctl(l->epoll_fd, EPOLL_CTL_MOD, conn->fd, &event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int open = l->open;
|
||||
int cap_per_thread =
|
||||
open + 1 > thread_count ? (open + 1) / thread_count : 1;
|
||||
|
||||
if (cap_per_thread > events_cap) {
|
||||
struct epoll_event *new_events =
|
||||
malloc(cap_per_thread * sizeof(struct epoll_event));
|
||||
|
||||
if (new_events == NULL) {
|
||||
return lnm_err_failed_alloc;
|
||||
}
|
||||
|
||||
free(events);
|
||||
events = new_events;
|
||||
events_cap = cap_per_thread;
|
||||
}
|
||||
}
|
||||
|
||||
return lnm_err_ok;
|
||||
}
|
||||
|
||||
lnm_err lnm_loop_run(lnm_loop *l, int thread_count) {
|
||||
if (l->epoll_fd == 0) {
|
||||
return lnm_err_not_setup;
|
||||
}
|
||||
|
||||
lnm_loop_thread_args args[thread_count];
|
||||
|
||||
for (int i = 1; i < thread_count; i++) {
|
||||
args[i].l = l;
|
||||
args[i].id = i;
|
||||
args[i].thread_count = thread_count;
|
||||
|
||||
pthread_t thread;
|
||||
pthread_create(&thread, NULL, (void *(*)(void *))lnm_loop_run_thread,
|
||||
&args[i]);
|
||||
}
|
||||
|
||||
args[0].l = l;
|
||||
args[0].id = 0;
|
||||
args[0].thread_count = thread_count;
|
||||
|
||||
lnm_loop_run_thread(&args[0]);
|
||||
|
||||
return lnm_err_ok;
|
||||
}
|
|
@ -1,22 +0,0 @@
|
|||
#include "lnm/loop_internal.h"
|
||||
|
||||
lnm_err lnm_loop_conn_init(lnm_loop_conn **out, lnm_loop *l) {
|
||||
lnm_loop_conn *conn = calloc(1, sizeof(lnm_loop_conn));
|
||||
|
||||
if (conn == NULL) {
|
||||
return lnm_err_failed_alloc;
|
||||
}
|
||||
|
||||
void *ctx;
|
||||
LNM_RES2(l->ctx_init(&ctx, l->gctx), free(conn));
|
||||
|
||||
conn->ctx = ctx;
|
||||
*out = conn;
|
||||
|
||||
return lnm_err_ok;
|
||||
}
|
||||
|
||||
void lnm_loop_conn_free(lnm_loop *l, lnm_loop_conn *conn) {
|
||||
l->ctx_free(conn->ctx);
|
||||
free(conn);
|
||||
}
|
|
@ -1,77 +0,0 @@
|
|||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "lnm/loop.h"
|
||||
#include "lnm/loop_internal.h"
|
||||
|
||||
void lnm_loop_conn_io_req(lnm_loop *l, lnm_loop_conn *conn) {
|
||||
do {
|
||||
// Move remaining data to front of buffer
|
||||
memmove(conn->r.buf, &conn->r.buf[conn->r.read],
|
||||
conn->r.size - conn->r.read);
|
||||
conn->r.size -= conn->r.read;
|
||||
conn->r.read = 0;
|
||||
|
||||
ssize_t res;
|
||||
size_t cap = LNM_LOOP_BUF_SIZE - conn->r.size;
|
||||
|
||||
do {
|
||||
res = read(conn->fd, &conn->r.buf[conn->r.size], cap);
|
||||
} while (res < 0 && errno == EINTR);
|
||||
|
||||
// Read can't be performed without blocking; we come back later
|
||||
if (res < 0 && errno == EAGAIN) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (res <= 0) {
|
||||
conn->state = lnm_loop_state_end;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
conn->r.size += res;
|
||||
l->data_read(conn);
|
||||
} while (conn->state == lnm_loop_state_req);
|
||||
}
|
||||
|
||||
void lnm_loop_conn_io_res(lnm_loop *l, lnm_loop_conn *conn) {
|
||||
do {
|
||||
l->data_write(conn);
|
||||
|
||||
ssize_t res;
|
||||
|
||||
do {
|
||||
res = write(conn->fd, conn->w.buf, conn->w.size);
|
||||
} while (res < 0 && errno == EINTR);
|
||||
|
||||
// Write can't be performed without blocking; we come back later
|
||||
if (res < 0 && errno == EAGAIN) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (res < 0) {
|
||||
conn->state = lnm_loop_state_end;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Move remaining data to front of buffer. Doing this here gives the data
|
||||
// writer function more space to work with
|
||||
memmove(conn->w.buf, &conn->w.buf[res], conn->w.size - res);
|
||||
conn->w.size -= res;
|
||||
} while (conn->state == lnm_loop_state_res);
|
||||
}
|
||||
|
||||
void lnm_loop_conn_io(lnm_loop *l, lnm_loop_conn *conn) {
|
||||
switch (conn->state) {
|
||||
case lnm_loop_state_req:
|
||||
lnm_loop_conn_io_req(l, conn);
|
||||
break;
|
||||
case lnm_loop_state_res:
|
||||
lnm_loop_conn_io_res(l, conn);
|
||||
break;
|
||||
default:;
|
||||
}
|
||||
}
|
|
@ -1,665 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2009-2014 Kazuho Oku, Tokuhiro Matsuno, Daisuke Murase,
|
||||
* Shigeo Mitsunari
|
||||
*
|
||||
* The software is licensed under either the MIT License (below) or the Perl
|
||||
* license.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#ifdef __SSE4_2__
|
||||
#ifdef _MSC_VER
|
||||
#include <nmmintrin.h>
|
||||
#else
|
||||
#include <x86intrin.h>
|
||||
#endif
|
||||
#endif
|
||||
#include "picohttpparser.h"
|
||||
|
||||
#if __GNUC__ >= 3
|
||||
#define likely(x) __builtin_expect(!!(x), 1)
|
||||
#define unlikely(x) __builtin_expect(!!(x), 0)
|
||||
#else
|
||||
#define likely(x) (x)
|
||||
#define unlikely(x) (x)
|
||||
#endif
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#define ALIGNED(n) _declspec(align(n))
|
||||
#else
|
||||
#define ALIGNED(n) __attribute__((aligned(n)))
|
||||
#endif
|
||||
|
||||
#define IS_PRINTABLE_ASCII(c) ((unsigned char)(c)-040u < 0137u)
|
||||
|
||||
#define CHECK_EOF() \
|
||||
if (buf == buf_end) { \
|
||||
*ret = -2; \
|
||||
return NULL; \
|
||||
}
|
||||
|
||||
#define EXPECT_CHAR_NO_CHECK(ch) \
|
||||
if (*buf++ != ch) { \
|
||||
*ret = -1; \
|
||||
return NULL; \
|
||||
}
|
||||
|
||||
#define EXPECT_CHAR(ch) \
|
||||
CHECK_EOF(); \
|
||||
EXPECT_CHAR_NO_CHECK(ch);
|
||||
|
||||
#define ADVANCE_TOKEN(tok, toklen) \
|
||||
do { \
|
||||
const char *tok_start = buf; \
|
||||
static const char ALIGNED(16) ranges2[16] = "\000\040\177\177"; \
|
||||
int found2; \
|
||||
buf = findchar_fast(buf, buf_end, ranges2, 4, &found2); \
|
||||
if (!found2) { \
|
||||
CHECK_EOF(); \
|
||||
} \
|
||||
while (1) { \
|
||||
if (*buf == ' ') { \
|
||||
break; \
|
||||
} else if (unlikely(!IS_PRINTABLE_ASCII(*buf))) { \
|
||||
if ((unsigned char)*buf < '\040' || *buf == '\177') { \
|
||||
*ret = -1; \
|
||||
return NULL; \
|
||||
} \
|
||||
} \
|
||||
++buf; \
|
||||
CHECK_EOF(); \
|
||||
} \
|
||||
tok = tok_start; \
|
||||
toklen = buf - tok_start; \
|
||||
} while (0)
|
||||
|
||||
static const char *token_char_map = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\1\0\1\1\1\1\1\0\0\1\1\0\1\1\0\1\1\1\1\1\1\1\1\1\1\0\0\0\0\0\0"
|
||||
"\0\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\0\0\0\1\1"
|
||||
"\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\0\1\0\1\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
|
||||
|
||||
static const char *findchar_fast(const char *buf, const char *buf_end, const char *ranges, size_t ranges_size, int *found)
|
||||
{
|
||||
*found = 0;
|
||||
#if __SSE4_2__
|
||||
if (likely(buf_end - buf >= 16)) {
|
||||
__m128i ranges16 = _mm_loadu_si128((const __m128i *)ranges);
|
||||
|
||||
size_t left = (buf_end - buf) & ~15;
|
||||
do {
|
||||
__m128i b16 = _mm_loadu_si128((const __m128i *)buf);
|
||||
int r = _mm_cmpestri(ranges16, ranges_size, b16, 16, _SIDD_LEAST_SIGNIFICANT | _SIDD_CMP_RANGES | _SIDD_UBYTE_OPS);
|
||||
if (unlikely(r != 16)) {
|
||||
buf += r;
|
||||
*found = 1;
|
||||
break;
|
||||
}
|
||||
buf += 16;
|
||||
left -= 16;
|
||||
} while (likely(left != 0));
|
||||
}
|
||||
#else
|
||||
/* suppress unused parameter warning */
|
||||
(void)buf_end;
|
||||
(void)ranges;
|
||||
(void)ranges_size;
|
||||
#endif
|
||||
return buf;
|
||||
}
|
||||
|
||||
static const char *get_token_to_eol(const char *buf, const char *buf_end, const char **token, size_t *token_len, int *ret)
|
||||
{
|
||||
const char *token_start = buf;
|
||||
|
||||
#ifdef __SSE4_2__
|
||||
static const char ALIGNED(16) ranges1[16] = "\0\010" /* allow HT */
|
||||
"\012\037" /* allow SP and up to but not including DEL */
|
||||
"\177\177"; /* allow chars w. MSB set */
|
||||
int found;
|
||||
buf = findchar_fast(buf, buf_end, ranges1, 6, &found);
|
||||
if (found)
|
||||
goto FOUND_CTL;
|
||||
#else
|
||||
/* find non-printable char within the next 8 bytes, this is the hottest code; manually inlined */
|
||||
while (likely(buf_end - buf >= 8)) {
|
||||
#define DOIT() \
|
||||
do { \
|
||||
if (unlikely(!IS_PRINTABLE_ASCII(*buf))) \
|
||||
goto NonPrintable; \
|
||||
++buf; \
|
||||
} while (0)
|
||||
DOIT();
|
||||
DOIT();
|
||||
DOIT();
|
||||
DOIT();
|
||||
DOIT();
|
||||
DOIT();
|
||||
DOIT();
|
||||
DOIT();
|
||||
#undef DOIT
|
||||
continue;
|
||||
NonPrintable:
|
||||
if ((likely((unsigned char)*buf < '\040') && likely(*buf != '\011')) || unlikely(*buf == '\177')) {
|
||||
goto FOUND_CTL;
|
||||
}
|
||||
++buf;
|
||||
}
|
||||
#endif
|
||||
for (;; ++buf) {
|
||||
CHECK_EOF();
|
||||
if (unlikely(!IS_PRINTABLE_ASCII(*buf))) {
|
||||
if ((likely((unsigned char)*buf < '\040') && likely(*buf != '\011')) || unlikely(*buf == '\177')) {
|
||||
goto FOUND_CTL;
|
||||
}
|
||||
}
|
||||
}
|
||||
FOUND_CTL:
|
||||
if (likely(*buf == '\015')) {
|
||||
++buf;
|
||||
EXPECT_CHAR('\012');
|
||||
*token_len = buf - 2 - token_start;
|
||||
} else if (*buf == '\012') {
|
||||
*token_len = buf - token_start;
|
||||
++buf;
|
||||
} else {
|
||||
*ret = -1;
|
||||
return NULL;
|
||||
}
|
||||
*token = token_start;
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
static const char *is_complete(const char *buf, const char *buf_end, size_t last_len, int *ret)
|
||||
{
|
||||
int ret_cnt = 0;
|
||||
buf = last_len < 3 ? buf : buf + last_len - 3;
|
||||
|
||||
while (1) {
|
||||
CHECK_EOF();
|
||||
if (*buf == '\015') {
|
||||
++buf;
|
||||
CHECK_EOF();
|
||||
EXPECT_CHAR('\012');
|
||||
++ret_cnt;
|
||||
} else if (*buf == '\012') {
|
||||
++buf;
|
||||
++ret_cnt;
|
||||
} else {
|
||||
++buf;
|
||||
ret_cnt = 0;
|
||||
}
|
||||
if (ret_cnt == 2) {
|
||||
return buf;
|
||||
}
|
||||
}
|
||||
|
||||
*ret = -2;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#define PARSE_INT(valp_, mul_) \
|
||||
if (*buf < '0' || '9' < *buf) { \
|
||||
buf++; \
|
||||
*ret = -1; \
|
||||
return NULL; \
|
||||
} \
|
||||
*(valp_) = (mul_) * (*buf++ - '0');
|
||||
|
||||
#define PARSE_INT_3(valp_) \
|
||||
do { \
|
||||
int res_ = 0; \
|
||||
PARSE_INT(&res_, 100) \
|
||||
*valp_ = res_; \
|
||||
PARSE_INT(&res_, 10) \
|
||||
*valp_ += res_; \
|
||||
PARSE_INT(&res_, 1) \
|
||||
*valp_ += res_; \
|
||||
} while (0)
|
||||
|
||||
/* returned pointer is always within [buf, buf_end), or null */
|
||||
static const char *parse_token(const char *buf, const char *buf_end, const char **token, size_t *token_len, char next_char,
|
||||
int *ret)
|
||||
{
|
||||
/* We use pcmpestri to detect non-token characters. This instruction can take no more than eight character ranges (8*2*8=128
|
||||
* bits that is the size of a SSE register). Due to this restriction, characters `|` and `~` are handled in the slow loop. */
|
||||
static const char ALIGNED(16) ranges[] = "\x00 " /* control chars and up to SP */
|
||||
"\"\"" /* 0x22 */
|
||||
"()" /* 0x28,0x29 */
|
||||
",," /* 0x2c */
|
||||
"//" /* 0x2f */
|
||||
":@" /* 0x3a-0x40 */
|
||||
"[]" /* 0x5b-0x5d */
|
||||
"{\xff"; /* 0x7b-0xff */
|
||||
const char *buf_start = buf;
|
||||
int found;
|
||||
buf = findchar_fast(buf, buf_end, ranges, sizeof(ranges) - 1, &found);
|
||||
if (!found) {
|
||||
CHECK_EOF();
|
||||
}
|
||||
while (1) {
|
||||
if (*buf == next_char) {
|
||||
break;
|
||||
} else if (!token_char_map[(unsigned char)*buf]) {
|
||||
*ret = -1;
|
||||
return NULL;
|
||||
}
|
||||
++buf;
|
||||
CHECK_EOF();
|
||||
}
|
||||
*token = buf_start;
|
||||
*token_len = buf - buf_start;
|
||||
return buf;
|
||||
}
|
||||
|
||||
/* returned pointer is always within [buf, buf_end), or null */
|
||||
static const char *parse_http_version(const char *buf, const char *buf_end, int *minor_version, int *ret)
|
||||
{
|
||||
/* we want at least [HTTP/1.<two chars>] to try to parse */
|
||||
if (buf_end - buf < 9) {
|
||||
*ret = -2;
|
||||
return NULL;
|
||||
}
|
||||
EXPECT_CHAR_NO_CHECK('H');
|
||||
EXPECT_CHAR_NO_CHECK('T');
|
||||
EXPECT_CHAR_NO_CHECK('T');
|
||||
EXPECT_CHAR_NO_CHECK('P');
|
||||
EXPECT_CHAR_NO_CHECK('/');
|
||||
EXPECT_CHAR_NO_CHECK('1');
|
||||
EXPECT_CHAR_NO_CHECK('.');
|
||||
PARSE_INT(minor_version, 1);
|
||||
return buf;
|
||||
}
|
||||
|
||||
static const char *parse_headers(const char *buf, const char *buf_end, struct phr_header *headers, size_t *num_headers,
|
||||
size_t max_headers, int *ret)
|
||||
{
|
||||
for (;; ++*num_headers) {
|
||||
CHECK_EOF();
|
||||
if (*buf == '\015') {
|
||||
++buf;
|
||||
EXPECT_CHAR('\012');
|
||||
break;
|
||||
} else if (*buf == '\012') {
|
||||
++buf;
|
||||
break;
|
||||
}
|
||||
if (*num_headers == max_headers) {
|
||||
*ret = -1;
|
||||
return NULL;
|
||||
}
|
||||
if (!(*num_headers != 0 && (*buf == ' ' || *buf == '\t'))) {
|
||||
/* parsing name, but do not discard SP before colon, see
|
||||
* http://www.mozilla.org/security/announce/2006/mfsa2006-33.html */
|
||||
if ((buf = parse_token(buf, buf_end, &headers[*num_headers].name, &headers[*num_headers].name_len, ':', ret)) == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
if (headers[*num_headers].name_len == 0) {
|
||||
*ret = -1;
|
||||
return NULL;
|
||||
}
|
||||
++buf;
|
||||
for (;; ++buf) {
|
||||
CHECK_EOF();
|
||||
if (!(*buf == ' ' || *buf == '\t')) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
headers[*num_headers].name = NULL;
|
||||
headers[*num_headers].name_len = 0;
|
||||
}
|
||||
const char *value;
|
||||
size_t value_len;
|
||||
if ((buf = get_token_to_eol(buf, buf_end, &value, &value_len, ret)) == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
/* remove trailing SPs and HTABs */
|
||||
const char *value_end = value + value_len;
|
||||
for (; value_end != value; --value_end) {
|
||||
const char c = *(value_end - 1);
|
||||
if (!(c == ' ' || c == '\t')) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
headers[*num_headers].value = value;
|
||||
headers[*num_headers].value_len = value_end - value;
|
||||
}
|
||||
return buf;
|
||||
}
|
||||
|
||||
static const char *parse_request(const char *buf, const char *buf_end, const char **method, size_t *method_len, const char **path,
|
||||
size_t *path_len, int *minor_version, struct phr_header *headers, size_t *num_headers,
|
||||
size_t max_headers, int *ret)
|
||||
{
|
||||
/* skip first empty line (some clients add CRLF after POST content) */
|
||||
CHECK_EOF();
|
||||
if (*buf == '\015') {
|
||||
++buf;
|
||||
EXPECT_CHAR('\012');
|
||||
} else if (*buf == '\012') {
|
||||
++buf;
|
||||
}
|
||||
|
||||
/* parse request line */
|
||||
if ((buf = parse_token(buf, buf_end, method, method_len, ' ', ret)) == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
do {
|
||||
++buf;
|
||||
CHECK_EOF();
|
||||
} while (*buf == ' ');
|
||||
ADVANCE_TOKEN(*path, *path_len);
|
||||
do {
|
||||
++buf;
|
||||
CHECK_EOF();
|
||||
} while (*buf == ' ');
|
||||
if (*method_len == 0 || *path_len == 0) {
|
||||
*ret = -1;
|
||||
return NULL;
|
||||
}
|
||||
if ((buf = parse_http_version(buf, buf_end, minor_version, ret)) == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
if (*buf == '\015') {
|
||||
++buf;
|
||||
EXPECT_CHAR('\012');
|
||||
} else if (*buf == '\012') {
|
||||
++buf;
|
||||
} else {
|
||||
*ret = -1;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return parse_headers(buf, buf_end, headers, num_headers, max_headers, ret);
|
||||
}
|
||||
|
||||
int phr_parse_request(const char *buf_start, size_t len, const char **method, size_t *method_len, const char **path,
|
||||
size_t *path_len, int *minor_version, struct phr_header *headers, size_t *num_headers, size_t last_len)
|
||||
{
|
||||
const char *buf = buf_start, *buf_end = buf_start + len;
|
||||
size_t max_headers = *num_headers;
|
||||
int r;
|
||||
|
||||
*method = NULL;
|
||||
*method_len = 0;
|
||||
*path = NULL;
|
||||
*path_len = 0;
|
||||
*minor_version = -1;
|
||||
*num_headers = 0;
|
||||
|
||||
/* if last_len != 0, check if the request is complete (a fast countermeasure
|
||||
againt slowloris */
|
||||
if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) {
|
||||
return r;
|
||||
}
|
||||
|
||||
if ((buf = parse_request(buf, buf_end, method, method_len, path, path_len, minor_version, headers, num_headers, max_headers,
|
||||
&r)) == NULL) {
|
||||
return r;
|
||||
}
|
||||
|
||||
return (int)(buf - buf_start);
|
||||
}
|
||||
|
||||
static const char *parse_response(const char *buf, const char *buf_end, int *minor_version, int *status, const char **msg,
|
||||
size_t *msg_len, struct phr_header *headers, size_t *num_headers, size_t max_headers, int *ret)
|
||||
{
|
||||
/* parse "HTTP/1.x" */
|
||||
if ((buf = parse_http_version(buf, buf_end, minor_version, ret)) == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
/* skip space */
|
||||
if (*buf != ' ') {
|
||||
*ret = -1;
|
||||
return NULL;
|
||||
}
|
||||
do {
|
||||
++buf;
|
||||
CHECK_EOF();
|
||||
} while (*buf == ' ');
|
||||
/* parse status code, we want at least [:digit:][:digit:][:digit:]<other char> to try to parse */
|
||||
if (buf_end - buf < 4) {
|
||||
*ret = -2;
|
||||
return NULL;
|
||||
}
|
||||
PARSE_INT_3(status);
|
||||
|
||||
/* get message including preceding space */
|
||||
if ((buf = get_token_to_eol(buf, buf_end, msg, msg_len, ret)) == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
if (*msg_len == 0) {
|
||||
/* ok */
|
||||
} else if (**msg == ' ') {
|
||||
/* Remove preceding space. Successful return from `get_token_to_eol` guarantees that we would hit something other than SP
|
||||
* before running past the end of the given buffer. */
|
||||
do {
|
||||
++*msg;
|
||||
--*msg_len;
|
||||
} while (**msg == ' ');
|
||||
} else {
|
||||
/* garbage found after status code */
|
||||
*ret = -1;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return parse_headers(buf, buf_end, headers, num_headers, max_headers, ret);
|
||||
}
|
||||
|
||||
int phr_parse_response(const char *buf_start, size_t len, int *minor_version, int *status, const char **msg, size_t *msg_len,
|
||||
struct phr_header *headers, size_t *num_headers, size_t last_len)
|
||||
{
|
||||
const char *buf = buf_start, *buf_end = buf + len;
|
||||
size_t max_headers = *num_headers;
|
||||
int r;
|
||||
|
||||
*minor_version = -1;
|
||||
*status = 0;
|
||||
*msg = NULL;
|
||||
*msg_len = 0;
|
||||
*num_headers = 0;
|
||||
|
||||
/* if last_len != 0, check if the response is complete (a fast countermeasure
|
||||
against slowloris */
|
||||
if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) {
|
||||
return r;
|
||||
}
|
||||
|
||||
if ((buf = parse_response(buf, buf_end, minor_version, status, msg, msg_len, headers, num_headers, max_headers, &r)) == NULL) {
|
||||
return r;
|
||||
}
|
||||
|
||||
return (int)(buf - buf_start);
|
||||
}
|
||||
|
||||
int phr_parse_headers(const char *buf_start, size_t len, struct phr_header *headers, size_t *num_headers, size_t last_len)
|
||||
{
|
||||
const char *buf = buf_start, *buf_end = buf + len;
|
||||
size_t max_headers = *num_headers;
|
||||
int r;
|
||||
|
||||
*num_headers = 0;
|
||||
|
||||
/* if last_len != 0, check if the response is complete (a fast countermeasure
|
||||
against slowloris */
|
||||
if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) {
|
||||
return r;
|
||||
}
|
||||
|
||||
if ((buf = parse_headers(buf, buf_end, headers, num_headers, max_headers, &r)) == NULL) {
|
||||
return r;
|
||||
}
|
||||
|
||||
return (int)(buf - buf_start);
|
||||
}
|
||||
|
||||
enum {
|
||||
CHUNKED_IN_CHUNK_SIZE,
|
||||
CHUNKED_IN_CHUNK_EXT,
|
||||
CHUNKED_IN_CHUNK_DATA,
|
||||
CHUNKED_IN_CHUNK_CRLF,
|
||||
CHUNKED_IN_TRAILERS_LINE_HEAD,
|
||||
CHUNKED_IN_TRAILERS_LINE_MIDDLE
|
||||
};
|
||||
|
||||
static int decode_hex(int ch)
|
||||
{
|
||||
if ('0' <= ch && ch <= '9') {
|
||||
return ch - '0';
|
||||
} else if ('A' <= ch && ch <= 'F') {
|
||||
return ch - 'A' + 0xa;
|
||||
} else if ('a' <= ch && ch <= 'f') {
|
||||
return ch - 'a' + 0xa;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, size_t *_bufsz)
|
||||
{
|
||||
size_t dst = 0, src = 0, bufsz = *_bufsz;
|
||||
ssize_t ret = -2; /* incomplete */
|
||||
|
||||
while (1) {
|
||||
switch (decoder->_state) {
|
||||
case CHUNKED_IN_CHUNK_SIZE:
|
||||
for (;; ++src) {
|
||||
int v;
|
||||
if (src == bufsz)
|
||||
goto Exit;
|
||||
if ((v = decode_hex(buf[src])) == -1) {
|
||||
if (decoder->_hex_count == 0) {
|
||||
ret = -1;
|
||||
goto Exit;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (decoder->_hex_count == sizeof(size_t) * 2) {
|
||||
ret = -1;
|
||||
goto Exit;
|
||||
}
|
||||
decoder->bytes_left_in_chunk = decoder->bytes_left_in_chunk * 16 + v;
|
||||
++decoder->_hex_count;
|
||||
}
|
||||
decoder->_hex_count = 0;
|
||||
decoder->_state = CHUNKED_IN_CHUNK_EXT;
|
||||
/* fallthru */
|
||||
case CHUNKED_IN_CHUNK_EXT:
|
||||
/* RFC 7230 A.2 "Line folding in chunk extensions is disallowed" */
|
||||
for (;; ++src) {
|
||||
if (src == bufsz)
|
||||
goto Exit;
|
||||
if (buf[src] == '\012')
|
||||
break;
|
||||
}
|
||||
++src;
|
||||
if (decoder->bytes_left_in_chunk == 0) {
|
||||
if (decoder->consume_trailer) {
|
||||
decoder->_state = CHUNKED_IN_TRAILERS_LINE_HEAD;
|
||||
break;
|
||||
} else {
|
||||
goto Complete;
|
||||
}
|
||||
}
|
||||
decoder->_state = CHUNKED_IN_CHUNK_DATA;
|
||||
/* fallthru */
|
||||
case CHUNKED_IN_CHUNK_DATA: {
|
||||
size_t avail = bufsz - src;
|
||||
if (avail < decoder->bytes_left_in_chunk) {
|
||||
if (dst != src)
|
||||
memmove(buf + dst, buf + src, avail);
|
||||
src += avail;
|
||||
dst += avail;
|
||||
decoder->bytes_left_in_chunk -= avail;
|
||||
goto Exit;
|
||||
}
|
||||
if (dst != src)
|
||||
memmove(buf + dst, buf + src, decoder->bytes_left_in_chunk);
|
||||
src += decoder->bytes_left_in_chunk;
|
||||
dst += decoder->bytes_left_in_chunk;
|
||||
decoder->bytes_left_in_chunk = 0;
|
||||
decoder->_state = CHUNKED_IN_CHUNK_CRLF;
|
||||
}
|
||||
/* fallthru */
|
||||
case CHUNKED_IN_CHUNK_CRLF:
|
||||
for (;; ++src) {
|
||||
if (src == bufsz)
|
||||
goto Exit;
|
||||
if (buf[src] != '\015')
|
||||
break;
|
||||
}
|
||||
if (buf[src] != '\012') {
|
||||
ret = -1;
|
||||
goto Exit;
|
||||
}
|
||||
++src;
|
||||
decoder->_state = CHUNKED_IN_CHUNK_SIZE;
|
||||
break;
|
||||
case CHUNKED_IN_TRAILERS_LINE_HEAD:
|
||||
for (;; ++src) {
|
||||
if (src == bufsz)
|
||||
goto Exit;
|
||||
if (buf[src] != '\015')
|
||||
break;
|
||||
}
|
||||
if (buf[src++] == '\012')
|
||||
goto Complete;
|
||||
decoder->_state = CHUNKED_IN_TRAILERS_LINE_MIDDLE;
|
||||
/* fallthru */
|
||||
case CHUNKED_IN_TRAILERS_LINE_MIDDLE:
|
||||
for (;; ++src) {
|
||||
if (src == bufsz)
|
||||
goto Exit;
|
||||
if (buf[src] == '\012')
|
||||
break;
|
||||
}
|
||||
++src;
|
||||
decoder->_state = CHUNKED_IN_TRAILERS_LINE_HEAD;
|
||||
break;
|
||||
default:
|
||||
assert(!"decoder is corrupt");
|
||||
}
|
||||
}
|
||||
|
||||
Complete:
|
||||
ret = bufsz - src;
|
||||
Exit:
|
||||
if (dst != src)
|
||||
memmove(buf + dst, buf + src, bufsz - src);
|
||||
*_bufsz = dst;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int phr_decode_chunked_is_in_data(struct phr_chunked_decoder *decoder)
|
||||
{
|
||||
return decoder->_state == CHUNKED_IN_CHUNK_DATA;
|
||||
}
|
||||
|
||||
#undef CHECK_EOF
|
||||
#undef EXPECT_CHAR
|
||||
#undef ADVANCE_TOKEN
|
|
@ -27,6 +27,7 @@ typedef enum lsm_error {
|
|||
lsm_error_null_value = 4,
|
||||
lsm_error_failed_io = 5,
|
||||
lsm_error_lock_busy = 6,
|
||||
lsm_error_done = 7,
|
||||
} lsm_error;
|
||||
|
||||
/*typedef struct lsm_string { */
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
#ifndef LSM_BT
|
||||
#define LSM_BT
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "lsm.h"
|
||||
|
||||
/**
|
||||
|
@ -8,6 +10,11 @@
|
|||
*/
|
||||
typedef struct lsm_bt lsm_bt;
|
||||
|
||||
/**
|
||||
* A node inside an `lsm_bt` binary tree.
|
||||
*/
|
||||
typedef struct lsm_bt_node lsm_bt_node;
|
||||
|
||||
/**
|
||||
* Initialize a new binary tree
|
||||
*
|
||||
|
@ -68,4 +75,33 @@ lsm_error lsm_bt_remove(void **out, lsm_bt *bt, char key);
|
|||
*/
|
||||
lsm_error lsm_bt_replace(void **out, lsm_bt *bt, char key, void *data);
|
||||
|
||||
/**
|
||||
* Struct representing an in-flight iterator over the binary tree
|
||||
*/
|
||||
typedef struct lsm_bt_iterator {
|
||||
lsm_bt_node *next;
|
||||
} lsm_bt_iterator;
|
||||
|
||||
/**
|
||||
* Initialize the given iterator for the binary tree.
|
||||
*
|
||||
* The iterator is explicitely allowed to be allocated by the user, as these are
|
||||
* commonly used inside functions where they can simply be stored on the stack.
|
||||
*
|
||||
* @param out iterator to initialize
|
||||
* @param bt binary tree to iterate
|
||||
*/
|
||||
void lsm_bt_iter(lsm_bt_iterator *out, const lsm_bt *bt);
|
||||
|
||||
/**
|
||||
* Advance the iterator to the next element.
|
||||
*
|
||||
* @param out where to store pointer to data; ignored if NULL
|
||||
* @param key_out where to store key; ignored if NULL
|
||||
* @param iter iterator to advance
|
||||
* @return true if a new entry was returned, false if the iterator has no more
|
||||
* entries to return
|
||||
*/
|
||||
bool lsm_bt_iter_next(void **out, char *key_out, lsm_bt_iterator *iter);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -98,6 +98,11 @@ lsm_error lsm_entry_attr_insert_uint8_t(lsm_entry_handle *handle, uint8_t type,
|
|||
lsm_error lsm_entry_attr_remove(lsm_str **out, lsm_entry_handle *handle,
|
||||
uint8_t type);
|
||||
|
||||
/**
|
||||
* Retrieve a pointer to the entry's key.
|
||||
*/
|
||||
void lsm_entry_key(const lsm_str **out, lsm_entry_handle *handle);
|
||||
|
||||
/**
|
||||
* A store consisting of LSM entries.
|
||||
*
|
||||
|
@ -228,4 +233,40 @@ lsm_error lsm_entry_data_read(uint64_t *out, char *buf,
|
|||
*/
|
||||
uint64_t lsm_entry_data_len(lsm_entry_handle *handle);
|
||||
|
||||
/**
|
||||
* Represents an in-flight iterator over an LSM store.
|
||||
*/
|
||||
typedef struct lsm_store_iterator lsm_store_iterator;
|
||||
|
||||
/**
|
||||
* Initialize an iterator to iterate over all entries with keys starting
|
||||
* with the given prefix.
|
||||
*
|
||||
* @param out pointer to store iterator pointer in
|
||||
* @param trie trie to iterate
|
||||
* @param prefix prefix of the keys; a zero-length string means iterating over
|
||||
* the entire trie; NULL is interpreted as a zero-length string
|
||||
*/
|
||||
lsm_error lsm_store_iter(lsm_store_iterator **out, lsm_store *store,
|
||||
const lsm_str *prefix);
|
||||
|
||||
/**
|
||||
* Advance the iterator, returning a read handle to the next entry. The caller
|
||||
* is responsible for closing this handle.
|
||||
*/
|
||||
lsm_error lsm_store_iter_next_read(lsm_entry_handle **out,
|
||||
lsm_store_iterator *iter);
|
||||
|
||||
/**
|
||||
* Advance the iterator, returning a write handle to the next entry. The caller
|
||||
* is responsible for closing this handle.
|
||||
*/
|
||||
/* lsm_error lsm_store_iter_next_write(lsm_entry_handle **out,
|
||||
* lsm_store_iterator *iter); */
|
||||
|
||||
/**
|
||||
* Free the given iterator.
|
||||
*/
|
||||
void lsm_store_iter_free(lsm_store_iterator *iter);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -178,6 +178,15 @@ lsm_error lsm_str_split(lsm_str *s, lsm_str *s2, uint64_t index);
|
|||
* @param s string to append s2 to
|
||||
* @param s2 string to append to s
|
||||
*/
|
||||
lsm_error lsm_str_append(lsm_str *s, lsm_str *s2);
|
||||
lsm_error lsm_str_append(lsm_str *s, const lsm_str *s2);
|
||||
|
||||
/**
|
||||
* Same as `lsm_str_append`, but it takes a C-style string instead.
|
||||
*
|
||||
* @param s string to append c_str to
|
||||
* @param c_str char buffer to append
|
||||
* @param len length of c_str
|
||||
*/
|
||||
lsm_error lsm_str_append_c(lsm_str *s, const char *c_str, uint64_t len);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
#ifndef LSM_TRIE
|
||||
#define LSM_TRIE
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "lsm.h"
|
||||
#include "lsm/bt.h"
|
||||
#include "lsm/str.h"
|
||||
|
||||
/**
|
||||
|
@ -9,6 +12,11 @@
|
|||
*/
|
||||
typedef struct lsm_trie lsm_trie;
|
||||
|
||||
/**
|
||||
* A node inside an `lsm_trie` trie
|
||||
*/
|
||||
typedef struct lsm_trie_node lsm_trie_node;
|
||||
|
||||
/**
|
||||
* Initialize a new trie.
|
||||
*
|
||||
|
@ -57,4 +65,36 @@ lsm_error lsm_trie_remove(void **out, lsm_trie *trie, const lsm_str *key);
|
|||
*/
|
||||
uint64_t lsm_trie_size(const lsm_trie *trie);
|
||||
|
||||
/**
|
||||
* Represents an in-flight iterator over a trie
|
||||
*/
|
||||
typedef struct lsm_trie_iterator lsm_trie_iterator;
|
||||
|
||||
/**
|
||||
* Initialize an iterator to iterate over all entries with keys starting
|
||||
* with the given prefix.
|
||||
*
|
||||
* @param out pointer to store iterator pointer in
|
||||
* @param trie trie to iterate
|
||||
* @param prefix prefix of the keys; a zero-length string means iterating over
|
||||
* the entire trie; NULL is interpreted as a zero-length string
|
||||
*/
|
||||
lsm_error lsm_trie_iter(lsm_trie_iterator **out, const lsm_trie *trie,
|
||||
const lsm_str *prefix);
|
||||
|
||||
/**
|
||||
* Advance the given iterator.
|
||||
*
|
||||
* @param out pointer to store next data pointer in
|
||||
* @param iter iterator to advance
|
||||
* @return `lsm_error_ok` if next element has been returned, `lsm_error_done` if
|
||||
* no more elements are present, or some other error code
|
||||
*/
|
||||
lsm_error lsm_trie_iter_next(void **out, lsm_trie_iterator *iter);
|
||||
|
||||
/**
|
||||
* Free the given iterator.
|
||||
*/
|
||||
void lsm_trie_iter_free(lsm_trie_iterator *iter);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -9,12 +9,13 @@
|
|||
/**
|
||||
* Node inside a binary tree
|
||||
*/
|
||||
typedef struct lsm_bt_node {
|
||||
struct lsm_bt_node {
|
||||
struct lsm_bt_node *left;
|
||||
struct lsm_bt_node *right;
|
||||
struct lsm_bt_node *parent;
|
||||
void *data;
|
||||
char key;
|
||||
} lsm_bt_node;
|
||||
};
|
||||
|
||||
/**
|
||||
* Initialize a new binary tree node
|
||||
|
|
|
@ -34,6 +34,11 @@ typedef struct lsm_entry {
|
|||
uint64_t idx_file_offset;
|
||||
} lsm_entry;
|
||||
|
||||
struct lsm_store_iterator {
|
||||
lsm_trie_iterator *iter;
|
||||
lsm_store *store;
|
||||
};
|
||||
|
||||
/**
|
||||
* Allocate and initialize a new lsm_entry object.
|
||||
*
|
||||
|
|
|
@ -5,14 +5,11 @@
|
|||
#include "lsm/str_internal.h"
|
||||
#include "lsm/trie.h"
|
||||
|
||||
/**
|
||||
* A node inside a trie structure
|
||||
*/
|
||||
typedef struct lsm_trie_node {
|
||||
struct lsm_trie_node {
|
||||
lsm_bt bt;
|
||||
lsm_str skip;
|
||||
void *data;
|
||||
} lsm_trie_node;
|
||||
};
|
||||
|
||||
/**
|
||||
* Allocate and initialize a new trie node
|
||||
|
@ -33,4 +30,13 @@ struct lsm_trie {
|
|||
uint64_t size;
|
||||
};
|
||||
|
||||
struct lsm_trie_iterator {
|
||||
lsm_trie_node *next;
|
||||
struct {
|
||||
lsm_bt_iterator *arr;
|
||||
size_t len;
|
||||
size_t cap;
|
||||
} stack;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#include <stdlib.h>
|
||||
|
||||
#include "lsm/bt.h"
|
||||
#include "lsm/bt_internal.h"
|
||||
|
||||
lsm_error lsm_bt_node_init(lsm_bt_node **ptr, const char key, void *data) {
|
||||
|
@ -60,10 +61,12 @@ void lsm_bt_free(lsm_bt *bt) {
|
|||
uint64_t lsm_bt_size(const lsm_bt *bt) { return bt->size; }
|
||||
|
||||
lsm_error lsm_bt_insert(lsm_bt *bt, char key, void *data) {
|
||||
lsm_bt_node *parent = NULL;
|
||||
lsm_bt_node **dest = &bt->root;
|
||||
|
||||
// Traverse down the tree until we reach the new point to insert our node
|
||||
while ((*dest != NULL) && ((*dest)->key != key)) {
|
||||
parent = *dest;
|
||||
dest = key < (*dest)->key ? &(*dest)->left : &(*dest)->right;
|
||||
}
|
||||
|
||||
|
@ -71,9 +74,8 @@ lsm_error lsm_bt_insert(lsm_bt *bt, char key, void *data) {
|
|||
return lsm_error_already_present;
|
||||
}
|
||||
|
||||
if (lsm_bt_node_init(dest, key, data) != lsm_error_ok) {
|
||||
return lsm_error_failed_alloc;
|
||||
}
|
||||
LSM_RES(lsm_bt_node_init(dest, key, data));
|
||||
(*dest)->parent = parent;
|
||||
|
||||
bt->size++;
|
||||
|
||||
|
@ -125,13 +127,27 @@ lsm_error lsm_bt_remove(void **out, lsm_bt *bt, char key) {
|
|||
(*dest)->data = (*succ)->data;
|
||||
|
||||
lsm_bt_node *succ_replacement = (*succ)->right;
|
||||
lsm_bt_node *parent = (*succ)->parent;
|
||||
|
||||
lsm_bt_node_free(*succ);
|
||||
|
||||
*succ = succ_replacement;
|
||||
|
||||
if (*succ != NULL) {
|
||||
(*succ)->parent = parent;
|
||||
}
|
||||
} else {
|
||||
lsm_bt_node *replacement =
|
||||
(*dest)->left != NULL ? (*dest)->left : (*dest)->right;
|
||||
lsm_bt_node *parent = (*dest)->parent;
|
||||
|
||||
lsm_bt_node_free(*dest);
|
||||
|
||||
*dest = replacement;
|
||||
|
||||
if (*dest != NULL) {
|
||||
(*dest)->parent = parent;
|
||||
}
|
||||
}
|
||||
|
||||
return lsm_error_ok;
|
||||
|
@ -156,3 +172,45 @@ lsm_error lsm_bt_replace(void **out, lsm_bt *bt, char key, void *data) {
|
|||
|
||||
return lsm_error_ok;
|
||||
}
|
||||
|
||||
void lsm_bt_iter(lsm_bt_iterator *out, const lsm_bt *bt) {
|
||||
out->next = bt->root;
|
||||
|
||||
if (bt->root != NULL) {
|
||||
// Initialize the iterator to the smallest element in the tree
|
||||
while (out->next->left != NULL) {
|
||||
out->next = out->next->left;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool lsm_bt_iter_next(void **out, char *key_out, lsm_bt_iterator *iter) {
|
||||
if (iter->next == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (out != NULL) {
|
||||
*out = iter->next->data;
|
||||
}
|
||||
|
||||
if (key_out != NULL) {
|
||||
*key_out = iter->next->key;
|
||||
}
|
||||
|
||||
if (iter->next->right != NULL) {
|
||||
iter->next = iter->next->right;
|
||||
|
||||
while (iter->next->left != NULL) {
|
||||
iter->next = iter->next->left;
|
||||
}
|
||||
} else {
|
||||
while ((iter->next->parent != NULL) &&
|
||||
(iter->next->parent->right == iter->next)) {
|
||||
iter->next = iter->next->parent;
|
||||
}
|
||||
|
||||
iter->next = iter->next->parent;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -316,3 +316,7 @@ lsm_error lsm_entry_data_open_read(lsm_entry_handle *handle) {
|
|||
|
||||
return lsm_error_ok;
|
||||
}
|
||||
|
||||
void lsm_entry_key(const lsm_str **out, lsm_entry_handle *handle) {
|
||||
*out = handle->wrapper->entry->key;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,58 @@
|
|||
#include "lsm/store.h"
|
||||
#include "lsm/store_internal.h"
|
||||
|
||||
lsm_error lsm_store_iter(lsm_store_iterator **out, lsm_store *store,
|
||||
const lsm_str *prefix) {
|
||||
lsm_trie_iterator *trie_iter;
|
||||
LSM_RES(lsm_trie_iter(&trie_iter, store->trie, prefix));
|
||||
|
||||
lsm_store_iterator *iter = calloc(1, sizeof(lsm_store_iterator));
|
||||
|
||||
if (iter == NULL) {
|
||||
lsm_trie_iter_free(trie_iter);
|
||||
|
||||
return lsm_error_failed_alloc;
|
||||
}
|
||||
|
||||
iter->iter = trie_iter;
|
||||
iter->store = store;
|
||||
|
||||
*out = iter;
|
||||
|
||||
return lsm_error_ok;
|
||||
}
|
||||
|
||||
lsm_error lsm_store_iter_next_read(lsm_entry_handle **out,
|
||||
lsm_store_iterator *iter) {
|
||||
lsm_entry_wrapper *wrapper = NULL;
|
||||
|
||||
// Traverse through the trie until a node is found with a filled data field
|
||||
do {
|
||||
// Exits function if iterator is done
|
||||
LSM_RES(lsm_trie_iter_next((void **)&wrapper, iter->iter));
|
||||
|
||||
// TODO error handling?
|
||||
pthread_rwlock_rdlock(&wrapper->lock);
|
||||
|
||||
if (wrapper->entry == NULL) {
|
||||
pthread_rwlock_unlock(&wrapper->lock);
|
||||
|
||||
wrapper = NULL;
|
||||
}
|
||||
} while (wrapper == NULL);
|
||||
|
||||
lsm_entry_handle *handle;
|
||||
LSM_RES2(lsm_entry_handle_init(&handle),
|
||||
pthread_rwlock_unlock(&wrapper->lock));
|
||||
|
||||
handle->wrapper = wrapper;
|
||||
handle->store = iter->store;
|
||||
*out = handle;
|
||||
|
||||
return lsm_error_ok;
|
||||
}
|
||||
|
||||
void lsm_store_iter_free(lsm_store_iterator *iter) {
|
||||
lsm_trie_iter_free(iter->iter);
|
||||
free(iter);
|
||||
}
|
|
@ -215,15 +215,15 @@ bool lsm_str_eq(const lsm_str *s1, const lsm_str *s2) {
|
|||
return memcmp(lsm_str_ptr(s1), lsm_str_ptr(s2), s1->len) == 0;
|
||||
}
|
||||
|
||||
lsm_error lsm_str_append(lsm_str *s, lsm_str *s2) {
|
||||
if (s2->len == 0) {
|
||||
lsm_error lsm_str_append_c(lsm_str *s, const char *c_str, uint64_t len) {
|
||||
if (len == 0) {
|
||||
return lsm_error_ok;
|
||||
}
|
||||
|
||||
uint64_t new_len = s->len + s2->len;
|
||||
uint64_t new_len = s->len + len;
|
||||
|
||||
if (new_len <= 8) {
|
||||
memcpy(&s->data.val[s->len], s2->data.val, s2->len);
|
||||
memcpy(&s->data.val[s->len], c_str, len);
|
||||
} else {
|
||||
char *buf;
|
||||
|
||||
|
@ -243,11 +243,15 @@ lsm_error lsm_str_append(lsm_str *s, lsm_str *s2) {
|
|||
}
|
||||
}
|
||||
|
||||
memcpy(&buf[s->len], lsm_str_ptr(s2), s2->len);
|
||||
memcpy(&buf[s->len], c_str, len);
|
||||
s->data.ptr = buf;
|
||||
}
|
||||
|
||||
s->len += s2->len;
|
||||
s->len += len;
|
||||
|
||||
return lsm_error_ok;
|
||||
}
|
||||
|
||||
lsm_error lsm_str_append(lsm_str *s, const lsm_str *s2) {
|
||||
return lsm_str_append_c(s, lsm_str_ptr(s2), s2->len);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,117 @@
|
|||
#include "lsm.h"
|
||||
#include "lsm/trie_internal.h"
|
||||
|
||||
/**
|
||||
* Advance the iterator so that `next` now points to a node containing data. If
|
||||
* no more data nodes are found, `next` is set to NULL. The initial value of
|
||||
* `next` is ignored.
|
||||
*/
|
||||
lsm_error lsm_trie_iter_next_data_node(lsm_trie_iterator *iter) {
|
||||
do {
|
||||
// Traverse back up the stack until we reach either the end of the subtree,
|
||||
// or an iterator that hasn't been exhausted yet
|
||||
while ((iter->stack.len > 0) &&
|
||||
!lsm_bt_iter_next((void **)&iter->next, NULL,
|
||||
&iter->stack.arr[iter->stack.len - 1])) {
|
||||
iter->stack.len--;
|
||||
}
|
||||
|
||||
if (iter->stack.len == 0) {
|
||||
iter->next = NULL;
|
||||
|
||||
return lsm_error_ok;
|
||||
}
|
||||
|
||||
// Add node's subtree to the stack
|
||||
if (iter->stack.len == iter->stack.cap) {
|
||||
lsm_bt_iterator *arr = realloc(
|
||||
iter->stack.arr, iter->stack.cap * 2 * sizeof(lsm_bt_iterator));
|
||||
|
||||
if (arr == NULL) {
|
||||
return lsm_error_failed_alloc;
|
||||
}
|
||||
|
||||
iter->stack.arr = arr;
|
||||
iter->stack.cap *= 2;
|
||||
}
|
||||
|
||||
lsm_bt_iter(&iter->stack.arr[iter->stack.len], &iter->next->bt);
|
||||
iter->stack.len++;
|
||||
|
||||
} while (iter->next->data == NULL);
|
||||
|
||||
return lsm_error_ok;
|
||||
}
|
||||
|
||||
lsm_error lsm_trie_iter(lsm_trie_iterator **out, const lsm_trie *trie,
|
||||
const lsm_str *prefix) {
|
||||
uint64_t prefix_len = prefix == NULL ? 0 : lsm_str_len(prefix);
|
||||
|
||||
uint64_t index = 0;
|
||||
lsm_trie_node *node = trie->root;
|
||||
|
||||
// Simplified traversal down the trie to find the root of the subtree
|
||||
// contaiing all nodes with the given prefix
|
||||
while ((index < prefix_len) && (node != NULL)) {
|
||||
char c = lsm_str_char(prefix, index);
|
||||
lsm_error res = lsm_bt_search((void **)&node, &node->bt, c);
|
||||
|
||||
switch (res) {
|
||||
case lsm_error_ok:
|
||||
index += 1 + lsm_str_len(&node->skip);
|
||||
break;
|
||||
case lsm_error_not_found:
|
||||
node = NULL;
|
||||
break;
|
||||
default:
|
||||
return res;
|
||||
};
|
||||
}
|
||||
|
||||
lsm_trie_iterator *iter = calloc(1, sizeof(lsm_trie_iterator));
|
||||
|
||||
if (iter == NULL) {
|
||||
return lsm_error_failed_alloc;
|
||||
}
|
||||
|
||||
iter->next = node;
|
||||
|
||||
if (node != NULL) {
|
||||
lsm_bt_iterator *arr = malloc(sizeof(lsm_bt_iterator));
|
||||
|
||||
if (arr == NULL) {
|
||||
free(iter);
|
||||
|
||||
return lsm_error_failed_alloc;
|
||||
}
|
||||
|
||||
iter->stack.arr = arr;
|
||||
iter->stack.cap = 1;
|
||||
iter->stack.len = 1;
|
||||
|
||||
lsm_bt_iter(&iter->stack.arr[0], &node->bt);
|
||||
|
||||
if (node->data == NULL) {
|
||||
LSM_RES(lsm_trie_iter_next_data_node(iter));
|
||||
}
|
||||
}
|
||||
|
||||
*out = iter;
|
||||
|
||||
return lsm_error_ok;
|
||||
}
|
||||
|
||||
lsm_error lsm_trie_iter_next(void **out, lsm_trie_iterator *iter) {
|
||||
if (iter->next == NULL) {
|
||||
return lsm_error_done;
|
||||
}
|
||||
|
||||
*out = iter->next->data;
|
||||
|
||||
return lsm_trie_iter_next_data_node(iter);
|
||||
}
|
||||
|
||||
void lsm_trie_iter_free(lsm_trie_iterator *iter) {
|
||||
free(iter->stack.arr);
|
||||
free(iter);
|
||||
}
|
|
@ -104,6 +104,33 @@ void test_remove_multiple() {
|
|||
lsm_bt_free(bt);
|
||||
}
|
||||
|
||||
void test_iter() {
|
||||
char chars[] = "falcoep";
|
||||
size_t char_count = sizeof(chars) / sizeof(char) - 1;
|
||||
|
||||
char sorted_chars[] = "aceflop";
|
||||
|
||||
BT_INIT();
|
||||
|
||||
for (size_t i = 0; i < char_count; i++) {
|
||||
TEST_CHECK(lsm_bt_insert(bt, chars[i], (void *)(i + 1)) == lsm_error_ok);
|
||||
}
|
||||
|
||||
lsm_bt_iterator iter;
|
||||
lsm_bt_iter(&iter, bt);
|
||||
|
||||
char key;
|
||||
void *data;
|
||||
size_t i = 0;
|
||||
|
||||
while (lsm_bt_iter_next(&data, &key, &iter)) {
|
||||
TEST_CHECK_(key == sorted_chars[i], "%c == %c", key, sorted_chars[i]);
|
||||
i++;
|
||||
}
|
||||
|
||||
TEST_CHECK(i == char_count);
|
||||
}
|
||||
|
||||
TEST_LIST = {
|
||||
{ "bt init", test_init },
|
||||
{ "bt insert first", test_insert_first },
|
||||
|
@ -111,5 +138,6 @@ TEST_LIST = {
|
|||
{ "bt insert multiple", test_insert_multiple },
|
||||
{ "bt remove root", test_remove_root },
|
||||
{ "bt remove multiple", test_remove_multiple },
|
||||
{ "bt iter", test_iter },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
|
|
@ -25,8 +25,64 @@ void test_insert_one() {
|
|||
TEST_CHECK(data == (void *)1);
|
||||
}
|
||||
|
||||
void test_iter() {
|
||||
TRIE_INIT();
|
||||
|
||||
lsm_str *s;
|
||||
|
||||
lsm_str_init_copy(&s, "hello");
|
||||
TEST_CHECK(lsm_trie_insert(trie, s, (void *)1) == lsm_error_ok);
|
||||
|
||||
lsm_str_init_copy(&s, "hella");
|
||||
TEST_CHECK(lsm_trie_insert(trie, s, (void *)2) == lsm_error_ok);
|
||||
|
||||
lsm_str_init_copy(&s, "hel");
|
||||
TEST_CHECK(lsm_trie_insert(trie, s, (void *)3) == lsm_error_ok);
|
||||
|
||||
lsm_str_init_copy(&s, "wow");
|
||||
TEST_CHECK(lsm_trie_insert(trie, s, (void *)4) == lsm_error_ok);
|
||||
|
||||
lsm_str_init_copy(&s, "hel");
|
||||
|
||||
lsm_trie_iterator *iter;
|
||||
TEST_CHECK(lsm_trie_iter(&iter, trie, s) == lsm_error_ok);
|
||||
|
||||
void *data;
|
||||
|
||||
TEST_CHECK(lsm_trie_iter_next(&data, iter) == lsm_error_ok);
|
||||
TEST_CHECK_(data == (void *)3, "%p == %p", data, (void *)3);
|
||||
|
||||
TEST_CHECK(lsm_trie_iter_next(&data, iter) == lsm_error_ok);
|
||||
TEST_CHECK_(data == (void *)2, "%p == %p", data, (void *)2);
|
||||
|
||||
TEST_CHECK(lsm_trie_iter_next(&data, iter) == lsm_error_ok);
|
||||
TEST_CHECK_(data == (void *)1, "%p == %p", data, (void *)1);
|
||||
|
||||
TEST_CHECK(lsm_trie_iter_next(&data, iter) == lsm_error_done);
|
||||
lsm_trie_iter_free(iter);
|
||||
|
||||
// Test full trie iterator
|
||||
TEST_CHECK(lsm_trie_iter(&iter, trie, NULL) == lsm_error_ok);
|
||||
|
||||
TEST_CHECK(lsm_trie_iter_next(&data, iter) == lsm_error_ok);
|
||||
TEST_CHECK_(data == (void *)3, "%p == %p", data, (void *)3);
|
||||
|
||||
TEST_CHECK(lsm_trie_iter_next(&data, iter) == lsm_error_ok);
|
||||
TEST_CHECK_(data == (void *)2, "%p == %p", data, (void *)2);
|
||||
|
||||
TEST_CHECK(lsm_trie_iter_next(&data, iter) == lsm_error_ok);
|
||||
TEST_CHECK_(data == (void *)1, "%p == %p", data, (void *)1);
|
||||
|
||||
TEST_CHECK(lsm_trie_iter_next(&data, iter) == lsm_error_ok);
|
||||
TEST_CHECK_(data == (void *)4, "%p == %p", data, (void *)4);
|
||||
|
||||
TEST_CHECK(lsm_trie_iter_next(&data, iter) == lsm_error_done);
|
||||
lsm_trie_iter_free(iter);
|
||||
}
|
||||
|
||||
TEST_LIST = {
|
||||
{ "trie init", test_init },
|
||||
{ "trie insert one", test_insert_one },
|
||||
{ "trie iter", test_iter },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
|
|
@ -9,14 +9,19 @@ SRCS != find '$(SRC_DIR)' -iname '*.c'
|
|||
SRCS_H != find include -iname '*.h'
|
||||
SRCS_H_INTERNAL != find $(SRC_DIR) -iname '*.h'
|
||||
SRCS_TEST != find '$(TEST_DIR)' -iname '*.c'
|
||||
SRCS_EXAMPLE != find '$(EXAMPLE_DIR)' -iname '*.c'
|
||||
|
||||
OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
|
||||
OBJS_TEST := $(SRCS_TEST:%=$(BUILD_DIR)/%.o)
|
||||
OBJS_EXAMPLE := $(SRCS_EXAMPLE:%=$(BUILD_DIR)/%.o)
|
||||
DEPS := $(SRCS:%=$(BUILD_DIR)/%.d) $(SRCS_TEST:%=$(BUILD_DIR)/%.d)
|
||||
|
||||
BINS_TEST := $(OBJS_TEST:%.c.o=%)
|
||||
BINS_EXAMPLE := $(OBJS_EXAMPLE:%.c.o=%)
|
||||
|
||||
TARGETS_TEST := $(BINS_TEST:%=test-%)
|
||||
TARGETS_MEM_TEST := $(BINS_TEST:%=test-mem-%)
|
||||
TARGETS_EXAMPLE := $(BINS_EXAMPLE:%=test-%)
|
||||
|
||||
_CFLAGS := $(addprefix -I,$(INC_DIRS)) $(CFLAGS) -Wall -Wextra
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
# LTM - Lander Template Module
|
||||
|
||||
This module provides an interface for generating `char` streams from a given
|
||||
template.
|
||||
|
||||
```html
|
||||
<body>
|
||||
<h1>{{ title }}</h1>
|
||||
<ul>
|
||||
{{ item start }}
|
||||
<li><a href="{{ url }}">{{ name }}</a></li>
|
||||
{{ item end }}
|
||||
</ul>
|
||||
</body>
|
||||
```
|
||||
|
||||
The above snippet shows everything the templating language currently offers;
|
||||
either singular variables (here: `title`) or nested templates (here: `item`).
|
||||
Note the absence of a loop construct. This is because each variable and nested
|
||||
template is implicitely already a loop; a variable or a nested template can be
|
||||
added as many times as needed, and they will appear in the order they were
|
||||
added.
|
||||
|
||||
Template strings are compiled into templates. From a template, an instance can
|
||||
be created to which the required data can be added to fill in the template.
|
||||
Instantiating a template does not modify the original template, meaning
|
||||
templates can be safely used concurrently.
|
||||
|
||||
Each inserted variable can get its data from a variety of sources, namely
|
||||
`char` buffers, files or a custom reader function. The resulting `char` stream
|
||||
is never fully loaded into memory; an instance can stream its content to a
|
||||
provided buffer to prevent unnecessary memory usage.
|
|
@ -1,9 +1,10 @@
|
|||
LIB_FILENAME = liblnm.a
|
||||
LIB_FILENAME = libltm.a
|
||||
|
||||
BUILD_DIR = build
|
||||
SRC_DIR = src
|
||||
TEST_DIR = test
|
||||
THIRDPARTY = src/picohttpparser.c include/picohttpparser.h
|
||||
EXAMPLE_DIR = example
|
||||
THIRDPARTY =
|
||||
|
||||
PUB_INC_DIR = include
|
||||
INC_DIRS = $(PUB_INC_DIR) src/_include
|
||||
|
@ -13,4 +14,4 @@ INC_DIRS = $(PUB_INC_DIR) src/_include
|
|||
# object file is also recompiled if only a header is changed.
|
||||
# -MP: generate a dummy target for every header file (according to the docs it
|
||||
# prevents some errors when removing header files)
|
||||
CFLAGS ?= -MMD -MP -g
|
||||
CFLAGS ?= -MMD -MP -g -Wall -Werror
|
|
@ -0,0 +1,48 @@
|
|||
#include <stdio.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "ltm/template.h"
|
||||
|
||||
const char *s = "<head><link rel=\"stylesheet\" href=\"https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/default.min.css\">\n"
|
||||
"<script src=\"https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js\"></script>\n"
|
||||
"<script>hljs.highlightAll();</script></head>\n"
|
||||
"<body><pre><code>{{ paste }}</code></pre></body>";
|
||||
|
||||
ltm_err reader(size_t *written, char *buf, size_t len, void *data) {
|
||||
FILE *f = data;
|
||||
|
||||
*written = fread(buf, 1, len, f);
|
||||
|
||||
return ltm_err_ok;
|
||||
}
|
||||
|
||||
int main() {
|
||||
ltm_template *template;
|
||||
ltm_template_compile(&template, s);
|
||||
ltm_instance *instance;
|
||||
ltm_template_instantiate(&instance, template);
|
||||
|
||||
const char *filename = "src/ltm_instance.c";
|
||||
|
||||
struct stat sb;
|
||||
stat(filename, &sb);
|
||||
|
||||
FILE *f = fopen(filename, "rb");
|
||||
|
||||
ltm_instance_block_add_var_fn(instance, "paste", reader, f, sb.st_size);
|
||||
/* ltm_instance_block_add_var(instance, "paste", ltm_instance_block_type_file_owned, f, sb.st_size); */
|
||||
/* ltm_instance_block_add_var(instance, "paste", ltm_instance_block_type_buf, "hello\n", 6); */
|
||||
/* ltm_instance_block_add_var(instance, "paste", ltm_instance_block_type_buf, "world\n", 6); */
|
||||
|
||||
char buf[128];
|
||||
size_t written = 0;
|
||||
|
||||
while (ltm_instance_write(&written, buf, 128, instance) != ltm_err_done) {
|
||||
printf("%.*s", (int)written, buf);
|
||||
}
|
||||
|
||||
printf("%.*s", (int)written, buf);
|
||||
|
||||
ltm_instance_free(instance);
|
||||
ltm_template_free(template);
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
#ifndef LTM_COMMON
|
||||
#define LTM_COMMON
|
||||
|
||||
#define LTM_RES(x) \
|
||||
{ \
|
||||
ltm_err res = x; \
|
||||
if (res != ltm_err_ok) \
|
||||
return res; \
|
||||
}
|
||||
|
||||
#define LTM_RES2(x, e) \
|
||||
{ \
|
||||
ltm_err res = x; \
|
||||
if (res != ltm_err_ok) { \
|
||||
e; \
|
||||
return res; \
|
||||
} \
|
||||
}
|
||||
|
||||
#define LTM_MIN(x, y) ((x) < (y) ? (x) : (y))
|
||||
#define LTM_MAX(x, y) ((x) > (y) ? (x) : (y))
|
||||
|
||||
typedef enum ltm_err {
|
||||
ltm_err_ok = 0,
|
||||
ltm_err_invalid_template,
|
||||
ltm_err_failed_alloc,
|
||||
ltm_err_failed_io,
|
||||
ltm_err_not_found,
|
||||
ltm_err_wrong_block_type,
|
||||
ltm_err_done,
|
||||
} ltm_err;
|
||||
|
||||
#endif
|
|
@ -0,0 +1,104 @@
|
|||
#ifndef LTM_TEMPLATE
|
||||
#define LTM_TEMPLATE
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "ltm/common.h"
|
||||
|
||||
/**
|
||||
* Represents a compiled template
|
||||
*/
|
||||
typedef struct ltm_template ltm_template;
|
||||
|
||||
/**
|
||||
* Compile the given template.
|
||||
*
|
||||
* @param out where to store pointer to newly allocated `ltm_template`
|
||||
* @param template nul-terminated string containing the template
|
||||
*/
|
||||
ltm_err ltm_template_compile(ltm_template **out, const char *template);
|
||||
|
||||
/**
|
||||
* Compile the given template with a given length.
|
||||
*
|
||||
* @param out where to store pointer to newly allocated `ltm_template`
|
||||
* @param template char buffer containing the template
|
||||
* @param len length of the char buffer
|
||||
*/
|
||||
ltm_err ltm_template_compile_n(ltm_template **out, const char *template,
|
||||
size_t len);
|
||||
|
||||
/**
|
||||
* Free the template instance. After freeing a template, no instances associated
|
||||
* with it are safe to use.
|
||||
*/
|
||||
void ltm_template_free(ltm_template *template);
|
||||
|
||||
/**
|
||||
* Represents a specific instance of a template.
|
||||
*/
|
||||
typedef struct ltm_instance ltm_instance;
|
||||
|
||||
typedef ltm_err (*ltm_data_fn)(size_t *written, char *buf, size_t len,
|
||||
void *data);
|
||||
|
||||
/**
|
||||
* Create a new instance of the given template.
|
||||
*/
|
||||
ltm_err ltm_template_instantiate(ltm_instance **out,
|
||||
const ltm_template *template);
|
||||
|
||||
/**
|
||||
* Free the given instance, as well as all nested instances.
|
||||
*/
|
||||
void ltm_instance_free(ltm_instance *instance);
|
||||
|
||||
typedef enum ltm_instance_block_type {
|
||||
ltm_instance_block_type_buf = 0,
|
||||
ltm_instance_block_type_buf_owned,
|
||||
ltm_instance_block_type_file,
|
||||
ltm_instance_block_type_file_owned,
|
||||
ltm_instance_block_type_nested,
|
||||
ltm_instance_block_type_fn,
|
||||
} ltm_instance_block_type;
|
||||
|
||||
/**
|
||||
* Add a new variable to the template.
|
||||
*/
|
||||
ltm_err ltm_instance_block_add_var(ltm_instance *instance, const char *name,
|
||||
ltm_instance_block_type type, void *data,
|
||||
size_t len);
|
||||
|
||||
/**
|
||||
* Add a new variable to the template whose data is provided by a data function.
|
||||
*/
|
||||
ltm_err ltm_instance_block_add_var_fn(ltm_instance *instance, const char *name,
|
||||
ltm_data_fn fn, void *data, size_t len);
|
||||
|
||||
/**
|
||||
* Add a new nested instance to the instance, returning a handle to the nested
|
||||
* instance.
|
||||
*/
|
||||
ltm_err ltm_instance_block_add_nested(ltm_instance **out,
|
||||
ltm_instance *instance, const char *name);
|
||||
|
||||
/**
|
||||
* Calculate the size of the resulting output of this instance.
|
||||
*/
|
||||
size_t ltm_instance_size(const ltm_instance *instance);
|
||||
|
||||
/**
|
||||
* Write at most `len` bytes to the given buffer.
|
||||
*
|
||||
* @param written outputs how many bytes were written to `buf`
|
||||
* @param buf buffer to write to
|
||||
* @param len length of the buffer
|
||||
* @param instance instance to write
|
||||
* @return `ltm_err_ok` if write was successful but there's more to be written,
|
||||
* `ltm_err_done` if everything has been written successfully, or some other
|
||||
* error code
|
||||
*/
|
||||
ltm_err ltm_instance_write(size_t *written, char *buf, size_t len,
|
||||
ltm_instance *instance);
|
||||
|
||||
#endif
|
|
@ -0,0 +1,119 @@
|
|||
#ifndef LTM_TEMPLATE_INTERNAL
|
||||
#define LTM_TEMPLATE_INTERNAL
|
||||
|
||||
#include "ltm/template.h"
|
||||
|
||||
typedef enum ltm_placeholder_type {
|
||||
ltm_placeholder_type_invalid = 0,
|
||||
ltm_placeholder_type_var,
|
||||
ltm_placeholder_type_nested_start,
|
||||
ltm_placeholder_type_nested_end,
|
||||
} ltm_placeholder_type;
|
||||
|
||||
/**
|
||||
* Internal struct used when compiling templates
|
||||
*/
|
||||
typedef struct ltm_placeholder {
|
||||
const char *start;
|
||||
const char *end;
|
||||
struct {
|
||||
const char *s;
|
||||
size_t len;
|
||||
} name;
|
||||
ltm_placeholder_type type;
|
||||
} ltm_placeholder;
|
||||
|
||||
/**
|
||||
* The type of a template block
|
||||
*/
|
||||
typedef enum ltm_template_block_type {
|
||||
ltm_template_block_type_literal = 0,
|
||||
ltm_template_block_type_var,
|
||||
ltm_template_block_type_nested,
|
||||
} ltm_template_block_type;
|
||||
|
||||
/**
|
||||
* A block in a template.
|
||||
*/
|
||||
typedef struct ltm_template_block {
|
||||
ltm_template_block_type type;
|
||||
struct {
|
||||
void *ptr;
|
||||
size_t len;
|
||||
} data;
|
||||
} ltm_template_block;
|
||||
|
||||
typedef struct ltm_template_block_name {
|
||||
struct {
|
||||
const char *s;
|
||||
size_t len;
|
||||
} name;
|
||||
size_t index;
|
||||
} ltm_template_block_name;
|
||||
|
||||
/**
|
||||
* Represents a compiled template. A template consists of a list of blocks, of
|
||||
* which some have names that can be indexed (e.g. variables, nested templates).
|
||||
*/
|
||||
struct ltm_template {
|
||||
struct {
|
||||
ltm_template_block *arr;
|
||||
size_t len;
|
||||
} blocks;
|
||||
struct {
|
||||
ltm_template_block_name *arr;
|
||||
size_t len;
|
||||
} names;
|
||||
};
|
||||
|
||||
/**
|
||||
* Allocate a new `ltm_template`.
|
||||
*/
|
||||
ltm_err ltm_template_init(ltm_template **out);
|
||||
|
||||
/**
|
||||
* Append a name to the template's list.
|
||||
*/
|
||||
ltm_err ltm_template_name_append(ltm_template *template, const char *s,
|
||||
size_t len, size_t index);
|
||||
|
||||
/**
|
||||
* Append a block to the template's list.
|
||||
*/
|
||||
ltm_err ltm_template_block_append(ltm_template *template,
|
||||
ltm_template_block_type type, void *data,
|
||||
size_t len);
|
||||
|
||||
typedef struct ltm_instance_block {
|
||||
ltm_instance_block_type type;
|
||||
ltm_data_fn fn;
|
||||
struct {
|
||||
void *ptr;
|
||||
size_t len;
|
||||
} data;
|
||||
struct ltm_instance_block *next;
|
||||
} ltm_instance_block;
|
||||
|
||||
ltm_err ltm_instance_block_init(ltm_instance_block **out);
|
||||
|
||||
/**
|
||||
* Add a new block to the instance with the given type.
|
||||
*/
|
||||
ltm_err ltm_instance_block_add(ltm_instance_block **out, ltm_instance *instance,
|
||||
const char *name, ltm_instance_block_type type);
|
||||
|
||||
struct ltm_instance {
|
||||
const ltm_template *template;
|
||||
struct {
|
||||
ltm_instance_block *head;
|
||||
ltm_instance_block *tail;
|
||||
ltm_instance_block *current;
|
||||
} blocks;
|
||||
ltm_instance_block ***vars;
|
||||
// How many bytes of the current block have been written
|
||||
size_t written;
|
||||
};
|
||||
|
||||
ltm_err ltm_instance_init(ltm_instance **out);
|
||||
|
||||
#endif
|
|
@ -0,0 +1,350 @@
|
|||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "ltm/common.h"
|
||||
#include "ltm/template.h"
|
||||
#include "ltm/template_internal.h"
|
||||
|
||||
ltm_err ltm_instance_block_init(ltm_instance_block **out) {
|
||||
ltm_instance_block *block = calloc(1, sizeof(ltm_instance_block));
|
||||
|
||||
if (block == NULL) {
|
||||
return ltm_err_failed_alloc;
|
||||
}
|
||||
|
||||
*out = block;
|
||||
|
||||
return ltm_err_ok;
|
||||
}
|
||||
|
||||
ltm_err ltm_instance_init(ltm_instance **out) {
|
||||
ltm_instance *instance = calloc(1, sizeof(ltm_instance));
|
||||
|
||||
if (instance == NULL) {
|
||||
return ltm_err_failed_alloc;
|
||||
}
|
||||
|
||||
*out = instance;
|
||||
|
||||
return ltm_err_ok;
|
||||
}
|
||||
|
||||
void ltm_instance_free(ltm_instance *instance) {
|
||||
if (instance->vars != NULL) {
|
||||
free(instance->vars);
|
||||
}
|
||||
|
||||
ltm_instance_block *block = instance->blocks.head;
|
||||
|
||||
while (block != NULL) {
|
||||
switch (block->type) {
|
||||
case ltm_instance_block_type_buf_owned:
|
||||
free(block->data.ptr);
|
||||
break;
|
||||
case ltm_instance_block_type_file_owned:
|
||||
fclose(block->data.ptr);
|
||||
break;
|
||||
case ltm_instance_block_type_nested:
|
||||
ltm_instance_free(block->data.ptr);
|
||||
break;
|
||||
case ltm_instance_block_type_file:
|
||||
case ltm_instance_block_type_fn:
|
||||
case ltm_instance_block_type_buf:;
|
||||
}
|
||||
|
||||
ltm_instance_block *temp = block->next;
|
||||
free(block);
|
||||
block = temp;
|
||||
}
|
||||
|
||||
free(instance);
|
||||
}
|
||||
|
||||
ltm_err ltm_template_instantiate(ltm_instance **out,
|
||||
const ltm_template *template) {
|
||||
ltm_instance *instance;
|
||||
LTM_RES(ltm_instance_init(&instance));
|
||||
|
||||
instance->template = template;
|
||||
|
||||
ltm_instance_block ***vars = NULL;
|
||||
|
||||
if (template->names.len > 0) {
|
||||
vars = malloc(template->names.len * sizeof(ltm_instance_block **));
|
||||
|
||||
if (vars == NULL) {
|
||||
ltm_instance_free(instance);
|
||||
|
||||
return ltm_err_failed_alloc;
|
||||
}
|
||||
|
||||
instance->vars = vars;
|
||||
}
|
||||
|
||||
for (size_t block_index = 0; block_index < template->blocks.len;
|
||||
block_index++) {
|
||||
ltm_template_block *template_block = &template->blocks.arr[block_index];
|
||||
|
||||
switch (template_block->type) {
|
||||
case ltm_template_block_type_literal: {
|
||||
ltm_instance_block *block;
|
||||
LTM_RES2(ltm_instance_block_init(&block), ltm_instance_free(instance));
|
||||
|
||||
block->type = ltm_instance_block_type_buf;
|
||||
block->data.ptr = template_block->data.ptr;
|
||||
block->data.len = template_block->data.len;
|
||||
|
||||
if (instance->blocks.head == NULL) {
|
||||
instance->blocks.head = block;
|
||||
instance->blocks.tail = block;
|
||||
instance->blocks.current = block;
|
||||
} else {
|
||||
instance->blocks.tail->next = block;
|
||||
instance->blocks.tail = block;
|
||||
}
|
||||
} break;
|
||||
case ltm_template_block_type_var:
|
||||
case ltm_template_block_type_nested:
|
||||
// Account for the possibility a template starts with a placeholder
|
||||
*vars = instance->blocks.tail != NULL ? &instance->blocks.tail->next
|
||||
: &instance->blocks.head;
|
||||
vars++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
*out = instance;
|
||||
|
||||
return ltm_err_ok;
|
||||
}
|
||||
|
||||
ltm_err ltm_instance_block_add(ltm_instance_block **out, ltm_instance *instance,
|
||||
const char *name, ltm_instance_block_type type) {
|
||||
const ltm_template *template = instance->template;
|
||||
|
||||
ltm_template_block_name *block_name = NULL;
|
||||
size_t i = 0;
|
||||
|
||||
while (i < template->names.len) {
|
||||
block_name = &template->names.arr[i];
|
||||
|
||||
if (strncmp(name, block_name->name.s, block_name->name.len) == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
if (i == template->names.len) {
|
||||
return ltm_err_not_found;
|
||||
}
|
||||
|
||||
ltm_template_block *template_block = &template->blocks.arr[block_name->index];
|
||||
bool correct_type = false;
|
||||
|
||||
switch (type) {
|
||||
case ltm_instance_block_type_nested:
|
||||
correct_type = template_block->type == ltm_template_block_type_nested;
|
||||
break;
|
||||
case ltm_instance_block_type_buf:
|
||||
case ltm_instance_block_type_buf_owned:
|
||||
case ltm_instance_block_type_file:
|
||||
case ltm_instance_block_type_file_owned:
|
||||
case ltm_instance_block_type_fn:
|
||||
correct_type = template_block->type == ltm_template_block_type_var;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!correct_type) {
|
||||
return ltm_err_wrong_block_type;
|
||||
}
|
||||
|
||||
ltm_instance_block *block;
|
||||
LTM_RES(ltm_instance_block_init(&block));
|
||||
|
||||
block->type = type;
|
||||
|
||||
// Insert the block in the linked list
|
||||
block->next = *instance->vars[i];
|
||||
*instance->vars[i] = block;
|
||||
instance->vars[i] = &block->next;
|
||||
|
||||
*out = block;
|
||||
|
||||
return ltm_err_ok;
|
||||
}
|
||||
|
||||
ltm_err ltm_instance_block_add_var(ltm_instance *instance, const char *name,
|
||||
ltm_instance_block_type type, void *data,
|
||||
size_t len) {
|
||||
ltm_instance_block *block;
|
||||
LTM_RES(ltm_instance_block_add(&block, instance, name, type));
|
||||
|
||||
block->data.ptr = data;
|
||||
block->data.len = len;
|
||||
|
||||
return ltm_err_ok;
|
||||
}
|
||||
|
||||
ltm_err ltm_instance_block_add_var_fn(ltm_instance *instance, const char *name,
|
||||
ltm_data_fn fn, void *data, size_t len) {
|
||||
ltm_instance_block *block;
|
||||
LTM_RES(ltm_instance_block_add(&block, instance, name,
|
||||
ltm_instance_block_type_fn));
|
||||
|
||||
block->fn = fn;
|
||||
block->data.ptr = data;
|
||||
block->data.len = len;
|
||||
|
||||
return ltm_err_ok;
|
||||
}
|
||||
|
||||
ltm_err ltm_instance_block_add_nested(ltm_instance **out,
|
||||
ltm_instance *instance,
|
||||
const char *name) {
|
||||
const ltm_template *template = instance->template;
|
||||
|
||||
ltm_template_block_name *block_name = NULL;
|
||||
size_t i = 0;
|
||||
|
||||
while (i < template->names.len) {
|
||||
block_name = &template->names.arr[i];
|
||||
|
||||
if (strncmp(name, block_name->name.s, block_name->name.len) == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
if (i == template->names.len) {
|
||||
return ltm_err_not_found;
|
||||
}
|
||||
|
||||
ltm_template_block *template_block = &template->blocks.arr[block_name->index];
|
||||
|
||||
if (template_block->type != ltm_template_block_type_nested) {
|
||||
return ltm_err_wrong_block_type;
|
||||
}
|
||||
|
||||
ltm_instance_block *block;
|
||||
LTM_RES(ltm_instance_block_init(&block));
|
||||
|
||||
ltm_instance *nested;
|
||||
LTM_RES2(ltm_template_instantiate(&nested, template_block->data.ptr),
|
||||
free(block));
|
||||
|
||||
block->type = ltm_instance_block_type_nested;
|
||||
block->data.ptr = nested;
|
||||
|
||||
// We insert the block in the linked list and replace its next pointer as the
|
||||
// new attachment point for this variable
|
||||
block->next = *instance->vars[i];
|
||||
*instance->vars[i] = block;
|
||||
instance->vars[i] = &block->next;
|
||||
|
||||
*out = nested;
|
||||
|
||||
return ltm_err_ok;
|
||||
}
|
||||
|
||||
size_t ltm_instance_size(const ltm_instance *instance) {
|
||||
size_t total = 0;
|
||||
ltm_instance_block *block = instance->blocks.head;
|
||||
|
||||
while (block != NULL) {
|
||||
switch (block->type) {
|
||||
case ltm_instance_block_type_buf:
|
||||
case ltm_instance_block_type_buf_owned:
|
||||
case ltm_instance_block_type_file:
|
||||
case ltm_instance_block_type_file_owned:
|
||||
case ltm_instance_block_type_fn:
|
||||
total += block->data.len;
|
||||
break;
|
||||
case ltm_instance_block_type_nested:
|
||||
total += ltm_instance_size(block->data.ptr);
|
||||
break;
|
||||
}
|
||||
|
||||
block = block->next;
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
ltm_err ltm_instance_write(size_t *written, char *buf, size_t len,
|
||||
ltm_instance *instance) {
|
||||
*written = 0;
|
||||
|
||||
while ((*written < len) && (instance->blocks.current != NULL)) {
|
||||
ltm_instance_block *current = instance->blocks.current;
|
||||
|
||||
switch (current->type) {
|
||||
case ltm_instance_block_type_buf:
|
||||
case ltm_instance_block_type_buf_owned: {
|
||||
size_t cap =
|
||||
LTM_MIN(current->data.len - instance->written, len - *written);
|
||||
memcpy(&buf[*written], &((char *)current->data.ptr)[instance->written],
|
||||
cap);
|
||||
*written += cap;
|
||||
instance->written += cap;
|
||||
|
||||
if (instance->written == current->data.len) {
|
||||
instance->blocks.current = current->next;
|
||||
instance->written = 0;
|
||||
}
|
||||
} break;
|
||||
case ltm_instance_block_type_file:
|
||||
case ltm_instance_block_type_file_owned: {
|
||||
size_t cap =
|
||||
LTM_MIN(current->data.len - instance->written, len - *written);
|
||||
size_t read = fread(&buf[*written], 1, cap, current->data.ptr);
|
||||
|
||||
if ((read == 0) && (ferror(current->data.ptr) != 0)) {
|
||||
return ltm_err_failed_io;
|
||||
}
|
||||
|
||||
*written += read;
|
||||
instance->written += read;
|
||||
|
||||
if (instance->written == current->data.len) {
|
||||
instance->blocks.current = current->next;
|
||||
instance->written = 0;
|
||||
}
|
||||
} break;
|
||||
case ltm_instance_block_type_fn: {
|
||||
size_t cap =
|
||||
LTM_MIN(current->data.len - instance->written, len - *written);
|
||||
size_t fn_written = 0;
|
||||
LTM_RES(current->fn(&fn_written, &buf[*written], cap, current->data.ptr));
|
||||
*written += fn_written;
|
||||
instance->written += fn_written;
|
||||
|
||||
if (instance->written == current->data.len) {
|
||||
instance->blocks.current = current->next;
|
||||
instance->written = 0;
|
||||
}
|
||||
} break;
|
||||
case ltm_instance_block_type_nested: {
|
||||
size_t nested_written = 0;
|
||||
ltm_err res = ltm_instance_write(&nested_written, &buf[*written],
|
||||
len - *written, current->data.ptr);
|
||||
*written += nested_written;
|
||||
|
||||
switch (res) {
|
||||
case ltm_err_done:
|
||||
instance->blocks.current = current->next;
|
||||
instance->written = 0;
|
||||
break;
|
||||
case ltm_err_ok:
|
||||
break;
|
||||
default:
|
||||
return res;
|
||||
}
|
||||
} break;
|
||||
}
|
||||
}
|
||||
|
||||
return instance->blocks.current == NULL ? ltm_err_done : ltm_err_ok;
|
||||
}
|
|
@ -0,0 +1,93 @@
|
|||
#include "ltm/template.h"
|
||||
#include "ltm/template_internal.h"
|
||||
|
||||
ltm_err ltm_template_init(ltm_template **out) {
|
||||
ltm_template *template = calloc(1, sizeof(ltm_template));
|
||||
|
||||
if (template == NULL) {
|
||||
return ltm_err_failed_alloc;
|
||||
}
|
||||
|
||||
*out = template;
|
||||
|
||||
return ltm_err_ok;
|
||||
}
|
||||
|
||||
void ltm_template_free(ltm_template *template) {
|
||||
if (template->blocks.len > 0) {
|
||||
for (size_t i = 0; i < template->blocks.len; i++) {
|
||||
ltm_template_block *block = &template->blocks.arr[i];
|
||||
|
||||
switch (block->type) {
|
||||
case ltm_template_block_type_nested:
|
||||
ltm_template_free(block->data.ptr);
|
||||
break;
|
||||
case ltm_template_block_type_var:
|
||||
case ltm_template_block_type_literal:;
|
||||
}
|
||||
}
|
||||
|
||||
free(template->blocks.arr);
|
||||
}
|
||||
|
||||
if (template->names.len > 0) {
|
||||
free(template->names.arr);
|
||||
}
|
||||
|
||||
free(template);
|
||||
}
|
||||
|
||||
ltm_err ltm_template_name_append(ltm_template *template, const char *s,
|
||||
size_t len, size_t index) {
|
||||
ltm_template_block_name *names =
|
||||
realloc(template->names.arr,
|
||||
(template->names.len + 1) * sizeof(ltm_template_block_name));
|
||||
|
||||
if (names == NULL) {
|
||||
return ltm_err_failed_alloc;
|
||||
}
|
||||
|
||||
ltm_template_block_name *name = &names[template->names.len];
|
||||
|
||||
name->name.s = s;
|
||||
name->name.len = len;
|
||||
name->index = index;
|
||||
|
||||
template->names.arr = names;
|
||||
template->names.len++;
|
||||
|
||||
return ltm_err_ok;
|
||||
}
|
||||
|
||||
ltm_err ltm_template_block_append(ltm_template *template,
|
||||
ltm_template_block_type type, void *data,
|
||||
size_t len) {
|
||||
ltm_template_block *blocks =
|
||||
realloc(template->blocks.arr,
|
||||
(template->blocks.len + 1) * sizeof(ltm_template_block));
|
||||
|
||||
if (blocks == NULL) {
|
||||
return ltm_err_failed_alloc;
|
||||
}
|
||||
|
||||
ltm_template_block *block = &blocks[template->blocks.len];
|
||||
|
||||
block->type = type;
|
||||
|
||||
switch (type) {
|
||||
case ltm_template_block_type_literal:
|
||||
block->data.ptr = data;
|
||||
block->data.len = len;
|
||||
break;
|
||||
case ltm_template_block_type_nested:
|
||||
block->data.ptr = data;
|
||||
break;
|
||||
// For the other cases, we explicitely ignore the data and len arguments
|
||||
default:;
|
||||
}
|
||||
|
||||
template->blocks.arr = blocks;
|
||||
template->blocks.len++;
|
||||
|
||||
return ltm_err_ok;
|
||||
}
|
|
@ -0,0 +1,198 @@
|
|||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "ltm/common.h"
|
||||
#include "ltm/template.h"
|
||||
#include "ltm/template_internal.h"
|
||||
|
||||
static bool ltm_template_next_placeholder(ltm_placeholder *ph, const char *s,
|
||||
size_t len) {
|
||||
if (len == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ph->start = memchr(s, '{', len - 1);
|
||||
|
||||
if ((ph->start == NULL) || (ph->start[1] != '{')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t new_len = len - (ph->start - s);
|
||||
|
||||
// A template can never be valid without at least 5 characters
|
||||
if (new_len < 5) {
|
||||
ph->type = ltm_placeholder_type_invalid;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
ph->end = memchr(ph->start + 2, '}', new_len - 3);
|
||||
|
||||
// Non-terminated placeholders aren't valid
|
||||
if ((ph->end == NULL) || (ph->end[1] != '}')) {
|
||||
ph->type = ltm_placeholder_type_invalid;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// End should point to final character
|
||||
ph->end++;
|
||||
|
||||
// Parse the words
|
||||
ph->name.s = ph->start + 2;
|
||||
|
||||
while ((*ph->name.s == ' ') && (ph->name.s != ph->end - 1)) {
|
||||
ph->name.s++;
|
||||
}
|
||||
|
||||
// Placeholder is empty
|
||||
if (ph->name.s == ph->end - 1) {
|
||||
ph->type = ltm_placeholder_type_invalid;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
const char *ident = ph->name.s;
|
||||
|
||||
while ((*ident != ' ') && (ident != ph->end - 1)) {
|
||||
ident++;
|
||||
}
|
||||
|
||||
ph->name.len = ident - ph->name.s;
|
||||
|
||||
// Skip whitespace over to next word
|
||||
while ((*ident == ' ') && (ident != ph->end - 1)) {
|
||||
ident++;
|
||||
}
|
||||
|
||||
if (ident == ph->end - 1) {
|
||||
ph->type = ltm_placeholder_type_var;
|
||||
} else {
|
||||
// Further parse the identifier
|
||||
const char *temp = ident;
|
||||
|
||||
while ((*temp != ' ') && (temp != ph->end - 1)) {
|
||||
temp++;
|
||||
}
|
||||
|
||||
size_t ident_len = temp - ident;
|
||||
|
||||
if (strncmp("start", ident, ident_len) == 0) {
|
||||
ph->type = ltm_placeholder_type_nested_start;
|
||||
} else if (strncmp("end", ident, ident_len) == 0) {
|
||||
ph->type = ltm_placeholder_type_nested_end;
|
||||
} else {
|
||||
ph->type = ltm_placeholder_type_invalid;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
ltm_err ltm_template_compile_n(ltm_template **out, const char *s, size_t len) {
|
||||
ltm_template *template;
|
||||
LTM_RES(ltm_template_init(&template));
|
||||
|
||||
ltm_placeholder ph;
|
||||
bool in_nested = false;
|
||||
const char *nested_start = NULL, *nested_name = NULL;
|
||||
size_t nested_depth = 0, nested_name_len = 0;
|
||||
|
||||
size_t cur_nested_depth = 0;
|
||||
|
||||
while (ltm_template_next_placeholder(&ph, s, len)) {
|
||||
// Add part before placeholder as literal
|
||||
if (!in_nested && (ph.start != s)) {
|
||||
LTM_RES2(ltm_template_block_append(template,
|
||||
ltm_template_block_type_literal,
|
||||
(void *)s, ph.start - s),
|
||||
ltm_template_free(template));
|
||||
}
|
||||
|
||||
switch (ph.type) {
|
||||
// Invalid placeholders can be detected as early as possible
|
||||
case ltm_placeholder_type_invalid:
|
||||
ltm_template_free(template);
|
||||
|
||||
return ltm_err_invalid_template;
|
||||
case ltm_placeholder_type_var:
|
||||
if (!in_nested) {
|
||||
LTM_RES2(ltm_template_block_append(
|
||||
template, ltm_template_block_type_var, NULL, 0),
|
||||
ltm_template_free(template));
|
||||
LTM_RES2(ltm_template_name_append(template, ph.name.s, ph.name.len,
|
||||
template->blocks.len - 1),
|
||||
ltm_template_free(template));
|
||||
}
|
||||
break;
|
||||
case ltm_placeholder_type_nested_start:
|
||||
if (!in_nested) {
|
||||
nested_start = ph.end + 1;
|
||||
nested_depth = cur_nested_depth;
|
||||
nested_name = ph.name.s;
|
||||
nested_name_len = ph.name.len;
|
||||
in_nested = true;
|
||||
}
|
||||
cur_nested_depth++;
|
||||
break;
|
||||
case ltm_placeholder_type_nested_end:
|
||||
// This means there's more nested end placeholders than nested starts
|
||||
if (cur_nested_depth == 0) {
|
||||
ltm_template_free(template);
|
||||
|
||||
return ltm_err_invalid_template;
|
||||
}
|
||||
|
||||
cur_nested_depth--;
|
||||
|
||||
if (in_nested && (cur_nested_depth == nested_depth)) {
|
||||
size_t nested_len = ph.start - nested_start;
|
||||
ltm_template *nested_template;
|
||||
LTM_RES2(
|
||||
ltm_template_compile_n(&nested_template, nested_start, nested_len),
|
||||
ltm_template_free(template));
|
||||
LTM_RES(ltm_template_block_append(
|
||||
template, ltm_template_block_type_nested, nested_template, 0));
|
||||
LTM_RES2(ltm_template_name_append(template, nested_name,
|
||||
nested_name_len,
|
||||
template->blocks.len - 1),
|
||||
ltm_template_free(template));
|
||||
|
||||
in_nested = false;
|
||||
}
|
||||
// We encountered a nested end without a start
|
||||
else if (!in_nested) {
|
||||
ltm_template_free(template);
|
||||
|
||||
return ltm_err_invalid_template;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
len -= ph.end + 1 - s;
|
||||
s = ph.end + 1;
|
||||
}
|
||||
|
||||
// Unfinished nested
|
||||
if (in_nested) {
|
||||
ltm_template_free(template);
|
||||
|
||||
return ltm_err_invalid_template;
|
||||
}
|
||||
|
||||
// Add remaining trailing literal
|
||||
if (len > 0) {
|
||||
LTM_RES2(ltm_template_block_append(
|
||||
template, ltm_template_block_type_literal, (void *)s, len),
|
||||
ltm_template_free(template));
|
||||
}
|
||||
|
||||
*out = template;
|
||||
|
||||
return ltm_err_ok;
|
||||
}
|
||||
|
||||
ltm_err ltm_template_compile(ltm_template **out, const char *template) {
|
||||
return ltm_template_compile_n(out, template, strlen(template));
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
#include "test.h"
|
||||
|
||||
#include "ltm/common.h"
|
||||
#include "ltm/template.h"
|
||||
#include "ltm/template_internal.h"
|
||||
|
||||
void test_single_placeholder() {
|
||||
const char *s = "Hello, {{ world }}!";
|
||||
|
||||
ltm_template *template;
|
||||
TEST_ASSERT(ltm_template_compile(&template, s) == ltm_err_ok);
|
||||
|
||||
ltm_instance *instance;
|
||||
TEST_CHECK(ltm_template_instantiate(&instance, template) == ltm_err_ok);
|
||||
|
||||
TEST_CHECK(ltm_instance_block_add_var(instance, "world", ltm_instance_block_type_buf, "World", 5) == ltm_err_ok);
|
||||
|
||||
TEST_CHECK(ltm_instance_size(instance) == 13);
|
||||
|
||||
char buf[13];
|
||||
|
||||
size_t written = 0;
|
||||
TEST_CHECK(ltm_instance_write(&written, buf, 5, instance) == ltm_err_ok);
|
||||
TEST_CHECK(written == 5);
|
||||
|
||||
written = 0;
|
||||
TEST_CHECK(ltm_instance_write(&written, buf + 5, 5, instance) == ltm_err_ok);
|
||||
TEST_CHECK(written == 5);
|
||||
|
||||
written = 0;
|
||||
TEST_CHECK(ltm_instance_write(&written, buf + 10, 5, instance) == ltm_err_done);
|
||||
TEST_CHECK(written == 3);
|
||||
|
||||
TEST_CHECK(strncmp(buf, "Hello, World!", 13) == 0);
|
||||
TEST_DUMP("buf", buf, 13);
|
||||
|
||||
ltm_instance_free(instance);
|
||||
ltm_template_free(template);
|
||||
}
|
||||
|
||||
TEST_LIST = {
|
||||
{ "instance single placeholder", test_single_placeholder },
|
||||
{ NULL, NULL }
|
||||
};
|
|
@ -0,0 +1,118 @@
|
|||
#include "test.h"
|
||||
|
||||
#include "ltm/common.h"
|
||||
#include "ltm/template.h"
|
||||
#include "ltm/template_internal.h"
|
||||
|
||||
void test_single_placeholder() {
|
||||
const char *s = "Hello, {{ world }}";
|
||||
|
||||
ltm_template *template;
|
||||
TEST_CHECK(ltm_template_compile(&template, s) == ltm_err_ok);
|
||||
|
||||
TEST_CHECK(template->blocks.len == 2);
|
||||
|
||||
TEST_CHECK(template->blocks.arr[0].type == ltm_template_block_type_literal);
|
||||
TEST_CHECK(template->blocks.arr[0].data.ptr == s);
|
||||
TEST_CHECK(template->blocks.arr[0].data.len == 7);
|
||||
|
||||
TEST_CHECK(template->blocks.arr[1].type == ltm_template_block_type_var);
|
||||
|
||||
TEST_CHECK(template->names.len == 1);
|
||||
|
||||
TEST_CHECK(template->names.arr[0].name.s == s + 10);
|
||||
TEST_CHECK(template->names.arr[0].name.len == 5);
|
||||
TEST_CHECK(template->names.arr[0].index == 1);
|
||||
|
||||
ltm_template_free(template);
|
||||
}
|
||||
|
||||
void test_single_placeholder_trailing() {
|
||||
const char *s = "Hello, {{ world }}!";
|
||||
|
||||
ltm_template *template;
|
||||
TEST_CHECK(ltm_template_compile(&template, s) == ltm_err_ok);
|
||||
|
||||
TEST_CHECK(template->blocks.len == 3);
|
||||
|
||||
TEST_CHECK(template->blocks.arr[0].type == ltm_template_block_type_literal);
|
||||
TEST_CHECK(template->blocks.arr[0].data.ptr == s);
|
||||
TEST_CHECK(template->blocks.arr[0].data.len == 7);
|
||||
|
||||
TEST_CHECK(template->blocks.arr[1].type == ltm_template_block_type_var);
|
||||
|
||||
TEST_CHECK(template->blocks.arr[2].type == ltm_template_block_type_literal);
|
||||
TEST_CHECK(template->blocks.arr[2].data.ptr == s + 18);
|
||||
TEST_CHECK(template->blocks.arr[2].data.len == 1);
|
||||
|
||||
TEST_CHECK(template->names.len == 1);
|
||||
|
||||
TEST_CHECK(template->names.arr[0].name.s == s + 10);
|
||||
TEST_CHECK(template->names.arr[0].name.len == 5);
|
||||
TEST_CHECK(template->names.arr[0].index == 1);
|
||||
|
||||
ltm_template_free(template);
|
||||
}
|
||||
|
||||
void test_single_nested() {
|
||||
const char *s = "abc {{ l start }}some content {{ var }} {{ l end }}";
|
||||
|
||||
ltm_template *template;
|
||||
TEST_CHECK(ltm_template_compile(&template, s) == ltm_err_ok);
|
||||
|
||||
TEST_CHECK(template->names.len == 1);
|
||||
TEST_CHECK(template->names.arr[0].name.s == s + 7);
|
||||
TEST_CHECK(template->names.arr[0].name.len == 1);
|
||||
TEST_CHECK(template->names.arr[0].index == 1);
|
||||
|
||||
TEST_CHECK(template->blocks.len == 2);
|
||||
|
||||
TEST_CHECK(template->blocks.arr[0].type == ltm_template_block_type_literal);
|
||||
TEST_CHECK(template->blocks.arr[0].data.ptr == s);
|
||||
TEST_CHECK(template->blocks.arr[0].data.len == 4);
|
||||
|
||||
TEST_CHECK(template->blocks.arr[1].type == ltm_template_block_type_nested);
|
||||
ltm_template *nested_template = template->blocks.arr[1].data.ptr;
|
||||
|
||||
TEST_CHECK(nested_template->blocks.len == 3);
|
||||
TEST_CHECK(nested_template->blocks.arr[0].type == ltm_template_block_type_literal);
|
||||
TEST_CHECK(nested_template->blocks.arr[0].data.ptr == s + 17);
|
||||
TEST_CHECK(nested_template->blocks.arr[0].data.len == 13);
|
||||
TEST_CHECK(nested_template->blocks.arr[1].type == ltm_template_block_type_var);
|
||||
TEST_CHECK(nested_template->blocks.arr[2].type == ltm_template_block_type_literal);
|
||||
TEST_CHECK(nested_template->blocks.arr[2].data.ptr == s + 39);
|
||||
TEST_CHECK(nested_template->blocks.arr[2].data.len == 1);
|
||||
|
||||
TEST_CHECK(nested_template->names.len == 1);
|
||||
TEST_CHECK(nested_template->names.arr[0].name.s == s + 33);
|
||||
TEST_CHECK(nested_template->names.arr[0].name.len == 3);
|
||||
TEST_CHECK(nested_template->names.arr[0].index == 1);
|
||||
|
||||
ltm_template_free(template);
|
||||
}
|
||||
|
||||
void test_unclosed_placeholder() {
|
||||
ltm_template *template;
|
||||
|
||||
const char *s = "abc {{ var }";
|
||||
TEST_CHECK(ltm_template_compile(&template, s) == ltm_err_invalid_template);
|
||||
|
||||
s = "abc {{ var ";
|
||||
TEST_CHECK(ltm_template_compile(&template, s) == ltm_err_invalid_template);
|
||||
}
|
||||
|
||||
void test_unclosed_nested() {
|
||||
ltm_template *template;
|
||||
|
||||
const char *s = "abc {{ var start }} {{ hello }}";
|
||||
TEST_CHECK(ltm_template_compile(&template, s) == ltm_err_invalid_template);
|
||||
}
|
||||
|
||||
TEST_LIST = {
|
||||
{ "template single placeholder", test_single_placeholder },
|
||||
{ "template single placeholder trailing", test_single_placeholder_trailing },
|
||||
{ "template single nested", test_single_nested },
|
||||
{ "template unclosed placeholder", test_unclosed_placeholder },
|
||||
{ "template unclosed nested", test_unclosed_nested },
|
||||
{ NULL, NULL }
|
||||
};
|
File diff suppressed because it is too large
Load Diff
|
@ -3,6 +3,7 @@
|
|||
|
||||
#include "lnm/common.h"
|
||||
#include "lsm/store.h"
|
||||
#include "ltm/template.h"
|
||||
|
||||
#include "lander.h"
|
||||
|
||||
|
@ -29,6 +30,12 @@ void lander_ctx_reset(lander_ctx *ctx) {
|
|||
|
||||
ctx->entry = NULL;
|
||||
}
|
||||
|
||||
if (ctx->instance != NULL) {
|
||||
ltm_instance_free(ctx->instance);
|
||||
|
||||
ctx->instance = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void lander_ctx_free(lander_ctx *ctx) { free(ctx); }
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
#include "lnm/http/req.h"
|
||||
#include "lnm/loop.h"
|
||||
|
||||
#include "lander.h"
|
||||
|
@ -8,8 +9,8 @@ lnm_http_step_err lander_remove_entry(lnm_http_conn *conn) {
|
|||
lnm_http_loop_gctx *gctx = ctx->g;
|
||||
lander_gctx *c_gctx = gctx->c;
|
||||
|
||||
const char *key_s = &ctx->req.path.s[ctx->req.path.groups[1].rm_so];
|
||||
int key_len = ctx->req.path.groups[1].rm_eo - ctx->req.path.groups[1].rm_so;
|
||||
const char *key_s;
|
||||
size_t key_len = lnm_http_req_route_segment(&key_s, &ctx->req, "key");
|
||||
|
||||
lsm_str *key;
|
||||
lsm_str_init_copy_n(&key, (char *)key_s, key_len);
|
||||
|
|
|
@ -3,11 +3,13 @@
|
|||
|
||||
#include "lnm/http/consts.h"
|
||||
#include "lnm/http/loop.h"
|
||||
#include "lnm/http/req.h"
|
||||
#include "lnm/log.h"
|
||||
#include "lnm/loop.h"
|
||||
#include "lsm/store.h"
|
||||
|
||||
#include "lander.h"
|
||||
#include "ltm/template.h"
|
||||
|
||||
static const char index_page[] =
|
||||
"<!DOCTYPE html>\n"
|
||||
|
@ -19,7 +21,7 @@ static const char index_page[] =
|
|||
" </body>\n"
|
||||
"</html>\n";
|
||||
|
||||
lnm_http_step_err lander_get_index(lnm_http_conn *conn) {
|
||||
lnm_http_step_err lander_get_root(lnm_http_conn *conn) {
|
||||
lnm_http_loop_ctx *ctx = conn->ctx;
|
||||
|
||||
lnm_http_res_body_set_buf(&ctx->res, (char *)index_page,
|
||||
|
@ -72,13 +74,43 @@ lnm_err lander_entry_data_streamer(uint64_t *written, char *buf,
|
|||
return lnm_err_ok;
|
||||
}
|
||||
|
||||
lnm_http_step_err lander_get_paste(lnm_http_conn *conn) {
|
||||
lnm_err lander_template_streamer(size_t *written, char *buf,
|
||||
lnm_http_conn *conn, uint64_t offset,
|
||||
uint64_t len) {
|
||||
lnm_http_loop_ctx *ctx = conn->ctx;
|
||||
lander_ctx *c_ctx = ctx->c;
|
||||
|
||||
lnm_http_res_body_set_fn(&ctx->res, lander_entry_data_streamer,
|
||||
// TODO respect offset variable
|
||||
|
||||
ltm_instance_write(written, buf, len, c_ctx->instance);
|
||||
|
||||
return lnm_err_ok;
|
||||
}
|
||||
|
||||
ltm_err lander_data_to_template(size_t *written, char *buf, size_t len,
|
||||
void *data) {
|
||||
lsm_entry_handle *entry = data;
|
||||
|
||||
lsm_entry_data_read(written, buf, entry, len);
|
||||
|
||||
return ltm_err_ok;
|
||||
}
|
||||
|
||||
lnm_http_step_err lander_get_paste(lnm_http_conn *conn) {
|
||||
lnm_http_loop_ctx *ctx = conn->ctx;
|
||||
lander_gctx *c_gctx = ctx->g->c;
|
||||
lander_ctx *c_ctx = ctx->c;
|
||||
|
||||
ltm_template_instantiate(&c_ctx->instance, c_gctx->templates.paste);
|
||||
/* ltm_instance_block_add_var(c_ctx->instance, ltm_instance_block_type_buf,
|
||||
* lsm_str_ptr(c_ctx->entry)) */
|
||||
ltm_instance_block_add_var_fn(c_ctx->instance, "paste",
|
||||
lander_data_to_template, c_ctx->entry,
|
||||
lsm_entry_data_len(c_ctx->entry));
|
||||
lnm_http_res_add_header(&ctx->res, lnm_http_header_content_type, "text/plain",
|
||||
|
||||
lnm_http_res_body_set_fn(&ctx->res, lander_template_streamer,
|
||||
ltm_instance_size(c_ctx->instance));
|
||||
lnm_http_res_add_header(&ctx->res, lnm_http_header_content_type, "text/html",
|
||||
false);
|
||||
|
||||
return lnm_http_step_err_done;
|
||||
|
@ -93,21 +125,92 @@ lnm_http_step_err lander_get_file(lnm_http_conn *conn) {
|
|||
lander_attr_to_header(ctx, lander_attr_type_content_type,
|
||||
lnm_http_header_content_type);
|
||||
|
||||
lsm_str *value;
|
||||
char *buf;
|
||||
return lnm_http_step_err_done;
|
||||
}
|
||||
|
||||
if (lsm_entry_attr_get(&value, c_ctx->entry, lander_attr_type_file_name) ==
|
||||
lnm_http_step_err lander_get_index(lnm_http_conn *conn) {
|
||||
lnm_http_loop_ctx *ctx = conn->ctx;
|
||||
lander_gctx *c_gctx = ctx->g->c;
|
||||
lander_ctx *c_ctx = ctx->c;
|
||||
|
||||
ltm_template_instantiate(&c_ctx->instance, c_gctx->templates.index);
|
||||
|
||||
const lsm_str *key;
|
||||
lsm_entry_key(&key, c_ctx->entry);
|
||||
|
||||
ltm_instance_block_add_var(c_ctx->instance, "title_key",
|
||||
ltm_instance_block_type_buf,
|
||||
(void *)lsm_str_ptr(key), lsm_str_len(key));
|
||||
|
||||
lsm_str *prefix;
|
||||
lsm_str_init_copy_n(&prefix, lsm_str_ptr(key), lsm_str_len(key));
|
||||
lsm_str_append_c(prefix, "_", 1);
|
||||
|
||||
uint64_t prefix_len = lsm_str_len(prefix);
|
||||
|
||||
lsm_store_iterator *iter;
|
||||
lsm_store_iter(&iter, c_gctx->store, prefix);
|
||||
|
||||
lsm_entry_handle *handle;
|
||||
while (lsm_store_iter_next_read(&handle, iter) == lsm_error_ok) {
|
||||
const lsm_str *entry_key;
|
||||
lsm_entry_key(&entry_key, handle);
|
||||
|
||||
lander_entry_type t;
|
||||
lsm_entry_attr_get_uint8_t((uint8_t *)&t, handle,
|
||||
lander_attr_type_entry_type);
|
||||
|
||||
ltm_instance *nested;
|
||||
|
||||
switch (t) {
|
||||
case lander_entry_type_redirect:
|
||||
ltm_instance_block_add_nested(&nested, c_ctx->instance, "redirect");
|
||||
|
||||
ltm_instance_block_add_var(
|
||||
nested, "full_key", ltm_instance_block_type_buf,
|
||||
(void *)lsm_str_ptr(entry_key), lsm_str_len(entry_key));
|
||||
ltm_instance_block_add_var(nested, "short_key",
|
||||
ltm_instance_block_type_buf,
|
||||
(void *)(lsm_str_ptr(entry_key) + prefix_len),
|
||||
lsm_str_len(entry_key) - prefix_len);
|
||||
break;
|
||||
case lander_entry_type_file:
|
||||
case lander_entry_type_paste:
|
||||
ltm_instance_block_add_nested(&nested, c_ctx->instance,
|
||||
t == lander_entry_type_file ? "file"
|
||||
: "paste");
|
||||
|
||||
ltm_instance_block_add_var(
|
||||
nested, "full_key", ltm_instance_block_type_buf,
|
||||
(void *)lsm_str_ptr(entry_key), lsm_str_len(entry_key));
|
||||
|
||||
// Add the filename as the link name if known, or just the key otherwise
|
||||
lsm_str *filename;
|
||||
if (lsm_entry_attr_get(&filename, handle, lander_attr_type_file_name) ==
|
||||
lsm_error_ok) {
|
||||
buf = malloc(24 + lsm_str_len(value));
|
||||
int len = lsm_str_len(value);
|
||||
sprintf(buf, "attachment; filename=\"%.*s\"", len, lsm_str_ptr(value));
|
||||
ltm_instance_block_add_var(
|
||||
nested, "filename", ltm_instance_block_type_buf,
|
||||
(void *)lsm_str_ptr(filename), lsm_str_len(filename));
|
||||
} else {
|
||||
buf = malloc(11);
|
||||
strcpy(buf, "attachment");
|
||||
ltm_instance_block_add_var(
|
||||
nested, "filename", ltm_instance_block_type_buf,
|
||||
(void *)(lsm_str_ptr(entry_key) + prefix_len),
|
||||
lsm_str_len(entry_key) - prefix_len);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
lnm_http_res_add_header(&ctx->res, lnm_http_header_content_disposition, buf,
|
||||
true);
|
||||
// TODO this should be done after writing the template
|
||||
lsm_entry_close(handle);
|
||||
}
|
||||
|
||||
lnm_http_res_body_set_fn(&ctx->res, lander_template_streamer,
|
||||
ltm_instance_size(c_ctx->instance));
|
||||
lnm_http_res_add_header(&ctx->res, lnm_http_header_content_type, "text/html",
|
||||
false);
|
||||
|
||||
lsm_store_iter_free(iter);
|
||||
lsm_str_free(prefix);
|
||||
|
||||
return lnm_http_step_err_done;
|
||||
}
|
||||
|
@ -118,8 +221,8 @@ lnm_http_step_err lander_get_entry(lnm_http_conn *conn) {
|
|||
lnm_http_loop_gctx *gctx = ctx->g;
|
||||
lander_gctx *c_gctx = gctx->c;
|
||||
|
||||
const char *key_s = &ctx->req.path.s[ctx->req.path.groups[1].rm_so];
|
||||
int key_len = ctx->req.path.groups[1].rm_eo - ctx->req.path.groups[1].rm_so;
|
||||
const char *key_s;
|
||||
size_t key_len = lnm_http_req_route_segment(&key_s, &ctx->req, "key");
|
||||
|
||||
lsm_str *key;
|
||||
lsm_str_init_copy_n(&key, (char *)key_s, key_len);
|
||||
|
@ -153,6 +256,12 @@ lnm_http_step_err lander_get_entry(lnm_http_conn *conn) {
|
|||
case lander_entry_type_file:
|
||||
res = lander_get_file(conn);
|
||||
break;
|
||||
case lander_entry_type_index:
|
||||
res = lander_get_index(conn);
|
||||
break;
|
||||
default:
|
||||
ctx->res.status = lnm_http_status_internal_server_error;
|
||||
res = lnm_http_step_err_res;
|
||||
}
|
||||
|
||||
return res;
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#include <string.h>
|
||||
|
||||
#include "lnm/http/req.h"
|
||||
#include "lnm/loop.h"
|
||||
#include "lsm/store.h"
|
||||
|
||||
|
@ -20,27 +21,24 @@ static void randomize_key(char *key, int len) {
|
|||
*
|
||||
* @return true on success, false otherwise
|
||||
*/
|
||||
bool lander_insert_entry(lnm_http_loop_ctx *ctx) {
|
||||
bool lander_insert_entry(lnm_http_loop_ctx *ctx, bool secure) {
|
||||
lnm_http_loop_gctx *gctx = ctx->g;
|
||||
lander_gctx *c_gctx = gctx->c;
|
||||
lander_ctx *c_ctx = ctx->c;
|
||||
|
||||
const char *key_s;
|
||||
size_t key_len = lnm_http_req_route_segment(&key_s, &ctx->req, "key");
|
||||
|
||||
lsm_str *key;
|
||||
int key_len;
|
||||
|
||||
if (ctx->req.path.groups[2].rm_eo == ctx->req.path.groups[2].rm_so) {
|
||||
if (key_len == 0) {
|
||||
// Generate a random key to insert
|
||||
bool secure =
|
||||
(ctx->req.path.groups[1].rm_eo - ctx->req.path.groups[1].rm_so) == 1;
|
||||
key_len = secure ? 16 : 4;
|
||||
char *key_s = malloc((key_len + 1) * sizeof(char));
|
||||
key_s = malloc((key_len + 1) * sizeof(char));
|
||||
|
||||
randomize_key(key_s, key_len);
|
||||
lsm_str_init(&key, key_s);
|
||||
randomize_key((char *)key_s, key_len);
|
||||
lsm_str_init(&key, (char *)key_s);
|
||||
} else {
|
||||
const char *key_s = &ctx->req.path.s[ctx->req.path.groups[2].rm_so];
|
||||
key_len = ctx->req.path.groups[2].rm_eo - ctx->req.path.groups[2].rm_so;
|
||||
|
||||
lsm_str_init_copy_n(&key, key_s, key_len);
|
||||
}
|
||||
|
||||
|
@ -68,11 +66,12 @@ bool lander_insert_entry(lnm_http_loop_ctx *ctx) {
|
|||
return true;
|
||||
}
|
||||
|
||||
lnm_http_step_err lander_post_redirect(lnm_http_conn *conn) {
|
||||
static lnm_http_step_err __lander_post_redirect(lnm_http_conn *conn,
|
||||
bool secure) {
|
||||
lnm_http_loop_ctx *ctx = conn->ctx;
|
||||
lander_ctx *c_ctx = ctx->c;
|
||||
|
||||
if (!lander_insert_entry(ctx)) {
|
||||
if (!lander_insert_entry(ctx, secure)) {
|
||||
return lnm_http_step_err_res;
|
||||
}
|
||||
|
||||
|
@ -82,6 +81,14 @@ lnm_http_step_err lander_post_redirect(lnm_http_conn *conn) {
|
|||
return lnm_http_step_err_done;
|
||||
}
|
||||
|
||||
lnm_http_step_err lander_post_redirect(lnm_http_conn *conn) {
|
||||
return __lander_post_redirect(conn, false);
|
||||
}
|
||||
|
||||
lnm_http_step_err lander_post_redirect_secure(lnm_http_conn *conn) {
|
||||
return __lander_post_redirect(conn, true);
|
||||
}
|
||||
|
||||
lnm_http_step_err lander_post_redirect_body_to_attr(lnm_http_conn *conn) {
|
||||
lnm_http_loop_ctx *ctx = conn->ctx;
|
||||
lander_ctx *c_ctx = ctx->c;
|
||||
|
@ -93,11 +100,11 @@ lnm_http_step_err lander_post_redirect_body_to_attr(lnm_http_conn *conn) {
|
|||
return lnm_http_step_err_done;
|
||||
}
|
||||
|
||||
lnm_http_step_err lander_post_paste(lnm_http_conn *conn) {
|
||||
static lnm_http_step_err __lander_post_paste(lnm_http_conn *conn, bool secure) {
|
||||
lnm_http_loop_ctx *ctx = conn->ctx;
|
||||
lander_ctx *c_ctx = ctx->c;
|
||||
|
||||
if (!lander_insert_entry(ctx)) {
|
||||
if (!lander_insert_entry(ctx, secure)) {
|
||||
return lnm_http_step_err_res;
|
||||
}
|
||||
|
||||
|
@ -108,11 +115,19 @@ lnm_http_step_err lander_post_paste(lnm_http_conn *conn) {
|
|||
return lnm_http_step_err_done;
|
||||
}
|
||||
|
||||
lnm_http_step_err lander_post_file(lnm_http_conn *conn) {
|
||||
lnm_http_step_err lander_post_paste(lnm_http_conn *conn) {
|
||||
return __lander_post_paste(conn, false);
|
||||
}
|
||||
|
||||
lnm_http_step_err lander_post_paste_secure(lnm_http_conn *conn) {
|
||||
return __lander_post_paste(conn, true);
|
||||
}
|
||||
|
||||
static lnm_http_step_err __lander_post_file(lnm_http_conn *conn, bool secure) {
|
||||
lnm_http_loop_ctx *ctx = conn->ctx;
|
||||
lander_ctx *c_ctx = ctx->c;
|
||||
|
||||
if (!lander_insert_entry(ctx)) {
|
||||
if (!lander_insert_entry(ctx, secure)) {
|
||||
return lnm_http_step_err_res;
|
||||
}
|
||||
|
||||
|
@ -124,3 +139,37 @@ lnm_http_step_err lander_post_file(lnm_http_conn *conn) {
|
|||
|
||||
return lnm_http_step_err_done;
|
||||
}
|
||||
|
||||
lnm_http_step_err lander_post_file(lnm_http_conn *conn) {
|
||||
return __lander_post_file(conn, false);
|
||||
}
|
||||
|
||||
lnm_http_step_err lander_post_file_secure(lnm_http_conn *conn) {
|
||||
return __lander_post_file(conn, true);
|
||||
}
|
||||
|
||||
static lnm_http_step_err __lander_post_index(lnm_http_conn *conn, bool secure) {
|
||||
lnm_http_loop_ctx *ctx = conn->ctx;
|
||||
lander_ctx *c_ctx = ctx->c;
|
||||
|
||||
if (!lander_insert_entry(ctx, secure)) {
|
||||
return lnm_http_step_err_res;
|
||||
}
|
||||
|
||||
lsm_entry_attr_insert_uint8_t(c_ctx->entry, lander_attr_type_entry_type,
|
||||
lander_entry_type_index);
|
||||
/* lander_header_to_attr(ctx, "X-Lander-Content-Type", */
|
||||
/* lander_attr_type_content_type); */
|
||||
/* lander_header_to_attr(ctx, "X-Lander-Filename",
|
||||
* lander_attr_type_file_name); */
|
||||
|
||||
return lnm_http_step_err_done;
|
||||
}
|
||||
|
||||
lnm_http_step_err lander_post_index(lnm_http_conn *conn) {
|
||||
return __lander_post_index(conn, false);
|
||||
}
|
||||
|
||||
lnm_http_step_err lander_post_index_secure(lnm_http_conn *conn) {
|
||||
return __lander_post_index(conn, true);
|
||||
}
|
||||
|
|
154
src/main.c
154
src/main.c
|
@ -4,57 +4,129 @@
|
|||
|
||||
#include "lnm/http/loop.h"
|
||||
#include "lnm/log.h"
|
||||
#include "ltm/template.h"
|
||||
|
||||
#include "lander.h"
|
||||
#include "log.h"
|
||||
|
||||
const char *lander_server = "lander/" LANDER_VERSION;
|
||||
const char *paste_template =
|
||||
"<!doctype html>\n"
|
||||
"<head><link rel=\"stylesheet\" "
|
||||
"href=\"https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.9.0/build/"
|
||||
"styles/stackoverflow-dark.min.css\">\n"
|
||||
"<script "
|
||||
"src=\"https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/"
|
||||
"highlight.min.js\"></script>\n"
|
||||
"<script>hljs.highlightAll();</script></head>\n"
|
||||
"<body><h1>{{ header }}</h1><pre><code>{{ paste }}</code></pre></body>";
|
||||
|
||||
const char *index_template =
|
||||
"<!doctype html>\n"
|
||||
"<body>\n"
|
||||
"<h1>Index /{{ title_key }}</h1>\n"
|
||||
"<h2>Redirects</h2>\n"
|
||||
"<ul>\n"
|
||||
"{{ redirect start }}\n"
|
||||
"<li><a href=\"/{{ full_key }}\">{{ short_key }}</a></li>\n"
|
||||
"{{ redirect end }}\n"
|
||||
"</ul>\n"
|
||||
"<h2>Files</h2>"
|
||||
"<ul>\n"
|
||||
"{{ file start }}\n"
|
||||
"<li><a href=\"/{{ full_key }}\">{{ filename }}</a></li>\n"
|
||||
"{{ file end }}\n"
|
||||
"</ul>\n"
|
||||
"<h2>Pastes</h2>\n"
|
||||
"<ul>\n"
|
||||
"{{ paste start }}\n"
|
||||
"<li><a href=\"/{{ full_key }}\">{{ filename }}</a></li>\n"
|
||||
"{{ paste end }}\n"
|
||||
"</ul>\n"
|
||||
"</body>\n";
|
||||
|
||||
lnm_http_loop *loop_init(lander_gctx *gctx, const char *api_key) {
|
||||
lnm_http_loop *hl;
|
||||
lnm_http_step *step = NULL;
|
||||
lnm_http_route *route;
|
||||
lnm_http_loop_init(&hl, gctx, lander_ctx_init,
|
||||
(lnm_http_ctx_reset_fn)lander_ctx_reset,
|
||||
(lnm_http_ctx_free_fn)lander_ctx_free);
|
||||
lnm_http_loop_set_api_key(hl, api_key);
|
||||
lnm_http_loop_set_server(hl, lander_server);
|
||||
|
||||
lnm_http_step_init(&step, lander_get_index);
|
||||
lnm_http_route_init_literal(&route, lnm_http_method_get, "/", step);
|
||||
lnm_http_loop_route_add(hl, route);
|
||||
lnm_http_router *router;
|
||||
lnm_http_router_init(&router);
|
||||
|
||||
lnm_http_step_init(&step, lander_get_entry);
|
||||
lnm_http_route_init_regex(&route, lnm_http_method_get, "^/([^/]+)$", 1, step);
|
||||
lnm_http_loop_route_add(hl, route);
|
||||
lnm_http_route *route;
|
||||
lnm_http_router_add(&route, router, lnm_http_method_get, "/");
|
||||
lnm_http_route_step_append(route, lander_get_root, false);
|
||||
|
||||
lnm_http_step_init(&step, lnm_http_loop_step_auth);
|
||||
lnm_http_route_init_regex(&route, lnm_http_method_post, "^/s(l?)/([^/]*)$", 2,
|
||||
step);
|
||||
lnm_http_step_append(&step, step, lander_post_redirect);
|
||||
lnm_http_step_append(&step, step, lnm_http_loop_step_body_to_buf);
|
||||
lnm_http_step_append(&step, step, lander_post_redirect_body_to_attr);
|
||||
lnm_http_loop_route_add(hl, route);
|
||||
lnm_http_router_add(&route, router, lnm_http_method_get, "/:key");
|
||||
lnm_http_route_step_append(route, lander_get_entry, false);
|
||||
|
||||
lnm_http_step_init(&step, lnm_http_loop_step_auth);
|
||||
lnm_http_route_init_regex(&route, lnm_http_method_post, "^/p(l?)/([^/]*)$", 2,
|
||||
step);
|
||||
lnm_http_step_append(&step, step, lander_post_paste);
|
||||
lnm_http_step_append(&step, step, lander_stream_body_to_entry);
|
||||
lnm_http_loop_route_add(hl, route);
|
||||
lnm_http_router_add(&route, router, lnm_http_method_delete, "/:key");
|
||||
lnm_http_route_step_append(route, lnm_http_loop_step_auth, false);
|
||||
lnm_http_route_step_append(route, lander_remove_entry, false);
|
||||
|
||||
lnm_http_step_init(&step, lnm_http_loop_step_auth);
|
||||
lnm_http_route_init_regex(&route, lnm_http_method_post, "^/f(l?)/([^/]*)$", 2,
|
||||
step);
|
||||
lnm_http_step_append(&step, step, lander_post_file);
|
||||
lnm_http_step_append(&step, step, lander_stream_body_to_entry);
|
||||
lnm_http_loop_route_add(hl, route);
|
||||
lnm_http_router_add(&route, router, lnm_http_method_post, "/s/");
|
||||
lnm_http_route_step_append(route, lnm_http_loop_step_auth, false);
|
||||
lnm_http_route_step_append(route, lander_post_redirect, false);
|
||||
lnm_http_route_step_append(route, lnm_http_loop_step_body_to_buf, false);
|
||||
lnm_http_route_step_append(route, lander_post_redirect_body_to_attr, false);
|
||||
|
||||
lnm_http_step_init(&step, lnm_http_loop_step_auth);
|
||||
lnm_http_route_init_regex(&route, lnm_http_method_delete, "^/([^/]+)$", 1,
|
||||
step);
|
||||
lnm_http_step_append(&step, step, lander_remove_entry);
|
||||
lnm_http_loop_route_add(hl, route);
|
||||
lnm_http_router_add(&route, router, lnm_http_method_post, "/sl/");
|
||||
lnm_http_route_step_append(route, lnm_http_loop_step_auth, false);
|
||||
lnm_http_route_step_append(route, lander_post_redirect_secure, false);
|
||||
lnm_http_route_step_append(route, lnm_http_loop_step_body_to_buf, false);
|
||||
lnm_http_route_step_append(route, lander_post_redirect_body_to_attr, false);
|
||||
|
||||
lnm_http_router_add(&route, router, lnm_http_method_post, "/s/:key");
|
||||
lnm_http_route_step_append(route, lnm_http_loop_step_auth, false);
|
||||
lnm_http_route_step_append(route, lander_post_redirect, false);
|
||||
lnm_http_route_step_append(route, lnm_http_loop_step_body_to_buf, false);
|
||||
lnm_http_route_step_append(route, lander_post_redirect_body_to_attr, false);
|
||||
|
||||
lnm_http_router_add(&route, router, lnm_http_method_post, "/p/");
|
||||
lnm_http_route_step_append(route, lnm_http_loop_step_auth, false);
|
||||
lnm_http_route_step_append(route, lander_post_paste, false);
|
||||
lnm_http_route_step_append(route, lander_stream_body_to_entry, false);
|
||||
|
||||
lnm_http_router_add(&route, router, lnm_http_method_post, "/pl/");
|
||||
lnm_http_route_step_append(route, lnm_http_loop_step_auth, false);
|
||||
lnm_http_route_step_append(route, lander_post_paste_secure, false);
|
||||
lnm_http_route_step_append(route, lander_stream_body_to_entry, false);
|
||||
|
||||
lnm_http_router_add(&route, router, lnm_http_method_post, "/p/:key");
|
||||
lnm_http_route_step_append(route, lnm_http_loop_step_auth, false);
|
||||
lnm_http_route_step_append(route, lander_post_paste, false);
|
||||
lnm_http_route_step_append(route, lander_stream_body_to_entry, false);
|
||||
|
||||
lnm_http_router_add(&route, router, lnm_http_method_post, "/f/");
|
||||
lnm_http_route_step_append(route, lnm_http_loop_step_auth, false);
|
||||
lnm_http_route_step_append(route, lander_post_file, false);
|
||||
lnm_http_route_step_append(route, lander_stream_body_to_entry, false);
|
||||
|
||||
lnm_http_router_add(&route, router, lnm_http_method_post, "/fl/");
|
||||
lnm_http_route_step_append(route, lnm_http_loop_step_auth, false);
|
||||
lnm_http_route_step_append(route, lander_post_file_secure, false);
|
||||
lnm_http_route_step_append(route, lander_stream_body_to_entry, false);
|
||||
|
||||
lnm_http_router_add(&route, router, lnm_http_method_post, "/f/:key");
|
||||
lnm_http_route_step_append(route, lnm_http_loop_step_auth, false);
|
||||
lnm_http_route_step_append(route, lander_post_file, false);
|
||||
lnm_http_route_step_append(route, lander_stream_body_to_entry, false);
|
||||
|
||||
lnm_http_router_add(&route, router, lnm_http_method_post, "/i/");
|
||||
lnm_http_route_step_append(route, lnm_http_loop_step_auth, false);
|
||||
lnm_http_route_step_append(route, lander_post_index, false);
|
||||
|
||||
lnm_http_router_add(&route, router, lnm_http_method_post, "/il/");
|
||||
lnm_http_route_step_append(route, lnm_http_loop_step_auth, false);
|
||||
lnm_http_route_step_append(route, lander_post_index_secure, false);
|
||||
|
||||
lnm_http_router_add(&route, router, lnm_http_method_post, "/i/:key");
|
||||
lnm_http_route_step_append(route, lnm_http_loop_step_auth, false);
|
||||
lnm_http_route_step_append(route, lander_post_index, false);
|
||||
|
||||
lnm_http_loop_router_set(hl, router);
|
||||
|
||||
return hl;
|
||||
}
|
||||
|
@ -76,7 +148,7 @@ int main() {
|
|||
srand(time(NULL));
|
||||
|
||||
lnm_log_init_global();
|
||||
lnm_log_register_stdout(lnm_log_level_debug);
|
||||
lnm_log_register_stdout(lnm_log_level_info);
|
||||
|
||||
ENV(api_key, "LANDER_API_KEY");
|
||||
ENV_OPT(port_str, "LANDER_PORT", "18080");
|
||||
|
@ -92,6 +164,9 @@ int main() {
|
|||
lander_gctx *c_gctx = lander_gctx_init();
|
||||
c_gctx->data_dir = data_dir_s;
|
||||
|
||||
ltm_template_compile(&c_gctx->templates.paste, paste_template);
|
||||
ltm_template_compile(&c_gctx->templates.index, index_template);
|
||||
|
||||
lsm_str *data_dir;
|
||||
lsm_str_init_copy(&data_dir, (char *)data_dir_s);
|
||||
|
||||
|
@ -104,14 +179,5 @@ int main() {
|
|||
lnm_linfo("main", "Store loaded containing %lu entries",
|
||||
lsm_store_size(c_gctx->store));
|
||||
lnm_http_loop *hl = loop_init(c_gctx, api_key);
|
||||
lnm_http_loop_run(hl, port, 1);
|
||||
|
||||
/* http_loop *hl = http_loop_init( */
|
||||
/* lander_routes, sizeof(lander_routes) / sizeof(lander_routes[0]),
|
||||
* c_gctx, */
|
||||
/* lander_ctx_init, (void (*)(void *))lander_ctx_reset, */
|
||||
/* (void (*)(void *))lander_ctx_free); */
|
||||
/* http_loop_set_api_key(hl, api_key); */
|
||||
|
||||
/* http_loop_run(hl, port); */
|
||||
lnm_http_loop_run(hl, port, 1, 0);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue