From 58b3fa64bc69dee10f1ccc53369c5eea5aeed39f Mon Sep 17 00:00:00 2001 From: peri4 Date: Mon, 25 Nov 2024 15:00:42 +0300 Subject: [PATCH] http server fix --- libs/http_server/microhttpd_server_p.cpp | 9 +++++---- main.cpp | 5 +++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/libs/http_server/microhttpd_server_p.cpp b/libs/http_server/microhttpd_server_p.cpp index 29a9f19f..1e36fcde 100644 --- a/libs/http_server/microhttpd_server_p.cpp +++ b/libs/http_server/microhttpd_server_p.cpp @@ -88,7 +88,7 @@ bool MicrohttpdServerConnection::ready() { return true; } } - // piCout << "ready" << (int)method << path; + // piCout << "ready" << (int)method << path << body.size(); MessageMutable req; req.setMethod(method); req.setPath(path); @@ -242,15 +242,16 @@ int answer_callback(void * cls, conn->method = m; MHD_get_connection_values(connection, MHD_HEADER_KIND, (MHD_KeyValueIterator)header_iterate, *con_cls); MHD_get_connection_values(connection, MHD_GET_ARGUMENT_KIND, (MHD_KeyValueIterator)args_iterate, *con_cls); + if (server->isBasicAuthEnabled() && !conn->authorized) { + if (!conn->checkBasicAuth()) return MHD_YES; + } + return MHD_YES; } if (m == Method::Unknown) { return conn->sendReply(MessageMutable::fromCode(Code::MethodNotAllowed)); } - if (server->isBasicAuthEnabled() && !conn->authorized) { - if (!conn->checkBasicAuth()) return MHD_YES; - } if (*upload_data_size) { if (!conn->postprocessor) { diff --git a/main.cpp b/main.cpp index df66637b..6061032f 100644 --- a/main.cpp +++ b/main.cpp @@ -29,9 +29,10 @@ int main(int argc, char * argv[]) { }); server.registerUnhandled([](const PIHTTP::MessageConst & msg) -> PIHTTP::MessageMutable { PIHTTP::MessageMutable ret; - piCout << "server rec:\n\tpath: %1\n\tmethod: %2\n\targs: %3\n\tbody: %4\n"_a.arg(msg.path()) + piCout << "server rec:\n\tpath: %1\n\tmethod: %2\n\targs: %3\n\theaders: %4\n\tbody: %5\n"_a.arg(msg.path()) .arg((int)msg.method()) .arg(stringify(msg.arguments())) + .arg(PIStringList(msg.headers().map([](PIString k, PIString v) { return k + " = " + v; })).join("\n\t\t")) .arg(PIString::fromUTF8(msg.body())); ret.setCode(PIHTTP::Code::BadRequest); ret.setBody(PIByteArray::fromAscii("hello client! 0123456789")); @@ -40,7 +41,7 @@ int main(int argc, char * argv[]) { PIHTTP::MessageMutable req; req.setBody(PIByteArray::fromAscii("hello server!")); - auto * c = PIHTTPClient::create("http://u:p2@127.0.0.1:7777/api", PIHTTP::Method::Get, req); + auto * c = PIHTTPClient::create("http://u:p@127.0.0.1:7777/api", PIHTTP::Method::Get, req); c->onFinish([](PIHTTP::MessageConst msg) { piCout << "client rec:\n\tcode: %5\n\tpath: %1\n\tmethod: %2\n\targs: %3\n\tbody: %4\n"_a.arg(msg.path()) .arg((int)msg.method())