1 | /* |
---|
2 | * Copyright (c) 2004-2005 Sergey Lyubka <valenok@gmail.com> |
---|
3 | * |
---|
4 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
5 | * copy of this software and associated documentation files (the "Software"), |
---|
6 | * to deal in the Software without restriction, including without limitation |
---|
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
8 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
9 | * Software is furnished to do so, subject to the following conditions: |
---|
10 | * |
---|
11 | * The above copyright notice and this permission notice shall be included |
---|
12 | * in all copies or substantial portions of the Software. |
---|
13 | * |
---|
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
20 | * DEALINGS IN THE SOFTWARE. |
---|
21 | * |
---|
22 | * $Id$ |
---|
23 | */ |
---|
24 | |
---|
25 | #ifndef SHTTPD_HEADER_INCLUDED |
---|
26 | #define SHTTPD_HEADER_INCLUDED |
---|
27 | |
---|
28 | #ifdef __cplusplus |
---|
29 | extern "C" { |
---|
30 | #endif /* __cplusplus */ |
---|
31 | |
---|
32 | /* |
---|
33 | * This structure is passed to the user callback function |
---|
34 | */ |
---|
35 | struct shttpd_arg { |
---|
36 | void *priv; /* Private! Do not touch! */ |
---|
37 | void *state; /* User state */ |
---|
38 | void *user_data; /* User-defined data */ |
---|
39 | |
---|
40 | struct { |
---|
41 | char *buf; /* Buffer pointer */ |
---|
42 | int len; /* Size of a buffer */ |
---|
43 | int num_bytes; /* Bytes processed by callback */ |
---|
44 | } in, out; /* POST data buffer (in) and output buffer (out) */ |
---|
45 | |
---|
46 | unsigned int flags; |
---|
47 | #define SHTTPD_END_OF_OUTPUT 1 |
---|
48 | #define SHTTPD_CONNECTION_ERROR 2 |
---|
49 | #define SHTTPD_MORE_POST_DATA 4 |
---|
50 | #define SHTTPD_POST_BUFFER_FULL 8 |
---|
51 | }; |
---|
52 | |
---|
53 | /* |
---|
54 | * User callback function. Called when certain registered URLs have been |
---|
55 | * requested. These are the requirements to the callback function: |
---|
56 | * |
---|
57 | * 1. it must copy data into 'out_buf', not more than 'out_buf_max_len' bytes, |
---|
58 | * and record how many bytes are copied, into 'out_buf_written_len' |
---|
59 | * 2. it must not block the execution |
---|
60 | * 3. it must set 'last' in shttpd_arg to 1 if there is no more data |
---|
61 | * 4. for POST requests, it must process the incoming data (in_buf), and |
---|
62 | * set 'in_buf_read_len', which is how many bytes of POST data is read |
---|
63 | * and can be discarded by SHTTPD. |
---|
64 | */ |
---|
65 | typedef void (*shttpd_callback_t)(struct shttpd_arg *); |
---|
66 | |
---|
67 | /* |
---|
68 | * shttpd_init Initialize shttpd context. Parameters: configuration |
---|
69 | * file name (may be NULL), then NULL-terminated |
---|
70 | * sequence of pairs "option_name", "option_value". |
---|
71 | * shttpd_init2 Same as shttpd_init, but the list of option/value |
---|
72 | * pairs is passed in arrays |
---|
73 | * shttpd_fini Dealocate the context |
---|
74 | * shttpd_register_uri Setup the callback function for specified URL. |
---|
75 | * shttpd_protect_uri Associate authorization file with an URL. |
---|
76 | * shttpd_add_mime_type Add mime type |
---|
77 | * shtppd_listen Setup a listening socket in the SHTTPD context |
---|
78 | * shttpd_poll Do connections processing |
---|
79 | * shttpd_version return string with SHTTPD version |
---|
80 | * shttpd_get_var Return POST/GET variable value for given variable name. |
---|
81 | * shttpd_get_header return value of the specified HTTP header |
---|
82 | * shttpd_get_env return string values for the following |
---|
83 | * pseudo-variables: "REQUEST_METHOD", "REQUEST_URI", |
---|
84 | * "REMOTE_USER" and "REMOTE_ADDR". |
---|
85 | */ |
---|
86 | |
---|
87 | struct shttpd_ctx; |
---|
88 | |
---|
89 | struct shttpd_ctx *shttpd_init(const char *config_file, ...); |
---|
90 | struct shttpd_ctx *shttpd_init2(const char *config_file, |
---|
91 | char *names[], char *values[], size_t num_options); |
---|
92 | void shttpd_fini(struct shttpd_ctx *); |
---|
93 | void shttpd_add_mime_type(struct shttpd_ctx *, |
---|
94 | const char *ext, const char *mime); |
---|
95 | int shttpd_listen(struct shttpd_ctx *ctx, int port); |
---|
96 | void shttpd_register_uri(struct shttpd_ctx *ctx, |
---|
97 | const char *uri, shttpd_callback_t callback, void *user_data); |
---|
98 | void shttpd_protect_uri(struct shttpd_ctx *ctx, |
---|
99 | const char *uri, const char *file); |
---|
100 | void shttpd_poll(struct shttpd_ctx *, int milliseconds); |
---|
101 | const char *shttpd_version(void); |
---|
102 | int shttpd_get_var(const char *var, const char *buf, int buf_len, |
---|
103 | char *value, int value_len); |
---|
104 | const char *shttpd_get_header(struct shttpd_arg *, const char *); |
---|
105 | const char *shttpd_get_env(struct shttpd_arg *, const char *); |
---|
106 | void shttpd_get_http_version(struct shttpd_arg *, |
---|
107 | unsigned long *major, unsigned long *minor); |
---|
108 | size_t shttpd_printf(struct shttpd_arg *, const char *fmt, ...); |
---|
109 | void shttpd_handle_error(struct shttpd_ctx *ctx, int status, |
---|
110 | shttpd_callback_t func, void *data); |
---|
111 | |
---|
112 | /* |
---|
113 | * The following three functions are for applications that need to |
---|
114 | * load-balance the connections on their own. Many threads may be spawned |
---|
115 | * with one SHTTPD context per thread. Boss thread may only wait for |
---|
116 | * new connections by means of shttpd_accept(). Then it may scan thread |
---|
117 | * pool for the idle thread by means of shttpd_active(), and add new |
---|
118 | * connection to the context by means of shttpd_add(). |
---|
119 | */ |
---|
120 | void shttpd_add_socket(struct shttpd_ctx *, int sock); |
---|
121 | int shttpd_accept(int lsn_sock, int milliseconds); |
---|
122 | int shttpd_active(struct shttpd_ctx *); |
---|
123 | |
---|
124 | |
---|
125 | #ifdef __cplusplus |
---|
126 | } |
---|
127 | #endif /* __cplusplus */ |
---|
128 | |
---|
129 | #endif /* SHTTPD_HEADER_INCLUDED */ |
---|