Changeset 4d4f656 in mainline for uspace/lib/http/src/headers.c


Ignore:
Timestamp:
2013-09-26T20:50:52Z (12 years ago)
Author:
Martin Sucha <sucha14@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
408424e
Parents:
b623b68
Message:

libhttp: Add higher-level API for working with headers

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/http/src/headers.c

    rb623b68 r4d4f656  
    4141#include <http/http.h>
    4242#include <http/ctype.h>
     43#include <http/errno.h>
    4344
    4445#define HTTP_HEADER_LINE "%s: %s\r\n"
     
    204205        int rc = http_header_receive_name(rb, &name);
    205206        if (rc != EOK) {
    206                 printf("Failed receiving header name\n");
    207207                return rc;
    208208        }
     
    247247}
    248248
     249/** Test if two header names are equivalent
     250 *
     251 */
     252bool http_header_name_match(const char *name_a, const char *name_b)
     253{
     254        return stricmp(name_a, name_b) == 0;
     255}
     256
     257void http_headers_init(http_headers_t *headers) {
     258        list_initialize(&headers->list);
     259}
     260
     261int 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
     284int 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
     295int 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
     315int 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
     326int 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;
     358error:
     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
     368void 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
    249380/** @}
    250381 */
Note: See TracChangeset for help on using the changeset viewer.