Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
9.2.2023: Due to updates GitLab will be unavailable for some minutes between 9:00 and 11:00.
Open sidebar
dcdb
dcdb
Commits
58873d63
Commit
58873d63
authored
Apr 13, 2019
by
Micha Mueller
Browse files
Minor improvements to REST API responses
parent
a35859b2
Changes
4
Hide whitespace changes
Inline
Side-by-side
analytics/AnalyticsManager.h
View file @
58873d63
...
...
@@ -183,8 +183,8 @@ public:
" Perform plugin-specific actions
\n
"
" (refer to documentation)
\n
"
"
\n
"
"All resources have to be prepended by host:port
and need at
\n
"
"
least the query ?authkey=[token
] at the end. Multiple queries
\n
"
"All resources have to be prepended by host:port
.
\n
"
"
A query can be appended as ?query=[value
] at the end. Multiple queries
\n
"
"need to be separated by semicolons(';')
\n
"
;
protected:
...
...
common/include/HttpsServer.h
View file @
58873d63
...
...
@@ -37,8 +37,7 @@ enum permission {
PUT
=
1
,
/**< Permission to make PUT requests */
POST
=
2
,
/**< Permission to make POST requests */
DELETE
=
3
,
/**< Permission to make DELETE requests */
NUM_PERMISSIONS
=
4
/**< Number of permissions there are. Keep updated in
the future! */
NUM_PERMISSIONS
=
4
/**< Number of permissions there are. */
};
using
userAttributes_t
=
std
::
pair
<
std
::
string
,
std
::
bitset
<
NUM_PERMISSIONS
>>
;
...
...
common/src/HttpsServer.cpp
View file @
58873d63
...
...
@@ -9,7 +9,6 @@
#include
<sstream>
#include
<boost/asio.hpp>
#include
<boost/asio/ssl.hpp>
#include
<boost/archive/iterators/binary_from_base64.hpp>
...
...
@@ -25,7 +24,7 @@ using namespace web::http::experimental::listener;
HttpsServer
::
HttpsServer
(
serverSettings_t
settings
)
{
http_listener_config
cfg
;
cfg
.
set_ssl_context_callback
([
&
settings
](
boost
::
asio
::
ssl
::
context
&
ctx
)
{
cfg
.
set_ssl_context_callback
([
settings
](
boost
::
asio
::
ssl
::
context
&
ctx
)
{
ctx
.
set_options
(
boost
::
asio
::
ssl
::
context
::
default_workarounds
|
boost
::
asio
::
ssl
::
context
::
no_sslv3
|
boost
::
asio
::
ssl
::
context
::
single_dh_use
);
...
...
@@ -59,35 +58,35 @@ HttpsServer::HttpsServer(serverSettings_t settings) {
void
HttpsServer
::
handle_get
(
http_request
&
msg
)
{
http_response
response
;
response
.
set_status_code
(
status_codes
::
NotImplemented
);
response
.
set_body
(
"GET Method not implemented"
);
response
.
set_body
(
"GET Method not implemented
\n
"
);
msg
.
reply
(
response
);
}
void
HttpsServer
::
handle_put
(
http_request
&
msg
)
{
http_response
response
;
response
.
set_status_code
(
status_codes
::
NotImplemented
);
response
.
set_body
(
"PUT Method not implemented"
);
response
.
set_body
(
"PUT Method not implemented
\n
"
);
msg
.
reply
(
response
);
}
void
HttpsServer
::
handle_post
(
http_request
&
msg
)
{
http_response
response
;
response
.
set_status_code
(
status_codes
::
NotImplemented
);
response
.
set_body
(
"POST Method not implemented"
);
response
.
set_body
(
"POST Method not implemented
\n
"
);
msg
.
reply
(
response
);
}
void
HttpsServer
::
handle_delete
(
http_request
&
msg
)
{
http_response
response
;
response
.
set_status_code
(
status_codes
::
NotImplemented
);
response
.
set_body
(
"DELETE Method not implemented"
);
response
.
set_body
(
"DELETE Method not implemented
\n
"
);
msg
.
reply
(
response
);
}
void
HttpsServer
::
handle_get_help
(
http_request
&
msg
)
{
http_response
response
;
response
.
set_status_code
(
status_codes
::
NotImplemented
);
response
.
set_body
(
"GET /help not implemented"
);
response
.
set_body
(
"GET /help not implemented
\n
"
);
msg
.
reply
(
response
);
}
...
...
@@ -154,7 +153,7 @@ bool HttpsServer::validateUser(const http_request& msg, permission perm) {
if
(
it
==
msg
.
headers
().
end
())
{
HLOG
(
info
)
<<
"No credentials were provided"
;
response
.
set_status_code
(
status_codes
::
Unauthorized
);
output
=
"Unauthorized access!"
;
output
=
"Unauthorized access!
\n
"
;
response
.
set_body
(
output
);
msg
.
reply
(
response
);
return
false
;
...
...
@@ -194,7 +193,7 @@ bool HttpsServer::validateUser(const http_request& msg, permission perm) {
}
catch
(
const
std
::
out_of_range
&
e
)
{
HLOG
(
warning
)
<<
"User does not exist: "
<<
usr
;
response
.
set_status_code
(
status_codes
::
Unauthorized
);
output
=
"Unauthorized access!"
;
output
=
"Unauthorized access!
\n
"
;
response
.
set_body
(
output
);
msg
.
reply
(
response
);
return
false
;
...
...
@@ -203,7 +202,7 @@ bool HttpsServer::validateUser(const http_request& msg, permission perm) {
if
(
pwd
!=
userData
.
first
)
{
HLOG
(
warning
)
<<
"Invalid password provided: "
<<
usr
<<
":"
<<
pwd
;
response
.
set_status_code
(
status_codes
::
Unauthorized
);
output
=
"Unauthorized access!"
;
output
=
"Unauthorized access!
\n
"
;
response
.
set_body
(
output
);
msg
.
reply
(
response
);
return
false
;
...
...
@@ -213,7 +212,7 @@ bool HttpsServer::validateUser(const http_request& msg, permission perm) {
if
(
!
userData
.
second
.
test
(
perm
))
{
HLOG
(
warning
)
<<
"User "
<<
usr
<<
" has insufficient permissions"
;
response
.
set_status_code
(
status_codes
::
Forbidden
);
output
=
"Insufficient permissions!"
;
output
=
"Insufficient permissions!
\n
"
;
response
.
set_body
(
output
);
msg
.
reply
(
response
);
return
false
;
...
...
@@ -221,7 +220,7 @@ bool HttpsServer::validateUser(const http_request& msg, permission perm) {
}
catch
(
const
std
::
out_of_range
&
e
)
{
HLOG
(
error
)
<<
"Internal error: permission out of range"
;
response
.
set_status_code
(
status_codes
::
InternalError
);
output
=
"Sorry, this should not have happened..."
;
output
=
"Sorry, this should not have happened...
\n
"
;
response
.
set_body
(
output
);
msg
.
reply
(
response
);
return
false
;
...
...
dcdbpusher/RestAPIServer.cpp
View file @
58873d63
...
...
@@ -26,7 +26,7 @@ void RestAPIServer::handle_get(http_request& msg) {
if
(
pathParts
.
size
()
<
1
)
{
RLOG
(
warning
)
<<
"Received malformed request: No first path part"
;
status
=
status_codes
::
BadRequest
;
output
=
"No first path part recognized"
;
output
=
"No first path part recognized
\n
"
;
goto
sendResponse
;
}
...
...
@@ -81,7 +81,7 @@ void RestAPIServer::handle_get(http_request& msg) {
}
if
(
pathParts
[
1
]
==
"sensors"
)
{
output
=
"Plugin not found!"
;
output
=
"Plugin not found!
\n
"
;
status
=
status_codes
::
NotFound
;
for
(
auto
&
p
:
_plugins
)
{
...
...
@@ -135,7 +135,7 @@ void RestAPIServer::handle_get(http_request& msg) {
//process actual request
bool
found
=
false
;
output
=
"Plugin not found!"
;
output
=
"Plugin not found!
\n
"
;
status
=
status_codes
::
NotFound
;
for
(
auto
&
p
:
_plugins
)
{
...
...
@@ -155,7 +155,7 @@ void RestAPIServer::handle_get(http_request& msg) {
break
;
}
output
=
pathParts
[
0
]
+
"::"
+
sensor
+
" Average of last "
+
std
::
to_string
(
time
)
+
" seconds is "
+
std
::
to_string
(
avg
);
std
::
to_string
(
time
)
+
" seconds is "
+
std
::
to_string
(
avg
)
+
"
\n
"
;
status
=
status_codes
::
OK
;
break
;
}
...
...
@@ -167,7 +167,7 @@ void RestAPIServer::handle_get(http_request& msg) {
if
(
!
found
)
{
for
(
auto
&
p
:
_manager
->
getPlugins
())
{
if
(
p
.
id
==
pathParts
[
0
])
{
output
=
"Sensor not found!"
;
output
=
"Sensor not found!
\n
"
;
for
(
const
auto
&
a
:
p
.
configurator
->
getAnalyzers
())
{
if
(
a
->
getStreaming
())
{
for
(
const
auto
&
u
:
a
->
getUnits
())
{
...
...
@@ -184,7 +184,7 @@ void RestAPIServer::handle_get(http_request& msg) {
break
;
}
output
=
pathParts
[
0
]
+
"::"
+
sensor
+
" Average of last "
+
std
::
to_string
(
time
)
+
" seconds is "
+
std
::
to_string
(
avg
);
std
::
to_string
(
time
)
+
" seconds is "
+
std
::
to_string
(
avg
)
+
"
\n
"
;
status
=
status_codes
::
OK
;
break
;
}
}
}
}
}
}
}
}
...
...
@@ -192,7 +192,7 @@ void RestAPIServer::handle_get(http_request& msg) {
}
sendResponse:
RLOG
(
info
)
<<
"Responding: "
<<
output
;
RLOG
(
info
)
<<
"Responding:
\n
"
<<
output
;
response
.
set_status_code
(
status
);
response
.
set_body
(
output
);
msg
.
reply
(
response
);
...
...
@@ -245,7 +245,7 @@ void RestAPIServer::handle_put(http_request& msg) {
std
::
string
action
=
pathParts
[
1
];
//process actual request
output
=
"Plugin not found!"
;
output
=
"Plugin not found!
\n
"
;
status
=
status_codes
::
NotFound
;
//switch code depending on selected action
...
...
@@ -255,7 +255,7 @@ void RestAPIServer::handle_put(http_request& msg) {
for
(
const
auto
&
g
:
p
.
configurator
->
getSensorGroups
())
{
g
->
start
();
}
output
=
"Plugin "
+
pathParts
[
0
]
+
": Sensors started"
;
output
=
"Plugin "
+
pathParts
[
0
]
+
": Sensors started
\n
"
;
status
=
status_codes
::
OK
;
break
;
}
...
...
@@ -266,7 +266,7 @@ void RestAPIServer::handle_put(http_request& msg) {
for
(
const
auto
&
g
:
p
.
configurator
->
getSensorGroups
())
{
g
->
stop
();
}
output
=
"Plugin "
+
pathParts
[
0
]
+
": Sensors stopped"
;
output
=
"Plugin "
+
pathParts
[
0
]
+
": Sensors stopped
\n
"
;
status
=
status_codes
::
OK
;
break
;
}
...
...
@@ -286,12 +286,12 @@ void RestAPIServer::handle_put(http_request& msg) {
if
(
p
.
configurator
->
reReadConfig
())
{
// Perform checks on MQTT topics
if
(
!
checkTopics
(
p
))
{
output
=
"Plugin "
+
pathParts
[
0
]
+
": problematic MQTT topics or sensor names, please check your config files!"
;
output
=
"Plugin "
+
pathParts
[
0
]
+
": problematic MQTT topics or sensor names, please check your config files!
\n
"
;
status
=
status_codes
::
InternalError
;
removeTopics
(
p
);
p
.
configurator
->
clearConfig
();
}
else
{
output
=
"Plugin "
+
pathParts
[
0
]
+
": Configuration reloaded"
;
output
=
"Plugin "
+
pathParts
[
0
]
+
": Configuration reloaded
\n
"
;
status
=
status_codes
::
OK
;
for
(
const
auto
&
g
:
p
.
configurator
->
getSensorGroups
())
{
g
->
init
(
_io
);
...
...
@@ -332,7 +332,7 @@ void RestAPIServer::handle_put(http_request& msg) {
}
sendResponse:
RLOG
(
info
)
<<
"Responding: "
<<
output
;
RLOG
(
info
)
<<
"Responding:
\n
"
<<
output
;
response
.
set_status_code
(
status
);
response
.
set_body
(
output
);
msg
.
reply
(
response
);
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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