2 d5aba4c7 2020-12-24 op * Copyright (c) 2020 Omar Polo <op@omarpolo.com>
4 d5aba4c7 2020-12-24 op * Permission to use, copy, modify, and distribute this software for any
5 d5aba4c7 2020-12-24 op * purpose with or without fee is hereby granted, provided that the above
6 d5aba4c7 2020-12-24 op * copyright notice and this permission notice appear in all copies.
8 d5aba4c7 2020-12-24 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 d5aba4c7 2020-12-24 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 d5aba4c7 2020-12-24 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 d5aba4c7 2020-12-24 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 d5aba4c7 2020-12-24 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 d5aba4c7 2020-12-24 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 d5aba4c7 2020-12-24 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 488f059a 2020-12-24 op #ifndef GMID_H
18 488f059a 2020-12-24 op #define GMID_H
20 488f059a 2020-12-24 op #include <arpa/inet.h>
21 488f059a 2020-12-24 op #include <netinet/in.h>
22 28778244 2021-01-11 op #include <sys/socket.h>
24 488f059a 2020-12-24 op #include <poll.h>
25 488f059a 2020-12-24 op #include <stdio.h>
26 488f059a 2020-12-24 op #include <stdlib.h>
27 488f059a 2020-12-24 op #include <syslog.h>
28 488f059a 2020-12-24 op #include <tls.h>
29 488f059a 2020-12-24 op #include <unistd.h>
31 488f059a 2020-12-24 op #ifndef __OpenBSD__
32 488f059a 2020-12-24 op # define pledge(a, b) 0
33 488f059a 2020-12-24 op # define unveil(a, b) 0
36 488f059a 2020-12-24 op #ifndef INFTIM
37 488f059a 2020-12-24 op # define INFTIM -1
40 488f059a 2020-12-24 op #define GEMINI_URL_LEN (1024+3) /* URL max len + \r\n + \0 */
42 488f059a 2020-12-24 op /* large enough to hold a copy of a gemini URL and still have extra room */
43 488f059a 2020-12-24 op #define PATHBUF 2048
45 488f059a 2020-12-24 op #define SUCCESS 20
46 488f059a 2020-12-24 op #define TEMP_FAILURE 40
47 488f059a 2020-12-24 op #define NOT_FOUND 51
48 488f059a 2020-12-24 op #define BAD_REQUEST 59
50 488f059a 2020-12-24 op #define MAX_USERS 64
54 488f059a 2020-12-24 op S_INITIALIZING,
59 488f059a 2020-12-24 op struct client {
60 488f059a 2020-12-24 op struct tls *ctx;
63 488f059a 2020-12-24 op const char *meta;
64 488f059a 2020-12-24 op int fd, waiting_on_child;
66 488f059a 2020-12-24 op char sbuf[1024]; /* static buffer */
67 488f059a 2020-12-24 op void *buf, *i; /* mmap buffer */
68 488f059a 2020-12-24 op ssize_t len, off; /* mmap/static buffer */
70 709d6e5e 2021-01-10 op struct sockaddr_storage addr;
77 33d32d1f 2020-12-25 op uint16_t port_no;
80 33d32d1f 2020-12-25 op char *fragment;
83 ef04b551 2021-01-09 op struct parser {
85 3c1cf9d0 2021-01-11 op struct iri *parsed;
86 ef04b551 2021-01-09 op const char *err;
91 488f059a 2020-12-24 op FILE_EXECUTABLE,
92 488f059a 2020-12-24 op FILE_DIRECTORY,
97 4a28dd01 2020-12-28 op void sig_handler(int);
98 488f059a 2020-12-24 op int starts_with(const char*, const char*);
100 488f059a 2020-12-24 op int start_reply(struct pollfd*, struct client*, int, const char*);
101 3d9a1c73 2020-12-28 op ssize_t filesize(int);
102 488f059a 2020-12-24 op const char *path_ext(const char*);
103 488f059a 2020-12-24 op const char *mime(const char*);
104 488f059a 2020-12-24 op int check_path(struct client*, const char*, int*);
105 488f059a 2020-12-24 op int check_for_cgi(char *, char*, struct pollfd*, struct client*);
106 488f059a 2020-12-24 op int open_file(char*, char*, struct pollfd*, struct client*);
107 488f059a 2020-12-24 op int start_cgi(const char*, const char*, const char*, struct pollfd*, struct client*);
108 6c6c7a0e 2020-12-28 op void cgi_poll_on_child(struct pollfd*, struct client*);
109 6c6c7a0e 2020-12-28 op void cgi_poll_on_client(struct pollfd*, struct client*);
110 488f059a 2020-12-24 op void handle_cgi(struct pollfd*, struct client*);
111 488f059a 2020-12-24 op void send_file(char*, char*, struct pollfd*, struct client*);
112 488f059a 2020-12-24 op void send_dir(char*, struct pollfd*, struct client*);
113 488f059a 2020-12-24 op void handle(struct pollfd*, struct client*);
115 488f059a 2020-12-24 op void mark_nonblock(int);
116 488f059a 2020-12-24 op int make_soket(int);
117 488f059a 2020-12-24 op void do_accept(int, struct tls*, struct pollfd*, struct client*);
118 488f059a 2020-12-24 op void goodbye(struct pollfd*, struct client*);
119 33756bd2 2021-01-10 op void loop(struct tls*, int, int);
121 488f059a 2020-12-24 op void usage(const char*);
124 ef04b551 2021-01-09 op int valid_multibyte_utf8(struct parser*);
127 3c1cf9d0 2021-01-11 op int parse_iri(char*, struct iri*, const char**);
128 3c1cf9d0 2021-01-11 op int trim_req_iri(char*);