5 #include <netinet/in.h>
15 # define pledge(a, b) 0
16 # define unveil(a, b) 0
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 */
29 #define TEMP_FAILURE 40
31 #define BAD_REQUEST 59
37 #define SAFE_SETENV(var, val) do { \
38 const char *_tmp = (val); \
41 setenv((var), _tmp, 1); \
44 #define LOG(priority, c, fmt, ...) \
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)); \
52 "%s " fmt "\n", buf, __VA_ARGS__); \
54 syslog((priority) | LOG_DAEMON, \
55 "%s " fmt, buf, __VA_ARGS__); \
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, ...) \
66 fprintf(stderr, fmt "\n", __VA_ARGS__); \
68 syslog(LOG_DAEMON | LOG_CRIT, \
85 int fd, waiting_on_child;
87 char sbuf[1024]; /* static buffer */
88 void *buf, *i; /* mmap buffer */
89 ssize_t len, off; /* mmap/static buffer */
101 struct etm { /* file extension to mime */
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"},
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);
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*);