# net/services/AllowNothingService.cpp

    00001 // ------------------------------------------------------------------
    00002 // pion-net: a C++ framework for building lightweight HTTP interfaces
    00003 // ------------------------------------------------------------------
    00004 // Copyright (C) 2007-2008 Atomic Labs, Inc.  (http://www.atomiclabs.com)
    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/PionConfig.hpp>
    00012 #include <pion/net/HTTPResponseWriter.hpp>
    00013
    00014 using namespace pion;
    00015 using namespace pion::net;
    00016
    00017 namespace pion {        // begin namespace pion
    00018 namespace plugins {     // begin namespace plugins
    00019
    00020
    00021 void AllowNothingService::operator()(HTTPRequestPtr& request, TCPConnectionPtr& tcp_conn)
    00022 {
    00023     static const std::string DENY_HTML = "<html><body>No, you can't.</body></html>";
    00024     HTTPResponseWriterPtr writer(HTTPResponseWriter::create(tcp_conn, *request,
    00025                                                             boost::bind(&TCPConnection::finish, tcp_conn)));
    00026     writer->getResponse().setStatusCode(HTTPTypes::RESPONSE_CODE_METHOD_NOT_ALLOWED);
    00027     writer->getResponse().setStatusMessage(HTTPTypes::RESPONSE_MESSAGE_METHOD_NOT_ALLOWED);
    00028
    00029     // This is a legitimate header, but it crashes when it's sent.
    00030     //writer->getResponse().addHeader("Allow", "");
    00031
    00032     // Use this line to demonstrate that it's the empty header value that's causing the problem.
    00033     writer->getResponse().addHeader("Allow", "GET");
    00034
    00035     writer->writeNoCopy(DENY_HTML);
    00036     writer->writeNoCopy(HTTPTypes::STRING_CRLF);
    00037     writer->writeNoCopy(HTTPTypes::STRING_CRLF);
    00038     writer->send();
    00039 }
    00040
    00041
    00042 }   // end namespace plugins
    00043 }   // end namespace pion
    00044
    00045
    00047 extern "C" PION_SERVICE_API pion::plugins::AllowNothingService *pion_create_AllowNothingService(void)
    00048 {
    00049     return new pion::plugins::AllowNothingService();
    00050 }
    00051
    00053 extern "C" PION_SERVICE_API void pion_destroy_AllowNothingService(pion::plugins::AllowNothingService *service_ptr)
    00054 {
    00055     delete service_ptr;
    00056 }