From f6f5206b1ac2ba48660db7b6858e32612a6ffd05 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 5 Aug 2024 10:38:22 -0700 Subject: 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. --- src/unstable/protocolwebsocket.cpp | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) (limited to 'src/unstable/protocolwebsocket.cpp') 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() bool Bu::ProtocolWebSocket::stateHandshake() { - DEBUG( Bu::println("websocket: Begining handshake.") ); + DEBUG( Bu::println("websocket: Processing header line (stateHandshake).") ); Bu::String sLine; if( !readHttpHdrLine( sLine ) ) return false; + + DEBUG( Bu::println("Hdr: >>%1<<").arg( sLine ) ); if( sLine.getSize() == 0 ) { + DEBUG( Bu::println("websocket: Out of header lines, completing handshake.") ); if( !processHeaders() ) return false; onHandshakeComplete(); @@ -191,26 +194,39 @@ bool Bu::ProtocolWebSocket::stateHandshake() } hHeader.get( sKey ).append( sValue ); - DEBUG( Bu::println("Hdr: >>%1<<").arg( sLine ) ); - DEBUG( Bu::println("%1 = %2").arg( sKey ).arg( sValue ) ); + DEBUG( Bu::println(" -->%1 = %2").arg( sKey ).arg( sValue ) ); return true; } bool Bu::ProtocolWebSocket::readHttpHdrLine( Bu::String &sLine ) { - char buf[1024]; - int iSize = pClient->peek( buf, 1024 ); - for( int j = 0; j < iSize-1; j++ ) +#define BUF_SIZ 512 + char buf[BUF_SIZ]; + int iOffset = 0; + sLine.clear(); + for(;;) { - if( buf[j] == '\r' && buf[j+1] == '\n' ) + int iSize = pClient->peek( buf, BUF_SIZ, iOffset ); + DEBUG( Bu::println(" $$ readHttpHdrLine: peek found %1 bytes (%1 offset).").arg( iSize ).arg( iOffset ) ); + for( int j = 0; j < iSize-1; j++ ) { - pClient->seek(j+2); - sLine.set( buf, j ); - return true; + if( buf[j] == '\r' && buf[j+1] == '\n' ) + { + pClient->seek(iOffset+j+2); + sLine.append( buf, j ); + return true; + } } + + if( iSize < BUF_SIZ ) + return false; + + sLine.append( buf, iSize ); + iOffset += BUF_SIZ; } return false; +#undef BUF_SIZ } bool Bu::ProtocolWebSocket::processHeaders() -- cgit v1.2.3