reply.cpp 7.1 KB

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