Go to the documentation of this file.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::stock_reply(HttpReply::status_type status)
00250 {
00251 return static_reply(status, "text/html", stock_replies::to_string(status));
00252 }
00253
00254 HttpServerRequestHandler HttpReply::from_file(HttpReply::status_type status,
00255 const std::string& content_type,
00256 const std::string& filename,
00257 const std::vector<HttpHeader>& additional_headers)
00258 {
00259 std::ifstream file_stream(filename.c_str());
00260 std::stringstream file_buffer;
00261 file_buffer << file_stream.rdbuf();
00262 return static_reply(status, content_type, file_buffer.str(), additional_headers);
00263 }
00264 HttpServerRequestHandler HttpReply::static_reply(HttpReply::status_type status,
00265 const std::string& content_type,
00266 const std::string& content,
00267 const std::vector<HttpHeader>& additional_headers)
00268 {
00269 std::vector<HttpHeader> headers;
00270 headers.push_back(HttpHeader("Content-Length", boost::lexical_cast<std::string>(content.size())));
00271 headers.push_back(HttpHeader("Content-Type", content_type));
00272 std::copy(additional_headers.begin(), additional_headers.end(), headers.begin());
00273 return StaticHttpRequestHandler(status, headers, content);
00274 }
00275
00276
00277 StaticHttpRequestHandler::StaticHttpRequestHandler(HttpReply::status_type status,
00278 const std::vector<HttpHeader> &headers,
00279 const std::string &content)
00280 : reply_builder_(status), content_string_(content)
00281 {
00282 reply_builder_.headers(headers);
00283 }
00284
00285 void StaticHttpRequestHandler::operator()(const HttpRequest &request, boost::shared_ptr<HttpConnection> connection, const char* begin, const char* end)
00286 {
00287 reply_builder_.write(connection);
00288 connection->write(content_string_);
00289 }
00290
00291
00292 ReplyBuilder HttpReply::builder(HttpReply::status_type status)
00293 {
00294 return ReplyBuilder(status);
00295 }
00296
00297 ReplyBuilder::ReplyBuilder(HttpReply::status_type status)
00298 : status_(status), headers_(new std::vector<HttpHeader>())
00299 {
00300 }
00301
00302 ReplyBuilder &ReplyBuilder::header(const std::string &name, const std::string &value)
00303 {
00304 return header(HttpHeader(name, value));
00305 }
00306
00307 ReplyBuilder &ReplyBuilder::header(const HttpHeader &header)
00308 {
00309 headers_->push_back(header);
00310 return *this;
00311 }
00312
00313 ReplyBuilder &ReplyBuilder::headers(const std::vector<HttpHeader> &headers)
00314 {
00315 headers_->insert(headers_->end(), headers.begin(), headers.end());
00316 return *this;
00317 }
00318
00319 void ReplyBuilder::write(HttpConnectionPtr connection)
00320 {
00321 connection->write(status_strings::to_buffer(status_), HttpConnection::ResourcePtr());
00322 connection->write(HttpReply::to_buffers(*headers_), headers_);
00323 }
00324
00325 }