00001 // 00002 // reply.cpp 00003 // ~~~~~~~~~ 00004 // 00005 // Copyright (c) 2003-2011 Christopher M. Kohlhoff (chris at kohlhoff dot com) 00006 // 00007 // Distributed under the Boost Software License, Version 1.0. (See accompanying 00008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 00009 // 00010 00011 #include "reply.h" 00012 #include <string> 00013 #include <boost/lexical_cast.hpp> 00014 00015 namespace ros_http_video_streamer 00016 { 00017 00018 namespace status_strings { 00019 00020 const std::string ok = 00021 "HTTP/1.0 200 OK\r\n"; 00022 const std::string created = 00023 "HTTP/1.0 201 Created\r\n"; 00024 const std::string accepted = 00025 "HTTP/1.0 202 Accepted\r\n"; 00026 const std::string no_content = 00027 "HTTP/1.0 204 No Content\r\n"; 00028 const std::string multiple_choices = 00029 "HTTP/1.0 300 Multiple Choices\r\n"; 00030 const std::string moved_permanently = 00031 "HTTP/1.0 301 Moved Permanently\r\n"; 00032 const std::string moved_temporarily = 00033 "HTTP/1.0 302 Moved Temporarily\r\n"; 00034 const std::string not_modified = 00035 "HTTP/1.0 304 Not Modified\r\n"; 00036 const std::string bad_request = 00037 "HTTP/1.0 400 Bad Request\r\n"; 00038 const std::string unauthorized = 00039 "HTTP/1.0 401 Unauthorized\r\n"; 00040 const std::string forbidden = 00041 "HTTP/1.0 403 Forbidden\r\n"; 00042 const std::string not_found = 00043 "HTTP/1.0 404 Not Found\r\n"; 00044 const std::string internal_server_error = 00045 "HTTP/1.0 500 Internal Server Error\r\n"; 00046 const std::string not_implemented = 00047 "HTTP/1.0 501 Not Implemented\r\n"; 00048 const std::string bad_gateway = 00049 "HTTP/1.0 502 Bad Gateway\r\n"; 00050 const std::string service_unavailable = 00051 "HTTP/1.0 503 Service Unavailable\r\n"; 00052 00053 boost::asio::const_buffer to_buffer(reply::status_type status) 00054 { 00055 switch (status) 00056 { 00057 case reply::ok: 00058 return boost::asio::buffer(ok); 00059 case reply::created: 00060 return boost::asio::buffer(created); 00061 case reply::accepted: 00062 return boost::asio::buffer(accepted); 00063 case reply::no_content: 00064 return boost::asio::buffer(no_content); 00065 case reply::multiple_choices: 00066 return boost::asio::buffer(multiple_choices); 00067 case reply::moved_permanently: 00068 return boost::asio::buffer(moved_permanently); 00069 case reply::moved_temporarily: 00070 return boost::asio::buffer(moved_temporarily); 00071 case reply::not_modified: 00072 return boost::asio::buffer(not_modified); 00073 case reply::bad_request: 00074 return boost::asio::buffer(bad_request); 00075 case reply::unauthorized: 00076 return boost::asio::buffer(unauthorized); 00077 case reply::forbidden: 00078 return boost::asio::buffer(forbidden); 00079 case reply::not_found: 00080 return boost::asio::buffer(not_found); 00081 case reply::internal_server_error: 00082 return boost::asio::buffer(internal_server_error); 00083 case reply::not_implemented: 00084 return boost::asio::buffer(not_implemented); 00085 case reply::bad_gateway: 00086 return boost::asio::buffer(bad_gateway); 00087 case reply::service_unavailable: 00088 return boost::asio::buffer(service_unavailable); 00089 default: 00090 return boost::asio::buffer(internal_server_error); 00091 } 00092 } 00093 00094 } // namespace status_strings 00095 00096 namespace misc_strings { 00097 00098 const char name_value_separator[] = { ':', ' ' }; 00099 const char crlf[] = { '\r', '\n' }; 00100 00101 } // namespace misc_strings 00102 00103 std::vector<boost::asio::const_buffer> reply::to_buffers() 00104 { 00105 std::vector<boost::asio::const_buffer> buffers; 00106 buffers.push_back(status_strings::to_buffer(status)); 00107 for (std::size_t i = 0; i < headers.size(); ++i) 00108 { 00109 header& h = headers[i]; 00110 buffers.push_back(boost::asio::buffer(h.name)); 00111 buffers.push_back(boost::asio::buffer(misc_strings::name_value_separator)); 00112 buffers.push_back(boost::asio::buffer(h.value)); 00113 buffers.push_back(boost::asio::buffer(misc_strings::crlf)); 00114 } 00115 buffers.push_back(boost::asio::buffer(misc_strings::crlf)); 00116 buffers.push_back(boost::asio::buffer(content)); 00117 return buffers; 00118 } 00119 00120 namespace stock_replies { 00121 00122 const char ok[] = ""; 00123 const char created[] = 00124 "<html>" 00125 "<head><title>Created</title></head>" 00126 "<body><h1>201 Created</h1></body>" 00127 "</html>"; 00128 const char accepted[] = 00129 "<html>" 00130 "<head><title>Accepted</title></head>" 00131 "<body><h1>202 Accepted</h1></body>" 00132 "</html>"; 00133 const char no_content[] = 00134 "<html>" 00135 "<head><title>No Content</title></head>" 00136 "<body><h1>204 Content</h1></body>" 00137 "</html>"; 00138 const char multiple_choices[] = 00139 "<html>" 00140 "<head><title>Multiple Choices</title></head>" 00141 "<body><h1>300 Multiple Choices</h1></body>" 00142 "</html>"; 00143 const char moved_permanently[] = 00144 "<html>" 00145 "<head><title>Moved Permanently</title></head>" 00146 "<body><h1>301 Moved Permanently</h1></body>" 00147 "</html>"; 00148 const char moved_temporarily[] = 00149 "<html>" 00150 "<head><title>Moved Temporarily</title></head>" 00151 "<body><h1>302 Moved Temporarily</h1></body>" 00152 "</html>"; 00153 const char not_modified[] = 00154 "<html>" 00155 "<head><title>Not Modified</title></head>" 00156 "<body><h1>304 Not Modified</h1></body>" 00157 "</html>"; 00158 const char bad_request[] = 00159 "<html>" 00160 "<head><title>Bad Request</title></head>" 00161 "<body><h1>400 Bad Request</h1></body>" 00162 "</html>"; 00163 const char unauthorized[] = 00164 "<html>" 00165 "<head><title>Unauthorized</title></head>" 00166 "<body><h1>401 Unauthorized</h1></body>" 00167 "</html>"; 00168 const char forbidden[] = 00169 "<html>" 00170 "<head><title>Forbidden</title></head>" 00171 "<body><h1>403 Forbidden</h1></body>" 00172 "</html>"; 00173 const char not_found[] = 00174 "<html>" 00175 "<head><title>Not Found</title></head>" 00176 "<body><h1>404 Not Found</h1></body>" 00177 "</html>"; 00178 const char internal_server_error[] = 00179 "<html>" 00180 "<head><title>Internal Server Error</title></head>" 00181 "<body><h1>500 Internal Server Error</h1></body>" 00182 "</html>"; 00183 const char not_implemented[] = 00184 "<html>" 00185 "<head><title>Not Implemented</title></head>" 00186 "<body><h1>501 Not Implemented</h1></body>" 00187 "</html>"; 00188 const char bad_gateway[] = 00189 "<html>" 00190 "<head><title>Bad Gateway</title></head>" 00191 "<body><h1>502 Bad Gateway</h1></body>" 00192 "</html>"; 00193 const char service_unavailable[] = 00194 "<html>" 00195 "<head><title>Service Unavailable</title></head>" 00196 "<body><h1>503 Service Unavailable</h1></body>" 00197 "</html>"; 00198 00199 std::string to_string(reply::status_type status) 00200 { 00201 switch (status) 00202 { 00203 case reply::ok: 00204 return ok; 00205 case reply::created: 00206 return created; 00207 case reply::accepted: 00208 return accepted; 00209 case reply::no_content: 00210 return no_content; 00211 case reply::multiple_choices: 00212 return multiple_choices; 00213 case reply::moved_permanently: 00214 return moved_permanently; 00215 case reply::moved_temporarily: 00216 return moved_temporarily; 00217 case reply::not_modified: 00218 return not_modified; 00219 case reply::bad_request: 00220 return bad_request; 00221 case reply::unauthorized: 00222 return unauthorized; 00223 case reply::forbidden: 00224 return forbidden; 00225 case reply::not_found: 00226 return not_found; 00227 case reply::internal_server_error: 00228 return internal_server_error; 00229 case reply::not_implemented: 00230 return not_implemented; 00231 case reply::bad_gateway: 00232 return bad_gateway; 00233 case reply::service_unavailable: 00234 return service_unavailable; 00235 default: 00236 return internal_server_error; 00237 } 00238 } 00239 00240 } // namespace stock_replies 00241 00242 reply reply::stock_reply(reply::status_type status) 00243 { 00244 reply rep; 00245 rep.status = status; 00246 rep.content = stock_replies::to_string(status); 00247 rep.headers.resize(2); 00248 rep.headers[0].name = "Content-Length"; 00249 rep.headers[0].value = boost::lexical_cast<std::string>(rep.content.size()); 00250 rep.headers[1].name = "Content-Type"; 00251 rep.headers[1].value = "text/html"; 00252 return rep; 00253 } 00254 00255 } // ros_http_video_streamer