pion-net  4.0.9
AllowNothingService.cpp
1 // ------------------------------------------------------------------
2 // pion-net: a C++ framework for building lightweight HTTP interfaces
3 // ------------------------------------------------------------------
4 // Copyright (C) 2007-2008 Atomic Labs, Inc. (http://www.atomiclabs.com)
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/PionConfig.hpp>
12 #include <pion/net/HTTPResponseWriter.hpp>
13 
14 using namespace pion;
15 using namespace pion::net;
16 
17 namespace pion { // begin namespace pion
18 namespace plugins { // begin namespace plugins
19 
20 
21 void AllowNothingService::operator()(HTTPRequestPtr& request, TCPConnectionPtr& tcp_conn)
22 {
23  static const std::string DENY_HTML = "<html><body>No, you can't.</body></html>";
24  HTTPResponseWriterPtr writer(HTTPResponseWriter::create(tcp_conn, *request,
25  boost::bind(&TCPConnection::finish, tcp_conn)));
26  writer->getResponse().setStatusCode(HTTPTypes::RESPONSE_CODE_METHOD_NOT_ALLOWED);
27  writer->getResponse().setStatusMessage(HTTPTypes::RESPONSE_MESSAGE_METHOD_NOT_ALLOWED);
28 
29  // This is a legitimate header, but it crashes when it's sent.
30  //writer->getResponse().addHeader("Allow", "");
31 
32  // Use this line to demonstrate that it's the empty header value that's causing the problem.
33  writer->getResponse().addHeader("Allow", "GET");
34 
35  writer->writeNoCopy(DENY_HTML);
36  writer->writeNoCopy(HTTPTypes::STRING_CRLF);
37  writer->writeNoCopy(HTTPTypes::STRING_CRLF);
38  writer->send();
39 }
40 
41 
42 } // end namespace plugins
43 } // end namespace pion
44 
45 
47 extern "C" PION_SERVICE_API pion::plugins::AllowNothingService *pion_create_AllowNothingService(void)
48 {
50 }
51 
53 extern "C" PION_SERVICE_API void pion_destroy_AllowNothingService(pion::plugins::AllowNothingService *service_ptr)
54 {
55  delete service_ptr;
56 }