Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2016, Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ChronoShare, a decentralized file sharing application over NDN. |
| 6 | * |
| 7 | * ChronoShare is free software: you can redistribute it and/or modify it under the terms |
| 8 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 9 | * version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License along with |
| 16 | * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * See AUTHORS.md for complete list of ChronoShare authors and contributors. |
| 19 | */ |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 20 | // |
| 21 | // reply.cpp |
| 22 | // ~~~~~~~~~ |
| 23 | // |
| 24 | // Copyright (c) 2003-2012 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
| 25 | // |
| 26 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 27 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 28 | // |
| 29 | |
| 30 | #include "reply.hpp" |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 31 | #include <boost/lexical_cast.hpp> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 32 | #include <string> |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 33 | |
| 34 | namespace http { |
| 35 | namespace server { |
| 36 | |
| 37 | namespace status_strings { |
| 38 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 39 | const std::string ok = "HTTP/1.0 200 OK\r\n"; |
| 40 | const std::string created = "HTTP/1.0 201 Created\r\n"; |
| 41 | const std::string accepted = "HTTP/1.0 202 Accepted\r\n"; |
| 42 | const std::string no_content = "HTTP/1.0 204 No Content\r\n"; |
| 43 | const std::string multiple_choices = "HTTP/1.0 300 Multiple Choices\r\n"; |
| 44 | const std::string moved_permanently = "HTTP/1.0 301 Moved Permanently\r\n"; |
| 45 | const std::string moved_temporarily = "HTTP/1.0 302 Moved Temporarily\r\n"; |
| 46 | const std::string not_modified = "HTTP/1.0 304 Not Modified\r\n"; |
| 47 | const std::string bad_request = "HTTP/1.0 400 Bad Request\r\n"; |
| 48 | const std::string unauthorized = "HTTP/1.0 401 Unauthorized\r\n"; |
| 49 | const std::string forbidden = "HTTP/1.0 403 Forbidden\r\n"; |
| 50 | const std::string not_found = "HTTP/1.0 404 Not Found\r\n"; |
| 51 | const std::string internal_server_error = "HTTP/1.0 500 Internal Server Error\r\n"; |
| 52 | const std::string not_implemented = "HTTP/1.0 501 Not Implemented\r\n"; |
| 53 | const std::string bad_gateway = "HTTP/1.0 502 Bad Gateway\r\n"; |
| 54 | const std::string service_unavailable = "HTTP/1.0 503 Service Unavailable\r\n"; |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 55 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 56 | boost::asio::const_buffer |
| 57 | to_buffer(reply::status_type status) |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 58 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 59 | switch (status) { |
| 60 | case reply::ok: |
| 61 | return boost::asio::buffer(ok); |
| 62 | case reply::created: |
| 63 | return boost::asio::buffer(created); |
| 64 | case reply::accepted: |
| 65 | return boost::asio::buffer(accepted); |
| 66 | case reply::no_content: |
| 67 | return boost::asio::buffer(no_content); |
| 68 | case reply::multiple_choices: |
| 69 | return boost::asio::buffer(multiple_choices); |
| 70 | case reply::moved_permanently: |
| 71 | return boost::asio::buffer(moved_permanently); |
| 72 | case reply::moved_temporarily: |
| 73 | return boost::asio::buffer(moved_temporarily); |
| 74 | case reply::not_modified: |
| 75 | return boost::asio::buffer(not_modified); |
| 76 | case reply::bad_request: |
| 77 | return boost::asio::buffer(bad_request); |
| 78 | case reply::unauthorized: |
| 79 | return boost::asio::buffer(unauthorized); |
| 80 | case reply::forbidden: |
| 81 | return boost::asio::buffer(forbidden); |
| 82 | case reply::not_found: |
| 83 | return boost::asio::buffer(not_found); |
| 84 | case reply::internal_server_error: |
| 85 | return boost::asio::buffer(internal_server_error); |
| 86 | case reply::not_implemented: |
| 87 | return boost::asio::buffer(not_implemented); |
| 88 | case reply::bad_gateway: |
| 89 | return boost::asio::buffer(bad_gateway); |
| 90 | case reply::service_unavailable: |
| 91 | return boost::asio::buffer(service_unavailable); |
| 92 | default: |
| 93 | return boost::asio::buffer(internal_server_error); |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
| 97 | } // namespace status_strings |
| 98 | |
| 99 | namespace misc_strings { |
| 100 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 101 | const char name_value_separator[] = {':', ' '}; |
| 102 | const char crlf[] = {'\r', '\n'}; |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 103 | |
| 104 | } // namespace misc_strings |
| 105 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 106 | std::vector<boost::asio::const_buffer> |
| 107 | reply::to_buffers() |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 108 | { |
| 109 | std::vector<boost::asio::const_buffer> buffers; |
| 110 | buffers.push_back(status_strings::to_buffer(status)); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 111 | for (std::size_t i = 0; i < headers.size(); ++i) { |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 112 | header& h = headers[i]; |
| 113 | buffers.push_back(boost::asio::buffer(h.name)); |
| 114 | buffers.push_back(boost::asio::buffer(misc_strings::name_value_separator)); |
| 115 | buffers.push_back(boost::asio::buffer(h.value)); |
| 116 | buffers.push_back(boost::asio::buffer(misc_strings::crlf)); |
| 117 | } |
| 118 | buffers.push_back(boost::asio::buffer(misc_strings::crlf)); |
| 119 | buffers.push_back(boost::asio::buffer(content)); |
| 120 | return buffers; |
| 121 | } |
| 122 | |
| 123 | namespace stock_replies { |
| 124 | |
| 125 | const char ok[] = ""; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 126 | const char created[] = "<html>" |
| 127 | "<head><title>Created</title></head>" |
| 128 | "<body><h1>201 Created</h1></body>" |
| 129 | "</html>"; |
| 130 | const char accepted[] = "<html>" |
| 131 | "<head><title>Accepted</title></head>" |
| 132 | "<body><h1>202 Accepted</h1></body>" |
| 133 | "</html>"; |
| 134 | const char no_content[] = "<html>" |
| 135 | "<head><title>No Content</title></head>" |
| 136 | "<body><h1>204 Content</h1></body>" |
| 137 | "</html>"; |
| 138 | const char multiple_choices[] = "<html>" |
| 139 | "<head><title>Multiple Choices</title></head>" |
| 140 | "<body><h1>300 Multiple Choices</h1></body>" |
| 141 | "</html>"; |
| 142 | const char moved_permanently[] = "<html>" |
| 143 | "<head><title>Moved Permanently</title></head>" |
| 144 | "<body><h1>301 Moved Permanently</h1></body>" |
| 145 | "</html>"; |
| 146 | const char moved_temporarily[] = "<html>" |
| 147 | "<head><title>Moved Temporarily</title></head>" |
| 148 | "<body><h1>302 Moved Temporarily</h1></body>" |
| 149 | "</html>"; |
| 150 | const char not_modified[] = "<html>" |
| 151 | "<head><title>Not Modified</title></head>" |
| 152 | "<body><h1>304 Not Modified</h1></body>" |
| 153 | "</html>"; |
| 154 | const char bad_request[] = "<html>" |
| 155 | "<head><title>Bad Request</title></head>" |
| 156 | "<body><h1>400 Bad Request</h1></body>" |
| 157 | "</html>"; |
| 158 | const char unauthorized[] = "<html>" |
| 159 | "<head><title>Unauthorized</title></head>" |
| 160 | "<body><h1>401 Unauthorized</h1></body>" |
| 161 | "</html>"; |
| 162 | const char forbidden[] = "<html>" |
| 163 | "<head><title>Forbidden</title></head>" |
| 164 | "<body><h1>403 Forbidden</h1></body>" |
| 165 | "</html>"; |
| 166 | const char not_found[] = "<html>" |
| 167 | "<head><title>Not Found</title></head>" |
| 168 | "<body><h1>404 Not Found</h1></body>" |
| 169 | "</html>"; |
| 170 | const char internal_server_error[] = "<html>" |
| 171 | "<head><title>Internal Server Error</title></head>" |
| 172 | "<body><h1>500 Internal Server Error</h1></body>" |
| 173 | "</html>"; |
| 174 | const char not_implemented[] = "<html>" |
| 175 | "<head><title>Not Implemented</title></head>" |
| 176 | "<body><h1>501 Not Implemented</h1></body>" |
| 177 | "</html>"; |
| 178 | const char bad_gateway[] = "<html>" |
| 179 | "<head><title>Bad Gateway</title></head>" |
| 180 | "<body><h1>502 Bad Gateway</h1></body>" |
| 181 | "</html>"; |
| 182 | const char service_unavailable[] = "<html>" |
| 183 | "<head><title>Service Unavailable</title></head>" |
| 184 | "<body><h1>503 Service Unavailable</h1></body>" |
| 185 | "</html>"; |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 186 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 187 | std::string |
| 188 | to_string(reply::status_type status) |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 189 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 190 | switch (status) { |
| 191 | case reply::ok: |
| 192 | return ok; |
| 193 | case reply::created: |
| 194 | return created; |
| 195 | case reply::accepted: |
| 196 | return accepted; |
| 197 | case reply::no_content: |
| 198 | return no_content; |
| 199 | case reply::multiple_choices: |
| 200 | return multiple_choices; |
| 201 | case reply::moved_permanently: |
| 202 | return moved_permanently; |
| 203 | case reply::moved_temporarily: |
| 204 | return moved_temporarily; |
| 205 | case reply::not_modified: |
| 206 | return not_modified; |
| 207 | case reply::bad_request: |
| 208 | return bad_request; |
| 209 | case reply::unauthorized: |
| 210 | return unauthorized; |
| 211 | case reply::forbidden: |
| 212 | return forbidden; |
| 213 | case reply::not_found: |
| 214 | return not_found; |
| 215 | case reply::internal_server_error: |
| 216 | return internal_server_error; |
| 217 | case reply::not_implemented: |
| 218 | return not_implemented; |
| 219 | case reply::bad_gateway: |
| 220 | return bad_gateway; |
| 221 | case reply::service_unavailable: |
| 222 | return service_unavailable; |
| 223 | default: |
| 224 | return internal_server_error; |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
| 228 | } // namespace stock_replies |
| 229 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 230 | reply |
| 231 | reply::stock_reply(reply::status_type status) |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 232 | { |
| 233 | reply rep; |
| 234 | rep.status = status; |
| 235 | rep.content = stock_replies::to_string(status); |
| 236 | rep.headers.resize(2); |
| 237 | rep.headers[0].name = "Content-Length"; |
| 238 | rep.headers[0].value = boost::lexical_cast<std::string>(rep.content.size()); |
| 239 | rep.headers[1].name = "Content-Type"; |
| 240 | rep.headers[1].value = "text/html"; |
| 241 | return rep; |
| 242 | } |
| 243 | |
| 244 | } // namespace server |
| 245 | } // namespace http |