Add asio::buffer() overloads for std::string.

This commit is contained in:
chris_kohlhoff 2005-12-04 11:29:03 +00:00
parent 0e7504cbd1
commit 840f3a313f
3 changed files with 126 additions and 71 deletions

View File

@ -21,6 +21,7 @@
#include <cstddef> #include <cstddef>
#include <boost/config.hpp> #include <boost/config.hpp>
#include <boost/array.hpp> #include <boost/array.hpp>
#include <string>
#include <vector> #include <vector>
#include "asio/detail/pop_options.hpp" #include "asio/detail/pop_options.hpp"
@ -509,6 +510,10 @@ inline const_buffer_container_1 buffer(boost::array<const Pod_Type, N>& data,
} }
/// Create a new modifiable buffer that represents the given POD vector. /// Create a new modifiable buffer that represents the given POD vector.
/**
* @note The buffer is invalidated by any vector operation that would also
* invalidate iterators.
*/
template <typename Pod_Type> template <typename Pod_Type>
inline mutable_buffer_container_1 buffer(std::vector<Pod_Type>& data) inline mutable_buffer_container_1 buffer(std::vector<Pod_Type>& data)
{ {
@ -517,6 +522,10 @@ inline mutable_buffer_container_1 buffer(std::vector<Pod_Type>& data)
} }
/// Create a new modifiable buffer that represents the given POD vector. /// Create a new modifiable buffer that represents the given POD vector.
/**
* @note The buffer is invalidated by any vector operation that would also
* invalidate iterators.
*/
template <typename Pod_Type> template <typename Pod_Type>
inline mutable_buffer_container_1 buffer(std::vector<Pod_Type>& data, inline mutable_buffer_container_1 buffer(std::vector<Pod_Type>& data,
std::size_t max_size_in_bytes) std::size_t max_size_in_bytes)
@ -528,6 +537,10 @@ inline mutable_buffer_container_1 buffer(std::vector<Pod_Type>& data,
} }
/// Create a new non-modifiable buffer that represents the given POD vector. /// Create a new non-modifiable buffer that represents the given POD vector.
/**
* @note The buffer is invalidated by any vector operation that would also
* invalidate iterators.
*/
template <typename Pod_Type> template <typename Pod_Type>
inline const_buffer_container_1 buffer(const std::vector<Pod_Type>& data) inline const_buffer_container_1 buffer(const std::vector<Pod_Type>& data)
{ {
@ -536,6 +549,10 @@ inline const_buffer_container_1 buffer(const std::vector<Pod_Type>& data)
} }
/// Create a new non-modifiable buffer that represents the given POD vector. /// Create a new non-modifiable buffer that represents the given POD vector.
/**
* @note The buffer is invalidated by any vector operation that would also
* invalidate iterators.
*/
template <typename Pod_Type> template <typename Pod_Type>
inline const_buffer_container_1 buffer(const std::vector<Pod_Type>& data, inline const_buffer_container_1 buffer(const std::vector<Pod_Type>& data,
std::size_t max_size_in_bytes) std::size_t max_size_in_bytes)
@ -547,6 +564,10 @@ inline const_buffer_container_1 buffer(const std::vector<Pod_Type>& data,
} }
/// Create a new non-modifiable buffer that represents the given POD vector. /// Create a new non-modifiable buffer that represents the given POD vector.
/**
* @note The buffer is invalidated by any vector operation that would also
* invalidate iterators.
*/
template <typename Pod_Type> template <typename Pod_Type>
inline const_buffer_container_1 buffer(std::vector<const Pod_Type>& data) inline const_buffer_container_1 buffer(std::vector<const Pod_Type>& data)
{ {
@ -555,6 +576,10 @@ inline const_buffer_container_1 buffer(std::vector<const Pod_Type>& data)
} }
/// Create a new non-modifiable buffer that represents the given POD vector. /// Create a new non-modifiable buffer that represents the given POD vector.
/**
* @note The buffer is invalidated by any vector operation that would also
* invalidate iterators.
*/
template <typename Pod_Type> template <typename Pod_Type>
inline const_buffer_container_1 buffer(std::vector<const Pod_Type>& data, inline const_buffer_container_1 buffer(std::vector<const Pod_Type>& data,
std::size_t max_size_in_bytes) std::size_t max_size_in_bytes)
@ -565,6 +590,30 @@ inline const_buffer_container_1 buffer(std::vector<const Pod_Type>& data,
? data.size() * sizeof(Pod_Type) : max_size_in_bytes)); ? data.size() * sizeof(Pod_Type) : max_size_in_bytes));
} }
/// Create a new non-modifiable buffer that represents the given string.
/**
* @note The buffer is invalidated by any non-const operation called on the
* given string object.
*/
inline const_buffer_container_1 buffer(const std::string& data)
{
return const_buffer_container_1(const_buffer(data.data(), data.size()));
}
/// Create a new non-modifiable buffer that represents the given string.
/**
* @note The buffer is invalidated by any non-const operation called on the
* given string object.
*/
inline const_buffer_container_1 buffer(const std::string& data,
std::size_t max_size_in_bytes)
{
return const_buffer_container_1(
const_buffer(data.data(),
data.size() < max_size_in_bytes
? data.size() : max_size_in_bytes));
}
/*@}*/ /*@}*/
} // namespace asio } // namespace asio

View File

@ -14,28 +14,23 @@ struct mapping
{ "htm", "text/html" }, { "htm", "text/html" },
{ "html", "text/html" }, { "html", "text/html" },
{ "jpg", "image/jpeg" }, { "jpg", "image/jpeg" },
{ "png", "image/png" } { "png", "image/png" },
{ 0, 0 } // Marks end of list.
}; };
template <std::size_t N> std::string extension_to_type(const std::string& extension)
std::string find_type(const mapping (&array)[N], const std::string& extension)
{ {
for (std::size_t i = 0; i < N; ++i) for (mapping* m = mappings; m->extension; ++m)
{ {
if (array[i].extension == extension) if (m->extension == extension)
{ {
return array[i].mime_type; return m->mime_type;
} }
} }
return "text/plain"; return "text/plain";
} }
std::string extension_to_type(const std::string& extension)
{
return find_type(mappings, extension);
}
} // namespace mime_types } // namespace mime_types
} // namespace server } // namespace server
} // namespace http } // namespace http

View File

@ -1,4 +1,5 @@
#include "reply.hpp" #include "reply.hpp"
#include <string>
#include <boost/lexical_cast.hpp> #include <boost/lexical_cast.hpp>
namespace http { namespace http {
@ -6,67 +7,77 @@ namespace server {
namespace status_strings { namespace status_strings {
char ok[] = "HTTP/1.0 200 OK\r\n"; const std::string ok =
char created[] = "HTTP/1.0 201 Created\r\n"; "HTTP/1.0 200 OK\r\n";
char accepted[] = "HTTP/1.0 202 Accepted\r\n"; const std::string created =
char no_content[] = "HTTP/1.0 204 No Content\r\n"; "HTTP/1.0 201 Created\r\n";
char multiple_choices[] = "HTTP/1.0 300 Multiple Choices\r\n"; const std::string accepted =
char moved_permanently[] = "HTTP/1.0 301 Moved Permanently\r\n"; "HTTP/1.0 202 Accepted\r\n";
char moved_temporarily[] = "HTTP/1.0 302 Moved Temporarily\r\n"; const std::string no_content =
char not_modified[] = "HTTP/1.0 304 Not Modified\r\n"; "HTTP/1.0 204 No Content\r\n";
char bad_request[] = "HTTP/1.0 400 Bad Request\r\n"; const std::string multiple_choices =
char unauthorized[] = "HTTP/1.0 401 Unauthorized\r\n"; "HTTP/1.0 300 Multiple Choices\r\n";
char forbidden[] = "HTTP/1.0 403 Forbidden\r\n"; const std::string moved_permanently =
char not_found[] = "HTTP/1.0 404 Not Found\r\n"; "HTTP/1.0 301 Moved Permanently\r\n";
char internal_server_error[] = "HTTP/1.0 500 Internal Server Error\r\n"; const std::string moved_temporarily =
char not_implemented[] = "HTTP/1.0 501 Not Implemented\r\n"; "HTTP/1.0 302 Moved Temporarily\r\n";
char bad_gateway[] = "HTTP/1.0 502 Bad Gateway\r\n"; const std::string not_modified =
char service_unavailable[] = "HTTP/1.0 503 Service Unavailable\r\n"; "HTTP/1.0 304 Not Modified\r\n";
const std::string bad_request =
template <std::size_t N> "HTTP/1.0 400 Bad Request\r\n";
asio::const_buffer to_buffer(const char (&str)[N]) const std::string unauthorized =
{ "HTTP/1.0 401 Unauthorized\r\n";
return asio::buffer(str, N - 1); const std::string forbidden =
} "HTTP/1.0 403 Forbidden\r\n";
const std::string not_found =
"HTTP/1.0 404 Not Found\r\n";
const std::string internal_server_error =
"HTTP/1.0 500 Internal Server Error\r\n";
const std::string not_implemented =
"HTTP/1.0 501 Not Implemented\r\n";
const std::string bad_gateway =
"HTTP/1.0 502 Bad Gateway\r\n";
const std::string service_unavailable =
"HTTP/1.0 503 Service Unavailable\r\n";
asio::const_buffer to_buffer(reply::status_type status) asio::const_buffer to_buffer(reply::status_type status)
{ {
switch (status) switch (status)
{ {
case reply::ok: case reply::ok:
return to_buffer(ok); return asio::buffer(ok);
case reply::created: case reply::created:
return to_buffer(created); return asio::buffer(created);
case reply::accepted: case reply::accepted:
return to_buffer(accepted); return asio::buffer(accepted);
case reply::no_content: case reply::no_content:
return to_buffer(no_content); return asio::buffer(no_content);
case reply::multiple_choices: case reply::multiple_choices:
return to_buffer(multiple_choices); return asio::buffer(multiple_choices);
case reply::moved_permanently: case reply::moved_permanently:
return to_buffer(moved_permanently); return asio::buffer(moved_permanently);
case reply::moved_temporarily: case reply::moved_temporarily:
return to_buffer(moved_temporarily); return asio::buffer(moved_temporarily);
case reply::not_modified: case reply::not_modified:
return to_buffer(not_modified); return asio::buffer(not_modified);
case reply::bad_request: case reply::bad_request:
return to_buffer(bad_request); return asio::buffer(bad_request);
case reply::unauthorized: case reply::unauthorized:
return to_buffer(unauthorized); return asio::buffer(unauthorized);
case reply::forbidden: case reply::forbidden:
return to_buffer(forbidden); return asio::buffer(forbidden);
case reply::not_found: case reply::not_found:
return to_buffer(not_found); return asio::buffer(not_found);
case reply::internal_server_error: case reply::internal_server_error:
return to_buffer(internal_server_error); return asio::buffer(internal_server_error);
case reply::not_implemented: case reply::not_implemented:
return to_buffer(not_implemented); return asio::buffer(not_implemented);
case reply::bad_gateway: case reply::bad_gateway:
return to_buffer(bad_gateway); return asio::buffer(bad_gateway);
case reply::service_unavailable: case reply::service_unavailable:
return to_buffer(service_unavailable); return asio::buffer(service_unavailable);
default: default:
return to_buffer(internal_server_error); return asio::buffer(internal_server_error);
} }
} }
@ -74,8 +85,8 @@ asio::const_buffer to_buffer(reply::status_type status)
namespace misc_strings { namespace misc_strings {
char name_value_separator[] = { ':', ' ' }; const char name_value_separator[] = { ':', ' ' };
char crlf[] = { '\r', '\n' }; const char crlf[] = { '\r', '\n' };
} // namespace misc_strings } // namespace misc_strings
@ -86,90 +97,90 @@ std::vector<asio::const_buffer> reply::to_buffers()
for (std::size_t i = 0; i < headers.size(); ++i) for (std::size_t i = 0; i < headers.size(); ++i)
{ {
header& h = headers[i]; header& h = headers[i];
buffers.push_back(asio::buffer(h.name.data(), h.name.size())); buffers.push_back(asio::buffer(h.name));
buffers.push_back(asio::buffer(misc_strings::name_value_separator)); buffers.push_back(asio::buffer(misc_strings::name_value_separator));
buffers.push_back(asio::buffer(h.value.data(), h.value.size())); buffers.push_back(asio::buffer(h.value));
buffers.push_back(asio::buffer(misc_strings::crlf)); buffers.push_back(asio::buffer(misc_strings::crlf));
} }
buffers.push_back(asio::buffer(misc_strings::crlf)); buffers.push_back(asio::buffer(misc_strings::crlf));
buffers.push_back(asio::buffer(content.data(), content.size())); buffers.push_back(asio::buffer(content));
return buffers; return buffers;
} }
namespace stock_replies { namespace stock_replies {
char ok[] = ""; const char ok[] = "";
char created[] = const char created[] =
"<html>" "<html>"
"<head><title>Created</title></head>" "<head><title>Created</title></head>"
"<body><h1>201 Created</h1></body>" "<body><h1>201 Created</h1></body>"
"</html>"; "</html>";
char accepted[] = const char accepted[] =
"<html>" "<html>"
"<head><title>Accepted</title></head>" "<head><title>Accepted</title></head>"
"<body><h1>202 Accepted</h1></body>" "<body><h1>202 Accepted</h1></body>"
"</html>"; "</html>";
char no_content[] = const char no_content[] =
"<html>" "<html>"
"<head><title>No Content</title></head>" "<head><title>No Content</title></head>"
"<body><h1>204 Content</h1></body>" "<body><h1>204 Content</h1></body>"
"</html>"; "</html>";
char multiple_choices[] = const char multiple_choices[] =
"<html>" "<html>"
"<head><title>Multiple Choices</title></head>" "<head><title>Multiple Choices</title></head>"
"<body><h1>300 Multiple Choices</h1></body>" "<body><h1>300 Multiple Choices</h1></body>"
"</html>"; "</html>";
char moved_permanently[] = const char moved_permanently[] =
"<html>" "<html>"
"<head><title>Moved Permanently</title></head>" "<head><title>Moved Permanently</title></head>"
"<body><h1>301 Moved Permanently</h1></body>" "<body><h1>301 Moved Permanently</h1></body>"
"</html>"; "</html>";
char moved_temporarily[] = const char moved_temporarily[] =
"<html>" "<html>"
"<head><title>Moved Temporarily</title></head>" "<head><title>Moved Temporarily</title></head>"
"<body><h1>302 Moved Temporarily</h1></body>" "<body><h1>302 Moved Temporarily</h1></body>"
"</html>"; "</html>";
char not_modified[] = const char not_modified[] =
"<html>" "<html>"
"<head><title>Not Modified</title></head>" "<head><title>Not Modified</title></head>"
"<body><h1>304 Not Modified</h1></body>" "<body><h1>304 Not Modified</h1></body>"
"</html>"; "</html>";
char bad_request[] = const char bad_request[] =
"<html>" "<html>"
"<head><title>Bad Request</title></head>" "<head><title>Bad Request</title></head>"
"<body><h1>400 Bad Request</h1></body>" "<body><h1>400 Bad Request</h1></body>"
"</html>"; "</html>";
char unauthorized[] = const char unauthorized[] =
"<html>" "<html>"
"<head><title>Unauthorized</title></head>" "<head><title>Unauthorized</title></head>"
"<body><h1>401 Unauthorized</h1></body>" "<body><h1>401 Unauthorized</h1></body>"
"</html>"; "</html>";
char forbidden[] = const char forbidden[] =
"<html>" "<html>"
"<head><title>Forbidden</title></head>" "<head><title>Forbidden</title></head>"
"<body><h1>403 Forbidden</h1></body>" "<body><h1>403 Forbidden</h1></body>"
"</html>"; "</html>";
char not_found[] = const char not_found[] =
"<html>" "<html>"
"<head><title>Not Found</title></head>" "<head><title>Not Found</title></head>"
"<body><h1>404 Not Found</h1></body>" "<body><h1>404 Not Found</h1></body>"
"</html>"; "</html>";
char internal_server_error[] = const char internal_server_error[] =
"<html>" "<html>"
"<head><title>Internal Server Error</title></head>" "<head><title>Internal Server Error</title></head>"
"<body><h1>500 Internal Server Error</h1></body>" "<body><h1>500 Internal Server Error</h1></body>"
"</html>"; "</html>";
char not_implemented[] = const char not_implemented[] =
"<html>" "<html>"
"<head><title>Not Implemented</title></head>" "<head><title>Not Implemented</title></head>"
"<body><h1>501 Not Implemented</h1></body>" "<body><h1>501 Not Implemented</h1></body>"
"</html>"; "</html>";
char bad_gateway[] = const char bad_gateway[] =
"<html>" "<html>"
"<head><title>Bad Gateway</title></head>" "<head><title>Bad Gateway</title></head>"
"<body><h1>502 Bad Gateway</h1></body>" "<body><h1>502 Bad Gateway</h1></body>"
"</html>"; "</html>";
char service_unavailable[] = const char service_unavailable[] =
"<html>" "<html>"
"<head><title>Service Unavailable</title></head>" "<head><title>Service Unavailable</title></head>"
"<body><h1>503 Service Unavailable</h1></body>" "<body><h1>503 Service Unavailable</h1></body>"