[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 |
|
---|
| 45 | static char *cut_str(const char *start, const char *end)
|
---|
| 46 | {
|
---|
| 47 | size_t size = end - start;
|
---|
| 48 | return str_ndup(start, size);
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | http_header_t *http_header_create(const char *name, const char *value)
|
---|
| 52 | {
|
---|
| 53 | char *dname = str_dup(name);
|
---|
| 54 | if (dname == NULL)
|
---|
| 55 | return NULL;
|
---|
| 56 |
|
---|
| 57 | char *dvalue = str_dup(value);
|
---|
| 58 | if (dvalue == NULL) {
|
---|
| 59 | free(dname);
|
---|
| 60 | return NULL;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | return http_header_create_no_copy(dname, dvalue);
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | http_header_t *http_header_create_no_copy(char *name, char *value)
|
---|
| 67 | {
|
---|
| 68 | http_header_t *header = malloc(sizeof(http_header_t));
|
---|
| 69 | if (header == NULL)
|
---|
| 70 | return NULL;
|
---|
| 71 |
|
---|
| 72 | link_initialize(&header->link);
|
---|
| 73 | header->name = name;
|
---|
| 74 | header->value = value;
|
---|
| 75 |
|
---|
| 76 | return header;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | void http_header_destroy(http_header_t *header)
|
---|
| 80 | {
|
---|
| 81 | free(header->name);
|
---|
| 82 | free(header->value);
|
---|
| 83 | free(header);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | ssize_t http_encode_header(char *buf, size_t buf_size, http_header_t *header)
|
---|
| 87 | {
|
---|
| 88 | /* TODO properly split long header values */
|
---|
| 89 | if (buf == NULL) {
|
---|
| 90 | return printf_size(HTTP_HEADER_LINE, header->name, header->value);
|
---|
| 91 | }
|
---|
| 92 | else {
|
---|
| 93 | return snprintf(buf, buf_size,
|
---|
| 94 | HTTP_HEADER_LINE, header->name, header->value);
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | int http_parse_header(const char *line, char **out_name, char **out_value)
|
---|
| 99 | {
|
---|
| 100 | const char *pos = line;
|
---|
| 101 | while (*pos != 0 && *pos != ':') pos++;
|
---|
| 102 | if (*pos != ':')
|
---|
| 103 | return EINVAL;
|
---|
| 104 |
|
---|
| 105 | char *name = cut_str(line, pos);
|
---|
| 106 | if (name == NULL)
|
---|
| 107 | return ENOMEM;
|
---|
| 108 |
|
---|
| 109 | pos++;
|
---|
| 110 |
|
---|
| 111 | while (*pos == ' ') pos++;
|
---|
| 112 |
|
---|
| 113 | char *value = str_dup(pos);
|
---|
| 114 | if (value == NULL) {
|
---|
| 115 | free(name);
|
---|
| 116 | return ENOMEM;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | *out_name = name;
|
---|
| 120 | *out_value = value;
|
---|
| 121 |
|
---|
| 122 | return EOK;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | /** @}
|
---|
| 126 | */
|
---|