/src/resiprocate/resip/stack/LazyParser.cxx
Line | Count | Source |
1 | | #if defined(HAVE_CONFIG_H) |
2 | | #include "config.h" |
3 | | #endif |
4 | | |
5 | | |
6 | | #include "rutil/ResipAssert.h" |
7 | | |
8 | | #include "resip/stack/Headers.hxx" |
9 | | #include "resip/stack/HeaderFieldValue.hxx" |
10 | | #include "resip/stack/LazyParser.hxx" |
11 | | #include "rutil/ParseBuffer.hxx" |
12 | | #include "rutil/WinLeakCheck.hxx" |
13 | | |
14 | | using namespace resip; |
15 | | |
16 | | LazyParser::LazyParser(const HeaderFieldValue& headerFieldValue) |
17 | 145k | : mHeaderField(headerFieldValue, HeaderFieldValue::NoOwnership), |
18 | 145k | mState(mHeaderField.getBuffer() == 0 ? DIRTY : NOT_PARSED) |
19 | 145k | { |
20 | 145k | } |
21 | | |
22 | | LazyParser::LazyParser(const HeaderFieldValue& headerFieldValue, |
23 | | HeaderFieldValue::CopyPaddingEnum e) |
24 | 0 | : mHeaderField(headerFieldValue, e), // Causes ownership to be taken. Oh well |
25 | 0 | mState(mHeaderField.getBuffer() == 0 ? DIRTY : NOT_PARSED) |
26 | 0 | {} |
27 | | |
28 | | LazyParser::LazyParser(const char* buf, int length) : |
29 | 570 | mHeaderField(buf, length), |
30 | 570 | mState(buf == 0 ? DIRTY : NOT_PARSED) |
31 | 570 | {} |
32 | | |
33 | | LazyParser::LazyParser() |
34 | 30.9k | : mHeaderField(), |
35 | 30.9k | mState(DIRTY) |
36 | 30.9k | { |
37 | 30.9k | } |
38 | | |
39 | | LazyParser::LazyParser(const LazyParser& rhs) |
40 | 72.7k | : mHeaderField((rhs.mState==DIRTY ? HeaderFieldValue::Empty : rhs.mHeaderField)), // Pretty cheap when rhs is DIRTY |
41 | 72.7k | mState(rhs.mState) |
42 | 72.7k | {} |
43 | | |
44 | | LazyParser::LazyParser(const LazyParser& rhs,HeaderFieldValue::CopyPaddingEnum e) |
45 | 0 | : mHeaderField((rhs.mState==DIRTY ? HeaderFieldValue::Empty : rhs.mHeaderField), e), // Pretty cheap when rhs is DIRTY |
46 | 0 | mState(rhs.mState) |
47 | 0 | {} |
48 | | |
49 | | |
50 | | LazyParser::~LazyParser() |
51 | 249k | { |
52 | 249k | clear(); |
53 | 249k | } |
54 | | |
55 | | LazyParser& |
56 | | LazyParser::operator=(const LazyParser& rhs) |
57 | 5.68k | { |
58 | 5.68k | resip_assert_not_null( &rhs ); |
59 | | |
60 | 5.68k | if (this != &rhs) |
61 | 5.68k | { |
62 | 5.68k | clear(); |
63 | 5.68k | mState = rhs.mState; |
64 | 5.68k | if (rhs.mState!=DIRTY) |
65 | 0 | { |
66 | 0 | mHeaderField=rhs.mHeaderField; |
67 | 0 | } |
68 | 5.68k | } |
69 | 5.68k | return *this; |
70 | 5.68k | } |
71 | | |
72 | | void |
73 | | LazyParser::doParse() const |
74 | 145k | { |
75 | 145k | LazyParser* ncThis = const_cast<LazyParser*>(this); |
76 | | // .bwc. We assume the worst, and if the parse succeeds, we update. |
77 | 145k | ncThis->mState = MALFORMED; |
78 | 145k | ParseBuffer pb(mHeaderField.getBuffer(), mHeaderField.getLength(), errorContext()); |
79 | 145k | ncThis->parse(pb); |
80 | | // .bwc. If we get this far without throwing, the parse has succeeded. |
81 | 145k | ncThis->mState = WELL_FORMED; |
82 | 145k | } |
83 | | |
84 | | bool |
85 | | LazyParser::isWellFormed() const |
86 | 0 | { |
87 | 0 | try |
88 | 0 | { |
89 | 0 | checkParsed(); |
90 | 0 | } |
91 | 0 | catch(resip::ParseException&) |
92 | 0 | { |
93 | 0 | } |
94 | | |
95 | 0 | return (mState!=MALFORMED); |
96 | 0 | } |
97 | | |
98 | | void |
99 | | LazyParser::clear() |
100 | 437k | { |
101 | 437k | mHeaderField.clear(); |
102 | 437k | } |
103 | | |
104 | | EncodeStream& |
105 | | LazyParser::encode(EncodeStream& str) const |
106 | 0 | { |
107 | 0 | if (mState == DIRTY) |
108 | 0 | { |
109 | 0 | return encodeParsed(str); |
110 | 0 | } |
111 | 0 | else |
112 | 0 | { |
113 | 0 | mHeaderField.encode(str); |
114 | 0 | return str; |
115 | 0 | } |
116 | 0 | } |
117 | | |
118 | | #ifndef RESIP_USE_STL_STREAMS |
119 | | EncodeStream& |
120 | | resip::operator<<(EncodeStream&s, const LazyParser& lp) |
121 | | { |
122 | | lp.encode(s); |
123 | | return s; |
124 | | } |
125 | | #endif |
126 | | |
127 | | std::ostream& |
128 | | resip::operator<<(std::ostream &s, const LazyParser& lp) |
129 | 0 | { |
130 | 0 | #ifdef RESIP_USE_STL_STREAMS |
131 | 0 | lp.encode(s); |
132 | | #else |
133 | | //this should only be called for things like cout,cerr, or other streams not supporting |
134 | | //other stream encoders, aka MD5Stream |
135 | | Data data; |
136 | | DataStream stream(data); |
137 | | |
138 | | lp.encode(stream); |
139 | | stream.flush(); |
140 | | s << data.c_str(); |
141 | | #endif |
142 | 0 | return s; |
143 | 0 | } |
144 | | /* ==================================================================== |
145 | | * The Vovida Software License, Version 1.0 |
146 | | * |
147 | | * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved. |
148 | | * |
149 | | * Redistribution and use in source and binary forms, with or without |
150 | | * modification, are permitted provided that the following conditions |
151 | | * are met: |
152 | | * |
153 | | * 1. Redistributions of source code must retain the above copyright |
154 | | * notice, this list of conditions and the following disclaimer. |
155 | | * |
156 | | * 2. Redistributions in binary form must reproduce the above copyright |
157 | | * notice, this list of conditions and the following disclaimer in |
158 | | * the documentation and/or other materials provided with the |
159 | | * distribution. |
160 | | * |
161 | | * 3. The names "VOCAL", "Vovida Open Communication Application Library", |
162 | | * and "Vovida Open Communication Application Library (VOCAL)" must |
163 | | * not be used to endorse or promote products derived from this |
164 | | * software without prior written permission. For written |
165 | | * permission, please contact vocal@vovida.org. |
166 | | * |
167 | | * 4. Products derived from this software may not be called "VOCAL", nor |
168 | | * may "VOCAL" appear in their name, without prior written |
169 | | * permission of Vovida Networks, Inc. |
170 | | * |
171 | | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED |
172 | | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
173 | | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND |
174 | | * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA |
175 | | * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES |
176 | | * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL, |
177 | | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
178 | | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
179 | | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
180 | | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
181 | | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
182 | | * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
183 | | * DAMAGE. |
184 | | * |
185 | | * ==================================================================== |
186 | | * |
187 | | * This software consists of voluntary contributions made by Vovida |
188 | | * Networks, Inc. and many individuals on behalf of Vovida Networks, |
189 | | * Inc. For more information on Vovida Networks, Inc., please see |
190 | | * <http://www.vovida.org/>. |
191 | | * |
192 | | */ |