22 #include <boost/lexical_cast.hpp> 31 HttpRequest::HttpRequest ( boost::asio::io_service &ioService,
const shared_ptr<HttpServer>& server,
const shared_ptr<RequestManager> &manager )
32 : socket ( ioService )
53 boost::asio::placeholders::error,
54 boost::asio::placeholders::bytes_transferred
60 shared_ptr<RequestManager>
manager = this->manager.lock();
61 shared_ptr<HttpServer>
server = this->server.lock();
62 assert(manager && server);
65 boost::tribool result;
66 boost::tie ( result, boost::tuples::ignore ) =
parser.
parse ( shared_from_this(),
buffer.data(),
buffer.data() + bytes_transferred );
69 manager->enqueue ( shared_from_this() );
70 }
else if ( !result ) {
76 }
else if ( e != boost::asio::error::operation_aborted ) {
77 server->stopRequest ( shared_from_this() );
84 shared_ptr<HttpServer>
server = this->server.lock();
89 boost::system::error_code ignored_ec;
90 socket.shutdown ( boost::asio::ip::tcp::socket::shutdown_both, ignored_ec );
93 if ( e != boost::asio::error::operation_aborted ) {
94 server->stopRequest ( shared_from_this() );
111 LOG_SEV(server_log, error) <<
"Tried to answer an already answered HttpRequest: " <<
getURL();
121 boost::asio::placeholders::error ) );
126 shared_ptr<RequestManager>
manager = this->manager.lock();
129 answer(manager->getCache()->getDefaultTile(), status);
140 reply.
headers[0].value = boost::lexical_cast<
string> ( tile->getImage()->size() );
143 reply.
headers[1].value.append ( tile->getIdentifier()->getImageFormatString() );
147 auto now = boost::posix_time::second_clock::local_time();
150 << now.date().day() <<
"/" << now.date().month() <<
"/" << now.date().year()
151 <<
":" << now.time_of_day().hours() <<
":" << now.time_of_day().minutes() <<
":" << now.time_of_day().seconds()
164 "HTTP/1.0 200 OK\r\n";
166 "HTTP/1.0 201 Created\r\n";
168 "HTTP/1.0 202 Accepted\r\n";
170 "HTTP/1.0 204 No Content\r\n";
172 "HTTP/1.0 300 Multiple Choices\r\n";
174 "HTTP/1.0 301 Moved Permanently\r\n";
176 "HTTP/1.0 302 Moved Temporarily\r\n";
178 "HTTP/1.0 304 Not Modified\r\n";
180 "HTTP/1.0 400 Bad Request\r\n";
182 "HTTP/1.0 401 Unauthorized\r\n";
184 "HTTP/1.0 403 Forbidden\r\n";
186 "HTTP/1.0 404 Not Found\r\n";
188 "HTTP/1.0 500 Internal Server Error\r\n";
190 "HTTP/1.0 501 Not Implemented\r\n";
192 "HTTP/1.0 502 Bad Gateway\r\n";
194 "HTTP/1.0 503 Service Unavailable\r\n";
200 return boost::asio::buffer ( ok );
202 return boost::asio::buffer ( created );
204 return boost::asio::buffer ( accepted );
206 return boost::asio::buffer ( no_content );
208 return boost::asio::buffer ( multiple_choices );
210 return boost::asio::buffer ( moved_permanently );
212 return boost::asio::buffer ( moved_temporarily );
214 return boost::asio::buffer ( not_modified );
216 return boost::asio::buffer ( bad_request );
218 return boost::asio::buffer ( unauthorized );
220 return boost::asio::buffer ( forbidden );
222 return boost::asio::buffer ( not_found );
224 return boost::asio::buffer ( internal_server_error );
226 return boost::asio::buffer ( not_implemented );
228 return boost::asio::buffer ( bad_gateway );
230 return boost::asio::buffer ( service_unavailable );
232 return boost::asio::buffer ( internal_server_error );
240 static const char name_value_separator[] = {
':',
' ' };
241 static const char crlf[] = {
'\r',
'\n' };
242 std::vector<boost::asio::const_buffer> buffers;
245 for ( std::size_t i = 0; i < headers.size(); ++i ) {
247 buffers.push_back ( boost::asio::buffer ( h.
name ) );
248 buffers.push_back ( boost::asio::buffer ( name_value_separator ) );
249 buffers.push_back ( boost::asio::buffer ( h.
value ) );
250 buffers.push_back ( boost::asio::buffer ( crlf ) );
253 buffers.push_back ( boost::asio::buffer ( crlf ) );
254 buffers.push_back ( boost::asio::buffer ( content ) );
256 buffers.push_back ( boost::asio::const_buffer ( (
void * ) tile->getImage()->data(), tile->getImage()->size() ) );
weak_ptr< HttpServer > server
Reply reply
The reply to be sent back to the client.
const std::string forbidden
void handleRead(const boost::system::error_code &e, std::size_t bytes_transferred)
Handle completion of a read operation.
const std::string created
HttpRequest(boost::asio::io_service &ioService, const shared_ptr< HttpServer > &server, const shared_ptr< RequestManager > &manager)
This file is part of alaCarte.
std::array< char, 8192 > buffer
Buffer for incoming data.
const std::string accepted
std::vector< Header > headers
The headers to be included in the Reply.
TESTABLE const string & getURL() const
#define LOG_SEV(log, lvl)
boost::asio::ip::tcp::socket socket
Socket for the connection.
static const char * access_log
Filepath to the access log (type: string)
const std::string not_found
void handleWrite(const boost::system::error_code &e)
Handle completion of a write operation.
HttpRequestParser parser
The parser for the incoming request.
const std::string bad_gateway
std::string content
The content to be sent in the Reply.
const std::string not_implemented
const std::string not_modified
const std::string bad_request
void startCollectingData()
Start the first asynchronous operation for the connection.
void close()
Stop all asynchronous operations associated with the connection.
StatusType status
The Status of the Reply.
const std::string unauthorized
boost::asio::const_buffer to_buffer(HttpRequest::Reply::StatusType status)
weak_ptr< RequestManager > manager
const std::string internal_server_error
shared_ptr< Tile > tile
The Tile to be sent in the Reply.
boost::asio::ip::tcp::socket & getSocket()
const std::string moved_temporarily
const std::string multiple_choices
const std::string no_content
std::vector< boost::asio::const_buffer > toBuffers()
Convert the Reply into a vector of buffers.
const std::string moved_permanently
boost::tuple< boost::tribool, InputIterator > parse(shared_ptr< HttpRequest > req, InputIterator begin, InputIterator end)
Parse some data.
const std::string service_unavailable