Commit ef9bd60a authored by Pavel Vainerman's avatar Pavel Vainerman

(UHttp): Работа на API v1.0

parent 22b49903
......@@ -388,6 +388,7 @@ AC_CONFIG_FILES([Makefile
tests/UniXmlTest/Makefile
tests/MQPerfTest/Makefile
tests/PocoTest/Makefile
tests/UHttpTest/Makefile
docs/Makefile
docs/UniSetDox.cfg
docs/UniSetDoxDevel.cfg
......
......@@ -18,18 +18,20 @@
#include <Poco/URI.h>
#include "UHttpRequestHandler.h"
// -------------------------------------------------------------------------
using namespace std;
using namespace Poco::Net;
using namespace UniSetTypes;
using namespace UHttp;
// -------------------------------------------------------------------------
UHttpRequestHandler::UHttpRequestHandler( std::shared_ptr<IHttpRequestRegistry> _supplier ):
supplier(_supplier)
UHttpRequestHandler::UHttpRequestHandler(std::shared_ptr<IHttpRequestRegistry> _registry ):
registry(_registry)
{
}
// -------------------------------------------------------------------------
void UHttpRequestHandler::handleRequest( Poco::Net::HTTPServerRequest& req, Poco::Net::HTTPServerResponse& resp )
{
if( !supplier )
if( !registry )
{
resp.setStatus(HTTPResponse::HTTP_NOT_FOUND);
resp.setContentType("text/json");
......@@ -40,28 +42,50 @@ void UHttpRequestHandler::handleRequest( Poco::Net::HTTPServerRequest& req, Poco
Poco::URI uri(req.getURI());
// в текущей версии подразумевается, что запрос идёт в формате
// http://xxx.host:port/ObjectName
// поэтому сразу передаём uri.getQuery() в качестве имени объекта
if( log->is_info() )
log->info() << req.getHost() << ": query: " << uri.getQuery() << endl;
std::vector<std::string> seg;
uri.getPathSegments(seg);
// example: http://host:port/api-version/get/ObjectName
if( seg.size() < 3
|| seg[0] != UHTTP_API_VERSION
|| seg[1] != "get"
|| seg[2].empty() )
{
resp.setStatus(HTTPResponse::HTTP_BAD_REQUEST);
resp.setContentType("text/json");
std::ostream& out = resp.send();
out.flush();
return;
}
const std::string objectName(seg[2]);
// auto qp = uri.getQueryParameters();
// cerr << "params: " << endl;
// for( const auto& p: qp )
// cerr << p.first << "=" << p.second << endl;
resp.setStatus(HTTPResponse::HTTP_OK);
resp.setContentType("text/json");
std::ostream& out = resp.send();
auto json = supplier->getData(uri.getQuery());
auto json = registry->getData(objectName);
out << json.dump();
out.flush();
}
// -------------------------------------------------------------------------
UHttpRequestHandlerFactory::UHttpRequestHandlerFactory( std::shared_ptr<IHttpRequestRegistry>& _supplier ):
supplier(_supplier)
UHttpRequestHandlerFactory::UHttpRequestHandlerFactory(std::shared_ptr<IHttpRequestRegistry>& _registry ):
registry(_registry)
{
}
// -------------------------------------------------------------------------
HTTPRequestHandler* UHttpRequestHandlerFactory::createRequestHandler( const HTTPServerRequest& req )
{
return new UHttpRequestHandler(supplier);
return new UHttpRequestHandler(registry);
}
// -------------------------------------------------------------------------
......@@ -19,9 +19,10 @@
// -------------------------------------------------------------------------
using namespace Poco::Net;
using namespace UniSetTypes;
using namespace UHttp;
// -------------------------------------------------------------------------
UHttpServer::UHttpServer(std::shared_ptr<IHttpRequestRegistry>& supplier, const std::string _host, int _port ):
UHttpServer::UHttpServer(std::shared_ptr<IHttpRequestRegistry>& supplier, const std::string& _host, int _port ):
sa(_host,_port)
{
/*! \FIXME: доделать конфигурирование параметров */
......@@ -49,3 +50,23 @@ std::shared_ptr<DebugStream> UHttpServer::log()
return mylog;
}
// -------------------------------------------------------------------------
void UHttpServer::run( bool thread )
{
if( !thread )
{
try
{
http->start();
pause();
http->stop();
}
catch( std::exception& ex )
{
mylog->crit() << "(UHttpServer): " << ex.what() << std::endl;
throw ex;
}
return;
}
}
// -------------------------------------------------------------------------
SUBDIRS=MQPerfTest PocoTest
SUBDIRS=MQPerfTest PocoTest UHttpTest
if HAVE_TESTS
############################################################################
# This file is part of the UniSet library #
......
noinst_PROGRAMS = uhttp-test
uhttp_test_LDADD = $(top_builddir)/lib/libUniSet2.la $(SIGC_LIBS) $(POCO_LIBS)
uhttp_test_CPPFLAGS = -I$(top_builddir)/include -I$(top_builddir)/extensions/include $(SIGC_CFLAGS) $(POCO_CFLAGS)
uhttp_test_SOURCES = uhttp-test.cc
#include <iostream>
#include <memory>
#include "UHttpServer.h"
// --------------------------------------------------------------------------
using namespace std;
using namespace UniSetTypes;
// --------------------------------------------------------------------------
class UTestSupplier:
public UHttp::IHttpRequest
{
public:
UTestSupplier(){}
virtual ~UTestSupplier(){}
virtual nlohmann::json getData() override
{
nlohmann::json j;
j["test"] = 23;
return j;
}
};
class UTestRequestRegistry:
public UHttp::IHttpRequestRegistry
{
public:
UTestRequestRegistry(){}
virtual ~UTestRequestRegistry(){}
virtual nlohmann::json getData( const std::string& name ) override
{
return sup.getData();
}
private:
UTestSupplier sup;
};
// --------------------------------------------------------------------------
int main(int argc, const char** argv)
{
try
{
auto reg = std::make_shared<UTestRequestRegistry>();
auto ireg = dynamic_pointer_cast<UHttp::IHttpRequestRegistry>(reg);
auto http = make_shared<UHttp::UHttpServer>(ireg,"localhost", 5555);
http->run(false);
return 0;
}
catch( const std::exception& e )
{
cerr << "(http-test): " << e.what() << endl;
}
catch(...)
{
cerr << "(http-test): catch(...)" << endl;
}
return 1;
}
......@@ -487,6 +487,7 @@ tests/umutex.cc
tests/perf_test.cc
tests/MQPerfTest/mq-test.cc
tests/PocoTest/poco-test.cc
tests/UHttpTest/uhttp-test.cc
testsuite/Makefile.am
Utilities/Admin/admin.cc
Utilities/Admin/Makefile.am
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment