source: rtems/cpukit/shttpd/defs.h @ a81de3f

4.104.114.84.95
Last change on this file since a81de3f was a81de3f, checked in by Ralf Corsepius <ralf.corsepius@…>, on 06/12/07 at 07:06:16

Eliminate my_strdup, my_strndup.

  • Property mode set to 100644
File size: 12.6 KB
Line 
1/*
2 * Copyright (c) 2004-2005 Sergey Lyubka <valenok@gmail.com>
3 * All rights reserved
4 *
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.
9 */
10
11#ifndef DEFS_HEADER_DEFINED
12#define DEFS_HEADER_DEFINED
13
14#include "std_includes.h"
15#include "llist.h"
16#include "io.h"
17#include "shttpd.h"
18#include "md5.h"
19
20#define VERSION         "1.37"          /* Version                      */
21
22#ifndef CONFIG
23#define CONFIG          "shttpd.conf"   /* Configuration file           */
24#endif /* CONFIG */
25
26#define HTPASSWD        ".htpasswd"     /* Passwords file name          */
27#define DFLT_IO_SIZ     "16384"         /* Default max request size     */
28#define HTTP_PORT       "80"            /* Default listening port       */
29#define INDEX_FILES     "index.html index.htm index.php index.cgi"
30#define CGI_EXT         ".cgi .pl .php" /* Default CGI extensions       */
31#define REALM           "mydomain.com"  /* Default authentication realm */
32#define DELIM_CHARS     " ,"            /* Separators for lists         */
33
34#define EXPIRE_TIME     3600            /* Expiration time, seconds     */
35#define ENV_MAX         4096            /* Size of environment block    */
36#define CGI_ENV_VARS    64              /* Maximum vars passed to CGI   */
37#define URI_MAX         32768           /* Maximum URI size             */
38#define MIN_REQ_LEN     16              /* "GET / HTTP/1.1\n\n"         */
39
40#define NELEMS(ar)      (sizeof(ar) / sizeof(ar[0]))
41
42#ifdef _DEBUG
43#define DBG(x)  do { printf x ; putchar('\n'); fflush(stdout); } while (0)
44#else
45#define DBG(x)
46#endif /* DEBUG */
47
48#ifdef EMBEDDED
49#include "shttpd.h"
50#endif /* EMBEDDED */
51
52/*
53 * Darwin prior to 7.0 and Win32 do not have socklen_t
54 */
55#ifdef NO_SOCKLEN_T
56typedef int socklen_t;
57#endif /* NO_SOCKLEN_T */
58
59/*
60 * For parsing. This guy represents a substring.
61 */
62struct vec {
63        const char      *ptr;
64        int             len;
65};
66
67enum {METHOD_GET, METHOD_POST, METHOD_PUT, METHOD_DELETE, METHOD_HEAD};
68enum {HDR_DATE, HDR_INT, HDR_STRING};   /* HTTP header types            */
69enum {E_FATAL = 1, E_LOG = 2};          /* Flags for elog() function    */
70typedef unsigned long big_int_t;        /* Type for Content-Length      */
71       
72/*
73 * Unified socket address
74 */
75struct usa {
76        socklen_t len;
77        union {
78                struct sockaddr sa;
79                struct sockaddr_in sin;
80        } u;
81};
82
83/*
84 * This thing is aimed to hold values of any type.
85 * Used to store parsed headers' values.
86 */
87union variant {
88        char            *v_str;
89        int             v_int;
90        big_int_t       v_big_int;
91        time_t          v_time;
92        void            (*v_func)(void);
93        void            *v_void;
94        struct vec      v_vec;
95};
96
97/*
98 * This structure is used to hold mime types and associated file extensions.
99 */
100struct mime_type {
101        const char      *ext;
102        int             ext_len;
103        const char      *mime;
104};
105
106struct mime_type_link {
107        struct llhead   link;
108        char            *ext;
109        int             ext_len;
110        char            *mime;
111};
112
113/*
114 * This is used only in embedded configuration. This structure holds a
115 * registered URI, associated callback function with callback data.
116 * For non-embedded compilation shttpd_callback_t is not defined, so
117 * we use union variant to keep the compiler silent.
118 */
119struct registered_uri {
120        struct llhead   link;
121        const char      *uri;
122        union variant   callback;
123        void            *callback_data;
124};
125
126/*
127 * User may bind a passwords file to any URI. This makes that URI password
128 * protected: anybody who accesses that URI will be asked to authorize.
129 */
130struct uri_auth {
131        struct llhead   link;
132        const char      *uri;
133        const char      *file_name;
134        size_t          uri_len;
135};
136
137/*
138 * User may want to handle certain errors. This structure holds the
139 * handlers for corresponding error codes.
140 */
141struct error_handler {
142        struct llhead   link;
143        int             code;
144        union variant   callback;
145        void            *callback_data;
146};
147
148struct http_header {
149        int             len;            /* Header name length           */
150        int             type;           /* Header type                  */
151        size_t          offset;         /* Value placeholder            */
152        const char      *name;          /* Header name                  */
153};
154
155/*
156 * This guy holds parsed HTTP headers
157 */
158struct headers {
159        union variant   cl;             /* Content-Length:              */
160        union variant   ct;             /* Content-Type:                */
161        union variant   connection;     /* Connection:                  */
162        union variant   ims;            /* If-Modified-Since:           */
163        union variant   user;           /* Remote user name             */
164        union variant   auth;           /* Authorization                */
165        union variant   useragent;      /* User-Agent:                  */
166        union variant   referer;        /* Referer:                     */
167        union variant   cookie;         /* Cookie:                      */
168        union variant   location;       /* Location:                    */
169        union variant   range;          /* Range:                       */
170        union variant   status;         /* Status:                      */
171        union variant   transenc;       /* Transfer-Encoding:           */
172};
173
174/* Must go after union variant definition */
175#include "ssl.h"
176
177/*
178 * The communication channel
179 */
180union channel {
181        int             fd;             /* Regular static file          */
182        int             sock;           /* Connected socket             */
183        struct {
184                int             sock;   /* XXX important. must be first */
185                SSL             *ssl;   /* shttpd_poll() assumes that   */
186        } ssl;                          /* SSL-ed socket                */
187        struct {
188                DIR     *dirp;
189                char    *path;
190        } dir;                          /* Opened directory             */
191        struct {
192                void            *state; /* For keeping state            */
193                union variant   func;   /* User callback function       */
194                void            *data;  /* User defined parameters      */
195        } emb;                          /* Embedded, user callback      */
196};
197
198struct stream;
199
200/*
201 * IO class descriptor (file, directory, socket, SSL, CGI, etc)
202 * These classes are defined in io_*.c files.
203 */
204struct io_class {
205        const char *name;
206        int (*read)(struct stream *, void *buf, size_t len);
207        int (*write)(struct stream *, const void *buf, size_t len);
208        void (*close)(struct stream *);
209};
210
211/*
212 * Data exchange stream. It is backed by some communication channel:
213 * opened file, socket, etc. The 'read' and 'write' methods are
214 * determined by a communication channel.
215 */
216struct stream {
217        struct conn             *conn;
218        union channel           chan;           /* Descriptor           */
219        struct io               io;             /* IO buffer            */
220        const struct io_class   *io_class;      /* IO class             */
221        int                     nread_last;     /* Bytes last read      */
222        int                     headers_len;
223        big_int_t               content_len;
224        unsigned int            flags;
225#define FLAG_HEADERS_PARSED     1
226#define FLAG_SSL_ACCEPTED       2
227#define FLAG_R                  4               /* Can read in general  */
228#define FLAG_W                  8               /* Can write in general */
229#define FLAG_CLOSED             16
230#define FLAG_DONT_CLOSE         32
231#define FLAG_ALWAYS_READY       64              /* File, dir, user_func */
232};
233
234struct conn {
235        struct llhead   link;           /* Connections chain            */
236        struct shttpd_ctx *ctx;         /* Context this conn belongs to */
237        struct usa      sa;             /* Remote socket address        */
238        time_t          birth_time;     /* Creation time                */
239        time_t          expire_time;    /* Expiration time              */
240
241        int             status;         /* Reply status code            */
242        int             method;         /* Request method               */
243        char            *uri;           /* Decoded URI                  */
244        unsigned long   major_version;  /* Major HTTP version number    */
245        unsigned long   minor_version;  /* Minor HTTP version number    */
246        char            *request;       /* Request line                 */
247        char            *headers;       /* Request headers              */
248        char            *query;         /* QUERY_STRING part of the URI */
249        char            *path_info;     /* PATH_INFO thing              */
250        const char      *mime_type;     /* Mime type                    */
251
252        struct headers  ch;             /* Parsed client headers        */
253
254        struct stream   loc;            /* Local stream                 */
255        struct stream   rem;            /* Remote stream                */
256};
257
258
259/*
260 * SHTTPD context
261 */
262struct shttpd_ctx {
263        time_t          start_time;     /* Start time                   */
264        int             nactive;        /* # of connections now         */
265        unsigned long   nrequests;      /* Requests made                */
266        uint64_t        in, out;        /* IN/OUT traffic counters      */
267#if !defined(NO_SSL)
268        SSL_CTX         *ssl_ctx;       /* SSL context                  */
269#endif /* NO_SSL */
270        struct llhead   connections;    /* List of connections          */
271
272        struct llhead   mime_types;     /* Known mime types             */
273        struct llhead   registered_uris;/* User urls                    */
274        struct llhead   uri_auths;      /* User auth files              */
275        struct llhead   error_handlers; /* Embedded error handlers      */
276
277        FILE    *access_log;            /* Access log stream            */
278        FILE    *error_log;             /* Error log stream             */
279        char    *put_auth_file;         /* PUT auth file                */
280        char    *document_root;         /* Document root                */
281        char    *index_files;           /* Index files                  */
282        char    *aliases;               /* Aliases                      */
283        char    *mime_file;             /* Mime types file              */
284#if !defined(NO_CGI)
285        char    *cgi_vars;              /* CGI environment variables    */
286        char    *cgi_extensions;        /* CGI extensions               */
287        char    *cgi_interpreter;       /* CGI script interpreter       */
288#endif /* NO_CGI */
289        char    *auth_realm;            /* Auth realm                   */
290        char    *global_passwd_file;    /* Global passwords file        */
291        char    *uid;                   /* Run as user                  */
292        int     port;                   /* Listening port               */
293        int     dirlist;                /* Directory listing            */
294        int     gui;                    /* Show GUI flag                */
295        int     auto_start;             /* Start on OS boot             */
296        int     io_buf_size;            /* IO buffer size               */
297        int     inetd_mode;             /* Inetd flag                   */
298#if defined(_WIN32)
299        CRITICAL_SECTION mutex;         /* For MT case          */
300        HANDLE          ev[2];          /* For thread synchronization */
301#elif defined(__rtems__)
302        rtems_id         mutex;
303#endif /* _WIN32 */
304};
305
306struct listener {
307        struct llhead   link;
308        struct shttpd_ctx *ctx;         /* Context that socket belongs  */
309        int             sock;           /* Listening socket             */
310};
311
312/* Option setter function */
313typedef void (*optset_t)(struct shttpd_ctx *, void *ptr, const char *string);
314struct opt {
315        int             sw;             /* Command line switch          */
316        const char      *name;          /* Option name in config file   */
317        const char      *desc;          /* Description                  */
318        optset_t        setter;         /* Option setter function       */
319        size_t          ofs;            /* Value offset in context      */
320        const char      *arg;           /* Argument format              */
321        const char      *def;           /* Default option value         */
322        unsigned int    flags;          /* Flags                        */
323#define OPT_BOOL        1
324#define OPT_INT         2
325#define OPT_FILE        4
326#define OPT_DIR         8
327#define OPT_ADVANCED    16
328};
329
330extern const struct opt options[];
331
332/*
333 * In SHTTPD, list of values are represented as comma or space separated
334 * string. For example, list of CGI extensions can be represented as
335 * ".cgi,.php,.pl", or ".cgi .php .pl". The macro that follows allows to
336 * loop through the individual values in that list.
337 * A "const char *" pointer and size_t variable must be passed to the macro.
338 * Spaces or commas can be used as delimiters (macro DELIM_CHARS)
339 */
340#define FOR_EACH_WORD_IN_LIST(s,len)    \
341        for (; s != NULL && (len = strcspn(s, DELIM_CHARS)) != 0; s += len + 1)
342
343/*
344 * shttpd.c
345 */
346extern time_t           current_time;   /* Current UTC time             */
347extern int              tz_offset;      /* Offset from GMT time zone    */
348extern const struct vec known_http_methods[];
349
350extern void     stop_stream(struct stream *stream);
351extern void     decode_url_encoded_string(const char *, int, char *dst, int);
352extern void     send_server_error(struct conn *, int code, const char *reason);
353extern int      get_headers_len(const char *buf, size_t buflen);
354extern void     parse_headers(const char *s, int len, struct headers *parsed);
355
356/*
357 * mime_type.c
358 */
359extern const char *get_mime_type(struct shttpd_ctx *, const char *uri, int len);
360extern void     set_mime_types(struct shttpd_ctx *ctx, const char *path);
361
362/*
363 * config.c
364 */
365extern void     usage(const char *prog);
366extern struct shttpd_ctx *init_from_argc_argv(const char *, int, char *[]);
367
368/*
369 * log.c
370 */
371extern void     elog(int flags, struct conn *c, const char *fmt, ...);
372extern void     log_access(FILE *fp, const struct conn *c);
373
374/*
375 * string.c
376 */
377extern void     my_strlcpy(register char *, register const char *, size_t);
378extern int      my_strncasecmp(register const char *,
379                register const char *, size_t);
380#ifndef HAVE_STRNDUP
381extern char     *my_strndup(const char *ptr, size_t len);
382#else
383#include <string.h>
384#define my_strndup(x,l) strndup((x),(l))
385#endif
386
387#ifndef HAVE_STRDUP
388extern char     *my_strdup(const char *str);
389#else
390#include <string.h>
391#define my_strdup(x) strdup(x)
392#endif
393extern int      my_snprintf(char *buf, size_t buflen, const char *fmt, ...);
394
395/*
396 * compat_*.c
397 */
398extern void     set_close_on_exec(int fd);
399extern int      set_non_blocking_mode(int fd);
400extern int      my_stat(const char *, struct stat *stp);
401extern int      my_open(const char *, int flags, int mode);
402extern int      my_remove(const char *);
403extern int      my_rename(const char *, const char *);
404extern int      my_mkdir(const char *, int);
405extern char *   my_getcwd(char *, int);
406extern int      spawn_process(struct conn *c, const char *prog,
407                char *envblk, char *envp[], int sock, const char *dir);
408
409/*
410 * io_*.c
411 */
412extern const struct io_class    io_file;
413extern const struct io_class    io_socket;
414extern const struct io_class    io_ssl;
415extern const struct io_class    io_cgi;
416extern const struct io_class    io_dir;
417extern const struct io_class    io_embedded;
418
419extern int      put_dir(const char *path);
420extern void     get_dir(struct conn *c);
421extern void     get_file(struct conn *c, struct stat *stp);
422extern void     ssl_handshake(struct stream *stream);
423extern void     setup_embedded_stream(struct conn *, union variant, void *);
424extern struct registered_uri *is_registered_uri(struct shttpd_ctx *,
425                const char *uri);
426
427/*
428 * auth.c
429 */
430extern int      check_authorization(struct conn *c, const char *path);
431extern int      is_authorized_for_put(struct conn *c);
432extern void     send_authorization_request(struct conn *c);
433extern int      edit_passwords(const char *fname, const char *domain,
434                const char *user, const char *pass);
435
436/*
437 * cgi.c
438 */
439extern int      is_cgi(struct shttpd_ctx *ctx, const char *path);
440extern int      run_cgi(struct conn *c, const char *prog);
441extern void     do_cgi(struct conn *c);
442
443#define CGI_REPLY       "HTTP/1.1     OK\r\n"
444#define CGI_REPLY_LEN   (sizeof(CGI_REPLY) - 1)
445
446#endif /* DEFS_HEADER_DEFINED */
Note: See TracBrowser for help on using the repository browser.