PIHTTPServer basic auth works

This commit is contained in:
2024-11-24 23:23:08 +03:00
parent c2c9822169
commit 1acaf24df9
8 changed files with 184 additions and 61 deletions

View File

@@ -3,7 +3,7 @@
using namespace PICoutManipulators;
// PIKbdListener kbd;
PIKbdListener kbd;
const char * pageTitle = "<!DOCTYPE html>"
"<html>"
"<body>"
@@ -11,14 +11,27 @@ const char * pageTitle = "<!DOCTYPE html>"
"</body>"
"</html>";
template<typename T>
PIString stringify(const T & v) {
PIString ret;
PICout::withExternalBuffer(&ret, 0, PICoutManipulators::AddSpaces) << v;
return ret;
}
int main(int argc, char * argv[]) {
PIHTTPServer server;
server.listen({"127.0.0.1:7777"});
server.setBasicAuthRealm("pip");
server.setBasicAuthEnabled(true);
server.setBasicAuthCallback([](const PIString & u, const PIString & p) -> bool {
piCout << "basic auth" << u << p;
return (u == "u" && p == "p");
});
server.registerUnhandled([](const PIHTTP::MessageConst & msg) -> PIHTTP::MessageMutable {
PIHTTP::MessageMutable ret;
piCout << "server rec:\n\tpath: %1\n\tmethod: %2\n\tbody: %3"_a.arg(msg.path())
piCout << "server rec:\n\tpath: %1\n\tmethod: %2\n\targs: %3\n\tbody: %4\n"_a.arg(msg.path())
.arg((int)msg.method())
.arg(stringify(msg.arguments()))
.arg(PIString::fromUTF8(msg.body()));
ret.setCode(PIHTTP::Code::BadRequest);
ret.setBody(PIByteArray::fromAscii("hello client! 0123456789"));
@@ -27,11 +40,13 @@ int main(int argc, char * argv[]) {
PIHTTP::MessageMutable req;
req.setBody(PIByteArray::fromAscii("hello server!"));
auto * c = PIHTTPClient::create("http://127.0.0.1:7777/api", PIHTTP::Method::Get, req);
c->onFinish([](PIHTTP::MessageConst r) {
piCout << "finish" << (int)r.code();
piCout << "headers" << r.headers();
piCout << "body" << PIString::fromUTF8(r.body());
auto * c = PIHTTPClient::create("http://u:p2@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())
.arg(stringify(msg.arguments()))
.arg(PIString::fromUTF8(msg.body()))
.arg((int)msg.code());
})
->onError([c](PIHTTP::MessageConst r) {
piCout << "error" << (int)r.code();
@@ -42,11 +57,16 @@ int main(int argc, char * argv[]) {
piCout << "msg" << c->lastError();
})
->start();
piMSleep(500);
// piMSleep(500);
kbd.enableExitCapture();
WAIT_FOR_EXIT
server.stop();
// c->abort();
// piMSleep(500);
return 0;
// piCout << PIString::readableSize(PISystemMonitor::usedRAM());
/*PIVector<int> vi;