From a95fcbbdbfc06e930a1b746bcc9f5be992cd1b4e Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Fri, 30 Aug 2019 12:48:18 -0700 Subject: [PATCH] user agent --- CMakeLists.txt | 3 + ixwebsocket/IXHttpClient.cpp | 3 +- ixwebsocket/IXHttpServer.cpp | 2 + ixwebsocket/IXUserAgent.cpp | 83 ++++++++++++++++++++++++++++ ixwebsocket/IXUserAgent.h | 14 +++++ ixwebsocket/IXWebSocketHandshake.cpp | 11 +++- ixwebsocket/IXWebSocketVersion.h | 9 +++ 7 files changed, 123 insertions(+), 2 deletions(-) create mode 100644 ixwebsocket/IXUserAgent.cpp create mode 100644 ixwebsocket/IXUserAgent.h create mode 100644 ixwebsocket/IXWebSocketVersion.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 82a0235c..296eebd5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,6 +37,7 @@ set( IXWEBSOCKET_SOURCES ixwebsocket/IXSocketFactory.cpp ixwebsocket/IXSocketServer.cpp ixwebsocket/IXUrlParser.cpp + ixwebsocket/IXUserAgent.cpp ixwebsocket/IXWebSocket.cpp ixwebsocket/IXWebSocketCloseConstants.cpp ixwebsocket/IXWebSocketHandshake.cpp @@ -68,6 +69,7 @@ set( IXWEBSOCKET_HEADERS ixwebsocket/IXSocketFactory.h ixwebsocket/IXSocketServer.h ixwebsocket/IXUrlParser.h + ixwebsocket/IXUserAgent.h ixwebsocket/IXWebSocket.h ixwebsocket/IXWebSocketCloseConstants.h ixwebsocket/IXWebSocketCloseInfo.h @@ -84,6 +86,7 @@ set( IXWEBSOCKET_HEADERS ixwebsocket/IXWebSocketSendInfo.h ixwebsocket/IXWebSocketServer.h ixwebsocket/IXWebSocketTransport.h + ixwebsocket/IXWebSocketVersion.h ixwebsocket/LUrlParser.h ixwebsocket/libwshandshake.hpp ) diff --git a/ixwebsocket/IXHttpClient.cpp b/ixwebsocket/IXHttpClient.cpp index 36467e95..d7caf9e6 100644 --- a/ixwebsocket/IXHttpClient.cpp +++ b/ixwebsocket/IXHttpClient.cpp @@ -6,6 +6,7 @@ #include "IXHttpClient.h" #include "IXUrlParser.h" +#include "IXUserAgent.h" #include "IXWebSocketHttpHeaders.h" #include "IXSocketFactory.h" @@ -171,7 +172,7 @@ namespace ix // Set a default User agent if none is present if (headers.find("User-Agent") == headers.end()) { - ss << "User-Agent: ixwebsocket" << "\r\n"; + ss << "User-Agent: " << userAgent() << "\r\n"; } if (verb == kPost || verb == kPut) diff --git a/ixwebsocket/IXHttpServer.cpp b/ixwebsocket/IXHttpServer.cpp index bcc60309..665052fa 100644 --- a/ixwebsocket/IXHttpServer.cpp +++ b/ixwebsocket/IXHttpServer.cpp @@ -132,6 +132,8 @@ namespace ix // Log request std::stringstream ss; ss << request->method + << " " + << request->headers["User-Agent"] << " " << request->uri << " " diff --git a/ixwebsocket/IXUserAgent.cpp b/ixwebsocket/IXUserAgent.cpp new file mode 100644 index 00000000..c444b3cb --- /dev/null +++ b/ixwebsocket/IXUserAgent.cpp @@ -0,0 +1,83 @@ +/* + * IXUserAgent.cpp + * Author: Benjamin Sergeant + * Copyright (c) 2017-2019 Machine Zone, Inc. All rights reserved. + */ + +#include "IXUserAgent.h" +#include "IXWebSocketVersion.h" + +#include +#include + +// Platform name +#if defined(_WIN32) + #define PLATFORM_NAME "windows" // Windows +#elif defined(_WIN64) + #define PLATFORM_NAME "windows" // Windows +#elif defined(__CYGWIN__) && !defined(_WIN32) + #define PLATFORM_NAME "windows" // Windows (Cygwin POSIX under Microsoft Window) +#elif defined(__ANDROID__) + #define PLATFORM_NAME "android" // Android (implies Linux, so it must come first) +#elif defined(__linux__) + #define PLATFORM_NAME "linux" // Debian, Ubuntu, Gentoo, Fedora, openSUSE, RedHat, Centos and other +#elif defined(__unix__) || !defined(__APPLE__) && defined(__MACH__) + #include + #if defined(BSD) + #define PLATFORM_NAME "bsd" // FreeBSD, NetBSD, OpenBSD, DragonFly BSD + #endif +#elif defined(__hpux) + #define PLATFORM_NAME "hp-ux" // HP-UX +#elif defined(_AIX) + #define PLATFORM_NAME "aix" // IBM AIX +#elif defined(__APPLE__) && defined(__MACH__) // Apple OSX and iOS (Darwin) + #include + #if TARGET_IPHONE_SIMULATOR == 1 + #define PLATFORM_NAME "ios" // Apple iOS + #elif TARGET_OS_IPHONE == 1 + #define PLATFORM_NAME "ios" // Apple iOS + #elif TARGET_OS_MAC == 1 + #define PLATFORM_NAME "macos" // Apple OSX + #endif +#elif defined(__sun) && defined(__SVR4) + #define PLATFORM_NAME "solaris" // Oracle Solaris, Open Indiana +#else + #define PLATFORM_NAME "unknown platform" +#endif + +// SSL +#if defined(IXWEBSOCKET_USE_OPEN_SSL) +#include +#endif + +namespace ix +{ + std::string userAgent() + { + std::stringstream ss; + + // IXWebSocket Version + ss << "ixwebsocket/" << IX_WEBSOCKET_VERSION; + + // Platform + ss << " " << PLATFORM_NAME; + + // TLS +#ifdef IXWEBSOCKET_USE_TLS +#ifdef IXWEBSOCKET_USE_MBED_TLS + ss << " ssl/mbedtls"; +#elif __APPLE__ + ss << " ssl/DarwinSSL"; +#elif defined(IXWEBSOCKET_USE_OPEN_SSL) + ss << " ssl/OpenSSL " << OPENSSL_VERSION_TEXT; +#endif +#else + ss << " nossl"; +#endif + + // Zlib version + ss << " zlib " << ZLIB_VERSION; + + return ss.str(); + } +} diff --git a/ixwebsocket/IXUserAgent.h b/ixwebsocket/IXUserAgent.h new file mode 100644 index 00000000..367ba18e --- /dev/null +++ b/ixwebsocket/IXUserAgent.h @@ -0,0 +1,14 @@ +/* + * IXUserAgent.h + * Author: Benjamin Sergeant + * Copyright (c) 2019 Machine Zone, Inc. All rights reserved. + */ + +#pragma once + +#include + +namespace ix +{ + std::string userAgent(); +} // namespace ix diff --git a/ixwebsocket/IXWebSocketHandshake.cpp b/ixwebsocket/IXWebSocketHandshake.cpp index 434f5dab..5a9c9537 100644 --- a/ixwebsocket/IXWebSocketHandshake.cpp +++ b/ixwebsocket/IXWebSocketHandshake.cpp @@ -8,6 +8,7 @@ #include "IXSocketConnect.h" #include "IXUrlParser.h" #include "IXHttp.h" +#include "IXUserAgent.h" #include "libwshandshake.hpp" @@ -128,9 +129,17 @@ namespace ix ss << "Sec-WebSocket-Version: 13\r\n"; ss << "Sec-WebSocket-Key: " << secWebSocketKey << "\r\n"; - for (auto& it : extraHeaders) { + // User-Agent can be customized by users + if (extraHeaders.find("User-Agent") == extraHeaders.end()) + { + ss << "User-Agent: " << userAgent() << "\r\n"; + } + + for (auto& it : extraHeaders) + { ss << it.first << ":" << it.second << "\r\n"; } + if (_enablePerMessageDeflate) { ss << _perMessageDeflateOptions.generateHeader(); diff --git a/ixwebsocket/IXWebSocketVersion.h b/ixwebsocket/IXWebSocketVersion.h new file mode 100644 index 00000000..437ccc6e --- /dev/null +++ b/ixwebsocket/IXWebSocketVersion.h @@ -0,0 +1,9 @@ +/* + * IXWebSocketVersion.h + * Author: Benjamin Sergeant + * Copyright (c) 2019 Machine Zone, Inc. All rights reserved. + */ + +#pragma once + +#define IX_WEBSOCKET_VERSION "5.0.9"