Changeset 4d4f656 in mainline for uspace/lib/http/src
- Timestamp:
- 2013-09-26T20:50:52Z (12 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 408424e
- Parents:
- b623b68
- Location:
- uspace/lib/http/src
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/http/src/headers.c
rb623b68 r4d4f656 41 41 #include <http/http.h> 42 42 #include <http/ctype.h> 43 #include <http/errno.h> 43 44 44 45 #define HTTP_HEADER_LINE "%s: %s\r\n" … … 204 205 int rc = http_header_receive_name(rb, &name); 205 206 if (rc != EOK) { 206 printf("Failed receiving header name\n");207 207 return rc; 208 208 } … … 247 247 } 248 248 249 /** Test if two header names are equivalent 250 * 251 */ 252 bool http_header_name_match(const char *name_a, const char *name_b) 253 { 254 return stricmp(name_a, name_b) == 0; 255 } 256 257 void http_headers_init(http_headers_t *headers) { 258 list_initialize(&headers->list); 259 } 260 261 int http_headers_find_single(http_headers_t *headers, const char *name, 262 http_header_t **out_header) 263 { 264 http_header_t *found = NULL; 265 http_headers_foreach(*headers, header) { 266 if (!http_header_name_match(header->name, name)) 267 continue; 268 269 if (found == NULL) { 270 found = header; 271 } 272 else { 273 return HTTP_EMULTIPLE_HEADERS; 274 } 275 } 276 277 if (found == NULL) 278 return HTTP_EMISSING_HEADER; 279 280 *out_header = found; 281 return EOK; 282 } 283 284 int http_headers_append(http_headers_t *headers, const char *name, 285 const char *value) 286 { 287 http_header_t *header = http_header_create(name, value); 288 if (header == NULL) 289 return ENOMEM; 290 291 http_headers_append_header(headers, header); 292 return EOK; 293 } 294 295 int http_headers_set(http_headers_t *headers, const char *name, 296 const char *value) 297 { 298 http_header_t *header = NULL; 299 int rc = http_headers_find_single(headers, name, &header); 300 if (rc != EOK && rc != HTTP_EMISSING_HEADER) 301 return rc; 302 303 if (rc == HTTP_EMISSING_HEADER) 304 return http_headers_append(headers, name, value); 305 306 char *new_value = str_dup(value); 307 if (new_value == NULL) 308 return ENOMEM; 309 310 free(header->value); 311 header->value = new_value; 312 return EOK; 313 } 314 315 int http_headers_get(http_headers_t *headers, const char *name, char **value) 316 { 317 http_header_t *header = NULL; 318 int rc = http_headers_find_single(headers, name, &header); 319 if (rc != EOK) 320 return rc; 321 322 *value = header->value; 323 return EOK; 324 } 325 326 int http_headers_receive(receive_buffer_t *rb, http_headers_t *headers) 327 { 328 int rc = EOK; 329 unsigned added = 0; 330 331 while (true) { 332 char c = 0; 333 rc = recv_char(rb, &c, false); 334 if (rc != EOK) 335 goto error; 336 337 if (c == '\n' || c == '\r') 338 break; 339 340 http_header_t *header = malloc(sizeof(http_header_t)); 341 if (header == NULL) { 342 rc = ENOMEM; 343 goto error; 344 } 345 http_header_init(header); 346 347 rc = http_header_receive(rb, header); 348 if (rc != EOK) { 349 free(header); 350 goto error; 351 } 352 353 http_headers_append_header(headers, header); 354 added++; 355 } 356 357 return EOK; 358 error: 359 while (added-- > 0) { 360 link_t *link = list_last(&headers->list); 361 http_header_t *header = list_get_instance(link, http_header_t, link); 362 http_headers_remove(headers, header); 363 http_header_destroy(header); 364 } 365 return rc; 366 } 367 368 void http_headers_clear(http_headers_t *headers) 369 { 370 link_t *link = list_first(&headers->list); 371 while (link != NULL) { 372 link_t *next = list_next(link, &headers->list); 373 http_header_t *header = list_get_instance(link, http_header_t, link); 374 list_remove(link); 375 http_header_destroy(header); 376 link = next; 377 } 378 } 379 249 380 /** @} 250 381 */ -
uspace/lib/http/src/request.c
rb623b68 r4d4f656 65 65 } 66 66 67 list_initialize(&req->headers);67 http_headers_init(&req->headers); 68 68 69 69 return req; … … 74 74 free(req->method); 75 75 free(req->path); 76 link_t *link = req->headers.head.next; 77 while (link != &req->headers.head) { 78 link_t *next = link->next; 79 http_header_t *header = list_get_instance(link, http_header_t, link); 80 http_header_destroy(header); 81 link = next; 82 } 76 http_headers_clear(&req->headers); 83 77 free(req); 84 78 } … … 104 98 size_t size = meth_size; 105 99 106 list_foreach(req->headers, link, http_header_t, header) {100 http_headers_foreach(req->headers, header) { 107 101 ssize_t header_size = http_header_encode(header, NULL, 0); 108 102 if (header_size < 0) … … 126 120 pos_size -= written; 127 121 128 list_foreach(req->headers, link, http_header_t, header) {122 http_headers_foreach(req->headers, header) { 129 123 written = http_header_encode(header, pos, pos_size); 130 124 if (written < 0) { -
uspace/lib/http/src/response.c
rb623b68 r4d4f656 40 40 41 41 #include <http/http.h> 42 #include <http/errno.h> 42 43 43 44 int http_parse_status(const char *line, http_version_t *out_version, … … 98 99 return ENOMEM; 99 100 memset(resp, 0, sizeof(http_response_t)); 100 list_initialize(&resp->headers);101 http_headers_init(&resp->headers); 101 102 102 103 char *line = malloc(http->buffer_size); … … 115 116 goto error; 116 117 117 while (true) { 118 rc = recv_eol(&http->recv_buffer); 119 if (rc < 0) 120 goto error; 121 122 /* Empty line ends header part */ 123 if (rc > 0) 124 break; 125 126 http_header_t *header = malloc(sizeof(http_header_t)); 127 if (header == NULL) { 128 rc = ENOMEM; 129 goto error; 130 } 131 http_header_init(header); 132 133 rc = http_header_receive(&http->recv_buffer, header); 134 if (rc != EOK) { 135 free(header); 136 goto error; 137 } 138 139 list_append(&header->link, &resp->headers); 140 } 118 rc = http_headers_receive(&http->recv_buffer, &resp->headers); 119 if (rc != EOK) 120 goto error; 121 122 rc = recv_eol(&http->recv_buffer); 123 if (rc == 0) 124 rc = HTTP_EPARSE; 125 if (rc < 0) 126 goto error; 141 127 142 128 *out_response = resp; … … 157 143 { 158 144 free(resp->message); 159 link_t *link = resp->headers.head.next; 160 while (link != &resp->headers.head) { 161 link_t *next = link->next; 162 http_header_t *header = list_get_instance(link, http_header_t, link); 163 http_header_destroy(header); 164 link = next; 165 } 145 http_headers_clear(&resp->headers); 166 146 free(resp); 167 147 }
Note:
See TracChangeset
for help on using the changeset viewer.