You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
1.8 KiB
C
83 lines
1.8 KiB
C
![]()
7 years ago
|
#ifndef WEBCC_HTTP_SESSION_H_
|
||
|
#define WEBCC_HTTP_SESSION_H_
|
||
![]()
8 years ago
|
|
||
|
#include <array>
|
||
|
#include <memory>
|
||
|
|
||
|
#include "boost/asio/ip/tcp.hpp" // for ip::tcp::socket
|
||
|
|
||
![]()
7 years ago
|
#include "webcc/common.h"
|
||
|
#include "webcc/http_request.h"
|
||
|
#include "webcc/http_request_parser.h"
|
||
|
#include "webcc/http_response.h"
|
||
![]()
8 years ago
|
|
||
![]()
7 years ago
|
namespace webcc {
|
||
![]()
8 years ago
|
|
||
|
class HttpRequestHandler;
|
||
|
|
||
![]()
7 years ago
|
class HttpSession : public std::enable_shared_from_this<HttpSession> {
|
||
![]()
8 years ago
|
public:
|
||
![]()
8 years ago
|
friend class HttpRequestHandler;
|
||
|
|
||
![]()
7 years ago
|
HttpSession(const HttpSession&) = delete;
|
||
|
HttpSession& operator=(const HttpSession&) = delete;
|
||
|
|
||
|
HttpSession(boost::asio::ip::tcp::socket socket,
|
||
|
HttpRequestHandler* handler);
|
||
|
|
||
|
const HttpRequest& request() const {
|
||
|
return request_;
|
||
|
}
|
||
![]()
8 years ago
|
|
||
|
void Start();
|
||
|
|
||
|
void Stop();
|
||
|
|
||
![]()
7 years ago
|
void SetResponseStatus(int status) {
|
||
|
response_.set_status(status);
|
||
|
}
|
||
|
|
||
|
void SetResponseContent(const std::string& content_type,
|
||
|
std::size_t content_length,
|
||
|
std::string&& content);
|
||
|
|
||
|
// Write response back to the client.
|
||
|
void SendResponse();
|
||
|
|
||
![]()
8 years ago
|
private:
|
||
|
void DoRead();
|
||
|
|
||
|
void DoWrite();
|
||
|
|
||
|
void HandleRead(boost::system::error_code ec,
|
||
![]()
8 years ago
|
std::size_t length);
|
||
![]()
8 years ago
|
|
||
|
void HandleWrite(boost::system::error_code ec,
|
||
![]()
8 years ago
|
std::size_t length);
|
||
![]()
8 years ago
|
|
||
|
private:
|
||
|
// Socket for the connection.
|
||
|
boost::asio::ip::tcp::socket socket_;
|
||
|
|
||
|
// The handler used to process the incoming request.
|
||
![]()
7 years ago
|
HttpRequestHandler* request_handler_;
|
||
![]()
8 years ago
|
|
||
|
// Buffer for incoming data.
|
||
![]()
7 years ago
|
std::array<char, kBufferSize> buffer_;
|
||
![]()
8 years ago
|
|
||
|
// The incoming request.
|
||
|
HttpRequest request_;
|
||
|
|
||
|
// The parser for the incoming request.
|
||
|
HttpRequestParser request_parser_;
|
||
|
|
||
![]()
8 years ago
|
// The response to be sent back to the client.
|
||
![]()
8 years ago
|
HttpResponse response_;
|
||
|
};
|
||
|
|
||
![]()
7 years ago
|
typedef std::shared_ptr<HttpSession> HttpSessionPtr;
|
||
![]()
8 years ago
|
|
||
![]()
7 years ago
|
} // namespace webcc
|
||
![]()
8 years ago
|
|
||
![]()
7 years ago
|
#endif // WEBCC_HTTP_SESSION_H_
|