lander/src/lander/lander.c

70 lines
1.7 KiB
C

#include <stdio.h>
#include <string.h>
#include "lnm/common.h"
#include "lsm/store.h"
#include "ltm/template.h"
#include "lander.h"
const char lander_key_charset[] =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
void *lander_gctx_init() { return calloc(1, sizeof(lander_gctx)); }
lnm_err lander_ctx_init(void **c_ctx, void *gctx) {
lander_ctx *ctx = calloc(1, sizeof(lander_ctx));
if (ctx == NULL) {
return lnm_err_failed_alloc;
}
*c_ctx = ctx;
return lnm_err_ok;
}
void lander_ctx_reset(lander_ctx *ctx) {
if (ctx->entry != NULL) {
lsm_entry_close(ctx->entry);
ctx->entry = NULL;
}
if (ctx->instance != NULL) {
ltm_instance_free(ctx->instance);
ctx->instance = NULL;
}
}
void lander_ctx_free(lander_ctx *ctx) { free(ctx); }
void lander_header_to_attr(lnm_http_loop_ctx *ctx, const char *header_name,
lander_attr_type attr_type) {
lander_ctx *c_ctx = ctx->c;
const char *header_value;
size_t header_value_len;
if (lnm_http_req_header_get_s(&header_value, &header_value_len, &ctx->req,
header_name) == lnm_err_ok) {
lsm_str *value;
lsm_str_init_copy_n(&value, (char *)header_value, header_value_len);
lsm_entry_attr_insert(c_ctx->entry, attr_type, value);
}
}
void lander_attr_to_header(lnm_http_loop_ctx *ctx, lander_attr_type attr_type,
lnm_http_header header_type) {
lander_ctx *c_ctx = ctx->c;
lsm_str *value;
if (lsm_entry_attr_get(&value, c_ctx->entry, attr_type) == lsm_error_ok) {
lnm_http_res_add_header_len(&ctx->res, header_type,
(char *)lsm_str_ptr(value), lsm_str_len(value),
false);
}
}