OW_HTTPDeflateIStream.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 #ifdef OW_HAVE_ZLIB_H
00038 #include "OW_HTTPDeflateIStream.hpp"
00039 #include "OW_HTTPException.hpp"
00040 
00041 namespace OW_NAMESPACE
00042 {
00043 
00044 using std::istream;
00045 HTTPDeflateIStreamBuffer::HTTPDeflateIStreamBuffer(istream& istr)
00046    : BaseStreamBuffer(HTTP_BUF_SIZE, "in")
00047    , m_istr(istr)
00048 {
00049    m_zstr.opaque = Z_NULL;
00050    m_zstr.zfree = Z_NULL;
00051    m_zstr.zalloc = Z_NULL;
00052    m_zstr.avail_in = 0;
00053    int rval = inflateInit(&m_zstr);
00054    if (rval != Z_OK)
00055    {
00056       String msg = "Error: inflateInit returned " + String(rval);
00057       if (m_zstr.msg)
00058       {
00059          msg += String(": ") + String(m_zstr.msg);
00060       }
00061       OW_THROW(HTTPException, msg.c_str());
00062    }
00063 }
00065 HTTPDeflateIStreamBuffer::~HTTPDeflateIStreamBuffer()
00066 {
00067    inflateEnd(&m_zstr);
00068 }
00070 int
00071 HTTPDeflateIStreamBuffer::buffer_from_device(char* c, int n)
00072 {
00073    if (n < 1)
00074    {
00075       return 0;
00076    }
00077    m_zstr.avail_out = n;
00078    m_zstr.next_out = reinterpret_cast<Bytef*>(c);
00079    int bytesRead = 0;
00080    while (m_zstr.avail_out > 0)
00081    {
00082       if (m_zstr.avail_in == 0)
00083       {
00084          if (m_istr)
00085          {
00086             m_istr.read(reinterpret_cast<char*>(m_inBuf), m_inBufSize);
00087             m_zstr.avail_in = m_istr.gcount();
00088             m_zstr.next_in = m_inBuf;
00089          }
00090       }
00091       int rv = inflate(&m_zstr, Z_SYNC_FLUSH);
00092       bytesRead = n - m_zstr.avail_out;
00093       if (rv != Z_OK)
00094       {
00095          break;
00096       }
00097    }
00098    if (bytesRead > 0)
00099    {
00100       return bytesRead;
00101    }
00102    else
00103    {
00104       return -1;
00105    }
00106    /*
00107    if (!m_istr)
00108    {
00109       return -1;
00110    }
00111    m_istr.read(m_inBuf, n);
00112    int bytesRead = m_istr.gcount();
00113    m_zstr.next_in = m_inBuf;
00114    m_zstr.avail_in = bytesRead;
00115    m_zstr.next_out = (Bytef*)c;  
00116    m_zstr.avail_out = n;
00117    int rv = inflate(&m_zstr, Z_SYNC_FLUSH);
00118    if (rv != Z_OK
00119        && rv != Z_STREAM_END)
00120    {
00121       return -1;
00122    }
00123    return (n - m_zstr.avail_out);
00124    */
00125 }
00127 HTTPDeflateIStream::HTTPDeflateIStream(
00128          const CIMProtocolIStreamIFCRef& istr)
00129    : HTTPDeflateIStreamBase(*istr)
00130    , CIMProtocolIStreamIFC(&m_strbuf)
00131    , m_istr(istr)
00132 {
00133 }
00134 
00136 // TODO: Move all this knowledge about CIM and specific trailers into HTTPClient
00137 void HTTPDeflateIStream::checkForError() const
00138 {
00139    m_istr->checkForError();
00140 }
00141 
00143 CIMProtocolIStreamIFCRef
00144 HTTPDeflateIStream::getInputStreamOrig()
00145 {
00146    return m_istr;
00147 }
00148 
00149 } // end namespace OW_NAMESPACE
00150 
00151 #endif // #ifdef OW_HAVE_ZLIB_H
00152 

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