pion  5.0.6
CookieService.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 "CookieService.hpp"
11 #include <pion/algorithm.hpp>
12 #include <pion/http/response_writer.hpp>
13 
14 using namespace pion;
15 
16 namespace pion { // begin namespace pion
17 namespace plugins { // begin namespace plugins
18 
19 
20 // CookieService member functions
21 
23 void CookieService::operator()(const http::request_ptr& http_request_ptr, const tcp::connection_ptr& tcp_conn)
24 {
25  static const std::string HEADER_HTML = "<html>\n<head>\n<title>Cookie Service</title>\n"
26  "</head>\n<body>\n\n<h1>Cookie Service</h1>\n";
27  static const std::string FOOTER_HTML = "\n</body>\n</html>\n";
28 
29  // Set Content-type for HTML and write the header
30  http::response_writer_ptr writer(http::response_writer::create(tcp_conn, *http_request_ptr,
31  boost::bind(&tcp::connection::finish, tcp_conn)));
32  writer->get_response().set_content_type(http::types::CONTENT_TYPE_HTML);
33  writer->write_no_copy(HEADER_HTML);
34 
35  // Check if we have an action to perform
36  if (http_request_ptr->has_query("action")) {
37  if (http_request_ptr->get_query("action") == "Add Cookie") {
38  // add a new cookie
39  const std::string cookie_name(http_request_ptr->get_query("cookie_name"));
40  const std::string cookie_value(http_request_ptr->get_query("cookie_value"));
41  if (cookie_name.empty() || cookie_value.empty()) {
42  writer << "\n<p>[Error: You must specify a name and value to add a cookie]</p>\n\n";
43  } else {
44  writer->get_response().set_cookie(cookie_name, cookie_value);
45  writer << "\n<p>[Added cookie "
46  << cookie_name << '=' << cookie_value << "]</p>\n\n";
47  }
48  } else if (http_request_ptr->get_query("action") == "delete") {
49  const std::string cookie_name(http_request_ptr->get_query("cookie_name"));
50  if (cookie_name.empty()) {
51  writer << "\n<p>[Error: You must specify a name to delete a cookie]</p>\n\n";
52  } else {
53  writer->get_response().delete_cookie(cookie_name);
54  writer << "\n<p>[Deleted cookie " << cookie_name << "]</p>\n\n";
55  }
56  } else {
57  writer << "\n<p>[Error: Unrecognized action]</p>\n\n";
58  }
59  }
60 
61  // display cookie headers in request
62  if (http_request_ptr->has_header(http::types::HEADER_COOKIE)) {
63  writer << "\n<h2>Cookie Headers</h2>\n<ul>\n";
64  std::pair<ihash_multimap::const_iterator, ihash_multimap::const_iterator>
65  header_pair = http_request_ptr->get_headers().equal_range(http::types::HEADER_COOKIE);
66  for (ihash_multimap::const_iterator header_iterator = header_pair.first;
67  header_iterator != http_request_ptr->get_headers().end()
68  && header_iterator != header_pair.second; ++header_iterator)
69  {
70  writer << "<li>Cookie: " << header_iterator->second << "\n";
71  }
72  writer << "</ul>\n\n";
73  } else {
74  writer << "\n<h2>No Cookie Headers</h2>\n\n";
75  }
76 
77  // display existing cookies
78  ihash_multimap& cookie_params = http_request_ptr->get_cookies();
79  if (! cookie_params.empty()) {
80  writer << "\n<h2>Cookie Variables</h2>\n<ul>\n";
81  for (ihash_multimap::const_iterator i = cookie_params.begin();
82  i != cookie_params.end(); ++i)
83  {
84  writer << "<li>" << i->first << ": " << i->second
85  << " <a href=\"" << http_request_ptr->get_resource()
86  << "?action=delete&cookie_name=" << i->first
87  << "\">[Delete]</a>\n";
88  }
89  writer << "</ul>\n\n";
90  } else {
91  writer << "\n<h2>No Cookie Variables</h2>\n\n";
92  }
93 
94  // display form to add a cookie
95  writer << "\n<h2>Add Cookie</h2>\n"
96  "<p><form action=\"" << http_request_ptr->get_resource() << "\" method=\"POST\">\n"
97  "Name: <input type=\"text\" name=\"cookie_name\"><br />\n"
98  "Value: <input type=\"text\" name=\"cookie_value\"><br />\n"
99  "<input type=\"submit\" name=\"action\" value=\"Add Cookie\"></p>\n"
100  "</form>\n\n";
101 
102  // write the footer
103  writer->write_no_copy(FOOTER_HTML);
104 
105  // send the writer
106  writer->send();
107 }
108 
109 
110 } // end namespace plugins
111 } // end namespace pion
112 
113 
115 extern "C" PION_PLUGIN pion::plugins::CookieService *pion_create_CookieService(void)
116 {
117  return new pion::plugins::CookieService();
118 }
119 
121 extern "C" PION_PLUGIN void pion_destroy_CookieService(pion::plugins::CookieService *service_ptr)
122 {
123  delete service_ptr;
124 }
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())