| Home | Libraries | People | FAQ | More

# http/server/reply.cpp

Go to the documentation of this file.

    00001 #include "reply.hpp"
    00002 #include <string>
    00003 #include <boost/lexical_cast.hpp>
    00004
    00005 namespace http {
    00006 namespace server {
    00007
    00008 namespace status_strings {
    00009
    00010 const std::string ok =
    00011   "HTTP/1.0 200 OK\r\n";
    00012 const std::string created =
    00013   "HTTP/1.0 201 Created\r\n";
    00014 const std::string accepted =
    00015   "HTTP/1.0 202 Accepted\r\n";
    00016 const std::string no_content =
    00017   "HTTP/1.0 204 No Content\r\n";
    00018 const std::string multiple_choices =
    00019   "HTTP/1.0 300 Multiple Choices\r\n";
    00020 const std::string moved_permanently =
    00021   "HTTP/1.0 301 Moved Permanently\r\n";
    00022 const std::string moved_temporarily =
    00023   "HTTP/1.0 302 Moved Temporarily\r\n";
    00024 const std::string not_modified =
    00025   "HTTP/1.0 304 Not Modified\r\n";
    00026 const std::string bad_request =
    00027   "HTTP/1.0 400 Bad Request\r\n";
    00028 const std::string unauthorized =
    00029   "HTTP/1.0 401 Unauthorized\r\n";
    00030 const std::string forbidden =
    00031   "HTTP/1.0 403 Forbidden\r\n";
    00032 const std::string not_found =
    00033   "HTTP/1.0 404 Not Found\r\n";
    00034 const std::string internal_server_error =
    00035   "HTTP/1.0 500 Internal Server Error\r\n";
    00036 const std::string not_implemented =
    00037   "HTTP/1.0 501 Not Implemented\r\n";
    00038 const std::string bad_gateway =
    00039   "HTTP/1.0 502 Bad Gateway\r\n";
    00040 const std::string service_unavailable =
    00041   "HTTP/1.0 503 Service Unavailable\r\n";
    00042
    00043 boost::asio::const_buffer to_buffer(reply::status_type status)
    00044 {
    00045   switch (status)
    00046   {
    00047   case reply::ok:
    00048     return boost::asio::buffer(ok);
    00049   case reply::created:
    00050     return boost::asio::buffer(created);
    00051   case reply::accepted:
    00052     return boost::asio::buffer(accepted);
    00053   case reply::no_content:
    00054     return boost::asio::buffer(no_content);
    00055   case reply::multiple_choices:
    00056     return boost::asio::buffer(multiple_choices);
    00057   case reply::moved_permanently:
    00058     return boost::asio::buffer(moved_permanently);
    00059   case reply::moved_temporarily:
    00060     return boost::asio::buffer(moved_temporarily);
    00061   case reply::not_modified:
    00062     return boost::asio::buffer(not_modified);
    00063   case reply::bad_request:
    00064     return boost::asio::buffer(bad_request);
    00065   case reply::unauthorized:
    00066     return boost::asio::buffer(unauthorized);
    00067   case reply::forbidden:
    00068     return boost::asio::buffer(forbidden);
    00069   case reply::not_found:
    00070     return boost::asio::buffer(not_found);
    00071   case reply::internal_server_error:
    00072     return boost::asio::buffer(internal_server_error);
    00073   case reply::not_implemented:
    00074     return boost::asio::buffer(not_implemented);
    00075   case reply::bad_gateway:
    00076     return boost::asio::buffer(bad_gateway);
    00077   case reply::service_unavailable:
    00078     return boost::asio::buffer(service_unavailable);
    00079   default:
    00080     return boost::asio::buffer(internal_server_error);
    00081   }
    00082 }
    00083
    00084 } // namespace status_strings
    00085
    00086 namespace misc_strings {
    00087
    00088 const char name_value_separator[] = { ':', ' ' };
    00089 const char crlf[] = { '\r', '\n' };
    00090
    00091 } // namespace misc_strings
    00092
    00093 std::vector<boost::asio::const_buffer> reply::to_buffers()
    00094 {
    00095   std::vector<boost::asio::const_buffer> buffers;
    00096   buffers.push_back(status_strings::to_buffer(status));
    00097   for (std::size_t i = 0; i < headers.size(); ++i)
    00098   {
    00099     header& h = headers[i];
    00100     buffers.push_back(boost::asio::buffer(h.name));
    00101     buffers.push_back(boost::asio::buffer(misc_strings::name_value_separator));
    00102     buffers.push_back(boost::asio::buffer(h.value));
    00103     buffers.push_back(boost::asio::buffer(misc_strings::crlf));
    00104   }
    00105   buffers.push_back(boost::asio::buffer(misc_strings::crlf));
    00106   buffers.push_back(boost::asio::buffer(content));
    00107   return buffers;
    00108 }
    00109
    00110 namespace stock_replies {
    00111
    00112 const char ok[] = "";
    00113 const char created[] =
    00114   "<html>"
    00115   "<head><title>Created</title></head>"
    00116   "<body><h1>201 Created</h1></body>"
    00117   "</html>";
    00118 const char accepted[] =
    00119   "<html>"
    00120   "<head><title>Accepted</title></head>"
    00121   "<body><h1>202 Accepted</h1></body>"
    00122   "</html>";
    00123 const char no_content[] =
    00124   "<html>"
    00125   "<head><title>No Content</title></head>"
    00126   "<body><h1>204 Content</h1></body>"
    00127   "</html>";
    00128 const char multiple_choices[] =
    00129   "<html>"
    00130   "<head><title>Multiple Choices</title></head>"
    00131   "<body><h1>300 Multiple Choices</h1></body>"
    00132   "</html>";
    00133 const char moved_permanently[] =
    00134   "<html>"
    00135   "<head><title>Moved Permanently</title></head>"
    00136   "<body><h1>301 Moved Permanently</h1></body>"
    00137   "</html>";
    00138 const char moved_temporarily[] =
    00139   "<html>"
    00140   "<head><title>Moved Temporarily</title></head>"
    00141   "<body><h1>302 Moved Temporarily</h1></body>"
    00142   "</html>";
    00143 const char not_modified[] =
    00144   "<html>"
    00145   "<head><title>Not Modified</title></head>"
    00146   "<body><h1>304 Not Modified</h1></body>"
    00147   "</html>";
    00148 const char bad_request[] =
    00149   "<html>"
    00150   "<head><title>Bad Request</title></head>"
    00151   "<body><h1>400 Bad Request</h1></body>"
    00152   "</html>";
    00153 const char unauthorized[] =
    00154   "<html>"
    00155   "<head><title>Unauthorized</title></head>"
    00156   "<body><h1>401 Unauthorized</h1></body>"
    00157   "</html>";
    00158 const char forbidden[] =
    00159   "<html>"
    00160   "<head><title>Forbidden</title></head>"
    00161   "<body><h1>403 Forbidden</h1></body>"
    00162   "</html>";
    00163 const char not_found[] =
    00164   "<html>"
    00165   "<head><title>Not Found</title></head>"
    00166   "<body><h1>404 Not Found</h1></body>"
    00167   "</html>";
    00168 const char internal_server_error[] =
    00169   "<html>"
    00170   "<head><title>Internal Server Error</title></head>"
    00171   "<body><h1>500 Internal Server Error</h1></body>"
    00172   "</html>";
    00173 const char not_implemented[] =
    00174   "<html>"
    00175   "<head><title>Not Implemented</title></head>"
    00176   "<body><h1>501 Not Implemented</h1></body>"
    00177   "</html>";
    00178 const char bad_gateway[] =
    00179   "<html>"
    00180   "<head><title>Bad Gateway</title></head>"
    00181   "<body><h1>502 Bad Gateway</h1></body>"
    00182   "</html>";
    00183 const char service_unavailable[] =
    00184   "<html>"
    00185   "<head><title>Service Unavailable</title></head>"
    00186   "<body><h1>503 Service Unavailable</h1></body>"
    00187   "</html>";
    00188
    00189 std::string to_string(reply::status_type status)
    00190 {
    00191   switch (status)
    00192   {
    00193   case reply::ok:
    00194     return ok;
    00195   case reply::created:
    00196     return created;
    00197   case reply::accepted:
    00198     return accepted;
    00199   case reply::no_content:
    00200     return no_content;
    00201   case reply::multiple_choices:
    00202     return multiple_choices;
    00203   case reply::moved_permanently:
    00204     return moved_permanently;
    00205   case reply::moved_temporarily:
    00206     return moved_temporarily;
    00207   case reply::not_modified:
    00208     return not_modified;
    00209   case reply::bad_request:
    00210     return bad_request;
    00211   case reply::unauthorized:
    00212     return unauthorized;
    00213   case reply::forbidden:
    00214     return forbidden;
    00215   case reply::not_found:
    00216     return not_found;
    00217   case reply::internal_server_error:
    00218     return internal_server_error;
    00219   case reply::not_implemented:
    00220     return not_implemented;
    00221   case reply::bad_gateway:
    00222     return bad_gateway;
    00223   case reply::service_unavailable:
    00224     return service_unavailable;
    00225   default:
    00226     return internal_server_error;
    00227   }
    00228 }
    00229
    00230 } // namespace stock_replies
    00231
    00232 reply reply::stock_reply(reply::status_type status)
    00233 {
    00234   reply rep;
    00235   rep.status = status;
    00236   rep.content = stock_replies::to_string(status);
    00237   rep.headers.resize(2);
    00238   rep.headers[0].name = "Content-Length";
    00239   rep.headers[0].value = boost::lexical_cast<std::string>(rep.content.size());
    00240   rep.headers[1].name = "Content-Type";
    00241   rep.headers[1].value = "text/html";
    00242   return rep;
    00243 }
    00244
    00245 } // namespace server
    00246 } // namespace http

| Copyright © 2003 - 2006 Christopher M. Kohlhoff