Blob


1 #ifndef GMID_H
2 #define GMID_H
4 #include <arpa/inet.h>
5 #include <netinet/in.h>
7 #include <poll.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <syslog.h>
11 #include <tls.h>
12 #include <unistd.h>
14 #ifndef __OpenBSD__
15 # define pledge(a, b) 0
16 # define unveil(a, b) 0
17 #endif
19 #ifndef INFTIM
20 # define INFTIM -1
21 #endif
23 #define GEMINI_URL_LEN (1024+3) /* URL max len + \r\n + \0 */
25 /* large enough to hold a copy of a gemini URL and still have extra room */
26 #define PATHBUF 2048
28 #define SUCCESS 20
29 #define TEMP_FAILURE 40
30 #define NOT_FOUND 51
31 #define BAD_REQUEST 59
33 #ifndef MAX_USERS
34 #define MAX_USERS 64
35 #endif
37 #define SAFE_SETENV(var, val) do { \
38 const char *_tmp = (val); \
39 if (_tmp == NULL) \
40 _tmp = ""; \
41 setenv((var), _tmp, 1); \
42 } while(0)
44 #define LOG(priority, c, fmt, ...) \
45 do { \
46 char buf[INET_ADDRSTRLEN]; \
47 if (inet_ntop((c)->af, &(c)->addr, \
48 buf, sizeof(buf)) == NULL) \
49 FATAL("inet_ntop: %s", strerror(errno)); \
50 if (foreground) \
51 fprintf(stderr, \
52 "%s " fmt "\n", buf, __VA_ARGS__); \
53 else \
54 syslog((priority) | LOG_DAEMON, \
55 "%s " fmt, buf, __VA_ARGS__); \
56 } while (0)
58 #define LOGE(c, fmt, ...) LOG(LOG_ERR, c, fmt, __VA_ARGS__)
59 #define LOGN(c, fmt, ...) LOG(LOG_NOTICE, c, fmt, __VA_ARGS__)
60 #define LOGI(c, fmt, ...) LOG(LOG_INFO, c, fmt, __VA_ARGS__)
61 #define LOGD(c, fmt, ...) LOG(LOG_DEBUG, c, fmt, __VA_ARGS__)
63 #define FATAL(fmt, ...) \
64 do { \
65 if (foreground) \
66 fprintf(stderr, fmt "\n", __VA_ARGS__); \
67 else \
68 syslog(LOG_DAEMON | LOG_CRIT, \
69 fmt, __VA_ARGS__); \
70 exit(1); \
71 } while (0)
73 enum {
74 S_OPEN,
75 S_INITIALIZING,
76 S_SENDING,
77 S_CLOSING,
78 };
80 struct client {
81 struct tls *ctx;
82 int state;
83 int code;
84 const char *meta;
85 int fd, waiting_on_child;
86 pid_t child;
87 char sbuf[1024]; /* static buffer */
88 void *buf, *i; /* mmap buffer */
89 ssize_t len, off; /* mmap/static buffer */
90 int af;
91 struct in_addr addr;
92 };
94 enum {
95 FILE_EXISTS,
96 FILE_EXECUTABLE,
97 FILE_DIRECTORY,
98 FILE_MISSING,
99 };
101 struct etm { /* file extension to mime */
102 const char *mime;
103 const char *ext;
104 } filetypes[] = {
105 {"application/pdf", "pdf"},
107 {"image/gif", "gif"},
108 {"image/jpeg", "jpg"},
109 {"image/jpeg", "jpeg"},
110 {"image/png", "png"},
111 {"image/svg+xml", "svg"},
113 {"text/gemini", "gemini"},
114 {"text/gemini", "gmi"},
115 {"text/markdown", "markdown"},
116 {"text/markdown", "md"},
117 {"text/plain", "txt"},
118 {"text/xml", "xml"},
120 {NULL, NULL}
121 };
123 void siginfo_handler(int);
124 int starts_with(const char*, const char*);
126 char *url_after_proto(char*);
127 char *url_start_of_request(char*);
128 int url_trim(struct client*, char*);
129 char *adjust_path(char*);
130 ssize_t filesize(int);
132 int start_reply(struct pollfd*, struct client*, int, const char*);
133 const char *path_ext(const char*);
134 const char *mime(const char*);
135 int check_path(struct client*, const char*, int*);
136 int check_for_cgi(char *, char*, struct pollfd*, struct client*);
137 int open_file(char*, char*, struct pollfd*, struct client*);
138 int start_cgi(const char*, const char*, const char*, struct pollfd*, struct client*);
139 void cgi_setpoll_on_child(struct pollfd*, struct client*);
140 void cgi_setpoll_on_client(struct pollfd*, struct client*);
141 void handle_cgi(struct pollfd*, struct client*);
142 void send_file(char*, char*, struct pollfd*, struct client*);
143 void send_dir(char*, struct pollfd*, struct client*);
144 void handle(struct pollfd*, struct client*);
146 void mark_nonblock(int);
147 int make_soket(int);
148 void do_accept(int, struct tls*, struct pollfd*, struct client*);
149 void goodbye(struct pollfd*, struct client*);
150 void loop(struct tls*, int);
152 void usage(const char*);
154 #endif