Rework the server to remove rest_xxx classes.
parent
43aadee885
commit
4140b4f94c
@ -1,70 +0,0 @@
|
||||
#include "webcc/rest_request_handler.h"
|
||||
|
||||
#include <utility> // for move()
|
||||
#include <vector>
|
||||
|
||||
#include "webcc/logger.h"
|
||||
#include "webcc/url.h"
|
||||
|
||||
#if WEBCC_ENABLE_GZIP
|
||||
#include "webcc/gzip.h"
|
||||
#endif
|
||||
|
||||
namespace webcc {
|
||||
|
||||
bool RestRequestHandler::Bind(RestServicePtr service, const std::string& url,
|
||||
bool is_regex) {
|
||||
return service_manager_.AddService(service, url, is_regex);
|
||||
}
|
||||
|
||||
void RestRequestHandler::HandleConnection(ConnectionPtr connection) {
|
||||
RequestPtr request = connection->request();
|
||||
|
||||
const Url& url = request->url();
|
||||
|
||||
RestRequest rest_request{ request };
|
||||
|
||||
// Get service by URL path.
|
||||
std::string path = "/" + url.path();
|
||||
auto service = service_manager_.GetService(path, &rest_request.url_matches);
|
||||
|
||||
if (!service) {
|
||||
LOG_WARN("No service matches the URL path: %s", url.path().c_str());
|
||||
connection->SendResponse(Status::kNotFound);
|
||||
return;
|
||||
}
|
||||
|
||||
RestResponse rest_response;
|
||||
service->Handle(rest_request, &rest_response);
|
||||
|
||||
auto response = std::make_shared<Response>(rest_response.status);
|
||||
|
||||
if (!rest_response.content.empty()) {
|
||||
if (!rest_response.media_type.empty()) {
|
||||
response->SetContentType(rest_response.media_type, rest_response.charset);
|
||||
}
|
||||
SetContent(request, response, std::move(rest_response.content));
|
||||
}
|
||||
|
||||
// Send response back to client.
|
||||
connection->SendResponse(response);
|
||||
}
|
||||
|
||||
void RestRequestHandler::SetContent(RequestPtr request, ResponsePtr response,
|
||||
std::string&& content) {
|
||||
#if WEBCC_ENABLE_GZIP
|
||||
// Only support gzip (no deflate) for response compression.
|
||||
if (content.size() > kGzipThreshold && request->AcceptEncodingGzip()) {
|
||||
std::string compressed;
|
||||
if (gzip::Compress(content, &compressed)) {
|
||||
response->SetHeader(headers::kContentEncoding, "gzip");
|
||||
response->SetContent(std::move(compressed), true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
#endif // WEBCC_ENABLE_GZIP
|
||||
|
||||
response->SetContent(std::move(content), true);
|
||||
}
|
||||
|
||||
} // namespace webcc
|
@ -1,33 +0,0 @@
|
||||
#ifndef WEBCC_REST_REQUEST_HANDLER_H_
|
||||
#define WEBCC_REST_REQUEST_HANDLER_H_
|
||||
|
||||
// HTTP server handling REST requests.
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "webcc/request_handler.h"
|
||||
#include "webcc/rest_service_manager.h"
|
||||
|
||||
namespace webcc {
|
||||
|
||||
class RestRequestHandler : public RequestHandler {
|
||||
public:
|
||||
RestRequestHandler() = default;
|
||||
|
||||
~RestRequestHandler() override = default;
|
||||
|
||||
bool Bind(RestServicePtr service, const std::string& url, bool is_regex);
|
||||
|
||||
private:
|
||||
void HandleConnection(ConnectionPtr connection) override;
|
||||
|
||||
void SetContent(RequestPtr request, ResponsePtr response,
|
||||
std::string&& content);
|
||||
|
||||
private:
|
||||
RestServiceManager service_manager_;
|
||||
};
|
||||
|
||||
} // namespace webcc
|
||||
|
||||
#endif // WEBCC_REST_REQUEST_HANDLER_H_
|
@ -1,45 +0,0 @@
|
||||
#ifndef WEBCC_REST_SERVER_H_
|
||||
#define WEBCC_REST_SERVER_H_
|
||||
|
||||
// HTTP server handling REST requests.
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "webcc/server.h"
|
||||
#include "webcc/rest_request_handler.h"
|
||||
#include "webcc/rest_service.h"
|
||||
|
||||
namespace webcc {
|
||||
|
||||
class RestServer : public Server {
|
||||
public:
|
||||
RestServer(std::uint16_t port, std::size_t workers)
|
||||
: Server(port, workers) {
|
||||
}
|
||||
|
||||
~RestServer() override = default;
|
||||
|
||||
// Bind a REST service to the given URL path.
|
||||
// The URL should start with "/" and it will be treated as a regular
|
||||
// expression if |is_regex| is true.
|
||||
// Examples:
|
||||
// - "/instances"
|
||||
// - "/instances/(\\d+)"
|
||||
// Binding to the same URL multiple times is allowed, but only the last one
|
||||
// takes effect.
|
||||
bool Bind(RestServicePtr service, const std::string& url, bool is_regex) {
|
||||
return request_handler_.Bind(service, url, is_regex);
|
||||
}
|
||||
|
||||
private:
|
||||
RequestHandler* GetRequestHandler() override {
|
||||
return &request_handler_;
|
||||
}
|
||||
|
||||
private:
|
||||
RestRequestHandler request_handler_;
|
||||
};
|
||||
|
||||
} // namespace webcc
|
||||
|
||||
#endif // WEBCC_REST_SERVER_H_
|
Loading…
Reference in New Issue