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)
34 * Darwin prior to 7.0 and Win32 do not have socklen_t
37 typedef int socklen_t;
38 #endif /* NO_SOCKLEN_T */
41 * For parsing. This guy represents a substring.
51 enum {METHOD_GET, METHOD_POST, METHOD_PUT, METHOD_DELETE, METHOD_HEAD};
52 enum {HDR_DATE, HDR_INT, HDR_STRING}; /* HTTP header types */
53 enum {E_FATAL = 1, E_LOG = 2}; /* Flags for elog() function */
54 typedef unsigned long big_int_t; /* Type for Content-Length */
57 * Unified socket address
63 struct sockaddr_in sin;
68 * This thing is aimed to hold values of any type.
69 * Used to store parsed headers' values.
82 * This is used only in embedded configuration. This structure holds a
83 * registered URI, associated callback function with callback data.
84 * For non-embedded compilation shttpd_callback_t is not defined, so
85 * we use union variant to keep the compiler silent.
87 struct registered_uri {
90 union variant callback;
95 * User may want to handle certain errors. This structure holds the
96 * handlers for corresponding error codes.
98 struct error_handler {
101 union variant callback;
106 int len; /* Header name length */
107 int type; /* Header type */
108 size_t offset; /* Value placeholder */
109 const char *name; /* Header name */
113 * This guy holds parsed HTTP headers
116 union variant cl; /* Content-Length: */
117 union variant ct; /* Content-Type: */
118 union variant connection; /* Connection: */
119 union variant ims; /* If-Modified-Since: */
120 union variant user; /* Remote user name */
121 union variant auth; /* Authorization */
122 union variant useragent; /* User-Agent: */
123 union variant referer; /* Referer: */
124 union variant cookie; /* Cookie: */
125 union variant location; /* Location: */
126 union variant range; /* Range: */
127 union variant status; /* Status: */
128 union variant transenc; /* Transfer-Encoding: */
131 /* Must go after union variant definition */
135 * The communication channel
138 int fd; /* Regular static file */
139 int sock; /* Connected socket */
141 int sock; /* XXX important. must be first */
142 SSL *ssl; /* shttpd_poll() assumes that */
143 } ssl; /* SSL-ed socket */
147 } dir; /* Opened directory */
149 void *state; /* For keeping state */
150 union variant func; /* User callback function */
151 void *data; /* User defined parameters */
152 } emb; /* Embedded, user callback */
158 * IO class descriptor (file, directory, socket, SSL, CGI, etc)
159 * These classes are defined in io_*.c files.
163 int (*read)(struct stream *, void *buf, size_t len);
164 int (*write)(struct stream *, const void *buf, size_t len);
165 void (*close)(struct stream *);
169 * Data exchange stream. It is backed by some communication channel:
170 * opened file, socket, etc. The 'read' and 'write' methods are
171 * determined by a communication channel.
175 union channel chan; /* Descriptor */
176 struct io io; /* IO buffer */
177 const struct io_class *io_class; /* IO class */
178 int nread_last; /* Bytes last read */
180 big_int_t content_len;
182 #define FLAG_HEADERS_PARSED 1
183 #define FLAG_SSL_ACCEPTED 2
184 #define FLAG_R 4 /* Can read in general */
185 #define FLAG_W 8 /* Can write in general */
186 #define FLAG_CLOSED 16
187 #define FLAG_DONT_CLOSE 32
188 #define FLAG_ALWAYS_READY 64 /* File, dir, user_func */
192 struct llhead link; /* Connections chain */
193 struct shttpd_ctx *ctx; /* Context this conn belongs to */
194 struct usa sa; /* Remote socket address */
195 time_t birth_time; /* Creation time */
196 time_t expire_time; /* Expiration time */
198 int loc_port; /* Local port */
199 int status; /* Reply status code */
200 int method; /* Request method */
201 char *uri; /* Decoded URI */
202 unsigned long major_version; /* Major HTTP version number */
203 unsigned long minor_version; /* Minor HTTP version number */
204 char *request; /* Request line */
205 char *headers; /* Request headers */
206 char *query; /* QUERY_STRING part of the URI */
207 char *path_info; /* PATH_INFO thing */
208 struct vec mime_type; /* Mime type */
210 struct headers ch; /* Parsed client headers */
212 struct stream loc; /* Local stream */
213 struct stream rem; /* Remote stream */
216 void *ssi; /* SSI descriptor */
221 OPT_ROOT, OPT_INDEX_FILES, OPT_PORTS, OPT_DIR_LIST,
222 OPT_CGI_EXTENSIONS, OPT_CGI_INTERPRETER, OPT_CGI_ENVIRONMENT,
223 OPT_SSI_EXTENSIONS, OPT_AUTH_REALM, OPT_AUTH_GPASSWD,
224 OPT_AUTH_PUT, OPT_ACCESS_LOG, OPT_ERROR_LOG, OPT_MIME_TYPES,
225 OPT_SSL_CERTIFICATE, OPT_ALIASES, OPT_ACL, OPT_INETD, OPT_UID,
226 OPT_CFG_URI, OPT_PROTECT,
234 time_t start_time; /* Start time */
235 int nactive; /* # of connections now */
236 unsigned long nrequests; /* Requests made */
237 uint64_t in, out; /* IN/OUT traffic counters */
238 SSL_CTX *ssl_ctx; /* SSL context */
239 struct llhead connections; /* List of connections */
241 struct llhead registered_uris;/* User urls */
242 struct llhead error_handlers; /* Embedded error handlers */
243 struct llhead acl; /* Access control list */
244 struct llhead ssi_funcs; /* SSI callback functions */
245 struct llhead listeners; /* Listening sockets */
247 FILE *access_log; /* Access log stream */
248 FILE *error_log; /* Error log stream */
250 char *options[NUM_OPTIONS]; /* Configurable options */
253 CRITICAL_SECTION mutex; /* For MT case */
254 HANDLE ev[2]; /* For thread synchronization */
255 #elif defined(__rtems__)
260 #define IS_TRUE(ctx, opt) ((ctx)->options[opt] && (ctx)->options[opt][0] =='1')
263 * In SHTTPD, list of values are represented as comma or space separated
264 * string. For example, list of CGI extensions can be represented as
265 * ".cgi,.php,.pl", or ".cgi .php .pl". The macro that follows allows to
266 * loop through the individual values in that list.
268 * A "const char *" pointer and size_t variable must be passed to the macro.
269 * Spaces or commas can be used as delimiters (macro DELIM_CHARS).
271 * In every iteration of the loop, "s" points to the current value, and
272 * "len" specifies its length. The code inside loop must not change
273 * "s" and "len" parameters.
275 #define FOR_EACH_WORD_IN_LIST(s,len) \
276 for (; s != NULL && (len = strcspn(s, DELIM_CHARS)) != 0; \
277 s += len, s+= strspn(s, DELIM_CHARS))
280 * IPv4 ACL entry. Specifies subnet with deny/allow flag
284 uint32_t ip; /* IP, in network byte order */
285 uint32_t mask; /* Also in network byte order */
286 int flag; /* Either '+' or '-' */
292 extern time_t current_time; /* Current UTC time */
293 extern int tz_offset; /* Offset from GMT time zone */
294 extern const struct vec known_http_methods[];
296 extern void stop_stream(struct stream *stream);
297 extern int url_decode(const char *, int, char *dst, int);
298 extern void send_server_error(struct conn *, int code, const char *reason);
299 extern int get_headers_len(const char *buf, size_t buflen);
300 extern void parse_headers(const char *s, int len, struct headers *parsed);
301 extern void open_listening_ports(struct shttpd_ctx *ctx);
302 extern void get_mime_type(struct shttpd_ctx *, const char *, int, struct vec *);
303 extern void free_list(struct llhead *head, void (*)(struct llhead *));
304 extern void registered_uri_destructor(struct llhead *);
305 extern void listener_destructor(struct llhead *);
310 extern void usage(const char *prog);
315 extern void elog(int flags, struct conn *c, const char *fmt, ...);
316 extern void log_access(FILE *fp, const struct conn *c);
321 extern void my_strlcpy(register char *, register const char *, size_t);
322 extern int my_strncasecmp(register const char *,
323 register const char *, size_t);
324 extern char *my_strndup(const char *ptr, size_t len);
325 extern char *my_strdup(const char *str);
326 extern int my_snprintf(char *buf, size_t buflen, const char *fmt, ...);
327 extern int match_extension(const char *path, const char *ext_list);
332 extern void set_close_on_exec(int fd);
333 extern int set_non_blocking_mode(int fd);
334 extern int my_stat(const char *, struct stat *stp);
335 extern int my_open(const char *, int flags, int mode);
336 extern int my_remove(const char *);
337 extern int my_rename(const char *, const char *);
338 extern int my_mkdir(const char *, int);
339 extern char * my_getcwd(char *, int);
340 extern int spawn_process(struct conn *c, const char *prog,
341 char *envblk, char *envp[], int sock, const char *dir);
346 extern const struct io_class io_file;
347 extern const struct io_class io_socket;
348 extern const struct io_class io_ssl;
349 extern const struct io_class io_cgi;
350 extern const struct io_class io_dir;
351 extern const struct io_class io_embedded;
352 extern const struct io_class io_ssi;
354 extern int put_dir(const char *path);
355 extern void get_dir(struct conn *c);
356 extern void get_file(struct conn *c, struct stat *stp);
357 extern void ssl_handshake(struct stream *stream);
358 extern void setup_embedded_stream(struct conn *, union variant, void *);
359 extern struct registered_uri *is_registered_uri(struct shttpd_ctx *,
361 extern void do_ssi(struct conn *);
362 extern void ssi_func_destructor(struct llhead *lp);
367 extern int check_authorization(struct conn *c, const char *path);
368 extern int is_authorized_for_put(struct conn *c);
369 extern void send_authorization_request(struct conn *c);
370 extern int edit_passwords(const char *fname, const char *domain,
371 const char *user, const char *pass);
376 extern int run_cgi(struct conn *c, const char *prog);
377 extern void do_cgi(struct conn *c);
379 #define CGI_REPLY "HTTP/1.1 OK\r\n"
380 #define CGI_REPLY_LEN (sizeof(CGI_REPLY) - 1)
382 #endif /* DEFS_HEADER_DEFINED */