diff options
Diffstat (limited to 'src/daemon/response.c')
-rw-r--r-- | src/daemon/response.c | 257 |
1 files changed, 128 insertions, 129 deletions
diff --git a/src/daemon/response.c b/src/daemon/response.c index 0326a15b..412bf2ea 100644 --- a/src/daemon/response.c +++ b/src/daemon/response.c | |||
@@ -34,26 +34,25 @@ | |||
34 | * @return MHD_NO on error (i.e. invalid header or content format). | 34 | * @return MHD_NO on error (i.e. invalid header or content format). |
35 | */ | 35 | */ |
36 | int | 36 | int |
37 | MHD_add_response_header(struct MHD_Response * response, | 37 | MHD_add_response_header (struct MHD_Response *response, |
38 | const char * header, | 38 | const char *header, const char *content) |
39 | const char * content) { | 39 | { |
40 | struct MHD_HTTP_Header * hdr; | 40 | struct MHD_HTTP_Header *hdr; |
41 | 41 | ||
42 | if ( (response == NULL) || | 42 | if ((response == NULL) || |
43 | (header == NULL) || | 43 | (header == NULL) || |
44 | (content == NULL) || | 44 | (content == NULL) || |
45 | (strlen(header) == 0) || | 45 | (strlen (header) == 0) || |
46 | (strlen(content) == 0) || | 46 | (strlen (content) == 0) || |
47 | (NULL != strstr(header, "\t")) || | 47 | (NULL != strstr (header, "\t")) || |
48 | (NULL != strstr(header, "\r")) || | 48 | (NULL != strstr (header, "\r")) || |
49 | (NULL != strstr(header, "\n")) || | 49 | (NULL != strstr (header, "\n")) || |
50 | (NULL != strstr(content, "\t")) || | 50 | (NULL != strstr (content, "\t")) || |
51 | (NULL != strstr(content, "\r")) || | 51 | (NULL != strstr (content, "\r")) || (NULL != strstr (content, "\n"))) |
52 | (NULL != strstr(content, "\n")) ) | ||
53 | return MHD_NO; | 52 | return MHD_NO; |
54 | hdr = malloc(sizeof(struct MHD_HTTP_Header)); | 53 | hdr = malloc (sizeof (struct MHD_HTTP_Header)); |
55 | hdr->header = strdup(header); | 54 | hdr->header = strdup (header); |
56 | hdr->value = strdup(content); | 55 | hdr->value = strdup (content); |
57 | hdr->kind = MHD_HEADER_KIND; | 56 | hdr->kind = MHD_HEADER_KIND; |
58 | hdr->next = response->first_header; | 57 | hdr->next = response->first_header; |
59 | response->first_header = hdr; | 58 | response->first_header = hdr; |
@@ -66,32 +65,33 @@ MHD_add_response_header(struct MHD_Response * response, | |||
66 | * @return MHD_NO on error (no such header known) | 65 | * @return MHD_NO on error (no such header known) |
67 | */ | 66 | */ |
68 | int | 67 | int |
69 | MHD_del_response_header(struct MHD_Response * response, | 68 | MHD_del_response_header (struct MHD_Response *response, |
70 | const char * header, | 69 | const char *header, const char *content) |
71 | const char * content) { | 70 | { |
72 | struct MHD_HTTP_Header * pos; | 71 | struct MHD_HTTP_Header *pos; |
73 | struct MHD_HTTP_Header * prev; | 72 | struct MHD_HTTP_Header *prev; |
74 | 73 | ||
75 | if ( (header == NULL) || | 74 | if ((header == NULL) || (content == NULL)) |
76 | (content == NULL) ) | ||
77 | return MHD_NO; | 75 | return MHD_NO; |
78 | prev = NULL; | 76 | prev = NULL; |
79 | pos = response->first_header; | 77 | pos = response->first_header; |
80 | while (pos != NULL) { | 78 | while (pos != NULL) |
81 | if ( (0 == strcmp(header, pos->header)) && | 79 | { |
82 | (0 == strcmp(content, pos->value)) ) { | 80 | if ((0 == strcmp (header, pos->header)) && |
83 | free(pos->header); | 81 | (0 == strcmp (content, pos->value))) |
84 | free(pos->value); | 82 | { |
85 | if (prev == NULL) | 83 | free (pos->header); |
86 | response->first_header = pos->next; | 84 | free (pos->value); |
87 | else | 85 | if (prev == NULL) |
88 | prev->next = pos->next; | 86 | response->first_header = pos->next; |
89 | free(pos); | 87 | else |
90 | return MHD_YES; | 88 | prev->next = pos->next; |
89 | free (pos); | ||
90 | return MHD_YES; | ||
91 | } | ||
92 | prev = pos; | ||
93 | pos = pos->next; | ||
91 | } | 94 | } |
92 | prev = pos; | ||
93 | pos = pos->next; | ||
94 | } | ||
95 | return MHD_NO; | 95 | return MHD_NO; |
96 | } | 96 | } |
97 | 97 | ||
@@ -104,22 +104,21 @@ MHD_del_response_header(struct MHD_Response * response, | |||
104 | * @return number of entries iterated over | 104 | * @return number of entries iterated over |
105 | */ | 105 | */ |
106 | int | 106 | int |
107 | MHD_get_response_headers(struct MHD_Response * response, | 107 | MHD_get_response_headers (struct MHD_Response *response, |
108 | MHD_KeyValueIterator iterator, | 108 | MHD_KeyValueIterator iterator, void *iterator_cls) |
109 | void * iterator_cls) { | 109 | { |
110 | struct MHD_HTTP_Header * pos; | 110 | struct MHD_HTTP_Header *pos; |
111 | int numHeaders = 0; | 111 | int numHeaders = 0; |
112 | pos = response->first_header; | 112 | pos = response->first_header; |
113 | while (pos != NULL) { | 113 | while (pos != NULL) |
114 | numHeaders++; | 114 | { |
115 | if ( (iterator != NULL) && | 115 | numHeaders++; |
116 | (MHD_YES != iterator(iterator_cls, | 116 | if ((iterator != NULL) && |
117 | pos->kind, | 117 | (MHD_YES != iterator (iterator_cls, |
118 | pos->header, | 118 | pos->kind, pos->header, pos->value))) |
119 | pos->value)) ) | 119 | break; |
120 | break; | 120 | pos = pos->next; |
121 | pos = pos->next; | 121 | } |
122 | } | ||
123 | return numHeaders; | 122 | return numHeaders; |
124 | } | 123 | } |
125 | 124 | ||
@@ -131,16 +130,16 @@ MHD_get_response_headers(struct MHD_Response * response, | |||
131 | * @return NULL if header does not exist | 130 | * @return NULL if header does not exist |
132 | */ | 131 | */ |
133 | const char * | 132 | const char * |
134 | MHD_get_response_header(struct MHD_Response * response, | 133 | MHD_get_response_header (struct MHD_Response *response, const char *key) |
135 | const char * key) { | 134 | { |
136 | struct MHD_HTTP_Header * pos; | 135 | struct MHD_HTTP_Header *pos; |
137 | pos = response->first_header; | 136 | pos = response->first_header; |
138 | while (pos != NULL) { | 137 | while (pos != NULL) |
139 | if (0 == strcmp(key, | 138 | { |
140 | pos->header)) | 139 | if (0 == strcmp (key, pos->header)) |
141 | return pos->value; | 140 | return pos->value; |
142 | pos = pos->next; | 141 | pos = pos->next; |
143 | } | 142 | } |
144 | return NULL; | 143 | return NULL; |
145 | } | 144 | } |
146 | 145 | ||
@@ -156,29 +155,30 @@ MHD_get_response_header(struct MHD_Response * response, | |||
156 | * @return NULL on error (i.e. invalid arguments, out of memory) | 155 | * @return NULL on error (i.e. invalid arguments, out of memory) |
157 | */ | 156 | */ |
158 | struct MHD_Response * | 157 | struct MHD_Response * |
159 | MHD_create_response_from_callback(size_t size, | 158 | MHD_create_response_from_callback (size_t size, |
160 | MHD_ContentReaderCallback crc, | 159 | MHD_ContentReaderCallback crc, |
161 | void * crc_cls, | 160 | void *crc_cls, |
162 | MHD_ContentReaderFreeCallback crfc) { | 161 | MHD_ContentReaderFreeCallback crfc) |
163 | struct MHD_Response * retVal; | 162 | { |
163 | struct MHD_Response *retVal; | ||
164 | 164 | ||
165 | if (crc == NULL) | 165 | if (crc == NULL) |
166 | return NULL; | 166 | return NULL; |
167 | retVal = malloc(sizeof(struct MHD_Response)); | 167 | retVal = malloc (sizeof (struct MHD_Response)); |
168 | memset(retVal, | 168 | memset (retVal, 0, sizeof (struct MHD_Response)); |
169 | 0, | 169 | retVal->data = malloc (MHD_BUF_INC_SIZE); |
170 | sizeof(struct MHD_Response)); | 170 | if (retVal->data == NULL) |
171 | retVal->data = malloc(MHD_BUF_INC_SIZE); | 171 | { |
172 | if (retVal->data == NULL) { | 172 | free (retVal); |
173 | free(retVal); | 173 | return NULL; |
174 | return NULL; | 174 | } |
175 | } | ||
176 | retVal->data_buffer_size = MHD_BUF_INC_SIZE; | 175 | retVal->data_buffer_size = MHD_BUF_INC_SIZE; |
177 | if (pthread_mutex_init(&retVal->mutex, NULL) != 0) { | 176 | if (pthread_mutex_init (&retVal->mutex, NULL) != 0) |
178 | free(retVal->data); | 177 | { |
179 | free(retVal); | 178 | free (retVal->data); |
180 | return NULL; | 179 | free (retVal); |
181 | } | 180 | return NULL; |
181 | } | ||
182 | retVal->crc = crc; | 182 | retVal->crc = crc; |
183 | retVal->crfc = crfc; | 183 | retVal->crfc = crfc; |
184 | retVal->crc_cls = crc_cls; | 184 | retVal->crc_cls = crc_cls; |
@@ -200,33 +200,28 @@ MHD_create_response_from_callback(size_t size, | |||
200 | * @return NULL on error (i.e. invalid arguments, out of memory) | 200 | * @return NULL on error (i.e. invalid arguments, out of memory) |
201 | */ | 201 | */ |
202 | struct MHD_Response * | 202 | struct MHD_Response * |
203 | MHD_create_response_from_data(size_t size, | 203 | MHD_create_response_from_data (size_t size, |
204 | void * data, | 204 | void *data, int must_free, int must_copy) |
205 | int must_free, | 205 | { |
206 | int must_copy) { | 206 | struct MHD_Response *retVal; |
207 | struct MHD_Response * retVal; | 207 | void *tmp; |
208 | void * tmp; | ||
209 | 208 | ||
210 | if ( (data == NULL) && | 209 | if ((data == NULL) && (size > 0)) |
211 | (size > 0) ) | ||
212 | return NULL; | 210 | return NULL; |
213 | retVal = malloc(sizeof(struct MHD_Response)); | 211 | retVal = malloc (sizeof (struct MHD_Response)); |
214 | memset(retVal, | 212 | memset (retVal, 0, sizeof (struct MHD_Response)); |
215 | 0, | 213 | if (pthread_mutex_init (&retVal->mutex, NULL) != 0) |
216 | sizeof(struct MHD_Response)); | 214 | { |
217 | if (pthread_mutex_init(&retVal->mutex, NULL) != 0) { | 215 | free (retVal); |
218 | free(retVal); | 216 | return NULL; |
219 | return NULL; | 217 | } |
220 | } | 218 | if ((must_copy) && (size > 0)) |
221 | if ( (must_copy) && | 219 | { |
222 | (size > 0) ) { | 220 | tmp = malloc (size); |
223 | tmp = malloc(size); | 221 | memcpy (tmp, data, size); |
224 | memcpy(tmp, | 222 | must_free = 1; |
225 | data, | 223 | data = tmp; |
226 | size); | 224 | } |
227 | must_free = 1; | ||
228 | data = tmp; | ||
229 | } | ||
230 | retVal->crc = NULL; | 225 | retVal->crc = NULL; |
231 | retVal->crfc = must_free ? &free : NULL; | 226 | retVal->crfc = must_free ? &free : NULL; |
232 | retVal->crc_cls = must_free ? data : NULL; | 227 | retVal->crc_cls = must_free ? data : NULL; |
@@ -244,38 +239,42 @@ MHD_create_response_from_data(size_t size, | |||
244 | * necessarily be freed immediatley. | 239 | * necessarily be freed immediatley. |
245 | */ | 240 | */ |
246 | void | 241 | void |
247 | MHD_destroy_response(struct MHD_Response * response) { | 242 | MHD_destroy_response (struct MHD_Response *response) |
248 | struct MHD_HTTP_Header * pos; | 243 | { |
244 | struct MHD_HTTP_Header *pos; | ||
249 | 245 | ||
250 | if (response == NULL) | 246 | if (response == NULL) |
251 | return; | ||
252 | pthread_mutex_lock(&response->mutex); | ||
253 | if (0 != --response->reference_count) { | ||
254 | pthread_mutex_unlock(&response->mutex); | ||
255 | return; | 247 | return; |
256 | } | 248 | pthread_mutex_lock (&response->mutex); |
257 | pthread_mutex_unlock(&response->mutex); | 249 | if (0 != --response->reference_count) |
258 | pthread_mutex_destroy(&response->mutex); | 250 | { |
251 | pthread_mutex_unlock (&response->mutex); | ||
252 | return; | ||
253 | } | ||
254 | pthread_mutex_unlock (&response->mutex); | ||
255 | pthread_mutex_destroy (&response->mutex); | ||
259 | if (response->crfc != NULL) | 256 | if (response->crfc != NULL) |
260 | response->crfc(response->crc_cls); | 257 | response->crfc (response->crc_cls); |
261 | while (response->first_header != NULL) { | 258 | while (response->first_header != NULL) |
262 | pos = response->first_header; | 259 | { |
263 | response->first_header = pos->next; | 260 | pos = response->first_header; |
264 | free(pos->header); | 261 | response->first_header = pos->next; |
265 | free(pos->value); | 262 | free (pos->header); |
266 | free(pos); | 263 | free (pos->value); |
267 | } | 264 | free (pos); |
268 | if (response->crc != NULL) | 265 | } |
269 | free(response->data); | 266 | if (response->crc != NULL) |
270 | free(response); | 267 | free (response->data); |
268 | free (response); | ||
271 | } | 269 | } |
272 | 270 | ||
273 | 271 | ||
274 | void | 272 | void |
275 | MHD_increment_response_rc(struct MHD_Response * response) { | 273 | MHD_increment_response_rc (struct MHD_Response *response) |
276 | pthread_mutex_lock(&response->mutex); | 274 | { |
275 | pthread_mutex_lock (&response->mutex); | ||
277 | response->reference_count++; | 276 | response->reference_count++; |
278 | pthread_mutex_unlock(&response->mutex); | 277 | pthread_mutex_unlock (&response->mutex); |
279 | } | 278 | } |
280 | 279 | ||
281 | 280 | ||