|
| 1 | +// Copyright 2009 (c) Dean Michael Berris <mikhailberis@gmail.com> |
| 2 | +// Copyright 2009 (c) Tarroo, Inc. |
| 3 | +// Adapted from Christopher Kholhoff's Boost.Asio Example, released under |
| 4 | +// the Boost Software License, Version 1.0. (See acccompanying file LICENSE_1_0.txt |
| 5 | +// or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | +// |
| 7 | +// ===================================================================================== |
| 8 | +// |
| 9 | +// Filename: connection.hpp |
| 10 | +// |
| 11 | +// Description: Connection handler for the HTTP requests. |
| 12 | +// |
| 13 | +// Version: 1.1 |
| 14 | +// Created: Sunday, 15 November, 2009 07:46:40 PHT |
| 15 | +// |
| 16 | +// Author: Dean Michael Berris (dmb), mikhailberis@gmail.com |
| 17 | +// |
| 18 | +// ===================================================================================== |
| 19 | +// |
| 20 | + |
| 21 | +#ifndef BOOST_NETWORK_HTTP_CONNECTION_HPP_ |
| 22 | +#defineBOOST_NETWORK_HTTP_CONNECTION_HPP_ |
| 23 | + |
| 24 | +#include<boost/enable_shared_from_this.hpp> |
| 25 | +#include<boost/network/protocol/http/request_parser.hpp> |
| 26 | +#include<boost/network/protocol/http/request.hpp> |
| 27 | +#include<boost/network/protocol/http/header.hpp> |
| 28 | +#include<boost/network/protocol/http/reply.hpp> |
| 29 | +#include<boost/asio.hpp> |
| 30 | +#include<boost/array.hpp> |
| 31 | +#include<boost/lexical_cast.hpp> |
| 32 | +#include<boost/algorithm/string/case_conv.hpp> |
| 33 | +#include<boost/bind.hpp> |
| 34 | + |
| 35 | +namespaceboost {namespacenetwork {namespacehttp { |
| 36 | + |
| 37 | +using boost::asio::io_service; |
| 38 | +namespacesystem= boost::system; |
| 39 | +using boost::asio::ip::tcp; |
| 40 | +using boost::array; |
| 41 | +using boost::tribool; |
| 42 | +using boost::tuple; |
| 43 | +namespacetuples= boost::tuples; |
| 44 | +using boost::tuples::tie; |
| 45 | +using boost::bind; |
| 46 | +using boost::to_lower_copy; |
| 47 | + |
| 48 | +template<classHandler> |
| 49 | +structconnection : boost::enable_shared_from_this<connection<Handler> > { |
| 50 | + |
| 51 | +connection(io_service & service, Handler & handler) |
| 52 | + : service_(service) |
| 53 | + , handler_(handler) |
| 54 | + , socket_(service_) |
| 55 | + , wrapper_(service_) |
| 56 | + { |
| 57 | +try { |
| 58 | + socket_.set_option(tcp::no_delay(true));// Don't delay writing |
| 59 | + }catch (system::system_error & e) { |
| 60 | + handler_.log(e.what()); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + tcp::socket &socket() { |
| 65 | +return socket_; |
| 66 | + } |
| 67 | + |
| 68 | +voidstart() { |
| 69 | +// This is HTTP so we really want to just |
| 70 | +// read and parse a request that's incoming |
| 71 | +// and then pass that request object to the |
| 72 | +// handler_ instance. |
| 73 | +// |
| 74 | + socket_.async_read_some( |
| 75 | +boost::asio::buffer(buffer_), |
| 76 | + wrapper_.wrap( |
| 77 | +bind( |
| 78 | + &connection<Handler>::handle_read_headers, |
| 79 | + connection<Handler>::shared_from_this(), |
| 80 | + boost::asio::placeholders::error, |
| 81 | + boost::asio::placeholders::bytes_transferred |
| 82 | + ) |
| 83 | + ) |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | +private: |
| 88 | + |
| 89 | +structis_content_length { |
| 90 | +template<classHeader> |
| 91 | +booloperator()(Headerconst & header) { |
| 92 | +returnto_lower_copy(header.name) =="content-length"; |
| 93 | + } |
| 94 | + }; |
| 95 | + |
| 96 | +voidhandle_read_headers(system::error_codeconst &ec,size_t bytes_transferred) { |
| 97 | +if (!ec) { |
| 98 | + tribool done; |
| 99 | +tie(done,tuples::ignore) = parser_.parse_headers(request_, buffer_.data(), buffer_.data() + bytes_transferred); |
| 100 | +if (done) { |
| 101 | +if (request_.method[0] =='P') { |
| 102 | +// look for the content-length header |
| 103 | + std::vector<request_header>::iterator it = |
| 104 | +find_if( |
| 105 | + request_.headers.begin(), |
| 106 | + request_.headers.end(), |
| 107 | +is_content_length() |
| 108 | + ); |
| 109 | +if (it == request_.headers.end()) { |
| 110 | + reply_=reply::stock_reply(reply::bad_request); |
| 111 | +boost::asio::async_write( |
| 112 | + socket_, |
| 113 | + reply_.to_buffers(), |
| 114 | + wrapper_.wrap( |
| 115 | +bind( |
| 116 | + &connection<Handler>::handle_write, |
| 117 | + connection<Handler>::shared_from_this(), |
| 118 | + boost::asio::placeholders::error |
| 119 | + ) |
| 120 | + ) |
| 121 | + ); |
| 122 | +return; |
| 123 | + } |
| 124 | + |
| 125 | +size_t content_length =0; |
| 126 | + |
| 127 | +try { |
| 128 | + content_length = boost::lexical_cast<size_t>(it->value); |
| 129 | + }catch (...) { |
| 130 | + reply_=reply::stock_reply(reply::bad_request); |
| 131 | +boost::asio::async_write( |
| 132 | + socket_, |
| 133 | + reply_.to_buffers(), |
| 134 | + wrapper_.wrap( |
| 135 | +bind( |
| 136 | + &connection<Handler>::handle_write, |
| 137 | + connection<Handler>::shared_from_this(), |
| 138 | + boost::asio::placeholders::error |
| 139 | + ) |
| 140 | + ) |
| 141 | + ); |
| 142 | +return; |
| 143 | + } |
| 144 | + |
| 145 | +if (content_length !=0) { |
| 146 | +async_read( |
| 147 | + socket_, |
| 148 | +boost::asio::buffer(buffer_), |
| 149 | +boost::asio::transfer_at_least(content_length), |
| 150 | + wrapper_.wrap( |
| 151 | +bind( |
| 152 | + &connection<Handler>::handle_read_body_contents, |
| 153 | + connection<Handler>::shared_from_this(), |
| 154 | + boost::asio::placeholders::error, |
| 155 | + content_length, |
| 156 | + boost::asio::placeholders::bytes_transferred |
| 157 | + ) |
| 158 | + ) |
| 159 | + ); |
| 160 | +return; |
| 161 | + } |
| 162 | + |
| 163 | +handler_(request_, reply_); |
| 164 | +boost::asio::async_write( |
| 165 | + socket_, |
| 166 | + reply_.to_buffers(), |
| 167 | + wrapper_.wrap( |
| 168 | +bind( |
| 169 | + &connection<Handler>::handle_write, |
| 170 | + connection<Handler>::shared_from_this(), |
| 171 | + boost::asio::placeholders::error |
| 172 | + ) |
| 173 | + ) |
| 174 | + ); |
| 175 | + }else { |
| 176 | +handler_(request_, reply_); |
| 177 | +boost::asio::async_write( |
| 178 | + socket_, |
| 179 | + reply_.to_buffers(), |
| 180 | + wrapper_.wrap( |
| 181 | +bind( |
| 182 | + &connection<Handler>::handle_write, |
| 183 | + connection<Handler>::shared_from_this(), |
| 184 | + boost::asio::placeholders::error |
| 185 | + ) |
| 186 | + ) |
| 187 | + ); |
| 188 | + } |
| 189 | + }elseif (!done) { |
| 190 | + reply_=reply::stock_reply(reply::bad_request); |
| 191 | +boost::asio::async_write( |
| 192 | + socket_, |
| 193 | + reply_.to_buffers(), |
| 194 | + wrapper_.wrap( |
| 195 | +bind( |
| 196 | + &connection<Handler>::handle_write, |
| 197 | + connection<Handler>::shared_from_this(), |
| 198 | + boost::asio::placeholders::error |
| 199 | + ) |
| 200 | + ) |
| 201 | + ); |
| 202 | + }else { |
| 203 | + socket_.async_read_some( |
| 204 | +boost::asio::buffer(buffer_), |
| 205 | + wrapper_.wrap( |
| 206 | +bind( |
| 207 | + &connection<Handler>::handle_read_headers, |
| 208 | + connection<Handler>::shared_from_this(), |
| 209 | + boost::asio::placeholders::error, |
| 210 | + boost::asio::placeholders::bytes_transferred |
| 211 | + ) |
| 212 | + ) |
| 213 | + ); |
| 214 | + } |
| 215 | + } |
| 216 | +// TODO Log the error? |
| 217 | + } |
| 218 | + |
| 219 | +voidhandle_read_body_contents(boost::system::error_codeconst & ec,size_t bytes_to_read,size_t bytes_transferred) { |
| 220 | +if (!ec) { |
| 221 | +size_t difference = bytes_to_read - bytes_transferred; |
| 222 | + request_.body.append(buffer_.begin(), buffer_.end()); |
| 223 | +if (difference ==0) { |
| 224 | +handler_(request_, reply_); |
| 225 | +boost::asio::async_write( |
| 226 | + socket_, |
| 227 | + reply_.to_buffers(), |
| 228 | + wrapper_.wrap( |
| 229 | +bind( |
| 230 | + &connection<Handler>::handle_write, |
| 231 | + connection<Handler>::shared_from_this(), |
| 232 | + boost::asio::placeholders::error |
| 233 | + ) |
| 234 | + ) |
| 235 | + ); |
| 236 | + }else { |
| 237 | + socket_.async_read_some( |
| 238 | +boost::asio::buffer(buffer_), |
| 239 | + wrapper_.wrap( |
| 240 | +bind( |
| 241 | + &connection<Handler>::handle_read_body_contents, |
| 242 | + connection<Handler>::shared_from_this(), |
| 243 | + boost::asio::placeholders::error, |
| 244 | + difference, |
| 245 | + boost::asio::placeholders::bytes_transferred |
| 246 | + ) |
| 247 | + ) |
| 248 | + ); |
| 249 | + } |
| 250 | + } |
| 251 | +// TODO Log the error? |
| 252 | + } |
| 253 | + |
| 254 | +voidhandle_write(boost::system::error_codeconst & ec) { |
| 255 | +if (!ec) { |
| 256 | + boost::system::error_code ignored_ec; |
| 257 | + socket_.shutdown(tcp::socket::shutdown_both, ignored_ec); |
| 258 | + } |
| 259 | + } |
| 260 | + |
| 261 | + io_service & service_; |
| 262 | + Handler & handler_; |
| 263 | + tcp::socket socket_; |
| 264 | + io_service::strand wrapper_; |
| 265 | + array<char,4096> buffer_; |
| 266 | + request_parser parser_; |
| 267 | + request_pod request_; |
| 268 | + reply reply_; |
| 269 | + }; |
| 270 | + |
| 271 | + |
| 272 | +}// namespace http |
| 273 | + |
| 274 | +}// namespace network |
| 275 | + |
| 276 | +}// namespace boost |
| 277 | + |
| 278 | +#endif// BOOST_NETWORK_HTTP_CONNECTION_HPP_ |
| 279 | + |