aboutsummaryrefslogtreecommitdiff
path: root/doc/microhttpd.texi
diff options
context:
space:
mode:
Diffstat (limited to 'doc/microhttpd.texi')
-rw-r--r--doc/microhttpd.texi1198
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
65This appendix documents Libmicrohttpd version 0.1.2. It is built upon
66the 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
88All symbols defined in the public @api{} start with @code{MHD_}. @mhd{}
89is a small @http{} daemon library. As such, it does not have any @api{}
90for logging errors (you can only enable or disable logging to stderr).
91Also, it may not support all of the @http{} features directly, where
92applicable, portions of @http{} may have to be handled by clients of the
93library.
94
95The library is supposed to handle everything that it must handle
96(because the @api{} would not allow clients to do this), such as basic
97connection management; however, detailed interpretations of headers ---
98such as range requests --- and @http{} methods are left to clients. The
99library does understand @code{HEAD} and will only send the headers of
100the response and not the body, even if the client supplied a body. The
101library also understands headers that control connection management
102(specifically, @code{Connection: close} and @code{Expect: 100 continue}
103are understood and handled automatically).
104
105@mhd{} understands @code{POST} data and is able to decode certain
106formats (at the moment only @code{application/x-www-form-urlencoded}) if
107the entire data fits into the allowed amount of memory for the
108connection. Unsupported encodings and large @code{POST} submissions are
109provided as a stream to the main application (and thus can be processed,
110just not conveniently by @mhd{}).
111
112The header file defines various constants used by the @http{} protocol.
113This does not mean that @mhd{} actually interprets all of these values.
114The provided constants are exported as a convenience for users of the
115library. @mhd{} does not verify that transmitted @http{} headers are
116part of the standard specification; users of the library are free to
117define their own extensions of the @http{} standard and use those with
118@mhd{}.
119
120All functions are guaranteed to be completely reentrant and
121thread--safe.
122
123
124
125@c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
126
127@c ------------------------------------------------------------
128@node microhttpd const
129@appendixsec Constants
130
131
132@deftp {Enumeration} MHD_FLAG
133Options for the @mhd{} daemon.
134
135Note that if neither @code{MHD_USER_THREAD_PER_CONNECTION} nor
136@code{MHD_USE_SELECT_INTERNALLY} are used, the client wants control over
137the process and will call the appropriate microhttpd callbacks.
138
139Starting the daemon may also fail if a particular option is not
140implemented 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
145No options selected.
146
147@item MHD_USE_DEBUG
148Run in debug mode. If this flag is used, the library should print error
149messages and warnings to stderr.
150
151@item MHD_USE_SSL
152Run in https mode.
153
154@item MHD_USE_THREAD_PER_CONNECTION
155Run using one thread per connection.
156
157@item MHD_USE_SELECT_INTERNALLY
158Run using an internal thread doing @code{SELECT}.
159
160@item MHD_USE_IPv6
161Run using the IPv6 protocol (otherwise, @mhd{} will just support IPv4).
162
163
164@item MHD_USE_PEDANTIC_CHECKS
165Be pedantic about the protocol (as opposed to as tolerant as possible).
166Specifically, at the moment, this flag causes @mhd{} to reject @http{}
1671.1 connections without a @code{Host} header. This is required by the
168standard, but of course in violation of the ``be as liberal as possible
169in what you accept'' norm. It is recommended to turn this @strong{ON}
170if you are testing clients against @mhd{}, and @strong{OFF} in
171production.
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
182No more options / last option. This is used to terminate the VARARGs
183list.
184
185@item MHD_OPTION_CONNECTION_MEMORY_LIMIT
186Maximum memory size per connection (followed by an @code{unsigned int}).
187
188@item MHD_OPTION_CONNECTION_LIMIT
189Maximum number of concurrenct connections to accept (followed by an
190@code{unsigned int}).
191
192@item MHD_OPTION_CONNECTION_TIMEOUT
193After how many seconds of inactivity should a connection automatically
194be timed out? (followed by an @code{unsigned int}; use zero for no
195timeout).
196
197@item MHD_OPTION_NOTIFY_COMPLETED
198Register a function that should be called whenever a request has been
199completed (this can be used for application--specific clean up).
200Requests that have never been presented to the application (via
201@cfunction{MHD_AccessHandlerCallback}) will not result in
202notifications.
203
204This option should be followed by @strong{TWO} pointers. First a
205pointer to a function of type @cfunction{MHD_RequestCompletedCallback}
206and second a pointer to a closure to pass to the request completed
207callback. The second pointer maybe @null{}.
208@end table
209@end deftp
210
211
212@deftp {Enumeration} MHD_ValueKind
213The @code{MHD_ValueKind} specifies the source of the key--value pairs in
214the @http{} protocol.
215
216@table @code
217@item MHD_RESPONSE_HEADER_KIND
218Response header.
219
220@item MHD_HEADER_KIND
221@http{} header.
222
223@item MHD_COOKIE_KIND
224Cookies. Note that the original @http{} header containing the cookie(s)
225will still be available and intact.
226
227@item MHD_POSTDATA_KIND
228@code{POST} data. This is available only if a content encoding
229supported by @mhd{} is used (currently only @acronym{URL} encoding), and
230only if the posted content fits within the available memory pool. Note
231that in that case, the upload data given to the
232@cfunction{MHD_AccessHandlerCallback} will be empty (since it has
233already 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
242The @code{MHD_RequestTerminationCode} specifies reasons why a request
243has been terminated (or completed).
244
245@table @code
246@item MHD_REQUEST_TERMINATED_COMPLETED_OK
247We finished sending the response.
248
249@item MHD_REQUEST_TERMINATED_WITH_ERROR
250Error handling the connection (resources exhausted, other side closed
251connection, application error accepting request, etc.)
252
253@item MHD_REQUEST_TERMINATED_TIMEOUT_REACHED
254No 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
258We 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
271Handle for the daemon (listening on a socket for @http{} traffic).
272@end deftp
273
274
275@deftp {C Struct} MHD_Connection
276Handle for a connection / @http{} request. With @http{}/1.1, multiple
277requests can be run over the same connection. However, @mhd{} will only
278show one request per @tcp{} connection to the client at any given time.
279@end deftp
280
281
282@deftp {C Struct} MHD_Response
283Handle for a response.
284@end deftp
285
286
287@deftp {C Struct} MHD_PostProcessor
288Handle 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)
300Invoked in the context of a connection to allow or deny a client to
301connect. This callback return @code{MHD_YES} if connection is allowed,
302@code{MHD_NO} if not.
303
304@table @var
305@item cls
306custom value selected at callback registration time;
307@item addr
308address information from the client;
309@item addrlen
310length 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)
316Invoked in the context of a connection to answer a request from the
317client. This callback must call @mhd{} functions (example: the
318@code{MHD_Response} ones) to provide content to give back to the client
319and return an @http{} status code (i.e. @code{200} for OK, @code{404},
320etc.).
321
322@ref{microhttpd post}, for details on how to code this callback.
323
324Must 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
326handling the request
327
328@table @var
329@item cls
330custom value selected at callback registration time;
331
332@item url
333the @urloc{} requested by the client;
334
335@item method
336the @http{} method used by the client (@code{GET}, @code{PUT},
337@code{DELETE}, @code{POST}, etc.);
338
339@item version
340the @http{} version string (i.e. @code{HTTP/1.1});
341
342@item upload_data
343the data being uploaded (excluding headers):
344
345@itemize
346@item
347for a @code{POST} that fits into memory and that is encoded with a
348supported 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
353very large @code{POST} data @strong{will} be made available
354incrementally in @var{upload_data};
355@end itemize
356
357@item upload_data_size
358set initially to the size of the @var{upload_data} provided; this
359callback must update this value to the number of bytes @strong{NOT}
360processed;
361
362@item con_cls
363reference to a pointer, initially set to @null{}, that this callback can
364set to some address and that will be preserved by @mhd{} for future
365calls for this request;
366
367since 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
369the application to easily associate some request--specific state;
370
371if 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)
379Signature of the callback used by @mhd{} to notify the application about
380completed requests.
381
382@table @var
383@item cls
384custom value selected at callback registration time;
385
386@item connection
387connection handle;
388
389@item con_cls
390value as set by the last call to the
391@code{MHD_AccessHandlerCallback};
392
393@item toe
394reason 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)
400Iterator over key--value pairs. This iterator can be used to iterate
401over all of the cookies, headers, or @code{POST}--data fields of a
402request, and also to iterate over the headers that have been added to a
403response.
404
405Return @code{MHD_YES} to continue iterating, @code{MHD_NO} to abort the
406iteration.
407@end deftypefn
408
409
410@deftypefn {Function Pointer} int {*MHD_ContentReaderCallback} (void *cls, size_t pos, char *buf, int max)
411Callback used by @mhd{} in order to obtain content. The callback has to
412copy at most @var{max} bytes of content into @var{buf}. The total
413number of bytes that has been placed into @var{buf} should be returned.
414
415Note that returning zero will cause @mhd{} to try again, either
416``immediately'' if in multi--threaded mode (in which case the callback
417may want to do blocking operations) or in the next round if MHD_run is
418used. Returning zero for a daemon that runs in internal
419@cfunction{select} mode is an error (since it would result in busy
420waiting) and will cause the program to be aborted (@cfunction{abort}).
421
422@table @var
423@item cls
424custom value selected at callback registration time;
425
426@item pos
427position in the datastream to access; note that if an
428@code{MHD_Response} object is re--used, it is possible for the same
429content reader to be queried multiple times for the same data; however,
430if 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
432from the content reader so far.
433@end table
434
435Return @code{-1} on error (@mhd{} will no longer try to read content and
436instead close the connection with the client).
437@end deftypefn
438
439
440@deftypefn {Function Pointer} void {*MHD_ContentReaderFreeCallback} (void *cls)
441This method is called by @mhd{} if we are done with a content reader.
442It 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)
447Iterator over key--value pairs where the value maybe made available in
448increments and/or may not be zero--terminated. Used for processing
449@code{POST} data.
450
451@table @var
452@item cls
453custom value selected at callback registration time;
454
455@item kind
456type of the value;
457
458@item key
459zero--terminated key for the value;
460
461@item filename
462name of the uploaded file, @null{} if not known;
463
464@item content_type
465mime--type of the data, @null{} if not known;
466
467@item transfer_encoding
468encoding of the data, @null{} if not known;
469
470@item data
471pointer to size bytes of data at the specified offset;
472
473@item off
474offset of data in the overall value;
475
476@item size
477number of bytes in data available.
478@end table
479
480Return @code{MHD_YES} to continue iterating, @code{MHD_NO} to abort the
481iteration.
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, ...)
493Start a webserver on the given port.
494
495@table @var
496@item flags
497OR--ed combination of @code{MHD_FLAG} values;
498
499@item port
500port to bind to;
501
502@item apc
503callback to call to check which clients will be allowed to connect; you
504can pass @null{} in which case connections from any @acronym{IP} will be
505accepted;
506
507@item apc_cls
508extra argument to @var{apc};
509
510@item dh
511default handler for all @uri{}s;
512
513@item dh_cls
514extra argument to @var{dh}.
515@end table
516
517Additional arguments are a list of options (type--value pairs,
518terminated with @code{MHD_OPTION_END}). It is mandatory to use
519@code{MHD_OPTION_END} as last argument, even when there are no
520additional arguments.
521
522Return @null{} on error, handle to daemon on success.
523@end deftypefun
524
525
526@deftypefun void MHD_stop_daemon (struct MHD_Daemon *daemon)
527Shutdown an @http{} daemon.
528@end deftypefun
529
530
531@deftypefun int MHD_run (struct MHD_Daemon *daemon)
532Run webserver operations (without blocking unless in client callbacks).
533This method should be called by clients in combination with
534@cfunction{MHD_get_fdset} if the client--controlled @cfunction{select}
535method is used.
536
537Return @code{MHD_YES} on success, @code{MHD_NO} if this daemon was not
538started 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)
550Obtain the @cfunction{select} sets for this daemon. The daemon's socket
551is added to @var{read_fd_set}. The list of currently existent
552connections is scanned and their file descriptors added to the correct
553set.
554
555@glibcref{Waiting for I/O}, for details on file descriptor sets.
556
557After the call completed successfully: the variable referenced by
558@var{max_fd} references the file descriptor with highest integer
559identifier. The variable must be set to zero before invoking this
560function.
561
562Return @code{MHD_YES} on success, @code{MHD_NO} if: the arguments are
563invalid (example: @null{} pointers); this daemon was not started with
564the right options for this call.
565@end deftypefun
566
567
568@deftypefun int MHD_get_timeout (struct MHD_Daemon *daemon, unsigned long long *timeout)
569Obtain timeout value for select for this daemon (only needed if
570connection timeout is used). The returned value is how long
571@cfunction{select} should at most block, not the timeout value set for
572connections.
573
574@table @var
575@item timeout
576set to the timeout (in milliseconds).
577@end table
578
579Return @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
581right 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
607A set of callbacks can be registered in the state of a daemon
608(@code{MHD_Daemon}) to handle request for specific sets of
609resources. 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
617matches all of the following:
618
619@example
620/bookcase/book.html
621/bookcase/pencil.html
622/bookcase/strawberry.html
623@end example
624
625Handlers are stored in a linked list (managed with @cfunction{malloc}
626and @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)
630Register an access handler for all @uri{}s beginning with
631@var{uri_prefix}, a zero--terminated @ascii{}--coded string.
632
633Return @code{MRI_NO} if: the arguments are invalid (example: @null{}
634pointers); a handler for this exact prefix already exists; an error
635allocating 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)
640Unregister an access handler for the @uri{}s beginning with
641@var{uri_prefix}.
642
643Return @code{MHD_NO} if: the arguments are invalid (example: @null{}
644pointers); 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)
656Get all the headers matching @var{kind} from the request.
657
658The @var{iterator} callback is invoked once for each header, with
659@var{iterator_cls} as first argument. Return the number of entries
660iterated over; this can be less than the number of headers if, while
661iterating, @var{iterator} returns @code{MHD_NO}.
662
663@var{iterator} can be @null{}: in this case this function just counts
664and 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)
669Get a particular header value. If multiple values match the @var{kind},
670return one of them (the ``first'', whatever that means). @var{key} must
671reference a zero--terminated @ascii{}--coded string representing the
672header to look for: it is compared against the headers using
673@cfunction{strcasecmp}, so case is ignored. Return @null{} if no such
674item 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
694Response objects handling by @mhd{} is asynchronous with respect to the
695application execution flow. Instances of the @code{MHD_Response}
696structure are not associated to a daemon and neither to a client
697connection: they are managed with reference counting.
698
699In the simplest case: we allocate a new @code{MHD_Response} structure
700for each response, we use it once and finally we destroy it.
701
702@mhd{} allows more efficient resources usages.
703
704Example: we allocate a new @code{MHD_Response} structure for each
705response @strong{kind}, we use it every time we have to give that
706responce 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)
717Queue a response to be transmitted to the client as soon as possible
718(increment the reference counter).
719
720@table @var
721@item connection
722the connection identifying the client;
723
724@item status_code
725@http{} status code (i.e. @code{200} for OK);
726
727@item response
728response to transmit.
729@end table
730
731Return @code{MHD_YES} on success or if message has been queued. Return
732@code{MHD_NO}: if arguments are invalid (example: @null{} pointer); on
733error (i.e. reply already sent).
734@end deftypefun
735
736
737@deftypefun void MHD_destroy_response (struct MHD_Response *response)
738Destroy a response object and associated resources (decrement the
739reference counter). Note that @mhd{} may keep some of the resources
740around if the response is still in the queue for some clients, so the
741memory may not necessarily be freed immediatley.
742@end deftypefun
743
744
745An explanation of reference counting@footnote{Note to readers acquainted
746to the Tcl @api{}: reference counting on @code{MHD_Connection}
747structures is handled in the same way as Tcl handles @code{Tcl_Obj}
748structures through @cfunction{Tcl_IncrRefCount} and
749@cfunction{Tcl_DecrRefCount}.}:
750
751@enumerate
752@item
753a @code{MHD_Response} object is allocated:
754
755@example
756struct MHD_Response * response = MHD_create_response_from_data(...);
757/* here: reference counter = 1 */
758@end example
759
760@item
761the @code{MHD_Response} object is enqueued in a @code{MHD_Connection}:
762
763@example
764MHD_queue_response(connection, , response);
765/* here: reference counter = 2 */
766@end example
767
768@item
769the creator of the response object discharges responsibility for it:
770
771@example
772MHD_destroy_response(response);
773/* here: reference counter = 1 */
774@end example
775
776@item
777the daemon handles the connection sending the response's data to the
778client then decrements the reference counter by calling
779@cfunction{MHD_destroy_response}: the counter's value drops to zero and
780the @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)
792Create a response object. The response object can be extended with
793header information and then it can be used any number of times.
794
795@table @var
796@item size
797size of the data portion of the response, @code{-1} for unknown;
798
799@item block_size
800preferred block size for querying @var{crc} (advisory only, @mhd{} may
801still call @var{crc} using smaller chunks); this is essentially the
802buffer size used for @acronym{IO}, clients should pick a value that is
803appropriate for @acronym{IO} and memory performance requirements;
804
805@item crc
806callback to use to obtain response data;
807
808@item crc_cls
809extra argument to @var{crc};
810
811@item crfc
812callback to call to free @var{crc_cls} resources.
813@end table
814
815Return @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)
820Create a response object. The response object can be extended with
821header information and then it can be used any number of times.
822
823@table @var
824@item size
825size of the data portion of the response;
826
827@item data
828the data itself;
829
830@item must_free
831if true: @mhd{} should free data when done;
832
833@item must_copy
834if 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;
836handling of the embedded memory is responsibility of @mhd{}; @var{data}
837can be released anytime after this call returns.
838@end table
839
840Return @null{} on error (i.e. invalid arguments, out of memory).
841@end deftypefun
842
843
844Example: create a response from a statically allocated string:
845
846@example
847const char * data = "<html><body><p>Error!</p></body></html>";
848
849struct MHD_Connection * connection = ...;
850struct MHD_Response * response;
851
852response = MHD_create_response_from_data(strlen(data), data,
853 MHD_NO, MHD_NO);
854MHD_queue_response(connection, 404, response);
855MHD_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)
868Add a header line to the response. The strings referenced by
869@var{header} and @var{content} must be zero--terminated and they are
870duplicated into memory blocks embedded in @var{response}.
871
872Notice that the strings must not hold newlines, carriage returns or tab
873chars.
874
875Return @code{MHD_NO} on error (i.e. invalid header or content format or
876memory allocation error).
877@end deftypefun
878
879
880@deftypefun int MHD_del_response_header (struct MHD_Response *response, const char *header, const char *content)
881Delete 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)
894Get all of the headers added to a response.
895
896Invoke the @var{iterator} callback for each header in the response,
897using @var{iterator_cls} as first argument. Return number of entries
898iterated over. @var{iterator} can be @null{}: in this case the function
899just counts headers.
900
901@var{iterator} should not modify the its key and value arguments, unless
902we 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)
907Find and return a pointer to the value of a particular header from the
908response. @var{key} must reference a zero--terminated string
909representing the header to look for. The search is case sensitive.
910Return @null{} if header does not exist or @var{key} is @null{}.
911
912We 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
930When a small amount of data comes from a client's @code{POST} request:
931the data is available through the values interface. @ref{microhttpd
932values}, for details. In this case @mhd{} invokes
933@code{MHD_AccessHandlerCallback} only once and the callback must process
934all the data during that invocation.
935
936When a big amount of data comes from a client's @code{POST} request: the
937@code{MHD_AccessHandlerCallback} will be invoked multiple times to
938process data as it arrives; at each invocation a new chunk of data must
939be processed. The arguments @var{upload_data} and @var{upload_data_size}
940are used to reference the chunk of data.
941
942When @code{MHD_AccessHandlerCallback} is invoked for a new connection:
943its @code{*@var{con_cls}} argument is set to @null{}. When @code{POST}
944data 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
947To detect that a new connection has come with the @code{POST} method:
948
949@example
950int
951access_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
973in this example the value of @code{*con_cls} is just an unused pointer
974to an unused integer: its purpose is to make @code{*con_cls} different
975from @null{}. When serious processing of @code{POST} data is needed: it
976can be a pointer to a dynamically allocated data structure.
977
978To detect that @code{POST} data is in the upload buffer:
979
980@example
981int
982access_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
1011At each invocation there are two options:
1012
1013@enumerate
1014@item
1015the callback can process the whole chunk by itself; every time @mhd{}
1016invokes it a new chunk is fully processed; with this mode the callback
1017has to @code{*upload_data_size = 0} before returning @code{MHD_YES};
1018
1019@item
1020the 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
1022coming 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
1024returning @code{MHD_YES};
1025
1026example: @mhd{} invokes the callback with @code{100 ==
1027*upload_data_size}; the callback processes the first 80 bytes and before
1028returning the callback sets @code{*upload_data_size = 20}; the last
1029unprocessed 20 bytes will be the first 20 at the next invocation;
1030
1031@item
1032when a new connection with @code{POST} data in the buffer comes: the
1033callback allocates a PostProcessor and hand to it the responsibility of
1034processing data; a pointer to the PostProcessor structure is saved in
1035@code{*con_cls}, so that it is available at each subsequent invocation;
1036
1037the post processor data can be freed by a later invocation to an
1038appropriate callback.
1039@end enumerate
1040
1041
1042Let's see how to implement strategy 1:
1043
1044@example
1045int
1046access_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
1084Let's see how to implement strategy 2:
1085
1086@example
1087int
1088access_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)
1143Create a PostProcessor. A PostProcessor can be used to (incrementally)
1144parse the data portion of a @code{POST} request.
1145
1146@table @var
1147@item connection
1148the connection on which the @code{POST} is happening (used to determine
1149the @code{POST} format);
1150
1151@item buffer_size
1152maximum number of bytes to use for internal buffering (used only for the
1153parsing, specifically the parsing of the keys). A tiny value (256-1024)
1154should be sufficient; do @strong{NOT} use a value smaller than 256;
1155
1156@item iterator
1157iterator to be called with the parsed data; must @strong{NOT} be
1158@null{};
1159
1160@item iterator_cls
1161custom value to be used as first argument to @var{iterator}.
1162@end table
1163
1164Return @null{} on error (out of memory, unsupported encoding), otherwise
1165a 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)
1170Parse and process @code{POST} data. Call this function when @code{POST}
1171data is available (usually during an @code{MHD_AccessHandlerCallback})
1172with the @var{upload_data} and @var{upload_data_size}. Whenever
1173possible, this will then cause calls to the
1174@code{MHD_IncrementalKeyValueIterator}.
1175
1176@table @var
1177@item pp
1178the post processor;
1179
1180@item post_data
1181@var{post_data_len} bytes of @code{POST} data;
1182
1183@item post_data_len
1184length of @var{post_data}.
1185@end table
1186
1187Return @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)
1193Release PostProcessor resources.
1194@end deftypefun
1195
1196
1197@c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1198