pion  5.0.6
EchoService.cpp
1 // ---------------------------------------------------------------------
2 // pion: a Boost C++ framework for building lightweight HTTP interfaces
3 // ---------------------------------------------------------------------
4 // Copyright (C) 2007-2014 Splunk Inc. (https://github.com/splunk/pion)
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // See http://www.boost.org/LICENSE_1_0.txt
8 //
9 
10 #include "EchoService.hpp"
11 #include <boost/bind.hpp>
12 #include <pion/algorithm.hpp>
13 #include <pion/http/response_writer.hpp>
14 #include <pion/user.hpp>
15 
16 using namespace pion;
17 
18 namespace pion { // begin namespace pion
19 namespace plugins { // begin namespace plugins
20 
21 
23 void writeDictionaryTerm(http::response_writer_ptr& writer,
24  const ihash_multimap::value_type& val)
25 {
26  // text is copied into writer text cache
27  writer << val.first << http::types::HEADER_NAME_VALUE_DELIMITER
28  << val.second
29  << http::types::STRING_CRLF;
30 }
31 
32 
33 // EchoService member functions
34 
36 void EchoService::operator()(const http::request_ptr& http_request_ptr, const tcp::connection_ptr& tcp_conn)
37 {
38  // this web service uses static text to test the mixture of "copied" with
39  // "static" (no-copy) text
40  static const std::string REQUEST_ECHO_TEXT("[Request Echo]");
41  static const std::string REQUEST_HEADERS_TEXT("[Request Headers]");
42  static const std::string QUERY_PARAMS_TEXT("[Query Parameters]");
43  static const std::string COOKIE_PARAMS_TEXT("[Cookie Parameters]");
44  static const std::string POST_CONTENT_TEXT("[POST Content]");
45  static const std::string USER_INFO_TEXT("[USER Info]");
46 
47  // Set Content-type to "text/plain" (plain ascii text)
48  http::response_writer_ptr writer(http::response_writer::create(tcp_conn, *http_request_ptr,
49  boost::bind(&tcp::connection::finish, tcp_conn)));
50  writer->get_response().set_content_type(http::types::CONTENT_TYPE_TEXT);
51 
52  // write request information
53  writer->write_no_copy(REQUEST_ECHO_TEXT);
54  writer->write_no_copy(http::types::STRING_CRLF);
55  writer->write_no_copy(http::types::STRING_CRLF);
56  writer
57  << "Request method: "
58  << http_request_ptr->get_method()
59  << http::types::STRING_CRLF
60  << "Resource originally requested: "
61  << http_request_ptr->get_original_resource()
62  << http::types::STRING_CRLF
63  << "Resource delivered: "
64  << http_request_ptr->get_resource()
65  << http::types::STRING_CRLF
66  << "Query string: "
67  << http_request_ptr->get_query_string()
68  << http::types::STRING_CRLF
69  << "HTTP version: "
70  << http_request_ptr->get_version_major() << '.' << http_request_ptr->get_version_minor()
71  << http::types::STRING_CRLF
72  << "Content length: "
73  << (unsigned long)http_request_ptr->get_content_length()
74  << http::types::STRING_CRLF
75  << http::types::STRING_CRLF;
76 
77  // write request headers
78  writer->write_no_copy(REQUEST_HEADERS_TEXT);
79  writer->write_no_copy(http::types::STRING_CRLF);
80  writer->write_no_copy(http::types::STRING_CRLF);
81  std::for_each(http_request_ptr->get_headers().begin(), http_request_ptr->get_headers().end(),
82  boost::bind(&writeDictionaryTerm, writer, _1));
83  writer->write_no_copy(http::types::STRING_CRLF);
84 
85  // write query parameters
86  writer->write_no_copy(QUERY_PARAMS_TEXT);
87  writer->write_no_copy(http::types::STRING_CRLF);
88  writer->write_no_copy(http::types::STRING_CRLF);
89  std::for_each(http_request_ptr->get_queries().begin(), http_request_ptr->get_queries().end(),
90  boost::bind(&writeDictionaryTerm, writer, _1));
91  writer->write_no_copy(http::types::STRING_CRLF);
92 
93  // write cookie parameters
94  writer->write_no_copy(COOKIE_PARAMS_TEXT);
95  writer->write_no_copy(http::types::STRING_CRLF);
96  writer->write_no_copy(http::types::STRING_CRLF);
97  std::for_each(http_request_ptr->get_cookies().begin(), http_request_ptr->get_cookies().end(),
98  boost::bind(&writeDictionaryTerm, writer, _1));
99  writer->write_no_copy(http::types::STRING_CRLF);
100 
101  // write POST content
102  writer->write_no_copy(POST_CONTENT_TEXT);
103  writer->write_no_copy(http::types::STRING_CRLF);
104  writer->write_no_copy(http::types::STRING_CRLF);
105  if (http_request_ptr->get_content_length() != 0) {
106  writer->write(http_request_ptr->get_content(), http_request_ptr->get_content_length());
107  writer->write_no_copy(http::types::STRING_CRLF);
108  writer->write_no_copy(http::types::STRING_CRLF);
109  }
110 
111  // if authenticated, write user info
112  user_ptr user = http_request_ptr->get_user();
113  if (user) {
114  writer->write_no_copy(USER_INFO_TEXT);
115  writer->write_no_copy(http::types::STRING_CRLF);
116  writer->write_no_copy(http::types::STRING_CRLF);
117  writer << "User authenticated, username: " << user->get_username();
118  writer->write_no_copy(http::types::STRING_CRLF);
119  }
120 
121  // send the writer
122  writer->send();
123 }
124 
125 
126 } // end namespace plugins
127 } // end namespace pion
128 
129 
131 extern "C" PION_PLUGIN pion::plugins::EchoService *pion_create_EchoService(void)
132 {
133  return new pion::plugins::EchoService();
134 }
135 
137 extern "C" PION_PLUGIN void pion_destroy_EchoService(pion::plugins::EchoService *service_ptr)
138 {
139  delete service_ptr;
140 }
static boost::shared_ptr< response_writer > create(const tcp::connection_ptr &tcp_conn, const http::response_ptr &http_response_ptr, finished_handler_t handler=finished_handler_t())
std::string const & get_username() const
returns user name as a string
Definition: user.hpp:66
virtual void operator()(const pion::http::request_ptr &http_request_ptr, const pion::tcp::connection_ptr &tcp_conn)
handles requests for EchoService
Definition: EchoService.cpp:36