Rename HttpSession back to HttpConnection.
This commit is contained in:
@@ -32,7 +32,7 @@ bool CalcClient::Divide(double x, double y, double* result) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool CalcClient::NotExist(double x, double y, double* result) {
|
bool CalcClient::NotExist(double x, double y, double* result) {
|
||||||
return Calc("notexist", "x", "y", x, y, result);
|
return Calc("not_exist", "x", "y", x, y, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CalcClient::Init() {
|
void CalcClient::Init() {
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ int main() {
|
|||||||
|
|
||||||
// Try to call a non-existing operation.
|
// Try to call a non-existing operation.
|
||||||
if (calc.NotExist(x, y, &result)) {
|
if (calc.NotExist(x, y, &result)) {
|
||||||
printf("notexist: %.1f\n", result);
|
printf("not_exist: %.1f\n", result);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ set(SRCS
|
|||||||
http_async_client.h
|
http_async_client.h
|
||||||
http_client.cc
|
http_client.cc
|
||||||
http_client.h
|
http_client.h
|
||||||
|
http_connection.cc
|
||||||
|
http_connection.h
|
||||||
http_message.cc
|
http_message.cc
|
||||||
http_message.h
|
http_message.h
|
||||||
http_parser.cc
|
http_parser.cc
|
||||||
@@ -24,8 +26,6 @@ set(SRCS
|
|||||||
http_response_parser.h
|
http_response_parser.h
|
||||||
http_server.cc
|
http_server.cc
|
||||||
http_server.h
|
http_server.h
|
||||||
http_session.cc
|
|
||||||
http_session.h
|
|
||||||
logger.cc
|
logger.cc
|
||||||
logger.h
|
logger.h
|
||||||
queue.h
|
queue.h
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#include "webcc/http_session.h"
|
#include "webcc/http_connection.h"
|
||||||
|
|
||||||
#include <utility> // for move()
|
#include <utility> // for move()
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@@ -11,42 +11,42 @@
|
|||||||
|
|
||||||
namespace webcc {
|
namespace webcc {
|
||||||
|
|
||||||
HttpSession::HttpSession(boost::asio::ip::tcp::socket socket,
|
HttpConnection::HttpConnection(boost::asio::ip::tcp::socket socket,
|
||||||
HttpRequestHandler* handler)
|
HttpRequestHandler* handler)
|
||||||
: socket_(std::move(socket)),
|
: socket_(std::move(socket)),
|
||||||
request_handler_(handler),
|
request_handler_(handler),
|
||||||
request_parser_(&request_) {
|
request_parser_(&request_) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpSession::Start() {
|
void HttpConnection::Start() {
|
||||||
AsyncRead();
|
AsyncRead();
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpSession::Close() {
|
void HttpConnection::Close() {
|
||||||
boost::system::error_code ec;
|
boost::system::error_code ec;
|
||||||
socket_.close(ec);
|
socket_.close(ec);
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpSession::SetResponseContent(std::string&& content,
|
void HttpConnection::SetResponseContent(std::string&& content,
|
||||||
const std::string& content_type) {
|
const std::string& content_type) {
|
||||||
response_.SetContent(std::move(content));
|
response_.SetContent(std::move(content));
|
||||||
response_.SetContentType(content_type);
|
response_.SetContentType(content_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpSession::SendResponse(HttpStatus::Enum status) {
|
void HttpConnection::SendResponse(HttpStatus::Enum status) {
|
||||||
response_.set_status(status);
|
response_.set_status(status);
|
||||||
AsyncWrite();
|
AsyncWrite();
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpSession::AsyncRead() {
|
void HttpConnection::AsyncRead() {
|
||||||
socket_.async_read_some(boost::asio::buffer(buffer_),
|
socket_.async_read_some(boost::asio::buffer(buffer_),
|
||||||
std::bind(&HttpSession::ReadHandler,
|
std::bind(&HttpConnection::ReadHandler,
|
||||||
shared_from_this(),
|
shared_from_this(),
|
||||||
std::placeholders::_1,
|
std::placeholders::_1,
|
||||||
std::placeholders::_2));
|
std::placeholders::_2));
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpSession::ReadHandler(boost::system::error_code ec,
|
void HttpConnection::ReadHandler(boost::system::error_code ec,
|
||||||
std::size_t length) {
|
std::size_t length) {
|
||||||
if (ec) {
|
if (ec) {
|
||||||
if (ec != boost::asio::error::operation_aborted) {
|
if (ec != boost::asio::error::operation_aborted) {
|
||||||
@@ -70,16 +70,16 @@ void HttpSession::ReadHandler(boost::system::error_code ec,
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Enqueue this session.
|
// Enqueue this connection.
|
||||||
// Some worker thread will handle it later.
|
// Some worker thread will handle it later.
|
||||||
// And DoWrite() will be called in the worker thread.
|
// And DoWrite() will be called in the worker thread.
|
||||||
request_handler_->Enqueue(shared_from_this());
|
request_handler_->Enqueue(shared_from_this());
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpSession::AsyncWrite() {
|
void HttpConnection::AsyncWrite() {
|
||||||
boost::asio::async_write(socket_,
|
boost::asio::async_write(socket_,
|
||||||
response_.ToBuffers(),
|
response_.ToBuffers(),
|
||||||
std::bind(&HttpSession::WriteHandler,
|
std::bind(&HttpConnection::WriteHandler,
|
||||||
shared_from_this(),
|
shared_from_this(),
|
||||||
std::placeholders::_1,
|
std::placeholders::_1,
|
||||||
std::placeholders::_2));
|
std::placeholders::_2));
|
||||||
@@ -89,7 +89,7 @@ void HttpSession::AsyncWrite() {
|
|||||||
// This write handler will be called from main thread (the thread calling
|
// This write handler will be called from main thread (the thread calling
|
||||||
// io_context.run), even though DoWrite() is invoked by worker threads. This is
|
// io_context.run), even though DoWrite() is invoked by worker threads. This is
|
||||||
// ensured by Asio.
|
// ensured by Asio.
|
||||||
void HttpSession::WriteHandler(boost::system::error_code ec,
|
void HttpConnection::WriteHandler(boost::system::error_code ec,
|
||||||
std::size_t length) {
|
std::size_t length) {
|
||||||
if (!ec) {
|
if (!ec) {
|
||||||
LOG_INFO("Response has been sent back.");
|
LOG_INFO("Response has been sent back.");
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
#ifndef WEBCC_HTTP_SESSION_H_
|
#ifndef WEBCC_HTTP_CONNECTION_H_
|
||||||
#define WEBCC_HTTP_SESSION_H_
|
#define WEBCC_HTTP_CONNECTION_H_
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
@@ -16,14 +16,14 @@ namespace webcc {
|
|||||||
|
|
||||||
class HttpRequestHandler;
|
class HttpRequestHandler;
|
||||||
|
|
||||||
class HttpSession : public std::enable_shared_from_this<HttpSession> {
|
class HttpConnection : public std::enable_shared_from_this<HttpConnection> {
|
||||||
public:
|
public:
|
||||||
HttpSession(boost::asio::ip::tcp::socket socket,
|
HttpConnection(boost::asio::ip::tcp::socket socket,
|
||||||
HttpRequestHandler* handler);
|
HttpRequestHandler* handler);
|
||||||
|
|
||||||
~HttpSession() = default;
|
~HttpConnection() = default;
|
||||||
|
|
||||||
DELETE_COPY_AND_ASSIGN(HttpSession);
|
DELETE_COPY_AND_ASSIGN(HttpConnection);
|
||||||
|
|
||||||
const HttpRequest& request() const {
|
const HttpRequest& request() const {
|
||||||
return request_;
|
return request_;
|
||||||
@@ -67,8 +67,8 @@ class HttpSession : public std::enable_shared_from_this<HttpSession> {
|
|||||||
HttpResponse response_;
|
HttpResponse response_;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef std::shared_ptr<HttpSession> HttpSessionPtr;
|
typedef std::shared_ptr<HttpConnection> HttpConnectionPtr;
|
||||||
|
|
||||||
} // namespace webcc
|
} // namespace webcc
|
||||||
|
|
||||||
#endif // WEBCC_HTTP_SESSION_H_
|
#endif // WEBCC_HTTP_CONNECTION_H_
|
||||||
@@ -31,7 +31,7 @@ void HttpMessage::Dump(std::ostream& os, std::size_t indent,
|
|||||||
os << indent_str << h.name << ": " << h.value << std::endl;
|
os << indent_str << h.name << ": " << h.value << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
os << std::endl;
|
os << indent_str << std::endl;
|
||||||
|
|
||||||
if (!content_.empty()) {
|
if (!content_.empty()) {
|
||||||
if (indent == 0) {
|
if (indent == 0) {
|
||||||
|
|||||||
@@ -9,8 +9,8 @@
|
|||||||
|
|
||||||
namespace webcc {
|
namespace webcc {
|
||||||
|
|
||||||
void HttpRequestHandler::Enqueue(HttpSessionPtr session) {
|
void HttpRequestHandler::Enqueue(HttpConnectionPtr connection) {
|
||||||
queue_.Push(session);
|
queue_.Push(connection);
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpRequestHandler::Start(std::size_t count) {
|
void HttpRequestHandler::Start(std::size_t count) {
|
||||||
@@ -24,14 +24,14 @@ void HttpRequestHandler::Start(std::size_t count) {
|
|||||||
void HttpRequestHandler::Stop() {
|
void HttpRequestHandler::Stop() {
|
||||||
LOG_INFO("Stopping workers...");
|
LOG_INFO("Stopping workers...");
|
||||||
|
|
||||||
// Close pending sessions.
|
// Close pending connections.
|
||||||
for (HttpSessionPtr conn = queue_.Pop(); conn; conn = queue_.Pop()) {
|
for (HttpConnectionPtr conn = queue_.Pop(); conn; conn = queue_.Pop()) {
|
||||||
LOG_INFO("Closing pending session...");
|
LOG_INFO("Closing pending connection...");
|
||||||
conn->Close();
|
conn->Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Enqueue a null session to trigger the first worker to stop.
|
// Enqueue a null connection to trigger the first worker to stop.
|
||||||
queue_.Push(HttpSessionPtr());
|
queue_.Push(HttpConnectionPtr());
|
||||||
|
|
||||||
workers_.join_all();
|
workers_.join_all();
|
||||||
|
|
||||||
@@ -42,19 +42,19 @@ void HttpRequestHandler::WorkerRoutine() {
|
|||||||
LOG_INFO("Worker is running.");
|
LOG_INFO("Worker is running.");
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
HttpSessionPtr session = queue_.PopOrWait();
|
HttpConnectionPtr connection = queue_.PopOrWait();
|
||||||
|
|
||||||
if (!session) {
|
if (!connection) {
|
||||||
LOG_INFO("Worker is going to stop.");
|
LOG_INFO("Worker is going to stop.");
|
||||||
|
|
||||||
// For stopping next worker.
|
// For stopping next worker.
|
||||||
queue_.Push(HttpSessionPtr());
|
queue_.Push(HttpConnectionPtr());
|
||||||
|
|
||||||
// Stop the worker.
|
// Stop the worker.
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
HandleSession(session);
|
HandleConnection(connection);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
#include "boost/thread/thread.hpp"
|
#include "boost/thread/thread.hpp"
|
||||||
|
|
||||||
#include "webcc/http_session.h"
|
#include "webcc/http_connection.h"
|
||||||
#include "webcc/queue.h"
|
#include "webcc/queue.h"
|
||||||
#include "webcc/soap_service.h"
|
#include "webcc/soap_service.h"
|
||||||
|
|
||||||
@@ -23,22 +23,22 @@ class HttpRequestHandler {
|
|||||||
|
|
||||||
DELETE_COPY_AND_ASSIGN(HttpRequestHandler);
|
DELETE_COPY_AND_ASSIGN(HttpRequestHandler);
|
||||||
|
|
||||||
// Put the session into the queue.
|
// Put the connection into the queue.
|
||||||
void Enqueue(HttpSessionPtr session);
|
void Enqueue(HttpConnectionPtr connection);
|
||||||
|
|
||||||
// Start worker threads.
|
// Start worker threads.
|
||||||
void Start(std::size_t count);
|
void Start(std::size_t count);
|
||||||
|
|
||||||
// Close pending sessions and stop worker threads.
|
// Close pending connections and stop worker threads.
|
||||||
void Stop();
|
void Stop();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void WorkerRoutine();
|
void WorkerRoutine();
|
||||||
|
|
||||||
// Called by the worker routine.
|
// Called by the worker routine.
|
||||||
virtual void HandleSession(HttpSessionPtr session) = 0;
|
virtual void HandleConnection(HttpConnectionPtr connection) = 0;
|
||||||
|
|
||||||
Queue<HttpSessionPtr> queue_;
|
Queue<HttpConnectionPtr> queue_;
|
||||||
boost::thread_group workers_;
|
boost::thread_group workers_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -64,10 +64,10 @@ void HttpServer::AsyncAccept() {
|
|||||||
if (!ec) {
|
if (!ec) {
|
||||||
LOG_INFO("Accepted a connection.");
|
LOG_INFO("Accepted a connection.");
|
||||||
|
|
||||||
HttpSessionPtr session{
|
HttpConnectionPtr connection{
|
||||||
new HttpSession(std::move(socket), GetRequestHandler())
|
new HttpConnection(std::move(socket), GetRequestHandler())
|
||||||
};
|
};
|
||||||
session->Start();
|
connection->Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
AsyncAccept();
|
AsyncAccept();
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
#include "boost/thread/thread.hpp"
|
#include "boost/thread/thread.hpp"
|
||||||
|
|
||||||
#include "webcc/globals.h"
|
#include "webcc/globals.h"
|
||||||
#include "webcc/http_session.h"
|
#include "webcc/http_connection.h"
|
||||||
|
|
||||||
namespace webcc {
|
namespace webcc {
|
||||||
|
|
||||||
|
|||||||
@@ -14,11 +14,11 @@ bool RestRequestHandler::Bind(RestServicePtr service,
|
|||||||
return service_manager_.AddService(service, url, is_regex);
|
return service_manager_.AddService(service, url, is_regex);
|
||||||
}
|
}
|
||||||
|
|
||||||
void RestRequestHandler::HandleSession(HttpSessionPtr session) {
|
void RestRequestHandler::HandleConnection(HttpConnectionPtr connection) {
|
||||||
Url url(session->request().url(), true);
|
Url url(connection->request().url(), true);
|
||||||
|
|
||||||
if (!url.IsValid()) {
|
if (!url.IsValid()) {
|
||||||
session->SendResponse(HttpStatus::kBadRequest);
|
connection->SendResponse(HttpStatus::kBadRequest);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,7 +27,7 @@ void RestRequestHandler::HandleSession(HttpSessionPtr session) {
|
|||||||
&sub_matches);
|
&sub_matches);
|
||||||
if (!service) {
|
if (!service) {
|
||||||
LOG_WARN("No service matches the URL: %s", url.path().c_str());
|
LOG_WARN("No service matches the URL: %s", url.path().c_str());
|
||||||
session->SendResponse(HttpStatus::kBadRequest);
|
connection->SendResponse(HttpStatus::kBadRequest);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,18 +35,18 @@ void RestRequestHandler::HandleSession(HttpSessionPtr session) {
|
|||||||
Url::SplitQuery(url.query(), &query);
|
Url::SplitQuery(url.query(), &query);
|
||||||
|
|
||||||
std::string content;
|
std::string content;
|
||||||
bool ok = service->Handle(session->request().method(),
|
bool ok = service->Handle(connection->request().method(),
|
||||||
sub_matches,
|
sub_matches,
|
||||||
query,
|
query,
|
||||||
session->request().content(),
|
connection->request().content(),
|
||||||
&content);
|
&content);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
session->SendResponse(HttpStatus::kBadRequest);
|
connection->SendResponse(HttpStatus::kBadRequest);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
session->SetResponseContent(std::move(content), kTextJsonUtf8);
|
connection->SetResponseContent(std::move(content), kTextJsonUtf8);
|
||||||
session->SendResponse(HttpStatus::kOK);
|
connection->SendResponse(HttpStatus::kOK);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace webcc
|
} // namespace webcc
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ class RestRequestHandler : public HttpRequestHandler {
|
|||||||
bool Bind(RestServicePtr service, const std::string& url, bool is_regex);
|
bool Bind(RestServicePtr service, const std::string& url, bool is_regex);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void HandleSession(HttpSessionPtr session) override;
|
void HandleConnection(HttpConnectionPtr connection) override;
|
||||||
|
|
||||||
RestServiceManager service_manager_;
|
RestServiceManager service_manager_;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -15,30 +15,30 @@ bool SoapRequestHandler::Bind(SoapServicePtr service, const std::string& url) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SoapRequestHandler::HandleSession(HttpSessionPtr session) {
|
void SoapRequestHandler::HandleConnection(HttpConnectionPtr connection) {
|
||||||
SoapServicePtr service = GetServiceByUrl(session->request().url());
|
SoapServicePtr service = GetServiceByUrl(connection->request().url());
|
||||||
if (!service) {
|
if (!service) {
|
||||||
session->SendResponse(HttpStatus::kBadRequest);
|
connection->SendResponse(HttpStatus::kBadRequest);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse the SOAP request XML.
|
// Parse the SOAP request XML.
|
||||||
SoapRequest soap_request;
|
SoapRequest soap_request;
|
||||||
if (!soap_request.FromXml(session->request().content())) {
|
if (!soap_request.FromXml(connection->request().content())) {
|
||||||
session->SendResponse(HttpStatus::kBadRequest);
|
connection->SendResponse(HttpStatus::kBadRequest);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
SoapResponse soap_response;
|
SoapResponse soap_response;
|
||||||
if (!service->Handle(soap_request, &soap_response)) {
|
if (!service->Handle(soap_request, &soap_response)) {
|
||||||
session->SendResponse(HttpStatus::kBadRequest);
|
connection->SendResponse(HttpStatus::kBadRequest);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string content;
|
std::string content;
|
||||||
soap_response.ToXml(&content);
|
soap_response.ToXml(&content);
|
||||||
session->SetResponseContent(std::move(content), kTextXmlUtf8);
|
connection->SetResponseContent(std::move(content), kTextXmlUtf8);
|
||||||
session->SendResponse(HttpStatus::kOK);
|
connection->SendResponse(HttpStatus::kOK);
|
||||||
}
|
}
|
||||||
|
|
||||||
SoapServicePtr SoapRequestHandler::GetServiceByUrl(const std::string& url) {
|
SoapServicePtr SoapRequestHandler::GetServiceByUrl(const std::string& url) {
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ class SoapRequestHandler : public HttpRequestHandler {
|
|||||||
bool Bind(SoapServicePtr service, const std::string& url);
|
bool Bind(SoapServicePtr service, const std::string& url);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void HandleSession(HttpSessionPtr session) override;
|
void HandleConnection(HttpConnectionPtr connection) override;
|
||||||
|
|
||||||
SoapServicePtr GetServiceByUrl(const std::string& url);
|
SoapServicePtr GetServiceByUrl(const std::string& url);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user