|
From: Severin P. <pap...@gm...> - 2011-02-25 20:48:27
|
Hi, All
below is echo server source code - Do Not Work. Could I extract headers from
request?
Looking from three miles down to design decision, made in cpp-netlib,
it would be nice to have uniform methods, applicable to both *request*
and*response.
*
There are, of course, some differences to be handled in a different way, but
for both
headers(request|response) shall return pointer/reference to headers, and
preferable of the same type,
body(request|response) shall return pointer/reference to headers, and
preferable of the same type
and so on and so forth. After all, they're very similar and both based upon
basic_message building block
sincerely
Sever
PS BOOST 1.46 gcc 4.4, Ubuntu 10.10 x64
------------------------------------------------------------- cut here
---------------------------------------------------------------------
//
// "echo" server
// simple test server, should just print source/body/headers of the request
and copy into response
//
#include <boost/network/protocol/http/server.hpp>
#include <boost/function_output_iterator.hpp>
#include <string>
#include <iostream>
struct headerPrinter
{
std::ostream& _os;
headerPrinter(std::ostream & os):
_os(os)
{
}
headerPrinter(const headerPrinter& hp):
_os(hp._os)
{
}
template <class Pair> void operator()(const Pair& p)
{
_os << p.first << ": " << p.second << std::endl;
}
};
struct theHandler
{
#pragma region Data
std::ostream* _log;
#pragma endregion
#pragma region Ctor/Dtor
theHandler(std::ostream* log = 0):
_log(log)
{
}
theHandler(const theHandler& thH):
_log(thH._log)
{
}
theHandler& operator=(const theHandler& thH)
{
if (this != &thH)
{
_log = thH._log;
}
return *this;
}
~theHandler()
{
}
#pragma endregion
#pragma region Handler
void operator() (const
boost::network::http::server<theHandler>::request& request,
boost::network::http::server<theHandler>::response& response)
{
using namespace boost::network;
/*
std::copy(headers_.begin(), headers_.end(),
boost::make_function_output_iterator(header_printer(cout)));
cout << endl;
*/
std::ostringstream data;
// ok, printing source
http::server<theHandler>::string_type src = source(request);
data << "Hello, " << src << "!" << std::endl;
// now printing body
http::server<theHandler>::string_type body = body(request);
data << body << std::endl;
// try to print headers
// DNW!
// headers_range<http::server<theHandler>::request>::type _headers =
request.headers();
// DNW either
headers_range<http::server<theHandler>::request>::type _headers =
headers(request);
std::copy(_headers.begin(), _headers.end(),
boost::make_function_output_iterator(headerPrinter(data)));
data << std::endl;
response =
http::server<theHandler>::response::stock_reply(http::server<theHandler>::response::ok,
data.str());
}
#pragma endregion
#pragma region Logger
void log(...)
{
if (_log)
{
(*_log) << "LOG: " << std::endl;
}
}
#pragma endregion
};
int main(int argc, char * argv[])
{
using namespace boost::network;
if (argc != 3)
{
std::cerr << "Usage: " << argv[0] << " address" << " port" <<
std::endl;
return EXIT_FAILURE;
}
try
{
// Creates the request handler
theHandler handler;
// Creates the server
http::server<theHandler> server_(argv[1], argv[2], handler);
// Runs the server
server_.run();
}
catch (std::exception &e)
{
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
|