aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Buland <mbuland@penny-arcade.com>2021-10-04 11:42:36 -0700
committerMike Buland <mbuland@penny-arcade.com>2021-10-04 11:42:36 -0700
commit4e91dc4f2d6eba2e66dd5b59445abcc778117c60 (patch)
tree4d8cd4e8016e222f5eccdf26674a097da3f7fb06
parent5cda6d3a1622167c5fed35bcd3a02a031ef7a86d (diff)
downloadlibbu++-4e91dc4f2d6eba2e66dd5b59445abcc778117c60.tar.gz
libbu++-4e91dc4f2d6eba2e66dd5b59445abcc778117c60.tar.bz2
libbu++-4e91dc4f2d6eba2e66dd5b59445abcc778117c60.tar.xz
libbu++-4e91dc4f2d6eba2e66dd5b59445abcc778117c60.zip
Added handler to check/modify http headers.
This is an extra call that happens during the http handshake that allows the implementation to set additional headers, modify existing headers, and cancel the whole connection if desired.
-rw-r--r--src/unstable/protocolwebsocket.cpp29
-rw-r--r--src/unstable/protocolwebsocket.h6
2 files changed, 29 insertions, 6 deletions
diff --git a/src/unstable/protocolwebsocket.cpp b/src/unstable/protocolwebsocket.cpp
index 8d4a124..08ac176 100644
--- a/src/unstable/protocolwebsocket.cpp
+++ b/src/unstable/protocolwebsocket.cpp
@@ -66,6 +66,11 @@ void Bu::ProtocolWebSocket::onNewData( Bu::Client * /*pClient*/ )
66 } 66 }
67} 67}
68 68
69bool onProcessHeaders( Bu::StringList & /*lHeadersOut*/ )
70{
71 return true;
72}
73
69void Bu::ProtocolWebSocket::writeMessage( const Bu::String &sData, 74void Bu::ProtocolWebSocket::writeMessage( const Bu::String &sData,
70 Bu::ProtocolWebSocket::Operation eOp ) 75 Bu::ProtocolWebSocket::Operation eOp )
71{ 76{
@@ -237,12 +242,24 @@ bool Bu::ProtocolWebSocket::processHeaders()
237 242
238 DEBUG( Bu::println("accept: %1").arg( mbOut.getString() ) ); 243 DEBUG( Bu::println("accept: %1").arg( mbOut.getString() ) );
239 244
240 pClient->write("HTTP/1.1 101 Switching Protocols\r\n" 245 Bu::StringList lHeadersOut;
241 "Upgrade: websocket\r\n" 246 lHeadersOut.append("Upgrade: websocket");
242 "Connection: Upgrade\r\n" 247 lHeadersOut.append("Connection: Upgrade");
243 "Sec-WebSocket-Accept: " + mbOut.getString() + "\r\n" 248 lHeadersOut.append("Sec-WebSocket-Accept: " + mbOut.getString());
244 "\r\n" 249
245 ); 250 if( !onProcessHeaders( lHeadersOut ) )
251 {
252 pClient->disconnect();
253 return false;
254 }
255
256 Bu::String sHeaderBlock("HTTP/1.1 101 Switching Protocols\r\n");
257 for( Bu::StringList::iterator i = lHeadersOut.begin(); i; i++ )
258 {
259 sHeaderBlock += (*i) + "\r\n";
260 }
261 sHeaderBlock += "\r\n";
262 pClient->write( sHeaderBlock );
246 263
247 DEBUG( Bu::println("websocket: Switching protocols.") ); 264 DEBUG( Bu::println("websocket: Switching protocols.") );
248 265
diff --git a/src/unstable/protocolwebsocket.h b/src/unstable/protocolwebsocket.h
index 6778ea5..e5ff804 100644
--- a/src/unstable/protocolwebsocket.h
+++ b/src/unstable/protocolwebsocket.h
@@ -35,6 +35,12 @@ namespace Bu
35 virtual void onNewConnection( Bu::Client *pClient ); 35 virtual void onNewConnection( Bu::Client *pClient );
36 virtual void onNewData( Bu::Client *pClient ); 36 virtual void onNewData( Bu::Client *pClient );
37 37
38 /**
39 * Return true to continue the connection, return false to disconnect.
40 * Feel free to modify the headers out list to add or remove http
41 * headers to go to the client.
42 */
43 virtual bool onProcessHeaders( Bu::StringList &lHeadersOut );
38 virtual void onHandshakeComplete()=0; 44 virtual void onHandshakeComplete()=0;
39 virtual void onNewMessage( const Bu::String &sData, Operation eOp )=0; 45 virtual void onNewMessage( const Bu::String &sData, Operation eOp )=0;
40 void writeMessage( const Bu::String &sData, Operation eOp=Text ); 46 void writeMessage( const Bu::String &sData, Operation eOp=Text );