lander/src/http_loop/http_loop_tools.c

38 lines
1.1 KiB
C

#include <sys/stat.h>
#include "http_loop.h"
void http_loop_res_set_body_buf(http_loop_ctx *ctx, const char *body,
size_t body_len, bool owned) {
ctx->res.body_type = http_body_buf;
ctx->res.body.buf = (char *)body;
ctx->res.body_len = body_len;
ctx->res.owns_body = owned;
}
void http_loop_res_set_body_file(http_loop_ctx *ctx, const char *filename) {
struct stat st;
stat(filename, &st);
// TODO error handling
FILE *f = fopen(filename, "r");
ctx->res.body_type = http_body_file;
ctx->res.body.file = f;
ctx->res.body_len = st.st_size;
}
void http_loop_res_add_header(http_loop_ctx *ctx, http_header type,
const char *value, bool owned) {
ctx->res.headers[ctx->res.header_count].type = type;
ctx->res.headers[ctx->res.header_count].value = value;
ctx->res.headers[ctx->res.header_count].owned = owned;
ctx->res.header_count++;
}
void http_loop_res_set_mime_type(http_loop_ctx *ctx, http_mime_type mime_type) {
http_loop_res_add_header(ctx, http_header_content_type,
http_mime_type_names[mime_type][1], false);
}