PageRenderTime 55ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/cpukit/shttpd/defs.h

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