Mstdlib-1.24.0
m_http.h
1/* The MIT License (MIT)
2 *
3 * Copyright (c) 2018 Monetra Technologies, LLC.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
24#ifndef __M_HTTP_H__
25#define __M_HTTP_H__
26
27/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
28
29#include <mstdlib/base/m_defs.h>
30#include <mstdlib/base/m_types.h>
31#include <mstdlib/base/m_list_str.h>
32#include <mstdlib/base/m_hash_multi.h>
33#include <mstdlib/base/m_parser.h>
34#include <mstdlib/base/m_buf.h>
35#include <mstdlib/text/m_textcodec.h>
36
37/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
38
39__BEGIN_DECLS
40
41/*! \addtogroup m_http HTTP
42 * \ingroup m_formats
43 *
44 * HTTP 1.0/1.1 message reading and writing.
45 *
46 * Conforms to:
47 *
48 * - RFC 7230 Hypertext Transfer Protocol (HTTP/1.1): Message Syntax and Routing
49 * - RFC 7231 Hypertext Transfer Protocol (HTTP/1.1): Semantics and Content
50 *
51 * There are two types of message parsing supported.
52 * - Stream based callback
53 * - Simple reader (memory buffered)
54 *
55 * Currently supported Read:
56 * - Callback
57 * - Simple
58 *
59 * Currently support Write:
60 * - Simple (simple can generate head only and data can be sent separately)
61 *
62 * @{
63 */
64
65/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
66
67/*! Error codes.
68 *
69 * M_HTTP_ERROR_SUCCESS, and M_HTTP_ERROR_SUCCESS_MORE_POSSIBLE are both
70 * Success conditions. All data is valid and has been parsed.
71 *
72 * M_HTTP_ERROR_MOREDATA indicates valid data and isn't always considered an
73 * error condition. It typically indicates a retry once more data is received
74 * condition. For example more headers could follow or the content length or
75 * chucked data was not complete. There is more data needed to complete the
76 * message. It is only an error if end of data has been reached.
77 *
78 * M_HTTP_ERROR_STOP is not considered an error and means no more processing
79 * will/should take place. A callback should generate this if all data the caller
80 * wants has been processed if partial processing is taking place. For example,
81 * a proxy looking for X-Forwarded-For header in order to blacklist an abusive
82 * IP before forwarding the message.
83 */
84typedef enum {
85 M_HTTP_ERROR_SUCCESS = 0, /*!< Success. Data fully parsed and all data is present. */
86 M_HTTP_ERROR_SUCCESS_MORE_POSSIBLE, /*!< Success but more data possible. No content length was sent or chunking was used. The only way to know all data was received is by a disconnect. */
87 M_HTTP_ERROR_MOREDATA, /*!< Incomplete message, more data required. Not necessarily an error if parsing as data is streaming. */
88 M_HTTP_ERROR_STOP, /*!< Stop processing (Used by callback functions to indicate non-error but stop processing). */
89 M_HTTP_ERROR_INVALIDUSE, /*!< Invalid use. */
90 M_HTTP_ERROR_LENGTH_REQUIRED, /*!< Content-Length is required but not provided. 411 code. */
91 M_HTTP_ERROR_CHUNK_EXTENSION_NOTALLOWED, /*!< Chunk extensions are present but not allowed. */
92 M_HTTP_ERROR_TRAILER_NOTALLOWED, /*!< Chunk trailer present but not allowed. */
93 M_HTTP_ERROR_URI, /*!< Invalid URI. 400 code. */
94 M_HTTP_ERROR_STARTLINE_LENGTH, /*!< Start line exceed maximum length (6k limit). 414 code. */
95 M_HTTP_ERROR_STARTLINE_MALFORMED, /*!< Start line is malformed. 400 code. */
96 M_HTTP_ERROR_UNKNOWN_VERSION, /*!< Unknown or unsupported HTTP version. */
97 M_HTTP_ERROR_REQUEST_METHOD, /*!< Invalid request method. 501 code. */
98 M_HTTP_ERROR_HEADER_LENGTH, /*!< Header exceeds maximum length (8k limit). 413 code. */
99 M_HTTP_ERROR_HEADER_FOLD, /*!< Header folded. Folding is deprecated and should not be used. 400/502 code. */
100 M_HTTP_ERROR_HEADER_INVALID, /*!< Header is malformed. 400 code. */
101 M_HTTP_ERROR_HEADER_DUPLICATE, /*!< Duplicate header present. 400 code. */
102 M_HTTP_ERROR_CHUNK_STARTLINE_LENGTH, /*!< Chunk start line exceed maximum length (6k limit). 414 code. */
103 M_HTTP_ERROR_CHUNK_LENGTH, /*!< Failed to parse chunk length. */
104 M_HTTP_ERROR_CHUNK_MALFORMED, /*!< Chunk is malformed. */
105 M_HTTP_ERROR_CHUNK_EXTENSION, /*!< Chunk extensions present but malformed. */
106 M_HTTP_ERROR_CHUNK_DATA_MALFORMED, /*!< Chunk data malformed. */
107 M_HTTP_ERROR_CONTENT_LENGTH_MALFORMED, /*!< Content-Length present but malformed. */
108 M_HTTP_ERROR_NOT_HTTP, /*!< Not an HTTP message. */
109 M_HTTP_ERROR_MULTIPART_NOBOUNDARY, /*!< Multipart message missing boundary. */
110 M_HTTP_ERROR_MULTIPART_MISSING, /*!< Multipart message but multipart missing. */
111 M_HTTP_ERROR_MULTIPART_MISSING_DATA, /*!< Multipart data missing. */
112 M_HTTP_ERROR_MULTIPART_INVALID, /*!< Multipart is invalid. */
113 M_HTTP_ERROR_UNSUPPORTED_DATA, /*!< Data received is unsupported. */
114 M_HTTP_ERROR_TEXTCODEC_FAILURE, /*!< Text decode failure. */
115 M_HTTP_ERROR_USER_FAILURE /*!< Generic callback generated failure. */
117
118
119/*! Message type. */
120typedef enum {
121 M_HTTP_MESSAGE_TYPE_UNKNOWN = 0, /*!< Unknown message type. */
122 M_HTTP_MESSAGE_TYPE_REQUEST, /*!< Request message. */
123 M_HTTP_MESSAGE_TYPE_RESPONSE /*!< Response message. */
125
126
127/*! HTTP version in use. */
128typedef enum {
129 M_HTTP_VERSION_UNKNOWN = 0, /*!< Unknown. */
133
134
135/*! HTTP methods. */
136typedef enum {
137 M_HTTP_METHOD_UNKNOWN = 0, /*!< Unknown method (null value). */
138 M_HTTP_METHOD_OPTIONS, /*!< Options. */
139 M_HTTP_METHOD_GET, /*!< Get. */
140 M_HTTP_METHOD_HEAD, /*!< Head. */
141 M_HTTP_METHOD_POST, /*!< Post. */
142 M_HTTP_METHOD_PUT, /*!< Put. */
143 M_HTTP_METHOD_DELETE, /*!< Delete. */
144 M_HTTP_METHOD_TRACE, /*!< Trace. */
145 M_HTTP_METHOD_CONNECT, /*!< Connect. */
146 M_HTTP_METHOD_PATCH /*!< Patch. */
148
149
150/*! HTTP Content type. */
151typedef enum {
152 M_HTTP_DATA_FORMAT_UNKNOWN = 0, /*! Could not determine the format of the data. */
153 M_HTTP_DATA_FORMAT_NONE, /*!< There is no data, Content-Length = 0. */
155 M_HTTP_DATA_FORMAT_CHUNKED, /*!< Data is chunked. */
156 M_HTTP_DATA_FORMAT_MULTIPART /*!< Data is multipart. */
158
159
160/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
161
162/*! Convert a version string into a version value.
163 *
164 * The version can start with "HTTP/" or without.
165 *
166 * \param[in] version Version string.
167 *
168 * \return Version.
169 */
170M_API M_http_version_t M_http_version_from_str(const char *version);
171
172
173/*! Convert an http version to a string.
174 *
175 * Returns in the format "HTTP/#".
176 *
177 * \param[in] version Version.
178 *
179 * \return String.
180 */
181M_API const char *M_http_version_to_str(M_http_version_t version);
182
183
184/*! Convert a method string into a method value.
185 *
186 * \param[in] method Method string.
187 *
188 * \return Method.
189 */
190M_API M_http_method_t M_http_method_from_str(const char *method);
191
192
193/*! Convert an http method to a string.
194 *
195 * \param[in] method Method.
196 *
197 * \return String.
198 */
199M_API const char *M_http_method_to_str(M_http_method_t method);
200
201
202/*! Convert an http code to a string.
203 *
204 * Not all codes can be converted to a string.
205 * Codes that cannot be converted will return "Generic".
206 *
207 * \param[in] code Code.
208 *
209 * \return String.
210 */
211M_API const char *M_http_code_to_reason(M_uint32 code);
212
213
214/*! Convert an http error code to a string.
215 *
216 * \param[in] err Error code.
217 *
218 * \return Name of error code (not a description, just the enum name, like M_HTTP_ERROR_SUCCESS).
219 */
221
222
223/*! Create query string, append to given URI, return as new string.
224 *
225 * Empty values are not permitted - keys whose values are set to the empty string will be left out of
226 * the query string.
227 *
228 * Query string are defined as application/x-www-form-urlencoded data so url form encoding is
229 * used.
230 *
231 * \see M_http_generate_query_string_buf()
232 *
233 * \param[in] uri Uri string (e.g., /cgi-bin/payment/start, or %http://google.com/whatever).
234 * \param[in] params Key-value pairs to encode in query string.
235 *
236 * \return New string with URI + query string, or \c NULL if there was an encoding error.
237 */
238M_API char *M_http_generate_query_string(const char *uri, const M_hash_dict_t *params);
239
240
241/*! Create query string, append URI + query string to buffer.
242 *
243 * Empty values are not permitted - keys whose values are set to the empty string will be left out of
244 * the query string.
245 *
246 * Query string are defined as application/x-www-form-urlencoded data so url form encoding is
247 * used.
248 *
249 * \see M_http_generate_query_string()
250 *
251 * \param[out] buf Buffer to add URI + query string to, contents remain unchanged if there was an error.
252 * \param[in] uri Uri string (e.g., /cgi-bin/payment/start, or %http://google.com/whatever).
253 * \param[in] params Key-value pairs to encode in query string.
254 *
255 * \return M_TRUE if successful, or \c M_FALSE if there was an encoding error.
256 */
257M_API M_bool M_http_generate_query_string_buf(M_buf_t *buf, const char *uri, const M_hash_dict_t *params);
258
259
260/*! Parse a query string.
261 *
262 * Components are expected to be application/x-www-form-urlencoded and will be decoded.
263 *
264 * \see M_http_generate_query_string()
265 *
266 * \param[in] data The formatted query arguments.
267 * \param[in] codec Additional encodings before the data was form encoded.
268 * The form decoded data will be decoded to utf-8 from this encoding.
269 * Use M_TEXTCODEC_UNKNOWN to skip additional decoding.
270 *
271 * \return Multi value dict of key value pairs. Keys are considered case insensitive.
272 */
274
275
276/*! Create form data string.
277 *
278 * Data is defined as application/x-www-form-urlencoded data so url form encoding is
279 * used.
280 *
281 * \see M_http_generate_form_data_string_buf()
282 *
283 * \param[in] params Key-value pairs to encode in form data string.
284 *
285 * \return New string.
286 */
288
289
290/*! Create form data string.
291 *
292 * \see M_http_generate_query_string()
293 *
294 * \param[out] buf Buffer to add URI + query string to, contents remain unchanged if there was an error.
295 * \param[in] params Key-value pairs to encode in query string.
296 *
297 * \return M_TRUE if successful, or \c M_FALSE if there was an encoding error.
298 */
300
301
302/*! Parse a application/x-www-form-urlencoded paramter string.
303 *
304 * Components are expected to be application/x-www-form-urlencoded and will be decoded.
305 * This is similar to decoding a query string but this is intended for form data from
306 * the body of a message.
307 *
308 * \see M_http_parse_query_string()
309 *
310 * \param[in] data The formatted query arguments.
311 * \param[in] codec Additional encodings before the data was form encoded.
312 * The form decoded data will be decoded to utf-8 from this encoding.
313 * Use M_TEXTCODEC_UNKNOWN to skip additional decoding.
314 *
315 * \return Multi value dict of key value pairs. Keys are considered case insensitive.
316 */
318
319/*! @} */
320
321
322/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
323
324/*! \addtogroup m_http_reader HTTP Stream Reader
325 * \ingroup m_http
326 *
327 * Stream reader used for parsing using callbacks.
328 * Very useful for large HTTP messages.
329 *
330 * @{
331 */
332
333struct M_http_reader;
334typedef struct M_http_reader M_http_reader_t;
335
336/*! Function definition for the start line.
337 *
338 * \param[in] type Type of message.
339 * \param[in] version HTTP version.
340 * \param[in] method If request, method of request.
341 * \param[in] uri If request, uri requested.
342 * \param[in] code If response, numeric response code.
343 * \param[in] reason If response, response reason.
344 * \param[in] thunk Thunk.
345 *
346 * \return Result
347 */
348typedef M_http_error_t (*M_http_reader_start_func)(M_http_message_type_t type, M_http_version_t version, M_http_method_t method, const char *uri, M_uint32 code, const char *reason, void *thunk);
349
350/*! Function definition for reading full headers.
351 *
352 * This will provide the full unparsed header.
353 * This is always called for every header.
354 * It may be called multiple times if a header appears multiple times.
355 * This is intended for informational use or if passing along data and
356 * not altering any headers in the process.
357 *
358 * A header appearing multiple times here means it was present multiple times.
359 *
360 * \param[in] key Header key.
361 * \param[in] val Header value.
362 * \param[in] thunk Thunk.
363 *
364 * \return Result
365 *
366 * \see M_http_reader_header_func
367 */
368typedef M_http_error_t (*M_http_reader_header_full_func)(const char *key, const char *val, void *thunk);
369
370/*! Function definition for reading headers as component parts.
371 *
372 * This is the main header reading callback that should be used when parsing a message.
373 *
374 * Headers are split if a header list. Keys will appear multiple times if values were
375 * in a list or if the header appears multiple times. The standard uses ',' as a list
376 * separator but some headers will use a semicolon ';'. Values with semicolon ';' separated
377 * parameters are split as well.
378 *
379 * Will be called for headers that have a single part and are not split.
380 *
381 * It is not possible to determine if a header was present multiple times vs being
382 * in list form.
383 *
384 * \param[in] key Header key.
385 * \param[in] val Header value.
386 * \param[in] thunk Thunk.
387 *
388 * \return Result
389 *
390 * \see M_http_reader_header_full_func
391 */
392typedef M_http_error_t (*M_http_reader_header_func)(const char *key, const char *val, void *thunk);
393
394/*! Function definition for header parsing completion.
395 *
396 * \param[in] format The format data was sent using.
397 * \param[in] thunk Thunk.
398 *
399 * \return Result
400 */
402
403/*! Function definition for reading body data.
404 *
405 * \param[in] data Data.
406 * \param[in] len Length of data.
407 * \param[in] thunk Thunk.
408 *
409 * \return Result
410 */
411typedef M_http_error_t (*M_http_reader_body_func)(const unsigned char *data, size_t len, void *thunk);
412
413/*! Function definition for completion of body parsing.
414 *
415 * This will only be called if the Content-Length header was specified.
416 *
417 * \param[in] thunk Thunk.
418 *
419 * \return Result
420 */
422
423/*! Function definition for reading chunk extensions.
424 *
425 * Extensions are not required to have values.
426 *
427 * \param[in] key Key.
428 * \param[in] val Value.
429 * \param[in] idx Chunk number the extension belongs to.
430 * \param[in] thunk Thunk.
431 *
432 * \return Result
433 */
434typedef M_http_error_t (*M_http_reader_chunk_extensions_func)(const char *key, const char *val, size_t idx, void *thunk);
435
436/*! Function definition for completion of chunk extension parsing.
437 *
438 * Will only be called if there were chunk extensions.
439 *
440 * \param[in] idx Chunk number that had extensions.
441 * \param[in] thunk Thunk.
442 *
443 * \return Result
444 */
446
447/*! Function definition for reading chunk data.
448 *
449 * \param[in] data Data.
450 * \param[in] len Length of data.
451 * \param[in] idx Chunk number the data belongs to.
452 * \param[in] thunk Thunk.
453 *
454 * \return Result
455 */
456typedef M_http_error_t (*M_http_reader_chunk_data_func)(const unsigned char *data, size_t len, size_t idx, void *thunk);
457
458/*! Function definition for completion of chunk data.
459 *
460 * \param[in] idx Chunk number that has been completely processed.
461 * \param[in] thunk Thunk.
462 *
463 * \return Result
464 */
465typedef M_http_error_t (*M_http_reader_chunk_data_done_func)(size_t idx, void *thunk);
466
467/*! Function definition for completion of parsing all chunks.
468 *
469 * Only called when data is chunked.
470 *
471 * \param[in] thunk Thunk.
472 *
473 * \return Result
474 */
476
477/*! Function definition for reading multipart preamble.
478 *
479 * Typically the preamble should be ignored if present.
480 *
481 * \param[in] data Data.
482 * \param[in] len Length of data.
483 * \param[in] thunk Thunk.
484 *
485 * \return Result
486 */
487typedef M_http_error_t (*M_http_reader_multipart_preamble_func)(const unsigned char *data, size_t len, void *thunk);
488
489/*! Function definition for completion of multipart preamble parsing.
490 *
491 * Only called if a preamble was present.
492 *
493 * \param[in] thunk Thunk.
494 *
495 * \return Result
496 */
498
499/*! Function definition for reading full multi part headers.
500 *
501 * This will provide the full unparsed header.
502 * This is always called for every header.
503 * It may be called multiple times if a header appears multiple times.
504 * This is intended for informational use or if passing along data and
505 * not altering any headers in the process.
506 *
507 * A header appearing multiple times here means it was present multiple times.
508 *
509 * \param[in] key Header key.
510 * \param[in] val Header value.
511 * \param[in] idx Part number the header belongs to.
512 * \param[in] thunk Thunk.
513 *
514 * \return Result
515 *
516 * \see M_http_reader_header_func
517 */
518typedef M_http_error_t (*M_http_reader_multipart_header_full_func)(const char *key, const char *val, size_t idx, void *thunk);
519
520/*! Function definition for reading multipart part headers.
521 *
522 * This is the main multi part header reading callback that should be used when parsing a message.
523 *
524 * Headers are split if a header list. Keys will appear multiple times if values were
525 * in a list or if the header appears multiple times. The standard uses ',' as a list
526 * separator but some headers will use a semicolon ';'. Values with semicolon ';' separated
527 * parameters are split as well.
528 *
529 * Will be called for headers that have a single part and are not split.
530 *
531 * It is not possible to determine if a header was present multiple times vs being
532 * in list form.
533 *
534 * \param[in] key Key.
535 * \param[in] val Value.
536 * \param[in] idx Part number the header belongs to.
537 * \param[in] thunk Thunk.
538 *
539 * \return Result
540 */
541typedef M_http_error_t (*M_http_reader_multipart_header_func)(const char *key, const char *val, size_t idx, void *thunk);
542
543/*! Function definition for completion of multipart part header parsing.
544 *
545 * \param[in] idx Part number.
546 * \param[in] thunk Thunk.
547 *
548 * \return Result
549 */
551
552/*! Function definition for reading multipart part data.
553 *
554 * \param[in] data Data.
555 * \param[in] len Length of data.
556 * \param[in] idx Partnumber the data belongs to.
557 * \param[in] thunk Thunk.
558 *
559 * \return Result
560 */
561typedef M_http_error_t (*M_http_reader_multipart_data_func)(const unsigned char *data, size_t len, size_t idx, void *thunk);
562
563/*! Function definition for completion of multipart part data.
564 *
565 * \param[in] idx Chunk number that has been completely processed.
566 * \param[in] thunk Thunk.
567 *
568 * \return Result
569 */
570typedef M_http_error_t (*M_http_reader_multipart_data_done_func)(size_t idx, void *thunk);
571
572/*! Function definition for completion of parsing all multipart parts.
573 *
574 * Only called when data is chunked.
575 *
576 * \param[in] thunk Thunk.
577 *
578 * \return Result
579 */
581
582/*! Function definition for reading multipart epilogue.
583 *
584 * Typically the epilogue should be ignored if present.
585 *
586 * \param[in] data Data.
587 * \param[in] len Length of data.
588 * \param[in] thunk Thunk.
589 *
590 * \return Result
591 */
592typedef M_http_error_t (*M_http_reader_multipart_epilouge_func)(const unsigned char *data, size_t len, void *thunk);
593
594/*! Function definition for completion of multipart epilogue parsing.
595 *
596 * Only called if a epilogue was present.
597 *
598 * \param[in] thunk Thunk.
599 *
600 * \return Result
601 */
603
604/*! Function definition for reading full trailer headers.
605 *
606 * This will provide the full unparsed header.
607 * This is always called for every trailer header.
608 * It may be called multiple times if a header appears multiple times.
609 * This is intended for informational use or if passing along data and
610 * not altering any headers in the process.
611 *
612 * A header appearing multiple times here means it was present multiple times.
613 *
614 * \param[in] key Header key.
615 * \param[in] val Header value.
616 * \param[in] thunk Thunk.
617 *
618 * \return Result
619 *
620 * \see M_http_reader_header_func
621 */
622typedef M_http_error_t (*M_http_reader_trailer_full_func)(const char *key, const char *val, void *thunk);
623
624/*! Function definition for reading trailing headers.
625 *
626 * This is the main trailer header reading callback that should be used when parsing a message.
627 *
628 * Headers are split if a header list. Keys will appear multiple times if values were
629 * in a list or if the header appears multiple times. The standard uses ',' as a list
630 * separator but some headers will use a semicolon ';'. Values with semicolon ';' separated
631 * parameters are split as well.
632 *
633 * Will be called for headers that have a single part and are not split.
634 *
635 * It is not possible to determine if a header was present multiple times vs being
636 * in list form.
637 *
638 * \param[in] key Header key.
639 * \param[in] val Header value.
640 * \param[in] thunk Thunk.
641 *
642 * \return Result
643 */
644typedef M_http_error_t (*M_http_reader_trailer_func)(const char *key, const char *val, void *thunk);
645
646/*! Function definition for trailing header parsing completion.
647 *
648 * Only called if trailing headers were present.
649 *
650 * \param[in] thunk Thunk.
651 *
652 * \return Result
653 */
655
656
657/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
658
659/*! Flags controlling reader behavior. */
660typedef enum {
661 M_HTTP_READER_NONE = 0, /*!< Default operation. */
662 M_HTTP_READER_SKIP_START /*!< Skip parsing start line. Data starts with headers. */
664
665
666/*! Callbacks for various stages of parsing. */
692};
693
694
695/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
696
697/*! Create an http reader object.
698 *
699 * \param[in] cbs Callbacks for processing.
700 * \param[in] flags Flags controlling behavior.
701 * \param[in] thunk Thunk passed to callbacks.
702 *
703 * \return Object.
704 */
705M_API M_http_reader_t *M_http_reader_create(struct M_http_reader_callbacks *cbs, M_uint32 flags, void *thunk);
706
707
708/*! Destroy an http object.
709 *
710 * \param[in] httpr Http reader object.
711 */
713
714
715/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
716
717/*! Parse http message from given data.
718 *
719 * M_HTTP_ERROR_SUCCESS, and M_HTTP_ERROR_SUCCESS_MORE_POSSIBLE are both
720 * Success conditions. Data is valid and has been parsed. Remaining unread data
721 * in the buffer on M_HTTP_ERROR_SUCCESS indicates a possible additional
722 * message.
723 *
724 * M_HTTP_ERROR_MOREDATA indicates valid data but an incomplete message. The
725 * parse should be run again starting where the last parse stopped. Until
726 * a known or possible full parse has completed.
727 *
728 * The reader can only be used once per complete message.
729 *
730 * \param[in] httpr Http reader object.
731 * \param[in] data Data to parse.
732 * \param[in] data_len Length of data.
733 * \param[out] len_read How much data was read.
734 *
735 * \return Result.
736 */
737M_API M_http_error_t M_http_reader_read(M_http_reader_t *httpr, const unsigned char *data, size_t data_len, size_t *len_read);
738
739/*! @} */
740
741
742/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
743
744/*! \addtogroup m_http_simple HTTP Simple
745 * \ingroup m_http
746 *
747 * Simple parsing and structuring of http data.
748 *
749 * @{
750 */
751
752/*! \addtogroup m_http_simple_read HTTP Simple Reader
753 * \ingroup m_http_simple
754 *
755 * Reads a full HTTP message. Useful for small messages.
756 * All data is contained within on object for
757 * easy processing.
758 *
759 * Will attempt to decode body data based on the detected charset
760 * unless M_HTTP_SIMPLE_READ_NODECODE_BODY is set.
761 *
762 * Use M_http_simple_read_is_body_form_data() to determine
763 * which accessors to use. M_http_simple_read_codec will
764 * dictated the current encoding of the body data. If
765 * form encoded this is the encoding of the key value pairs
766 * not the raw body data. If not form data this is the encoding
767 * of the body data. M_http_simple_read_is_body will always return
768 * body data. If from encoded the data will still be form encoded.
769 *
770 * Codec will be set to the codec of the body data. Unless form
771 * encoded, in which case it is the codec for the key value pairs
772 * from M_http_simple_read_is_body_form_data().
773 *
774 * If the encoding could not be detected or is not supported the codec will be
775 * unknown. Use M_http_simple_read_charset() to determine the charset of the
776 * data.
777 *
778 * Charset and codec will only be read from the Content-Type header. Content
779 * type and encoding set in an HTML tag is not supported.
780 *
781 * @{
782 */
783
784struct M_http_simple_read;
786
787
788typedef enum {
790 M_HTTP_SIMPLE_READ_NODECODE_BODY, /*!< Do not attempt to decode the body data (from detected charset). */
791 M_HTTP_SIMPLE_READ_LEN_REQUIRED, /*!< Require content-length, cannot be chunked data. */
792 M_HTTP_SIMPLE_READ_FAIL_EXTENSION, /*!< Fail if chunked extensions are specified. Otherwise, Ignore. */
793 M_HTTP_SIMPLE_READ_FAIL_TRAILERS /*!< Fail if trailers sent. Otherwise, they are ignored. */
795
796
797/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
798
799/*! Read the next HTTP message from the given buffer, store results in a new M_http_simple_read_t object.
800 *
801 * Will return M_HTTP_ERROR_MOREDATA if we need to wait for more data to get a complete message.
802 *
803 * \param[out] simple Place to store new M_http_simple_read_t object. Can be NULL to check for valid message.
804 * Will only be set on M_HTTP_ERROR_SUCCESS, and M_HTTP_ERROR_SUCCESS_MORE_POSSIBLE.
805 * \param[in] data Buffer containing HTTP messages to read.
806 * \param[in] data_len Length of \a data.
807 * \param[in] flags Read options (OR'd combo of M_http_simple_read_flags_t).
808 * \param[in] len_read Num bytes consumed from \a data (may be NULL, if caller doesn't need this info).
809 * Will be set on error indicating the location in the message that generated the error.
810 *
811 * \return Response code.
812 *
813 * \see M_http_reader_read
814 * \see M_http_simple_read_parser
815 * \see M_http_simple_read_destroy
816 */
817M_API M_http_error_t M_http_simple_read(M_http_simple_read_t **simple, const unsigned char *data, size_t data_len, M_uint32 flags, size_t *len_read);
818
819
820/*! Read the next HTTP message from the given parser.
821 *
822 * Will return M_HTTP_ERROR_MOREDATA if we need to wait for more data to get a complete message.
823 * No data will be dropped from the parser, in this case.
824 *
825 * On all other return values the parser will advance and data consumed. On a hard ERROR condition
826 * the parser will start at the point of the error. If this is undesirable, the parser should be
827 * marked and rewound after this function is called.
828 *
829 * \param[out] simple Place to store new M_http_simple_read_t object. Can be NULL to check for valid message.
830 * \param[in] parser Buffer containing HTTP messages to read.
831 * \param[in] flags Read options (OR'd combo of M_http_simple_read_flags_t).
832 *
833 * \return Response code.
834 *
835 * \see M_http_reader_read
836 * \see M_http_simple_read
837 * \see M_http_simple_destroy
838 */
840
841
842/*! Destroy the given M_http_simple_read_t object.
843 *
844 * \param[in] http object to destroy
845 *
846 * \see M_http_simple_read
847 * \see M_http_simple_read_parser
848 */
850
851
852/*! Return the type of the parsed message.
853 *
854 * \param[in] simple Parsed HTTP message.
855 *
856 * \return Type of message (REQUEST or RESPONSE, usually).
857 */
859
860
861/*! Return the HTTP protocol version of the parsed message.
862 *
863 * \param[in] simple Parsed HTTP message.
864 *
865 * \return HTTP protocol version (1.0, 1.1).
866 */
868
869
870/*! Return the HTTP status code of the parsed message.
871 *
872 * The status code is only set for response messages (type == M_HTTP_MESSAGE_TYPE_RESPONSE).
873 * If the parsed message wasn't a response, the returned status code will be 0.
874 *
875 * \see M_http_simple_read_reason_phrase
876 *
877 * \param[in] simple Parsed HTTP message.
878 *
879 * \return HTTP status code (200, 404, etc.), or 0 if this isn't a response.
880 */
882
883
884/*! Return the human-readable status of the parsed message.
885 *
886 * This is the text that goes with the HTTP status code in the message.
887 *
888 * The reason phrase is only set for response messages (type == M_HTTP_MESSAGE_TYPE_RESPONSE).
889 * If the parsed message wasn't a response, the returned string will be \c NULL.
890 *
891 * \param[in] simple Parsed HTTP message.
892 *
893 * \return String describing reason for message's status code, or \c NULL if this isn't a response.
894 *
895 * \see M_http_simple_read_status_code
896 */
898
899
900/*! Return the HTTP method (GET, POST, etc) of the parsed message.
901 *
902 * The method is only set for request messages (type == M_HTTP_MESSAGE_TYPE_REQUEST).
903 * If the parsed message wasn't a request, M_HTTP_METHOD_UNKNOWN will be returned.
904 *
905 * \param[in] simple Parsed HTTP message.
906 *
907 * \return HTTP verb used by the parsed message, or M_HTTP_METHOD_UNKNOWN if this isn't a request.
908 */
910
911
912/*! Return the full URI (port, path and query) of the parsed message.
913 *
914 * Only request messages have a URI. If the parsed message wasn't a request, the
915 * returned string will be \c NULL.
916 *
917 * \param[in] simple Parsed HTTP message.
918 *
919 * \return URI of the parsed message, or \c NULL if this isn't a request.
920 *
921 * \see M_http_simple_read_port
922 * \see M_http_simple_read_path
923 * \see M_http_simple_read_query_string
924 * \see M_http_simple_read_query_args
925 */
926M_API const char *M_http_simple_read_uri(const M_http_simple_read_t *simple);
927
928
929/*! Return the path component of the URI from the parsed message.
930 *
931 * Only request messages have a URI. If the parsed message wasn't a request, the
932 * function will return \c NULL.
933 *
934 * The path may be relative or absolute.
935 *
936 * \param[in] simple Parsed HTTP message.
937 *
938 * \return Path part of URI, or \c NULL if this isn't a request.
939 *
940 * \see M_http_simple_read_uri
941 */
942M_API const char *M_http_simple_read_path(const M_http_simple_read_t *simple);
943
944
945/*! Return the query component of the URI from the parsed message.
946 *
947 * The returned query string hasn't been processed in any way. Call M_http_simple_read_query_args()
948 * instead to process the query and return its contents as a set of key-value pairs.
949 *
950 * Only request messages have a URI. If the parsed message wasn't a request, the
951 * function will return \c NULL.
952 *
953 * Not all requests have a query string embedded in the URI. This is normally seen
954 * in GET requests, but it's not always present even there.
955 *
956 * \param[in] simple Parsed HTTP message.
957 *
958 * \return Query string from URI, or \c NULL if not present.
959 *
960 * \see M_http_simple_read_query_args
961 * \see M_http_simple_read_uri
962 */
964
965
966/*! Parse arguments from query component of URI as key-value pairs.
967 *
968 * Processes the query string (if any), then returns a key->value mapping of all
969 * the values present in the string.
970 *
971 * \warning
972 * Any keys in the query string that don't have values (no '='), or whose values
973 * are empty ('key=') will not be present in the returned mapping. To parse empty
974 * keys, you have to process the query string returned by M_http_simple_read_query_string()
975 * yourself.
976 *
977 * \param[in] simple Parsed HTTP message.
978 *
979 * \return Dictionary containing key-value mappings from query string, or \c NULL if there were none.
980 *
981 * \see M_http_simple_read_query_string
982 * \see M_http_simple_read_uri
983 */
985
986
987/*! Return the host.
988 *
989 * Only request messages have a host. If the parsed message wasn't a request, the
990 * function will return NULL.
991 *
992 * The host will be read from the URI and fall back to the Host header.
993 * If neither are present the host will be NULL.
994 *
995 * \param[in] simple Parsed HTTP message.
996 *
997 * \return Host.
998 */
999M_API const char *M_http_simple_read_host(const M_http_simple_read_t *simple);
1000
1001
1002/*! Return the port number.
1003 *
1004 * Only request messages have a port. If the parsed message wasn't a request, the
1005 * function will return M_FALSE and set \a port to 0.
1006 *
1007 * The port will be read from the URI and fall back to the Host header.
1008 * If neither are present the port will be returned as 0. In this case it
1009 * should be assumed to be 80.
1010 *
1011 * \param[in] simple Parsed HTTP message.
1012 * \param[out] port Place to store port number. May be \c NULL, if you're just checking to see if a port is present.
1013 *
1014 * \return M_TRUE if a port was set, M_FALSE if there was no port in the message.
1015 */
1016M_API M_bool M_http_simple_read_port(const M_http_simple_read_t *simple, M_uint16 *port);
1017
1018
1019
1020/*! Get headers from parsed message as key-multivalue pairs.
1021 *
1022 * Note that some headers may contain a list of multiple values, so the returned
1023 * M_hash_dict_t is a multimap (one key may map to a list of values).
1024 * The first entry in the list is the first item in the value list if there are
1025 * multiple values.
1026 *
1027 * Modfiers that are part of the value are included with the value.
1028 *
1029 * Header names are not case-sensitive, when doing lookups into the returned dictionary.
1030 *
1031 * \warning
1032 * M_http_simple_read_header should be used in order to get a full header. This is
1033 * a parsed list and is provided to make searching for specific values easier.
1034 * Use M_http_simple_read_headers to get a list of headers.
1035 *
1036 * \warning
1037 * The returned dictionary does not include "Set-Cookie" headers, because they can
1038 * be sent multiple times with different attributes, and their values cannot be
1039 * merged into a list.
1040 *
1041 * \param[in] simple Parsed HTTP message.
1042 *
1043 * \return Multimap of header names and values.
1044 *
1045 * \see M_http_simple_read_header
1046 * \see M_http_simple_read_headers
1047 * \see M_http_simple_read_get_set_cookie
1048 */
1050
1051
1052/*! Get a list of headers.
1053 *
1054 * Header names are not case-sensitive.
1055 *
1056 * \warning
1057 * The returned list does not include "Set-Cookie" headers, because they can
1058 * be sent multiple times with different attributes, and their values cannot be
1059 * merged into a list.
1060 *
1061 * \param[in] simple Parsed HTTP message.
1062 *
1063 * \return List of headers
1064 *
1065 * \see M_http_simple_read_header
1066 * \see M_http_simple_read_get_set_cookie
1067 */
1069
1070
1071/*! Get value of the named header from the parsed message.
1072 *
1073 * The key is not case-sensitive - it will match header names that only differ because
1074 * of capitalization.
1075 *
1076 * Note that some headers may contain a list of multiple values. For these headers,
1077 * this function will return a comma-delimited list of values. Some extra white space
1078 * may be added in addition to the commas.
1079 *
1080 * \warning
1081 * Attempts to retrieve "Set-Cookie" header values with this function will fail, because
1082 * those headers may be sent multiple times with different attributes, and their values
1083 * cannot be merged into a list.
1084 *
1085 * \param[in] simple Parsed HTTP message.
1086 * \param[in] key Name of header to retrieve values from.
1087 *
1088 * \return Delimited list of values (if necessary) for this header, or \c NULL if no data found.
1089 *
1090 * \see M_http_simple_read_headers
1091 * \see M_http_simple_read_get_set_cookie
1092 */
1093M_API char *M_http_simple_read_header(const M_http_simple_read_t *simple, const char *key);
1094
1095
1096/*! Return list of values from all Set-Cookie headers in the parsed message.
1097 *
1098 * \note
1099 * This does not set anything, it is an accessor to get the "Set-Cookie"
1100 * header field. The header is called "Set-Cookie" and can be set
1101 * multiple times with different values.
1102 *
1103 * The returned list of values is stable-sorted alphabetically.
1104 *
1105 * \param[in] simple Parsed HTTP message.
1106 *
1107 * \return Sorted list of all cookies in the message (may be empty).
1108 *
1109 * \see M_http_simple_read_header
1110 * \see M_http_simple_read_headers
1111 */
1113
1114
1115/*! Whether the decoded body data is form encoded and can be accessed using
1116 * M_http_simple_read_body_form_data(). If the text codec is set, the codec
1117 * represents the codec of the form data key value pairs. While M_http_simple_read_body()
1118 * will still be encoded as form data.
1119 *
1120 * \param[in] simple Parsed HTTP message.
1121 *
1122 * \return Bytes from body of message.
1123 */
1125
1126
1127/*! Return the body of the parsed message (if any).
1128 *
1129 * If the body is application/x-www-form-urlencoded this will return
1130 * the raw form data encoded body. Form data decodes to key value pairs
1131 * so accessing the data will need to be used through M_http_simple_read_body_form_data().
1132 *
1133 * When not form encoded. The body data will be in be represented by the codec
1134 * from M_http_simple_read_codec().
1135 *
1136 * \see M_http_simple_read_body_form_data()
1137 *
1138 * \param[in] simple Parsed HTTP message.
1139 * \param[out] len Place to store length of body (may be \c NULL).
1140 *
1141 * \return Bytes from body of message.
1142 */
1143M_API const unsigned char *M_http_simple_read_body(const M_http_simple_read_t *simple, size_t *len);
1144
1145
1146/*! Return the body of the parsed message (if any) if it was form data encoded.
1147 *
1148 * This will only be filled when the content-type is application/x-www-form-urlencoded
1149 * and there is key value pair body data. The data within the key value pairs will have
1150 * be represented by the codec from M_http_simple_read_codec().
1151 *
1152 * \param[in] simple Parsed HTTP message.
1153 *
1154 * \return Bytes from body of message.
1155 */
1157
1158
1159/*! Get the content type.
1160 *
1161 * May be empty if the content type was not set. Or if it was
1162 * application/x-www-form-urlencoded and the body was auto decoded.
1163 *
1164 * application/x-www-form-urlencoded does not allows allow the encoding
1165 * to be known. Preventing auto deciding from taking place. Leaving this
1166 * as application/x-www-form-urlencoded and the codec as unknown.
1167 *
1168 * \param[in] simple Parsed HTTP message.
1169 *
1170 * \return The content type.
1171 */
1173
1174
1175/*! Get the codec of the data.
1176 *
1177 * Codec is detected by charset from the Content-Type header. May be unknown if charset was not present
1178 * or if the charset is not supported by mstdlib's text encoding functionality.
1179 *
1180 * If body decoding takes place and the data will have been be decoded to utf-8.
1181 * The codec will have been updated to utf-8 and the content length will have been
1182 * updated to match the new body length.
1183 *
1184 * \param[in] simple Parsed HTTP message.
1185 *
1186 * \return Encoded data codec.
1187 *
1188 * \see M_http_simple_read_charset
1189 */
1191
1192
1193/*! Get the text charset of the data.
1194 *
1195 * The charater set as defined in the Content-Type header. This should correspond to
1196 * codec returned by M_http_simple_read_codec. However, if codec is unknown due to not
1197 * being supported by mstdlib's text codec, this is the charset used by the data.
1198 * Body decoding will not take place if codec is unknown. It is up to the application
1199 * to determine how to decoded the body data in this situation.
1200 *
1201 * \param[in] simple Parsed HTTP message.
1202 *
1203 * \return Character set.
1204 *
1205 * \see M_http_simple_read_codec
1206 */
1207M_API const char *M_http_simple_read_charset(const M_http_simple_read_t *simple);
1208
1209/*! @} */
1210
1211/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1212
1213/*! \addtogroup m_http_simple_write HTTP Simple Writer
1214 * \ingroup m_http_simple
1215 *
1216 * Generate request and response messages.
1217 *
1218 * Does not support:
1219 * - Multipart messages.
1220 * - Chunked data messages.
1221 *
1222 * @{
1223 */
1224
1225/*! Create an HTTP/1.1 request message, return as a new string.
1226 *
1227 * Caller is responsible for freeing the returned string.
1228 *
1229 * If the Content-Length header is not provided in \a headers, it will be added
1230 * automatically for you, using \a data_len as the length. Data can be NULL
1231 * If either the header or data_len is provided. Data can be sent later.
1232 * This allows generating the header and sending large messages without
1233 * buffering the data in memory. In this case, this function will generate
1234 * the necessary HTTP header part of the message.
1235 *
1236 * If set host, port, uri, user_agent, and content_type will all override
1237 * any that were previoiusly set in the headers.
1238 *
1239 * \see M_http_simple_write_request_buf
1240 *
1241 * \param[in] method HTTP verb to use (GET, POST, etc).
1242 * \param[in] host Host the request will be sent to. Required if 'Host' header is not set.
1243 * \param[in] port Port the request will be sent on. 0 or 80 will not set the port part of the 'Host' Header.
1244 * However, if the port is 0 it is assumed to be 80. Will be ignored if 'Host' header is set.
1245 * \param[in] uri URI (may be absolute or relative, may include query string). Typically, this will be relative.
1246 * if absolute, the host and port should match the host and port parameters.
1247 * \param[in] user_agent Optional user agent to include in the request.
1248 * \param[in] content_type Optional content type. If not set and not in headers this will default to 'plain/text'.
1249 * \param[in] headers Additional headers to include in request.
1250 * \param[in] data String to place in body of message (may be empty).
1251 * \param[in] data_len Number of chars to use from \c data (may be 0). Cannot be 0 if data is NULL.
1252 * \param[in] charset Encoding of the data. M_TEXTCODEC_UNKNOWN should be used if charset should not be added
1253 * to the Content-Type header. This will overwrite the charset if already present in
1254 * Content-Type. Also use M_TEXTCODEC_UNKNOWN for non-text data.
1255 * \param[out] len Place to store length of returned HTTP request message (may be NULL).
1256 *
1257 * \return Allocated string containing HTTP request message. The string will be NULL terminated.
1258 */
1260 const char *host, unsigned short port, const char *uri,
1261 const char *user_agent, const char *content_type, const M_hash_dict_t *headers,
1262 const unsigned char *data, size_t data_len, const char *charset, size_t *len);
1263
1264
1265/*! Create an HTTP/1.1 request message, add it to the given buffer.
1266 *
1267 * Same as M_http_simple_write_request(), except that it adds the new message to the given buffer instead
1268 * of returning it in a newly-allocated string.
1269 *
1270 * \param[out] buf Buffer to add the message to.
1271 * \param[in] method HTTP verb to use (GET, POST, etc).
1272 * \param[in] host Host the request will be sent to. Required if 'Host' header is not set.
1273 * \param[in] port Port the request will be sent on. 0 or 80 will not set the port part of the 'Host' Header.
1274 * However, if the port is 0 it is assumed to be 80. Will be ignored if 'Host' header is set.
1275 * \param[in] uri URI (may be absolute or relative, may include query string). Typically, this will be relative.
1276 * if absolute, the host and port should match the host and port parameters.
1277 * \param[in] user_agent Optional user agent to include in the request.
1278 * \param[in] content_type Optional content type. If not set and not in headers this will default to 'plain/text'.
1279 * \param[in] headers Additional headers to include in request.
1280 * \param[in] data String to place in body of message (may be empty).
1281 * \param[in] data_len Number of chars to use from \c data (may be 0). Cannot be 0 if data is NULL.
1282 * \param[in] charset Encoding of the data. Set NULL or empty to not add a charset to the Content-Type header.
1283 * If set, this will overwrite the charset if already present in Content-Type.
1284 *
1285 * \return M_TRUE if add was successful, M_FALSE if message creation failed.
1286 *
1287 * \see M_http_simple_write_request
1288 */
1290 const char *host, unsigned short port, const char *uri,
1291 const char *user_agent, const char *content_type, const M_hash_dict_t *headers,
1292 const unsigned char *data, size_t data_len, const char *charset);
1293
1294
1295/*! Create an HTTP/1.1 response message, return as new string.
1296 *
1297 * Caller is responsible for freeing the returned string.
1298 *
1299 * If the Content-Length header is not provided in \a headers, it will be added
1300 * automatically for you, using \a data_len as the length. Data can be NULL
1301 * If either the header or data_len is provided. Data can be sent later.
1302 * This allows generating the header and sending large messages without
1303 * buffering the data in memory. In this case, this function will generate
1304 * the necessary HTTP header part of the message.
1305 *
1306 * \param[in] code HTTP status code to use (200, 404, etc).
1307 * \param[in] reason HTTP status reason string. If NULL, will attempt to pick one automatically based on code.
1308 * \param[in] content_type Optional content type. If not set and not in headers this will default to 'plain/text'.
1309 * \param[in] headers Headers to include in response.
1310 * \param[in] data String to place in body of message (may be empty).
1311 * \param[in] data_len Number of chars to use from \c data (may be 0).
1312 * \param[in] charset Encoding of the data. Set NULL or empty to not add a charset to the Content-Type header.
1313 * If set, this will overwrite the charset if already present in Content-Type.
1314 * \param[out] len Place to store length of returned HTTP response message (may be NULL).
1315 *
1316 * \return New string containing HTTP response message.
1317 *
1318 * \see M_http_simple_write_response_buf
1319 */
1320M_API unsigned char *M_http_simple_write_response(M_uint32 code, const char *reason,
1321 const char *content_type, const M_hash_dict_t *headers, const unsigned char *data, size_t data_len,
1322 const char *charset, size_t *len);
1323
1324
1325/*! Create an HTTP/1.1 response message, add it to the given buffer.
1326 *
1327 * Same as M_http_simple_write_response(), except that it adds the new message to the given buffer instead
1328 * of returning it in a newly-allocated string.
1329 *
1330 * \param[out] buf Buffer to add the message to.
1331 * \param[in] code HTTP status code to use (200, 404, etc).
1332 * \param[in] reason HTTP status reason string. If NULL, will attempt to pick one automatically based on code.
1333 * \param[in] content_type Optional content type. If not set and not in headers this will default to 'plain/text'.
1334 * \param[in] headers Headers to include in response.
1335 * \param[in] data String to place in body of message (may be empty).
1336 * \param[in] data_len Number of chars to use from \c data (may be 0).
1337 * \param[in] charset Encoding of the data. Set NULL or empty to not add a charset to the Content-Type header.
1338 * If set, this will overwrite the charset if already present in Content-Type.
1339 *
1340 * \return M_TRUE if add was successful, M_FALSE if message creation failed.
1341 *
1342 * \see M_http_simple_write_response
1343 */
1344M_API M_bool M_http_simple_write_response_buf(M_buf_t *buf, M_uint32 code, const char *reason,
1345 const char *content_type, const M_hash_dict_t *headers, const unsigned char *data, size_t data_len,
1346 const char *charset);
1347
1348/*! @} */
1349
1350/*! @} */
1351
1352__END_DECLS
1353
1354#endif /* __M_HTTP_H__ */
struct M_buf M_buf_t
Definition: m_buf.h:77
struct M_hash_dict M_hash_dict_t
Definition: m_hash_dict.h:52
M_http_reader_chunk_data_done_func chunk_data_done_func
Definition: m_http.h:677
M_http_reader_multipart_data_func multipart_data_func
Definition: m_http.h:684
M_http_reader_trailer_full_func trailer_full_func
Definition: m_http.h:689
M_http_reader_header_done_func header_done_func
Definition: m_http.h:671
M_http_reader_body_func body_func
Definition: m_http.h:672
M_http_reader_chunk_extensions_func chunk_extensions_func
Definition: m_http.h:674
M_http_reader_chunk_extensions_done_func chunk_extensions_done_func
Definition: m_http.h:675
M_http_reader_trailer_done_func trailer_done_func
Definition: m_http.h:691
M_http_reader_trailer_func trailer_func
Definition: m_http.h:690
M_http_reader_header_full_func header_full_func
Definition: m_http.h:669
M_http_reader_multipart_header_done_func multipart_header_done_func
Definition: m_http.h:683
M_http_reader_multipart_epilouge_done_func multipart_epilouge_done_func
Definition: m_http.h:688
M_http_reader_chunk_data_finished_func chunk_data_finished_func
Definition: m_http.h:678
M_http_reader_multipart_preamble_func multipart_preamble_func
Definition: m_http.h:679
M_http_reader_multipart_data_done_func multipart_data_done_func
Definition: m_http.h:685
M_http_reader_header_func header_func
Definition: m_http.h:670
M_http_reader_multipart_data_finished_func multipart_data_finished_func
Definition: m_http.h:686
M_http_reader_start_func start_func
Definition: m_http.h:668
M_http_reader_multipart_preamble_done_func multipart_preamble_done_func
Definition: m_http.h:680
M_http_reader_multipart_header_func multipart_header_func
Definition: m_http.h:682
M_http_reader_body_done_func body_done_func
Definition: m_http.h:673
M_http_reader_multipart_header_full_func multipart_header_full_func
Definition: m_http.h:681
M_http_reader_multipart_epilouge_func multipart_epilouge_func
Definition: m_http.h:687
M_http_reader_chunk_data_func chunk_data_func
Definition: m_http.h:676
M_http_error_t(* M_http_reader_body_func)(const unsigned char *data, size_t len, void *thunk)
Definition: m_http.h:411
M_http_error_t(* M_http_reader_multipart_header_done_func)(size_t idx, void *thunk)
Definition: m_http.h:550
M_http_error_t(* M_http_reader_header_func)(const char *key, const char *val, void *thunk)
Definition: m_http.h:392
struct M_http_reader M_http_reader_t
Definition: m_http.h:334
void M_http_reader_destroy(M_http_reader_t *httpr)
M_http_error_t(* M_http_reader_chunk_data_done_func)(size_t idx, void *thunk)
Definition: m_http.h:465
M_http_reader_t * M_http_reader_create(struct M_http_reader_callbacks *cbs, M_uint32 flags, void *thunk)
M_http_error_t(* M_http_reader_multipart_header_full_func)(const char *key, const char *val, size_t idx, void *thunk)
Definition: m_http.h:518
M_http_error_t M_http_reader_read(M_http_reader_t *httpr, const unsigned char *data, size_t data_len, size_t *len_read)
M_http_error_t(* M_http_reader_multipart_data_done_func)(size_t idx, void *thunk)
Definition: m_http.h:570
M_http_error_t(* M_http_reader_header_done_func)(M_http_data_format_t format, void *thunk)
Definition: m_http.h:401
M_http_reader_flags_t
Definition: m_http.h:660
M_http_error_t(* M_http_reader_multipart_preamble_done_func)(void *thunk)
Definition: m_http.h:497
M_http_error_t(* M_http_reader_trailer_func)(const char *key, const char *val, void *thunk)
Definition: m_http.h:644
M_http_error_t(* M_http_reader_chunk_data_finished_func)(void *thunk)
Definition: m_http.h:475
M_http_error_t(* M_http_reader_header_full_func)(const char *key, const char *val, void *thunk)
Definition: m_http.h:368
M_http_error_t(* M_http_reader_body_done_func)(void *thunk)
Definition: m_http.h:421
M_http_error_t(* M_http_reader_multipart_header_func)(const char *key, const char *val, size_t idx, void *thunk)
Definition: m_http.h:541
M_http_error_t(* M_http_reader_multipart_epilouge_done_func)(void *thunk)
Definition: m_http.h:602
M_http_error_t(* M_http_reader_trailer_full_func)(const char *key, const char *val, void *thunk)
Definition: m_http.h:622
M_http_error_t(* M_http_reader_multipart_data_finished_func)(void *thunk)
Definition: m_http.h:580
M_http_error_t(* M_http_reader_start_func)(M_http_message_type_t type, M_http_version_t version, M_http_method_t method, const char *uri, M_uint32 code, const char *reason, void *thunk)
Definition: m_http.h:348
M_http_error_t(* M_http_reader_chunk_data_func)(const unsigned char *data, size_t len, size_t idx, void *thunk)
Definition: m_http.h:456
M_http_error_t(* M_http_reader_chunk_extensions_done_func)(size_t idx, void *thunk)
Definition: m_http.h:445
M_http_error_t(* M_http_reader_multipart_data_func)(const unsigned char *data, size_t len, size_t idx, void *thunk)
Definition: m_http.h:561
M_http_error_t(* M_http_reader_trailer_done_func)(void *thunk)
Definition: m_http.h:654
M_http_error_t(* M_http_reader_multipart_epilouge_func)(const unsigned char *data, size_t len, void *thunk)
Definition: m_http.h:592
M_http_error_t(* M_http_reader_multipart_preamble_func)(const unsigned char *data, size_t len, void *thunk)
Definition: m_http.h:487
M_http_error_t(* M_http_reader_chunk_extensions_func)(const char *key, const char *val, size_t idx, void *thunk)
Definition: m_http.h:434
@ M_HTTP_READER_NONE
Definition: m_http.h:661
@ M_HTTP_READER_SKIP_START
Definition: m_http.h:662
Definition: m_http.h:667
M_hash_dict_t * M_http_simple_read_headers_dict(const M_http_simple_read_t *simple)
M_http_error_t M_http_simple_read(M_http_simple_read_t **simple, const unsigned char *data, size_t data_len, M_uint32 flags, size_t *len_read)
M_http_version_t M_http_simple_read_version(const M_http_simple_read_t *simple)
M_http_method_t M_http_simple_read_method(const M_http_simple_read_t *simple)
char * M_http_simple_read_header(const M_http_simple_read_t *simple, const char *key)
const char * M_http_simple_read_content_type(const M_http_simple_read_t *simple)
const char * M_http_simple_read_uri(const M_http_simple_read_t *simple)
M_textcodec_codec_t M_http_simple_read_codec(const M_http_simple_read_t *simple)
const char * M_http_simple_read_reason_phrase(const M_http_simple_read_t *simple)
const char * M_http_simple_read_query_string(const M_http_simple_read_t *simple)
M_list_str_t * M_http_simple_read_headers(const M_http_simple_read_t *simple)
M_bool M_http_simple_read_is_body_form_data(const M_http_simple_read_t *simple)
const M_list_str_t * M_http_simple_read_get_set_cookie(const M_http_simple_read_t *simple)
const unsigned char * M_http_simple_read_body(const M_http_simple_read_t *simple, size_t *len)
const char * M_http_simple_read_charset(const M_http_simple_read_t *simple)
M_bool M_http_simple_read_port(const M_http_simple_read_t *simple, M_uint16 *port)
struct M_http_simple_read M_http_simple_read_t
Definition: m_http.h:785
const char * M_http_simple_read_path(const M_http_simple_read_t *simple)
const M_hash_dict_t * M_http_simple_read_query_args(const M_http_simple_read_t *simple)
M_uint32 M_http_simple_read_status_code(const M_http_simple_read_t *simple)
void M_http_simple_read_destroy(M_http_simple_read_t *http)
M_http_simple_read_flags_t
Definition: m_http.h:788
M_http_error_t M_http_simple_read_parser(M_http_simple_read_t **simple, M_parser_t *parser, M_uint32 flags)
M_http_message_type_t M_http_simple_read_message_type(const M_http_simple_read_t *simple)
const M_hash_dict_t * M_http_simple_read_body_form_data(const M_http_simple_read_t *simple)
const char * M_http_simple_read_host(const M_http_simple_read_t *simple)
@ M_HTTP_SIMPLE_READ_FAIL_TRAILERS
Definition: m_http.h:793
@ M_HTTP_SIMPLE_READ_LEN_REQUIRED
Definition: m_http.h:791
@ M_HTTP_SIMPLE_READ_NONE
Definition: m_http.h:789
@ M_HTTP_SIMPLE_READ_FAIL_EXTENSION
Definition: m_http.h:792
@ M_HTTP_SIMPLE_READ_NODECODE_BODY
Definition: m_http.h:790
M_bool M_http_simple_write_response_buf(M_buf_t *buf, M_uint32 code, const char *reason, const char *content_type, const M_hash_dict_t *headers, const unsigned char *data, size_t data_len, const char *charset)
unsigned char * M_http_simple_write_request(M_http_method_t method, const char *host, unsigned short port, const char *uri, const char *user_agent, const char *content_type, const M_hash_dict_t *headers, const unsigned char *data, size_t data_len, const char *charset, size_t *len)
unsigned char * M_http_simple_write_response(M_uint32 code, const char *reason, const char *content_type, const M_hash_dict_t *headers, const unsigned char *data, size_t data_len, const char *charset, size_t *len)
M_bool M_http_simple_write_request_buf(M_buf_t *buf, M_http_method_t method, const char *host, unsigned short port, const char *uri, const char *user_agent, const char *content_type, const M_hash_dict_t *headers, const unsigned char *data, size_t data_len, const char *charset)
M_http_version_t M_http_version_from_str(const char *version)
char * M_http_generate_form_data_string(const M_hash_dict_t *params)
M_http_data_format_t
Definition: m_http.h:151
M_http_method_t M_http_method_from_str(const char *method)
M_bool M_http_generate_query_string_buf(M_buf_t *buf, const char *uri, const M_hash_dict_t *params)
const char * M_http_version_to_str(M_http_version_t version)
M_http_version_t
Definition: m_http.h:128
M_hash_dict_t * M_http_parse_query_string(const char *data, M_textcodec_codec_t codec)
M_http_error_t
Definition: m_http.h:84
M_http_method_t
Definition: m_http.h:136
M_bool M_http_generate_form_data_string_buf(M_buf_t *buf, const M_hash_dict_t *params)
M_hash_dict_t * M_http_parse_form_data_string(const char *data, M_textcodec_codec_t codec)
M_http_message_type_t
Definition: m_http.h:120
const char * M_http_errcode_to_str(M_http_error_t err)
char * M_http_generate_query_string(const char *uri, const M_hash_dict_t *params)
const char * M_http_code_to_reason(M_uint32 code)
const char * M_http_method_to_str(M_http_method_t method)
@ M_HTTP_DATA_FORMAT_BODY
Definition: m_http.h:154
@ M_HTTP_DATA_FORMAT_CHUNKED
Definition: m_http.h:155
@ M_HTTP_DATA_FORMAT_UNKNOWN
Definition: m_http.h:152
@ M_HTTP_DATA_FORMAT_NONE
Definition: m_http.h:153
@ M_HTTP_DATA_FORMAT_MULTIPART
Definition: m_http.h:156
@ M_HTTP_VERSION_1_1
Definition: m_http.h:131
@ M_HTTP_VERSION_UNKNOWN
Definition: m_http.h:129
@ M_HTTP_VERSION_1_0
Definition: m_http.h:130
@ M_HTTP_ERROR_REQUEST_METHOD
Definition: m_http.h:97
@ M_HTTP_ERROR_STARTLINE_LENGTH
Definition: m_http.h:94
@ M_HTTP_ERROR_CHUNK_EXTENSION_NOTALLOWED
Definition: m_http.h:91
@ M_HTTP_ERROR_CHUNK_STARTLINE_LENGTH
Definition: m_http.h:102
@ M_HTTP_ERROR_SUCCESS_MORE_POSSIBLE
Definition: m_http.h:86
@ M_HTTP_ERROR_TRAILER_NOTALLOWED
Definition: m_http.h:92
@ M_HTTP_ERROR_TEXTCODEC_FAILURE
Definition: m_http.h:114
@ M_HTTP_ERROR_MULTIPART_INVALID
Definition: m_http.h:112
@ M_HTTP_ERROR_CONTENT_LENGTH_MALFORMED
Definition: m_http.h:107
@ M_HTTP_ERROR_MOREDATA
Definition: m_http.h:87
@ M_HTTP_ERROR_URI
Definition: m_http.h:93
@ M_HTTP_ERROR_MULTIPART_MISSING_DATA
Definition: m_http.h:111
@ M_HTTP_ERROR_HEADER_INVALID
Definition: m_http.h:100
@ M_HTTP_ERROR_UNSUPPORTED_DATA
Definition: m_http.h:113
@ M_HTTP_ERROR_NOT_HTTP
Definition: m_http.h:108
@ M_HTTP_ERROR_STOP
Definition: m_http.h:88
@ M_HTTP_ERROR_CHUNK_DATA_MALFORMED
Definition: m_http.h:106
@ M_HTTP_ERROR_STARTLINE_MALFORMED
Definition: m_http.h:95
@ M_HTTP_ERROR_USER_FAILURE
Definition: m_http.h:115
@ M_HTTP_ERROR_MULTIPART_MISSING
Definition: m_http.h:110
@ M_HTTP_ERROR_HEADER_DUPLICATE
Definition: m_http.h:101
@ M_HTTP_ERROR_MULTIPART_NOBOUNDARY
Definition: m_http.h:109
@ M_HTTP_ERROR_HEADER_LENGTH
Definition: m_http.h:98
@ M_HTTP_ERROR_CHUNK_LENGTH
Definition: m_http.h:103
@ M_HTTP_ERROR_UNKNOWN_VERSION
Definition: m_http.h:96
@ M_HTTP_ERROR_CHUNK_EXTENSION
Definition: m_http.h:105
@ M_HTTP_ERROR_CHUNK_MALFORMED
Definition: m_http.h:104
@ M_HTTP_ERROR_SUCCESS
Definition: m_http.h:85
@ M_HTTP_ERROR_INVALIDUSE
Definition: m_http.h:89
@ M_HTTP_ERROR_HEADER_FOLD
Definition: m_http.h:99
@ M_HTTP_ERROR_LENGTH_REQUIRED
Definition: m_http.h:90
@ M_HTTP_METHOD_OPTIONS
Definition: m_http.h:138
@ M_HTTP_METHOD_DELETE
Definition: m_http.h:143
@ M_HTTP_METHOD_CONNECT
Definition: m_http.h:145
@ M_HTTP_METHOD_HEAD
Definition: m_http.h:140
@ M_HTTP_METHOD_UNKNOWN
Definition: m_http.h:137
@ M_HTTP_METHOD_PUT
Definition: m_http.h:142
@ M_HTTP_METHOD_GET
Definition: m_http.h:139
@ M_HTTP_METHOD_PATCH
Definition: m_http.h:146
@ M_HTTP_METHOD_POST
Definition: m_http.h:141
@ M_HTTP_METHOD_TRACE
Definition: m_http.h:144
@ M_HTTP_MESSAGE_TYPE_UNKNOWN
Definition: m_http.h:121
@ M_HTTP_MESSAGE_TYPE_REQUEST
Definition: m_http.h:122
@ M_HTTP_MESSAGE_TYPE_RESPONSE
Definition: m_http.h:123
struct M_list_str M_list_str_t
Definition: m_list_str.h:80
struct M_parser M_parser_t
Definition: m_parser.h:52
M_textcodec_codec_t
Definition: m_textcodec.h:103