00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "EchoService.hpp"
00011 #include <boost/bind.hpp>
00012 #include <pion/net/HTTPResponseWriter.hpp>
00013 #include <pion/net/PionUser.hpp>
00014
00015 using namespace pion;
00016 using namespace pion::net;
00017
00018 namespace pion {
00019 namespace plugins {
00020
00021
00023 void writeDictionaryTerm(HTTPResponseWriterPtr& writer,
00024 const HTTPTypes::QueryParams::value_type& val,
00025 const bool decode)
00026 {
00027
00028 writer << val.first << HTTPTypes::HEADER_NAME_VALUE_DELIMITER
00029 << (decode ? HTTPTypes::url_decode(val.second) : val.second)
00030 << HTTPTypes::STRING_CRLF;
00031 }
00032
00033
00034
00035
00037 void EchoService::operator()(HTTPRequestPtr& request, TCPConnectionPtr& tcp_conn)
00038 {
00039
00040
00041 static const std::string REQUEST_ECHO_TEXT("[Request Echo]");
00042 static const std::string REQUEST_HEADERS_TEXT("[Request Headers]");
00043 static const std::string QUERY_PARAMS_TEXT("[Query Parameters]");
00044 static const std::string COOKIE_PARAMS_TEXT("[Cookie Parameters]");
00045 static const std::string POST_CONTENT_TEXT("[POST Content]");
00046 static const std::string USER_INFO_TEXT("[USER Info]");
00047
00048
00049 HTTPResponseWriterPtr writer(HTTPResponseWriter::create(tcp_conn, *request,
00050 boost::bind(&TCPConnection::finish, tcp_conn)));
00051 writer->getResponse().setContentType(HTTPTypes::CONTENT_TYPE_TEXT);
00052
00053
00054 writer->writeNoCopy(REQUEST_ECHO_TEXT);
00055 writer->writeNoCopy(HTTPTypes::STRING_CRLF);
00056 writer->writeNoCopy(HTTPTypes::STRING_CRLF);
00057 writer
00058 << "Request method: "
00059 << request->getMethod()
00060 << HTTPTypes::STRING_CRLF
00061 << "Resource originally requested: "
00062 << request->getOriginalResource()
00063 << HTTPTypes::STRING_CRLF
00064 << "Resource delivered: "
00065 << request->getResource()
00066 << HTTPTypes::STRING_CRLF
00067 << "Query string: "
00068 << request->getQueryString()
00069 << HTTPTypes::STRING_CRLF
00070 << "HTTP version: "
00071 << request->getVersionMajor() << '.' << request->getVersionMinor()
00072 << HTTPTypes::STRING_CRLF
00073 << "Content length: "
00074 << (unsigned long)request->getContentLength()
00075 << HTTPTypes::STRING_CRLF
00076 << HTTPTypes::STRING_CRLF;
00077
00078
00079 writer->writeNoCopy(REQUEST_HEADERS_TEXT);
00080 writer->writeNoCopy(HTTPTypes::STRING_CRLF);
00081 writer->writeNoCopy(HTTPTypes::STRING_CRLF);
00082 std::for_each(request->getHeaders().begin(), request->getHeaders().end(),
00083 boost::bind(&writeDictionaryTerm, writer, _1, false));
00084 writer->writeNoCopy(HTTPTypes::STRING_CRLF);
00085
00086
00087 writer->writeNoCopy(QUERY_PARAMS_TEXT);
00088 writer->writeNoCopy(HTTPTypes::STRING_CRLF);
00089 writer->writeNoCopy(HTTPTypes::STRING_CRLF);
00090 std::for_each(request->getQueryParams().begin(), request->getQueryParams().end(),
00091 boost::bind(&writeDictionaryTerm, writer, _1, true));
00092 writer->writeNoCopy(HTTPTypes::STRING_CRLF);
00093
00094
00095 writer->writeNoCopy(COOKIE_PARAMS_TEXT);
00096 writer->writeNoCopy(HTTPTypes::STRING_CRLF);
00097 writer->writeNoCopy(HTTPTypes::STRING_CRLF);
00098 std::for_each(request->getCookieParams().begin(), request->getCookieParams().end(),
00099 boost::bind(&writeDictionaryTerm, writer, _1, false));
00100 writer->writeNoCopy(HTTPTypes::STRING_CRLF);
00101
00102
00103 writer->writeNoCopy(POST_CONTENT_TEXT);
00104 writer->writeNoCopy(HTTPTypes::STRING_CRLF);
00105 writer->writeNoCopy(HTTPTypes::STRING_CRLF);
00106 if (request->getContentLength() != 0) {
00107 writer->write(request->getContent(), request->getContentLength());
00108 writer->writeNoCopy(HTTPTypes::STRING_CRLF);
00109 writer->writeNoCopy(HTTPTypes::STRING_CRLF);
00110 }
00111
00112
00113 PionUserPtr user = request->getUser();
00114 if (user) {
00115 writer->writeNoCopy(USER_INFO_TEXT);
00116 writer->writeNoCopy(HTTPTypes::STRING_CRLF);
00117 writer->writeNoCopy(HTTPTypes::STRING_CRLF);
00118 writer << "User authenticated, username: " << user->getUsername();
00119 writer->writeNoCopy(HTTPTypes::STRING_CRLF);
00120 }
00121
00122
00123 writer->send();
00124 }
00125
00126
00127 }
00128 }
00129
00130
00132 extern "C" PION_SERVICE_API pion::plugins::EchoService *pion_create_EchoService(void)
00133 {
00134 return new pion::plugins::EchoService();
00135 }
00136
00138 extern "C" PION_SERVICE_API void pion_destroy_EchoService(pion::plugins::EchoService *service_ptr)
00139 {
00140 delete service_ptr;
00141 }