This commit is contained in:
alessandro bason 2025-06-24 17:47:08 +02:00
parent 95d74c2ef4
commit a66e58193f
25 changed files with 2600 additions and 93 deletions

29
net.c
View file

@ -1,6 +1,8 @@
#include "net.h"
#include "arena.h"
#include <stdio.h> // sscanf
#if COLLA_WIN
#include "win/net_win32.c"
#else
@ -38,7 +40,7 @@ const char *http_get_status_string(int status) {
case 404: return "NOT FOUND";
case 407: return "RANGE NOT SATISFIABLE";
case 500: return "INTERNAL SERVER_ERROR";
case 500: return "INTERNAL SERVER ERROR";
case 501: return "NOT IMPLEMENTED";
case 502: return "BAD GATEWAY";
case 503: return "SERVICE NOT AVAILABLE";
@ -54,6 +56,11 @@ http_header_t *http__parse_headers_instream(arena_t *arena, instream_t *in) {
while (!istr_is_finished(in)) {
strview_t line = istr_get_line(in);
// end of headers
if (strv_is_empty(line)) {
break;
}
usize pos = strv_find(line, ':', 0);
if (pos != STR_NONE) {
@ -112,7 +119,7 @@ http_res_t http_parse_res(arena_t *arena, strview_t response) {
http_res_t res = {0};
instream_t in = istr_init(response);
strview_t http = istr_get_view_len(&in, 5);
strview_t http = istr_get_view_len(&in, 4);
if (!strv_equals(http, strv("HTTP"))) {
err("response doesn't start with 'HTTP', instead with %v", http);
return (http_res_t){0};
@ -258,7 +265,7 @@ str_t http_decode_url_safe(arena_t *arena, strview_t string) {
}
}
assert(final_len <= string.len);
colla_assert(final_len <= string.len);
str_t out = {
.buf = alloc(arena, char, final_len + 1),
@ -531,7 +538,7 @@ sha1_result_t sha1(sha1_t *ctx, const void *buf, usize len) {
return result;
}
str_t sha1Str(arena_t *arena, sha1_t *ctx, const void *buf, usize len) {
str_t sha1_str(arena_t *arena, sha1_t *ctx, const void *buf, usize len) {
sha1_result_t result = sha1(ctx, buf, len);
return str_fmt(arena, "%08x%08x%08x%08x%08x", result.digest[0], result.digest[1], result.digest[2], result.digest[3], result.digest[4]);
}
@ -623,10 +630,20 @@ buffer_t base64_decode(arena_t *arena, buffer_t buffer) {
bytes[2] = (triple >> 0) & 0xFF;
}
usize spaces_count = 0;
for (isize i = buffer.len - 1; i >= 0; --i) {
if (buffer.data[i] == '=') {
spaces_count++;
}
else {
break;
}
}
usize outlen = arena_tell(arena) - start;
return (buffer_t){
.data = out,
.len = outlen,
.len = outlen - spaces_count,
};
}
}