diff options
author | Roland Reichwein <mail@reichwein.it> | 2020-04-18 15:07:33 +0200 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2020-04-18 15:07:33 +0200 |
commit | 5400eaea898bcf6526d5c18fa8c274ee51081002 (patch) | |
tree | 437d91f177860bcca2f0900bb4018dfc15b35c21 /response.cpp | |
parent | 39bd177bdb80c24e73f7cf3db4239e55e13eb152 (diff) |
CGI interface
Diffstat (limited to 'response.cpp')
-rw-r--r-- | response.cpp | 58 |
1 files changed, 48 insertions, 10 deletions
diff --git a/response.cpp b/response.cpp index 0c619a2..ca7a58d 100644 --- a/response.cpp +++ b/response.cpp @@ -59,6 +59,10 @@ public: std::string GetTarget() const {return m_target;} std::string GetHost() const {return m_host;} + + Server& GetServer() const {return m_server; } + + const Socket& GetSocket() const {return m_server.GetSocket(); } }; std::string extend_index_html(std::string path) @@ -68,30 +72,64 @@ std::string extend_index_html(std::string path) return path; } +std::unordered_map<std::string, std::function<std::string(Server&)>> GetServerParamFunctions{ + // following are the supported fields: + {"version", [](Server& server) { return Server::VersionString; }}, + {"address", [](Server& server) { return server.GetSocket().address; }}, + {"port", [](Server& server) { return server.GetSocket().port; }}, +}; + std::string GetServerParam(const std::string& key, Server& server) { - // following are the supported fields: - // ... + auto it = GetServerParamFunctions.find(key); + if (it != GetServerParamFunctions.end()) + return it->second(server); + throw std::runtime_error("Unsupported server param: "s + key); } std::unordered_map<std::string, std::function<std::string(RequestContext&)>> GetRequestParamFunctions{ // following are the supported fields: - {"target", [](RequestContext& req_ctx) {return req_ctx.GetTarget();}}, + {"authorization", [](RequestContext& req_ctx) { return std::string{req_ctx.GetReq()[http::field::authorization]}; }}, + + {"body", [](RequestContext& req_ctx) { return req_ctx.GetReq().body(); }}, - {"rel_target", [](RequestContext& req_ctx) {return req_ctx.GetRelativePath();}}, + {"content_length", [](RequestContext& req_ctx) { return std::to_string(req_ctx.GetReq().body().size()); }}, + + {"content_type", [](RequestContext& req_ctx) { return std::string{req_ctx.GetReq()[http::field::content_type]}; }}, {"doc_root", [](RequestContext& req_ctx) { return req_ctx.GetDocRoot();}}, + + {"host", [](RequestContext& req_ctx) { return req_ctx.GetHost();}}, - {"body", [](RequestContext& req_ctx) { return req_ctx.GetReq().body(); }}, + {"http_accept", [](RequestContext& req_ctx) { return std::string{req_ctx.GetReq()[http::field::accept]};}}, - {"content_length", [](RequestContext& req_ctx) { return std::to_string(req_ctx.GetReq().body().size()); }}, + {"http_accept_charset", [](RequestContext& req_ctx) { return std::string{req_ctx.GetReq()[http::field::accept_charset]};}}, - {"content_type", [](RequestContext& req_ctx) { return std::string{req_ctx.GetReq()[http::field::content_type]}; }}, + {"http_accept_encoding", [](RequestContext& req_ctx) { return std::string{req_ctx.GetReq()[http::field::accept_encoding]};}}, - {"method", [](RequestContext& req_ctx) { return std::string{req_ctx.GetReq().method_string()};}}, + {"http_accept_language", [](RequestContext& req_ctx) { return std::string{req_ctx.GetReq()[http::field::accept_language]};}}, + + {"http_connection", [](RequestContext& req_ctx) { return std::string{req_ctx.GetReq()[http::field::connection]};}}, + + {"http_host", [](RequestContext& req_ctx) { return std::string{req_ctx.GetReq()[http::field::host]};}}, + + {"http_user_agent", [](RequestContext& req_ctx) { return std::string{req_ctx.GetReq()[http::field::user_agent]};}}, + {"http_version", [](RequestContext& req_ctx) { + unsigned version {req_ctx.GetReq().version()}; + unsigned major{version / 10}; + unsigned minor{version % 10}; + return "HTTP/"s + std::to_string(major) + "."s + std::to_string(minor); + }}, + {"location", [](RequestContext& req_ctx) { return req_ctx.GetTarget(); }}, + + {"method", [](RequestContext& req_ctx) { return std::string{req_ctx.GetReq().method_string()};}}, + + {"rel_target", [](RequestContext& req_ctx) {return req_ctx.GetRelativePath();}}, + + {"target", [](RequestContext& req_ctx) {return req_ctx.GetTarget();}}, }; std::string GetRequestParam(const std::string& key, RequestContext& req_ctx) @@ -187,7 +225,7 @@ response_type HttpStatus(std::string status, std::string message, response_type& res.set(http::field::content_type, "text/html"); if (res.result_int() == 401) res.set(http::field::www_authenticate, "Basic realm=\"Webbox Login\""); - res.body() = "<html><body><h1>"s + VersionString + " Error</h1><p>"s + status + " "s + message + "</p></body></html>"s; + res.body() = "<html><body><h1>"s + Server::VersionString + " Error</h1><p>"s + status + " "s + message + "</p></body></html>"s; res.prepare_payload(); return res; @@ -198,7 +236,7 @@ response_type HttpStatus(std::string status, std::string message, response_type& response_type generate_response(request_type& req, Server& server) { response_type res{http::status::ok, req.version()}; - res.set(http::field::server, VersionString); + res.set(http::field::server, Server::VersionString); res.set(http::field::content_type, mime_type(extend_index_html(std::string(req.target())))); res.keep_alive(req.keep_alive()); |