Blame


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