10 #include "EchoService.hpp"
11 #include <boost/bind.hpp>
12 #include <pion/PionAlgorithms.hpp>
13 #include <pion/net/HTTPResponseWriter.hpp>
14 #include <pion/net/PionUser.hpp>
17 using namespace pion::net;
24 void writeDictionaryTerm(HTTPResponseWriterPtr& writer,
25 const HTTPTypes::QueryParams::value_type& val,
29 writer << val.first << HTTPTypes::HEADER_NAME_VALUE_DELIMITER
38 void EchoService::operator()(HTTPRequestPtr& request, TCPConnectionPtr& tcp_conn)
42 static const std::string REQUEST_ECHO_TEXT(
"[Request Echo]");
43 static const std::string REQUEST_HEADERS_TEXT(
"[Request Headers]");
44 static const std::string QUERY_PARAMS_TEXT(
"[Query Parameters]");
45 static const std::string COOKIE_PARAMS_TEXT(
"[Cookie Parameters]");
46 static const std::string POST_CONTENT_TEXT(
"[POST Content]");
47 static const std::string USER_INFO_TEXT(
"[USER Info]");
50 HTTPResponseWriterPtr writer(HTTPResponseWriter::create(tcp_conn, *request,
51 boost::bind(&TCPConnection::finish, tcp_conn)));
52 writer->getResponse().setContentType(HTTPTypes::CONTENT_TYPE_TEXT);
55 writer->writeNoCopy(REQUEST_ECHO_TEXT);
56 writer->writeNoCopy(HTTPTypes::STRING_CRLF);
57 writer->writeNoCopy(HTTPTypes::STRING_CRLF);
60 << request->getMethod()
61 << HTTPTypes::STRING_CRLF
62 <<
"Resource originally requested: "
63 << request->getOriginalResource()
64 << HTTPTypes::STRING_CRLF
65 <<
"Resource delivered: "
66 << request->getResource()
67 << HTTPTypes::STRING_CRLF
69 << request->getQueryString()
70 << HTTPTypes::STRING_CRLF
72 << request->getVersionMajor() <<
'.' << request->getVersionMinor()
73 << HTTPTypes::STRING_CRLF
75 << (
unsigned long)request->getContentLength()
76 << HTTPTypes::STRING_CRLF
77 << HTTPTypes::STRING_CRLF;
80 writer->writeNoCopy(REQUEST_HEADERS_TEXT);
81 writer->writeNoCopy(HTTPTypes::STRING_CRLF);
82 writer->writeNoCopy(HTTPTypes::STRING_CRLF);
83 std::for_each(request->getHeaders().begin(), request->getHeaders().end(),
84 boost::bind(&writeDictionaryTerm, writer, _1,
false));
85 writer->writeNoCopy(HTTPTypes::STRING_CRLF);
88 writer->writeNoCopy(QUERY_PARAMS_TEXT);
89 writer->writeNoCopy(HTTPTypes::STRING_CRLF);
90 writer->writeNoCopy(HTTPTypes::STRING_CRLF);
91 std::for_each(request->getQueryParams().begin(), request->getQueryParams().end(),
92 boost::bind(&writeDictionaryTerm, writer, _1,
true));
93 writer->writeNoCopy(HTTPTypes::STRING_CRLF);
96 writer->writeNoCopy(COOKIE_PARAMS_TEXT);
97 writer->writeNoCopy(HTTPTypes::STRING_CRLF);
98 writer->writeNoCopy(HTTPTypes::STRING_CRLF);
99 std::for_each(request->getCookieParams().begin(), request->getCookieParams().end(),
100 boost::bind(&writeDictionaryTerm, writer, _1,
false));
101 writer->writeNoCopy(HTTPTypes::STRING_CRLF);
104 writer->writeNoCopy(POST_CONTENT_TEXT);
105 writer->writeNoCopy(HTTPTypes::STRING_CRLF);
106 writer->writeNoCopy(HTTPTypes::STRING_CRLF);
107 if (request->getContentLength() != 0) {
108 writer->write(request->getContent(), request->getContentLength());
109 writer->writeNoCopy(HTTPTypes::STRING_CRLF);
110 writer->writeNoCopy(HTTPTypes::STRING_CRLF);
114 PionUserPtr user = request->getUser();
116 writer->writeNoCopy(USER_INFO_TEXT);
117 writer->writeNoCopy(HTTPTypes::STRING_CRLF);
118 writer->writeNoCopy(HTTPTypes::STRING_CRLF);
119 writer <<
"User authenticated, username: " << user->getUsername();
120 writer->writeNoCopy(HTTPTypes::STRING_CRLF);
static std::string url_decode(const std::string &str)
escapes URL-encoded strings (a%20value+with%20spaces)