Blob


1 /*
2 * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #define FD_RESERVE 5
18 #define GEMINI_MAXLEN 1025 /* including NUL */
20 #ifdef DEBUG
21 #define DPRINTF log_debug
22 #else
23 #define DPRINTF(x...) do {} while (0)
24 #endif
26 struct bufferevent;
27 struct event;
28 struct fcgi;
29 struct sqlite3;
30 struct sqlite3_stmt;
32 enum {
33 METHOD_UNKNOWN,
34 METHOD_GET,
35 METHOD_POST,
36 };
38 struct client {
39 uint32_t clt_id;
40 int clt_fd;
41 struct fcgi *clt_fcgi;
42 char *clt_server_name;
43 char *clt_script_name;
44 char *clt_path_info;
45 char *clt_query;
46 int clt_method;
47 #if template
48 struct template *clt_tp;
49 #endif
50 char clt_buf[1024];
51 size_t clt_buflen;
53 SPLAY_ENTRY(client) clt_nodes;
54 };
55 SPLAY_HEAD(client_tree, client);
57 struct fcgi {
58 uint32_t fcg_id;
59 int fcg_s;
60 struct client_tree fcg_clients;
61 struct bufferevent *fcg_bev;
62 int fcg_toread;
63 int fcg_want;
64 int fcg_padding;
65 int fcg_type;
66 int fcg_rec_id;
67 int fcg_keep_conn;
68 int fcg_done;
70 struct env *fcg_env;
72 SPLAY_ENTRY(fcgi) fcg_nodes;
73 };
74 SPLAY_HEAD(fcgi_tree, fcgi);
76 struct env {
77 int env_sockfd;
78 struct event env_sockev;
79 struct event env_pausev;
80 struct fcgi_tree env_fcgi_socks;
82 struct sqlite3 *env_db;
83 struct sqlite3_stmt *env_qsearch;
84 struct sqlite3_stmt *env_qfullpkgpath;
85 struct sqlite3_stmt *env_qcats;
86 struct sqlite3_stmt *env_qbycat;
87 };
89 /* fcgi.c */
90 int fcgi_end_request(struct client *, int);
91 int fcgi_abort_request(struct client *);
92 void fcgi_accept(int, short, void *);
93 void fcgi_read(struct bufferevent *, void *);
94 void fcgi_write(struct bufferevent *, void *);
95 void fcgi_error(struct bufferevent *, short, void *);
96 void fcgi_free(struct fcgi *);
97 int clt_putc(struct client *, char);
98 int clt_puts(struct client *, const char *);
99 int clt_write_bufferevent(struct client *, struct bufferevent *);
100 int clt_flush(struct client *);
101 int clt_write(struct client *, const uint8_t *, size_t);
102 int clt_printf(struct client *, const char *, ...)
103 __attribute__((__format__(printf, 2, 3)))
104 __attribute__((__nonnull__(2)));
105 #if template
106 int clt_tp_puts(struct template *, const char *);
107 int clt_tp_putc(struct template *, int);
108 #endif
109 int fcgi_cmp(struct fcgi *, struct fcgi *);
110 int fcgi_client_cmp(struct client *, struct client *);
112 /* server.c */
113 int server_main(const char *);
114 int server_handle(struct env *, struct client *);
115 void server_client_free(struct client *);
117 #if template
118 /* ui.tmpl */
119 int tp_home(struct template *);
120 #endif
122 SPLAY_PROTOTYPE(client_tree, client, clt_nodes, fcgi_client_cmp);
123 SPLAY_PROTOTYPE(fcgi_tree, fcgi, fcg_nodes, fcgi_cmp);