pion  5.0.6
AllowNothingService.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 "AllowNothingService.hpp"
11 #include <pion/config.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 void AllowNothingService::operator()(http::request_ptr& http_request_ptr, tcp::connection_ptr& tcp_conn)
21 {
22  static const std::string DENY_HTML = "<html><body>No, you can't.</body></html>";
23  http::response_writer_ptr writer(http::response_writer::create(tcp_conn, *http_request_ptr,
24  boost::bind(&tcp::connection::finish, tcp_conn)));
25  writer->get_response().set_status_code(http::types::RESPONSE_CODE_METHOD_NOT_ALLOWED);
26  writer->get_response().set_status_message(http::types::RESPONSE_MESSAGE_METHOD_NOT_ALLOWED);
27 
28  // This is a legitimate header, but it crashes when it's sent.
29  //writer->get_response().add_header("Allow", "");
30 
31  // Use this line to demonstrate that it's the empty header value that's causing the problem.
32  writer->get_response().add_header("Allow", "GET");
33 
34  writer->write_no_copy(DENY_HTML);
35  writer->write_no_copy(http::types::STRING_CRLF);
36  writer->write_no_copy(http::types::STRING_CRLF);
37  writer->send();
38 }
39 
40 
41 } // end namespace plugins
42 } // end namespace pion
43 
44 
46 extern "C" PION_PLUGIN pion::plugins::AllowNothingService *pion_create_AllowNothingService(void)
47 {
49 }
50 
52 extern "C" PION_PLUGIN void pion_destroy_AllowNothingService(pion::plugins::AllowNothingService *service_ptr)
53 {
54  delete service_ptr;
55 }
virtual void operator()(pion::http::request_ptr &http_request_ptr, pion::tcp::connection_ptr &tcp_conn)
static boost::shared_ptr< response_writer > create(tcp::connection_ptr &tcp_conn, http::response_ptr &http_response_ptr, finished_handler_t handler=finished_handler_t())