Go to the documentation of this file.00001 #ifndef CPP_WEB_SERVER_HTTP_CONNECTION_HPP
00002 #define CPP_WEB_SERVER_HTTP_CONNECTION_HPP
00003
00004 #include <boost/asio.hpp>
00005 #include <boost/noncopyable.hpp>
00006 #include <boost/shared_ptr.hpp>
00007 #include <boost/enable_shared_from_this.hpp>
00008 #include <boost/thread/mutex.hpp>
00009 #include "async_web_server_cpp/http_request_handler.hpp"
00010 #include "async_web_server_cpp/http_request.hpp"
00011 #include "async_web_server_cpp/http_request_parser.hpp"
00012
00013 namespace async_web_server_cpp
00014 {
00015
00016 class HttpConnection;
00017 typedef boost::shared_ptr<HttpConnection> HttpConnectionPtr;
00018 typedef boost::weak_ptr<HttpConnection> HttpConnectionWeakPtr;
00019
00033 class HttpConnection : public boost::enable_shared_from_this<HttpConnection>,
00034 private boost::noncopyable
00035 {
00036 public:
00037 typedef boost::function<void(const char* begin, const char* end)> ReadHandler;
00038 typedef boost::shared_ptr<const void> ResourcePtr;
00039
00040 explicit HttpConnection(boost::asio::io_service &io_service,
00041 HttpServerRequestHandler request_handler);
00042
00043 boost::asio::ip::tcp::socket &socket();
00044
00048 void start();
00049
00053 void async_read(ReadHandler callback);
00054
00058 void write_and_clear(std::vector<unsigned char> &data);
00059
00060 void write(const std::string &);
00061
00062 void write(const boost::asio::const_buffer &buffer,
00063 ResourcePtr resource);
00064
00065 void write(const std::vector<boost::asio::const_buffer> &buffer,
00066 ResourcePtr resource);
00067
00068 private:
00069 void handle_read(const char* begin, const char* end);
00070 void handle_read_raw(ReadHandler callback,
00071 const boost::system::error_code &e,
00072 std::size_t bytes_transferred);
00073
00074
00075 void write_pending();
00076
00077 void handle_write(const boost::system::error_code &e,
00078 std::vector<ResourcePtr> resources);
00079
00080 boost::asio::io_service::strand strand_;
00081 boost::asio::ip::tcp::socket socket_;
00082 HttpServerRequestHandler request_handler_;
00083 boost::array<char, 8192> buffer_;
00084 HttpRequest request_;
00085 HttpRequestParser request_parser_;
00086
00087 boost::mutex write_mutex_;
00088 bool write_in_progress_;
00089 std::vector<boost::asio::const_buffer> pending_write_buffers_;
00090 std::vector<ResourcePtr> pending_write_resources_;
00091 boost::system::error_code last_error_;
00092 ReadHandler read_handler_;
00093 };
00094
00095 }
00096
00097 #endif