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