00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 #include "CookieService.hpp"
00011 #include <pion/algorithm.hpp>
00012 #include <pion/http/response_writer.hpp>
00013 
00014 using namespace pion;
00015 
00016 namespace pion {        
00017 namespace plugins {     
00018 
00019     
00020 
00021 
00023 void CookieService::operator()(http::request_ptr& http_request_ptr, tcp::connection_ptr& tcp_conn)
00024 {
00025     static const std::string HEADER_HTML = "<html>\n<head>\n<title>Cookie Service</title>\n"
00026         "</head>\n<body>\n\n<h1>Cookie Service</h1>\n";
00027     static const std::string FOOTER_HTML = "\n</body>\n</html>\n";
00028 
00029     
00030     http::response_writer_ptr writer(http::response_writer::create(tcp_conn, *http_request_ptr,
00031                                                             boost::bind(&tcp::connection::finish, tcp_conn)));
00032     writer->get_response().set_content_type(http::types::CONTENT_TYPE_HTML);
00033     writer->write_no_copy(HEADER_HTML);
00034 
00035     
00036     if (http_request_ptr->has_query("action")) {
00037         if (http_request_ptr->get_query("action") == "Add Cookie") {
00038             
00039             const std::string cookie_name(http_request_ptr->get_query("cookie_name"));
00040             const std::string cookie_value(http_request_ptr->get_query("cookie_value"));
00041             if (cookie_name.empty() || cookie_value.empty()) {
00042                 writer << "\n<p>[Error: You must specify a name and value to add a cookie]</p>\n\n";
00043             } else {
00044                 writer->get_response().set_cookie(cookie_name, cookie_value);
00045                 writer << "\n<p>[Added cookie "
00046                     << cookie_name << '=' << cookie_value << "]</p>\n\n";
00047             }
00048         } else if (http_request_ptr->get_query("action") == "delete") {
00049             const std::string cookie_name(http_request_ptr->get_query("cookie_name"));
00050             if (cookie_name.empty()) {
00051                 writer << "\n<p>[Error: You must specify a name to delete a cookie]</p>\n\n";
00052             } else {
00053                 writer->get_response().delete_cookie(cookie_name);
00054                 writer << "\n<p>[Deleted cookie " << cookie_name << "]</p>\n\n";
00055             }
00056         } else {
00057             writer << "\n<p>[Error: Unrecognized action]</p>\n\n";
00058         }
00059     }
00060     
00061     
00062     if (http_request_ptr->has_header(http::types::HEADER_COOKIE)) {
00063         writer << "\n<h2>Cookie Headers</h2>\n<ul>\n";
00064         std::pair<ihash_multimap::const_iterator, ihash_multimap::const_iterator>
00065             header_pair = http_request_ptr->get_headers().equal_range(http::types::HEADER_COOKIE);
00066         for (ihash_multimap::const_iterator header_iterator = header_pair.first;
00067              header_iterator != http_request_ptr->get_headers().end()
00068              && header_iterator != header_pair.second; ++header_iterator)
00069         {
00070             writer << "<li>Cookie: " << header_iterator->second << "\n";
00071         }
00072         writer << "</ul>\n\n";
00073     } else {
00074         writer << "\n<h2>No Cookie Headers</h2>\n\n";
00075     }
00076     
00077     
00078     ihash_multimap& cookie_params = http_request_ptr->get_cookies();
00079     if (! cookie_params.empty()) {
00080         writer << "\n<h2>Cookie Variables</h2>\n<ul>\n";
00081         for (ihash_multimap::const_iterator i = cookie_params.begin();
00082              i != cookie_params.end(); ++i)
00083         {
00084             writer << "<li>" << i->first << ": " << i->second
00085                 << " <a href=\"" << http_request_ptr->get_resource()
00086                 << "?action=delete&cookie_name=" << i->first
00087                 << "\">[Delete]</a>\n";
00088         }
00089         writer << "</ul>\n\n";
00090     } else {
00091         writer << "\n<h2>No Cookie Variables</h2>\n\n";
00092     }
00093 
00094     
00095     writer << "\n<h2>Add Cookie</h2>\n"
00096         "<p><form action=\"" << http_request_ptr->get_resource() << "\" method=\"POST\">\n"
00097         "Name: <input type=\"text\" name=\"cookie_name\"><br />\n"
00098         "Value: <input type=\"text\" name=\"cookie_value\"><br />\n"
00099         "<input type=\"submit\" name=\"action\" value=\"Add Cookie\"></p>\n"
00100         "</form>\n\n";
00101     
00102     
00103     writer->write_no_copy(FOOTER_HTML);
00104     
00105     
00106     writer->send();
00107 }
00108 
00109 
00110 }   
00111 }   
00112 
00113 
00115 extern "C" PION_PLUGIN pion::plugins::CookieService *pion_create_CookieService(void)
00116 {
00117     return new pion::plugins::CookieService();
00118 }
00119 
00121 extern "C" PION_PLUGIN void pion_destroy_CookieService(pion::plugins::CookieService *service_ptr)
00122 {
00123     delete service_ptr;
00124 }