reSIProcate/stack  9694
LazyParser.cxx
Go to the documentation of this file.
00001 #if defined(HAVE_CONFIG_H)
00002 #include "config.h"
00003 #endif
00004 
00005 
00006 #include <cassert>
00007 
00008 #include "resip/stack/Headers.hxx"
00009 #include "resip/stack/HeaderFieldValue.hxx"
00010 #include "resip/stack/LazyParser.hxx"
00011 #include "rutil/ParseBuffer.hxx"
00012 #include "rutil/WinLeakCheck.hxx"
00013 
00014 using namespace resip;
00015 
00016 LazyParser::LazyParser(const HeaderFieldValue& headerFieldValue)
00017    : mHeaderField(headerFieldValue, HeaderFieldValue::NoOwnership),
00018       mState(mHeaderField.getBuffer() == 0 ? DIRTY : NOT_PARSED)
00019 {
00020 }
00021 
00022 LazyParser::LazyParser(const HeaderFieldValue& headerFieldValue,
00023                         HeaderFieldValue::CopyPaddingEnum e)
00024    : mHeaderField(headerFieldValue, e), // Causes ownership to be taken. Oh well
00025       mState(mHeaderField.getBuffer() == 0 ? DIRTY : NOT_PARSED)
00026 {}
00027 
00028 LazyParser::LazyParser(const char* buf, int length) :
00029    mHeaderField(buf, length),
00030    mState(buf == 0 ? DIRTY : NOT_PARSED)
00031 {}
00032 
00033 LazyParser::LazyParser()
00034    : mHeaderField(),
00035       mState(DIRTY)
00036 {
00037 }
00038 
00039 LazyParser::LazyParser(const LazyParser& rhs)
00040    : mHeaderField((rhs.mState==DIRTY ? HeaderFieldValue::Empty : rhs.mHeaderField)), // Pretty cheap when rhs is DIRTY
00041       mState(rhs.mState)
00042 {}
00043 
00044 LazyParser::LazyParser(const LazyParser& rhs,HeaderFieldValue::CopyPaddingEnum e)
00045    :  mHeaderField((rhs.mState==DIRTY ? HeaderFieldValue::Empty : rhs.mHeaderField), e), // Pretty cheap when rhs is DIRTY
00046       mState(rhs.mState)
00047 {}
00048 
00049 
00050 LazyParser::~LazyParser()
00051 {
00052    clear();
00053 }
00054 
00055 LazyParser&
00056 LazyParser::operator=(const LazyParser& rhs)
00057 {
00058    assert( &rhs != 0 );
00059    
00060    if (this != &rhs)
00061    {
00062       clear();
00063       mState = rhs.mState;
00064       if (rhs.mState!=DIRTY)
00065       {
00066          mHeaderField=rhs.mHeaderField;
00067       }
00068    }
00069    return *this;
00070 }
00071 
00072 void
00073 LazyParser::doParse() const
00074 {
00075    LazyParser* ncThis = const_cast<LazyParser*>(this);
00076    // .bwc. We assume the worst, and if the parse succeeds, we update.
00077    ncThis->mState = MALFORMED;
00078    ParseBuffer pb(mHeaderField.getBuffer(), mHeaderField.getLength(), errorContext());
00079    ncThis->parse(pb);
00080    // .bwc. If we get this far without throwing, the parse has succeeded.
00081    ncThis->mState = WELL_FORMED;
00082 }
00083 
00084 bool
00085 LazyParser::isWellFormed() const
00086 {
00087    try
00088    {
00089       checkParsed();
00090    }
00091    catch(resip::ParseException&)
00092    {
00093    }
00094    
00095    return (mState!=MALFORMED);
00096 }
00097 
00098 void
00099 LazyParser::clear()
00100 {
00101    mHeaderField.clear();
00102 }
00103 
00104 EncodeStream&
00105 LazyParser::encode(EncodeStream& str) const
00106 {
00107    if (mState == DIRTY)
00108    {
00109       return encodeParsed(str);
00110    }
00111    else
00112    {
00113       mHeaderField.encode(str);
00114       return str;
00115    }
00116 }
00117 
00118 #ifndef  RESIP_USE_STL_STREAMS
00119 EncodeStream&
00120 resip::operator<<(EncodeStream&s, const LazyParser& lp)
00121 {
00122    lp.encode(s);
00123    return s; 
00124 }
00125 #endif
00126 
00127 std::ostream&
00128 resip::operator<<(std::ostream &s, const LazyParser& lp)
00129 {       
00130 #ifdef  RESIP_USE_STL_STREAMS
00131         lp.encode(s);
00132 #else
00133         //this should only be called for things like cout,cerr, or other streams not supporting
00134         //other stream encoders, aka MD5Stream
00135         Data data;
00136         DataStream stream(data);
00137 
00138         lp.encode(stream);
00139         stream.flush();
00140         s << data.c_str();
00141 #endif
00142    return s; 
00143 }
00144 /* ====================================================================
00145  * The Vovida Software License, Version 1.0 
00146  * 
00147  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00148  * 
00149  * Redistribution and use in source and binary forms, with or without
00150  * modification, are permitted provided that the following conditions
00151  * are met:
00152  * 
00153  * 1. Redistributions of source code must retain the above copyright
00154  *    notice, this list of conditions and the following disclaimer.
00155  * 
00156  * 2. Redistributions in binary form must reproduce the above copyright
00157  *    notice, this list of conditions and the following disclaimer in
00158  *    the documentation and/or other materials provided with the
00159  *    distribution.
00160  * 
00161  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00162  *    and "Vovida Open Communication Application Library (VOCAL)" must
00163  *    not be used to endorse or promote products derived from this
00164  *    software without prior written permission. For written
00165  *    permission, please contact vocal@vovida.org.
00166  *
00167  * 4. Products derived from this software may not be called "VOCAL", nor
00168  *    may "VOCAL" appear in their name, without prior written
00169  *    permission of Vovida Networks, Inc.
00170  * 
00171  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00172  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00173  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00174  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00175  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00176  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00177  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00178  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00179  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00180  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00181  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00182  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00183  * DAMAGE.
00184  * 
00185  * ====================================================================
00186  * 
00187  * This software consists of voluntary contributions made by Vovida
00188  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00189  * Inc.  For more information on Vovida Networks, Inc., please see
00190  * <http://www.vovida.org/>.
00191  *
00192  */