diff options
Diffstat (limited to 'src/lib/response_from_buffer.c')
-rw-r--r-- | src/lib/response_from_buffer.c | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/src/lib/response_from_buffer.c b/src/lib/response_from_buffer.c new file mode 100644 index 00000000..be6a1e00 --- /dev/null +++ b/src/lib/response_from_buffer.c | |||
@@ -0,0 +1,88 @@ | |||
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_buffer.c | ||
22 | * @brief implementation of MHD_response_from_buffer() | ||
23 | * @author Christian Grothoff | ||
24 | */ | ||
25 | #include "internal.h" | ||
26 | |||
27 | |||
28 | /** | ||
29 | * Create a response object. 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 use for the response; | ||
33 | * #MHD_HTTP_NO_CONTENT is only valid if @a size is 0; | ||
34 | * @param size size of the data portion of the response | ||
35 | * @param buffer size bytes containing the response's data portion | ||
36 | * @param mode flags for buffer management | ||
37 | * @return NULL on error (i.e. invalid arguments, out of memory) | ||
38 | * @ingroup response | ||
39 | */ | ||
40 | struct MHD_Response * | ||
41 | MHD_response_from_buffer (enum MHD_HTTP_StatusCode sc, | ||
42 | size_t size, | ||
43 | void *buffer, | ||
44 | enum MHD_ResponseMemoryMode mode) | ||
45 | { | ||
46 | struct MHD_Response *response; | ||
47 | void *tmp; | ||
48 | |||
49 | mhd_assert ( (NULL != data) || | ||
50 | (0 == size) ); | ||
51 | if (NULL == | ||
52 | (response = MHD_calloc_ (1, | ||
53 | sizeof (struct MHD_Response)))) | ||
54 | return NULL; | ||
55 | response->fd = -1; | ||
56 | if (! MHD_mutex_init_ (&response->mutex)) | ||
57 | { | ||
58 | free (response); | ||
59 | return NULL; | ||
60 | } | ||
61 | if ( (MHD_RESPMEM_MUST_COPY == mode) && | ||
62 | (size > 0) ) | ||
63 | { | ||
64 | if (NULL == (tmp = malloc (size))) | ||
65 | { | ||
66 | MHD_mutex_destroy_chk_ (&response->mutex); | ||
67 | free (response); | ||
68 | return NULL; | ||
69 | } | ||
70 | memcpy (tmp, | ||
71 | data, | ||
72 | size); | ||
73 | data = tmp; | ||
74 | } | ||
75 | if (MHD_RESPMEM_PERSISTENT != mode) | ||
76 | { | ||
77 | response->crfc = &free; | ||
78 | response->crc_cls = data; | ||
79 | } | ||
80 | response->status_code = sc; | ||
81 | response->reference_count = 1; | ||
82 | response->total_size = size; | ||
83 | response->data = data; | ||
84 | response->data_size = size; | ||
85 | return response; | ||
86 | } | ||
87 | |||
88 | /* end of response_from_buffer.c */ | ||