OW_HTTPChunkedOStream.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2001-2004 Vintela, Inc. All rights reserved.
00003 *
00004 * Redistribution and use in source and binary forms, with or without
00005 * modification, are permitted provided that the following conditions are met:
00006 *
00007 *  - Redistributions of source code must retain the above copyright notice,
00008 *    this list of conditions and the following disclaimer.
00009 *
00010 *  - Redistributions in binary form must reproduce the above copyright notice,
00011 *    this list of conditions and the following disclaimer in the documentation
00012 *    and/or other materials provided with the distribution.
00013 *
00014 *  - Neither the name of Vintela, Inc. nor the names of its
00015 *    contributors may be used to endorse or promote products derived from this
00016 *    software without specific prior written permission.
00017 *
00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc. OR THE CONTRIBUTORS
00022 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00023 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00024 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00027 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00028 * POSSIBILITY OF SUCH DAMAGE.
00029 *******************************************************************************/
00030 
00036 #include "OW_config.h"
00037 #include "OW_HTTPChunkedOStream.hpp"
00038 
00039 namespace OW_NAMESPACE
00040 {
00041 
00042 using std::ostream;
00044 HTTPChunkedOStreamBuffer::HTTPChunkedOStreamBuffer(ostream& ostr)
00045    : BaseStreamBuffer(HTTP_BUF_SIZE, "out"), m_ostr(ostr)
00046 {
00047 }
00049 HTTPChunkedOStreamBuffer::~HTTPChunkedOStreamBuffer()
00050 {
00051    try
00052    {
00053       sync();
00054    }
00055    catch (...)
00056    {
00057       // don't let exceptions escape
00058    }
00059 }
00061 int
00062 HTTPChunkedOStreamBuffer::sync()
00063 {
00064    int rval = BaseStreamBuffer::sync();
00065    m_ostr.flush();
00066    return rval;
00067 }
00069 int
00070 HTTPChunkedOStreamBuffer::buffer_to_device(const char* c, int n)
00071 {
00072    if (n <= 0) // we don't ever want to write a 0 length here.
00073    {
00074       return 0;
00075    }
00076    m_ostr << std::hex << n << std::dec << "\r\n";
00077    m_ostr.write(c, n);
00078    m_ostr.write("\r\n", 2);
00079    if (m_ostr.good())
00080    {
00081       return 0;
00082    }
00083    else
00084    {
00085       return -1;
00086    }
00087 }
00090 HTTPChunkedOStream::HTTPChunkedOStream(ostream& ostr) :
00091    HTTPChunkedOStreamBase(ostr),
00092    std::basic_ostream<char, std::char_traits<char> >(&m_strbuf),
00093    m_ostr(ostr)
00094 {
00095 }
00097 HTTPChunkedOStream::~HTTPChunkedOStream()
00098 {
00099 }
00101 void
00102 HTTPChunkedOStream::termOutput(ESendLastChunkFlag sendLastChunk)
00103 {
00104    if (sendLastChunk == E_DISCARD_LAST_CHUNK)
00105    {
00106       // this will just erase everything in the stream buffer
00107       m_strbuf.initPutBuffer();
00108    }
00109    else
00110    {
00111       m_strbuf.BaseStreamBuffer::sync(); // don't call the derived one because it flushes to the stream, which isn't necessary at this point.
00112    }
00113 
00114    m_ostr << "0\r\n";
00115    for (size_t i = 0; i < m_trailers.size(); i++)
00116    {
00117       m_ostr << m_trailers[i] << "\r\n";
00118    }
00119    m_ostr << "\r\n";
00120    m_ostr.flush();
00121 
00122    // once the output is terminated, we have to reset the stream
00123    m_strbuf.initPutBuffer();
00124    m_trailers.clear();
00125 }
00127 void
00128 HTTPChunkedOStream::addTrailer(const String& key, const String& value)
00129 {
00130    String tmpKey = key;
00131    tmpKey.trim();
00132    if (!tmpKey.empty())
00133    {
00134       m_trailers.push_back(key + ": " + value);
00135    }
00136    else // A "folded" continuation line from previous key
00137    {
00138       m_trailers.push_back(" " + value);
00139    }
00140 }
00141 
00142 } // end namespace OW_NAMESPACE
00143 

Generated on Thu Feb 9 08:47:59 2006 for openwbem by  doxygen 1.4.6