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 <boost/config.hpp>
#include <boost/array.hpp>
#include <string>
#include <vector>
#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.
/**
* @note The buffer is invalidated by any vector operation that would also
* invalidate iterators.
*/
template <typename Pod_Type>
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.
/**
* @note The buffer is invalidated by any vector operation that would also
* invalidate iterators.
*/
template <typename Pod_Type>
inline mutable_buffer_container_1 buffer(std::vector<Pod_Type>& data,
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.
/**
* @note The buffer is invalidated by any vector operation that would also
* invalidate iterators.
*/
template <typename Pod_Type>
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.
/**
* @note The buffer is invalidated by any vector operation that would also
* invalidate iterators.
*/
template <typename Pod_Type>
inline const_buffer_container_1 buffer(const std::vector<Pod_Type>& data,
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.
/**
* @note The buffer is invalidated by any vector operation that would also
* invalidate iterators.
*/
template <typename Pod_Type>
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.
/**
* @note The buffer is invalidated by any vector operation that would also
* invalidate iterators.
*/
template <typename Pod_Type>
inline const_buffer_container_1 buffer(std::vector<const Pod_Type>& data,
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));
}
/// 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

View File

@ -14,28 +14,23 @@ struct mapping
{ "htm", "text/html" },
{ "html", "text/html" },
{ "jpg", "image/jpeg" },
{ "png", "image/png" }
{ "png", "image/png" },
{ 0, 0 } // Marks end of list.
};
template <std::size_t N>
std::string find_type(const mapping (&array)[N], const std::string& extension)
std::string extension_to_type(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";
}
std::string extension_to_type(const std::string& extension)
{
return find_type(mappings, extension);
}
} // namespace mime_types
} // namespace server
} // namespace http

View File

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