diff options
Diffstat (limited to 'src/lib/response_from_callback.c')
-rw-r--r-- | src/lib/response_from_callback.c | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/src/lib/response_from_callback.c b/src/lib/response_from_callback.c new file mode 100644 index 00000000..1fd65bf0 --- /dev/null +++ b/src/lib/response_from_callback.c | |||
@@ -0,0 +1,80 @@ | |||
1 | /* | ||
2 | This file is part of libmicrohttpd | ||
3 | Copyright (C) 2007-2018 Daniel Pittman and Christian Grothoff | ||
4 | |||
5 | This library is free software; you can redistribute it and/or | ||
6 | modify it under the terms of the GNU Lesser General Public | ||
7 | License as published by the Free Software Foundation; either | ||
8 | version 2.1 of the License, or (at your option) any later version. | ||
9 | |||
10 | This library is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Lesser General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Lesser General Public | ||
16 | License along with this library; if not, write to the Free Software | ||
17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
18 | */ | ||
19 | |||
20 | /** | ||
21 | * @file lib/response_from_callback.c | ||
22 | * @brief implementation of MHD_response_from_callback() | ||
23 | * @author Christian Grothoff | ||
24 | */ | ||
25 | #include "internal.h" | ||
26 | |||
27 | |||
28 | /** | ||
29 | * Create a response action. The response object can be extended with | ||
30 | * header information and then be used any number of times. | ||
31 | * | ||
32 | * @param sc status code to return | ||
33 | * @param size size of the data portion of the response, #MHD_SIZE_UNKNOWN for unknown | ||
34 | * @param block_size preferred block size for querying crc (advisory only, | ||
35 | * MHD may still call @a crc using smaller chunks); this | ||
36 | * is essentially the buffer size used for IO, clients | ||
37 | * should pick a value that is appropriate for IO and | ||
38 | * memory performance requirements | ||
39 | * @param crc callback to use to obtain response data | ||
40 | * @param crc_cls extra argument to @a crc | ||
41 | * @param crfc callback to call to free @a crc_cls resources | ||
42 | * @return NULL on error (i.e. invalid arguments, out of memory) | ||
43 | * @ingroup response | ||
44 | */ | ||
45 | struct MHD_Response * | ||
46 | MHD_response_from_callback (enum MHD_HTTP_StatusCode sc, | ||
47 | uint64_t size, | ||
48 | size_t block_size, | ||
49 | MHD_ContentReaderCallback crc, | ||
50 | void *crc_cls, | ||
51 | MHD_ContentReaderFreeCallback crfc) | ||
52 | { | ||
53 | struct MHD_Response *response; | ||
54 | |||
55 | mhd_assert (NULL != crc); | ||
56 | mhd_assert (0 != block_size); | ||
57 | if (NULL == | ||
58 | (response = MHD_calloc_ (1, | ||
59 | sizeof (struct MHD_Response) + | ||
60 | block_size))) | ||
61 | return NULL; | ||
62 | response->fd = -1; | ||
63 | response->status_code = sc; | ||
64 | response->data = (void *) &response[1]; | ||
65 | response->data_buffer_size = block_size; | ||
66 | if (! MHD_mutex_init_ (&response->mutex)) | ||
67 | { | ||
68 | free (response); | ||
69 | return NULL; | ||
70 | } | ||
71 | response->crc = crc; | ||
72 | response->crfc = crfc; | ||
73 | response->crc_cls = crc_cls; | ||
74 | response->reference_count = 1; | ||
75 | response->total_size = size; | ||
76 | return response; | ||
77 | } | ||
78 | |||
79 | |||
80 | /* end of response_from_callback.c */ | ||