00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <boost/lexical_cast.hpp>
00013 #include <fstream>
00014 #include "async_web_server_cpp/http_reply.hpp"
00015
00016 namespace async_web_server_cpp
00017 {
00018
00019 namespace status_strings
00020 {
00021
00022 const std::string switching_protocols =
00023 "HTTP/1.0 101 Switching Protocols\r\n";
00024 const std::string ok =
00025 "HTTP/1.0 200 OK\r\n";
00026 const std::string created =
00027 "HTTP/1.0 201 Created\r\n";
00028 const std::string accepted =
00029 "HTTP/1.0 202 Accepted\r\n";
00030 const std::string no_content =
00031 "HTTP/1.0 204 No Content\r\n";
00032 const std::string multiple_choices =
00033 "HTTP/1.0 300 Multiple Choices\r\n";
00034 const std::string moved_permanently =
00035 "HTTP/1.0 301 Moved Permanently\r\n";
00036 const std::string moved_temporarily =
00037 "HTTP/1.0 302 Moved Temporarily\r\n";
00038 const std::string not_modified =
00039 "HTTP/1.0 304 Not Modified\r\n";
00040 const std::string bad_request =
00041 "HTTP/1.0 400 Bad Request\r\n";
00042 const std::string unauthorized =
00043 "HTTP/1.0 401 Unauthorized\r\n";
00044 const std::string forbidden =
00045 "HTTP/1.0 403 Forbidden\r\n";
00046 const std::string not_found =
00047 "HTTP/1.0 404 Not Found\r\n";
00048 const std::string internal_server_error =
00049 "HTTP/1.0 500 Internal Server Error\r\n";
00050 const std::string not_implemented =
00051 "HTTP/1.0 501 Not Implemented\r\n";
00052 const std::string bad_gateway =
00053 "HTTP/1.0 502 Bad Gateway\r\n";
00054 const std::string service_unavailable =
00055 "HTTP/1.0 503 Service Unavailable\r\n";
00056
00057 boost::asio::const_buffer to_buffer(HttpReply::status_type status)
00058 {
00059 switch (status)
00060 {
00061 case HttpReply::switching_protocols:
00062 return boost::asio::buffer(switching_protocols);
00063 case HttpReply::ok:
00064 return boost::asio::buffer(ok);
00065 case HttpReply::created:
00066 return boost::asio::buffer(created);
00067 case HttpReply::accepted:
00068 return boost::asio::buffer(accepted);
00069 case HttpReply::no_content:
00070 return boost::asio::buffer(no_content);
00071 case HttpReply::multiple_choices:
00072 return boost::asio::buffer(multiple_choices);
00073 case HttpReply::moved_permanently:
00074 return boost::asio::buffer(moved_permanently);
00075 case HttpReply::moved_temporarily:
00076 return boost::asio::buffer(moved_temporarily);
00077 case HttpReply::not_modified:
00078 return boost::asio::buffer(not_modified);
00079 case HttpReply::bad_request:
00080 return boost::asio::buffer(bad_request);
00081 case HttpReply::unauthorized:
00082 return boost::asio::buffer(unauthorized);
00083 case HttpReply::forbidden:
00084 return boost::asio::buffer(forbidden);
00085 case HttpReply::not_found:
00086 return boost::asio::buffer(not_found);
00087 case HttpReply::internal_server_error:
00088 return boost::asio::buffer(internal_server_error);
00089 case HttpReply::not_implemented:
00090 return boost::asio::buffer(not_implemented);
00091 case HttpReply::bad_gateway:
00092 return boost::asio::buffer(bad_gateway);
00093 case HttpReply::service_unavailable:
00094 return boost::asio::buffer(service_unavailable);
00095 default:
00096 return boost::asio::buffer(internal_server_error);
00097 }
00098 }
00099
00100 }
00101
00102 namespace misc_strings
00103 {
00104
00105 const char name_value_separator[] = {':', ' '};
00106 const char crlf[] = {'\r', '\n'};
00107
00108 }
00109
00110 namespace stock_replies
00111 {
00112
00113 const char ok[] = "";
00114 const char created[] =
00115 "<html>"
00116 "<head><title>Created</title></head>"
00117 "<body><h1>201 Created</h1></body>"
00118 "</html>";
00119 const char accepted[] =
00120 "<html>"
00121 "<head><title>Accepted</title></head>"
00122 "<body><h1>202 Accepted</h1></body>"
00123 "</html>";
00124 const char no_content[] =
00125 "<html>"
00126 "<head><title>No Content</title></head>"
00127 "<body><h1>204 Content</h1></body>"
00128 "</html>";
00129 const char multiple_choices[] =
00130 "<html>"
00131 "<head><title>Multiple Choices</title></head>"
00132 "<body><h1>300 Multiple Choices</h1></body>"
00133 "</html>";
00134 const char moved_permanently[] =
00135 "<html>"
00136 "<head><title>Moved Permanently</title></head>"
00137 "<body><h1>301 Moved Permanently</h1></body>"
00138 "</html>";
00139 const char moved_temporarily[] =
00140 "<html>"
00141 "<head><title>Moved Temporarily</title></head>"
00142 "<body><h1>302 Moved Temporarily</h1></body>"
00143 "</html>";
00144 const char not_modified[] =
00145 "<html>"
00146 "<head><title>Not Modified</title></head>"
00147 "<body><h1>304 Not Modified</h1></body>"
00148 "</html>";
00149 const char bad_request[] =
00150 "<html>"
00151 "<head><title>Bad Request</title></head>"
00152 "<body><h1>400 Bad Request</h1></body>"
00153 "</html>";
00154 const char unauthorized[] =
00155 "<html>"
00156 "<head><title>Unauthorized</title></head>"
00157 "<body><h1>401 Unauthorized</h1></body>"
00158 "</html>";
00159 const char forbidden[] =
00160 "<html>"
00161 "<head><title>Forbidden</title></head>"
00162 "<body><h1>403 Forbidden</h1></body>"
00163 "</html>";
00164 const char not_found[] =
00165 "<html>"
00166 "<head><title>Not Found</title></head>"
00167 "<body><h1>404 Not Found</h1></body>"
00168 "</html>";
00169 const char internal_server_error[] =
00170 "<html>"
00171 "<head><title>Internal Server Error</title></head>"
00172 "<body><h1>500 Internal Server Error</h1></body>"
00173 "</html>";
00174 const char not_implemented[] =
00175 "<html>"
00176 "<head><title>Not Implemented</title></head>"
00177 "<body><h1>501 Not Implemented</h1></body>"
00178 "</html>";
00179 const char bad_gateway[] =
00180 "<html>"
00181 "<head><title>Bad Gateway</title></head>"
00182 "<body><h1>502 Bad Gateway</h1></body>"
00183 "</html>";
00184 const char service_unavailable[] =
00185 "<html>"
00186 "<head><title>Service Unavailable</title></head>"
00187 "<body><h1>503 Service Unavailable</h1></body>"
00188 "</html>";
00189
00190 std::string to_string(HttpReply::status_type status)
00191 {
00192 switch (status)
00193 {
00194 case HttpReply::ok:
00195 return ok;
00196 case HttpReply::created:
00197 return created;
00198 case HttpReply::accepted:
00199 return accepted;
00200 case HttpReply::no_content:
00201 return no_content;
00202 case HttpReply::multiple_choices:
00203 return multiple_choices;
00204 case HttpReply::moved_permanently:
00205 return moved_permanently;
00206 case HttpReply::moved_temporarily:
00207 return moved_temporarily;
00208 case HttpReply::not_modified:
00209 return not_modified;
00210 case HttpReply::bad_request:
00211 return bad_request;
00212 case HttpReply::unauthorized:
00213 return unauthorized;
00214 case HttpReply::forbidden:
00215 return forbidden;
00216 case HttpReply::not_found:
00217 return not_found;
00218 case HttpReply::internal_server_error:
00219 return internal_server_error;
00220 case HttpReply::not_implemented:
00221 return not_implemented;
00222 case HttpReply::bad_gateway:
00223 return bad_gateway;
00224 case HttpReply::service_unavailable:
00225 return service_unavailable;
00226 default:
00227 return internal_server_error;
00228 }
00229 }
00230
00231 }
00232
00233 std::vector<boost::asio::const_buffer> HttpReply::to_buffers(const std::vector<HttpHeader> &headers)
00234 {
00235 std::vector<boost::asio::const_buffer> buffers;
00236 for (std::size_t i = 0; i < headers.size(); ++i)
00237 {
00238 const HttpHeader &h = headers[i];
00239 buffers.push_back(boost::asio::buffer(h.name));
00240 buffers.push_back(boost::asio::buffer(misc_strings::name_value_separator));
00241 buffers.push_back(boost::asio::buffer(h.value));
00242 buffers.push_back(boost::asio::buffer(misc_strings::crlf));
00243 }
00244 buffers.push_back(boost::asio::buffer(misc_strings::crlf));
00245 return buffers;
00246 }
00247
00248
00249 HttpServerRequestHandler HttpReply::from_file(HttpReply::status_type status,
00250 const std::string& content_type,
00251 const std::string& filename,
00252 const std::vector<HttpHeader>& additional_headers)
00253 {
00254 std::vector<HttpHeader> headers;
00255 headers.push_back(HttpHeader("Content-Type", content_type));
00256 std::copy(additional_headers.begin(), additional_headers.end(), headers.begin());
00257
00258 return FileHttpRequestHandler(status, filename, headers);
00259 }
00260 FileHttpRequestHandler::FileHttpRequestHandler(HttpReply::status_type status,
00261 const std::string& filename,
00262 const std::vector<HttpHeader>& headers)
00263 : status_(status), headers_(headers), filename_(filename)
00264 {
00265 }
00266
00267 static bool serveFromFile(HttpReply::status_type status, const std::string& filename, const std::vector<HttpHeader>& headers, boost::shared_ptr<HttpConnection> connection) {
00268 std::ifstream file_stream(filename.c_str());
00269 std::stringstream file_buffer;
00270 file_buffer << file_stream.rdbuf();
00271 std::string content = file_buffer.str();
00272
00273 ReplyBuilder reply_builder_(status);
00274 reply_builder_.headers(headers);
00275 reply_builder_.header("Content-Length", boost::lexical_cast<std::string>(content.size()));
00276 reply_builder_.write(connection);
00277 connection->write(content);
00278 }
00279 bool FileHttpRequestHandler::operator()(const HttpRequest &request, boost::shared_ptr<HttpConnection> connection, const char* begin, const char* end)
00280 {
00281 serveFromFile(status_, filename_, headers_, connection);
00282 return true;
00283 }
00284
00285
00286 HttpServerRequestHandler HttpReply::from_filesystem(HttpReply::status_type status,
00287 const std::string& path_root,
00288 const std::string& filesystem_root,
00289 bool list_directories,
00290 const std::vector<HttpHeader>& additional_headers)
00291 {
00292 return FilesystemHttpRequestHandler(status, path_root, filesystem_root, list_directories, additional_headers);
00293 }
00294 FilesystemHttpRequestHandler::FilesystemHttpRequestHandler(HttpReply::status_type status,
00295 const std::string& path_root,
00296 const std::string& filesystem_root,
00297 bool list_directories,
00298 const std::vector<HttpHeader>& headers)
00299 : status_(status), headers_(headers), path_root_(path_root), filesystem_root_(filesystem_root), list_directories_(list_directories)
00300 {
00301 }
00302
00303 bool FilesystemHttpRequestHandler::operator()(const HttpRequest &request, boost::shared_ptr<HttpConnection> connection, const char* begin, const char* end)
00304 {
00305 if(request.path.find(path_root_) == 0) {
00306 std::string rel_path = request.path.substr(path_root_.length());
00307 if(rel_path.find_first_of('/') == 0) {
00308 rel_path = rel_path.substr(1);
00309 }
00310
00311 boost::filesystem::path requested_path = filesystem_root_ / rel_path;
00312
00313 if(boost::filesystem::exists(requested_path)) {
00314 if(boost::filesystem::is_directory(requested_path)) {
00315 if(list_directories_) {
00316 std::stringstream content;
00317 content << "<html><body>";
00318 content << "<h1> Directory Listing: " << request.path << "</h1>";
00319 boost::filesystem::directory_iterator end_itr;
00320 for (boost::filesystem::directory_iterator itr(requested_path); itr != end_itr; ++itr) {
00321 if(boost::filesystem::is_directory(itr->status())) {
00322 content << "<a href=\"" << itr->path().leaf().generic_string() << "/\">";
00323 content << itr->path().leaf().generic_string() << "/";
00324 content << "</a>";
00325 }
00326 else if(boost::filesystem::is_regular_file(itr->status())) {
00327 content << "<a href=\"" << itr->path().leaf().generic_string() << "\">";
00328 content << itr->path().leaf().generic_string();
00329 content << "</a>";
00330 }
00331 content << "<br>";
00332 }
00333 content << "</body></html>";
00334 HttpReply::static_reply(HttpReply::ok, "text/html", content.str(), headers_)(request, connection, begin, end);
00335 }
00336 else {
00337 HttpReply::stock_reply(HttpReply::forbidden)(request, connection, begin, end);
00338 }
00339 return true;
00340 }
00341 else if(boost::filesystem::is_regular_file(requested_path)) {
00342 serveFromFile(status_, requested_path.generic_string(), headers_, connection);
00343 return true;
00344 }
00345 else {
00346 }
00347 }
00348 else {
00349 return false;
00350 }
00351 }
00352 else {
00353 return false;
00354 }
00355 }
00356
00357
00358 HttpServerRequestHandler HttpReply::stock_reply(HttpReply::status_type status)
00359 {
00360 return static_reply(status, "text/html", stock_replies::to_string(status));
00361 }
00362
00363 HttpServerRequestHandler HttpReply::static_reply(HttpReply::status_type status,
00364 const std::string& content_type,
00365 const std::string& content,
00366 const std::vector<HttpHeader>& additional_headers)
00367 {
00368 std::vector<HttpHeader> headers;
00369 headers.push_back(HttpHeader("Content-Length", boost::lexical_cast<std::string>(content.size())));
00370 headers.push_back(HttpHeader("Content-Type", content_type));
00371 std::copy(additional_headers.begin(), additional_headers.end(), headers.begin());
00372 return StaticHttpRequestHandler(status, headers, content);
00373 }
00374
00375
00376 StaticHttpRequestHandler::StaticHttpRequestHandler(HttpReply::status_type status,
00377 const std::vector<HttpHeader> &headers,
00378 const std::string &content)
00379 : reply_builder_(status), content_string_(content)
00380 {
00381 reply_builder_.headers(headers);
00382 }
00383
00384 bool StaticHttpRequestHandler::operator()(const HttpRequest &request, boost::shared_ptr<HttpConnection> connection, const char* begin, const char* end)
00385 {
00386 reply_builder_.write(connection);
00387 connection->write(content_string_);
00388 return true;
00389 }
00390
00391
00392 ReplyBuilder HttpReply::builder(HttpReply::status_type status)
00393 {
00394 return ReplyBuilder(status);
00395 }
00396
00397 ReplyBuilder::ReplyBuilder(HttpReply::status_type status)
00398 : status_(status), headers_(new std::vector<HttpHeader>())
00399 {
00400 }
00401
00402 ReplyBuilder &ReplyBuilder::header(const std::string &name, const std::string &value)
00403 {
00404 return header(HttpHeader(name, value));
00405 }
00406
00407 ReplyBuilder &ReplyBuilder::header(const HttpHeader &header)
00408 {
00409 headers_->push_back(header);
00410 return *this;
00411 }
00412
00413 ReplyBuilder &ReplyBuilder::headers(const std::vector<HttpHeader> &headers)
00414 {
00415 headers_->insert(headers_->end(), headers.begin(), headers.end());
00416 return *this;
00417 }
00418
00419 void ReplyBuilder::write(HttpConnectionPtr connection)
00420 {
00421 connection->write(status_strings::to_buffer(status_), HttpConnection::ResourcePtr());
00422 connection->write(HttpReply::to_buffers(*headers_), headers_);
00423 }
00424
00425 }