Hash :
19fd1592
Author :
Thomas de Grivel
Date :
2024-08-15T00:05:55
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
/* kc3
* Copyright 2022,2023,2024 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software granted the above
* copyright notice and this permission paragraph are included in all
* copies and substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#include <string.h>
#include <libkc3/kc3.h>
#include "http_response.h"
s_http_response * http_response_buf_parse (s_http_response *response,
s_buf *buf, bool parse_body)
{
sw content_length = -1;
s_str content_length_str = {0};
s_tag *key = NULL;
s_list **l = NULL;
sw r = 0;
s_buf_save save;
s_str str;
s_tag tag_code = {0};
s_http_response tmp = {0};
s_tuple *tuple = NULL;
s_tag *value = NULL;
assert(response);
assert(buf);
buf_save_init(buf, &save);
if (! buf_read_until_1_into_str(buf, " ", &tmp.protocol))
goto clean;
if (! buf_parse_tag_u16(buf, &tag_code))
goto restore;
if (tag_code.data.u16 < 100 || tag_code.data.u16 > 999) {
err_puts("http_response_buf_parse: invalid response code");
goto restore;
}
if ((r = buf_read_1(buf, " ")) <= 0)
goto restore;
if (! buf_read_until_1_into_str(buf, "\r\n", &tmp.message))
goto restore;
str_init_1(&content_length_str, NULL, "content-length");
l = &tmp.headers;
while (1) {
if ((r = buf_read_1(buf, "\r\n")) < 0)
goto restore;
if (r > 0)
break;
*l = list_new_tuple(2, NULL);
if (! *l)
goto restore;
tuple = &(*l)->tag.data.tuple;
key = tuple->tag;
value = tuple->tag + 1;
key->type = TAG_STR;
if (! buf_read_until_1_into_str(buf, ":", &key->data.str))
goto restore;
if ((r = buf_ignore_spaces(buf)) < 0)
goto restore;
value->type = TAG_STR;
if (! buf_read_until_1_into_str(buf, "\r\n", &value->data.str))
goto restore;
if (! str_init_to_lower(&str, &key->data.str))
goto restore;
if (! compare_str(&content_length_str, &str))
sw_init_str(&content_length, &value->data.str);
str_clean(&str);
l = &(*l)->next.data.list;
}
if (! parse_body)
goto ok;
if (content_length < 0)
goto restore;
if (! buf_read(buf, content_length, &response->body))
goto restore;
ok:
buf_save_clean(buf, &save);
*response = tmp;
return response;
restore:
buf_save_restore_rpos(buf, &save);
clean:
buf_save_clean(buf, &save);
return NULL;
}
sw http_response_buf_write (const s_http_response *response,
s_buf *buf, bool send_body)
{
sw content_length = -1;
s_str content_length_str = {0};
s_tag default_messages = {0};
s_ident ident = {0};
s_tag *key = NULL;
const s_list *l = NULL;
s_str protocol = {0};
sw r = 0;
sw result = 0;
s_tag tag_code = {0};
s_tag tag_message = {0};
s_tag *value = NULL;
assert(response);
assert(buf);
if (! response->protocol.size)
str_init_1(&protocol, NULL, "HTTP/1.1");
else
protocol = response->protocol;
if ((r = buf_write_str(buf, &protocol)) < 0)
return r;
result += r;
if ((r = buf_write_1(buf, " ")) < 0)
return r;
result += r;
tag_code.type = TAG_U16;
tag_code.data.u16 = response->code;
if (! tag_code.data.u16)
tag_code.data.u16 = 200;
if (tag_code.data.u16 < 100 || tag_code.data.u16 > 999) {
err_puts("http_response_buf_write: invalid response code");
return -1;
}
if ((r = buf_inspect_u16_decimal(buf, &tag_code.data.u16)) < 0)
return r;
result += r;
if ((r = buf_write_1(buf, " ")) < 0)
return r;
result += r;
if (! response->message.size) {
ident_init(&ident, sym_1("HTTP.Response"), sym_1("default_messages"));
ident_get(&ident, &default_messages);
if (! tag_is_alist(&default_messages)) {
err_puts("http_response_buf_write: invalid default_messages:"
" not an AList");
tag_clean(&default_messages);
return -1;
}
if (alist_get((const s_list * const *) &default_messages.data.list,
&tag_code, &tag_message)) {
if (tag_message.type != TAG_STR) {
err_puts("http_response_buf_write: invalid default message:"
" not a Str");
tag_clean(&tag_message);
tag_clean(&default_messages);
return -1;
}
}
}
else {
tag_message.type = TAG_STR;
tag_message.data.str = response->message;
tag_message.data.str.free.p = NULL;
}
if ((r = buf_write_str(buf, &tag_message.data.str)) < 0) {
tag_clean(&tag_message);
tag_clean(&default_messages);
return r;
}
result += r;
tag_clean(&tag_message);
tag_clean(&default_messages);
if ((r = buf_write_1(buf, "\r\n")) < 0)
return r;
result += r;
str_init_1(&content_length_str, NULL, "Content-Length");
l = response->headers;
while (l) {
if (l->tag.type != TAG_TUPLE ||
l->tag.data.tuple.count != 2) {
err_puts("http_response_buf_write: invalid header: not a Tuple");
return -1;
}
key = l->tag.data.tuple.tag;
value = key + 1;
if (key->type != TAG_STR || value->type != TAG_STR) {
err_puts("http_response_buf_write: invalid header: not a Str");
return -1;
}
if (! compare_str(&content_length_str, &key->data.str))
sw_init_str(&content_length, &value->data.str);
if ((r = buf_write_str(buf, &key->data.str)) < 0)
return r;
result += r;
if ((r = buf_write_1(buf, ": ")) < 0)
return r;
result += r;
if ((r = buf_write_str(buf, &value->data.str)) < 0)
return r;
result += r;
if ((r = buf_write_1(buf, "\r\n")) < 0)
return r;
result += r;
l = list_next(l);
}
if (content_length < 0) {
if ((r = buf_write_str(buf, &content_length_str)) < 0)
return r;
result += r;
if ((r = buf_write_1(buf, ": ")) < 0)
return r;
result += r;
if ((r = buf_inspect_uw_decimal(buf, &response->body.size)) < 0)
return r;
result += r;
if ((r = buf_write_1(buf, "\r\n")) < 0)
return r;
result += r;
}
if ((r = buf_write_1(buf, "\r\n")) < 0)
return r;
result += r;
if (send_body) {
if ((r = buf_write_str(buf, &response->body)) < 0)
return r;
result += r;
}
return result;
}