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