source: mainline/uspace/lib/http/headers.c@ c17469e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c17469e was 0005b63, checked in by Martin Sucha <sucha14@…>, 12 years ago

libhttp: make header api a little bit more flexible

  • Property mode set to 100644
File size: 3.1 KB
RevLine 
[f9a2831]1/*
2 * Copyright (c) 2013 Martin Sucha
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup http
30 * @{
31 */
32/**
33 * @file
34 */
35
36#include <stdio.h>
37#include <stdlib.h>
38#include <str.h>
39#include <macros.h>
40
41#include "http.h"
42
43#define HTTP_HEADER_LINE "%s: %s\r\n"
44
45static char *cut_str(const char *start, const char *end)
46{
47 size_t size = end - start;
48 return str_ndup(start, size);
49}
50
[0005b63]51void http_header_init(http_header_t *header)
[f9a2831]52{
[0005b63]53 link_initialize(&header->link);
54 header->name = NULL;
55 header->value = NULL;
[f9a2831]56}
57
[0005b63]58http_header_t *http_header_create(const char *name, const char *value)
[f9a2831]59{
60 http_header_t *header = malloc(sizeof(http_header_t));
61 if (header == NULL)
62 return NULL;
[0005b63]63 http_header_init(header);
64
65 header->name = str_dup(name);
66 if (header->name == NULL) {
67 free(header);
68 return NULL;
69 }
[f9a2831]70
[0005b63]71 header->value = str_dup(value);
72 if (header->value == NULL) {
73 free(header->name);
74 free(header);
75 return NULL;
76 }
77
[f9a2831]78 return header;
79}
80
81void http_header_destroy(http_header_t *header)
82{
83 free(header->name);
84 free(header->value);
85 free(header);
86}
87
[0005b63]88ssize_t http_header_encode(http_header_t *header, char *buf, size_t buf_size)
[f9a2831]89{
90 /* TODO properly split long header values */
91 if (buf == NULL) {
92 return printf_size(HTTP_HEADER_LINE, header->name, header->value);
93 }
94 else {
95 return snprintf(buf, buf_size,
96 HTTP_HEADER_LINE, header->name, header->value);
97 }
98}
99
[0005b63]100int http_header_parse(const char *line, http_header_t *header)
[f9a2831]101{
102 const char *pos = line;
103 while (*pos != 0 && *pos != ':') pos++;
104 if (*pos != ':')
105 return EINVAL;
106
107 char *name = cut_str(line, pos);
108 if (name == NULL)
109 return ENOMEM;
110
111 pos++;
112
113 while (*pos == ' ') pos++;
114
115 char *value = str_dup(pos);
116 if (value == NULL) {
117 free(name);
118 return ENOMEM;
119 }
120
[0005b63]121 header->name = name;
122 header->value = value;
[f9a2831]123
124 return EOK;
125}
126
127/** @}
128 */
Note: See TracBrowser for help on using the repository browser.