Remove API Key and Update Callback
This commit is contained in:
@@ -241,4 +241,49 @@ HttpReply HttpReply::StockReply(HttpReply::Status::type status)
|
||||
return reply;
|
||||
}
|
||||
|
||||
namespace Conversions {
|
||||
|
||||
HttpReply::Status::type HttpReply(HttpServer::HttpReply::Status::type status)
|
||||
{
|
||||
switch (status)
|
||||
{
|
||||
case HttpServer::HttpReply::Status::Ok:
|
||||
return HttpReply::Status::Ok;
|
||||
case HttpServer::HttpReply::Status::Created:
|
||||
return HttpReply::Status::Created;
|
||||
case HttpServer::HttpReply::Status::Accepted:
|
||||
return HttpReply::Status::Accepted;
|
||||
case HttpServer::HttpReply::Status::NoContent:
|
||||
return HttpReply::Status::NoContent;
|
||||
case HttpServer::HttpReply::Status::MultipleChoices:
|
||||
return HttpReply::Status::MultipleChoices;
|
||||
case HttpServer::HttpReply::Status::MovedPermanently:
|
||||
return HttpReply::Status::MovedPermanently;
|
||||
case HttpServer::HttpReply::Status::MovedTemporarily:
|
||||
return HttpReply::Status::MovedTemporarily;
|
||||
case HttpReply::Status::NotModified:
|
||||
return HttpReply::Status::NotModified;
|
||||
case HttpServer::HttpReply::Status::BadRequest:
|
||||
return HttpReply::Status::BadRequest;
|
||||
case HttpServer::HttpReply::Status::Unauthorized:
|
||||
return HttpReply::Status::Unauthorized;
|
||||
case HttpServer::HttpReply::Status::Forbidden:
|
||||
return HttpReply::Status::Forbidden;
|
||||
case HttpServer::HttpReply::Status::NotFound:
|
||||
return HttpReply::Status::NotFound;
|
||||
case HttpServer::HttpServer::HttpReply::Status::InternalServerError:
|
||||
return HttpReply::Status::InternalServerError;
|
||||
case HttpServer::HttpReply::Status::NotImplemented:
|
||||
return HttpReply::Status::NotImplemented;
|
||||
case HttpServer::HttpReply::Status::BadGateway:
|
||||
return HttpReply::Status::BadGateway;
|
||||
case HttpServer::HttpReply::Status::ServiceUnavailable:
|
||||
return HttpReply::Status::ServiceUnavailable;
|
||||
default:
|
||||
return HttpReply::Status::InternalServerError;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Conversions
|
||||
|
||||
} // namespace Http
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define HTTP_HTTPREPLY_H
|
||||
|
||||
#include "HttpHeaders.h"
|
||||
#include "HttpServer.h"
|
||||
#include <asio.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@@ -41,6 +42,12 @@ struct HttpReply
|
||||
static HttpReply StockReply(Status::type status);
|
||||
};
|
||||
|
||||
namespace Conversions {
|
||||
|
||||
HttpReply::Status::type HttpReply(HttpServer::HttpReply::Status::type status);
|
||||
|
||||
} // namespace Conversions
|
||||
|
||||
} // namespace Http
|
||||
|
||||
#endif // HTTP_HTTPREPLY_H
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
namespace Http {
|
||||
|
||||
HttpRequestHandler::HttpRequestHandler(std::function<std::string(const std::string&, const std::vector<HttpPostData>&)> callback) :
|
||||
HttpRequestHandler::HttpRequestHandler(HttpServer::CallbackMethod 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)
|
||||
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;
|
||||
reply.status = HttpReply::Status::Ok;
|
||||
reply.content = answer;
|
||||
reply.status = Conversions::HttpReply(forwardReply.status);
|
||||
reply.content = forwardReply.content;
|
||||
|
||||
reply.headers.resize(3);
|
||||
reply.headers[0].name = "Content-Length";
|
||||
@@ -91,7 +104,7 @@ std::vector<HttpPostData> HttpRequestHandler::GetPostData(const std::string& bou
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "HttpReply.h"
|
||||
#include "HttpRequest.h"
|
||||
#include "HttpPostData.h"
|
||||
#include "HttpServer.h"
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@@ -14,7 +15,7 @@ namespace Http {
|
||||
class HttpRequestHandler
|
||||
{
|
||||
public:
|
||||
explicit HttpRequestHandler(std::function<std::string(const std::string&, const std::vector<HttpPostData>&)> callback);
|
||||
explicit HttpRequestHandler(HttpServer::CallbackMethod callback);
|
||||
|
||||
HttpRequestHandler(const HttpRequestHandler&) = delete;
|
||||
|
||||
@@ -22,12 +23,12 @@ public:
|
||||
|
||||
private:
|
||||
std::vector<HttpPostData> GetPostData(const std::string& boundary, const std::string& data) const;
|
||||
std::string HandlePostData(const std::string& uri, const std::vector<HttpPostData>& postData);
|
||||
HttpServer::HttpReply ForwardRequest(const std::string& uri, const std::vector<HttpPostData>& postData);
|
||||
|
||||
static bool URLDecode(const std::string& in, std::string& out);
|
||||
|
||||
private:
|
||||
std::function<std::string(const std::string&, const std::vector<HttpPostData>&)> m_callback;
|
||||
HttpServer::CallbackMethod m_callback;
|
||||
};
|
||||
|
||||
} // namespace Http
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@
|
||||
|
||||
namespace Http {
|
||||
|
||||
HttpServer::HttpServer(unsigned short port, std::function<std::string(const std::string&, const std::vector<HttpPostData>&)> callback) :
|
||||
HttpServer::HttpServer(unsigned short port, CallbackMethod callback) :
|
||||
m_pHttpServerImpl(new HttpServerImpl(port, callback))
|
||||
{
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
namespace Http {
|
||||
|
||||
HttpServerImpl::HttpServerImpl(unsigned short port, std::function<std::string(const std::string&, const std::vector<HttpPostData>&)> callback) :
|
||||
HttpServerImpl::HttpServerImpl(unsigned short port, HttpServer::CallbackMethod callback) :
|
||||
m_threadPoolSize(3),
|
||||
m_signals(m_ioService),
|
||||
m_acceptor(m_ioService),
|
||||
|
||||
@@ -15,7 +15,7 @@ namespace Http {
|
||||
class HttpServerImpl
|
||||
{
|
||||
public:
|
||||
HttpServerImpl(unsigned short port, std::function<std::string(const std::string&, const std::vector<HttpPostData>&)> callback);
|
||||
HttpServerImpl(unsigned short port, HttpServer::CallbackMethod callback);
|
||||
~HttpServerImpl();
|
||||
|
||||
void Wait();
|
||||
|
||||
+34
-1
@@ -14,7 +14,40 @@ class HttpServerImpl;
|
||||
class HttpServer
|
||||
{
|
||||
public:
|
||||
HttpServer(unsigned short port, std::function<std::string(const std::string&, const std::vector<HttpPostData>&)> callback);
|
||||
struct HttpReply
|
||||
{
|
||||
struct Status
|
||||
{
|
||||
enum type
|
||||
{
|
||||
Ok = 200,
|
||||
Created = 201,
|
||||
Accepted = 202,
|
||||
NoContent = 204,
|
||||
MultipleChoices = 300,
|
||||
MovedPermanently = 301,
|
||||
MovedTemporarily = 302,
|
||||
NotModified = 304,
|
||||
BadRequest = 400,
|
||||
Unauthorized = 401,
|
||||
Forbidden = 403,
|
||||
NotFound = 404,
|
||||
InternalServerError = 500,
|
||||
NotImplemented = 501,
|
||||
BadGateway = 502,
|
||||
ServiceUnavailable = 503
|
||||
};
|
||||
};
|
||||
|
||||
Status::type status;
|
||||
std::string content;
|
||||
};
|
||||
|
||||
public:
|
||||
typedef std::function<HttpReply(const std::string&, const std::vector<HttpPostData>&)> CallbackMethod;
|
||||
|
||||
public:
|
||||
HttpServer(unsigned short port, CallbackMethod callback);
|
||||
~HttpServer();
|
||||
|
||||
void Wait();
|
||||
|
||||
Reference in New Issue
Block a user