Web/context.h
2025-09-15 15:29:08 -05:00

65 lines
1.7 KiB
C

#ifndef CONTEXT_H
#define CONTEXT_H
#include "request.h"
#include "response.h"
#include "reactor/conn.h"
typedef struct context context_t;
typedef struct context_data context_data_t;
// Key-value storage for context
struct context_data {
char *key;
void *value;
context_data_t *next;
};
// Request context
struct context {
request_t *request;
response_t *response;
conn_t *conn;
arena_t *arena;
// User data storage
context_data_t *data;
};
// Context lifecycle
context_t* context_new(conn_t *conn);
void context_free(context_t *ctx);
// Data storage
void context_set(context_t *ctx, const char *key, void *value);
void* context_get(context_t *ctx, const char *key);
// Request helpers
const char* ctx_param(context_t *ctx, const char *name);
const char* ctx_query(context_t *ctx, const char *name);
const char* ctx_header(context_t *ctx, const char *name);
const char* ctx_cookie(context_t *ctx, const char *name);
const char* ctx_body(context_t *ctx);
// Response helpers
void ctx_status(context_t *ctx, int code);
void ctx_header_set(context_t *ctx, const char *name, const char *value);
// Send responses
void ctx_text(context_t *ctx, int status, const char *text);
void ctx_html(context_t *ctx, int status, const char *html);
void ctx_json(context_t *ctx, int status, const char *json);
void ctx_file(context_t *ctx, const char *path);
void ctx_redirect(context_t *ctx, const char *url);
void ctx_error(context_t *ctx, int status, const char *message);
// Response building
void ctx_write(context_t *ctx, const char *data, size_t len);
void ctx_printf(context_t *ctx, const char *fmt, ...);
// Send response to client
int ctx_send(context_t *ctx);
int ctx_send_chunk(context_t *ctx, const char *data, size_t len);
#endif // CONTEXT_H