- Main Page
- Classes
- Files

- File List

# services/EchoService.cpp

    00001 // ---------------------------------------------------------------------
    00002 // pion:  a Boost C++ framework for building lightweight HTTP interfaces
    00003 // ---------------------------------------------------------------------
    00004 // Copyright (C) 2007-2012 Cloudmeter, Inc.  (http://www.cloudmeter.com)
    00005 //
    00006 // Distributed under the Boost Software License, Version 1.0.
    00007 // See http://www.boost.org/LICENSE_1_0.txt
    00008 //
    00009
    00010 #include "EchoService.hpp"
    00011 #include <boost/bind.hpp>
    00012 #include <pion/algorithm.hpp>
    00013 #include <pion/http/response_writer.hpp>
    00014 #include <pion/user.hpp>
    00015
    00016 using namespace pion;
    00017
    00018 namespace pion {        // begin namespace pion
    00019 namespace plugins {     // begin namespace plugins
    00020
    00021
    00023 void writeDictionaryTerm(http::response_writer_ptr& writer,
    00024                          const ihash_multimap::value_type& val)
    00025 {
    00026     // text is copied into writer text cache
    00027     writer << val.first << http::types::HEADER_NAME_VALUE_DELIMITER
    00028     << val.second
    00029     << http::types::STRING_CRLF;
    00030 }
    00031
    00032
    00033 // EchoService member functions
    00034
    00036 void EchoService::operator()(http::request_ptr& http_request_ptr, tcp::connection_ptr& tcp_conn)
    00037 {
    00038     // this web service uses static text to test the mixture of "copied" with
    00039     // "static" (no-copy) text
    00040     static const std::string REQUEST_ECHO_TEXT("[Request Echo]");
    00041     static const std::string REQUEST_HEADERS_TEXT("[Request Headers]");
    00042     static const std::string QUERY_PARAMS_TEXT("[Query Parameters]");
    00043     static const std::string COOKIE_PARAMS_TEXT("[Cookie Parameters]");
    00044     static const std::string POST_CONTENT_TEXT("[POST Content]");
    00045     static const std::string USER_INFO_TEXT("[USER Info]");
    00046
    00047     // Set Content-type to "text/plain" (plain ascii text)
    00048     http::response_writer_ptr writer(http::response_writer::create(tcp_conn, *http_request_ptr,
    00049                                                             boost::bind(&tcp::connection::finish, tcp_conn)));
    00050     writer->get_response().set_content_type(http::types::CONTENT_TYPE_TEXT);
    00051
    00052     // write request information
    00053     writer->write_no_copy(REQUEST_ECHO_TEXT);
    00054     writer->write_no_copy(http::types::STRING_CRLF);
    00055     writer->write_no_copy(http::types::STRING_CRLF);
    00056     writer
    00057         << "Request method: "
    00058         << http_request_ptr->get_method()
    00059         << http::types::STRING_CRLF
    00060         << "Resource originally requested: "
    00061         << http_request_ptr->get_original_resource()
    00062         << http::types::STRING_CRLF
    00063         << "Resource delivered: "
    00064         << http_request_ptr->get_resource()
    00065         << http::types::STRING_CRLF
    00066         << "Query string: "
    00067         << http_request_ptr->get_query_string()
    00068         << http::types::STRING_CRLF
    00069         << "HTTP version: "
    00070         << http_request_ptr->get_version_major() << '.' << http_request_ptr->get_version_minor()
    00071         << http::types::STRING_CRLF
    00072         << "Content length: "
    00073         << (unsigned long)http_request_ptr->get_content_length()
    00074         << http::types::STRING_CRLF
    00075         << http::types::STRING_CRLF;
    00076
    00077     // write request headers
    00078     writer->write_no_copy(REQUEST_HEADERS_TEXT);
    00079     writer->write_no_copy(http::types::STRING_CRLF);
    00080     writer->write_no_copy(http::types::STRING_CRLF);
    00081     std::for_each(http_request_ptr->get_headers().begin(), http_request_ptr->get_headers().end(),
    00082                   boost::bind(&writeDictionaryTerm, writer, _1));
    00083     writer->write_no_copy(http::types::STRING_CRLF);
    00084
    00085     // write query parameters
    00086     writer->write_no_copy(QUERY_PARAMS_TEXT);
    00087     writer->write_no_copy(http::types::STRING_CRLF);
    00088     writer->write_no_copy(http::types::STRING_CRLF);
    00089     std::for_each(http_request_ptr->get_queries().begin(), http_request_ptr->get_queries().end(),
    00090                   boost::bind(&writeDictionaryTerm, writer, _1));
    00091     writer->write_no_copy(http::types::STRING_CRLF);
    00092
    00093     // write cookie parameters
    00094     writer->write_no_copy(COOKIE_PARAMS_TEXT);
    00095     writer->write_no_copy(http::types::STRING_CRLF);
    00096     writer->write_no_copy(http::types::STRING_CRLF);
    00097     std::for_each(http_request_ptr->get_cookies().begin(), http_request_ptr->get_cookies().end(),
    00098                   boost::bind(&writeDictionaryTerm, writer, _1));
    00099     writer->write_no_copy(http::types::STRING_CRLF);
    00100
    00101     // write POST content
    00102     writer->write_no_copy(POST_CONTENT_TEXT);
    00103     writer->write_no_copy(http::types::STRING_CRLF);
    00104     writer->write_no_copy(http::types::STRING_CRLF);
    00105     if (http_request_ptr->get_content_length() != 0) {
    00106         writer->write(http_request_ptr->get_content(), http_request_ptr->get_content_length());
    00107         writer->write_no_copy(http::types::STRING_CRLF);
    00108         writer->write_no_copy(http::types::STRING_CRLF);
    00109     }
    00110
    00111     // if authenticated, write user info
    00112     user_ptr user = http_request_ptr->get_user();
    00113     if (user) {
    00114         writer->write_no_copy(USER_INFO_TEXT);
    00115         writer->write_no_copy(http::types::STRING_CRLF);
    00116         writer->write_no_copy(http::types::STRING_CRLF);
    00117         writer << "User authenticated, username: " << user->get_username();
    00118         writer->write_no_copy(http::types::STRING_CRLF);
    00119     }
    00120
    00121     // send the writer
    00122     writer->send();
    00123 }
    00124
    00125
    00126 }   // end namespace plugins
    00127 }   // end namespace pion
    00128
    00129
    00131 extern "C" PION_PLUGIN pion::plugins::EchoService *pion_create_EchoService(void)
    00132 {
    00133     return new pion::plugins::EchoService();
    00134 }
    00135
    00137 extern "C" PION_PLUGIN void pion_destroy_EchoService(pion::plugins::EchoService *service_ptr)
    00138 {
    00139     delete service_ptr;
    00140 }