PIProtectedVariable - user now can`t mistake
PIHTTPServer improvements
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
#include "microhttpd_server_p.h"
|
||||
|
||||
#include "microhttpd_server.h"
|
||||
#include "piliterals_string.h"
|
||||
#include "piliterals_time.h"
|
||||
|
||||
@@ -126,7 +125,10 @@ void request_completed(void * cls, MHD_Connection * connection, void ** con_cls,
|
||||
MicrohttpdServerConnection *& conn((MicrohttpdServerConnection *&)(*con_cls));
|
||||
// piCout << "request_completed" << conn << conn->headers << conn->post << '"' << conn->body << '"';
|
||||
if (!conn) return;
|
||||
if (conn->method == MicrohttpdServer::Method::Post && conn->postprocessor) MHD_destroy_post_processor(conn->postprocessor);
|
||||
if (conn->postprocessor) {
|
||||
MHD_destroy_post_processor(conn->postprocessor);
|
||||
conn->postprocessor = nullptr;
|
||||
}
|
||||
conn->ready();
|
||||
piDeleteSafety(conn);
|
||||
}
|
||||
@@ -186,7 +188,7 @@ int answer_to_connection(void * cls,
|
||||
return MHD_NO;
|
||||
}
|
||||
|
||||
// piCout << "answer" << url << method << server;
|
||||
piCout << "answer" << url << method << (int)m << server;
|
||||
MicrohttpdServerConnection *& conn((MicrohttpdServerConnection *&)(*con_cls));
|
||||
if (!conn) {
|
||||
conn = new MicrohttpdServerConnection();
|
||||
@@ -196,35 +198,25 @@ int answer_to_connection(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);
|
||||
return MHD_YES;
|
||||
}
|
||||
|
||||
if (m == MicrohttpdServer::Method::Post) {
|
||||
// qDebug() << "new POST" << *upload_data_size;
|
||||
if (m == MicrohttpdServer::Method::Unknown) {
|
||||
return conn->send_error();
|
||||
}
|
||||
|
||||
if (*upload_data_size) {
|
||||
if (!conn->postprocessor) {
|
||||
conn->postprocessor = MHD_create_post_processor(connection, 65536, (MHD_PostDataIterator)iterate_post, (void *)conn);
|
||||
}
|
||||
return MHD_YES;
|
||||
}
|
||||
|
||||
switch (m) {
|
||||
case MicrohttpdServer::Method::Get:
|
||||
conn->body.append(upload_data, *upload_data_size);
|
||||
MHD_post_process(conn->postprocessor, upload_data, *upload_data_size);
|
||||
*upload_data_size = 0;
|
||||
} else {
|
||||
// qDebug() << "answer ok";
|
||||
if (!conn->ready()) return conn->send_error();
|
||||
return MHD_YES;
|
||||
case MicrohttpdServer::Method::Post:
|
||||
// qDebug() << "add POST" << *upload_data_size << PIString::fromUtf8(upload_data, *upload_data_size);
|
||||
if (*upload_data_size) {
|
||||
conn->body.append(upload_data, *upload_data_size);
|
||||
if (conn->postprocessor) MHD_post_process(conn->postprocessor, upload_data, *upload_data_size);
|
||||
*upload_data_size = 0;
|
||||
return MHD_YES;
|
||||
} else {
|
||||
// qDebug() << "answer ok";
|
||||
if (!conn->ready()) return conn->send_error();
|
||||
return MHD_YES;
|
||||
}
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
return conn->send_error();
|
||||
return MHD_YES;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -9,15 +9,20 @@ PIHTTPServer::PIHTTPServer() {
|
||||
rep.setCode(404);
|
||||
auto in_path = r.path.split("/");
|
||||
in_path.removeAll("");
|
||||
auto it = functions.makeReverseIterator();
|
||||
auto it = functions.makeReverseIterator();
|
||||
bool found = false;
|
||||
while (it.next()) {
|
||||
if (it.value().function) {
|
||||
if (it.value().match(in_path)) {
|
||||
rep = it.value().function(r);
|
||||
break;
|
||||
if (it.value().method == r.method) {
|
||||
if (it.value().match(in_path)) {
|
||||
rep = it.value().function(r);
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!found && unhandled) rep = unhandled(r);
|
||||
auto hit = reply_headers.makeIterator();
|
||||
while (hit.next())
|
||||
rep.addHeader(hit.key(), hit.value());
|
||||
@@ -31,14 +36,50 @@ PIHTTPServer::~PIHTTPServer() {
|
||||
}
|
||||
|
||||
|
||||
void PIHTTPServer::registerPath(const PIString & path, RequestFunction functor) {
|
||||
auto & ep(functions[path]);
|
||||
void PIHTTPServer::registerPath(const PIString & path, Method method, RequestFunction functor) {
|
||||
auto & ep(functions[path + PIString::fromNumber((int)method)]);
|
||||
ep.path = path.split("/");
|
||||
ep.method = method;
|
||||
ep.function = functor;
|
||||
ep.path.removeAll("");
|
||||
}
|
||||
|
||||
|
||||
void PIHTTPServer::registerUnhandled(RequestFunction functor) {
|
||||
unhandled = functor;
|
||||
}
|
||||
|
||||
|
||||
void PIHTTPServer::unregisterPath(const PIString & path, Method method) {
|
||||
auto pl = path.split("/");
|
||||
pl.removeAll("");
|
||||
auto it = functions.makeIterator();
|
||||
while (it.next()) {
|
||||
if (it.value().method == method) {
|
||||
if (it.value().path == pl) {
|
||||
functions.remove(it.key());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void PIHTTPServer::unregisterPath(const PIString & path) {
|
||||
auto pl = path.split("/");
|
||||
pl.removeAll("");
|
||||
auto it = functions.makeIterator();
|
||||
PIStringList keys;
|
||||
while (it.next()) {
|
||||
if (it.value().path == pl) {
|
||||
keys << it.key();
|
||||
}
|
||||
}
|
||||
for (const auto & k: keys)
|
||||
functions.remove(k);
|
||||
}
|
||||
|
||||
|
||||
bool PIHTTPServer::Endpoint::match(const PIStringList & in_path) const {
|
||||
if (in_path.size() != path.size()) return false;
|
||||
for (int i = 0; i < path.size_s(); ++i) {
|
||||
|
||||
Reference in New Issue
Block a user