blob: 7c7133c5c5c48f7d69ae5358a0e8f60637e52363 [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- 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 Zhua9a7e1d2013-02-25 18:29:07 -080020//
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 Zhua9a7e1d2013-02-25 18:29:07 -080031#include <boost/lexical_cast.hpp>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080032#include <string>
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080033
34namespace http {
35namespace server {
36
37namespace status_strings {
38
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080039const std::string ok = "HTTP/1.0 200 OK\r\n";
40const std::string created = "HTTP/1.0 201 Created\r\n";
41const std::string accepted = "HTTP/1.0 202 Accepted\r\n";
42const std::string no_content = "HTTP/1.0 204 No Content\r\n";
43const std::string multiple_choices = "HTTP/1.0 300 Multiple Choices\r\n";
44const std::string moved_permanently = "HTTP/1.0 301 Moved Permanently\r\n";
45const std::string moved_temporarily = "HTTP/1.0 302 Moved Temporarily\r\n";
46const std::string not_modified = "HTTP/1.0 304 Not Modified\r\n";
47const std::string bad_request = "HTTP/1.0 400 Bad Request\r\n";
48const std::string unauthorized = "HTTP/1.0 401 Unauthorized\r\n";
49const std::string forbidden = "HTTP/1.0 403 Forbidden\r\n";
50const std::string not_found = "HTTP/1.0 404 Not Found\r\n";
51const std::string internal_server_error = "HTTP/1.0 500 Internal Server Error\r\n";
52const std::string not_implemented = "HTTP/1.0 501 Not Implemented\r\n";
53const std::string bad_gateway = "HTTP/1.0 502 Bad Gateway\r\n";
54const std::string service_unavailable = "HTTP/1.0 503 Service Unavailable\r\n";
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080055
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080056boost::asio::const_buffer
57to_buffer(reply::status_type status)
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080058{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080059 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 Zhua9a7e1d2013-02-25 18:29:07 -080094 }
95}
96
97} // namespace status_strings
98
99namespace misc_strings {
100
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800101const char name_value_separator[] = {':', ' '};
102const char crlf[] = {'\r', '\n'};
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -0800103
104} // namespace misc_strings
105
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800106std::vector<boost::asio::const_buffer>
107reply::to_buffers()
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -0800108{
109 std::vector<boost::asio::const_buffer> buffers;
110 buffers.push_back(status_strings::to_buffer(status));
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800111 for (std::size_t i = 0; i < headers.size(); ++i) {
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -0800112 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
123namespace stock_replies {
124
125const char ok[] = "";
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800126const char created[] = "<html>"
127 "<head><title>Created</title></head>"
128 "<body><h1>201 Created</h1></body>"
129 "</html>";
130const char accepted[] = "<html>"
131 "<head><title>Accepted</title></head>"
132 "<body><h1>202 Accepted</h1></body>"
133 "</html>";
134const char no_content[] = "<html>"
135 "<head><title>No Content</title></head>"
136 "<body><h1>204 Content</h1></body>"
137 "</html>";
138const char multiple_choices[] = "<html>"
139 "<head><title>Multiple Choices</title></head>"
140 "<body><h1>300 Multiple Choices</h1></body>"
141 "</html>";
142const char moved_permanently[] = "<html>"
143 "<head><title>Moved Permanently</title></head>"
144 "<body><h1>301 Moved Permanently</h1></body>"
145 "</html>";
146const char moved_temporarily[] = "<html>"
147 "<head><title>Moved Temporarily</title></head>"
148 "<body><h1>302 Moved Temporarily</h1></body>"
149 "</html>";
150const char not_modified[] = "<html>"
151 "<head><title>Not Modified</title></head>"
152 "<body><h1>304 Not Modified</h1></body>"
153 "</html>";
154const char bad_request[] = "<html>"
155 "<head><title>Bad Request</title></head>"
156 "<body><h1>400 Bad Request</h1></body>"
157 "</html>";
158const char unauthorized[] = "<html>"
159 "<head><title>Unauthorized</title></head>"
160 "<body><h1>401 Unauthorized</h1></body>"
161 "</html>";
162const char forbidden[] = "<html>"
163 "<head><title>Forbidden</title></head>"
164 "<body><h1>403 Forbidden</h1></body>"
165 "</html>";
166const char not_found[] = "<html>"
167 "<head><title>Not Found</title></head>"
168 "<body><h1>404 Not Found</h1></body>"
169 "</html>";
170const char internal_server_error[] = "<html>"
171 "<head><title>Internal Server Error</title></head>"
172 "<body><h1>500 Internal Server Error</h1></body>"
173 "</html>";
174const char not_implemented[] = "<html>"
175 "<head><title>Not Implemented</title></head>"
176 "<body><h1>501 Not Implemented</h1></body>"
177 "</html>";
178const char bad_gateway[] = "<html>"
179 "<head><title>Bad Gateway</title></head>"
180 "<body><h1>502 Bad Gateway</h1></body>"
181 "</html>";
182const char service_unavailable[] = "<html>"
183 "<head><title>Service Unavailable</title></head>"
184 "<body><h1>503 Service Unavailable</h1></body>"
185 "</html>";
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -0800186
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800187std::string
188to_string(reply::status_type status)
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -0800189{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800190 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 Zhua9a7e1d2013-02-25 18:29:07 -0800225 }
226}
227
228} // namespace stock_replies
229
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800230reply
231reply::stock_reply(reply::status_type status)
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -0800232{
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