2 * Copyright (c) 2004-2005 Sergey Lyubka <valenok@gmail.com>
5 * "THE BEER-WARE LICENSE" (Revision 42):
6 * Sergey Lyubka wrote this file. As long as you retain this notice you
7 * can do whatever you want with this stuff. If we meet some day, and you think
8 * this stuff is worth it, you can buy me a beer in return.
11 #ifndef DEFS_HEADER_DEFINED
12 #define DEFS_HEADER_DEFINED
14 #include "std_includes.h"
21 #define NELEMS(ar) (sizeof(ar) / sizeof(ar[0]))
24 #define DBG(x) do { printf x ; putchar('\n'); fflush(stdout); } while (0)
30 * Darwin prior to 7.0 and Win32 do not have socklen_t
33 typedef int socklen_t;
34 #endif /* NO_SOCKLEN_T */
37 * For parsing. This guy represents a substring.
48 enum {METHOD_GET, METHOD_POST, METHOD_PUT, METHOD_DELETE, METHOD_HEAD};
49 enum {HDR_DATE, HDR_INT, HDR_STRING}; /* HTTP header types */
50 enum {E_FATAL = 1, E_LOG = 2}; /* Flags for elog() function */
51 typedef unsigned long big_int_t; /* Type for Content-Length */
54 * Unified socket address
60 struct sockaddr_in sin;
65 * This thing is aimed to hold values of any type.
66 * Used to store parsed headers' values.
79 * This is used only in embedded configuration. This structure holds a
80 * registered URI, associated callback function with callback data.
81 * For non-embedded compilation shttpd_callback_t is not defined, so
82 * we use union variant to keep the compiler silent.
84 struct registered_uri {
87 union variant callback;
92 * User may want to handle certain errors. This structure holds the
93 * handlers for corresponding error codes.
95 struct error_handler {
98 union variant callback;
103 int len; /* Header name length */
104 int type; /* Header type */
105 size_t offset; /* Value placeholder */
106 const char *name; /* Header name */
110 * This guy holds parsed HTTP headers
113 union variant cl; /* Content-Length: */
114 union variant ct; /* Content-Type: */
115 union variant connection; /* Connection: */
116 union variant ims; /* If-Modified-Since: */
117 union variant user; /* Remote user name */
118 union variant auth; /* Authorization */
119 union variant useragent; /* User-Agent: */
120 union variant referer; /* Referer: */
121 union variant cookie; /* Cookie: */
122 union variant location; /* Location: */
123 union variant range; /* Range: */
124 union variant status; /* Status: */
125 union variant transenc; /* Transfer-Encoding: */
128 /* Must go after union variant definition */
132 * The communication channel
135 int fd; /* Regular static file */
136 int sock; /* Connected socket */
138 int sock; /* XXX important. must be first */
139 SSL *ssl; /* shttpd_poll() assumes that */
140 } ssl; /* SSL-ed socket */
144 } dir; /* Opened directory */
146 void *state; /* For keeping state */
147 union variant func; /* User callback function */
148 void *data; /* User defined parameters */
149 } emb; /* Embedded, user callback */
155 * IO class descriptor (file, directory, socket, SSL, CGI, etc)
156 * These classes are defined in io_*.c files.
160 int (*read)(struct stream *, void *buf, size_t len);
161 int (*write)(struct stream *, const void *buf, size_t len);
162 void (*close)(struct stream *);
166 * Data exchange stream. It is backed by some communication channel:
167 * opened file, socket, etc. The 'read' and 'write' methods are
168 * determined by a communication channel.
172 union channel chan; /* Descriptor */
173 struct io io; /* IO buffer */
174 const struct io_class *io_class; /* IO class */
176 big_int_t content_len;
178 #define FLAG_HEADERS_PARSED 1
179 #define FLAG_SSL_ACCEPTED 2
180 #define FLAG_R 4 /* Can read in general */
181 #define FLAG_W 8 /* Can write in general */
182 #define FLAG_CLOSED 16
183 #define FLAG_DONT_CLOSE 32
184 #define FLAG_ALWAYS_READY 64 /* File, dir, user_func */
185 #define FLAG_SUSPEND 128
190 int num_conns; /* Num of active connections */
191 int exit_flag; /* Ditto - exit flag */
192 int ctl[2]; /* Control socket pair */
193 struct shttpd_ctx *ctx; /* Context reference */
194 struct llhead connections; /* List of connections */
198 struct llhead link; /* Connections chain */
199 struct worker *worker; /* Worker this conn belongs to */
200 struct shttpd_ctx *ctx; /* Context this conn belongs to */
201 struct usa sa; /* Remote socket address */
202 time_t birth_time; /* Creation time */
203 time_t expire_time; /* Expiration time */
205 int loc_port; /* Local port */
206 int status; /* Reply status code */
207 int method; /* Request method */
208 char *uri; /* Decoded URI */
209 unsigned long major_version; /* Major HTTP version number */
210 unsigned long minor_version; /* Minor HTTP version number */
211 char *request; /* Request line */
212 char *headers; /* Request headers */
213 char *query; /* QUERY_STRING part of the URI */
214 char *path_info; /* PATH_INFO thing */
215 struct vec mime_type; /* Mime type */
217 struct headers ch; /* Parsed client headers */
219 struct stream loc; /* Local stream */
220 struct stream rem; /* Remote stream */
223 void *ssi; /* SSI descriptor */
228 OPT_ROOT, OPT_INDEX_FILES, OPT_PORTS, OPT_DIR_LIST,
229 OPT_CGI_EXTENSIONS, OPT_CGI_INTERPRETER, OPT_CGI_ENVIRONMENT,
230 OPT_SSI_EXTENSIONS, OPT_AUTH_REALM, OPT_AUTH_GPASSWD,
231 OPT_AUTH_PUT, OPT_ACCESS_LOG, OPT_ERROR_LOG, OPT_MIME_TYPES,
232 OPT_SSL_CERTIFICATE, OPT_ALIASES, OPT_ACL, OPT_INETD, OPT_UID,
233 OPT_CFG_URI, OPT_PROTECT, OPT_SERVICE, OPT_HIDE, OPT_THREADS,
241 SSL_CTX *ssl_ctx; /* SSL context */
243 struct llhead registered_uris;/* User urls */
244 struct llhead error_handlers; /* Embedded error handlers */
245 struct llhead acl; /* Access control list */
246 struct llhead ssi_funcs; /* SSI callback functions */
247 struct llhead listeners; /* Listening sockets */
248 struct llhead workers; /* Worker workers */
250 FILE *access_log; /* Access log stream */
251 FILE *error_log; /* Error log stream */
253 char *options[NUM_OPTIONS]; /* Configurable options */
254 #if defined(__rtems__)
261 struct shttpd_ctx *ctx; /* Context that socket belongs */
262 int sock; /* Listening socket */
263 int is_ssl; /* Should be SSL-ed */
266 /* Types of messages that could be sent over the control socket */
267 enum {CTL_PASS_SOCKET, CTL_WAKEUP};
270 * In SHTTPD, list of values are represented as comma or space separated
271 * string. For example, list of CGI extensions can be represented as
272 * ".cgi,.php,.pl", or ".cgi .php .pl". The macro that follows allows to
273 * loop through the individual values in that list.
275 * A "const char *" pointer and size_t variable must be passed to the macro.
276 * Spaces or commas can be used as delimiters (macro DELIM_CHARS).
278 * In every iteration of the loop, "s" points to the current value, and
279 * "len" specifies its length. The code inside loop must not change
280 * "s" and "len" parameters.
282 #define FOR_EACH_WORD_IN_LIST(s,len) \
283 for (; s != NULL && (len = strcspn(s, DELIM_CHARS)) != 0; \
284 s += len, s+= strspn(s, DELIM_CHARS))
287 * IPv4 ACL entry. Specifies subnet with deny/allow flag
291 uint32_t ip; /* IP, in network byte order */
292 uint32_t mask; /* Also in network byte order */
293 int flag; /* Either '+' or '-' */
299 extern time_t _shttpd_current_time; /* Current UTC time */
300 extern int _shttpd_tz_offset; /* Offset from GMT time zone */
301 extern const struct vec _shttpd_known_http_methods[];
303 extern void _shttpd_stop_stream(struct stream *stream);
304 extern int _shttpd_url_decode(const char *, int, char *dst, int);
305 extern void _shttpd_send_server_error(struct conn *, int, const char *);
306 extern int _shttpd_get_headers_len(const char *buf, size_t buflen);
307 extern void _shttpd_parse_headers(const char *s, int, struct headers *);
308 extern int _shttpd_is_true(const char *str);
309 extern int _shttpd_socketpair(int pair[2]);
310 extern void _shttpd_get_mime_type(struct shttpd_ctx *,
311 const char *, int, struct vec *);
313 #define IS_TRUE(ctx, opt) _shttpd_is_true((ctx)->options[opt])
318 extern void _shttpd_usage(const char *prog);
323 extern void _shttpd_elog(int flags, struct conn *c, const char *fmt, ...);
324 extern void _shttpd_log_access(FILE *fp, const struct conn *c);
329 extern void _shttpd_strlcpy(register char *, register const char *, size_t);
330 extern int _shttpd_strncasecmp(register const char *,
331 register const char *, size_t);
332 extern char *_shttpd_strndup(const char *ptr, size_t len);
333 extern char *_shttpd_strdup(const char *str);
334 extern int _shttpd_snprintf(char *buf, size_t len, const char *fmt, ...);
335 extern int _shttpd_match_extension(const char *path, const char *ext_list);
340 extern void _shttpd_set_close_on_exec(int fd);
341 extern int _shttpd_set_non_blocking_mode(int fd);
342 extern int _shttpd_stat(const char *, struct stat *stp);
343 extern int _shttpd_open(const char *, int flags, int mode);
344 extern int _shttpd_remove(const char *);
345 extern int _shttpd_rename(const char *, const char *);
346 extern int _shttpd_mkdir(const char *, int);
347 extern char * _shttpd_getcwd(char *, int);
348 extern int _shttpd_spawn_process(struct conn *c, const char *prog,
349 char *envblk, char *envp[], int sock, const char *dir);
351 extern int _shttpd_set_nt_service(struct shttpd_ctx *, const char *);
352 extern int _shttpd_set_systray(struct shttpd_ctx *, const char *);
353 extern void _shttpd_try_to_run_as_nt_service(void);
358 extern const struct io_class _shttpd_io_file;
359 extern const struct io_class _shttpd_io_socket;
360 extern const struct io_class _shttpd_io_ssl;
361 extern const struct io_class _shttpd_io_cgi;
362 extern const struct io_class _shttpd_io_dir;
363 extern const struct io_class _shttpd_io_embedded;
364 extern const struct io_class _shttpd_io_ssi;
366 extern int _shttpd_put_dir(const char *path);
367 extern void _shttpd_get_dir(struct conn *c);
368 extern void _shttpd_get_file(struct conn *c, struct stat *stp);
369 extern void _shttpd_ssl_handshake(struct stream *stream);
370 extern void _shttpd_setup_embedded_stream(struct conn *,
371 union variant, void *);
372 extern struct registered_uri *_shttpd_is_registered_uri(struct shttpd_ctx *,
374 extern void _shttpd_do_ssi(struct conn *);
375 extern void _shttpd_ssi_func_destructor(struct llhead *lp);
380 extern int _shttpd_check_authorization(struct conn *c, const char *path);
381 extern int _shttpd_is_authorized_for_put(struct conn *c);
382 extern void _shttpd_send_authorization_request(struct conn *c);
383 extern int _shttpd_edit_passwords(const char *fname, const char *domain,
384 const char *user, const char *pass);
389 extern int _shttpd_run_cgi(struct conn *c, const char *prog);
390 extern void _shttpd_do_cgi(struct conn *c);
392 #define CGI_REPLY "HTTP/1.1 OK\r\n"
393 #define CGI_REPLY_LEN (sizeof(CGI_REPLY) - 1)
395 #endif /* DEFS_HEADER_DEFINED */