|
@@ -7,7 +7,7 @@
|
|
|
|
|
|
|
|
namespace Http {
|
|
namespace Http {
|
|
|
|
|
|
|
|
-HttpRequestHandler::HttpRequestHandler(std::function<std::string(const std::string&, const std::vector<HttpPostData>&)> callback) :
|
|
|
|
|
|
|
+HttpRequestHandler::HttpRequestHandler(HttpServer::CallbackMethod callback) :
|
|
|
m_callback(callback)
|
|
m_callback(callback)
|
|
|
{
|
|
{
|
|
|
}
|
|
}
|
|
@@ -21,15 +21,28 @@ HttpReply HttpRequestHandler::HandleRequest(const HttpRequest& request, const st
|
|
|
if (requestPath.empty() || requestPath[0] != '/' || requestPath.find("..") != std::string::npos)
|
|
if (requestPath.empty() || requestPath[0] != '/' || requestPath.find("..") != std::string::npos)
|
|
|
return HttpReply::StockReply(HttpReply::Status::BadRequest);
|
|
return HttpReply::StockReply(HttpReply::Status::BadRequest);
|
|
|
|
|
|
|
|
- if (!StringAlgorithm::iequals(request.uri, "/api") && !StringAlgorithm::istarts_with(request.uri, "/api/"))
|
|
|
|
|
- return HttpReply::StockReply(HttpReply::Status::NotFound);
|
|
|
|
|
|
|
+ std::vector<HttpPostData> postData;
|
|
|
|
|
+ std::string boundary = request.headers.GetBoundary();
|
|
|
|
|
+ if (!boundary.empty())
|
|
|
|
|
+ {
|
|
|
|
|
+ postData = GetPostData(request.headers.GetBoundary(), data);
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ if (!data.empty())
|
|
|
|
|
+ {
|
|
|
|
|
+ HttpPostData httpPostData;
|
|
|
|
|
+ httpPostData.name = "data";
|
|
|
|
|
+ httpPostData.value = data;
|
|
|
|
|
+ postData.push_back(httpPostData);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- std::vector<HttpPostData> postData = GetPostData(request.headers.GetBoundary(), data);
|
|
|
|
|
- std::string answer = HandlePostData(request.uri.substr(4), postData);
|
|
|
|
|
|
|
+ HttpServer::HttpReply forwardReply = ForwardRequest(request.uri, postData);
|
|
|
|
|
|
|
|
HttpReply reply;
|
|
HttpReply reply;
|
|
|
- reply.status = HttpReply::Status::Ok;
|
|
|
|
|
- reply.content = answer;
|
|
|
|
|
|
|
+ reply.status = Conversions::HttpReply(forwardReply.status);
|
|
|
|
|
+ reply.content = forwardReply.content;
|
|
|
|
|
|
|
|
reply.headers.resize(3);
|
|
reply.headers.resize(3);
|
|
|
reply.headers[0].name = "Content-Length";
|
|
reply.headers[0].name = "Content-Length";
|
|
@@ -91,7 +104,7 @@ std::vector<HttpPostData> HttpRequestHandler::GetPostData(const std::string& bou
|
|
|
return postDataCollection;
|
|
return postDataCollection;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-std::string HttpRequestHandler::HandlePostData(const std::string& uri, const std::vector<Http::HttpPostData>& postData)
|
|
|
|
|
|
|
+HttpServer::HttpReply HttpRequestHandler::ForwardRequest(const std::string& uri, const std::vector<Http::HttpPostData>& postData)
|
|
|
{
|
|
{
|
|
|
return m_callback(uri, postData);
|
|
return m_callback(uri, postData);
|
|
|
}
|
|
}
|