Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
U
uniset2
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
1
Issues
1
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
UniSet project repositories
uniset2
Commits
ef9bd60a
Commit
ef9bd60a
authored
Oct 30, 2016
by
Pavel Vainerman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(UHttp): Работа на API v1.0
parent
22b49903
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
126 additions
and
12 deletions
+126
-12
configure.ac
configure.ac
+1
-0
UHttpRequestHandler.cc
src/Communications/Http/UHttpRequestHandler.cc
+34
-10
UHttpServer.cc
src/Communications/Http/UHttpServer.cc
+22
-1
Makefile.am
tests/Makefile.am
+1
-1
Makefile.am
tests/UHttpTest/Makefile.am
+4
-0
uhttp-test.cc
tests/UHttpTest/uhttp-test.cc
+63
-0
uniset2.files
uniset2.files
+1
-0
No files found.
configure.ac
View file @
ef9bd60a
...
...
@@ -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
...
...
src/Communications/Http/UHttpRequestHandler.cc
View file @
ef9bd60a
...
...
@@ -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
);
}
// -------------------------------------------------------------------------
src/Communications/Http/UHttpServer.cc
View file @
ef9bd60a
...
...
@@ -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
;
}
}
// -------------------------------------------------------------------------
tests/Makefile.am
View file @
ef9bd60a
SUBDIRS
=
MQPerfTest PocoTest
SUBDIRS
=
MQPerfTest PocoTest
UHttpTest
if
HAVE_TESTS
############################################################################
# This file is part of the UniSet library #
...
...
tests/UHttpTest/Makefile.am
0 → 100644
View file @
ef9bd60a
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
tests/UHttpTest/uhttp-test.cc
0 → 100644
View file @
ef9bd60a
#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
;
}
uniset2.files
View file @
ef9bd60a
...
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment