EchoService.cpp
// ---------------------------------------------------------------------
// pion: a Boost C++ framework for building lightweight HTTP interfaces
// ---------------------------------------------------------------------
// Copyright (C) 2007-2014 Splunk Inc. (https://github.com/splunk/pion)
//
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
//
#include "EchoService.hpp"
#include <boost/bind.hpp>
#include <pion/algorithm.hpp>
#include <pion/http/response_writer.hpp>
#include <pion/user.hpp>
using namespace pion;
namespace pion { // begin namespace pion
namespace plugins { // begin namespace plugins
void writeDictionaryTerm(http::response_writer_ptr& writer,
const ihash_multimap::value_type& val)
{
// text is copied into writer text cache
writer << val.first << http::types::HEADER_NAME_VALUE_DELIMITER
<< val.second
<< http::types::STRING_CRLF;
}
// EchoService member functions
void EchoService::operator()(const http::request_ptr& http_request_ptr, const tcp::connection_ptr& tcp_conn)
{
// this web service uses static text to test the mixture of "copied" with
// "static" (no-copy) text
static const std::string REQUEST_ECHO_TEXT("[Request Echo]");
static const std::string REQUEST_HEADERS_TEXT("[Request Headers]");
static const std::string QUERY_PARAMS_TEXT("[Query Parameters]");
static const std::string COOKIE_PARAMS_TEXT("[Cookie Parameters]");
static const std::string POST_CONTENT_TEXT("[POST Content]");
static const std::string USER_INFO_TEXT("[USER Info]");
// Set Content-type to "text/plain" (plain ascii text)
http::response_writer_ptr writer(http::response_writer::create(tcp_conn, *http_request_ptr,
boost::bind(&tcp::connection::finish, tcp_conn)));
writer->get_response().set_content_type(http::types::CONTENT_TYPE_TEXT);
// write request information
writer->write_no_copy(REQUEST_ECHO_TEXT);
writer->write_no_copy(http::types::STRING_CRLF);
writer->write_no_copy(http::types::STRING_CRLF);
writer
<< "Request method: "
<< http_request_ptr->get_method()
<< http::types::STRING_CRLF
<< "Resource originally requested: "
<< http_request_ptr->get_original_resource()
<< http::types::STRING_CRLF
<< "Resource delivered: "
<< http_request_ptr->get_resource()
<< http::types::STRING_CRLF
<< "Query string: "
<< http_request_ptr->get_query_string()
<< http::types::STRING_CRLF
<< "HTTP version: "
<< http_request_ptr->get_version_major() << '.' << http_request_ptr->get_version_minor()
<< http::types::STRING_CRLF
<< "Content length: "
<< (unsigned long)http_request_ptr->get_content_length()
<< http::types::STRING_CRLF
<< http::types::STRING_CRLF;
// write request headers
writer->write_no_copy(REQUEST_HEADERS_TEXT);
writer->write_no_copy(http::types::STRING_CRLF);
writer->write_no_copy(http::types::STRING_CRLF);
std::for_each(http_request_ptr->get_headers().begin(), http_request_ptr->get_headers().end(),
boost::bind(&writeDictionaryTerm, writer, _1));
writer->write_no_copy(http::types::STRING_CRLF);
// write query parameters
writer->write_no_copy(QUERY_PARAMS_TEXT);
writer->write_no_copy(http::types::STRING_CRLF);
writer->write_no_copy(http::types::STRING_CRLF);
std::for_each(http_request_ptr->get_queries().begin(), http_request_ptr->get_queries().end(),
boost::bind(&writeDictionaryTerm, writer, _1));
writer->write_no_copy(http::types::STRING_CRLF);
// write cookie parameters
writer->write_no_copy(COOKIE_PARAMS_TEXT);
writer->write_no_copy(http::types::STRING_CRLF);
writer->write_no_copy(http::types::STRING_CRLF);
std::for_each(http_request_ptr->get_cookies().begin(), http_request_ptr->get_cookies().end(),
boost::bind(&writeDictionaryTerm, writer, _1));
writer->write_no_copy(http::types::STRING_CRLF);
// write POST content
writer->write_no_copy(POST_CONTENT_TEXT);
writer->write_no_copy(http::types::STRING_CRLF);
writer->write_no_copy(http::types::STRING_CRLF);
if (http_request_ptr->get_content_length() != 0) {
writer->write(http_request_ptr->get_content(), http_request_ptr->get_content_length());
writer->write_no_copy(http::types::STRING_CRLF);
writer->write_no_copy(http::types::STRING_CRLF);
}
// if authenticated, write user info
user_ptr user = http_request_ptr->get_user();
if (user) {
writer->write_no_copy(USER_INFO_TEXT);
writer->write_no_copy(http::types::STRING_CRLF);
writer->write_no_copy(http::types::STRING_CRLF);
writer << "User authenticated, username: " << user->get_username();
writer->write_no_copy(http::types::STRING_CRLF);
}
// send the writer
writer->send();
}
} // end namespace plugins
} // end namespace pion
extern "C" PION_PLUGIN pion::plugins::EchoService *pion_create_EchoService(void)
{
return new pion::plugins::EchoService();
}
extern "C" PION_PLUGIN void pion_destroy_EchoService(pion::plugins::EchoService *service_ptr)
{
delete service_ptr;
}
pion::plugins::EchoService
Definition: EchoService.hpp:22
pion::tcp::connection::finish
void finish(void)
Definition: connection.hpp:596
pion::http::response_writer::create
static boost::shared_ptr< response_writer > create(const tcp::connection_ptr &tcp_conn, const http::response_ptr &http_response_ptr, finished_handler_t handler=finished_handler_t())
Definition: response_writer.hpp:50
pion::user::get_username
std::string const & get_username() const
returns user name as a string
Definition: user.hpp:66
pion::user
Definition: user.hpp:39
pion
Definition: admin_rights.hpp:19
pion::plugins::EchoService::operator()
virtual void operator()(const pion::http::request_ptr &http_request_ptr, const pion::tcp::connection_ptr &tcp_conn)
handles requests for EchoService
Definition: EchoService.cpp:36