pion  5.0.6
services/AllowNothingService.cpp
00001 // ---------------------------------------------------------------------
00002 // pion:  a Boost C++ framework for building lightweight HTTP interfaces
00003 // ---------------------------------------------------------------------
00004 // Copyright (C) 2007-2014 Splunk Inc.  (https://github.com/splunk/pion)
00005 //
00006 // Distributed under the Boost Software License, Version 1.0.
00007 // See http://www.boost.org/LICENSE_1_0.txt
00008 //
00009 
00010 #include "AllowNothingService.hpp"
00011 #include <pion/config.hpp>
00012 #include <pion/http/response_writer.hpp>
00013 
00014 using namespace pion;
00015 
00016 namespace pion {        // begin namespace pion
00017 namespace plugins {     // begin namespace plugins
00018 
00019     
00020 void AllowNothingService::operator()(http::request_ptr& http_request_ptr, tcp::connection_ptr& tcp_conn)
00021 {
00022     static const std::string DENY_HTML = "<html><body>No, you can't.</body></html>";
00023     http::response_writer_ptr writer(http::response_writer::create(tcp_conn, *http_request_ptr,
00024                                                             boost::bind(&tcp::connection::finish, tcp_conn)));
00025     writer->get_response().set_status_code(http::types::RESPONSE_CODE_METHOD_NOT_ALLOWED);
00026     writer->get_response().set_status_message(http::types::RESPONSE_MESSAGE_METHOD_NOT_ALLOWED);
00027 
00028     // This is a legitimate header, but it crashes when it's sent.
00029     //writer->get_response().add_header("Allow", "");
00030 
00031     // Use this line to demonstrate that it's the empty header value that's causing the problem.
00032     writer->get_response().add_header("Allow", "GET");
00033 
00034     writer->write_no_copy(DENY_HTML);
00035     writer->write_no_copy(http::types::STRING_CRLF);
00036     writer->write_no_copy(http::types::STRING_CRLF);
00037     writer->send();
00038 }
00039 
00040     
00041 }   // end namespace plugins
00042 }   // end namespace pion
00043 
00044 
00046 extern "C" PION_PLUGIN pion::plugins::AllowNothingService *pion_create_AllowNothingService(void)
00047 {
00048     return new pion::plugins::AllowNothingService();
00049 }
00050 
00052 extern "C" PION_PLUGIN void pion_destroy_AllowNothingService(pion::plugins::AllowNothingService *service_ptr)
00053 {
00054     delete service_ptr;
00055 }