diff options
Diffstat (limited to 'doc/microhttpd.texi')
-rw-r--r-- | doc/microhttpd.texi | 1198 |
1 files changed, 1198 insertions, 0 deletions
diff --git a/doc/microhttpd.texi b/doc/microhttpd.texi new file mode 100644 index 00000000..e9b63779 --- /dev/null +++ b/doc/microhttpd.texi | |||
@@ -0,0 +1,1198 @@ | |||
1 | @setfilename microhttpd.info | ||
2 | |||
3 | @macro gnu{} | ||
4 | @acronym{GNU} | ||
5 | @end macro | ||
6 | |||
7 | @macro gpl{} | ||
8 | @acronym{GPL} | ||
9 | @end macro | ||
10 | |||
11 | @macro http{} | ||
12 | @acronym{HTTP} | ||
13 | @end macro | ||
14 | |||
15 | @macro tcp{} | ||
16 | @acronym{TCP} | ||
17 | @end macro | ||
18 | |||
19 | @macro api{} | ||
20 | @acronym{API} | ||
21 | @end macro | ||
22 | |||
23 | @macro urloc{} | ||
24 | @acronym{URL} | ||
25 | @end macro | ||
26 | |||
27 | @macro uri{} | ||
28 | @acronym{URI} | ||
29 | @end macro | ||
30 | |||
31 | @macro ascii{} | ||
32 | @acronym{ASCII} | ||
33 | @end macro | ||
34 | |||
35 | @c ............................................................ | ||
36 | |||
37 | @macro cfunction{NAME} | ||
38 | @code{\NAME\()} | ||
39 | @end macro | ||
40 | |||
41 | @macro null{} | ||
42 | @code{NULL} | ||
43 | @end macro | ||
44 | |||
45 | @c ............................................................ | ||
46 | |||
47 | @macro glibcref{NODE, NODE} | ||
48 | @pxref{\NODE\, \NODE\, \NODE\, libc} | ||
49 | @end macro | ||
50 | |||
51 | @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
52 | |||
53 | |||
54 | @c ------------------------------------------------------------ | ||
55 | @node microhttpd | ||
56 | @appendix Libmicrohttpd documentation | ||
57 | |||
58 | |||
59 | @macro mhd{} | ||
60 | @acronym{MHD} | ||
61 | @end macro | ||
62 | |||
63 | |||
64 | @noindent | ||
65 | This appendix documents Libmicrohttpd version 0.1.2. It is built upon | ||
66 | the documentation in the header file @file{microhttpd.h}. | ||
67 | |||
68 | @menu | ||
69 | * microhttpd intro:: Introduction. | ||
70 | * microhttpd const:: Constants. | ||
71 | * microhttpd struct:: Structures type definition. | ||
72 | * microhttpd cb:: Callback functions definition. | ||
73 | * microhttpd init:: Starting and stopping the server. | ||
74 | * microhttpd inspect:: Inspection. | ||
75 | * microhttpd requests:: Handling requests. | ||
76 | * microhttpd responses:: Building responses to requests. | ||
77 | * microhttpd post:: Adding a @code{POST} processor. | ||
78 | @end menu | ||
79 | |||
80 | @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
81 | |||
82 | @c ------------------------------------------------------------ | ||
83 | @node microhttpd intro | ||
84 | @appendixsec Introduction | ||
85 | |||
86 | |||
87 | @noindent | ||
88 | All symbols defined in the public @api{} start with @code{MHD_}. @mhd{} | ||
89 | is a small @http{} daemon library. As such, it does not have any @api{} | ||
90 | for logging errors (you can only enable or disable logging to stderr). | ||
91 | Also, it may not support all of the @http{} features directly, where | ||
92 | applicable, portions of @http{} may have to be handled by clients of the | ||
93 | library. | ||
94 | |||
95 | The library is supposed to handle everything that it must handle | ||
96 | (because the @api{} would not allow clients to do this), such as basic | ||
97 | connection management; however, detailed interpretations of headers --- | ||
98 | such as range requests --- and @http{} methods are left to clients. The | ||
99 | library does understand @code{HEAD} and will only send the headers of | ||
100 | the response and not the body, even if the client supplied a body. The | ||
101 | library also understands headers that control connection management | ||
102 | (specifically, @code{Connection: close} and @code{Expect: 100 continue} | ||
103 | are understood and handled automatically). | ||
104 | |||
105 | @mhd{} understands @code{POST} data and is able to decode certain | ||
106 | formats (at the moment only @code{application/x-www-form-urlencoded}) if | ||
107 | the entire data fits into the allowed amount of memory for the | ||
108 | connection. Unsupported encodings and large @code{POST} submissions are | ||
109 | provided as a stream to the main application (and thus can be processed, | ||
110 | just not conveniently by @mhd{}). | ||
111 | |||
112 | The header file defines various constants used by the @http{} protocol. | ||
113 | This does not mean that @mhd{} actually interprets all of these values. | ||
114 | The provided constants are exported as a convenience for users of the | ||
115 | library. @mhd{} does not verify that transmitted @http{} headers are | ||
116 | part of the standard specification; users of the library are free to | ||
117 | define their own extensions of the @http{} standard and use those with | ||
118 | @mhd{}. | ||
119 | |||
120 | All functions are guaranteed to be completely reentrant and | ||
121 | thread--safe. | ||
122 | |||
123 | |||
124 | |||
125 | @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
126 | |||
127 | @c ------------------------------------------------------------ | ||
128 | @node microhttpd const | ||
129 | @appendixsec Constants | ||
130 | |||
131 | |||
132 | @deftp {Enumeration} MHD_FLAG | ||
133 | Options for the @mhd{} daemon. | ||
134 | |||
135 | Note that if neither @code{MHD_USER_THREAD_PER_CONNECTION} nor | ||
136 | @code{MHD_USE_SELECT_INTERNALLY} are used, the client wants control over | ||
137 | the process and will call the appropriate microhttpd callbacks. | ||
138 | |||
139 | Starting the daemon may also fail if a particular option is not | ||
140 | implemented or not supported on the target platform (i.e. no support for | ||
141 | @acronym{SSL}, threads or IPv6). | ||
142 | |||
143 | @table @code | ||
144 | @item MHD_NO_FLAG | ||
145 | No options selected. | ||
146 | |||
147 | @item MHD_USE_DEBUG | ||
148 | Run in debug mode. If this flag is used, the library should print error | ||
149 | messages and warnings to stderr. | ||
150 | |||
151 | @item MHD_USE_SSL | ||
152 | Run in https mode. | ||
153 | |||
154 | @item MHD_USE_THREAD_PER_CONNECTION | ||
155 | Run using one thread per connection. | ||
156 | |||
157 | @item MHD_USE_SELECT_INTERNALLY | ||
158 | Run using an internal thread doing @code{SELECT}. | ||
159 | |||
160 | @item MHD_USE_IPv6 | ||
161 | Run using the IPv6 protocol (otherwise, @mhd{} will just support IPv4). | ||
162 | |||
163 | |||
164 | @item MHD_USE_PEDANTIC_CHECKS | ||
165 | Be pedantic about the protocol (as opposed to as tolerant as possible). | ||
166 | Specifically, at the moment, this flag causes @mhd{} to reject @http{} | ||
167 | 1.1 connections without a @code{Host} header. This is required by the | ||
168 | standard, but of course in violation of the ``be as liberal as possible | ||
169 | in what you accept'' norm. It is recommended to turn this @strong{ON} | ||
170 | if you are testing clients against @mhd{}, and @strong{OFF} in | ||
171 | production. | ||
172 | @end table | ||
173 | @end deftp | ||
174 | |||
175 | |||
176 | @deftp {Enumeration} MHD_OPTION | ||
177 | @mhd{} options. Passed in the varargs portion of | ||
178 | @cfunction{MHD_start_daemon}. | ||
179 | |||
180 | @table @code | ||
181 | @item MHD_OPTION_END | ||
182 | No more options / last option. This is used to terminate the VARARGs | ||
183 | list. | ||
184 | |||
185 | @item MHD_OPTION_CONNECTION_MEMORY_LIMIT | ||
186 | Maximum memory size per connection (followed by an @code{unsigned int}). | ||
187 | |||
188 | @item MHD_OPTION_CONNECTION_LIMIT | ||
189 | Maximum number of concurrenct connections to accept (followed by an | ||
190 | @code{unsigned int}). | ||
191 | |||
192 | @item MHD_OPTION_CONNECTION_TIMEOUT | ||
193 | After how many seconds of inactivity should a connection automatically | ||
194 | be timed out? (followed by an @code{unsigned int}; use zero for no | ||
195 | timeout). | ||
196 | |||
197 | @item MHD_OPTION_NOTIFY_COMPLETED | ||
198 | Register a function that should be called whenever a request has been | ||
199 | completed (this can be used for application--specific clean up). | ||
200 | Requests that have never been presented to the application (via | ||
201 | @cfunction{MHD_AccessHandlerCallback}) will not result in | ||
202 | notifications. | ||
203 | |||
204 | This option should be followed by @strong{TWO} pointers. First a | ||
205 | pointer to a function of type @cfunction{MHD_RequestCompletedCallback} | ||
206 | and second a pointer to a closure to pass to the request completed | ||
207 | callback. The second pointer maybe @null{}. | ||
208 | @end table | ||
209 | @end deftp | ||
210 | |||
211 | |||
212 | @deftp {Enumeration} MHD_ValueKind | ||
213 | The @code{MHD_ValueKind} specifies the source of the key--value pairs in | ||
214 | the @http{} protocol. | ||
215 | |||
216 | @table @code | ||
217 | @item MHD_RESPONSE_HEADER_KIND | ||
218 | Response header. | ||
219 | |||
220 | @item MHD_HEADER_KIND | ||
221 | @http{} header. | ||
222 | |||
223 | @item MHD_COOKIE_KIND | ||
224 | Cookies. Note that the original @http{} header containing the cookie(s) | ||
225 | will still be available and intact. | ||
226 | |||
227 | @item MHD_POSTDATA_KIND | ||
228 | @code{POST} data. This is available only if a content encoding | ||
229 | supported by @mhd{} is used (currently only @acronym{URL} encoding), and | ||
230 | only if the posted content fits within the available memory pool. Note | ||
231 | that in that case, the upload data given to the | ||
232 | @cfunction{MHD_AccessHandlerCallback} will be empty (since it has | ||
233 | already been processed). | ||
234 | |||
235 | @item MHD_GET_ARGUMENT_KIND | ||
236 | @code{GET} (@uri{}) arguments. | ||
237 | @end table | ||
238 | @end deftp | ||
239 | |||
240 | |||
241 | @deftp {Enumeration} MHD_RequestTerminationCode | ||
242 | The @code{MHD_RequestTerminationCode} specifies reasons why a request | ||
243 | has been terminated (or completed). | ||
244 | |||
245 | @table @code | ||
246 | @item MHD_REQUEST_TERMINATED_COMPLETED_OK | ||
247 | We finished sending the response. | ||
248 | |||
249 | @item MHD_REQUEST_TERMINATED_WITH_ERROR | ||
250 | Error handling the connection (resources exhausted, other side closed | ||
251 | connection, application error accepting request, etc.) | ||
252 | |||
253 | @item MHD_REQUEST_TERMINATED_TIMEOUT_REACHED | ||
254 | No activity on the connection for the number of seconds specified using | ||
255 | @code{MHD_OPTION_CONNECTION_TIMEOUT}. | ||
256 | |||
257 | @item MHD_REQUEST_TERMINATED_DAEMON_SHUTDOWN | ||
258 | We had to close the session since @mhd{} was being shut down. | ||
259 | @end table | ||
260 | @end deftp | ||
261 | |||
262 | |||
263 | @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
264 | |||
265 | @c ------------------------------------------------------------ | ||
266 | @node microhttpd struct | ||
267 | @appendixsec Structures type definition | ||
268 | |||
269 | |||
270 | @deftp {C Struct} MHD_Daemon | ||
271 | Handle for the daemon (listening on a socket for @http{} traffic). | ||
272 | @end deftp | ||
273 | |||
274 | |||
275 | @deftp {C Struct} MHD_Connection | ||
276 | Handle for a connection / @http{} request. With @http{}/1.1, multiple | ||
277 | requests can be run over the same connection. However, @mhd{} will only | ||
278 | show one request per @tcp{} connection to the client at any given time. | ||
279 | @end deftp | ||
280 | |||
281 | |||
282 | @deftp {C Struct} MHD_Response | ||
283 | Handle for a response. | ||
284 | @end deftp | ||
285 | |||
286 | |||
287 | @deftp {C Struct} MHD_PostProcessor | ||
288 | Handle for @code{POST} processing. | ||
289 | @end deftp | ||
290 | |||
291 | |||
292 | @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
293 | |||
294 | @c ------------------------------------------------------------ | ||
295 | @node microhttpd cb | ||
296 | @appendixsec Callback functions definition | ||
297 | |||
298 | |||
299 | @deftypefn {Function Pointer} int {*MHD_AcceptPolicyCallback} (void *cls, const struct sockaddr * addr, socklen_t addrlen) | ||
300 | Invoked in the context of a connection to allow or deny a client to | ||
301 | connect. This callback return @code{MHD_YES} if connection is allowed, | ||
302 | @code{MHD_NO} if not. | ||
303 | |||
304 | @table @var | ||
305 | @item cls | ||
306 | custom value selected at callback registration time; | ||
307 | @item addr | ||
308 | address information from the client; | ||
309 | @item addrlen | ||
310 | length of the address information. | ||
311 | @end table | ||
312 | @end deftypefn | ||
313 | |||
314 | |||
315 | @deftypefn {Function Pointer} int {*MHD_AccessHandlerCallback} (void *cls, struct MHD_Connection * connection, const char *url, const char *method, const char *version, const char *upload_data, unsigned int *upload_data_size, void **con_cls) | ||
316 | Invoked in the context of a connection to answer a request from the | ||
317 | client. This callback must call @mhd{} functions (example: the | ||
318 | @code{MHD_Response} ones) to provide content to give back to the client | ||
319 | and return an @http{} status code (i.e. @code{200} for OK, @code{404}, | ||
320 | etc.). | ||
321 | |||
322 | @ref{microhttpd post}, for details on how to code this callback. | ||
323 | |||
324 | Must return @code{MHD_YES} if the connection was handled successfully, | ||
325 | @code{MHD_NO} if the socket must be closed due to a serious error while | ||
326 | handling the request | ||
327 | |||
328 | @table @var | ||
329 | @item cls | ||
330 | custom value selected at callback registration time; | ||
331 | |||
332 | @item url | ||
333 | the @urloc{} requested by the client; | ||
334 | |||
335 | @item method | ||
336 | the @http{} method used by the client (@code{GET}, @code{PUT}, | ||
337 | @code{DELETE}, @code{POST}, etc.); | ||
338 | |||
339 | @item version | ||
340 | the @http{} version string (i.e. @code{HTTP/1.1}); | ||
341 | |||
342 | @item upload_data | ||
343 | the data being uploaded (excluding headers): | ||
344 | |||
345 | @itemize | ||
346 | @item | ||
347 | for a @code{POST} that fits into memory and that is encoded with a | ||
348 | supported encoding, the @code{POST} data will @strong{NOT} be given in | ||
349 | @var{upload_data} and is instead available as part of | ||
350 | @cfunction{MHD_get_connection_values}; | ||
351 | |||
352 | @item | ||
353 | very large @code{POST} data @strong{will} be made available | ||
354 | incrementally in @var{upload_data}; | ||
355 | @end itemize | ||
356 | |||
357 | @item upload_data_size | ||
358 | set initially to the size of the @var{upload_data} provided; this | ||
359 | callback must update this value to the number of bytes @strong{NOT} | ||
360 | processed; | ||
361 | |||
362 | @item con_cls | ||
363 | reference to a pointer, initially set to @null{}, that this callback can | ||
364 | set to some address and that will be preserved by @mhd{} for future | ||
365 | calls for this request; | ||
366 | |||
367 | since the access handler may be called many times (i.e., for a | ||
368 | @code{PUT}/@code{POST} operation with plenty of upload data) this allows | ||
369 | the application to easily associate some request--specific state; | ||
370 | |||
371 | if necessary, this state can be cleaned up in the global | ||
372 | @code{MHD_RequestCompletedCallback} (which can be set with the | ||
373 | @code{MHD_OPTION_NOTIFY_COMPLETED}). | ||
374 | @end table | ||
375 | @end deftypefn | ||
376 | |||
377 | |||
378 | @deftypefn {Function Pointer} void {*MHD_RequestCompletedCallback} (void *cls, struct MHD_Connectionconnection, void **con_cls, enum MHD_RequestTerminationCode toe) | ||
379 | Signature of the callback used by @mhd{} to notify the application about | ||
380 | completed requests. | ||
381 | |||
382 | @table @var | ||
383 | @item cls | ||
384 | custom value selected at callback registration time; | ||
385 | |||
386 | @item connection | ||
387 | connection handle; | ||
388 | |||
389 | @item con_cls | ||
390 | value as set by the last call to the | ||
391 | @code{MHD_AccessHandlerCallback}; | ||
392 | |||
393 | @item toe | ||
394 | reason for request termination see @code{MHD_OPTION_NOTIFY_COMPLETED}. | ||
395 | @end table | ||
396 | @end deftypefn | ||
397 | |||
398 | |||
399 | @deftypefn {Function Pointer} int {*MHD_KeyValueIterator} (void *cls, enum MHD_ValueKind kind, const char *key, const char *value) | ||
400 | Iterator over key--value pairs. This iterator can be used to iterate | ||
401 | over all of the cookies, headers, or @code{POST}--data fields of a | ||
402 | request, and also to iterate over the headers that have been added to a | ||
403 | response. | ||
404 | |||
405 | Return @code{MHD_YES} to continue iterating, @code{MHD_NO} to abort the | ||
406 | iteration. | ||
407 | @end deftypefn | ||
408 | |||
409 | |||
410 | @deftypefn {Function Pointer} int {*MHD_ContentReaderCallback} (void *cls, size_t pos, char *buf, int max) | ||
411 | Callback used by @mhd{} in order to obtain content. The callback has to | ||
412 | copy at most @var{max} bytes of content into @var{buf}. The total | ||
413 | number of bytes that has been placed into @var{buf} should be returned. | ||
414 | |||
415 | Note that returning zero will cause @mhd{} to try again, either | ||
416 | ``immediately'' if in multi--threaded mode (in which case the callback | ||
417 | may want to do blocking operations) or in the next round if MHD_run is | ||
418 | used. Returning zero for a daemon that runs in internal | ||
419 | @cfunction{select} mode is an error (since it would result in busy | ||
420 | waiting) and will cause the program to be aborted (@cfunction{abort}). | ||
421 | |||
422 | @table @var | ||
423 | @item cls | ||
424 | custom value selected at callback registration time; | ||
425 | |||
426 | @item pos | ||
427 | position in the datastream to access; note that if an | ||
428 | @code{MHD_Response} object is re--used, it is possible for the same | ||
429 | content reader to be queried multiple times for the same data; however, | ||
430 | if an @code{MHD_Response} is not re--used, @mhd{} guarantees that | ||
431 | @var{pos} will be the sum of all non--negative return values obtained | ||
432 | from the content reader so far. | ||
433 | @end table | ||
434 | |||
435 | Return @code{-1} on error (@mhd{} will no longer try to read content and | ||
436 | instead close the connection with the client). | ||
437 | @end deftypefn | ||
438 | |||
439 | |||
440 | @deftypefn {Function Pointer} void {*MHD_ContentReaderFreeCallback} (void *cls) | ||
441 | This method is called by @mhd{} if we are done with a content reader. | ||
442 | It should be used to free resources associated with the content reader. | ||
443 | @end deftypefn | ||
444 | |||
445 | |||
446 | @deftypefn {Function Pointer} int {*MHD_PostDataIterator} (void *cls, enum MHD_ValueKind kind, const char *key, const char *filename, const char *content_type, const char *transfer_encoding, const char *data, size_t off, size_t size) | ||
447 | Iterator over key--value pairs where the value maybe made available in | ||
448 | increments and/or may not be zero--terminated. Used for processing | ||
449 | @code{POST} data. | ||
450 | |||
451 | @table @var | ||
452 | @item cls | ||
453 | custom value selected at callback registration time; | ||
454 | |||
455 | @item kind | ||
456 | type of the value; | ||
457 | |||
458 | @item key | ||
459 | zero--terminated key for the value; | ||
460 | |||
461 | @item filename | ||
462 | name of the uploaded file, @null{} if not known; | ||
463 | |||
464 | @item content_type | ||
465 | mime--type of the data, @null{} if not known; | ||
466 | |||
467 | @item transfer_encoding | ||
468 | encoding of the data, @null{} if not known; | ||
469 | |||
470 | @item data | ||
471 | pointer to size bytes of data at the specified offset; | ||
472 | |||
473 | @item off | ||
474 | offset of data in the overall value; | ||
475 | |||
476 | @item size | ||
477 | number of bytes in data available. | ||
478 | @end table | ||
479 | |||
480 | Return @code{MHD_YES} to continue iterating, @code{MHD_NO} to abort the | ||
481 | iteration. | ||
482 | @end deftypefn | ||
483 | |||
484 | |||
485 | @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
486 | |||
487 | @c ------------------------------------------------------------ | ||
488 | @node microhttpd init | ||
489 | @appendixsec Starting and stopping the server | ||
490 | |||
491 | |||
492 | @deftypefun {struct MHD_Daemon *} MHD_start_daemon (unsigned int flags, unsigned short port, MHD_AcceptPolicyCallback apc, void *apc_cls, MHD_AccessHandlerCallback dh, void *dh_cls, ...) | ||
493 | Start a webserver on the given port. | ||
494 | |||
495 | @table @var | ||
496 | @item flags | ||
497 | OR--ed combination of @code{MHD_FLAG} values; | ||
498 | |||
499 | @item port | ||
500 | port to bind to; | ||
501 | |||
502 | @item apc | ||
503 | callback to call to check which clients will be allowed to connect; you | ||
504 | can pass @null{} in which case connections from any @acronym{IP} will be | ||
505 | accepted; | ||
506 | |||
507 | @item apc_cls | ||
508 | extra argument to @var{apc}; | ||
509 | |||
510 | @item dh | ||
511 | default handler for all @uri{}s; | ||
512 | |||
513 | @item dh_cls | ||
514 | extra argument to @var{dh}. | ||
515 | @end table | ||
516 | |||
517 | Additional arguments are a list of options (type--value pairs, | ||
518 | terminated with @code{MHD_OPTION_END}). It is mandatory to use | ||
519 | @code{MHD_OPTION_END} as last argument, even when there are no | ||
520 | additional arguments. | ||
521 | |||
522 | Return @null{} on error, handle to daemon on success. | ||
523 | @end deftypefun | ||
524 | |||
525 | |||
526 | @deftypefun void MHD_stop_daemon (struct MHD_Daemon *daemon) | ||
527 | Shutdown an @http{} daemon. | ||
528 | @end deftypefun | ||
529 | |||
530 | |||
531 | @deftypefun int MHD_run (struct MHD_Daemon *daemon) | ||
532 | Run webserver operations (without blocking unless in client callbacks). | ||
533 | This method should be called by clients in combination with | ||
534 | @cfunction{MHD_get_fdset} if the client--controlled @cfunction{select} | ||
535 | method is used. | ||
536 | |||
537 | Return @code{MHD_YES} on success, @code{MHD_NO} if this daemon was not | ||
538 | started with the right options for this call. | ||
539 | @end deftypefun | ||
540 | |||
541 | |||
542 | @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
543 | |||
544 | @c ------------------------------------------------------------ | ||
545 | @node microhttpd inspect | ||
546 | @appendixsec Inspection | ||
547 | |||
548 | |||
549 | @deftypefun int MHD_get_fdset (struct MHD_Daemon *daemon, fd_set * read_fd_set, fd_set * write_fd_set, fd_set * except_fd_set, int *max_fd) | ||
550 | Obtain the @cfunction{select} sets for this daemon. The daemon's socket | ||
551 | is added to @var{read_fd_set}. The list of currently existent | ||
552 | connections is scanned and their file descriptors added to the correct | ||
553 | set. | ||
554 | |||
555 | @glibcref{Waiting for I/O}, for details on file descriptor sets. | ||
556 | |||
557 | After the call completed successfully: the variable referenced by | ||
558 | @var{max_fd} references the file descriptor with highest integer | ||
559 | identifier. The variable must be set to zero before invoking this | ||
560 | function. | ||
561 | |||
562 | Return @code{MHD_YES} on success, @code{MHD_NO} if: the arguments are | ||
563 | invalid (example: @null{} pointers); this daemon was not started with | ||
564 | the right options for this call. | ||
565 | @end deftypefun | ||
566 | |||
567 | |||
568 | @deftypefun int MHD_get_timeout (struct MHD_Daemon *daemon, unsigned long long *timeout) | ||
569 | Obtain timeout value for select for this daemon (only needed if | ||
570 | connection timeout is used). The returned value is how long | ||
571 | @cfunction{select} should at most block, not the timeout value set for | ||
572 | connections. | ||
573 | |||
574 | @table @var | ||
575 | @item timeout | ||
576 | set to the timeout (in milliseconds). | ||
577 | @end table | ||
578 | |||
579 | Return @code{MHD_YES} on success, @code{MHD_NO} if timeouts are not used | ||
580 | (or no connections exist that would necessiate the use of a timeout | ||
581 | right now). | ||
582 | @end deftypefun | ||
583 | |||
584 | |||
585 | @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
586 | |||
587 | @c ------------------------------------------------------------ | ||
588 | @node microhttpd requests | ||
589 | @appendixsec Handling requests | ||
590 | |||
591 | |||
592 | @menu | ||
593 | * microhttpd handlers:: @uri{} specific handlers. | ||
594 | * microhttpd values:: Connection headers and small | ||
595 | @code{POST} data. | ||
596 | @end menu | ||
597 | |||
598 | |||
599 | @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
600 | |||
601 | @c ------------------------------------------------------------ | ||
602 | @node microhttpd handlers | ||
603 | @appendixsubsec @uri{} specific handlers | ||
604 | |||
605 | |||
606 | @noindent | ||
607 | A set of callbacks can be registered in the state of a daemon | ||
608 | (@code{MHD_Daemon}) to handle request for specific sets of | ||
609 | resources. The set is selected by specifying the prefix string of the | ||
610 | @uri{}, example: | ||
611 | |||
612 | @example | ||
613 | /bookcase | ||
614 | @end example | ||
615 | |||
616 | @noindent | ||
617 | matches all of the following: | ||
618 | |||
619 | @example | ||
620 | /bookcase/book.html | ||
621 | /bookcase/pencil.html | ||
622 | /bookcase/strawberry.html | ||
623 | @end example | ||
624 | |||
625 | Handlers are stored in a linked list (managed with @cfunction{malloc} | ||
626 | and @cfunction{free}). Prefixes are compared with @cfunction{strcmp}. | ||
627 | |||
628 | |||
629 | @deftypefun int MHD_register_handler (struct MHD_Daemon *daemon, const char *uri_prefix, MHD_AccessHandlerCallback dh, void *dh_cls) | ||
630 | Register an access handler for all @uri{}s beginning with | ||
631 | @var{uri_prefix}, a zero--terminated @ascii{}--coded string. | ||
632 | |||
633 | Return @code{MRI_NO} if: the arguments are invalid (example: @null{} | ||
634 | pointers); a handler for this exact prefix already exists; an error | ||
635 | allocating memory happens. | ||
636 | @end deftypefun | ||
637 | |||
638 | |||
639 | @deftypefun int MHD_unregister_handler (struct MHD_Daemon *daemon, const char *uri_prefix, MHD_AccessHandlerCallback dh, void *dh_cls) | ||
640 | Unregister an access handler for the @uri{}s beginning with | ||
641 | @var{uri_prefix}. | ||
642 | |||
643 | Return @code{MHD_NO} if: the arguments are invalid (example: @null{} | ||
644 | pointers); a handler for this exact prefix is not known for this daemon. | ||
645 | @end deftypefun | ||
646 | |||
647 | |||
648 | @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
649 | |||
650 | @c ------------------------------------------------------------ | ||
651 | @node microhttpd values | ||
652 | @appendixsubsec Connection headers and small @code{POST} data | ||
653 | |||
654 | |||
655 | @deftypefun int MHD_get_connection_values (struct MHD_Connection *connection, enum MHD_ValueKind kind, MHD_KeyValueIterator iterator, void *iterator_cls) | ||
656 | Get all the headers matching @var{kind} from the request. | ||
657 | |||
658 | The @var{iterator} callback is invoked once for each header, with | ||
659 | @var{iterator_cls} as first argument. Return the number of entries | ||
660 | iterated over; this can be less than the number of headers if, while | ||
661 | iterating, @var{iterator} returns @code{MHD_NO}. | ||
662 | |||
663 | @var{iterator} can be @null{}: in this case this function just counts | ||
664 | and returns the number of headers. | ||
665 | @end deftypefun | ||
666 | |||
667 | |||
668 | @deftypefun {const char *} MHD_lookup_connection_value (struct MHD_Connection *connection, enum MHD_ValueKind kind, const char *key) | ||
669 | Get a particular header value. If multiple values match the @var{kind}, | ||
670 | return one of them (the ``first'', whatever that means). @var{key} must | ||
671 | reference a zero--terminated @ascii{}--coded string representing the | ||
672 | header to look for: it is compared against the headers using | ||
673 | @cfunction{strcasecmp}, so case is ignored. Return @null{} if no such | ||
674 | item was found. | ||
675 | @end deftypefun | ||
676 | |||
677 | |||
678 | @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
679 | |||
680 | @c ------------------------------------------------------------ | ||
681 | @node microhttpd responses | ||
682 | @appendixsec Building answers to responses | ||
683 | |||
684 | |||
685 | @menu | ||
686 | * microhttpd response enqueue:: Enqueuing a response. | ||
687 | * microhttpd response create:: Creating a response object. | ||
688 | * microhttpd response headers:: Adding headers to a response. | ||
689 | * microhttpd response inspect:: Inspecting a response object. | ||
690 | @end menu | ||
691 | |||
692 | |||
693 | @noindent | ||
694 | Response objects handling by @mhd{} is asynchronous with respect to the | ||
695 | application execution flow. Instances of the @code{MHD_Response} | ||
696 | structure are not associated to a daemon and neither to a client | ||
697 | connection: they are managed with reference counting. | ||
698 | |||
699 | In the simplest case: we allocate a new @code{MHD_Response} structure | ||
700 | for each response, we use it once and finally we destroy it. | ||
701 | |||
702 | @mhd{} allows more efficient resources usages. | ||
703 | |||
704 | Example: we allocate a new @code{MHD_Response} structure for each | ||
705 | response @strong{kind}, we use it every time we have to give that | ||
706 | responce and we finally destroy it only when the daemon shuts down. | ||
707 | |||
708 | |||
709 | @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
710 | |||
711 | @c ------------------------------------------------------------ | ||
712 | @node microhttpd response enqueue | ||
713 | @appendixsubsec Enqueuing a response | ||
714 | |||
715 | |||
716 | @deftypefun int MHD_queue_response (struct MHD_Connection *connection, unsigned int status_code, struct MHD_Response *response) | ||
717 | Queue a response to be transmitted to the client as soon as possible | ||
718 | (increment the reference counter). | ||
719 | |||
720 | @table @var | ||
721 | @item connection | ||
722 | the connection identifying the client; | ||
723 | |||
724 | @item status_code | ||
725 | @http{} status code (i.e. @code{200} for OK); | ||
726 | |||
727 | @item response | ||
728 | response to transmit. | ||
729 | @end table | ||
730 | |||
731 | Return @code{MHD_YES} on success or if message has been queued. Return | ||
732 | @code{MHD_NO}: if arguments are invalid (example: @null{} pointer); on | ||
733 | error (i.e. reply already sent). | ||
734 | @end deftypefun | ||
735 | |||
736 | |||
737 | @deftypefun void MHD_destroy_response (struct MHD_Response *response) | ||
738 | Destroy a response object and associated resources (decrement the | ||
739 | reference counter). Note that @mhd{} may keep some of the resources | ||
740 | around if the response is still in the queue for some clients, so the | ||
741 | memory may not necessarily be freed immediatley. | ||
742 | @end deftypefun | ||
743 | |||
744 | |||
745 | An explanation of reference counting@footnote{Note to readers acquainted | ||
746 | to the Tcl @api{}: reference counting on @code{MHD_Connection} | ||
747 | structures is handled in the same way as Tcl handles @code{Tcl_Obj} | ||
748 | structures through @cfunction{Tcl_IncrRefCount} and | ||
749 | @cfunction{Tcl_DecrRefCount}.}: | ||
750 | |||
751 | @enumerate | ||
752 | @item | ||
753 | a @code{MHD_Response} object is allocated: | ||
754 | |||
755 | @example | ||
756 | struct MHD_Response * response = MHD_create_response_from_data(...); | ||
757 | /* here: reference counter = 1 */ | ||
758 | @end example | ||
759 | |||
760 | @item | ||
761 | the @code{MHD_Response} object is enqueued in a @code{MHD_Connection}: | ||
762 | |||
763 | @example | ||
764 | MHD_queue_response(connection, , response); | ||
765 | /* here: reference counter = 2 */ | ||
766 | @end example | ||
767 | |||
768 | @item | ||
769 | the creator of the response object discharges responsibility for it: | ||
770 | |||
771 | @example | ||
772 | MHD_destroy_response(response); | ||
773 | /* here: reference counter = 1 */ | ||
774 | @end example | ||
775 | |||
776 | @item | ||
777 | the daemon handles the connection sending the response's data to the | ||
778 | client then decrements the reference counter by calling | ||
779 | @cfunction{MHD_destroy_response}: the counter's value drops to zero and | ||
780 | the @code{MHD_Response} object is released. | ||
781 | @end enumerate | ||
782 | |||
783 | |||
784 | @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
785 | |||
786 | @c ------------------------------------------------------------ | ||
787 | @node microhttpd response create | ||
788 | @appendixsubsec Creating response objects | ||
789 | |||
790 | |||
791 | @deftypefun {struct MHD_Response *} MHD_create_response_from_callback (size_t size, unsigned int block_size, MHD_ContentReaderCallback crc, void *crc_cls, MHD_ContentReaderFreeCallback crfc) | ||
792 | Create a response object. The response object can be extended with | ||
793 | header information and then it can be used any number of times. | ||
794 | |||
795 | @table @var | ||
796 | @item size | ||
797 | size of the data portion of the response, @code{-1} for unknown; | ||
798 | |||
799 | @item block_size | ||
800 | preferred block size for querying @var{crc} (advisory only, @mhd{} may | ||
801 | still call @var{crc} using smaller chunks); this is essentially the | ||
802 | buffer size used for @acronym{IO}, clients should pick a value that is | ||
803 | appropriate for @acronym{IO} and memory performance requirements; | ||
804 | |||
805 | @item crc | ||
806 | callback to use to obtain response data; | ||
807 | |||
808 | @item crc_cls | ||
809 | extra argument to @var{crc}; | ||
810 | |||
811 | @item crfc | ||
812 | callback to call to free @var{crc_cls} resources. | ||
813 | @end table | ||
814 | |||
815 | Return @null{} on error (i.e. invalid arguments, out of memory). | ||
816 | @end deftypefun | ||
817 | |||
818 | |||
819 | @deftypefun {struct MHD_Response *} MHD_create_response_from_data (size_t size, void *data, int must_free, int must_copy) | ||
820 | Create a response object. The response object can be extended with | ||
821 | header information and then it can be used any number of times. | ||
822 | |||
823 | @table @var | ||
824 | @item size | ||
825 | size of the data portion of the response; | ||
826 | |||
827 | @item data | ||
828 | the data itself; | ||
829 | |||
830 | @item must_free | ||
831 | if true: @mhd{} should free data when done; | ||
832 | |||
833 | @item must_copy | ||
834 | if true: @mhd{} allocates a block of memory and use it to make a copy of | ||
835 | @var{data} embedded in the returned @code{MHD_Response} structure; | ||
836 | handling of the embedded memory is responsibility of @mhd{}; @var{data} | ||
837 | can be released anytime after this call returns. | ||
838 | @end table | ||
839 | |||
840 | Return @null{} on error (i.e. invalid arguments, out of memory). | ||
841 | @end deftypefun | ||
842 | |||
843 | |||
844 | Example: create a response from a statically allocated string: | ||
845 | |||
846 | @example | ||
847 | const char * data = "<html><body><p>Error!</p></body></html>"; | ||
848 | |||
849 | struct MHD_Connection * connection = ...; | ||
850 | struct MHD_Response * response; | ||
851 | |||
852 | response = MHD_create_response_from_data(strlen(data), data, | ||
853 | MHD_NO, MHD_NO); | ||
854 | MHD_queue_response(connection, 404, response); | ||
855 | MHD_destroy_response(response); | ||
856 | @end example | ||
857 | |||
858 | |||
859 | |||
860 | @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
861 | |||
862 | @c ------------------------------------------------------------ | ||
863 | @node microhttpd response headers | ||
864 | @appendixsubsec Adding headers to a response | ||
865 | |||
866 | |||
867 | @deftypefun int MHD_add_response_header (struct MHD_Response *response, const char *header, const char *content) | ||
868 | Add a header line to the response. The strings referenced by | ||
869 | @var{header} and @var{content} must be zero--terminated and they are | ||
870 | duplicated into memory blocks embedded in @var{response}. | ||
871 | |||
872 | Notice that the strings must not hold newlines, carriage returns or tab | ||
873 | chars. | ||
874 | |||
875 | Return @code{MHD_NO} on error (i.e. invalid header or content format or | ||
876 | memory allocation error). | ||
877 | @end deftypefun | ||
878 | |||
879 | |||
880 | @deftypefun int MHD_del_response_header (struct MHD_Response *response, const char *header, const char *content) | ||
881 | Delete a header line from the response. Return @code{MHD_NO} on error | ||
882 | (arguments are invalid or no such header known). | ||
883 | @end deftypefun | ||
884 | |||
885 | |||
886 | @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
887 | |||
888 | @c ------------------------------------------------------------ | ||
889 | @node microhttpd response inspect | ||
890 | @appendixsubsec Inspecting a response object | ||
891 | |||
892 | |||
893 | @deftypefun int MHD_get_response_headers (struct MHD_Response *response, MHD_KeyValueIterator iterator, void *iterator_cls) | ||
894 | Get all of the headers added to a response. | ||
895 | |||
896 | Invoke the @var{iterator} callback for each header in the response, | ||
897 | using @var{iterator_cls} as first argument. Return number of entries | ||
898 | iterated over. @var{iterator} can be @null{}: in this case the function | ||
899 | just counts headers. | ||
900 | |||
901 | @var{iterator} should not modify the its key and value arguments, unless | ||
902 | we know what we are doing. | ||
903 | @end deftypefun | ||
904 | |||
905 | |||
906 | @deftypefun {const char *} MHD_get_response_header (struct MHD_Response *response, const char *key) | ||
907 | Find and return a pointer to the value of a particular header from the | ||
908 | response. @var{key} must reference a zero--terminated string | ||
909 | representing the header to look for. The search is case sensitive. | ||
910 | Return @null{} if header does not exist or @var{key} is @null{}. | ||
911 | |||
912 | We should not modify the value, unless we know what we are doing. | ||
913 | @end deftypefun | ||
914 | |||
915 | |||
916 | @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
917 | |||
918 | @c ------------------------------------------------------------ | ||
919 | @node microhttpd post | ||
920 | @appendixsec Adding a @code{POST} processor | ||
921 | |||
922 | |||
923 | @menu | ||
924 | * microhttpd post api:: Programming interface for the | ||
925 | @code{POST} processor. | ||
926 | @end menu | ||
927 | |||
928 | |||
929 | @noindent | ||
930 | When a small amount of data comes from a client's @code{POST} request: | ||
931 | the data is available through the values interface. @ref{microhttpd | ||
932 | values}, for details. In this case @mhd{} invokes | ||
933 | @code{MHD_AccessHandlerCallback} only once and the callback must process | ||
934 | all the data during that invocation. | ||
935 | |||
936 | When a big amount of data comes from a client's @code{POST} request: the | ||
937 | @code{MHD_AccessHandlerCallback} will be invoked multiple times to | ||
938 | process data as it arrives; at each invocation a new chunk of data must | ||
939 | be processed. The arguments @var{upload_data} and @var{upload_data_size} | ||
940 | are used to reference the chunk of data. | ||
941 | |||
942 | When @code{MHD_AccessHandlerCallback} is invoked for a new connection: | ||
943 | its @code{*@var{con_cls}} argument is set to @null{}. When @code{POST} | ||
944 | data comes in the upload buffer it is @strong{mandatory} to use the | ||
945 | @var{con_cls} to hold data or to mark an ongoing process. | ||
946 | |||
947 | To detect that a new connection has come with the @code{POST} method: | ||
948 | |||
949 | @example | ||
950 | int | ||
951 | access_handler (void *cls, | ||
952 | struct MHD_Connection * connection, | ||
953 | const char *url, | ||
954 | const char *method, const char *version, | ||
955 | const char *upload_data, unsigned int *upload_data_size, | ||
956 | void **con_cls) | ||
957 | @{ | ||
958 | static int old_connection_marker; | ||
959 | int new_connection = (NULL == *con_cls); | ||
960 | int method_is_post = strcmp("POST",method); | ||
961 | |||
962 | if (new_connection && method_is_post) | ||
963 | @{ | ||
964 | /* new connection with POST */ | ||
965 | *con_cls = &old_connection_marker; | ||
966 | @} | ||
967 | |||
968 | ... | ||
969 | @} | ||
970 | @end example | ||
971 | |||
972 | @noindent | ||
973 | in this example the value of @code{*con_cls} is just an unused pointer | ||
974 | to an unused integer: its purpose is to make @code{*con_cls} different | ||
975 | from @null{}. When serious processing of @code{POST} data is needed: it | ||
976 | can be a pointer to a dynamically allocated data structure. | ||
977 | |||
978 | To detect that @code{POST} data is in the upload buffer: | ||
979 | |||
980 | @example | ||
981 | int | ||
982 | access_handler (void *cls, | ||
983 | struct MHD_Connection * connection, | ||
984 | const char *url, | ||
985 | const char *method, const char *version, | ||
986 | const char *upload_data, unsigned int *upload_data_size, | ||
987 | void **con_cls) | ||
988 | @{ | ||
989 | static int old_connection_marker; | ||
990 | int new_connection = (NULL == *con_cls); | ||
991 | int method_is_post = strcmp("POST",method); | ||
992 | |||
993 | if (new_connection && method_is_post) | ||
994 | @{ | ||
995 | int data_in_upload_buffer = (0 != *upload_data_size); | ||
996 | |||
997 | if (data_in_upload_buffer) | ||
998 | @{ | ||
999 | *con_cls = &old_connection_marker; | ||
1000 | /* POST data in the buffer */ | ||
1001 | @} | ||
1002 | else | ||
1003 | /* POST data accessible with the values API */ | ||
1004 | @} | ||
1005 | |||
1006 | ... | ||
1007 | @} | ||
1008 | @end example | ||
1009 | |||
1010 | |||
1011 | At each invocation there are two options: | ||
1012 | |||
1013 | @enumerate | ||
1014 | @item | ||
1015 | the callback can process the whole chunk by itself; every time @mhd{} | ||
1016 | invokes it a new chunk is fully processed; with this mode the callback | ||
1017 | has to @code{*upload_data_size = 0} before returning @code{MHD_YES}; | ||
1018 | |||
1019 | @item | ||
1020 | the callback can process a section of the chunk by itself; every time | ||
1021 | @mhd{} invokes it the buffer holds the old data as well as new data | ||
1022 | coming from the client; with this mode the callback has to set | ||
1023 | @code{*upload_data_size} to the numbe of bytes still to process before | ||
1024 | returning @code{MHD_YES}; | ||
1025 | |||
1026 | example: @mhd{} invokes the callback with @code{100 == | ||
1027 | *upload_data_size}; the callback processes the first 80 bytes and before | ||
1028 | returning the callback sets @code{*upload_data_size = 20}; the last | ||
1029 | unprocessed 20 bytes will be the first 20 at the next invocation; | ||
1030 | |||
1031 | @item | ||
1032 | when a new connection with @code{POST} data in the buffer comes: the | ||
1033 | callback allocates a PostProcessor and hand to it the responsibility of | ||
1034 | processing data; a pointer to the PostProcessor structure is saved in | ||
1035 | @code{*con_cls}, so that it is available at each subsequent invocation; | ||
1036 | |||
1037 | the post processor data can be freed by a later invocation to an | ||
1038 | appropriate callback. | ||
1039 | @end enumerate | ||
1040 | |||
1041 | |||
1042 | Let's see how to implement strategy 1: | ||
1043 | |||
1044 | @example | ||
1045 | int | ||
1046 | access_handler (void *cls, | ||
1047 | struct MHD_Connection * connection, | ||
1048 | const char *url, | ||
1049 | const char *method, const char *version, | ||
1050 | const char *upload_data, unsigned int *upload_data_size, | ||
1051 | void **con_cls) | ||
1052 | @{ | ||
1053 | static int old_connection_marker; | ||
1054 | int new_connection = (NULL == *con_cls); | ||
1055 | int method_is_post = strcmp("POST",method); | ||
1056 | |||
1057 | if (new_connection && method_is_post) | ||
1058 | @{ | ||
1059 | int data_in_upload_buffer = (0 != *upload_data_size); | ||
1060 | |||
1061 | if (data_in_upload_buffer) | ||
1062 | @{ | ||
1063 | *con_cls = &old_connection_marker; | ||
1064 | @} | ||
1065 | else | ||
1066 | @{ | ||
1067 | /* POST data accessible with the values API */ | ||
1068 | return MHD_YES; | ||
1069 | @} | ||
1070 | @} | ||
1071 | |||
1072 | if (&old_connection_marker == *con_cls) | ||
1073 | @{ | ||
1074 | consume_data(upload_data, *upload_data_size); | ||
1075 | *upload_data_size = 0; | ||
1076 | return MHD_YES; | ||
1077 | @} | ||
1078 | |||
1079 | /* process other methods */ | ||
1080 | return MHD_YES; | ||
1081 | @} | ||
1082 | @end example | ||
1083 | |||
1084 | Let's see how to implement strategy 2: | ||
1085 | |||
1086 | @example | ||
1087 | int | ||
1088 | access_handler (void *cls, | ||
1089 | struct MHD_Connection * connection, | ||
1090 | const char *url, | ||
1091 | const char *method, const char *version, | ||
1092 | const char *upload_data, unsigned int *upload_data_size, | ||
1093 | void **con_cls) | ||
1094 | @{ | ||
1095 | static int old_connection_marker; | ||
1096 | int new_connection = (NULL == *con_cls); | ||
1097 | int method_is_post = strcmp("POST",method); | ||
1098 | |||
1099 | if (new_connection && method_is_post) | ||
1100 | @{ | ||
1101 | int data_in_upload_buffer = (0 != *upload_data_size); | ||
1102 | |||
1103 | if (data_in_upload_buffer) | ||
1104 | @{ | ||
1105 | *con_cls = &old_connection_marker; | ||
1106 | @} | ||
1107 | else | ||
1108 | @{ | ||
1109 | /* POST data accessible with the values API */ | ||
1110 | return MHD_YES; | ||
1111 | @} | ||
1112 | @} | ||
1113 | |||
1114 | if (&old_connection_marker == *con_cls) | ||
1115 | @{ | ||
1116 | int number_of_bytes_to_consume = 100; | ||
1117 | |||
1118 | if (number_of_bytes_to_consume > *upload_data_size) | ||
1119 | number_of_bytes_to_consume = *upload_data_size; | ||
1120 | |||
1121 | consume_some_data(upload_data, number_of_bytes_to_consume); | ||
1122 | *upload_data_size -= number_of_bytes_to_consume; | ||
1123 | return MHD_YES; | ||
1124 | @} | ||
1125 | |||
1126 | /* process other methods */ | ||
1127 | return MHD_YES; | ||
1128 | @} | ||
1129 | |||
1130 | @end example | ||
1131 | |||
1132 | |||
1133 | |||
1134 | @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
1135 | |||
1136 | @c ------------------------------------------------------------ | ||
1137 | @node microhttpd post api | ||
1138 | @appendixsubsec Programming interface for the @code{POST} processor | ||
1139 | |||
1140 | |||
1141 | |||
1142 | @deftypefun {struct MHD_PostProcessor *} MHD_create_post_processor (struct MHD_Connection *connection, unsigned int buffer_size, MHD_PostDataIterator iterator, void *iterator_cls) | ||
1143 | Create a PostProcessor. A PostProcessor can be used to (incrementally) | ||
1144 | parse the data portion of a @code{POST} request. | ||
1145 | |||
1146 | @table @var | ||
1147 | @item connection | ||
1148 | the connection on which the @code{POST} is happening (used to determine | ||
1149 | the @code{POST} format); | ||
1150 | |||
1151 | @item buffer_size | ||
1152 | maximum number of bytes to use for internal buffering (used only for the | ||
1153 | parsing, specifically the parsing of the keys). A tiny value (256-1024) | ||
1154 | should be sufficient; do @strong{NOT} use a value smaller than 256; | ||
1155 | |||
1156 | @item iterator | ||
1157 | iterator to be called with the parsed data; must @strong{NOT} be | ||
1158 | @null{}; | ||
1159 | |||
1160 | @item iterator_cls | ||
1161 | custom value to be used as first argument to @var{iterator}. | ||
1162 | @end table | ||
1163 | |||
1164 | Return @null{} on error (out of memory, unsupported encoding), otherwise | ||
1165 | a PP handle. | ||
1166 | @end deftypefun | ||
1167 | |||
1168 | |||
1169 | @deftypefun int MHD_post_process (struct MHD_PostProcessor *pp, const char *post_data, unsigned int post_data_len) | ||
1170 | Parse and process @code{POST} data. Call this function when @code{POST} | ||
1171 | data is available (usually during an @code{MHD_AccessHandlerCallback}) | ||
1172 | with the @var{upload_data} and @var{upload_data_size}. Whenever | ||
1173 | possible, this will then cause calls to the | ||
1174 | @code{MHD_IncrementalKeyValueIterator}. | ||
1175 | |||
1176 | @table @var | ||
1177 | @item pp | ||
1178 | the post processor; | ||
1179 | |||
1180 | @item post_data | ||
1181 | @var{post_data_len} bytes of @code{POST} data; | ||
1182 | |||
1183 | @item post_data_len | ||
1184 | length of @var{post_data}. | ||
1185 | @end table | ||
1186 | |||
1187 | Return @code{MHD_YES} on success, @code{MHD_NO} on error | ||
1188 | (out--of--memory, iterator aborted, parse error). | ||
1189 | @end deftypefun | ||
1190 | |||
1191 | |||
1192 | @deftypefun void MHD_destroy_post_processor (struct MHD_PostProcessor *pp) | ||
1193 | Release PostProcessor resources. | ||
1194 | @end deftypefun | ||
1195 | |||
1196 | |||
1197 | @c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
1198 | |||