reply.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //
  2. // reply.cpp
  3. // ~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #include <string>
  11. #include <boost/lexical_cast.hpp>
  12. #include "Experimental/http_server/reply.h"
  13. namespace http_server {
  14. namespace status_strings {
  15. const std::string ok = "HTTP/1.0 200 OK\r\n";
  16. const std::string created = "HTTP/1.0 201 Created\r\n";
  17. const std::string accepted = "HTTP/1.0 202 Accepted\r\n";
  18. const std::string no_content = "HTTP/1.0 204 No Content\r\n";
  19. const std::string multiple_choices = "HTTP/1.0 300 Multiple Choices\r\n";
  20. const std::string moved_permanently = "HTTP/1.0 301 Moved Permanently\r\n";
  21. const std::string moved_temporarily = "HTTP/1.0 302 Moved Temporarily\r\n";
  22. const std::string not_modified = "HTTP/1.0 304 Not Modified\r\n";
  23. const std::string bad_request = "HTTP/1.0 400 Bad Request\r\n";
  24. const std::string unauthorized = "HTTP/1.0 401 Unauthorized\r\n";
  25. const std::string forbidden = "HTTP/1.0 403 Forbidden\r\n";
  26. const std::string not_found = "HTTP/1.0 404 Not Found\r\n";
  27. const std::string internal_server_error =
  28. "HTTP/1.0 500 Internal Server Error\r\n";
  29. const std::string not_implemented = "HTTP/1.0 501 Not Implemented\r\n";
  30. const std::string bad_gateway = "HTTP/1.0 502 Bad Gateway\r\n";
  31. const std::string service_unavailable = "HTTP/1.0 503 Service Unavailable\r\n";
  32. boost::asio::const_buffer to_buffer(reply::status_type status)
  33. {
  34. switch (status)
  35. {
  36. case reply::ok:
  37. return boost::asio::buffer(ok);
  38. case reply::created:
  39. return boost::asio::buffer(created);
  40. case reply::accepted:
  41. return boost::asio::buffer(accepted);
  42. case reply::no_content:
  43. return boost::asio::buffer(no_content);
  44. case reply::multiple_choices:
  45. return boost::asio::buffer(multiple_choices);
  46. case reply::moved_permanently:
  47. return boost::asio::buffer(moved_permanently);
  48. case reply::moved_temporarily:
  49. return boost::asio::buffer(moved_temporarily);
  50. case reply::not_modified:
  51. return boost::asio::buffer(not_modified);
  52. case reply::bad_request:
  53. return boost::asio::buffer(bad_request);
  54. case reply::unauthorized:
  55. return boost::asio::buffer(unauthorized);
  56. case reply::forbidden:
  57. return boost::asio::buffer(forbidden);
  58. case reply::not_found:
  59. return boost::asio::buffer(not_found);
  60. case reply::internal_server_error:
  61. return boost::asio::buffer(internal_server_error);
  62. case reply::not_implemented:
  63. return boost::asio::buffer(not_implemented);
  64. case reply::bad_gateway:
  65. return boost::asio::buffer(bad_gateway);
  66. case reply::service_unavailable:
  67. return boost::asio::buffer(service_unavailable);
  68. default:
  69. return boost::asio::buffer(internal_server_error);
  70. }
  71. }
  72. } // namespace status_strings
  73. namespace misc_strings {
  74. const char name_value_separator[] =
  75. {':', ' '};
  76. const char crlf[] =
  77. {'\r', '\n'};
  78. } // namespace misc_strings
  79. std::vector<boost::asio::const_buffer> reply::to_buffers()
  80. {
  81. std::vector<boost::asio::const_buffer> buffers;
  82. buffers.push_back(status_strings::to_buffer(status));
  83. for (std::size_t i = 0; i < headers.size(); ++i)
  84. {
  85. header& h = headers[i];
  86. buffers.push_back(boost::asio::buffer(h.name));
  87. buffers.push_back(boost::asio::buffer(
  88. misc_strings::name_value_separator));
  89. buffers.push_back(boost::asio::buffer(h.value));
  90. buffers.push_back(boost::asio::buffer(misc_strings::crlf));
  91. }
  92. buffers.push_back(boost::asio::buffer(misc_strings::crlf));
  93. buffers.push_back(boost::asio::buffer(content));
  94. return buffers;
  95. }
  96. namespace stock_replies {
  97. const char ok[] = "";
  98. const char created[] = "<html>"
  99. "<head><title>Created</title></head>"
  100. "<body><h1>201 Created</h1></body>"
  101. "</html>";
  102. const char accepted[] = "<html>"
  103. "<head><title>Accepted</title></head>"
  104. "<body><h1>202 Accepted</h1></body>"
  105. "</html>";
  106. const char no_content[] = "<html>"
  107. "<head><title>No Content</title></head>"
  108. "<body><h1>204 Content</h1></body>"
  109. "</html>";
  110. const char multiple_choices[] = "<html>"
  111. "<head><title>Multiple Choices</title></head>"
  112. "<body><h1>300 Multiple Choices</h1></body>"
  113. "</html>";
  114. const char moved_permanently[] = "<html>"
  115. "<head><title>Moved Permanently</title></head>"
  116. "<body><h1>301 Moved Permanently</h1></body>"
  117. "</html>";
  118. const char moved_temporarily[] = "<html>"
  119. "<head><title>Moved Temporarily</title></head>"
  120. "<body><h1>302 Moved Temporarily</h1></body>"
  121. "</html>";
  122. const char not_modified[] = "<html>"
  123. "<head><title>Not Modified</title></head>"
  124. "<body><h1>304 Not Modified</h1></body>"
  125. "</html>";
  126. const char bad_request[] = "<html>"
  127. "<head><title>Bad Request</title></head>"
  128. "<body><h1>400 Bad Request</h1></body>"
  129. "</html>";
  130. const char unauthorized[] = "<html>"
  131. "<head><title>Unauthorized</title></head>"
  132. "<body><h1>401 Unauthorized</h1></body>"
  133. "</html>";
  134. const char forbidden[] = "<html>"
  135. "<head><title>Forbidden</title></head>"
  136. "<body><h1>403 Forbidden</h1></body>"
  137. "</html>";
  138. const char not_found[] = "<html>"
  139. "<head><title>Not Found</title></head>"
  140. "<body><h1>404 Not Found</h1></body>"
  141. "</html>";
  142. const char internal_server_error[] = "<html>"
  143. "<head><title>Internal Server Error</title></head>"
  144. "<body><h1>500 Internal Server Error</h1></body>"
  145. "</html>";
  146. const char not_implemented[] = "<html>"
  147. "<head><title>Not Implemented</title></head>"
  148. "<body><h1>501 Not Implemented</h1></body>"
  149. "</html>";
  150. const char bad_gateway[] = "<html>"
  151. "<head><title>Bad Gateway</title></head>"
  152. "<body><h1>502 Bad Gateway</h1></body>"
  153. "</html>";
  154. const char service_unavailable[] = "<html>"
  155. "<head><title>Service Unavailable</title></head>"
  156. "<body><h1>503 Service Unavailable</h1></body>"
  157. "</html>";
  158. std::string to_string(reply::status_type status)
  159. {
  160. switch (status)
  161. {
  162. case reply::ok:
  163. return ok;
  164. case reply::created:
  165. return created;
  166. case reply::accepted:
  167. return accepted;
  168. case reply::no_content:
  169. return no_content;
  170. case reply::multiple_choices:
  171. return multiple_choices;
  172. case reply::moved_permanently:
  173. return moved_permanently;
  174. case reply::moved_temporarily:
  175. return moved_temporarily;
  176. case reply::not_modified:
  177. return not_modified;
  178. case reply::bad_request:
  179. return bad_request;
  180. case reply::unauthorized:
  181. return unauthorized;
  182. case reply::forbidden:
  183. return forbidden;
  184. case reply::not_found:
  185. return not_found;
  186. case reply::internal_server_error:
  187. return internal_server_error;
  188. case reply::not_implemented:
  189. return not_implemented;
  190. case reply::bad_gateway:
  191. return bad_gateway;
  192. case reply::service_unavailable:
  193. return service_unavailable;
  194. default:
  195. return internal_server_error;
  196. }
  197. }
  198. } // namespace stock_replies
  199. reply reply::stock_reply(reply::status_type status)
  200. {
  201. reply rep;
  202. rep.status = status;
  203. rep.content = stock_replies::to_string(status);
  204. rep.headers.resize(2);
  205. rep.headers[0].name = "Content-Length";
  206. rep.headers[0].value = boost::lexical_cast<std::string>(rep.content.size());
  207. rep.headers[1].name = "Content-Type";
  208. rep.headers[1].value = "text/html";
  209. return rep;
  210. }
  211. } // namespace http_server