reprap: Progress
[simavr] / examples / shared / mongoose.h
1 // Copyright (c) 2004-2011 Sergey Lyubka\r
2 //\r
3 // Permission is hereby granted, free of charge, to any person obtaining a copy\r
4 // of this software and associated documentation files (the "Software"), to deal\r
5 // in the Software without restriction, including without limitation the rights\r
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
7 // copies of the Software, and to permit persons to whom the Software is\r
8 // furnished to do so, subject to the following conditions:\r
9 //\r
10 // The above copyright notice and this permission notice shall be included in\r
11 // all copies or substantial portions of the Software.\r
12 //\r
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
19 // THE SOFTWARE.\r
20 \r
21 #ifndef MONGOOSE_HEADER_INCLUDED\r
22 #define  MONGOOSE_HEADER_INCLUDED\r
23 \r
24 #include <stddef.h>\r
25 \r
26 #ifdef __cplusplus\r
27 extern "C" {\r
28 #endif // __cplusplus\r
29 \r
30 struct mg_context;     // Handle for the HTTP service itself\r
31 struct mg_connection;  // Handle for the individual connection\r
32 \r
33 \r
34 // This structure contains information about the HTTP request.\r
35 struct mg_request_info {\r
36   void *user_data;       // User-defined pointer passed to mg_start()\r
37   char *request_method;  // "GET", "POST", etc\r
38   char *uri;             // URL-decoded URI\r
39   char *http_version;    // E.g. "1.0", "1.1"\r
40   char *query_string;    // URL part after '?' (not including '?') or NULL\r
41   char *remote_user;     // Authenticated user, or NULL if no auth used\r
42   char *log_message;     // Mongoose error log message, MG_EVENT_LOG only\r
43   long remote_ip;        // Client's IP address\r
44   int remote_port;       // Client's port\r
45   int status_code;       // HTTP reply status code, e.g. 200\r
46   int is_ssl;            // 1 if SSL-ed, 0 if not\r
47   int num_headers;       // Number of headers\r
48   struct mg_header {\r
49     char *name;          // HTTP header name\r
50     char *value;         // HTTP header value\r
51   } http_headers[64];    // Maximum 64 headers\r
52 };\r
53 \r
54 // Various events on which user-defined function is called by Mongoose.\r
55 enum mg_event {\r
56   MG_NEW_REQUEST,   // New HTTP request has arrived from the client\r
57   MG_HTTP_ERROR,    // HTTP error must be returned to the client\r
58   MG_EVENT_LOG,     // Mongoose logs an event, request_info.log_message\r
59   MG_INIT_SSL,      // Mongoose initializes SSL. Instead of mg_connection *,\r
60                     // SSL context is passed to the callback function.\r
61   MG_REQUEST_COMPLETE  // Mongoose has finished handling the request\r
62 };\r
63 \r
64 // Prototype for the user-defined function. Mongoose calls this function\r
65 // on every MG_* event.\r
66 //\r
67 // Parameters:\r
68 //   event: which event has been triggered.\r
69 //   conn: opaque connection handler. Could be used to read, write data to the\r
70 //         client, etc. See functions below that have "mg_connection *" arg.\r
71 //   request_info: Information about HTTP request.\r
72 //\r
73 // Return:\r
74 //   If handler returns non-NULL, that means that handler has processed the\r
75 //   request by sending appropriate HTTP reply to the client. Mongoose treats\r
76 //   the request as served.\r
77 //   If handler returns NULL, that means that handler has not processed\r
78 //   the request. Handler must not send any data to the client in this case.\r
79 //   Mongoose proceeds with request handling as if nothing happened.\r
80 typedef void * (*mg_callback_t)(enum mg_event event,\r
81                                 struct mg_connection *conn,\r
82                                 const struct mg_request_info *request_info);\r
83 \r
84 \r
85 // Start web server.\r
86 //\r
87 // Parameters:\r
88 //   callback: user defined event handling function or NULL.\r
89 //   options: NULL terminated list of option_name, option_value pairs that\r
90 //            specify Mongoose configuration parameters.\r
91 //\r
92 // Side-effects: on UNIX, ignores SIGCHLD and SIGPIPE signals. If custom\r
93 //    processing is required for these, signal handlers must be set up\r
94 //    after calling mg_start().\r
95 //\r
96 //\r
97 // Example:\r
98 //   const char *options[] = {\r
99 //     "document_root", "/var/www",\r
100 //     "listening_ports", "80,443s",\r
101 //     NULL\r
102 //   };\r
103 //   struct mg_context *ctx = mg_start(&my_func, NULL, options);\r
104 //\r
105 // Please refer to http://code.google.com/p/mongoose/wiki/MongooseManual\r
106 // for the list of valid option and their possible values.\r
107 //\r
108 // Return:\r
109 //   web server context, or NULL on error.\r
110 struct mg_context *mg_start(mg_callback_t callback, void *user_data,\r
111                             const char **options);\r
112 \r
113 \r
114 // Stop the web server.\r
115 //\r
116 // Must be called last, when an application wants to stop the web server and\r
117 // release all associated resources. This function blocks until all Mongoose\r
118 // threads are stopped. Context pointer becomes invalid.\r
119 void mg_stop(struct mg_context *);\r
120 \r
121 \r
122 // Get the value of particular configuration parameter.\r
123 // The value returned is read-only. Mongoose does not allow changing\r
124 // configuration at run time.\r
125 // If given parameter name is not valid, NULL is returned. For valid\r
126 // names, return value is guaranteed to be non-NULL. If parameter is not\r
127 // set, zero-length string is returned.\r
128 const char *mg_get_option(const struct mg_context *ctx, const char *name);\r
129 \r
130 \r
131 // Return array of strings that represent valid configuration options.\r
132 // For each option, a short name, long name, and default value is returned.\r
133 // Array is NULL terminated.\r
134 const char **mg_get_valid_option_names(void);\r
135 \r
136 \r
137 // Add, edit or delete the entry in the passwords file.\r
138 //\r
139 // This function allows an application to manipulate .htpasswd files on the\r
140 // fly by adding, deleting and changing user records. This is one of the\r
141 // several ways of implementing authentication on the server side. For another,\r
142 // cookie-based way please refer to the examples/chat.c in the source tree.\r
143 //\r
144 // If password is not NULL, entry is added (or modified if already exists).\r
145 // If password is NULL, entry is deleted.\r
146 //\r
147 // Return:\r
148 //   1 on success, 0 on error.\r
149 int mg_modify_passwords_file(const char *passwords_file_name,\r
150                              const char *domain,\r
151                              const char *user,\r
152                              const char *password);\r
153 \r
154 // Send data to the client.\r
155 int mg_write(struct mg_connection *, const void *buf, size_t len);\r
156 \r
157 \r
158 // Send data to the browser using printf() semantics.\r
159 //\r
160 // Works exactly like mg_write(), but allows to do message formatting.\r
161 // Note that mg_printf() uses internal buffer of size IO_BUF_SIZE\r
162 // (8 Kb by default) as temporary message storage for formatting. Do not\r
163 // print data that is bigger than that, otherwise it will be truncated.\r
164 int mg_printf(struct mg_connection *, const char *fmt, ...)\r
165 #ifdef __GNUC__\r
166 __attribute__((format(printf, 2, 3)))\r
167 #endif\r
168 ;\r
169 \r
170 \r
171 // Send contents of the entire file together with HTTP headers.\r
172 void mg_send_file(struct mg_connection *conn, const char *path);\r
173 \r
174 \r
175 // Read data from the remote end, return number of bytes read.\r
176 int mg_read(struct mg_connection *, void *buf, size_t len);\r
177 \r
178 \r
179 // Get the value of particular HTTP header.\r
180 //\r
181 // This is a helper function. It traverses request_info->http_headers array,\r
182 // and if the header is present in the array, returns its value. If it is\r
183 // not present, NULL is returned.\r
184 const char *mg_get_header(const struct mg_connection *, const char *name);\r
185 \r
186 \r
187 // Get a value of particular form variable.\r
188 //\r
189 // Parameters:\r
190 //   data: pointer to form-uri-encoded buffer. This could be either POST data,\r
191 //         or request_info.query_string.\r
192 //   data_len: length of the encoded data.\r
193 //   var_name: variable name to decode from the buffer\r
194 //   buf: destination buffer for the decoded variable\r
195 //   buf_len: length of the destination buffer\r
196 //\r
197 // Return:\r
198 //   On success, length of the decoded variable.\r
199 //   On error, -1 (variable not found, or destination buffer is too small).\r
200 //\r
201 // Destination buffer is guaranteed to be '\0' - terminated. In case of\r
202 // failure, dst[0] == '\0'.\r
203 int mg_get_var(const char *data, size_t data_len,\r
204                const char *var_name, char *buf, size_t buf_len);\r
205 \r
206 // Fetch value of certain cookie variable into the destination buffer.\r
207 //\r
208 // Destination buffer is guaranteed to be '\0' - terminated. In case of\r
209 // failure, dst[0] == '\0'. Note that RFC allows many occurrences of the same\r
210 // parameter. This function returns only first occurrence.\r
211 //\r
212 // Return:\r
213 //   On success, value length.\r
214 //   On error, 0 (either "Cookie:" header is not present at all, or the\r
215 //   requested parameter is not found, or destination buffer is too small\r
216 //   to hold the value).\r
217 int mg_get_cookie(const struct mg_connection *,\r
218                   const char *cookie_name, char *buf, size_t buf_len);\r
219 \r
220 \r
221 // Return Mongoose version.\r
222 const char *mg_version(void);\r
223 \r
224 \r
225 // MD5 hash given strings.\r
226 // Buffer 'buf' must be 33 bytes long. Varargs is a NULL terminated list of\r
227 // asciiz strings. When function returns, buf will contain human-readable\r
228 // MD5 hash. Example:\r
229 //   char buf[33];\r
230 //   mg_md5(buf, "aa", "bb", NULL);\r
231 void mg_md5(char *buf, ...);\r
232 \r
233 \r
234 #ifdef __cplusplus\r
235 }\r
236 #endif // __cplusplus\r
237 \r
238 #endif // MONGOOSE_HEADER_INCLUDED\r