00001
00002
00003
00004
00005
00006
00007
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 {
00019 namespace plugins {
00020
00021
00023 void writeDictionaryTerm(http::response_writer_ptr& writer,
00024 const ihash_multimap::value_type& val)
00025 {
00026
00027 writer << val.first << http::types::HEADER_NAME_VALUE_DELIMITER
00028 << val.second
00029 << http::types::STRING_CRLF;
00030 }
00031
00032
00033
00034
00036 void EchoService::operator()(http::request_ptr& http_request_ptr, tcp::connection_ptr& tcp_conn)
00037 {
00038
00039
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
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
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
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
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
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
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
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
00122 writer->send();
00123 }
00124
00125
00126 }
00127 }
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 }