9 static const char rcsid[] __attribute__((used)) =
"$Id: helpers.cpp 1653 2016-02-28 19:54:59Z sella $";
12 #include "../util/CommonMacro.h"
13 #include "../util/Log.h"
14 #include "../util/String.h"
15 #include "../net/IPAddressPort.h"
17 using namespace sella::server;
19 sella::variant::Variant http::parseURI(
const HTTPConnection::shared &connection,
bool addHeader,
bool addSession,
bool addCRUD,
bool addBody)
throw () {
24 auto &uri = data[
"_uri"];
25 const std::string &ruri = req.getURI();
26 const std::string &path = req.getPath();
27 const std::string &query = req.getQuery();
28 const std::string &host = req.getHost();
29 const auto &headers = req.getHeaders();
32 std::vector<std::string> parts;
35 parts = sella::util::String::split(query,
'&',
false);
36 for (
auto p = parts.begin(); p != parts.end(); ++p) {
37 const auto &kv = sella::util::String::split(*p,
'=',
true, 2);
41 }
else if (kv.size() == 2) {
47 parts = sella::util::String::split(path,
'/',
false);
48 for (
size_t i = 0; i < parts.size(); i++) {
49 uri[sella::util::String::sprintf(
"%zd", i + 2)] = parts.at(i);
52 if (connection->isSecure()) {
53 uri[
"0"] = std::string(
"https://").append(host).append(ruri);
55 uri[
"0"] = std::string(
"http://").append(host).append(ruri);
61 auto &header = data[
"_header"];
63 for (
auto h = headers.begin(); h != headers.end(); ++h) {
64 std::vector<std::string> parts = sella::util::String::split(*h,
": ",
true,
true);
66 if (parts.size() == 2) {
67 header[parts[0]] = parts[1];
68 }
else if (parts.size() == 1) {
75 auto &session = data[
"_session"];
78 size_t pos = host.find_last_of(
':'), pos2 = host.find_last_of(
']');
80 if (pos == std::string::npos || (pos2 != std::string::npos && pos2 > pos)) {
81 session[
"vhost"] = host;
82 session[
"port"] = local.getPort().str();
84 session[
"vhost"] = host.substr(0, pos);
85 session[
"port"] = host.substr(pos + 1);
87 }
catch (std::out_of_range &e) {
88 if (addSession && !session.contains(
"vhost")) {
89 session[
"vhost"] =
"default";
93 session[
"secure"] = connection->isSecure();
94 session[
"method"] = req.getMethodRaw();
95 session[
"version"] = req.getVersion();
96 session[
"uri"] = ruri;
97 session[
"path"] = path;
98 session[
"query"] = query;
99 session[
"local"] = local.str();
100 session[
"local-ip"] = local.getIPAddress().str();
101 session[
"local-port"] = local.getPort().str();
102 session[
"remote"] = remote.str();
103 session[
"remote-ip"] = remote.getIPAddress().str();
104 session[
"remote-port"] = remote.getPort().str();
108 auto &session = data[
"_session"];
110 switch (req.getMethod()) {
111 case HTTPRequest::Post:
112 session[
"function"] = CRUD::Create;
113 session[
"function-name"] =
"create";
116 case HTTPRequest::Get:
117 session[
"function"] = CRUD::Read;
118 session[
"function-name"] =
"read";
121 case HTTPRequest::Patch:
122 case HTTPRequest::Put:
123 session[
"function"] = CRUD::Update;
124 session[
"function-name"] =
"update";
127 case HTTPRequest::Delete:
128 session[
"function"] = CRUD::Delete;
129 session[
"function-name"] =
"delete";
133 session[
"function"] = CRUD::None;
134 session[
"function-name"] =
"none";
146 std::string http::buildOutput(
const HTTPConnection::shared &connection,
sella::variant::Variant &data)
throw (ServerException) {
148 const HTTPRequest &request = connection->getRequest();
150 switch (request.getAcceptType()) {
151 case HTTPRequest::AcceptType::JSON:
152 output = data.json();
156 case HTTPRequest::AcceptType::XML:
161 case HTTPRequest::AcceptType::CSV: