aboutsummaryrefslogtreecommitdiff
path: root/beaker.h
blob: cc876341181f2e49932c839f47a8b2d8bec0cd06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#ifndef BEAKER_H
#define BEAKER_H

#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_CONTEXT_VARS 32       
#define MAX_KEY_LEN 64            
#define MAX_VALUE_LEN 256         
#define MAX_PATH_LEN 256          
#define MAX_HANDLERS 32           
#define BUFFER_SIZE 4096          
#define MAX_URL_PARAMS 16         
#define MAX_COOKIES 10            
#define MAX_OUTER_ARRAY_ITEMS 100 
#define MAX_INNER_ARRAY_ITEMS 200 

#define TEMPLATES_DIR "templates/" 
#define STATIC_DIR "static/"       
#define LOCALES_DIR "locales/"     

#define INITIAL_LOCALES_CAPACITY 8        
#define INITIAL_LOCALE_KEYS_CAPACITY 16   
#define MAX_LOCALES_HARD 1024             
#define MAX_LOCALE_KEYS_HARD 65536        
#define MAX_LOCALE_ID_LEN 64              
#define MAX_LOCALE_VALUE_LEN 512          

typedef enum {
  CONTEXT_TYPE_STRING,          
  CONTEXT_TYPE_STRING_ARRAY,    
  CONTEXT_TYPE_STRING_2D_ARRAY, 
} ContextType;

typedef struct {
  char key[MAX_KEY_LEN]; 
  ContextType type;      
  union {
    char string_val[MAX_VALUE_LEN]; 
    struct {
      char **values;      
      int count;          
    } string_array_data;
    struct {
      char ***values;     
      int outer_count;    
      int *inner_counts;  
    } string_2d_array_data; 
  } value;                   
} ContextVar;

typedef struct {
  ContextVar vars[MAX_CONTEXT_VARS]; 
  int count;                         
} TemplateContext;

typedef struct {
  char key[MAX_KEY_LEN];   
  char value[MAX_VALUE_LEN]; 
} UrlParam;

typedef struct {
  UrlParam params[MAX_URL_PARAMS]; 
  int count;                       
} UrlParams;

typedef struct {
  char name[MAX_KEY_LEN];    
  char value[MAX_VALUE_LEN]; 
  char expires[MAX_VALUE_LEN]; 
  char path[MAX_KEY_LEN];    
  bool http_only;            
  bool secure;               
} Cookie;

typedef struct {
  char id[MAX_LOCALE_ID_LEN];     
  char name[MAX_VALUE_LEN];       
  char direction[16];              
} LocaleMeta;

typedef struct {
  char key[MAX_KEY_LEN];              
  char value[MAX_LOCALE_VALUE_LEN];   
} LocaleKV;

typedef struct {
  LocaleMeta meta;                
  LocaleKV *keys;                 
  int key_count;                  
  int key_capacity;               
} Locale;

typedef struct {
  LocaleMeta meta; 
} LocaleInfo;

typedef int (*RequestHandler)(UrlParams *params);

typedef struct {
  char path[MAX_PATH_LEN];     
  RequestHandler handler;      
} RouteHandler;

TemplateContext new_context();
void context_set(TemplateContext *ctx, const char *key, const char *value);
void context_set_string_array(TemplateContext *ctx, const char *key,
                              char *values[], int count);
void context_set_array_of_arrays(TemplateContext *ctx, const char *key,
                                 char **values_2d[], int outer_count,
                                 int inner_counts[]);
void free_context(TemplateContext *ctx);
char *render_template(const char *template_file, TemplateContext *ctx);

int beaker_load_locales(void);
void beaker_set_locale(TemplateContext *ctx, const char *locale_id);
int beaker_get_all_locales(LocaleInfo *out, int max_count);
const LocaleMeta *beaker_get_locale_meta(const char *locale_id);
const char *beaker_get_locale_value(const char *locale_id, const char *key);
void beaker_free_locales(void);

void send_response(const char *html);
void send_redirect(const char *location);
void set_cookie(const char *name, const char *value, const char *expires,
                const char *path, bool http_only, bool secure);
char *get_cookie(const char *cookie_name);

void set_handler(const char *path, RequestHandler handler);
char *parse_request_url(const char *request_buffer, UrlParams *params);
const char *get_mime_type(const char *file_path);
bool serve_static_file(const char *request_path_relative_to_static);
bool serve_static_file_with_mime(const char *request_path_relative_to_static, const char *mime_type);
bool serve_data(const char *data, size_t size, const char *mime_type);

int beaker_run(const char *ip, int port);
void beaker_run_with_threads(const char *ip, int port, int num_workers);

#endif