1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
/*
This file is part of libmicrohttpd
Copyright (C) 2007-2018 Daniel Pittman and Christian Grothoff
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file lib/response.c
* @brief implementation of general response functions
* @author Daniel Pittman
* @author Christian Grothoff
* @author Karlson2k (Evgeny Grin)
*/
#include "internal.h"
/**
* Add a header or footer line to the response.
*
* @param response response to add a header to
* @param kind header or footer
* @param header the header to add
* @param content value to add
* @return false on error (i.e. invalid header or content format).
*/
static bool
add_response_entry (struct MHD_Response *response,
enum MHD_ValueKind kind,
const char *header,
const char *content)
{
struct MHD_HTTP_Header *hdr;
if ( (NULL == header) ||
(NULL == content) ||
(0 == header[0]) ||
(0 == content[0]) ||
(NULL != strchr (header, '\t')) ||
(NULL != strchr (header, '\r')) ||
(NULL != strchr (header, '\n')) ||
(NULL != strchr (content, '\t')) ||
(NULL != strchr (content, '\r')) ||
(NULL != strchr (content, '\n')) )
return false;
if (NULL == (hdr = malloc (sizeof (struct MHD_HTTP_Header))))
return false;
if (NULL == (hdr->header = strdup (header)))
{
free (hdr);
return false;
}
if (NULL == (hdr->value = strdup (content)))
{
free (hdr->header);
free (hdr);
return false;
}
hdr->kind = kind;
hdr->next = response->first_header;
response->first_header = hdr;
return true;
}
/**
* Explicitly decrease reference counter of a response object. If the
* counter hits zero, destroys a response object and associated
* resources. Usually, this is implicitly done by converting a
* response to an action and returning the action to MHD.
*
* @param response response to decrement RC of
* @ingroup response
*/
void
MHD_response_queue_for_destroy (struct MHD_Response *response)
{
struct MHD_HTTP_Header *pos;
MHD_mutex_lock_chk_ (&response->mutex);
if (0 != --(response->reference_count))
{
MHD_mutex_unlock_chk_ (&response->mutex);
return;
}
MHD_mutex_unlock_chk_ (&response->mutex);
MHD_mutex_destroy_chk_ (&response->mutex);
if (NULL != response->crfc)
response->crfc (response->crc_cls);
while (NULL != response->first_header)
{
pos = response->first_header;
response->first_header = pos->next;
free (pos->header);
free (pos->value);
free (pos);
}
free (response);
}
/**
* Add a header line to the response.
*
* @param response response to add a header to
* @param header the header to add
* @param content value to add
* @return #MHD_NO on error (i.e. invalid header or content format),
* or out of memory
* @ingroup response
*/
enum MHD_Bool
MHD_response_add_header (struct MHD_Response *response,
const char *header,
const char *content)
{
return add_response_entry (response,
MHD_HEADER_KIND,
header,
content) ? MHD_YES : MHD_NO;
}
/**
* Add a tailer line to the response.
*
* @param response response to add a footer to
* @param footer the footer to add
* @param content value to add
* @return #MHD_NO on error (i.e. invalid footer or content format),
* or out of memory
* @ingroup response
*/
enum MHD_Bool
MHD_response_add_trailer (struct MHD_Response *response,
const char *footer,
const char *content)
{
return add_response_entry (response,
MHD_FOOTER_KIND,
footer,
content) ? MHD_YES : MHD_NO;
}
/**
* Delete a header (or footer) line from the response.
*
* @param response response to remove a header from
* @param header the header to delete
* @param content value to delete
* @return #MHD_NO on error (no such header known)
* @ingroup response
*/
enum MHD_Bool
MHD_response_del_header (struct MHD_Response *response,
const char *header,
const char *content)
{
struct MHD_HTTP_Header *pos;
struct MHD_HTTP_Header *prev;
prev = NULL;
pos = response->first_header;
while (NULL != pos)
{
if ((0 == strcmp (header,
pos->header)) &&
(0 == strcmp (content,
pos->value)))
{
free (pos->header);
free (pos->value);
if (NULL == prev)
response->first_header = pos->next;
else
prev->next = pos->next;
free (pos);
return MHD_YES;
}
prev = pos;
pos = pos->next;
}
return MHD_NO;
}
/**
* Get all of the headers (and footers) added to a response.
*
* @param response response to query
* @param iterator callback to call on each header;
* maybe NULL (then just count headers)
* @param iterator_cls extra argument to @a iterator
* @return number of entries iterated over
* @ingroup response
*/
unsigned int
MHD_response_get_headers (struct MHD_Response *response,
MHD_KeyValueIterator iterator,
void *iterator_cls)
{
unsigned int numHeaders = 0;
struct MHD_HTTP_Header *pos;
for (pos = response->first_header;
NULL != pos;
pos = pos->next)
{
numHeaders++;
if ( (NULL != iterator) &&
(MHD_YES != iterator (iterator_cls,
pos->kind,
pos->header,
pos->value)) )
break;
}
return numHeaders;
}
/**
* Get a particular header (or footer) from the response.
*
* @param response response to query
* @param key which header to get
* @return NULL if header does not exist
* @ingroup response
*/
const char *
MHD_response_get_header (struct MHD_Response *response,
const char *key)
{
struct MHD_HTTP_Header *pos;
for (pos = response->first_header;
NULL != pos;
pos = pos->next)
{
if (MHD_str_equal_caseless_ (pos->header,
key))
return pos->value;
}
return NULL;
}
/* end of response.c */
|