capture path/uri when connecting, and pass it back through callbacks in the openInfo member

This commit is contained in:
Benjamin Sergeant 2019-01-03 17:44:10 -08:00
parent 6ac3bdb94a
commit bd1c8873d0
13 changed files with 64 additions and 42 deletions

View File

@ -28,14 +28,15 @@ int main(int argc, char** argv)
const std::string& str, const std::string& str,
size_t wireSize, size_t wireSize,
const ix::WebSocketErrorInfo& error, const ix::WebSocketErrorInfo& error,
const ix::WebSocketCloseInfo& closeInfo, const ix::WebSocketOpenInfo& openInfo,
const ix::WebSocketHttpHeaders& headers) const ix::WebSocketCloseInfo& closeInfo)
{ {
if (messageType == ix::WebSocket_MessageType_Open) if (messageType == ix::WebSocket_MessageType_Open)
{ {
std::cerr << "New connection" << std::endl; std::cerr << "New connection" << std::endl;
std::cerr << "Uri: " << openInfo.uri << std::endl;
std::cerr << "Headers:" << std::endl; std::cerr << "Headers:" << std::endl;
for (auto it : headers) for (auto it : openInfo.headers)
{ {
std::cerr << it.first << ": " << it.second << std::endl; std::cerr << it.first << ": " << it.second << std::endl;
} }

View File

@ -87,8 +87,8 @@ namespace
const std::string& str, const std::string& str,
size_t wireSize, size_t wireSize,
const ix::WebSocketErrorInfo& error, const ix::WebSocketErrorInfo& error,
const ix::WebSocketCloseInfo& closeInfo, const ix::WebSocketOpenInfo& openInfo,
const ix::WebSocketHttpHeaders& headers) const ix::WebSocketCloseInfo& closeInfo)
{ {
std::stringstream ss; std::stringstream ss;
if (messageType == ix::WebSocket_MessageType_Open) if (messageType == ix::WebSocket_MessageType_Open)

View File

@ -28,14 +28,15 @@ int main(int argc, char** argv)
const std::string& str, const std::string& str,
size_t wireSize, size_t wireSize,
const ix::WebSocketErrorInfo& error, const ix::WebSocketErrorInfo& error,
const ix::WebSocketCloseInfo& closeInfo, const ix::WebSocketOpenInfo& openInfo,
const ix::WebSocketHttpHeaders& headers) const ix::WebSocketCloseInfo& closeInfo)
{ {
if (messageType == ix::WebSocket_MessageType_Open) if (messageType == ix::WebSocket_MessageType_Open)
{ {
std::cerr << "New connection" << std::endl; std::cerr << "New connection" << std::endl;
std::cerr << "Uri: " << openInfo.uri << std::endl;
std::cerr << "Headers:" << std::endl; std::cerr << "Headers:" << std::endl;
for (auto it : headers) for (auto it : openInfo.headers)
{ {
std::cerr << it.first << ": " << it.second << std::endl; std::cerr << it.first << ": " << it.second << std::endl;
} }

View File

@ -58,8 +58,8 @@ namespace
const std::string& str, const std::string& str,
size_t wireSize, size_t wireSize,
const ix::WebSocketErrorInfo& error, const ix::WebSocketErrorInfo& error,
const ix::WebSocketCloseInfo& closeInfo, const ix::WebSocketOpenInfo& openInfo,
const ix::WebSocketHttpHeaders& headers) const ix::WebSocketCloseInfo& closeInfo)
{ {
std::stringstream ss; std::stringstream ss;
if (messageType == ix::WebSocket_MessageType_Open) if (messageType == ix::WebSocket_MessageType_Open)

View File

@ -61,15 +61,16 @@ namespace
const std::string& str, const std::string& str,
size_t wireSize, size_t wireSize,
const ix::WebSocketErrorInfo& error, const ix::WebSocketErrorInfo& error,
const ix::WebSocketCloseInfo& closeInfo, const ix::WebSocketOpenInfo& openInfo,
const ix::WebSocketHttpHeaders& headers) const ix::WebSocketCloseInfo& closeInfo)
{ {
std::stringstream ss; std::stringstream ss;
if (messageType == ix::WebSocket_MessageType_Open) if (messageType == ix::WebSocket_MessageType_Open)
{ {
log("ws_connect: connected"); log("ws_connect: connected");
std::cout << "Uri: " << openInfo.uri << std::endl;
std::cout << "Handshake Headers:" << std::endl; std::cout << "Handshake Headers:" << std::endl;
for (auto it : headers) for (auto it : openInfo.headers)
{ {
std::cout << it.first << ": " << it.second << std::endl; std::cout << it.first << ": " << it.second << std::endl;
} }

View File

@ -40,9 +40,8 @@ namespace ix
[this](uint16_t code, const std::string& reason, size_t wireSize) [this](uint16_t code, const std::string& reason, size_t wireSize)
{ {
_onMessageCallback(WebSocket_MessageType_Close, "", wireSize, _onMessageCallback(WebSocket_MessageType_Close, "", wireSize,
WebSocketErrorInfo(), WebSocketErrorInfo(), WebSocketOpenInfo(),
WebSocketCloseInfo(code, reason), WebSocketCloseInfo(code, reason));
WebSocketHttpHeaders());
} }
); );
} }
@ -119,8 +118,9 @@ namespace ix
} }
_onMessageCallback(WebSocket_MessageType_Open, "", 0, _onMessageCallback(WebSocket_MessageType_Open, "", 0,
WebSocketErrorInfo(), WebSocketCloseInfo(), WebSocketErrorInfo(),
status.headers); WebSocketOpenInfo(status.uri, status.headers),
WebSocketCloseInfo());
return status; return status;
} }
@ -138,8 +138,9 @@ namespace ix
} }
_onMessageCallback(WebSocket_MessageType_Open, "", 0, _onMessageCallback(WebSocket_MessageType_Open, "", 0,
WebSocketErrorInfo(), WebSocketCloseInfo(), WebSocketErrorInfo(),
status.headers); WebSocketOpenInfo(status.uri, status.headers),
WebSocketCloseInfo());
return status; return status;
} }
@ -184,8 +185,8 @@ namespace ix
connectErr.reason = status.errorStr; connectErr.reason = status.errorStr;
connectErr.http_status = status.http_status; connectErr.http_status = status.http_status;
_onMessageCallback(WebSocket_MessageType_Error, "", 0, _onMessageCallback(WebSocket_MessageType_Error, "", 0,
connectErr, WebSocketCloseInfo(), connectErr, WebSocketOpenInfo(),
WebSocketHttpHeaders()); WebSocketCloseInfo());
std::this_thread::sleep_for(duration); std::this_thread::sleep_for(duration);
} }
@ -240,8 +241,8 @@ namespace ix
webSocketErrorInfo.decompressionError = decompressionError; webSocketErrorInfo.decompressionError = decompressionError;
_onMessageCallback(webSocketMessageType, msg, wireSize, _onMessageCallback(webSocketMessageType, msg, wireSize,
webSocketErrorInfo, WebSocketCloseInfo(), webSocketErrorInfo, WebSocketOpenInfo(),
WebSocketHttpHeaders()); WebSocketCloseInfo());
WebSocket::invokeTrafficTrackerCallback(msg.size(), true); WebSocket::invokeTrafficTrackerCallback(msg.size(), true);
}); });

View File

@ -41,6 +41,20 @@ namespace ix
WebSocket_MessageType_Pong = 5 WebSocket_MessageType_Pong = 5
}; };
struct WebSocketOpenInfo
{
std::string uri;
WebSocketHttpHeaders headers;
WebSocketOpenInfo(const std::string& u = std::string(),
const WebSocketHttpHeaders& h = WebSocketHttpHeaders())
: uri(u)
, headers(h)
{
;
}
};
struct WebSocketCloseInfo struct WebSocketCloseInfo
{ {
uint16_t code; uint16_t code;
@ -59,8 +73,9 @@ namespace ix
const std::string&, const std::string&,
size_t wireSize, size_t wireSize,
const WebSocketErrorInfo&, const WebSocketErrorInfo&,
const WebSocketCloseInfo&, const WebSocketOpenInfo&,
const WebSocketHttpHeaders&)>; const WebSocketCloseInfo&)>;
using OnTrafficTrackerCallback = std::function<void(size_t size, bool incoming)>; using OnTrafficTrackerCallback = std::function<void(size_t size, bool incoming)>;
class WebSocket class WebSocket

View File

@ -380,7 +380,7 @@ namespace ix
} }
} }
return WebSocketInitResult(true, status, "", headers); return WebSocketInitResult(true, status, "", headers, path);
} }
WebSocketInitResult WebSocketHandshake::serverHandshake(int fd) WebSocketInitResult WebSocketHandshake::serverHandshake(int fd)
@ -491,6 +491,6 @@ namespace ix
return WebSocketInitResult(false, 0, std::string("Failed sending response to ") + remote); return WebSocketInitResult(false, 0, std::string("Failed sending response to ") + remote);
} }
return WebSocketInitResult(true, 200, "", headers); return WebSocketInitResult(true, 200, "", headers, uri);
} }
} }

View File

@ -26,16 +26,19 @@ namespace ix
int http_status; int http_status;
std::string errorStr; std::string errorStr;
WebSocketHttpHeaders headers; WebSocketHttpHeaders headers;
std::string uri;
WebSocketInitResult(bool s = false, WebSocketInitResult(bool s = false,
int status = 0, int status = 0,
const std::string& e = std::string(), const std::string& e = std::string(),
WebSocketHttpHeaders h = WebSocketHttpHeaders()) WebSocketHttpHeaders h = WebSocketHttpHeaders(),
const std::string& u = std::string())
{ {
success = s; success = s;
http_status = status; http_status = status;
errorStr = e; errorStr = e;
headers = h; headers = h;
uri = u;
} }
}; };

View File

@ -4,11 +4,6 @@
* Copyright (c) 2018 Machine Zone, Inc. All rights reserved. * Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
*/ */
// TODO
// pass to callback PATH
// pass connection success too to callback
//
#pragma once #pragma once
#include <utility> // pair #include <utility> // pair

View File

@ -15,6 +15,8 @@ build:
(cd examples/chat ; mkdir -p build ; cd build ; cmake .. ; make) (cd examples/chat ; mkdir -p build ; cd build ; cmake .. ; make)
(cd examples/ping_pong ; mkdir -p build ; cd build ; cmake .. ; make) (cd examples/ping_pong ; mkdir -p build ; cd build ; cmake .. ; make)
(cd examples/ws_connect ; mkdir -p build ; cd build ; cmake .. ; make) (cd examples/ws_connect ; mkdir -p build ; cd build ; cmake .. ; make)
(cd examples/echo_server ; mkdir -p build ; cd build ; cmake .. ; make)
(cd examples/broadcast_server ; mkdir -p build ; cd build ; cmake .. ; make)
# That target is used to start a node server, but isn't required as we have # That target is used to start a node server, but isn't required as we have
# a builtin C++ server started in the unittest now # a builtin C++ server started in the unittest now
@ -26,3 +28,4 @@ test:
(cd test && sh run.sh) (cd test && sh run.sh)
.PHONY: test .PHONY: test
.PHONY: build

View File

@ -27,14 +27,15 @@ namespace ix
const std::string& str, const std::string& str,
size_t wireSize, size_t wireSize,
const ix::WebSocketErrorInfo& error, const ix::WebSocketErrorInfo& error,
const ix::WebSocketCloseInfo& closeInfo, const ix::WebSocketOpenInfo& openInfo,
const ix::WebSocketHttpHeaders& headers) const ix::WebSocketCloseInfo& closeInfo)
{ {
if (messageType == ix::WebSocket_MessageType_Open) if (messageType == ix::WebSocket_MessageType_Open)
{ {
std::cerr << "New connection" << std::endl; std::cerr << "New connection" << std::endl;
std::cerr << "Uri: " << openInfo.uri << std::endl;
std::cerr << "Headers:" << std::endl; std::cerr << "Headers:" << std::endl;
for (auto it : headers) for (auto it : openInfo.headers)
{ {
std::cerr << it.first << ": " << it.second << std::endl; std::cerr << it.first << ": " << it.second << std::endl;
} }

View File

@ -94,8 +94,8 @@ namespace
const std::string& str, const std::string& str,
size_t wireSize, size_t wireSize,
const ix::WebSocketErrorInfo& error, const ix::WebSocketErrorInfo& error,
const ix::WebSocketCloseInfo& closeInfo, const ix::WebSocketOpenInfo& openInfo,
const ix::WebSocketHttpHeaders& headers) const ix::WebSocketCloseInfo& closeInfo)
{ {
std::stringstream ss; std::stringstream ss;
if (messageType == ix::WebSocket_MessageType_Open) if (messageType == ix::WebSocket_MessageType_Open)
@ -183,14 +183,15 @@ namespace
const std::string& str, const std::string& str,
size_t wireSize, size_t wireSize,
const ix::WebSocketErrorInfo& error, const ix::WebSocketErrorInfo& error,
const ix::WebSocketCloseInfo& closeInfo, const ix::WebSocketOpenInfo& openInfo,
const ix::WebSocketHttpHeaders& headers) const ix::WebSocketCloseInfo& closeInfo)
{ {
if (messageType == ix::WebSocket_MessageType_Open) if (messageType == ix::WebSocket_MessageType_Open)
{ {
std::cerr << "New connection" << std::endl; std::cerr << "New connection" << std::endl;
std::cerr << "Uri: " << openInfo.uri << std::endl;
std::cerr << "Headers:" << std::endl; std::cerr << "Headers:" << std::endl;
for (auto it : headers) for (auto it : openInfo.headers)
{ {
std::cerr << it.first << ": " << it.second << std::endl; std::cerr << it.first << ": " << it.second << std::endl;
} }