diff options
author | Mike Buland <mike@xagasoft.com> | 2024-08-05 10:38:22 -0700 |
---|---|---|
committer | Mike Buland <mike@xagasoft.com> | 2024-08-05 10:38:22 -0700 |
commit | f6f5206b1ac2ba48660db7b6858e32612a6ffd05 (patch) | |
tree | 13de4c170cebefd4bc8678ea71b2c0e2602b65de | |
parent | bab5a53403fc4d89b0c61ccd6ce901c17fd379da (diff) | |
download | libbu++-f6f5206b1ac2ba48660db7b6858e32612a6ffd05.tar.gz libbu++-f6f5206b1ac2ba48660db7b6858e32612a6ffd05.tar.bz2 libbu++-f6f5206b1ac2ba48660db7b6858e32612a6ffd05.tar.xz libbu++-f6f5206b1ac2ba48660db7b6858e32612a6ffd05.zip |
Fixed buffer issue reading headers.
We were only reading 1k of data in before, but if a header was more than
1k then we just would give up. Now we read as much as we can that has
loaded into the buffer and if we run out we can try again, but if it's
just a very long header we'll deal with it properly still.
Diffstat (limited to '')
-rw-r--r-- | src/unstable/protocolwebsocket.cpp | 36 |
1 files changed, 26 insertions, 10 deletions
diff --git a/src/unstable/protocolwebsocket.cpp b/src/unstable/protocolwebsocket.cpp index 756c177..3c0d091 100644 --- a/src/unstable/protocolwebsocket.cpp +++ b/src/unstable/protocolwebsocket.cpp | |||
@@ -158,14 +158,17 @@ bool Bu::ProtocolWebSocket::stateProtoId() | |||
158 | 158 | ||
159 | bool Bu::ProtocolWebSocket::stateHandshake() | 159 | bool Bu::ProtocolWebSocket::stateHandshake() |
160 | { | 160 | { |
161 | DEBUG( Bu::println("websocket: Begining handshake.") ); | 161 | DEBUG( Bu::println("websocket: Processing header line (stateHandshake).") ); |
162 | 162 | ||
163 | Bu::String sLine; | 163 | Bu::String sLine; |
164 | if( !readHttpHdrLine( sLine ) ) | 164 | if( !readHttpHdrLine( sLine ) ) |
165 | return false; | 165 | return false; |
166 | |||
167 | DEBUG( Bu::println("Hdr: >>%1<<").arg( sLine ) ); | ||
166 | 168 | ||
167 | if( sLine.getSize() == 0 ) | 169 | if( sLine.getSize() == 0 ) |
168 | { | 170 | { |
171 | DEBUG( Bu::println("websocket: Out of header lines, completing handshake.") ); | ||
169 | if( !processHeaders() ) | 172 | if( !processHeaders() ) |
170 | return false; | 173 | return false; |
171 | onHandshakeComplete(); | 174 | onHandshakeComplete(); |
@@ -191,26 +194,39 @@ bool Bu::ProtocolWebSocket::stateHandshake() | |||
191 | } | 194 | } |
192 | hHeader.get( sKey ).append( sValue ); | 195 | hHeader.get( sKey ).append( sValue ); |
193 | 196 | ||
194 | DEBUG( Bu::println("Hdr: >>%1<<").arg( sLine ) ); | 197 | DEBUG( Bu::println(" -->%1 = %2").arg( sKey ).arg( sValue ) ); |
195 | DEBUG( Bu::println("%1 = %2").arg( sKey ).arg( sValue ) ); | ||
196 | 198 | ||
197 | return true; | 199 | return true; |
198 | } | 200 | } |
199 | 201 | ||
200 | bool Bu::ProtocolWebSocket::readHttpHdrLine( Bu::String &sLine ) | 202 | bool Bu::ProtocolWebSocket::readHttpHdrLine( Bu::String &sLine ) |
201 | { | 203 | { |
202 | char buf[1024]; | 204 | #define BUF_SIZ 512 |
203 | int iSize = pClient->peek( buf, 1024 ); | 205 | char buf[BUF_SIZ]; |
204 | for( int j = 0; j < iSize-1; j++ ) | 206 | int iOffset = 0; |
207 | sLine.clear(); | ||
208 | for(;;) | ||
205 | { | 209 | { |
206 | if( buf[j] == '\r' && buf[j+1] == '\n' ) | 210 | int iSize = pClient->peek( buf, BUF_SIZ, iOffset ); |
211 | DEBUG( Bu::println(" $$ readHttpHdrLine: peek found %1 bytes (%1 offset).").arg( iSize ).arg( iOffset ) ); | ||
212 | for( int j = 0; j < iSize-1; j++ ) | ||
207 | { | 213 | { |
208 | pClient->seek(j+2); | 214 | if( buf[j] == '\r' && buf[j+1] == '\n' ) |
209 | sLine.set( buf, j ); | 215 | { |
210 | return true; | 216 | pClient->seek(iOffset+j+2); |
217 | sLine.append( buf, j ); | ||
218 | return true; | ||
219 | } | ||
211 | } | 220 | } |
221 | |||
222 | if( iSize < BUF_SIZ ) | ||
223 | return false; | ||
224 | |||
225 | sLine.append( buf, iSize ); | ||
226 | iOffset += BUF_SIZ; | ||
212 | } | 227 | } |
213 | return false; | 228 | return false; |
229 | #undef BUF_SIZ | ||
214 | } | 230 | } |
215 | 231 | ||
216 | bool Bu::ProtocolWebSocket::processHeaders() | 232 | bool Bu::ProtocolWebSocket::processHeaders() |