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