version 0.2.1
[fms.git] / libs / shttpd / defs.h
index 9c606f4..bfcb119 100644 (file)
 #include "io.h"
 #include "shttpd.h"
 #include "md5.h"
-
-#define        VERSION         "1.38"          /* Version                      */
-
-#ifndef CONFIG
-#define        CONFIG          "shttpd.conf"   /* Configuration file           */
-#endif /* CONFIG */
-
-#define        HTPASSWD        ".htpasswd"     /* Passwords file name          */
-#define        DFLT_IO_SIZ     "16384"         /* Default max request size     */
-#define        LISTENING_PORTS "80"            /* Default listening ports      */
-#define        INDEX_FILES     "index.html index.htm index.php index.cgi"
-#define        CGI_EXT         ".cgi .pl .php" /* Default CGI extensions       */
-#define        SSI_EXT         ".shtml .shtm"  /* Default SSI extensions       */
-#define        REALM           "mydomain.com"  /* Default authentication realm */
-#define        DELIM_CHARS     " ,"            /* Separators for lists         */
-
-#define        EXPIRE_TIME     3600            /* Expiration time, seconds     */
-#define        ENV_MAX         4096            /* Size of environment block    */
-#define        CGI_ENV_VARS    64              /* Maximum vars passed to CGI   */
-#define        URI_MAX         32768           /* Maximum URI size             */
-#define        MIN_REQ_LEN     16              /* "GET / HTTP/1.1\n\n"         */
+#include "config.h"
 
 #define        NELEMS(ar)      (sizeof(ar) / sizeof(ar[0]))
 
@@ -65,6 +45,9 @@ struct vec {
        int             len;
 };
 
+#if !defined(_WIN32)
+enum {FALSE, TRUE};
+#endif /* _WIN32 */
 enum {METHOD_GET, METHOD_POST, METHOD_PUT, METHOD_DELETE, METHOD_HEAD};
 enum {HDR_DATE, HDR_INT, HDR_STRING};  /* HTTP header types            */
 enum {E_FATAL = 1, E_LOG = 2};         /* Flags for elog() function    */
@@ -96,22 +79,6 @@ union variant {
 };
 
 /*
- * This structure is used to hold mime types and associated file extensions.
- */
-struct mime_type {
-       const char      *ext;
-       int             ext_len;
-       const char      *mime;
-};
-
-struct mime_type_link {
-       struct llhead   link;
-       char            *ext;
-       int             ext_len;
-       char            *mime;
-};
-
-/*
  * This is used only in embedded configuration. This structure holds a
  * registered URI, associated callback function with callback data.
  * For non-embedded compilation shttpd_callback_t is not defined, so
@@ -125,17 +92,6 @@ struct registered_uri {
 };
 
 /*
- * User may bind a passwords file to any URI. This makes that URI password
- * protected: anybody who accesses that URI will be asked to authorize.
- */
-struct uri_auth {
-       struct llhead   link;
-       const char      *uri;
-       const char      *file_name;
-       size_t          uri_len;
-};
-
-/*
  * User may want to handle certain errors. This structure holds the
  * handlers for corresponding error codes.
  */
@@ -249,7 +205,7 @@ struct conn {
        char            *headers;       /* Request headers              */
        char            *query;         /* QUERY_STRING part of the URI */
        char            *path_info;     /* PATH_INFO thing              */
-       const char      *mime_type;     /* Mime type                    */
+       struct vec      mime_type;      /* Mime type                    */
 
        struct headers  ch;             /* Parsed client headers        */
 
@@ -261,6 +217,15 @@ struct conn {
 #endif /* NO_SSI */
 };
 
+enum {
+       OPT_ROOT, OPT_INDEX_FILES, OPT_PORTS, OPT_DIR_LIST,
+       OPT_CGI_EXTENSIONS, OPT_CGI_INTERPRETER, OPT_CGI_ENVIRONMENT,
+       OPT_SSI_EXTENSIONS, OPT_AUTH_REALM, OPT_AUTH_GPASSWD,
+       OPT_AUTH_PUT, OPT_ACCESS_LOG, OPT_ERROR_LOG, OPT_MIME_TYPES,
+       OPT_SSL_CERTIFICATE, OPT_ALIASES, OPT_ACL, OPT_INETD, OPT_UID,
+       OPT_CFG_URI, OPT_PROTECT,
+       NUM_OPTIONS
+};
 
 /*
  * SHTTPD context
@@ -273,36 +238,17 @@ struct shttpd_ctx {
        SSL_CTX         *ssl_ctx;       /* SSL context                  */
        struct llhead   connections;    /* List of connections          */
 
-       struct llhead   mime_types;     /* Known mime types             */
        struct llhead   registered_uris;/* User urls                    */
-       struct llhead   uri_auths;      /* User auth files              */
        struct llhead   error_handlers; /* Embedded error handlers      */
+       struct llhead   acl;            /* Access control list          */
+       struct llhead   ssi_funcs;      /* SSI callback functions       */
+       struct llhead   listeners;      /* Listening sockets            */
 
        FILE    *access_log;            /* Access log stream            */
        FILE    *error_log;             /* Error log stream             */
-       char    *put_auth_file;         /* PUT auth file                */
-       char    *document_root;         /* Document root                */
-       char    *index_files;           /* Index files                  */
-       char    *aliases;               /* Aliases                      */
-       char    *mime_file;             /* Mime types file              */
-#if !defined(NO_CGI)
-       char    *cgi_vars;              /* CGI environment variables    */
-       char    *cgi_extensions;        /* CGI extensions               */
-       char    *cgi_interpreter;       /* CGI script interpreter       */
-#endif /* NO_CGI */
-#if !defined(NO_SSI)
-       char    *ssi_extensions;        /* SSI file extensions          */
-       struct llhead   ssi_funcs;      /* SSI callback functions       */
-#endif /* NO_SSI */
-       char    *auth_realm;            /* Auth realm                   */
-       char    *global_passwd_file;    /* Global passwords file        */
-       char    *uid;                   /* Run as user                  */
-       char    *ports;                 /* Listening ports              */
-       int     dirlist;                /* Directory listing            */
-       int     gui;                    /* Show GUI flag                */
-       int     auto_start;             /* Start on OS boot             */
-       int     io_buf_size;            /* IO buffer size               */
-       int     inetd_mode;             /* Inetd flag                   */
+
+       char    *options[NUM_OPTIONS];  /* Configurable options         */
+
 #if defined(_WIN32)
        CRITICAL_SECTION mutex;         /* For MT case                  */
        HANDLE          ev[2];          /* For thread synchronization */
@@ -311,36 +257,34 @@ struct shttpd_ctx {
 #endif /* _WIN32 */
 };
 
-/* Option setter function */
-typedef void (*optset_t)(struct shttpd_ctx *, void *ptr, const char *string);
-struct opt {
-       int             sw;             /* Command line switch          */
-       const char      *name;          /* Option name in config file   */
-       const char      *desc;          /* Description                  */
-       optset_t        setter;         /* Option setter function       */
-       size_t          ofs;            /* Value offset in context      */
-       const char      *arg;           /* Argument format              */
-       const char      *def;           /* Default option value         */
-       unsigned int    flags;          /* Flags                        */
-#define        OPT_BOOL        1
-#define        OPT_INT         2
-#define        OPT_FILE        4
-#define        OPT_DIR         8
-#define        OPT_ADVANCED    16
-};
-
-extern const struct opt options[];
+#define IS_TRUE(ctx, opt) ((ctx)->options[opt] && (ctx)->options[opt][0] =='1')
 
 /*
  * In SHTTPD, list of values are represented as comma or space separated
  * string. For example, list of CGI extensions can be represented as
  * ".cgi,.php,.pl", or ".cgi .php .pl". The macro that follows allows to
  * loop through the individual values in that list.
+ *
  * A "const char *" pointer and size_t variable must be passed to the macro.
- * Spaces or commas can be used as delimiters (macro DELIM_CHARS)
+ * Spaces or commas can be used as delimiters (macro DELIM_CHARS).
+ *
+ * In every iteration of the loop, "s" points to the current value, and
+ * "len" specifies its length. The code inside loop must not change
+ * "s" and "len" parameters.
  */
-#define        FOR_EACH_WORD_IN_LIST(s,len)    \
-       for (; s != NULL && (len = strcspn(s, DELIM_CHARS)) != 0; s += len + 1)
+#define        FOR_EACH_WORD_IN_LIST(s,len)                                    \
+       for (; s != NULL && (len = strcspn(s, DELIM_CHARS)) != 0;       \
+                       s += len, s+= strspn(s, DELIM_CHARS))
+
+/*
+ * IPv4 ACL entry. Specifies subnet with deny/allow flag
+ */
+struct acl {
+       struct llhead   link;
+       uint32_t        ip;             /* IP, in network byte order    */
+       uint32_t        mask;           /* Also in network byte order   */
+       int             flag;           /* Either '+' or '-'            */
+};
 
 /*
  * shttpd.c
@@ -355,18 +299,15 @@ extern void       send_server_error(struct conn *, int code, const char *reason);
 extern int     get_headers_len(const char *buf, size_t buflen);
 extern void    parse_headers(const char *s, int len, struct headers *parsed);
 extern void    open_listening_ports(struct shttpd_ctx *ctx);
-
-/*
- * mime_type.c
- */
-extern const char *get_mime_type(struct shttpd_ctx *, const char *uri, int len);
-extern void    set_mime_types(struct shttpd_ctx *ctx, const char *path);
+extern void get_mime_type(struct shttpd_ctx *, const char *, int, struct vec *);
+extern void    free_list(struct llhead *head, void (*)(struct llhead *));
+extern void    registered_uri_destructor(struct llhead *);
+extern void    listener_destructor(struct llhead *);
 
 /*
  * config.c
  */
 extern void    usage(const char *prog);
-extern struct shttpd_ctx *init_from_argc_argv(const char *, int, char *[]);
 
 /*
  * log.c
@@ -418,7 +359,7 @@ extern void setup_embedded_stream(struct conn *, union variant, void *);
 extern struct registered_uri *is_registered_uri(struct shttpd_ctx *,
                const char *uri);
 extern void    do_ssi(struct conn *);
-extern void    free_ssi_funcs(struct shttpd_ctx *ctx);
+extern void    ssi_func_destructor(struct llhead *lp);
 
 /*
  * auth.c