aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2006-12-23 12:39:05 +0000
committerChristian Grothoff <christian@grothoff.org>2006-12-23 12:39:05 +0000
commit845b05cc597431ee09c52c05c0678535ab0d48bb (patch)
tree799927ece43eb0e06c95d4bdd17d3625095c647f
parent96b2dcfc43eb89eeaec4689b7eb802370f6be741 (diff)
downloadlibmicrohttpd-845b05cc597431ee09c52c05c0678535ab0d48bb.tar.gz
libmicrohttpd-845b05cc597431ee09c52c05c0678535ab0d48bb.zip
miro
-rw-r--r--src/include/microhttpd.h431
1 files changed, 431 insertions, 0 deletions
diff --git a/src/include/microhttpd.h b/src/include/microhttpd.h
new file mode 100644
index 00000000..be6f4587
--- /dev/null
+++ b/src/include/microhttpd.h
@@ -0,0 +1,431 @@
1/*
2 This file is part of libmicrohttpd
3 (C) 2006 Christian Grothoff (and other contributing authors)
4
5 libmicrohttpd is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
9
10 libmicrohttpd is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with libmicrohttpd; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19*/
20
21/**
22 * @file microhttpd.h
23 * @brief public interface to libmicrohttpd
24 * @author Christian Grothoff
25 *
26 * All symbols defined in this header start with MHD. MHD is a
27 * micro-httpd library. As such, it does not have any API for logging
28 * errors.<p>
29 *
30 * All functions are guaranteed to be completely reentrant and
31 * thread-safe.<p>
32 *
33 * TODO:
34 * - Is it ok to treat POST data and GET-URI arguments (a=4&b=5) equally,
35 * or do we need an extra pair of methods?
36 * - We probably need a significantly more extensive API for
37 * proper SSL support (set local certificate, etc.)
38 */
39
40#ifndef MHD_MICROHTTPD_H
41#define MHD_MICROHTTPD_H
42
43#include <sys/types.h>
44#include <sys/select.h>
45#include <sys/socket.h>
46
47#ifdef __cplusplus
48extern "C" {
49#if 0 /* keep Emacsens' auto-indent happy */
50}
51#endif
52#endif
53
54#define MHD_VERSION 0x00000000
55
56#define MHD_YES 1
57
58#define MHD_NO 0
59
60/**
61 * Options for the MHD daemon. Note that if neither
62 * MHD_USER_THREAD_PER_CONNECTION nor MHD_USE_SELECT_INTERNALLY are
63 * used, the client wants control over the process and will call the
64 * appropriate microhttpd callbacks.<p>
65 *
66 * Note that it is legal to specify that both IPv4 and IPv6
67 * should be used. However, if neither IPv4 nor IPv6 is
68 * specified, starting the daemon will fail.<p>
69 *
70 * Starting the daemon may also fail if a particular option is not
71 * implemented or not supported on the target platform (i.e. no
72 * support for SSL, threads or IPv6).
73 */
74enum MHD_OPTION {
75 /**
76 * No options selected.
77 */
78 MHD_NO_OPTION = 0,
79
80 /**
81 * Run in debug mode. If this flag is used, the
82 * library should print error messages and warnings
83 * to stderr.
84 */
85 MHD_USE_DEBUG = 1,
86
87 /**
88 * Run in https mode.
89 */
90 MHD_USE_SSL = 2,
91
92 /**
93 * Run using one thread per connection.
94 */
95 MHD_USE_THREAD_PER_CONNECTION = 4,
96
97 /**
98 * Run using an internal thread doing SELECT.
99 */
100 MHD_USE_SELECT_INTERNALLY = 8,
101
102 /**
103 * Run using the IPv4 protocol
104 */
105 MHD_USE_IPv4 = 16,
106
107 /**
108 * Run using the IPv6 protocol
109 */
110 MHD_USE_IPv6 = 32,
111};
112
113struct MHD_Daemon;
114
115struct MHD_Session;
116
117struct MHD_Response;
118
119/**
120 * Allow or deny a client to connect.
121 *
122 *
123 * @param addr address information from the client
124 * @param addrlen length of the address information
125 * @return MHD_YES if connection is allowed, MHD_NO if not
126 */
127typedef int
128(*MHD_AcceptPolicyCallback)(void * cls,
129 const struct sockaddr * addr,
130 socklen_t addrlen);
131
132/**
133 * A client has requested the given url using the given method ("GET",
134 * "PUT" or "POST"). The callback must call MHS callbacks to provide
135 * content to give back to the client and return an HTTP status code
136 * (i.e. 200 for OK, 404, etc.).
137 *
138 * @return MHS_YES if the connection was handled successfully,
139 * MHS_NO if the socket must be closed due to a serios
140 * error while handling the request
141 */
142typedef int
143(*MHD_AccessHandlerCallback)(void * cls,
144 struct MHD_Session * session,
145 const char * url,
146 const char * method);
147
148/**
149 * Iterator over key-value pairs. This iterator
150 * can be used to iterate over all of the cookies,
151 * headers, or POST-data fields of a request, and
152 * also to iterate over the headers that have been
153 * added to a response.
154 *
155 * @return MHD_YES to continue iterating,
156 * MHD_NO to abort the iteration
157 */
158typedef int
159(*MHD_KeyValueIterator)(void * cls,
160 const char * key,
161 const char * value);
162
163/**
164 * Callback used by libmicrohttpd in order to obtain content. The
165 * callback is to copy at most "max" bytes of content into "buf". The
166 * total number of bytes that has been placed into "buf" should be
167 * returned.<p>
168 *
169 * Note that returning zero will cause libmicrohttpd to try again,
170 * either "immediately" if in multi-threaded mode (in which case the
171 * callback may want to do blocking operations) or in the next round
172 * if MHD_run is used. Returning 0 for a daemon that runs in internal
173 * select mode is an error (since it would result in busy waiting) and
174 * will cause the program to be aborted (abort()).
175 *
176 * @param cls extra argument to the callback
177 * @param pos position in the datastream to access;
178 * note that if an MHD_Response object is re-used,
179 * it is possible for the same content reader to
180 * be queried multiple times for the same data;
181 * however, if an MHD_Response is not re-used,
182 * libmicrohttpd guarantees that "pos" will be
183 * the sum of all non-negative return values
184 * obtained from the content reader so far.
185 * @return -1 on error (libmicrohttpd will no longer
186 * try to read content and instead close the connection
187 * with the client).
188 */
189typedef int
190(*MHD_ContentReaderCallback)(void * cls,
191 size_t pos,
192 char * buf,
193 int max);
194
195/**
196 * This method is called by libmicrohttpd if we
197 * are done with a content reader. It should
198 * be used to free resources associated with the
199 * content reader.
200 */
201typedef void
202(*MHD_ContentReaderFreeCallback)(void * cls);
203
204/**
205 * Start a webserver on the given port.
206 * @param port port to bind to
207 * @param apc callback to call to check which clients
208 * will be allowed to connect
209 * @param apc_cls extra argument to apc
210 * @param dh default handler for all URIs
211 * @param dh_cls extra argument to dh
212 * @return NULL on error, handle to daemon on success
213 */
214struct MHD_Daemon *
215MHD_start_daemon(unsigned int options,
216 unsigned short port,
217 MHD_AcceptPolicyCallback apc,
218 void * apc_cls,
219 MHD_AccessHandlerCallback dh,
220 void * dh_cls);
221
222
223
224/**
225 * Shutdown an http daemon.
226 */
227void
228MHD_stop_daemon(struct MHD_Daemon * daemon);
229
230
231/**
232 * Obtain the select sets for this daemon.
233 *
234 * @return MHD_YES on success, MHD_NO if this
235 * daemon was not started with the right
236 * options for this call.
237 */
238int
239MHD_get_fdset(struct MHD_Daemon * daemon,
240 fd_set * read_fd_set,
241 fd_set * write_fd_set,
242 fd_set * exc_fd_set,
243 int * max_fd);
244
245/**
246 * Run webserver operations (without blocking unless
247 * in client callbacks). This method should be called
248 * by clients in combination with MHD_get_fdset
249 * if the client-controlled select method is used.
250 *
251 * @return MHD_YES on success, MHD_NO if this
252 * daemon was not started with the right
253 * options for this call.
254 */
255int
256MHD_run(struct MHD_Daemon * daemon);
257
258
259/**
260 * Register an access handler for all URIs beginning with uri_prefix.
261 *
262 * @param uri_prefix
263 * @return MRI_NO if a handler for this exact prefix
264 * already exists
265 */
266int
267MHD_register_handler(struct MHD_Daemon * daemon,
268 const char * uri_prefix,
269 MHD_AccessHandlerCallback dh,
270 void * dh_cls);
271
272/**
273 * Unregister an access handler for the URIs beginning with
274 * uri_prefix.
275 *
276 * @param uri_prefix
277 * @return MHD_NO if a handler for this exact prefix
278 * is not known for this daemon
279 */
280 */
281int
282MHD_unregister_handler(struct MHD_Daemon * daemon,
283 const char * uri_prefix,
284 MHD_AccessHandlerCallback dh,
285 void * dh_cls);
286
287/**
288 * Get all of the headers from the request.
289 *
290 * @param iterator callback to call on each header;
291 * maybe NULL (then just count headers)
292 * @param iterator_cls extra argument to iterator
293 * @return number of entries iterated over
294 */
295int
296MHD_get_session_headers(struct MHD_Session * session,
297 MHD_KeyValueIterator * iterator,
298 void * iterator_cls);
299
300/**
301 * Get a particular header value.
302 *
303 * @param key the header to look for
304 * @return NULL if no such item was found
305 */
306const char *
307MHD_lookup_session_header(struct MHD_Session * session,
308 const char * key);
309
310/**
311 * Get all of the form fields from POST.
312 *
313 * @param iterator callback to call on each header;
314 * maybe NULL (then just count headers)
315 * @param iterator_cls extra argument to iterator
316 * @return number of entries iterated over
317 */
318int
319MHD_get_post_items(struct MHD_Session * session,
320 MHD_KeyValueIterator * iterator,
321 void * iterator_cls);
322
323/**
324 * Get a particular form field from POST.
325 *
326 * @param key the field to look for
327 * @return NULL if no such item was found
328 */
329const char *
330MHD_lookup_post_item(struct MHD_Session * session,
331 const char * key);
332
333/**
334 * Queue a response to be transmitted to the client (as soon as
335 * possible).
336 *
337 * @param session the session identifying the client
338 * @param status_code HTTP status code (i.e. 200 for OK)
339 * @param response response to transmit
340 * @return MHD_NO on error (i.e. reply already sent),
341 * MHD_YES on success or if message has been queued
342 */
343int
344MHD_queue_response(struct MHD_Session * session,
345 unsigned int status_code,
346 struct MHD_Response * response);
347
348/**
349 * Create a response object. The response object can be extended with
350 * header information and then be used any number of times.
351 *
352 * @param size size of the data portion of the response, -1 for unknown
353 * @param crc callback to use to obtain response data
354 * @param crc_cls extra argument to crc
355 * @param crfc callback to call to free crc_cls resources
356 * @return NULL on error (i.e. invalid arguments, out of memory)
357 */
358struct MHD_Response *
359MHD_create_response_from_callback(size_t size,
360 MHD_ContentReaderCallback crc,
361 void * crc_cls,
362 MHD_ContentReaderFreeCallback crfc);
363
364/**
365 * Create a response object. The response object can be extended with
366 * header information and then be used any number of times.
367 *
368 * @param size size of the data portion of the response
369 * @param data the data itself
370 * @param must_free libmicrohttpd should free data when done
371 * @param must_copy libmicrohttpd must make a copy of data
372 * right away, the data maybe released anytime after
373 * this call returns
374 * @return NULL on error (i.e. invalid arguments, out of memory)
375 */
376struct MHD_Response *
377MHD_create_response_from_data(size_t size,
378 void * data,
379 int must_free,
380 int must_copy);
381
382/**
383 * Destroy a response object and associated resources. Note that
384 * libmicrohttpd may keep some of the resources around if the response
385 * is still in the queue for some clients, so the memory may not
386 * necessarily be freed immediatley.
387 */
388void
389MHD_destroy_response(struct MHD_Response * response);
390
391/**
392 * Add a header line to the response.
393 *
394 * @return MHD_NO on error (i.e. invalid header or content format).
395 */
396int
397MHD_add_response_header(struct MHD_Response * response,
398 const char * header,
399 const char * content);
400
401/**
402 * Delete a header line from the response.
403 *
404 * @return MHD_NO on error (no such header known)
405 */
406int
407MHD_del_response_header(struct MHD_Response * response,
408 const char * header,
409 const char * content);
410
411/**
412 * Get all of the headers added to a response.
413 *
414 * @param iterator callback to call on each header;
415 * maybe NULL (then just count headers)
416 * @param iterator_cls extra argument to iterator
417 * @return number of entries iterated over
418 */
419int
420MHD_get_response_headers(struct MHD_Response * response,
421 MHD_KeyValueIterator * iterator,
422 void * iterator_cls);
423
424#if 0 /* keep Emacsens' auto-indent happy */
425{
426#endif
427#ifdef __cplusplus
428}
429#endif
430
431#endif