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 current_time; /* Current UTC time */
300
extern int tz_offset; /* Offset from GMT time zone */
301
extern const struct vec known_http_methods[];
303
extern void stop_stream(struct stream *stream);
304
extern int url_decode(const char *, int, char *dst, int);
305
extern void send_server_error(struct conn *, int code, const char *reason);
306
extern int get_headers_len(const char *buf, size_t buflen);
307
extern void parse_headers(const char *s, int len, struct headers *parsed);
308
extern int is_true(const char *str);
309
extern int shttpd_socketpair(int pair[2]);
310
extern void get_mime_type(struct shttpd_ctx *, const char *, int, struct vec *);
312
#define IS_TRUE(ctx, opt) is_true((ctx)->options[opt])
317
extern void usage(const char *prog);
322
extern void elog(int flags, struct conn *c, const char *fmt, ...);
323
extern void log_access(FILE *fp, const struct conn *c);
328
extern void my_strlcpy(register char *, register const char *, size_t);
329
extern int my_strncasecmp(register const char *,
330
register const char *, size_t);
331
extern char *my_strndup(const char *ptr, size_t len);
332
extern char *my_strdup(const char *str);
333
extern int my_snprintf(char *buf, size_t buflen, const char *fmt, ...);
334
extern int match_extension(const char *path, const char *ext_list);
339
extern void set_close_on_exec(int fd);
340
extern int set_non_blocking_mode(int fd);
341
extern int my_stat(const char *, struct stat *stp);
342
extern int my_open(const char *, int flags, int mode);
343
extern int my_remove(const char *);
344
extern int my_rename(const char *, const char *);
345
extern int my_mkdir(const char *, int);
346
extern char * my_getcwd(char *, int);
347
extern int spawn_process(struct conn *c, const char *prog,
348
char *envblk, char *envp[], int sock, const char *dir);
350
extern void set_nt_service(struct shttpd_ctx *, const char *);
351
extern void set_systray(struct shttpd_ctx *, const char *);
352
extern void try_to_run_as_nt_service(void);
357
extern const struct io_class io_file;
358
extern const struct io_class io_socket;
359
extern const struct io_class io_ssl;
360
extern const struct io_class io_cgi;
361
extern const struct io_class io_dir;
362
extern const struct io_class io_embedded;
363
extern const struct io_class io_ssi;
365
extern int put_dir(const char *path);
366
extern void get_dir(struct conn *c);
367
extern void get_file(struct conn *c, struct stat *stp);
368
extern void ssl_handshake(struct stream *stream);
369
extern void setup_embedded_stream(struct conn *, union variant, void *);
370
extern struct registered_uri *is_registered_uri(struct shttpd_ctx *,
372
extern void do_ssi(struct conn *);
373
extern void ssi_func_destructor(struct llhead *lp);
378
extern int check_authorization(struct conn *c, const char *path);
379
extern int is_authorized_for_put(struct conn *c);
380
extern void send_authorization_request(struct conn *c);
381
extern int edit_passwords(const char *fname, const char *domain,
382
const char *user, const char *pass);
387
extern int run_cgi(struct conn *c, const char *prog);
388
extern void do_cgi(struct conn *c);
390
#define CGI_REPLY "HTTP/1.1 OK\r\n"
391
#define CGI_REPLY_LEN (sizeof(CGI_REPLY) - 1)
393
#endif /* DEFS_HEADER_DEFINED */