connection.cc 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // connection.cpp
  3. // ~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #include <vector>
  11. #include <boost/bind.hpp>
  12. #include "Experimental/http_server/connection.h"
  13. #include "Experimental/http_server/connection_manager.h"
  14. #include "Experimental/http_server/request_handler.h"
  15. namespace http_server {
  16. connection::connection(boost::asio::io_service& io_service,
  17. connection_manager& manager, request_handler& handler) :
  18. socket_(io_service), connection_manager_(manager),
  19. request_handler_(handler)
  20. {
  21. }
  22. boost::asio::ip::tcp::socket& connection::socket()
  23. {
  24. return socket_;
  25. }
  26. void connection::start()
  27. {
  28. socket_.async_read_some(boost::asio::buffer(buffer_), boost::bind(
  29. &connection::handle_read, shared_from_this(),
  30. boost::asio::placeholders::error,
  31. boost::asio::placeholders::bytes_transferred));
  32. }
  33. void connection::stop()
  34. {
  35. socket_.close();
  36. }
  37. void connection::handle_read(const boost::system::error_code& e,
  38. std::size_t bytes_transferred)
  39. {
  40. if (!e)
  41. {
  42. boost::tribool result;
  43. boost::tie(result, boost::tuples::ignore) = request_parser_.parse(
  44. request_, buffer_.data(), buffer_.data() + bytes_transferred);
  45. if (result)
  46. {
  47. request_handler_.handle_request(request_, reply_);
  48. boost::asio::async_write(socket_, reply_.to_buffers(), boost::bind(
  49. &connection::handle_write, shared_from_this(),
  50. boost::asio::placeholders::error));
  51. }
  52. else if (!result)
  53. {
  54. reply_ = reply::stock_reply(reply::bad_request);
  55. boost::asio::async_write(socket_, reply_.to_buffers(), boost::bind(
  56. &connection::handle_write, shared_from_this(),
  57. boost::asio::placeholders::error));
  58. }
  59. else
  60. {
  61. socket_.async_read_some(boost::asio::buffer(buffer_), boost::bind(
  62. &connection::handle_read, shared_from_this(),
  63. boost::asio::placeholders::error,
  64. boost::asio::placeholders::bytes_transferred));
  65. }
  66. }
  67. else if (e != boost::asio::error::operation_aborted)
  68. {
  69. connection_manager_.stop(shared_from_this());
  70. }
  71. }
  72. void connection::handle_write(const boost::system::error_code& e)
  73. {
  74. if (!e)
  75. {
  76. // Initiate graceful connection closure.
  77. boost::system::error_code ignored_ec;
  78. socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both,
  79. ignored_ec);
  80. }
  81. if (e != boost::asio::error::operation_aborted)
  82. {
  83. connection_manager_.stop(shared_from_this());
  84. }
  85. }
  86. } // namespace http_server