refactor: network - harden TCP/IP and unify HTTP clients

This commit is contained in:
2026-07-29 15:35:17 +01:00
parent 75ae7ede56
commit a288dee7df
28 changed files with 1852 additions and 1361 deletions
+19 -43
View File
@@ -6,19 +6,14 @@
#include "wikipedia.h"
int wiki_fetch(const char* path, char* respBuf, int respMax) {
static char request[2560];
int reqLen = snprintf(request, sizeof(request),
"GET %s HTTP/1.0\r\n"
"Host: %s\r\n"
http::Response wiki_fetch(const char* path, char* respBuf, int respMax) {
http::RequestOptions options;
options.resolved_ip = g_server_ip;
options.extra_headers =
"User-Agent: MontaukOS/1.0 wikipedia\r\n"
"Accept: application/json\r\n"
"Connection: close\r\n"
"\r\n",
path, WIKI_HOST);
return tls::https_fetch(WIKI_HOST, g_server_ip, 443,
request, reqLen, g_tas,
respBuf, respMax);
"Accept: application/json\r\n";
return http::request_into("GET", WIKI_HOST, path, nullptr, nullptr, 0,
&g_tas, respBuf, respMax, options);
}
bool ensure_wiki_tls_ready(char* err, int err_len) {
@@ -74,27 +69,16 @@ void do_welcome_fetch() {
return;
}
int respLen = wiki_fetch(path, g_resp_buf, RESP_MAX);
if (respLen <= 0) {
http::Response response = wiki_fetch(path, g_resp_buf, RESP_MAX);
if (response.error != http::Error::NONE) {
snprintf(g_welcome_status, sizeof(g_welcome_status),
"Daily featured article unavailable; search is ready.");
build_welcome_lines(true);
return;
}
g_resp_buf[respLen] = '\0';
int headerEnd = find_header_end(g_resp_buf, respLen);
if (headerEnd < 0) {
snprintf(g_welcome_status, sizeof(g_welcome_status),
"Daily featured article unavailable; search is ready.");
build_welcome_lines(true);
return;
}
int status = parse_status_code(g_resp_buf, headerEnd);
const char* body = g_resp_buf + headerEnd;
int bodyLen = respLen - headerEnd;
if (status < 200 || status >= 300) {
const char* body = response.body;
int bodyLen = response.body_len;
if (response.status < 200 || response.status >= 300) {
snprintf(g_welcome_status, sizeof(g_welcome_status),
"Daily featured article unavailable; search is ready.");
build_welcome_lines(true);
@@ -148,24 +132,16 @@ void do_search(const char* query) {
"/w/api.php?action=query&format=json&formatversion=2"
"&prop=extracts&explaintext=1&titles=%s", encoded);
int respLen = wiki_fetch(path, g_resp_buf, RESP_MAX);
if (respLen <= 0) {
snprintf(g_status, sizeof(g_status), "Error: no response from Wikipedia");
http::Response response = wiki_fetch(path, g_resp_buf, RESP_MAX);
if (response.error != http::Error::NONE) {
snprintf(g_status, sizeof(g_status), "Error: %s",
http::error_string(response.error));
g_phase = AppPhase::ERR; return;
}
g_resp_buf[respLen] = '\0';
const char* body = response.body;
int bodyLen = response.body_len;
int headerEnd = find_header_end(g_resp_buf, respLen);
if (headerEnd < 0) {
snprintf(g_status, sizeof(g_status), "Error: malformed HTTP response");
g_phase = AppPhase::ERR; return;
}
int status = parse_status_code(g_resp_buf, headerEnd);
const char* body = g_resp_buf + headerEnd;
int bodyLen = respLen - headerEnd;
if (status == 404) {
if (response.status == 404) {
snprintf(g_status, sizeof(g_status), "Article not found: %s", query);
g_phase = AppPhase::ERR; return;
}
-20
View File
@@ -140,26 +140,6 @@ unsigned decode_utf8_codepoint(const char* buf, int len, int* consumed) {
return '?';
}
// ============================================================================
// HTTP parsing
// ============================================================================
int find_header_end(const char* buf, int len) {
for (int i = 0; i + 3 < len; i++)
if (buf[i]=='\r' && buf[i+1]=='\n' && buf[i+2]=='\r' && buf[i+3]=='\n')
return i + 4;
return -1;
}
int parse_status_code(const char* buf, int len) {
int i = 0;
while (i < len && buf[i] != ' ') i++;
if (i >= len || i + 3 >= len) return -1;
i++;
if (buf[i] < '0' || buf[i] > '9') return -1;
return (buf[i]-'0')*100 + (buf[i+1]-'0')*10 + (buf[i+2]-'0');
}
int find_substr(const char* buf, int len, const char* needle) {
int nlen = (int)strlen(needle);
if (!buf || !needle || nlen <= 0 || nlen > len) return -1;
+2 -4
View File
@@ -14,7 +14,7 @@
#include <gui/standalone.hpp>
#include <gui/svg.hpp>
#include <gui/truetype.hpp>
#include <tls/tls.hpp>
#include <http/http.hpp>
extern "C" {
#include <string.h>
@@ -190,8 +190,6 @@ void underscores_to_spaces(char* text);
bool is_main_page_query(const char* query);
unsigned decode_utf8_codepoint(const char* buf, int len, int* consumed);
int find_header_end(const char* buf, int len);
int parse_status_code(const char* buf, int len);
int find_substr(const char* buf, int len, const char* needle);
int url_encode_title(const char* in, char* out, int maxLen);
@@ -219,7 +217,7 @@ bool handle_reader_option_click(int mx, int my);
// network.cpp -- TLS fetch, welcome fetch, search
// ============================================================================
int wiki_fetch(const char* path, char* respBuf, int respMax);
http::Response wiki_fetch(const char* path, char* respBuf, int respMax);
bool ensure_wiki_tls_ready(char* err, int err_len);
bool welcome_feed_path(char* path, int path_len);
void do_welcome_fetch();