Compare commits
	
		
			54 Commits
		
	
	
		
			user/bserg
			...
			feature/ht
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
|  | 73f4ba7ee7 | ||
|  | 6f39592c7b | ||
|  | 38200fc5d7 | ||
|  | 285c12775a | ||
|  | 6d56f7223a | ||
|  | 1db3568375 | ||
|  | 0a752e7d18 | ||
|  | 7c2bc2cf7e | ||
|  | 79f601ac65 | ||
|  | 069eccf415 | ||
|  | b563541b14 | ||
|  | 3bcd6f97a6 | ||
|  | c04bc3cdfc | ||
|  | 846f0c680a | ||
|  | c552a03ef0 | ||
|  | 0f175143c9 | ||
|  | 85569cb401 | ||
|  | bd854553d4 | ||
|  | 38c57e1ed2 | ||
|  | 26cc5025fb | ||
|  | 806cf39efc | ||
|  | daaa7ec704 | ||
|  | 3cffc6f9a5 | ||
|  | f8b1a03ee6 | ||
|  | a7ff3c41a1 | ||
|  | 78dbba5521 | ||
|  | b211bdbe38 | ||
|  | a0a53ab986 | ||
|  | 8d819053ff | ||
|  | e20ddc2a08 | ||
|  | c415ba9427 | ||
|  | e881b82511 | ||
|  | d5551e5d68 | ||
|  | e8583000b8 | ||
|  | d642ef1a89 | ||
|  | 2df118022d | ||
|  | 95457c8f4c | ||
|  | 0a45b7787f | ||
|  | b8c397e180 | ||
|  | 90105fa2b3 | ||
|  | 24859fef8a | ||
|  | 73d7280723 | ||
|  | 262de49c3c | ||
|  | 3a77e96a05 | ||
|  | 505dd6d50f | ||
|  | 3f8027b65c | ||
|  | 0b7c3ec235 | ||
|  | 29c96f287f | ||
|  | 2a17cad1bf | ||
|  | 0f2c765f45 | ||
|  | 49077f8f44 | ||
|  | 6a23b8530f | ||
|  | ae841af91a | ||
|  | 44f38849b2 | 
							
								
								
									
										1
									
								
								.dockerignore
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								.dockerignore
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | |||||||
|  | build | ||||||
							
								
								
									
										1
									
								
								examples/ping_pong/.gitignore → .gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								examples/ping_pong/.gitignore → .gitignore
									
									
									
									
										vendored
									
									
								
							| @@ -1,2 +1 @@ | |||||||
| venv |  | ||||||
| build | build | ||||||
							
								
								
									
										14
									
								
								.travis.yml
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								.travis.yml
									
									
									
									
									
								
							| @@ -2,8 +2,16 @@ language: cpp | |||||||
| dist: xenial | dist: xenial | ||||||
|  |  | ||||||
| compiler: | compiler: | ||||||
|  |   - gcc | ||||||
|   - clang |   - clang | ||||||
| #   - gcc | os: | ||||||
|  |   - linux | ||||||
|  |   - osx | ||||||
|  |  | ||||||
|     # os: osx | matrix: | ||||||
| script: make test |   exclude: | ||||||
|  |     # GCC fails on recent Travis OSX images. | ||||||
|  |     - compiler: gcc | ||||||
|  |       os: osx | ||||||
|  |  | ||||||
|  | script: python test/run.py | ||||||
|   | |||||||
| @@ -10,6 +10,7 @@ set (CMAKE_CXX_STANDARD 14) | |||||||
| set (CXX_STANDARD_REQUIRED ON) | set (CXX_STANDARD_REQUIRED ON) | ||||||
| set (CMAKE_CXX_EXTENSIONS OFF) | set (CMAKE_CXX_EXTENSIONS OFF) | ||||||
|  |  | ||||||
|  | # -Wshorten-64-to-32 does not work with clang | ||||||
| if (NOT WIN32) | if (NOT WIN32) | ||||||
|   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic") |   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic") | ||||||
| endif() | endif() | ||||||
| @@ -19,6 +20,7 @@ set( IXWEBSOCKET_SOURCES | |||||||
|     ixwebsocket/IXSocket.cpp |     ixwebsocket/IXSocket.cpp | ||||||
|     ixwebsocket/IXSocketServer.cpp |     ixwebsocket/IXSocketServer.cpp | ||||||
|     ixwebsocket/IXSocketConnect.cpp |     ixwebsocket/IXSocketConnect.cpp | ||||||
|  |     ixwebsocket/IXSocketFactory.cpp | ||||||
|     ixwebsocket/IXDNSLookup.cpp |     ixwebsocket/IXDNSLookup.cpp | ||||||
|     ixwebsocket/IXCancellationRequest.cpp |     ixwebsocket/IXCancellationRequest.cpp | ||||||
|     ixwebsocket/IXWebSocket.cpp |     ixwebsocket/IXWebSocket.cpp | ||||||
| @@ -28,6 +30,9 @@ set( IXWEBSOCKET_SOURCES | |||||||
|     ixwebsocket/IXWebSocketPerMessageDeflate.cpp |     ixwebsocket/IXWebSocketPerMessageDeflate.cpp | ||||||
|     ixwebsocket/IXWebSocketPerMessageDeflateCodec.cpp |     ixwebsocket/IXWebSocketPerMessageDeflateCodec.cpp | ||||||
|     ixwebsocket/IXWebSocketPerMessageDeflateOptions.cpp |     ixwebsocket/IXWebSocketPerMessageDeflateOptions.cpp | ||||||
|  |     ixwebsocket/IXWebSocketHttpHeaders.cpp | ||||||
|  |     ixwebsocket/IXHttpClient.cpp | ||||||
|  |     ixwebsocket/IXUrlParser.cpp | ||||||
| ) | ) | ||||||
|  |  | ||||||
| set( IXWEBSOCKET_HEADERS | set( IXWEBSOCKET_HEADERS | ||||||
| @@ -35,9 +40,11 @@ set( IXWEBSOCKET_HEADERS | |||||||
|     ixwebsocket/IXSocket.h |     ixwebsocket/IXSocket.h | ||||||
|     ixwebsocket/IXSocketServer.h |     ixwebsocket/IXSocketServer.h | ||||||
|     ixwebsocket/IXSocketConnect.h |     ixwebsocket/IXSocketConnect.h | ||||||
|  |     ixwebsocket/IXSocketFactory.h | ||||||
|     ixwebsocket/IXSetThreadName.h |     ixwebsocket/IXSetThreadName.h | ||||||
|     ixwebsocket/IXDNSLookup.h |     ixwebsocket/IXDNSLookup.h | ||||||
|     ixwebsocket/IXCancellationRequest.h |     ixwebsocket/IXCancellationRequest.h | ||||||
|  |     ixwebsocket/IXProgressCallback.h | ||||||
|     ixwebsocket/IXWebSocket.h |     ixwebsocket/IXWebSocket.h | ||||||
|     ixwebsocket/IXWebSocketServer.h |     ixwebsocket/IXWebSocketServer.h | ||||||
|     ixwebsocket/IXWebSocketTransport.h |     ixwebsocket/IXWebSocketTransport.h | ||||||
| @@ -49,6 +56,8 @@ set( IXWEBSOCKET_HEADERS | |||||||
|     ixwebsocket/IXWebSocketPerMessageDeflateOptions.h |     ixwebsocket/IXWebSocketPerMessageDeflateOptions.h | ||||||
|     ixwebsocket/IXWebSocketHttpHeaders.h |     ixwebsocket/IXWebSocketHttpHeaders.h | ||||||
|     ixwebsocket/libwshandshake.hpp |     ixwebsocket/libwshandshake.hpp | ||||||
|  |     ixwebsocket/IXHttpClient.h | ||||||
|  |     ixwebsocket/IXUrlParser.h | ||||||
| ) | ) | ||||||
|  |  | ||||||
| # Platform specific code | # Platform specific code | ||||||
| @@ -112,3 +121,5 @@ set( IXWEBSOCKET_INCLUDE_DIRS | |||||||
|     . |     . | ||||||
|     ../../shared/OpenSSL/include) |     ../../shared/OpenSSL/include) | ||||||
| target_include_directories( ixwebsocket PUBLIC ${IXWEBSOCKET_INCLUDE_DIRS} ) | target_include_directories( ixwebsocket PUBLIC ${IXWEBSOCKET_INCLUDE_DIRS} ) | ||||||
|  |  | ||||||
|  | add_subdirectory(ws) | ||||||
|   | |||||||
							
								
								
									
										91
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										91
									
								
								README.md
									
									
									
									
									
								
							| @@ -5,7 +5,7 @@ | |||||||
| ## Introduction | ## Introduction | ||||||
|  |  | ||||||
| [*WebSocket*](https://en.wikipedia.org/wiki/WebSocket) is a computer communications protocol, providing full-duplex | [*WebSocket*](https://en.wikipedia.org/wiki/WebSocket) is a computer communications protocol, providing full-duplex | ||||||
| communication channels over a single TCP connection. *IXWebSocket* is a C++ library for client and server Websocket communication. The code is derived from [easywsclient](https://github.com/dhbaird/easywsclient) and from the [Satori C SDK](https://github.com/satori-com/satori-rtm-sdk-c). It has been tested on the following platforms. | communication channels over a single TCP connection. *IXWebSocket* is a C++ library for client and server Websocket communication, and for client HTTP communication. The code is derived from [easywsclient](https://github.com/dhbaird/easywsclient) and from the [Satori C SDK](https://github.com/satori-com/satori-rtm-sdk-c). It has been tested on the following platforms. | ||||||
|  |  | ||||||
| * macOS | * macOS | ||||||
| * iOS | * iOS | ||||||
| @@ -15,7 +15,7 @@ communication channels over a single TCP connection. *IXWebSocket* is a C++ libr | |||||||
|  |  | ||||||
| ## Examples | ## Examples | ||||||
|  |  | ||||||
| The examples folder countains a simple chat program, using a node.js broadcast server. | The [*ws*](https://github.com/machinezone/IXWebSocket/tree/master/ws) folder countains many interactive programs for chat, [file transfers](https://github.com/machinezone/IXWebSocket/blob/master/ws/ws_send.cpp), [curl like](https://github.com/machinezone/IXWebSocket/blob/master/ws/ws_http_client.cpp) http clients, demonstrating client and server usage. | ||||||
|  |  | ||||||
| Here is what the client API looks like. | Here is what the client API looks like. | ||||||
|  |  | ||||||
| @@ -25,7 +25,7 @@ ix::WebSocket webSocket; | |||||||
| std::string url("ws://localhost:8080/"); | std::string url("ws://localhost:8080/"); | ||||||
| webSocket.setUrl(url); | webSocket.setUrl(url); | ||||||
|  |  | ||||||
| // Optional heart beat, sent every 45 seconds when there isn't any traffic | // Optional heart beat, sent every 45 seconds when there is not any traffic | ||||||
| // to make sure that load balancers do not kill an idle connection. | // to make sure that load balancers do not kill an idle connection. | ||||||
| webSocket.setHeartBeatPeriod(45); | webSocket.setHeartBeatPeriod(45); | ||||||
|  |  | ||||||
| @@ -110,6 +110,68 @@ server.wait(); | |||||||
|  |  | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
|  | Here is what the HTTP client API looks like. Note that HTTP client support is very recent and subject to changes. | ||||||
|  |  | ||||||
|  | ``` | ||||||
|  | // | ||||||
|  | // Preparation | ||||||
|  | // | ||||||
|  | HttpClient httpClient; | ||||||
|  | HttpRequestArgs args; | ||||||
|  |  | ||||||
|  | // Custom headers can be set | ||||||
|  | WebSocketHttpHeaders headers; | ||||||
|  | headers["Foo"] = "bar"; | ||||||
|  | args.extraHeaders = parseHeaders(headersData); | ||||||
|  |  | ||||||
|  | // Timeout options | ||||||
|  | args.connectTimeout = connectTimeout; | ||||||
|  | args.transferTimeout = transferTimeout; | ||||||
|  |  | ||||||
|  | // Redirect options | ||||||
|  | args.followRedirects = followRedirects; | ||||||
|  | args.maxRedirects = maxRedirects; | ||||||
|  |  | ||||||
|  | // Misc | ||||||
|  | args.compress = compress; // Enable gzip compression | ||||||
|  | args.verbose = verbose; | ||||||
|  | args.logger = [](const std::string& msg) | ||||||
|  | { | ||||||
|  |     std::cout << msg; | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | // | ||||||
|  | // Request | ||||||
|  | // | ||||||
|  | HttpResponse out; | ||||||
|  | std::string url = "https://www.google.com"; | ||||||
|  |  | ||||||
|  | // HEAD request | ||||||
|  | out = httpClient.head(url, args); | ||||||
|  |  | ||||||
|  | // GET request | ||||||
|  | out = httpClient.get(url, args); | ||||||
|  |  | ||||||
|  | // POST request with parameters | ||||||
|  | HttpParameters httpParameters; | ||||||
|  | httpParameters["foo"] = "bar"; | ||||||
|  | out = httpClient.post(url, httpParameters, args); | ||||||
|  |  | ||||||
|  | // POST request with a body | ||||||
|  | out = httpClient.post(url, std::string("foo=bar"), args); | ||||||
|  |  | ||||||
|  | // | ||||||
|  | // Result | ||||||
|  | // | ||||||
|  | auto statusCode = std::get<0>(out); | ||||||
|  | auto errorCode = std::get<1>(out); | ||||||
|  | auto responseHeaders = std::get<2>(out); | ||||||
|  | auto payload = std::get<3>(out); | ||||||
|  | auto errorMsg = std::get<4>(out); | ||||||
|  | auto uploadSize = std::get<5>(out); | ||||||
|  | auto downloadSize = std::get<6>(out); | ||||||
|  | ``` | ||||||
|  |  | ||||||
| ## Build | ## Build | ||||||
|  |  | ||||||
| CMakefiles for the library and the examples are available. This library has few dependencies, so it is possible to just add the source files into your project. | CMakefiles for the library and the examples are available. This library has few dependencies, so it is possible to just add the source files into your project. | ||||||
| @@ -134,25 +196,19 @@ No manual polling to fetch data is required. Data is sent and received instantly | |||||||
|  |  | ||||||
| If the remote end (server) breaks the connection, the code will try to perpetually reconnect, by using an exponential backoff strategy, capped at one retry every 10 seconds. | If the remote end (server) breaks the connection, the code will try to perpetually reconnect, by using an exponential backoff strategy, capped at one retry every 10 seconds. | ||||||
|  |  | ||||||
|  | ### Large messages | ||||||
|  |  | ||||||
|  | Large frames are broken up into smaller chunks or messages to avoid filling up the os tcp buffers, which is permitted thanks to WebSocket [fragmentation](https://tools.ietf.org/html/rfc6455#section-5.4). Messages up to 500M were sent and received succesfully. | ||||||
|  |  | ||||||
| ## Limitations | ## Limitations | ||||||
|  |  | ||||||
| * There is no text support for sending data, only the binary protocol is supported. Sending json or text over the binary protocol works well. | * There is no text support for sending data, only the binary protocol is supported. Sending json or text over the binary protocol works well. | ||||||
| * Automatic reconnection works at the TCP socket level, and will detect remote end disconnects. However, if the device/computer network become unreachable (by turning off wifi), it is quite hard to reliably and timely detect it at the socket level using `recv` and `send` error codes. [Here](https://stackoverflow.com/questions/14782143/linux-socket-how-to-detect-disconnected-network-in-a-client-program) is a good discussion on the subject. This behavior is consistent with other runtimes such as node.js. One way to detect a disconnected device with low level C code is to do a name resolution with DNS but this can be expensive. Mobile devices have good and reliable API to do that. | * Automatic reconnection works at the TCP socket level, and will detect remote end disconnects. However, if the device/computer network become unreachable (by turning off wifi), it is quite hard to reliably and timely detect it at the socket level using `recv` and `send` error codes. [Here](https://stackoverflow.com/questions/14782143/linux-socket-how-to-detect-disconnected-network-in-a-client-program) is a good discussion on the subject. This behavior is consistent with other runtimes such as node.js. One way to detect a disconnected device with low level C code is to do a name resolution with DNS but this can be expensive. Mobile devices have good and reliable API to do that. | ||||||
| * The server code is using select to detect incoming data, and creates one OS thread per connection. This isn't as scalable as strategies using epoll or kqueue. | * The server code is using select to detect incoming data, and creates one OS thread per connection. This is not as scalable as strategies using epoll or kqueue. | ||||||
|  |  | ||||||
| ## Examples |  | ||||||
|  |  | ||||||
| 1. Bring up a terminal and jump to the examples folder. |  | ||||||
| 2. Compile the example C++ code. `sh build.sh` |  | ||||||
| 3. Install node.js from [here](https://nodejs.org/en/download/). |  | ||||||
| 4. Type `npm install` to install the node.js dependencies. Then `node broadcast-server.js` to run the server. |  | ||||||
| 5. Bring up a second terminal. `./cmd_websocket_chat bob` |  | ||||||
| 6. Bring up a third terminal. `./cmd_websocket_chat bill` |  | ||||||
| 7. Start typing things in any of those terminals. Hopefully you should see your message being received on the other end. |  | ||||||
|  |  | ||||||
| ## C++ code organization | ## C++ code organization | ||||||
|  |  | ||||||
| Here's a simplistic diagram which explains how the code is structured in term of class/modules. | Here is a simplistic diagram which explains how the code is structured in term of class/modules. | ||||||
|  |  | ||||||
| ``` | ``` | ||||||
| +-----------------------+ --- Public | +-----------------------+ --- Public | ||||||
| @@ -204,7 +260,7 @@ If the connection was closed and sending failed, the return value will be set to | |||||||
| 1. WebSocket_ReadyState_Connecting - The connection is not yet open. | 1. WebSocket_ReadyState_Connecting - The connection is not yet open. | ||||||
| 2. WebSocket_ReadyState_Open       - The connection is open and ready to communicate. | 2. WebSocket_ReadyState_Open       - The connection is open and ready to communicate. | ||||||
| 3. WebSocket_ReadyState_Closing    - The connection is in the process of closing. | 3. WebSocket_ReadyState_Closing    - The connection is in the process of closing. | ||||||
| 4. WebSocket_MessageType_Close     - The connection is closed or couldn't be opened. | 4. WebSocket_MessageType_Close     - The connection is closed or could not be opened. | ||||||
|  |  | ||||||
| ### Open and Close notifications | ### Open and Close notifications | ||||||
|  |  | ||||||
| @@ -309,11 +365,12 @@ A ping message can be sent to the server, with an optional data string. | |||||||
|  |  | ||||||
| ``` | ``` | ||||||
| websocket.ping("ping data, optional (empty string is ok): limited to 125 bytes long"); | websocket.ping("ping data, optional (empty string is ok): limited to 125 bytes long"); | ||||||
|  | ``` | ||||||
|  |  | ||||||
| ### Heartbeat. | ### Heartbeat. | ||||||
|  |  | ||||||
| You can configure an optional heart beat / keep-alive, sent every 45 seconds | You can configure an optional heart beat / keep-alive, sent every 45 seconds | ||||||
| when there isn't any traffic to make sure that load balancers do not kill an | when there is not any traffic to make sure that load balancers do not kill an | ||||||
| idle connection. | idle connection. | ||||||
|  |  | ||||||
| ``` | ``` | ||||||
|   | |||||||
| @@ -15,5 +15,8 @@ RUN apt-get -y install cmake | |||||||
|  |  | ||||||
| COPY . . | COPY . . | ||||||
|  |  | ||||||
| WORKDIR test | WORKDIR ws | ||||||
| RUN ["sh", "build_linux.sh"] | RUN ["sh", "docker_build.sh"] | ||||||
|  |  | ||||||
|  | EXPOSE 8765 | ||||||
|  | CMD ["/ws/ws", "transfer", "8765"] | ||||||
|   | |||||||
							
								
								
									
										9
									
								
								examples/broadcast_server/.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										9
									
								
								examples/broadcast_server/.gitignore
									
									
									
									
										vendored
									
									
								
							| @@ -1,9 +0,0 @@ | |||||||
| CMakeCache.txt |  | ||||||
| package-lock.json |  | ||||||
| CMakeFiles		 |  | ||||||
| ixwebsocket_unittest	 |  | ||||||
| cmake_install.cmake	 |  | ||||||
| node_modules |  | ||||||
| ixwebsocket |  | ||||||
| Makefile |  | ||||||
| build |  | ||||||
| @@ -1,30 +0,0 @@ | |||||||
| # |  | ||||||
| # Author: Benjamin Sergeant |  | ||||||
| # Copyright (c) 2018 Machine Zone, Inc. All rights reserved. |  | ||||||
| # |  | ||||||
|  |  | ||||||
| cmake_minimum_required (VERSION 3.4.1) |  | ||||||
| project (broadcast_server) |  | ||||||
|  |  | ||||||
| # There's -Weverything too for clang |  | ||||||
| set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic -Wshorten-64-to-32") |  | ||||||
|  |  | ||||||
| set (OPENSSL_PREFIX /usr/local/opt/openssl) # Homebrew openssl |  | ||||||
|  |  | ||||||
| set (CMAKE_CXX_STANDARD 14) |  | ||||||
|  |  | ||||||
| option(USE_TLS "Add TLS support" ON) |  | ||||||
|  |  | ||||||
| add_subdirectory(${PROJECT_SOURCE_DIR}/../.. ixwebsocket) |  | ||||||
|  |  | ||||||
| include_directories(broadcast_server .) |  | ||||||
|  |  | ||||||
| add_executable(broadcast_server  |  | ||||||
|   broadcast_server.cpp) |  | ||||||
|  |  | ||||||
| if (APPLE AND USE_TLS) |  | ||||||
|     target_link_libraries(broadcast_server "-framework foundation" "-framework security") |  | ||||||
| endif() |  | ||||||
|  |  | ||||||
| target_link_libraries(broadcast_server ixwebsocket) |  | ||||||
| install(TARGETS broadcast_server DESTINATION bin) |  | ||||||
| @@ -1,74 +0,0 @@ | |||||||
| /* |  | ||||||
|  *  broadcast_server.cpp |  | ||||||
|  *  Author: Benjamin Sergeant |  | ||||||
|  *  Copyright (c) 2018 Machine Zone, Inc. All rights reserved. |  | ||||||
|  */ |  | ||||||
|  |  | ||||||
| #include <iostream> |  | ||||||
| #include <sstream> |  | ||||||
| #include <ixwebsocket/IXWebSocketServer.h> |  | ||||||
|  |  | ||||||
| int main(int argc, char** argv) |  | ||||||
| { |  | ||||||
|     int port = 8080; |  | ||||||
|     if (argc == 2) |  | ||||||
|     { |  | ||||||
|         std::stringstream ss; |  | ||||||
|         ss << argv[1]; |  | ||||||
|         ss >> port; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     ix::WebSocketServer server(port); |  | ||||||
|  |  | ||||||
|     server.setOnConnectionCallback( |  | ||||||
|         [&server](std::shared_ptr<ix::WebSocket> webSocket) |  | ||||||
|         { |  | ||||||
|             webSocket->setOnMessageCallback( |  | ||||||
|                 [webSocket, &server](ix::WebSocketMessageType messageType, |  | ||||||
|                    const std::string& str, |  | ||||||
|                    size_t wireSize, |  | ||||||
|                    const ix::WebSocketErrorInfo& error, |  | ||||||
|                    const ix::WebSocketOpenInfo& openInfo, |  | ||||||
|                    const ix::WebSocketCloseInfo& closeInfo) |  | ||||||
|                 { |  | ||||||
|                     if (messageType == ix::WebSocket_MessageType_Open) |  | ||||||
|                     { |  | ||||||
|                         std::cerr << "New connection" << std::endl; |  | ||||||
|                         std::cerr << "Uri: " << openInfo.uri << std::endl; |  | ||||||
|                         std::cerr << "Headers:" << std::endl; |  | ||||||
|                         for (auto it : openInfo.headers) |  | ||||||
|                         { |  | ||||||
|                             std::cerr << it.first << ": " << it.second << std::endl; |  | ||||||
|                         } |  | ||||||
|                     } |  | ||||||
|                     else if (messageType == ix::WebSocket_MessageType_Close) |  | ||||||
|                     { |  | ||||||
|                         std::cerr << "Closed connection" << std::endl; |  | ||||||
|                     } |  | ||||||
|                     else if (messageType == ix::WebSocket_MessageType_Message) |  | ||||||
|                     { |  | ||||||
|                         for (auto&& client : server.getClients()) |  | ||||||
|                         { |  | ||||||
|                             if (client != webSocket) |  | ||||||
|                             { |  | ||||||
|                                 client->send(str); |  | ||||||
|                             } |  | ||||||
|                         } |  | ||||||
|                     } |  | ||||||
|                 } |  | ||||||
|             ); |  | ||||||
|         } |  | ||||||
|     ); |  | ||||||
|  |  | ||||||
|     auto res = server.listen(); |  | ||||||
|     if (!res.first) |  | ||||||
|     { |  | ||||||
|         std::cerr << res.second << std::endl; |  | ||||||
|         return 1; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     server.start(); |  | ||||||
|     server.wait(); |  | ||||||
|  |  | ||||||
|     return 0; |  | ||||||
| } |  | ||||||
							
								
								
									
										3
									
								
								examples/chat/.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										3
									
								
								examples/chat/.gitignore
									
									
									
									
										vendored
									
									
								
							| @@ -1,3 +0,0 @@ | |||||||
| build |  | ||||||
| venv |  | ||||||
| node_modules |  | ||||||
| @@ -1,23 +0,0 @@ | |||||||
| # |  | ||||||
| # cmd_websocket_chat.cpp |  | ||||||
| # Author: Benjamin Sergeant |  | ||||||
| # Copyright (c) 2018 Machine Zone, Inc. All rights reserved. |  | ||||||
| # |  | ||||||
|  |  | ||||||
| cmake_minimum_required (VERSION 3.4.1) |  | ||||||
| project (cmd_websocket_chat) |  | ||||||
|  |  | ||||||
| set (CMAKE_CXX_STANDARD 14) |  | ||||||
|  |  | ||||||
| option(USE_TLS "Add TLS support" ON) |  | ||||||
|  |  | ||||||
| add_subdirectory(${PROJECT_SOURCE_DIR}/../.. ixwebsocket) |  | ||||||
|  |  | ||||||
| add_executable(cmd_websocket_chat cmd_websocket_chat.cpp) |  | ||||||
|  |  | ||||||
| if (APPLE AND USE_TLS) |  | ||||||
|     target_link_libraries(cmd_websocket_chat "-framework foundation" "-framework security") |  | ||||||
| endif() |  | ||||||
|  |  | ||||||
| target_link_libraries(cmd_websocket_chat ixwebsocket) |  | ||||||
| install(TARGETS cmd_websocket_chat DESTINATION bin) |  | ||||||
| @@ -1,39 +0,0 @@ | |||||||
| # Building |  | ||||||
|  |  | ||||||
| 1. cmake -G . |  | ||||||
| 2. make |  | ||||||
|  |  | ||||||
| ## Disable TLS |  | ||||||
|  |  | ||||||
| chat$ cmake -DUSE_TLS=OFF . |  | ||||||
| -- Configuring done |  | ||||||
| -- Generating done |  | ||||||
| -- Build files have been written to: /Users/bsergeant/src/foss/ixwebsocket/examples/chat |  | ||||||
| chat$ make |  | ||||||
| Scanning dependencies of target ixwebsocket |  | ||||||
| [ 16%] Building CXX object ixwebsocket/CMakeFiles/ixwebsocket.dir/ixwebsocket/IXSocket.cpp.o |  | ||||||
| [ 33%] Building CXX object ixwebsocket/CMakeFiles/ixwebsocket.dir/ixwebsocket/IXWebSocket.cpp.o |  | ||||||
| [ 50%] Building CXX object ixwebsocket/CMakeFiles/ixwebsocket.dir/ixwebsocket/IXWebSocketTransport.cpp.o |  | ||||||
| [ 66%] Linking CXX static library libixwebsocket.a |  | ||||||
| [ 66%] Built target ixwebsocket |  | ||||||
| [ 83%] Linking CXX executable cmd_websocket_chat |  | ||||||
| [100%] Built target cmd_websocket_chat |  | ||||||
|  |  | ||||||
| ## Enable TLS (default) |  | ||||||
|  |  | ||||||
| ``` |  | ||||||
| chat$ cmake -DUSE_TLS=ON . |  | ||||||
| -- Configuring done |  | ||||||
| -- Generating done |  | ||||||
| -- Build files have been written to: /Users/bsergeant/src/foss/ixwebsocket/examples/chat |  | ||||||
| (venv) chat$ make |  | ||||||
| Scanning dependencies of target ixwebsocket |  | ||||||
| [ 14%] Building CXX object ixwebsocket/CMakeFiles/ixwebsocket.dir/ixwebsocket/IXSocket.cpp.o |  | ||||||
| [ 28%] Building CXX object ixwebsocket/CMakeFiles/ixwebsocket.dir/ixwebsocket/IXWebSocket.cpp.o |  | ||||||
| [ 42%] Building CXX object ixwebsocket/CMakeFiles/ixwebsocket.dir/ixwebsocket/IXWebSocketTransport.cpp.o |  | ||||||
| [ 57%] Building CXX object ixwebsocket/CMakeFiles/ixwebsocket.dir/ixwebsocket/IXSocketAppleSSL.cpp.o |  | ||||||
| [ 71%] Linking CXX static library libixwebsocket.a |  | ||||||
| [ 71%] Built target ixwebsocket |  | ||||||
| [ 85%] Linking CXX executable cmd_websocket_chat |  | ||||||
| [100%] Built target cmd_websocket_chat |  | ||||||
| ``` |  | ||||||
| @@ -1,15 +0,0 @@ | |||||||
| #!/bin/sh |  | ||||||
| # |  | ||||||
| # Author: Benjamin Sergeant |  | ||||||
| # Copyright (c) 2017-2018 Machine Zone, Inc. All rights reserved. |  | ||||||
| # |  | ||||||
|  |  | ||||||
| # 'manual' way of building. You can also use cmake. |  | ||||||
|  |  | ||||||
| g++ --std=c++11 \ |  | ||||||
|     ../../ixwebsocket/IXSocket.cpp	\ |  | ||||||
|     ../../ixwebsocket/IXWebSocketTransport.cpp \ |  | ||||||
|     ../../ixwebsocket/IXWebSocket.cpp \ |  | ||||||
|     -I ../.. \ |  | ||||||
|     cmd_websocket_chat.cpp \ |  | ||||||
|     -o cmd_websocket_chat |  | ||||||
| @@ -1,17 +0,0 @@ | |||||||
| #!/bin/sh |  | ||||||
| # |  | ||||||
| # Author: Benjamin Sergeant |  | ||||||
| # Copyright (c) 2017-2018 Machine Zone, Inc. All rights reserved. |  | ||||||
| # |  | ||||||
|  |  | ||||||
| # 'manual' way of building. You can also use cmake. |  | ||||||
|  |  | ||||||
| clang++ --std=c++11 --stdlib=libc++ \ |  | ||||||
|     ../../ixwebsocket/IXSocket.cpp	\ |  | ||||||
|     ../../ixwebsocket/IXWebSocketTransport.cpp \ |  | ||||||
|     ../../ixwebsocket/IXSocketAppleSSL.cpp	\ |  | ||||||
|     ../../ixwebsocket/IXWebSocket.cpp \ |  | ||||||
|     cmd_websocket_chat.cpp \ |  | ||||||
|     -o cmd_websocket_chat \ |  | ||||||
|     -framework Security \ |  | ||||||
|     -framework Foundation |  | ||||||
							
								
								
									
										31
									
								
								examples/chat/package-lock.json
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										31
									
								
								examples/chat/package-lock.json
									
									
									
										generated
									
									
									
								
							| @@ -1,31 +0,0 @@ | |||||||
| { |  | ||||||
|   "requires": true, |  | ||||||
|   "lockfileVersion": 1, |  | ||||||
|   "dependencies": { |  | ||||||
|     "async-limiter": { |  | ||||||
|       "version": "1.0.0", |  | ||||||
|       "resolved": "https://registry.npmjs.org/async-limiter/-/async-limiter-1.0.0.tgz", |  | ||||||
|       "integrity": "sha512-jp/uFnooOiO+L211eZOoSyzpOITMXx1rBITauYykG3BRYPu8h0UcxsPNB04RR5vo4Tyz3+ay17tR6JVf9qzYWg==" |  | ||||||
|     }, |  | ||||||
|     "safe-buffer": { |  | ||||||
|       "version": "5.1.2", |  | ||||||
|       "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", |  | ||||||
|       "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" |  | ||||||
|     }, |  | ||||||
|     "ultron": { |  | ||||||
|       "version": "1.1.1", |  | ||||||
|       "resolved": "https://registry.npmjs.org/ultron/-/ultron-1.1.1.tgz", |  | ||||||
|       "integrity": "sha512-UIEXBNeYmKptWH6z8ZnqTeS8fV74zG0/eRU9VGkpzz+LIJNs8W/zM/L+7ctCkRrgbNnnR0xxw4bKOr0cW0N0Og==" |  | ||||||
|     }, |  | ||||||
|     "ws": { |  | ||||||
|       "version": "3.3.3", |  | ||||||
|       "resolved": "https://registry.npmjs.org/ws/-/ws-3.3.3.tgz", |  | ||||||
|       "integrity": "sha512-nnWLa/NwZSt4KQJu51MYlCcSQ5g7INpOrOMt4XV8j4dqTXdmlUmSHQ8/oLC069ckre0fRsgfvsKwbTdtKLCDkA==", |  | ||||||
|       "requires": { |  | ||||||
|         "async-limiter": "1.0.0", |  | ||||||
|         "safe-buffer": "5.1.2", |  | ||||||
|         "ultron": "1.1.1" |  | ||||||
|       } |  | ||||||
|     } |  | ||||||
|   } |  | ||||||
| } |  | ||||||
| @@ -1,6 +0,0 @@ | |||||||
| { |  | ||||||
|   "dependencies": { |  | ||||||
|     "msgpack-js": "^0.3.0", |  | ||||||
|     "ws": "^3.3.3" |  | ||||||
|   } |  | ||||||
| } |  | ||||||
							
								
								
									
										22
									
								
								examples/cobra_publisher/ixcrypto/IXHash.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								examples/cobra_publisher/ixcrypto/IXHash.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,22 @@ | |||||||
|  | /* | ||||||
|  |  *  IXHash.h | ||||||
|  |  *  Author: Benjamin Sergeant | ||||||
|  |  *  Copyright (c) 2018 Machine Zone. All rights reserved. | ||||||
|  |  */ | ||||||
|  |  | ||||||
|  | #include <string> | ||||||
|  |  | ||||||
|  | namespace ix | ||||||
|  | { | ||||||
|  |     uint64_t djb2Hash(const std::string& data) | ||||||
|  |     { | ||||||
|  |         uint64_t hashAddress = 5381; | ||||||
|  |  | ||||||
|  |         for (auto& c : data) | ||||||
|  |         { | ||||||
|  |             hashAddress = ((hashAddress << 5) + hashAddress) + c; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         return hashAddress; | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										15
									
								
								examples/cobra_publisher/ixcrypto/IXHash.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								examples/cobra_publisher/ixcrypto/IXHash.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,15 @@ | |||||||
|  | /* | ||||||
|  |  *  IXHash.h | ||||||
|  |  *  Author: Benjamin Sergeant | ||||||
|  |  *  Copyright (c) 2018 Machine Zone. All rights reserved. | ||||||
|  |  */ | ||||||
|  |  | ||||||
|  | #pragma once | ||||||
|  |  | ||||||
|  | #include <string> | ||||||
|  |  | ||||||
|  | namespace ix | ||||||
|  | { | ||||||
|  |     uint64_t djb2Hash(const std::string& data); | ||||||
|  | } | ||||||
|  |  | ||||||
							
								
								
									
										75
									
								
								examples/cobra_publisher/ixcrypto/IXUuid.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										75
									
								
								examples/cobra_publisher/ixcrypto/IXUuid.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,75 @@ | |||||||
|  | /* | ||||||
|  |  *  IXUuid.cpp | ||||||
|  |  *  Author: Benjamin Sergeant | ||||||
|  |  *  Copyright (c) 2018 Machine Zone. All rights reserved. | ||||||
|  |  */ | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Generate a random uuid similar to the uuid python module | ||||||
|  |  * | ||||||
|  |  * >>> import uuid | ||||||
|  |  * >>> uuid.uuid4().hex | ||||||
|  |  * 'bec08155b37d4050a1f3c3fa0276bf12' | ||||||
|  |  * | ||||||
|  |  * Code adapted from https://github.com/r-lyeh-archived/sole | ||||||
|  |  */ | ||||||
|  |  | ||||||
|  | #include "IXUuid.h" | ||||||
|  |  | ||||||
|  | #include <sstream> | ||||||
|  | #include <string> | ||||||
|  | #include <iomanip> | ||||||
|  | #include <random> | ||||||
|  |  | ||||||
|  |  | ||||||
|  | namespace ix | ||||||
|  | { | ||||||
|  |     class Uuid | ||||||
|  |     { | ||||||
|  |         public: | ||||||
|  |             Uuid(); | ||||||
|  |             std::string toString() const; | ||||||
|  |  | ||||||
|  |         private: | ||||||
|  |             uint64_t _ab; | ||||||
|  |             uint64_t _cd; | ||||||
|  |     }; | ||||||
|  |  | ||||||
|  |     Uuid::Uuid() | ||||||
|  |     { | ||||||
|  |         static std::random_device rd; | ||||||
|  |         static std::uniform_int_distribution<uint64_t> dist(0, (uint64_t)(~0)); | ||||||
|  |  | ||||||
|  |         _ab = dist(rd); | ||||||
|  |         _cd = dist(rd); | ||||||
|  |  | ||||||
|  |         _ab = (_ab & 0xFFFFFFFFFFFF0FFFULL) | 0x0000000000004000ULL; | ||||||
|  |         _cd = (_cd & 0x3FFFFFFFFFFFFFFFULL) | 0x8000000000000000ULL; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     std::string Uuid::toString() const | ||||||
|  |     { | ||||||
|  |         std::stringstream ss; | ||||||
|  |         ss << std::hex << std::nouppercase << std::setfill('0'); | ||||||
|  |  | ||||||
|  |         uint32_t a = (_ab >> 32); | ||||||
|  |         uint32_t b = (_ab & 0xFFFFFFFF); | ||||||
|  |         uint32_t c = (_cd >> 32); | ||||||
|  |         uint32_t d = (_cd & 0xFFFFFFFF); | ||||||
|  |  | ||||||
|  |         ss << std::setw(8) << (a); | ||||||
|  |         ss << std::setw(4) << (b >> 16); | ||||||
|  |         ss << std::setw(4) << (b & 0xFFFF); | ||||||
|  |         ss << std::setw(4) << (c >> 16 ); | ||||||
|  |         ss << std::setw(4) << (c & 0xFFFF); | ||||||
|  |         ss << std::setw(8) << d; | ||||||
|  |  | ||||||
|  |         return ss.str(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     std::string uuid4() | ||||||
|  |     { | ||||||
|  |         Uuid id; | ||||||
|  |         return id.toString(); | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										17
									
								
								examples/cobra_publisher/ixcrypto/IXUuid.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								examples/cobra_publisher/ixcrypto/IXUuid.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,17 @@ | |||||||
|  | /* | ||||||
|  |  *  IXUuid.h | ||||||
|  |  *  Author: Benjamin Sergeant | ||||||
|  |  *  Copyright (c) 2017 Machine Zone. All rights reserved. | ||||||
|  |  */ | ||||||
|  | #pragma once | ||||||
|  |  | ||||||
|  | #include <string> | ||||||
|  |  | ||||||
|  | namespace ix | ||||||
|  | { | ||||||
|  |    /** | ||||||
|  |     * Generate a random uuid | ||||||
|  |     */ | ||||||
|  |    std::string uuid4(); | ||||||
|  |  | ||||||
|  | } | ||||||
| @@ -1,30 +0,0 @@ | |||||||
| # |  | ||||||
| # Author: Benjamin Sergeant |  | ||||||
| # Copyright (c) 2018 Machine Zone, Inc. All rights reserved. |  | ||||||
| # |  | ||||||
|  |  | ||||||
| cmake_minimum_required (VERSION 3.4.1) |  | ||||||
| project (echo_server) |  | ||||||
|  |  | ||||||
| # There's -Weverything too for clang |  | ||||||
| set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic -Wshorten-64-to-32") |  | ||||||
|  |  | ||||||
| set (OPENSSL_PREFIX /usr/local/opt/openssl) # Homebrew openssl |  | ||||||
|  |  | ||||||
| set (CMAKE_CXX_STANDARD 14) |  | ||||||
|  |  | ||||||
| option(USE_TLS "Add TLS support" ON) |  | ||||||
|  |  | ||||||
| add_subdirectory(${PROJECT_SOURCE_DIR}/../.. ixwebsocket) |  | ||||||
|  |  | ||||||
| include_directories(echo_server .) |  | ||||||
|  |  | ||||||
| add_executable(echo_server  |  | ||||||
|   echo_server.cpp) |  | ||||||
|  |  | ||||||
| if (APPLE AND USE_TLS) |  | ||||||
|     target_link_libraries(echo_server "-framework foundation" "-framework security") |  | ||||||
| endif() |  | ||||||
|  |  | ||||||
| target_link_libraries(echo_server ixwebsocket) |  | ||||||
| install(TARGETS echo_server DESTINATION bin) |  | ||||||
| @@ -1,68 +0,0 @@ | |||||||
| /* |  | ||||||
|  *  echo_server.cpp |  | ||||||
|  *  Author: Benjamin Sergeant |  | ||||||
|  *  Copyright (c) 2018 Machine Zone, Inc. All rights reserved. |  | ||||||
|  */ |  | ||||||
|  |  | ||||||
| #include <iostream> |  | ||||||
| #include <sstream> |  | ||||||
| #include <ixwebsocket/IXWebSocketServer.h> |  | ||||||
|  |  | ||||||
| int main(int argc, char** argv) |  | ||||||
| { |  | ||||||
|     int port = 8080; |  | ||||||
|     if (argc == 2) |  | ||||||
|     { |  | ||||||
|         std::stringstream ss; |  | ||||||
|         ss << argv[1]; |  | ||||||
|         ss >> port; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     ix::WebSocketServer server(port); |  | ||||||
|  |  | ||||||
|     server.setOnConnectionCallback( |  | ||||||
|         [&server](std::shared_ptr<ix::WebSocket> webSocket) |  | ||||||
|         { |  | ||||||
|             webSocket->setOnMessageCallback( |  | ||||||
|                 [webSocket, &server](ix::WebSocketMessageType messageType, |  | ||||||
|                    const std::string& str, |  | ||||||
|                    size_t wireSize, |  | ||||||
|                    const ix::WebSocketErrorInfo& error, |  | ||||||
|                    const ix::WebSocketOpenInfo& openInfo, |  | ||||||
|                    const ix::WebSocketCloseInfo& closeInfo) |  | ||||||
|                 { |  | ||||||
|                     if (messageType == ix::WebSocket_MessageType_Open) |  | ||||||
|                     { |  | ||||||
|                         std::cerr << "New connection" << std::endl; |  | ||||||
|                         std::cerr << "Uri: " << openInfo.uri << std::endl; |  | ||||||
|                         std::cerr << "Headers:" << std::endl; |  | ||||||
|                         for (auto it : openInfo.headers) |  | ||||||
|                         { |  | ||||||
|                             std::cerr << it.first << ": " << it.second << std::endl; |  | ||||||
|                         } |  | ||||||
|                     } |  | ||||||
|                     else if (messageType == ix::WebSocket_MessageType_Close) |  | ||||||
|                     { |  | ||||||
|                         std::cerr << "Closed connection" << std::endl; |  | ||||||
|                     } |  | ||||||
|                     else if (messageType == ix::WebSocket_MessageType_Message) |  | ||||||
|                     { |  | ||||||
|                         webSocket->send(str); |  | ||||||
|                     } |  | ||||||
|                 } |  | ||||||
|             ); |  | ||||||
|         } |  | ||||||
|     ); |  | ||||||
|  |  | ||||||
|     auto res = server.listen(); |  | ||||||
|     if (!res.first) |  | ||||||
|     { |  | ||||||
|         std::cerr << res.second << std::endl; |  | ||||||
|         return 1; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     server.start(); |  | ||||||
|     server.wait(); |  | ||||||
|  |  | ||||||
|     return 0; |  | ||||||
| } |  | ||||||
| @@ -1,27 +0,0 @@ | |||||||
| # |  | ||||||
| # Author: Benjamin Sergeant |  | ||||||
| # Copyright (c) 2018 Machine Zone, Inc. All rights reserved. |  | ||||||
| # |  | ||||||
|  |  | ||||||
| cmake_minimum_required (VERSION 3.4.1) |  | ||||||
| project (ping_pong) |  | ||||||
|  |  | ||||||
| set (CMAKE_CXX_STANDARD 14) |  | ||||||
|  |  | ||||||
| option(USE_TLS "Add TLS support" ON) |  | ||||||
|  |  | ||||||
| add_subdirectory(${PROJECT_SOURCE_DIR}/../.. ixwebsocket) |  | ||||||
|  |  | ||||||
| add_executable(ping_pong ping_pong.cpp) |  | ||||||
|  |  | ||||||
| if (APPLE AND USE_TLS) |  | ||||||
|     target_link_libraries(ping_pong "-framework foundation" "-framework security") |  | ||||||
| endif() |  | ||||||
|  |  | ||||||
| if (WIN32) |  | ||||||
|     target_link_libraries(ping_pong wsock32 ws2_32) |  | ||||||
|     add_definitions(-D_CRT_SECURE_NO_WARNINGS) |  | ||||||
| endif() |  | ||||||
|  |  | ||||||
| target_link_libraries(ping_pong ixwebsocket) |  | ||||||
| install(TARGETS ping_pong DESTINATION bin) |  | ||||||
| @@ -1,15 +0,0 @@ | |||||||
| #!/bin/sh |  | ||||||
| # |  | ||||||
| # Author: Benjamin Sergeant |  | ||||||
| # Copyright (c) 2017-2018 Machine Zone, Inc. All rights reserved. |  | ||||||
| # |  | ||||||
|  |  | ||||||
| # 'manual' way of building. You can also use cmake. |  | ||||||
|  |  | ||||||
| g++ --std=c++11 \ |  | ||||||
|     ../../ixwebsocket/IXSocket.cpp	\ |  | ||||||
|     ../../ixwebsocket/IXWebSocketTransport.cpp \ |  | ||||||
|     ../../ixwebsocket/IXWebSocket.cpp \ |  | ||||||
|     -I ../.. \ |  | ||||||
|     cmd_websocket_chat.cpp \ |  | ||||||
|     -o cmd_websocket_chat |  | ||||||
| @@ -1,17 +0,0 @@ | |||||||
| #!/usr/bin/env python |  | ||||||
|  |  | ||||||
| import asyncio |  | ||||||
| import websockets |  | ||||||
|  |  | ||||||
| async def hello(uri): |  | ||||||
|     async with websockets.connect(uri) as websocket: |  | ||||||
|         await websocket.send("Hello world!") |  | ||||||
|         response = await websocket.recv() |  | ||||||
|         print(response) |  | ||||||
|  |  | ||||||
|         pong_waiter = await websocket.ping('coucou') |  | ||||||
|         ret = await pong_waiter   # only if you want to wait for the pong |  | ||||||
|         print(ret) |  | ||||||
|  |  | ||||||
| asyncio.get_event_loop().run_until_complete( |  | ||||||
|     hello('ws://localhost:5678')) |  | ||||||
| @@ -1,21 +0,0 @@ | |||||||
| #!/usr/bin/env python |  | ||||||
|  |  | ||||||
| import os |  | ||||||
| import asyncio |  | ||||||
| import websockets |  | ||||||
|  |  | ||||||
| async def echo(websocket, path): |  | ||||||
|     async for message in websocket: |  | ||||||
|         print(message) |  | ||||||
|         await websocket.send(message) |  | ||||||
|  |  | ||||||
|         if os.getenv('TEST_CLOSE'): |  | ||||||
|             print('Closing') |  | ||||||
|             # breakpoint() |  | ||||||
|             await websocket.close(1001, 'close message') |  | ||||||
|             # await websocket.close() |  | ||||||
|             break |  | ||||||
|  |  | ||||||
| asyncio.get_event_loop().run_until_complete( |  | ||||||
|     websockets.serve(echo, 'localhost', 5678)) |  | ||||||
| asyncio.get_event_loop().run_forever() |  | ||||||
| @@ -1,9 +0,0 @@ | |||||||
| #!/bin/sh |  | ||||||
|  |  | ||||||
| test -d build || { |  | ||||||
|     mkdir -p build |  | ||||||
|     cd build |  | ||||||
|     cmake .. |  | ||||||
| } |  | ||||||
| (cd build ; make) |  | ||||||
| ./build/ping_pong ws://localhost:5678 |  | ||||||
							
								
								
									
										3
									
								
								examples/ws_connect/.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										3
									
								
								examples/ws_connect/.gitignore
									
									
									
									
										vendored
									
									
								
							| @@ -1,3 +0,0 @@ | |||||||
| build |  | ||||||
| venv |  | ||||||
| node_modules |  | ||||||
| @@ -1,22 +0,0 @@ | |||||||
| # |  | ||||||
| # Author: Benjamin Sergeant |  | ||||||
| # Copyright (c) 2018 Machine Zone, Inc. All rights reserved. |  | ||||||
| # |  | ||||||
|  |  | ||||||
| cmake_minimum_required (VERSION 3.4.1) |  | ||||||
| project (ws_connect) |  | ||||||
|  |  | ||||||
| set (CMAKE_CXX_STANDARD 14) |  | ||||||
|  |  | ||||||
| option(USE_TLS "Add TLS support" ON) |  | ||||||
|  |  | ||||||
| add_subdirectory(${PROJECT_SOURCE_DIR}/../.. ixwebsocket) |  | ||||||
|  |  | ||||||
| add_executable(ws_connect ws_connect.cpp) |  | ||||||
|  |  | ||||||
| if (APPLE AND USE_TLS) |  | ||||||
|     target_link_libraries(ws_connect "-framework foundation" "-framework security") |  | ||||||
| endif() |  | ||||||
|  |  | ||||||
| target_link_libraries(ws_connect ixwebsocket) |  | ||||||
| install(TARGETS ws_connect DESTINATION bin) |  | ||||||
| @@ -1,11 +0,0 @@ | |||||||
| # Building |  | ||||||
|  |  | ||||||
| 1. mkdir build |  | ||||||
| 2. cd build |  | ||||||
| 3. cmake .. |  | ||||||
| 4. make |  | ||||||
|  |  | ||||||
| ## Disable TLS |  | ||||||
|  |  | ||||||
| * Enable: `cmake -DUSE_TLS=OFF ..` |  | ||||||
| * Disable: `cmake -DUSE_TLS=ON ..` |  | ||||||
| @@ -1,25 +0,0 @@ | |||||||
| #!/bin/sh |  | ||||||
| # |  | ||||||
| # Author: Benjamin Sergeant |  | ||||||
| # Copyright (c) 2017-2018 Machine Zone, Inc. All rights reserved. |  | ||||||
| # |  | ||||||
|  |  | ||||||
| # 'manual' way of building. You can also use cmake. |  | ||||||
|  |  | ||||||
| g++ --std=c++11 \ |  | ||||||
|     -DIXWEBSOCKET_USE_TLS \ |  | ||||||
|     -g \ |  | ||||||
|     ../../ixwebsocket/IXEventFd.cpp	\ |  | ||||||
|     ../../ixwebsocket/IXSocket.cpp	\ |  | ||||||
|     ../../ixwebsocket/IXSetThreadName.cpp	\ |  | ||||||
|     ../../ixwebsocket/IXWebSocketTransport.cpp \ |  | ||||||
|     ../../ixwebsocket/IXWebSocket.cpp \ |  | ||||||
|     ../../ixwebsocket/IXDNSLookup.cpp \ |  | ||||||
|     ../../ixwebsocket/IXSocketConnect.cpp \ |  | ||||||
|     ../../ixwebsocket/IXSocketOpenSSL.cpp \ |  | ||||||
|     ../../ixwebsocket/IXWebSocketPerMessageDeflate.cpp \ |  | ||||||
|     ../../ixwebsocket/IXWebSocketPerMessageDeflateOptions.cpp \ |  | ||||||
|     -I ../.. \ |  | ||||||
|     ws_connect.cpp \ |  | ||||||
|     -o ws_connect \ |  | ||||||
|     -lcrypto -lssl -lz -lpthread |  | ||||||
							
								
								
									
										1
									
								
								examples/ws_receive/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								examples/ws_receive/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | |||||||
|  | build | ||||||
							
								
								
									
										30
									
								
								examples/ws_receive/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								examples/ws_receive/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,30 @@ | |||||||
|  | # | ||||||
|  | # Author: Benjamin Sergeant | ||||||
|  | # Copyright (c) 2019 Machine Zone, Inc. All rights reserved. | ||||||
|  | # | ||||||
|  |  | ||||||
|  | cmake_minimum_required (VERSION 3.4.1) | ||||||
|  | project (ws_receive) | ||||||
|  |  | ||||||
|  | # There's -Weverything too for clang | ||||||
|  | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic -Wshorten-64-to-32") | ||||||
|  |  | ||||||
|  | set (CMAKE_CXX_STANDARD 14) | ||||||
|  |  | ||||||
|  | option(USE_TLS "Add TLS support" ON) | ||||||
|  |  | ||||||
|  | add_subdirectory(${PROJECT_SOURCE_DIR}/../.. ixwebsocket) | ||||||
|  |  | ||||||
|  | include_directories(ws_receive .) | ||||||
|  |  | ||||||
|  | add_executable(ws_receive  | ||||||
|  |   jsoncpp/jsoncpp.cpp | ||||||
|  |   ixcrypto/IXBase64.cpp | ||||||
|  |   ixcrypto/IXHash.cpp | ||||||
|  |   ws_receive.cpp) | ||||||
|  |  | ||||||
|  | if (APPLE AND USE_TLS) | ||||||
|  |     target_link_libraries(ws_receive "-framework foundation" "-framework security") | ||||||
|  | endif() | ||||||
|  |  | ||||||
|  | target_link_libraries(ws_receive ixwebsocket) | ||||||
							
								
								
									
										1
									
								
								examples/ws_receive/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								examples/ws_receive/README.md
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | |||||||
|  | ws_receive is a simple server upload program. It needs to be used in conjonction with ws_send. | ||||||
							
								
								
									
										1
									
								
								examples/ws_receive/ixcrypto
									
									
									
									
									
										Symbolic link
									
								
							
							
						
						
									
										1
									
								
								examples/ws_receive/ixcrypto
									
									
									
									
									
										Symbolic link
									
								
							| @@ -0,0 +1 @@ | |||||||
|  | ../cobra_publisher/ixcrypto | ||||||
							
								
								
									
										29
									
								
								examples/ws_receive/package-lock.json
									
									
									
										generated
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								examples/ws_receive/package-lock.json
									
									
									
										generated
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,29 @@ | |||||||
|  | { | ||||||
|  |   "requires": true, | ||||||
|  |   "lockfileVersion": 1, | ||||||
|  |   "dependencies": { | ||||||
|  |     "async-limiter": { | ||||||
|  |       "version": "1.0.0", | ||||||
|  |       "resolved": "https://registry.npmjs.org/async-limiter/-/async-limiter-1.0.0.tgz", | ||||||
|  |       "integrity": "sha512-jp/uFnooOiO+L211eZOoSyzpOITMXx1rBITauYykG3BRYPu8h0UcxsPNB04RR5vo4Tyz3+ay17tR6JVf9qzYWg==" | ||||||
|  |     }, | ||||||
|  |     "base-64": { | ||||||
|  |       "version": "0.1.0", | ||||||
|  |       "resolved": "https://registry.npmjs.org/base-64/-/base-64-0.1.0.tgz", | ||||||
|  |       "integrity": "sha1-eAqZyE59YAJgNhURxId2E78k9rs=" | ||||||
|  |     }, | ||||||
|  |     "djb2": { | ||||||
|  |       "version": "0.0.2", | ||||||
|  |       "resolved": "https://registry.npmjs.org/djb2/-/djb2-0.0.2.tgz", | ||||||
|  |       "integrity": "sha1-RAs4kao6uBQrVNRpsXe66p6W5O8=" | ||||||
|  |     }, | ||||||
|  |     "ws": { | ||||||
|  |       "version": "6.1.4", | ||||||
|  |       "resolved": "https://registry.npmjs.org/ws/-/ws-6.1.4.tgz", | ||||||
|  |       "integrity": "sha512-eqZfL+NE/YQc1/ZynhojeV8q+H050oR8AZ2uIev7RU10svA9ZnJUddHcOUZTJLinZ9yEfdA2kSATS2qZK5fhJA==", | ||||||
|  |       "requires": { | ||||||
|  |         "async-limiter": "1.0.0" | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | } | ||||||
							
								
								
									
										153
									
								
								examples/ws_receive/ws_receive.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										153
									
								
								examples/ws_receive/ws_receive.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,153 @@ | |||||||
|  | /* | ||||||
|  |  *  ws_receive.cpp | ||||||
|  |  *  Author: Benjamin Sergeant | ||||||
|  |  *  Copyright (c) 2018 Machine Zone, Inc. All rights reserved. | ||||||
|  |  */ | ||||||
|  |  | ||||||
|  | #include <iostream> | ||||||
|  | #include <sstream> | ||||||
|  | #include <fstream> | ||||||
|  | #include <ixwebsocket/IXWebSocketServer.h> | ||||||
|  | #include <jsoncpp/json/json.h> | ||||||
|  | #include <ixcrypto/IXBase64.h> | ||||||
|  | #include <ixcrypto/IXHash.h> | ||||||
|  |  | ||||||
|  |  | ||||||
|  | namespace | ||||||
|  | { | ||||||
|  |     // We should cleanup the file name and full path further to remove .. as well | ||||||
|  |     std::string extractFilename(const std::string& path) | ||||||
|  |     { | ||||||
|  |         std::string filename("filename.conf"); | ||||||
|  |         std::string::size_type idx; | ||||||
|  |  | ||||||
|  |         idx = path.rfind('/'); | ||||||
|  |         if (idx != std::string::npos) | ||||||
|  |         { | ||||||
|  |             std::string filename = path.substr(idx+1); | ||||||
|  |             return filename; | ||||||
|  |         } | ||||||
|  |         else | ||||||
|  |         { | ||||||
|  |             return std::string(); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | namespace ix | ||||||
|  | { | ||||||
|  |     void errorHandler(const std::string& errMsg, | ||||||
|  |                       const std::string& id, | ||||||
|  |                       std::shared_ptr<ix::WebSocket> webSocket) | ||||||
|  |     { | ||||||
|  |         Json::Value pdu; | ||||||
|  |         pdu["kind"] = "error"; | ||||||
|  |         pdu["id"] = id; | ||||||
|  |         pdu["message"] = errMsg; | ||||||
|  |         webSocket->send(pdu.toStyledString()); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     void messageHandler(const std::string& str, | ||||||
|  |                         std::shared_ptr<ix::WebSocket> webSocket) | ||||||
|  |     { | ||||||
|  |         std::cerr << "Received message: " << str.size() << std::endl; | ||||||
|  |  | ||||||
|  |         Json::Value data; | ||||||
|  |         Json::Reader reader; | ||||||
|  |         if (!reader.parse(str, data)) | ||||||
|  |         { | ||||||
|  |             errorHandler("Invalid JSON", std::string(), webSocket); | ||||||
|  |             return; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         std::cout << "id: " << data["id"].asString() << std::endl; | ||||||
|  |  | ||||||
|  |         std::string content = ix::base64_decode(data["content"].asString()); | ||||||
|  |         std::cout << "Content size: " << content.size() << std::endl; | ||||||
|  |  | ||||||
|  |         // Validate checksum | ||||||
|  |         uint64_t cksum = ix::djb2Hash(data["content"].asString()); | ||||||
|  |         uint64_t cksumRef = data["djb2_hash"].asUInt64(); | ||||||
|  |  | ||||||
|  |         std::cout << "Computed hash: " << cksum << std::endl; | ||||||
|  |         std::cout << "Reference hash: " << cksumRef << std::endl; | ||||||
|  |  | ||||||
|  |         if (cksum != cksumRef) | ||||||
|  |         { | ||||||
|  |             errorHandler("Hash mismatch.", std::string(), webSocket); | ||||||
|  |             return; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         std::string filename = data["filename"].asString(); | ||||||
|  |         filename = extractFilename(filename); | ||||||
|  |  | ||||||
|  |         std::ofstream out(filename); | ||||||
|  |         out << content; | ||||||
|  |         out.close(); | ||||||
|  |  | ||||||
|  |         Json::Value pdu; | ||||||
|  |         pdu["ack"] = true; | ||||||
|  |         pdu["id"] = data["id"]; | ||||||
|  |         pdu["filename"] = data["filename"]; | ||||||
|  |         webSocket->send(pdu.toStyledString()); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | int main(int argc, char** argv) | ||||||
|  | { | ||||||
|  |     int port = 8080; | ||||||
|  |     if (argc == 2) | ||||||
|  |     { | ||||||
|  |         std::stringstream ss; | ||||||
|  |         ss << argv[1]; | ||||||
|  |         ss >> port; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     ix::WebSocketServer server(port); | ||||||
|  |  | ||||||
|  |     server.setOnConnectionCallback( | ||||||
|  |         [&server](std::shared_ptr<ix::WebSocket> webSocket) | ||||||
|  |         { | ||||||
|  |             webSocket->setOnMessageCallback( | ||||||
|  |                 [webSocket, &server](ix::WebSocketMessageType messageType, | ||||||
|  |                                      const std::string& str, | ||||||
|  |                                      size_t wireSize, | ||||||
|  |                                      const ix::WebSocketErrorInfo& error, | ||||||
|  |                                      const ix::WebSocketOpenInfo& openInfo, | ||||||
|  |                                      const ix::WebSocketCloseInfo& closeInfo) | ||||||
|  |                 { | ||||||
|  |                     if (messageType == ix::WebSocket_MessageType_Open) | ||||||
|  |                     { | ||||||
|  |                         std::cerr << "New connection" << std::endl; | ||||||
|  |                         std::cerr << "Uri: " << openInfo.uri << std::endl; | ||||||
|  |                         std::cerr << "Headers:" << std::endl; | ||||||
|  |                         for (auto it : openInfo.headers) | ||||||
|  |                         { | ||||||
|  |                             std::cerr << it.first << ": " << it.second << std::endl; | ||||||
|  |                         } | ||||||
|  |                     } | ||||||
|  |                     else if (messageType == ix::WebSocket_MessageType_Close) | ||||||
|  |                     { | ||||||
|  |                         std::cerr << "Closed connection" << std::endl; | ||||||
|  |                     } | ||||||
|  |                     else if (messageType == ix::WebSocket_MessageType_Message) | ||||||
|  |                     { | ||||||
|  |                         messageHandler(str, webSocket); | ||||||
|  |                     } | ||||||
|  |                 } | ||||||
|  |             ); | ||||||
|  |         } | ||||||
|  |     ); | ||||||
|  |  | ||||||
|  |     auto res = server.listen(); | ||||||
|  |     if (!res.first) | ||||||
|  |     { | ||||||
|  |         std::cerr << res.second << std::endl; | ||||||
|  |         return 1; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     server.start(); | ||||||
|  |     server.wait(); | ||||||
|  |  | ||||||
|  |     return 0; | ||||||
|  | } | ||||||
							
								
								
									
										43
									
								
								examples/ws_receive/ws_receive.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								examples/ws_receive/ws_receive.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,43 @@ | |||||||
|  | /* | ||||||
|  |  *  ws_receive.js | ||||||
|  |  *  Author: Benjamin Sergeant | ||||||
|  |  *  Copyright (c) 2019 Machine Zone, Inc. All rights reserved. | ||||||
|  |  */ | ||||||
|  | const WebSocket = require('ws') | ||||||
|  | const djb2 = require('djb2') | ||||||
|  | const fs = require('fs') | ||||||
|  |  | ||||||
|  | const wss = new WebSocket.Server({ port: 8080, | ||||||
|  |                                    perMessageDeflate: false, | ||||||
|  |                                    maxPayload: 1024 * 1024 * 1024 * 1024}); | ||||||
|  |  | ||||||
|  | wss.on('connection', function connection(ws) { | ||||||
|  |   ws.on('message', function incoming(data) { | ||||||
|  |     console.log('Received message') | ||||||
|  |  | ||||||
|  |     let str = data.toString() | ||||||
|  |     let obj = JSON.parse(str) | ||||||
|  |  | ||||||
|  |     console.log(obj.id) | ||||||
|  |     console.log(obj.djb2_hash) | ||||||
|  |     console.log(djb2(obj.content)) | ||||||
|  |  | ||||||
|  |     var content = Buffer.from(obj.content, 'base64') | ||||||
|  |     // let bytes = base64.decode(obj.content) | ||||||
|  |  | ||||||
|  |     let path = obj.filename | ||||||
|  |     fs.writeFile(path, content, function(err) { | ||||||
|  |       if (err) { | ||||||
|  |         throw err | ||||||
|  |       } else { | ||||||
|  |         console.log('wrote data to disk') | ||||||
|  |       } | ||||||
|  |     }); | ||||||
|  |  | ||||||
|  |     let response = { | ||||||
|  |       id: obj.id | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     ws.send(JSON.stringify(response)) | ||||||
|  |   }); | ||||||
|  | }); | ||||||
							
								
								
									
										1
									
								
								examples/ws_send/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								examples/ws_send/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | |||||||
|  | build | ||||||
							
								
								
									
										31
									
								
								examples/ws_send/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								examples/ws_send/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,31 @@ | |||||||
|  | # | ||||||
|  | # Author: Benjamin Sergeant | ||||||
|  | # Copyright (c) 2019 Machine Zone, Inc. All rights reserved. | ||||||
|  | # | ||||||
|  |  | ||||||
|  | cmake_minimum_required (VERSION 3.4.1) | ||||||
|  | project (ws_send) | ||||||
|  |  | ||||||
|  | # There's -Weverything too for clang | ||||||
|  | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic -Wshorten-64-to-32") | ||||||
|  |  | ||||||
|  | set (CMAKE_CXX_STANDARD 14) | ||||||
|  |  | ||||||
|  | option(USE_TLS "Add TLS support" ON) | ||||||
|  |  | ||||||
|  | add_subdirectory(${PROJECT_SOURCE_DIR}/../.. ixwebsocket) | ||||||
|  |  | ||||||
|  | include_directories(ws_send .) | ||||||
|  |  | ||||||
|  | add_executable(ws_send  | ||||||
|  |   jsoncpp/jsoncpp.cpp | ||||||
|  |   ixcrypto/IXBase64.cpp | ||||||
|  |   ixcrypto/IXUuid.cpp | ||||||
|  |   ixcrypto/IXHash.cpp | ||||||
|  |   ws_send.cpp) | ||||||
|  |  | ||||||
|  | if (APPLE AND USE_TLS) | ||||||
|  |     target_link_libraries(ws_send "-framework foundation" "-framework security") | ||||||
|  | endif() | ||||||
|  |  | ||||||
|  | target_link_libraries(ws_send ixwebsocket) | ||||||
							
								
								
									
										1
									
								
								examples/ws_send/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								examples/ws_send/README.md
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | |||||||
|  | ws_send is a simple upload program. It needs to be used in conjonction with ws_receive. | ||||||
							
								
								
									
										1
									
								
								examples/ws_send/ixcrypto
									
									
									
									
									
										Symbolic link
									
								
							
							
						
						
									
										1
									
								
								examples/ws_send/ixcrypto
									
									
									
									
									
										Symbolic link
									
								
							| @@ -0,0 +1 @@ | |||||||
|  | ../cobra_publisher/ixcrypto | ||||||
							
								
								
									
										1
									
								
								examples/ws_send/jsoncpp
									
									
									
									
									
										Symbolic link
									
								
							
							
						
						
									
										1
									
								
								examples/ws_send/jsoncpp
									
									
									
									
									
										Symbolic link
									
								
							| @@ -0,0 +1 @@ | |||||||
|  | ../cobra_publisher/jsoncpp | ||||||
							
								
								
									
										306
									
								
								examples/ws_send/ws_send.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										306
									
								
								examples/ws_send/ws_send.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,306 @@ | |||||||
|  | /* | ||||||
|  |  *  ws_send.cpp | ||||||
|  |  *  Author: Benjamin Sergeant | ||||||
|  |  *  Copyright (c) 2017-2018 Machine Zone, Inc. All rights reserved. | ||||||
|  |  */ | ||||||
|  |  | ||||||
|  | #include <iostream> | ||||||
|  | #include <fstream> | ||||||
|  | #include <sstream> | ||||||
|  | #include <vector> | ||||||
|  | #include <condition_variable> | ||||||
|  | #include <mutex> | ||||||
|  | #include <chrono> | ||||||
|  | #include <ixwebsocket/IXWebSocket.h> | ||||||
|  | #include <ixwebsocket/IXSocket.h> | ||||||
|  | #include <ixcrypto/IXUuid.h> | ||||||
|  | #include <ixcrypto/IXBase64.h> | ||||||
|  | #include <ixcrypto/IXHash.h> | ||||||
|  | #include <jsoncpp/json/json.h> | ||||||
|  |  | ||||||
|  | using namespace ix; | ||||||
|  |  | ||||||
|  | namespace | ||||||
|  | { | ||||||
|  |     void log(const std::string& msg) | ||||||
|  |     { | ||||||
|  |         std::cout << msg << std::endl; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     class WebSocketSender | ||||||
|  |     { | ||||||
|  |         public: | ||||||
|  |             WebSocketSender(const std::string& _url, | ||||||
|  |                             bool enablePerMessageDeflate); | ||||||
|  |  | ||||||
|  |             void subscribe(const std::string& channel); | ||||||
|  |             void start(); | ||||||
|  |             void stop(); | ||||||
|  |  | ||||||
|  |             void waitForConnection(); | ||||||
|  |             void waitForAck(); | ||||||
|  |  | ||||||
|  |             void sendMessage(const std::string& filename, bool throttle); | ||||||
|  |  | ||||||
|  |         private: | ||||||
|  |             std::string _url; | ||||||
|  |             std::string _id; | ||||||
|  |             ix::WebSocket _webSocket; | ||||||
|  |             bool _enablePerMessageDeflate; | ||||||
|  |  | ||||||
|  |             std::mutex _conditionVariableMutex; | ||||||
|  |             std::condition_variable _condition; | ||||||
|  |     }; | ||||||
|  |  | ||||||
|  |     WebSocketSender::WebSocketSender(const std::string& url, | ||||||
|  |                                      bool enablePerMessageDeflate) : | ||||||
|  |         _url(url), | ||||||
|  |         _enablePerMessageDeflate(enablePerMessageDeflate) | ||||||
|  |     { | ||||||
|  |         ; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     void WebSocketSender::stop() | ||||||
|  |     { | ||||||
|  |         _webSocket.stop(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     void WebSocketSender::waitForConnection() | ||||||
|  |     { | ||||||
|  |         std::cout << "Connecting..." << std::endl; | ||||||
|  |  | ||||||
|  |         std::unique_lock<std::mutex> lock(_conditionVariableMutex); | ||||||
|  |         _condition.wait(lock); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     void WebSocketSender::waitForAck() | ||||||
|  |     { | ||||||
|  |         std::cout << "Waiting for ack..." << std::endl; | ||||||
|  |  | ||||||
|  |         std::unique_lock<std::mutex> lock(_conditionVariableMutex); | ||||||
|  |         _condition.wait(lock); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     std::string load(const std::string& path) | ||||||
|  |     { | ||||||
|  |         // std::vector<uint8_t> memblock; | ||||||
|  |         std::string str; | ||||||
|  |  | ||||||
|  |         std::ifstream file(path); | ||||||
|  |         if (!file.is_open()) return std::string(); | ||||||
|  |  | ||||||
|  |         file.seekg(0, file.end); | ||||||
|  |         std::streamoff size = file.tellg(); | ||||||
|  |         file.seekg(0, file.beg); | ||||||
|  |  | ||||||
|  |         str.resize(size); | ||||||
|  |         file.read((char*)&str.front(), static_cast<std::streamsize>(size)); | ||||||
|  |  | ||||||
|  |         return str; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     void WebSocketSender::start() | ||||||
|  |     { | ||||||
|  |         _webSocket.setUrl(_url); | ||||||
|  |  | ||||||
|  |         ix::WebSocketPerMessageDeflateOptions webSocketPerMessageDeflateOptions( | ||||||
|  |             _enablePerMessageDeflate, false, false, 15, 15); | ||||||
|  |         _webSocket.setPerMessageDeflateOptions(webSocketPerMessageDeflateOptions); | ||||||
|  |  | ||||||
|  |         std::stringstream ss; | ||||||
|  |         log(std::string("Connecting to url: ") + _url); | ||||||
|  |  | ||||||
|  |         _webSocket.setOnMessageCallback( | ||||||
|  |             [this](ix::WebSocketMessageType messageType, | ||||||
|  |                const std::string& str, | ||||||
|  |                size_t wireSize, | ||||||
|  |                const ix::WebSocketErrorInfo& error, | ||||||
|  |                const ix::WebSocketOpenInfo& openInfo, | ||||||
|  |                const ix::WebSocketCloseInfo& closeInfo) | ||||||
|  |             { | ||||||
|  |                 std::stringstream ss; | ||||||
|  |                 if (messageType == ix::WebSocket_MessageType_Open) | ||||||
|  |                 { | ||||||
|  |                     _condition.notify_one(); | ||||||
|  |  | ||||||
|  |                     log("ws_send: connected"); | ||||||
|  |                     std::cout << "Uri: " << openInfo.uri << std::endl; | ||||||
|  |                     std::cout << "Handshake Headers:" << std::endl; | ||||||
|  |                     for (auto it : openInfo.headers) | ||||||
|  |                     { | ||||||
|  |                         std::cout << it.first << ": " << it.second << std::endl; | ||||||
|  |                     } | ||||||
|  |                 } | ||||||
|  |                 else if (messageType == ix::WebSocket_MessageType_Close) | ||||||
|  |                 { | ||||||
|  |                     ss << "ws_send: connection closed:"; | ||||||
|  |                     ss << " code " << closeInfo.code; | ||||||
|  |                     ss << " reason " << closeInfo.reason << std::endl; | ||||||
|  |                     log(ss.str()); | ||||||
|  |                 } | ||||||
|  |                 else if (messageType == ix::WebSocket_MessageType_Message) | ||||||
|  |                 { | ||||||
|  |                     _condition.notify_one(); | ||||||
|  |  | ||||||
|  |                     ss << "ws_send: received message: " | ||||||
|  |                        << str; | ||||||
|  |                     log(ss.str()); | ||||||
|  |  | ||||||
|  |                     Json::Value data; | ||||||
|  |                     Json::Reader reader; | ||||||
|  |                     if (!reader.parse(str, data)) | ||||||
|  |                     { | ||||||
|  |                         std::cerr << "Invalid JSON response" << std::endl; | ||||||
|  |                         return; | ||||||
|  |                     } | ||||||
|  |  | ||||||
|  |                     std::string id = data["id"].asString(); | ||||||
|  |                     if (_id != id) | ||||||
|  |                     { | ||||||
|  |                         std::cerr << "Invalid id" << std::endl; | ||||||
|  |                     } | ||||||
|  |                 } | ||||||
|  |                 else if (messageType == ix::WebSocket_MessageType_Error) | ||||||
|  |                 { | ||||||
|  |                     ss << "Connection error: " << error.reason      << std::endl; | ||||||
|  |                     ss << "#retries: "         << error.retries     << std::endl; | ||||||
|  |                     ss << "Wait time(ms): "    << error.wait_time   << std::endl; | ||||||
|  |                     ss << "HTTP Status: "      << error.http_status << std::endl; | ||||||
|  |                     log(ss.str()); | ||||||
|  |                 } | ||||||
|  |                 else | ||||||
|  |                 { | ||||||
|  |                     ss << "Invalid ix::WebSocketMessageType"; | ||||||
|  |                     log(ss.str()); | ||||||
|  |                 } | ||||||
|  |             }); | ||||||
|  |  | ||||||
|  |         _webSocket.start(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     class Bench | ||||||
|  |     { | ||||||
|  |         public: | ||||||
|  |             Bench(const std::string& description) : | ||||||
|  |                 _description(description), | ||||||
|  |                 _start(std::chrono::system_clock::now()), | ||||||
|  |                 _reported(false) | ||||||
|  |             { | ||||||
|  |                 ; | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             ~Bench() | ||||||
|  |             { | ||||||
|  |                 if (!_reported) | ||||||
|  |                 { | ||||||
|  |                     report(); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             void report() | ||||||
|  |             { | ||||||
|  |                 auto now = std::chrono::system_clock::now(); | ||||||
|  |                 auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(now - _start); | ||||||
|  |  | ||||||
|  |                 _ms = milliseconds.count(); | ||||||
|  |                 std::cout << _description << " completed in " | ||||||
|  |                           << _ms << "ms" << std::endl; | ||||||
|  |  | ||||||
|  |                 _reported = true; | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             uint64_t getDuration() const | ||||||
|  |             { | ||||||
|  |                 return _ms; | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |         private: | ||||||
|  |             std::string _description; | ||||||
|  |             std::chrono::time_point<std::chrono::system_clock> _start; | ||||||
|  |             uint64_t _ms; | ||||||
|  |             bool _reported; | ||||||
|  |     }; | ||||||
|  |  | ||||||
|  |     void WebSocketSender::sendMessage(const std::string& filename, | ||||||
|  |                                       bool throttle) | ||||||
|  |     { | ||||||
|  |         std::string content; | ||||||
|  |         { | ||||||
|  |             Bench bench("load file from disk"); | ||||||
|  |             content = load(filename); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         _id = uuid4(); | ||||||
|  |  | ||||||
|  |         std::string b64Content; | ||||||
|  |         { | ||||||
|  |             Bench bench("base 64 encode file"); | ||||||
|  |             b64Content = base64_encode(content, content.size()); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         Json::Value pdu; | ||||||
|  |         pdu["kind"] = "send"; | ||||||
|  |         pdu["id"] = _id; | ||||||
|  |         pdu["content"] = b64Content; | ||||||
|  |         pdu["djb2_hash"] = djb2Hash(b64Content); | ||||||
|  |         pdu["filename"] = filename; | ||||||
|  |  | ||||||
|  |         Bench bench("Sending file through websocket"); | ||||||
|  |         _webSocket.send(pdu.toStyledString(), | ||||||
|  |                         [throttle](int current, int total) -> bool | ||||||
|  |         { | ||||||
|  |             std::cout << "Step " << current << " out of " << total << std::endl; | ||||||
|  |  | ||||||
|  |             if (throttle) | ||||||
|  |             { | ||||||
|  |                 std::chrono::duration<double, std::milli> duration(10); | ||||||
|  |                 std::this_thread::sleep_for(duration); | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             return true; | ||||||
|  |         }); | ||||||
|  |  | ||||||
|  |         bench.report(); | ||||||
|  |         auto duration = bench.getDuration(); | ||||||
|  |         auto transferRate = 1000 * b64Content.size() / duration; | ||||||
|  |         transferRate /= (1024 * 1024); | ||||||
|  |         std::cout << "Send transfer rate: " << transferRate << "MB/s" << std::endl; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     void wsSend(const std::string& url, | ||||||
|  |                 const std::string& path, | ||||||
|  |                 bool enablePerMessageDeflate, | ||||||
|  |                 bool throttle) | ||||||
|  |     { | ||||||
|  |         WebSocketSender webSocketSender(url, enablePerMessageDeflate); | ||||||
|  |         webSocketSender.start(); | ||||||
|  |  | ||||||
|  |         webSocketSender.waitForConnection(); | ||||||
|  |  | ||||||
|  |         std::cout << "Sending..." << std::endl; | ||||||
|  |         webSocketSender.sendMessage(path, throttle); | ||||||
|  |  | ||||||
|  |         webSocketSender.waitForAck(); | ||||||
|  |  | ||||||
|  |         std::cout << "Done !" << std::endl; | ||||||
|  |         webSocketSender.stop(); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | int main(int argc, char** argv) | ||||||
|  | { | ||||||
|  |     if (argc != 3) | ||||||
|  |     { | ||||||
|  |         std::cerr << "Usage: ws_send <url> <path>" << std::endl; | ||||||
|  |         return 1; | ||||||
|  |     } | ||||||
|  |     std::string url = argv[1]; | ||||||
|  |     std::string path = argv[2]; | ||||||
|  |  | ||||||
|  |     bool throttle = false; | ||||||
|  |     bool enablePerMessageDeflate = false; | ||||||
|  |  | ||||||
|  |     Socket::init(); | ||||||
|  |     wsSend(url, path, enablePerMessageDeflate, throttle); | ||||||
|  |     return 0; | ||||||
|  | } | ||||||
							
								
								
									
										471
									
								
								ixwebsocket/IXHttpClient.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										471
									
								
								ixwebsocket/IXHttpClient.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,471 @@ | |||||||
|  | /* | ||||||
|  |  *  IXHttpClient.cpp | ||||||
|  |  *  Author: Benjamin Sergeant | ||||||
|  |  *  Copyright (c) 2019 Machine Zone, Inc. All rights reserved. | ||||||
|  |  */ | ||||||
|  |  | ||||||
|  | #include "IXHttpClient.h" | ||||||
|  | #include "IXUrlParser.h" | ||||||
|  | #include "IXWebSocketHttpHeaders.h" | ||||||
|  | #include "IXSocketFactory.h" | ||||||
|  |  | ||||||
|  | #include <sstream> | ||||||
|  | #include <iomanip> | ||||||
|  | #include <vector> | ||||||
|  | #include <cstring> | ||||||
|  |  | ||||||
|  | #include <zlib.h> | ||||||
|  |  | ||||||
|  | namespace ix | ||||||
|  | { | ||||||
|  |     const std::string HttpClient::kPost = "POST"; | ||||||
|  |     const std::string HttpClient::kGet = "GET"; | ||||||
|  |     const std::string HttpClient::kHead = "HEAD"; | ||||||
|  |  | ||||||
|  |     HttpClient::HttpClient() | ||||||
|  |     { | ||||||
|  |  | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     HttpClient::~HttpClient() | ||||||
|  |     { | ||||||
|  |  | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     HttpResponse HttpClient::request( | ||||||
|  |         const std::string& url, | ||||||
|  |         const std::string& verb, | ||||||
|  |         const std::string& body, | ||||||
|  |         const HttpRequestArgs& args, | ||||||
|  |         int redirects) | ||||||
|  |     { | ||||||
|  |         uint64_t uploadSize = 0; | ||||||
|  |         uint64_t downloadSize = 0; | ||||||
|  |         int code = 0; | ||||||
|  |         WebSocketHttpHeaders headers; | ||||||
|  |         std::string payload; | ||||||
|  |  | ||||||
|  |         std::string protocol, host, path, query; | ||||||
|  |         int port; | ||||||
|  |         bool websocket = false; | ||||||
|  |  | ||||||
|  |         if (!UrlParser::parse(url, protocol, host, path, query, port, websocket)) | ||||||
|  |         { | ||||||
|  |             std::stringstream ss; | ||||||
|  |             ss << "Cannot parse url: " << url; | ||||||
|  |             return std::make_tuple(code, HttpErrorCode_UrlMalformed, | ||||||
|  |                                    headers, payload, ss.str(), | ||||||
|  |                                    uploadSize, downloadSize); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         bool tls = protocol == "https"; | ||||||
|  |         std::string errorMsg; | ||||||
|  |         _socket = createSocket(tls, errorMsg); | ||||||
|  |  | ||||||
|  |         if (!_socket) | ||||||
|  |         { | ||||||
|  |             return std::make_tuple(code, HttpErrorCode_CannotCreateSocket, | ||||||
|  |                                    headers, payload, errorMsg, | ||||||
|  |                                    uploadSize, downloadSize); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         // Build request string | ||||||
|  |         std::stringstream ss; | ||||||
|  |         ss << verb << " " << path << " HTTP/1.1\r\n"; | ||||||
|  |         ss << "Host: " << host << "\r\n"; | ||||||
|  |         ss << "User-Agent: ixwebsocket/1.0.0" << "\r\n"; | ||||||
|  |         ss << "Accept: */*" << "\r\n"; | ||||||
|  |  | ||||||
|  |         if (args.compress) | ||||||
|  |         { | ||||||
|  |             ss << "Accept-Encoding: gzip" << "\r\n"; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         // Append extra headers | ||||||
|  |         for (auto&& it : args.extraHeaders) | ||||||
|  |         { | ||||||
|  |             ss << it.first << ": " << it.second << "\r\n"; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         if (verb == kPost) | ||||||
|  |         { | ||||||
|  |             ss << "Content-Length: " << body.size() << "\r\n"; | ||||||
|  |  | ||||||
|  |             // Set default Content-Type if unspecified | ||||||
|  |             if (args.extraHeaders.find("Content-Type") == args.extraHeaders.end()) | ||||||
|  |             { | ||||||
|  |                 ss << "Content-Type: application/x-www-form-urlencoded" << "\r\n"; | ||||||
|  |             } | ||||||
|  |             ss << "\r\n"; | ||||||
|  |             ss << body; | ||||||
|  |         } | ||||||
|  |         else | ||||||
|  |         { | ||||||
|  |             ss << "\r\n"; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         std::string req(ss.str()); | ||||||
|  |         std::string errMsg; | ||||||
|  |         std::atomic<bool> requestInitCancellation(false); | ||||||
|  |  | ||||||
|  |         // Make a cancellation object dealing with connection timeout | ||||||
|  |         auto isCancellationRequested = | ||||||
|  |             makeCancellationRequestWithTimeout(args.connectTimeout, requestInitCancellation); | ||||||
|  |  | ||||||
|  |         bool success = _socket->connect(host, port, errMsg, isCancellationRequested); | ||||||
|  |         if (!success) | ||||||
|  |         { | ||||||
|  |             std::stringstream ss; | ||||||
|  |             ss << "Cannot connect to url: " << url; | ||||||
|  |             return std::make_tuple(code, HttpErrorCode_CannotConnect, | ||||||
|  |                                    headers, payload, ss.str(), | ||||||
|  |                                    uploadSize, downloadSize); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         // Make a new cancellation object dealing with transfer timeout | ||||||
|  |         isCancellationRequested = | ||||||
|  |             makeCancellationRequestWithTimeout(args.transferTimeout, requestInitCancellation); | ||||||
|  |  | ||||||
|  |         if (args.verbose) | ||||||
|  |         { | ||||||
|  |             std::stringstream ss; | ||||||
|  |             ss << "Sending " << verb << " request " | ||||||
|  |                << "to " << host << ":" << port << std::endl | ||||||
|  |                << "request size: " << req.size() << " bytes" << std::endl | ||||||
|  |                << "=============" << std::endl | ||||||
|  |                << req | ||||||
|  |                << "=============" << std::endl | ||||||
|  |                << std::endl; | ||||||
|  |  | ||||||
|  |             log(ss.str(), args); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         if (!_socket->writeBytes(req, isCancellationRequested)) | ||||||
|  |         { | ||||||
|  |             std::string errorMsg("Cannot send request"); | ||||||
|  |             return std::make_tuple(code, HttpErrorCode_SendError, | ||||||
|  |                                    headers, payload, errorMsg, | ||||||
|  |                                    uploadSize, downloadSize); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         uploadSize = req.size(); | ||||||
|  |  | ||||||
|  |         auto lineResult = _socket->readLine(isCancellationRequested); | ||||||
|  |         auto lineValid = lineResult.first; | ||||||
|  |         auto line = lineResult.second; | ||||||
|  |  | ||||||
|  |         if (!lineValid) | ||||||
|  |         { | ||||||
|  |             std::string errorMsg("Cannot retrieve status line"); | ||||||
|  |             return std::make_tuple(code, HttpErrorCode_CannotReadStatusLine, | ||||||
|  |                                    headers, payload, errorMsg, | ||||||
|  |                                    uploadSize, downloadSize); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         if (args.verbose) | ||||||
|  |         { | ||||||
|  |             std::stringstream ss; | ||||||
|  |             ss << "Status line " << line; | ||||||
|  |             log(ss.str(), args); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         if (sscanf(line.c_str(), "HTTP/1.1 %d", &code) != 1) | ||||||
|  |         { | ||||||
|  |             std::string errorMsg("Cannot parse response code from status line"); | ||||||
|  |             return std::make_tuple(code, HttpErrorCode_MissingStatus, | ||||||
|  |                                    headers, payload, errorMsg, | ||||||
|  |                                    uploadSize, downloadSize); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         auto result = parseHttpHeaders(_socket, isCancellationRequested); | ||||||
|  |         auto headersValid = result.first; | ||||||
|  |         headers = result.second; | ||||||
|  |  | ||||||
|  |         if (!headersValid) | ||||||
|  |         { | ||||||
|  |             std::string errorMsg("Cannot parse http headers"); | ||||||
|  |             return std::make_tuple(code, HttpErrorCode_HeaderParsingError, | ||||||
|  |                                    headers, payload, errorMsg, | ||||||
|  |                                    uploadSize, downloadSize); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         // Redirect ? | ||||||
|  |         if ((code >= 301 && code <= 308) && args.followRedirects) | ||||||
|  |         { | ||||||
|  |             if (headers.find("Location") == headers.end()) | ||||||
|  |             { | ||||||
|  |                 std::string errorMsg("Missing location header for redirect"); | ||||||
|  |                 return std::make_tuple(code, HttpErrorCode_MissingLocation, | ||||||
|  |                                        headers, payload, errorMsg, | ||||||
|  |                                        uploadSize, downloadSize); | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             if (redirects >= args.maxRedirects) | ||||||
|  |             { | ||||||
|  |                 std::stringstream ss; | ||||||
|  |                 ss << "Too many redirects: " << redirects; | ||||||
|  |                 return std::make_tuple(code, HttpErrorCode_TooManyRedirects, | ||||||
|  |                                        headers, payload, ss.str(), | ||||||
|  |                                        uploadSize, downloadSize); | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             // Recurse | ||||||
|  |             std::string location = headers["Location"]; | ||||||
|  |             return request(location, verb, body, args, redirects+1); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         if (verb == "HEAD") | ||||||
|  |         { | ||||||
|  |             return std::make_tuple(code, HttpErrorCode_Ok, | ||||||
|  |                                    headers, payload, std::string(), | ||||||
|  |                                    uploadSize, downloadSize); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         // Parse response: | ||||||
|  |         if (headers.find("Content-Length") != headers.end()) | ||||||
|  |         { | ||||||
|  |             ssize_t contentLength = -1; | ||||||
|  |             ss.str(""); | ||||||
|  |             ss << headers["Content-Length"]; | ||||||
|  |             ss >> contentLength; | ||||||
|  |  | ||||||
|  |             payload.reserve(contentLength); | ||||||
|  |  | ||||||
|  |             // FIXME: very inefficient way to read bytes, but it works... | ||||||
|  |             for (int i = 0; i < contentLength; ++i) | ||||||
|  |             { | ||||||
|  |                 char c; | ||||||
|  |                 if (!_socket->readByte(&c, isCancellationRequested)) | ||||||
|  |                 { | ||||||
|  |                     return std::make_tuple(code, HttpErrorCode_ReadError, | ||||||
|  |                                            headers, payload, "Cannot read byte", | ||||||
|  |                                            uploadSize, downloadSize); | ||||||
|  |                 } | ||||||
|  |  | ||||||
|  |                 payload += c; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         else if (headers.find("Transfer-Encoding") != headers.end() && | ||||||
|  |                  headers["Transfer-Encoding"] == "chunked") | ||||||
|  |         { | ||||||
|  |             std::stringstream ss; | ||||||
|  |  | ||||||
|  |             while (true) | ||||||
|  |             { | ||||||
|  |                 lineResult = _socket->readLine(isCancellationRequested); | ||||||
|  |                 line = lineResult.second; | ||||||
|  |  | ||||||
|  |                 if (!lineResult.first) | ||||||
|  |                 { | ||||||
|  |                     return std::make_tuple(code, HttpErrorCode_ChunkReadError, | ||||||
|  |                                            headers, payload, errorMsg, | ||||||
|  |                                            uploadSize, downloadSize); | ||||||
|  |                 } | ||||||
|  |  | ||||||
|  |                 uint64_t chunkSize; | ||||||
|  |                 ss.str(""); | ||||||
|  |                 ss << std::hex << line; | ||||||
|  |                 ss >> chunkSize; | ||||||
|  |  | ||||||
|  |                 if (args.verbose) | ||||||
|  |                 { | ||||||
|  |                     std::stringstream oss; | ||||||
|  |                     oss << "Reading " << chunkSize << " bytes" | ||||||
|  |                         << std::endl; | ||||||
|  |                     log(oss.str(), args); | ||||||
|  |                 } | ||||||
|  |  | ||||||
|  |                 payload.reserve(payload.size() + chunkSize); | ||||||
|  |  | ||||||
|  |                 // Read another line | ||||||
|  |  | ||||||
|  |                 for (uint64_t i = 0; i < chunkSize; ++i) | ||||||
|  |                 { | ||||||
|  |                     char c; | ||||||
|  |                     if (!_socket->readByte(&c, isCancellationRequested)) | ||||||
|  |                     { | ||||||
|  |                         errorMsg = "Cannot read byte"; | ||||||
|  |                         return std::make_tuple(code, HttpErrorCode_ChunkReadError, | ||||||
|  |                                                headers, payload, errorMsg, | ||||||
|  |                                                uploadSize, downloadSize); | ||||||
|  |                     } | ||||||
|  |  | ||||||
|  |                     payload += c; | ||||||
|  |                 } | ||||||
|  |  | ||||||
|  |                 lineResult = _socket->readLine(isCancellationRequested); | ||||||
|  |  | ||||||
|  |                 if (!lineResult.first) | ||||||
|  |                 { | ||||||
|  |                     return std::make_tuple(code, HttpErrorCode_ChunkReadError, | ||||||
|  |                                            headers, payload, errorMsg, | ||||||
|  |                                            uploadSize, downloadSize); | ||||||
|  |                 } | ||||||
|  |  | ||||||
|  |                 if (chunkSize == 0) break; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         else if (code == 204) | ||||||
|  |         { | ||||||
|  |             ; // 204 is NoContent response code | ||||||
|  |         } | ||||||
|  |         else | ||||||
|  |         { | ||||||
|  |             std::string errorMsg("Cannot read http body"); | ||||||
|  |             return std::make_tuple(code, HttpErrorCode_CannotReadBody, | ||||||
|  |                                    headers, payload, errorMsg, | ||||||
|  |                                    uploadSize, downloadSize); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         downloadSize = payload.size(); | ||||||
|  |  | ||||||
|  |         // If the content was compressed with gzip, decode it | ||||||
|  |         if (headers["Content-Encoding"] == "gzip") | ||||||
|  |         { | ||||||
|  |             std::string decompressedPayload; | ||||||
|  |             if (!gzipInflate(payload, decompressedPayload)) | ||||||
|  |             { | ||||||
|  |                 std::string errorMsg("Error decompressing payload"); | ||||||
|  |                 return std::make_tuple(code, HttpErrorCode_Gzip, | ||||||
|  |                                        headers, payload, errorMsg, | ||||||
|  |                                        uploadSize, downloadSize); | ||||||
|  |             } | ||||||
|  |             payload = decompressedPayload; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         return std::make_tuple(code, HttpErrorCode_Ok, | ||||||
|  |                                headers, payload, std::string(), | ||||||
|  |                                uploadSize, downloadSize); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     HttpResponse HttpClient::get(const std::string& url, | ||||||
|  |                                  const HttpRequestArgs& args) | ||||||
|  |     { | ||||||
|  |         return request(url, kGet, std::string(), args); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     HttpResponse HttpClient::head(const std::string& url, | ||||||
|  |                                   const HttpRequestArgs& args) | ||||||
|  |     { | ||||||
|  |         return request(url, kHead, std::string(), args); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     HttpResponse HttpClient::post(const std::string& url, | ||||||
|  |                                   const HttpParameters& httpParameters, | ||||||
|  |                                   const HttpRequestArgs& args) | ||||||
|  |     { | ||||||
|  |         return request(url, kPost, serializeHttpParameters(httpParameters), args); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     HttpResponse HttpClient::post(const std::string& url, | ||||||
|  |                                   const std::string& body, | ||||||
|  |                                   const HttpRequestArgs& args) | ||||||
|  |     { | ||||||
|  |         return request(url, kPost, body, args); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     std::string HttpClient::urlEncode(const std::string& value) | ||||||
|  |     { | ||||||
|  |         std::ostringstream escaped; | ||||||
|  |         escaped.fill('0'); | ||||||
|  |         escaped << std::hex; | ||||||
|  |  | ||||||
|  |         for (std::string::const_iterator i = value.begin(), n = value.end(); | ||||||
|  |              i != n; ++i) | ||||||
|  |         { | ||||||
|  |             std::string::value_type c = (*i); | ||||||
|  |  | ||||||
|  |             // Keep alphanumeric and other accepted characters intact | ||||||
|  |             if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') | ||||||
|  |             { | ||||||
|  |                 escaped << c; | ||||||
|  |                 continue; | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             // Any other characters are percent-encoded | ||||||
|  |             escaped << std::uppercase; | ||||||
|  |             escaped << '%' << std::setw(2) << int((unsigned char) c); | ||||||
|  |             escaped << std::nouppercase; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         return escaped.str(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     std::string HttpClient::serializeHttpParameters(const HttpParameters& httpParameters) | ||||||
|  |     { | ||||||
|  |         std::stringstream ss; | ||||||
|  |         size_t count = httpParameters.size(); | ||||||
|  |         size_t i = 0; | ||||||
|  |  | ||||||
|  |         for (auto&& it : httpParameters) | ||||||
|  |         { | ||||||
|  |             ss << urlEncode(it.first) | ||||||
|  |                << "=" | ||||||
|  |                << urlEncode(it.second); | ||||||
|  |  | ||||||
|  |             if (i++ < (count-1)) | ||||||
|  |             { | ||||||
|  |                ss << "&"; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         return ss.str(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     bool HttpClient::gzipInflate( | ||||||
|  |         const std::string& in, | ||||||
|  |         std::string& out) | ||||||
|  |     { | ||||||
|  |         z_stream inflateState; | ||||||
|  |         std::memset(&inflateState, 0, sizeof(inflateState)); | ||||||
|  |  | ||||||
|  |         inflateState.zalloc = Z_NULL; | ||||||
|  |         inflateState.zfree = Z_NULL; | ||||||
|  |         inflateState.opaque = Z_NULL; | ||||||
|  |         inflateState.avail_in = 0; | ||||||
|  |         inflateState.next_in = Z_NULL; | ||||||
|  |  | ||||||
|  |         if (inflateInit2(&inflateState, 16+MAX_WBITS) != Z_OK) | ||||||
|  |         { | ||||||
|  |             return false; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         inflateState.avail_in = (uInt) in.size(); | ||||||
|  |         inflateState.next_in = (unsigned char *)(const_cast<char *>(in.data())); | ||||||
|  |  | ||||||
|  |         const int kBufferSize = 1 << 14; | ||||||
|  |  | ||||||
|  |         std::unique_ptr<unsigned char[]> compressBuffer = | ||||||
|  |             std::make_unique<unsigned char[]>(kBufferSize); | ||||||
|  |  | ||||||
|  |         do | ||||||
|  |         { | ||||||
|  |             inflateState.avail_out = (uInt) kBufferSize; | ||||||
|  |             inflateState.next_out = compressBuffer.get(); | ||||||
|  |  | ||||||
|  |             int ret = inflate(&inflateState, Z_SYNC_FLUSH); | ||||||
|  |  | ||||||
|  |             if (ret == Z_NEED_DICT || ret == Z_DATA_ERROR || ret == Z_MEM_ERROR) | ||||||
|  |             { | ||||||
|  |                 inflateEnd(&inflateState); | ||||||
|  |                 return false; | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             out.append( | ||||||
|  |                 reinterpret_cast<char *>(compressBuffer.get()), | ||||||
|  |                 kBufferSize - inflateState.avail_out | ||||||
|  |             ); | ||||||
|  |         } while (inflateState.avail_out == 0); | ||||||
|  |  | ||||||
|  |         inflateEnd(&inflateState); | ||||||
|  |         return true; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     void HttpClient::log(const std::string& msg, | ||||||
|  |                          const HttpRequestArgs& args) | ||||||
|  |     { | ||||||
|  |         if (args.logger) | ||||||
|  |         { | ||||||
|  |             args.logger(msg); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										106
									
								
								ixwebsocket/IXHttpClient.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										106
									
								
								ixwebsocket/IXHttpClient.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,106 @@ | |||||||
|  | /* | ||||||
|  |  *  IXHttpClient.h | ||||||
|  |  *  Author: Benjamin Sergeant | ||||||
|  |  *  Copyright (c) 2019 Machine Zone, Inc. All rights reserved. | ||||||
|  |  */ | ||||||
|  |  | ||||||
|  | #pragma once | ||||||
|  |  | ||||||
|  | #include <algorithm> | ||||||
|  | #include <functional> | ||||||
|  | #include <mutex> | ||||||
|  | #include <atomic> | ||||||
|  | #include <tuple> | ||||||
|  | #include <memory> | ||||||
|  | #include <map> | ||||||
|  |  | ||||||
|  | #include "IXSocket.h" | ||||||
|  | #include "IXWebSocketHttpHeaders.h" | ||||||
|  |  | ||||||
|  | namespace ix | ||||||
|  | { | ||||||
|  |     enum HttpErrorCode | ||||||
|  |     { | ||||||
|  |         HttpErrorCode_Ok = 0, | ||||||
|  |         HttpErrorCode_CannotConnect = 1, | ||||||
|  |         HttpErrorCode_Timeout = 2, | ||||||
|  |         HttpErrorCode_Gzip = 3, | ||||||
|  |         HttpErrorCode_UrlMalformed = 4, | ||||||
|  |         HttpErrorCode_CannotCreateSocket = 5, | ||||||
|  |         HttpErrorCode_SendError = 6, | ||||||
|  |         HttpErrorCode_ReadError = 7, | ||||||
|  |         HttpErrorCode_CannotReadStatusLine = 8, | ||||||
|  |         HttpErrorCode_MissingStatus = 9, | ||||||
|  |         HttpErrorCode_HeaderParsingError = 10, | ||||||
|  |         HttpErrorCode_MissingLocation = 11, | ||||||
|  |         HttpErrorCode_TooManyRedirects = 12, | ||||||
|  |         HttpErrorCode_ChunkReadError = 13, | ||||||
|  |         HttpErrorCode_CannotReadBody = 14 | ||||||
|  |     }; | ||||||
|  |  | ||||||
|  |     using HttpResponse = std::tuple<int, // status | ||||||
|  |                                     HttpErrorCode, // error code | ||||||
|  |                                     WebSocketHttpHeaders, | ||||||
|  |                                     std::string, // payload | ||||||
|  |                                     std::string, // error msg | ||||||
|  |                                     uint64_t,    // upload size | ||||||
|  |                                     uint64_t>;   // download size | ||||||
|  |  | ||||||
|  |     using HttpParameters = std::map<std::string, std::string>; | ||||||
|  |     using Logger = std::function<void(const std::string&)>; | ||||||
|  |  | ||||||
|  |     struct HttpRequestArgs | ||||||
|  |     { | ||||||
|  |         std::string url; | ||||||
|  |         WebSocketHttpHeaders extraHeaders; | ||||||
|  |         std::string body; | ||||||
|  |         int connectTimeout; | ||||||
|  |         int transferTimeout; | ||||||
|  |         bool followRedirects; | ||||||
|  |         int maxRedirects; | ||||||
|  |         bool verbose; | ||||||
|  |         bool compress; | ||||||
|  |         Logger logger; | ||||||
|  |     }; | ||||||
|  |  | ||||||
|  |     class HttpClient { | ||||||
|  |     public: | ||||||
|  |         HttpClient(); | ||||||
|  |         ~HttpClient(); | ||||||
|  |  | ||||||
|  |         HttpResponse get(const std::string& url, | ||||||
|  |                          const HttpRequestArgs& args); | ||||||
|  |         HttpResponse head(const std::string& url, | ||||||
|  |                           const HttpRequestArgs& args); | ||||||
|  |  | ||||||
|  |         HttpResponse post(const std::string& url, | ||||||
|  |                           const HttpParameters& httpParameters, | ||||||
|  |                           const HttpRequestArgs& args); | ||||||
|  |         HttpResponse post(const std::string& url, | ||||||
|  |                           const std::string& body, | ||||||
|  |                           const HttpRequestArgs& args); | ||||||
|  |  | ||||||
|  |     private: | ||||||
|  |         HttpResponse request(const std::string& url, | ||||||
|  |                              const std::string& verb, | ||||||
|  |                              const std::string& body, | ||||||
|  |                              const HttpRequestArgs& args, | ||||||
|  |                              int redirects = 0); | ||||||
|  |  | ||||||
|  |         std::string serializeHttpParameters(const HttpParameters& httpParameters); | ||||||
|  |  | ||||||
|  |         std::string urlEncode(const std::string& value); | ||||||
|  |  | ||||||
|  |         void log(const std::string& msg, const HttpRequestArgs& args); | ||||||
|  |  | ||||||
|  |         bool gzipInflate( | ||||||
|  |             const std::string& in, | ||||||
|  |             std::string& out); | ||||||
|  |  | ||||||
|  |         std::shared_ptr<Socket> _socket; | ||||||
|  |  | ||||||
|  |         const static std::string kPost; | ||||||
|  |         const static std::string kGet; | ||||||
|  |         const static std::string kHead; | ||||||
|  |     }; | ||||||
|  | } | ||||||
							
								
								
									
										14
									
								
								ixwebsocket/IXProgressCallback.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								ixwebsocket/IXProgressCallback.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,14 @@ | |||||||
|  | /* | ||||||
|  |  *  IXProgressCallback.h | ||||||
|  |  *  Author: Benjamin Sergeant | ||||||
|  |  *  Copyright (c) 2019 Machine Zone, Inc. All rights reserved. | ||||||
|  |  */ | ||||||
|  |  | ||||||
|  | #pragma once | ||||||
|  |  | ||||||
|  | #include <functional> | ||||||
|  |  | ||||||
|  | namespace ix | ||||||
|  | { | ||||||
|  |     using OnProgressCallback = std::function<bool(int current, int total)>; | ||||||
|  | } | ||||||
| @@ -15,7 +15,6 @@ | |||||||
| #include <stdint.h> | #include <stdint.h> | ||||||
| #include <fcntl.h> | #include <fcntl.h> | ||||||
| #include <sys/types.h> | #include <sys/types.h> | ||||||
| #include <poll.h> |  | ||||||
|  |  | ||||||
| #include <algorithm> | #include <algorithm> | ||||||
| #include <iostream> | #include <iostream> | ||||||
| @@ -44,21 +43,22 @@ namespace ix | |||||||
|             return; |             return; | ||||||
|         } |         } | ||||||
|  |  | ||||||
| #ifdef __linux__ |         fd_set rfds; | ||||||
|         constexpr int nfds = 2; |         FD_ZERO(&rfds); | ||||||
| #else |         FD_SET(_sockfd, &rfds); | ||||||
|         constexpr int nfds = 1; |  | ||||||
| #endif |  | ||||||
|  |  | ||||||
|         struct pollfd fds[nfds]; |  | ||||||
|         fds[0].fd = _sockfd; |  | ||||||
|         fds[0].events = POLLIN; |  | ||||||
|  |  | ||||||
| #ifdef __linux__ | #ifdef __linux__ | ||||||
|         fds[1].fd = _eventfd.getFd(); |         FD_SET(_eventfd.getFd(), &rfds); | ||||||
|         fds[1].events = POLLIN; |  | ||||||
| #endif | #endif | ||||||
|         int ret = ::poll(fds, nfds, timeoutSecs * 1000); |  | ||||||
|  |         struct timeval timeout; | ||||||
|  |         timeout.tv_sec = timeoutSecs; | ||||||
|  |         timeout.tv_usec = 0; | ||||||
|  |  | ||||||
|  |         int sockfd = _sockfd; | ||||||
|  |         int nfds = (std::max)(sockfd, _eventfd.getFd()); | ||||||
|  |         int ret = select(nfds + 1, &rfds, nullptr, nullptr, | ||||||
|  |                          (timeoutSecs < 0) ? nullptr : &timeout); | ||||||
|  |  | ||||||
|         PollResultType pollResult = PollResultType_ReadyForRead; |         PollResultType pollResult = PollResultType_ReadyForRead; | ||||||
|         if (ret < 0) |         if (ret < 0) | ||||||
| @@ -71,7 +71,6 @@ namespace ix | |||||||
|         } |         } | ||||||
|  |  | ||||||
|         onPollCallback(pollResult); |         onPollCallback(pollResult); | ||||||
|  |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     void Socket::wakeUpFromPoll() |     void Socket::wakeUpFromPoll() | ||||||
|   | |||||||
							
								
								
									
										42
									
								
								ixwebsocket/IXSocketFactory.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								ixwebsocket/IXSocketFactory.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,42 @@ | |||||||
|  | /* | ||||||
|  |  *  IXSocketFactory.cpp | ||||||
|  |  *  Author: Benjamin Sergeant | ||||||
|  |  *  Copyright (c) 2019 Machine Zone, Inc. All rights reserved. | ||||||
|  |  */ | ||||||
|  |  | ||||||
|  | #include "IXSocketFactory.h" | ||||||
|  |  | ||||||
|  | #if defined(__APPLE__) or defined(__linux__) | ||||||
|  | # ifdef __APPLE__ | ||||||
|  | #  include <ixwebsocket/IXSocketAppleSSL.h> | ||||||
|  | # else | ||||||
|  | #  include <ixwebsocket/IXSocketOpenSSL.h> | ||||||
|  | # endif | ||||||
|  | #endif | ||||||
|  |  | ||||||
|  | namespace ix | ||||||
|  | { | ||||||
|  |     std::shared_ptr<Socket> createSocket(bool tls, | ||||||
|  |                                          std::string& errorMsg) | ||||||
|  |     { | ||||||
|  |         errorMsg.clear(); | ||||||
|  |  | ||||||
|  |         if (!tls) | ||||||
|  |         { | ||||||
|  |             return std::make_shared<Socket>(); | ||||||
|  |         } | ||||||
|  |         else | ||||||
|  |         { | ||||||
|  | #ifdef IXWEBSOCKET_USE_TLS | ||||||
|  | # ifdef __APPLE__ | ||||||
|  |             return std::make_shared<SocketAppleSSL>(); | ||||||
|  | # else | ||||||
|  |             return std::make_shared<SocketOpenSSL>(); | ||||||
|  | # endif | ||||||
|  | #else | ||||||
|  |             errorMsg = "TLS support is not enabled on this platform."; | ||||||
|  |             return nullptr; | ||||||
|  | #endif | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										17
									
								
								ixwebsocket/IXSocketFactory.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								ixwebsocket/IXSocketFactory.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,17 @@ | |||||||
|  |  | ||||||
|  | /* | ||||||
|  |  *  IXSocketFactory.h | ||||||
|  |  *  Author: Benjamin Sergeant | ||||||
|  |  *  Copyright (c) 2018 Machine Zone, Inc. All rights reserved. | ||||||
|  |  */ | ||||||
|  |  | ||||||
|  | #pragma once | ||||||
|  |  | ||||||
|  | #include <memory> | ||||||
|  |  | ||||||
|  | namespace ix | ||||||
|  | { | ||||||
|  |     class Socket; | ||||||
|  |     std::shared_ptr<Socket> createSocket(bool tls, | ||||||
|  |                                          std::string& errorMsg); | ||||||
|  | } | ||||||
							
								
								
									
										104
									
								
								ixwebsocket/IXUrlParser.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										104
									
								
								ixwebsocket/IXUrlParser.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,104 @@ | |||||||
|  | /* | ||||||
|  |  *  IXUrlParser.cpp | ||||||
|  |  *  Author: Benjamin Sergeant | ||||||
|  |  *  Copyright (c) 2019 Machine Zone, Inc. All rights reserved. | ||||||
|  |  */ | ||||||
|  |  | ||||||
|  | #include "IXUrlParser.h" | ||||||
|  |  | ||||||
|  | #include <iostream> | ||||||
|  | #include <sstream> | ||||||
|  |  | ||||||
|  |  | ||||||
|  | namespace ix | ||||||
|  | { | ||||||
|  |     // | ||||||
|  |     // The only difference between those 2 regex is the protocol | ||||||
|  |     // | ||||||
|  |     std::regex UrlParser::_httpRegex("(http|https)://([^/ :]+):?([^/ ]*)(/?[^ #?]*)\\x3f?([^ #]*)#?([^ ]*)"); | ||||||
|  |     std::regex UrlParser::_webSocketRegex("(ws|wss)://([^/ :]+):?([^/ ]*)(/?[^ #?]*)\\x3f?([^ #]*)#?([^ ]*)"); | ||||||
|  |  | ||||||
|  |     bool UrlParser::parse(const std::string& url, | ||||||
|  |                           std::string& protocol, | ||||||
|  |                           std::string& host, | ||||||
|  |                           std::string& path, | ||||||
|  |                           std::string& query, | ||||||
|  |                           int& port, | ||||||
|  |                           bool websocket) | ||||||
|  |     { | ||||||
|  |         std::cmatch what; | ||||||
|  |         if (!regex_match(url.c_str(), what, | ||||||
|  |                          websocket ? _webSocketRegex : _httpRegex)) | ||||||
|  |         { | ||||||
|  |             return false; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         std::string portStr; | ||||||
|  |  | ||||||
|  |         protocol = std::string(what[1].first, what[1].second); | ||||||
|  |         host     = std::string(what[2].first, what[2].second); | ||||||
|  |         portStr  = std::string(what[3].first, what[3].second); | ||||||
|  |         path     = std::string(what[4].first, what[4].second); | ||||||
|  |         query    = std::string(what[5].first, what[5].second); | ||||||
|  |  | ||||||
|  |         if (portStr.empty()) | ||||||
|  |         { | ||||||
|  |             if (protocol == "ws" || protocol == "http") | ||||||
|  |             { | ||||||
|  |                 port = 80; | ||||||
|  |             } | ||||||
|  |             else if (protocol == "wss" || protocol == "https") | ||||||
|  |             { | ||||||
|  |                 port = 443; | ||||||
|  |             } | ||||||
|  |             else | ||||||
|  |             { | ||||||
|  |                 // Invalid protocol. Should be caught by regex check | ||||||
|  |                 // but this missing branch trigger cpplint linter. | ||||||
|  |                 return false; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         else | ||||||
|  |         { | ||||||
|  |             std::stringstream ss; | ||||||
|  |             ss << portStr; | ||||||
|  |             ss >> port; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         if (path.empty()) | ||||||
|  |         { | ||||||
|  |             path = "/"; | ||||||
|  |         } | ||||||
|  |         else if (path[0] != '/') | ||||||
|  |         { | ||||||
|  |             path = '/' + path; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         if (!query.empty()) | ||||||
|  |         { | ||||||
|  |             path += "?"; | ||||||
|  |             path += query; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         return true; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     void UrlParser::printUrl(const std::string& url, bool websocket) | ||||||
|  |     { | ||||||
|  |         std::string protocol, host, path, query; | ||||||
|  |         int port {0}; | ||||||
|  |  | ||||||
|  |         if (!parse(url, protocol, host, path, query, port, websocket)) | ||||||
|  |         { | ||||||
|  |             return; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         std::cout << "[" << url << "]" << std::endl; | ||||||
|  |         std::cout << protocol << std::endl; | ||||||
|  |         std::cout << host << std::endl; | ||||||
|  |         std::cout << port << std::endl; | ||||||
|  |         std::cout << path << std::endl; | ||||||
|  |         std::cout << query << std::endl; | ||||||
|  |         std::cout << "-------------------------------" << std::endl; | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										31
									
								
								ixwebsocket/IXUrlParser.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								ixwebsocket/IXUrlParser.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,31 @@ | |||||||
|  | /* | ||||||
|  |  *  IXUrlParser.h | ||||||
|  |  *  Author: Benjamin Sergeant | ||||||
|  |  *  Copyright (c) 2019 Machine Zone, Inc. All rights reserved. | ||||||
|  |  */ | ||||||
|  |  | ||||||
|  | #pragma once | ||||||
|  |  | ||||||
|  | #include <string> | ||||||
|  | #include <regex> | ||||||
|  |  | ||||||
|  | namespace ix | ||||||
|  | { | ||||||
|  |     class UrlParser | ||||||
|  |     { | ||||||
|  |     public: | ||||||
|  |         static bool parse(const std::string& url, | ||||||
|  |                           std::string& protocol, | ||||||
|  |                           std::string& host, | ||||||
|  |                           std::string& path, | ||||||
|  |                           std::string& query, | ||||||
|  |                           int& port, | ||||||
|  |                           bool websocket); | ||||||
|  |  | ||||||
|  |         static void printUrl(const std::string& url, bool websocket); | ||||||
|  |  | ||||||
|  |     private: | ||||||
|  |         static std::regex _httpRegex; | ||||||
|  |         static std::regex _webSocketRegex; | ||||||
|  |     }; | ||||||
|  | } | ||||||
| @@ -294,9 +294,10 @@ namespace ix | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     WebSocketSendInfo WebSocket::send(const std::string& text) |     WebSocketSendInfo WebSocket::send(const std::string& text, | ||||||
|  |                                       const OnProgressCallback& onProgressCallback) | ||||||
|     { |     { | ||||||
|         return sendMessage(text, false); |         return sendMessage(text, false, onProgressCallback); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     WebSocketSendInfo WebSocket::ping(const std::string& text) |     WebSocketSendInfo WebSocket::ping(const std::string& text) | ||||||
| @@ -308,7 +309,9 @@ namespace ix | |||||||
|         return sendMessage(text, true); |         return sendMessage(text, true); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     WebSocketSendInfo WebSocket::sendMessage(const std::string& text, bool ping) |     WebSocketSendInfo WebSocket::sendMessage(const std::string& text, | ||||||
|  |                                              bool ping, | ||||||
|  |                                              const OnProgressCallback& onProgressCallback) | ||||||
|     { |     { | ||||||
|         if (!isConnected()) return WebSocketSendInfo(false); |         if (!isConnected()) return WebSocketSendInfo(false); | ||||||
|  |  | ||||||
| @@ -330,7 +333,7 @@ namespace ix | |||||||
|         } |         } | ||||||
|         else |         else | ||||||
|         { |         { | ||||||
|             webSocketSendInfo = _ws.sendBinary(text); |             webSocketSendInfo = _ws.sendBinary(text, onProgressCallback); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         WebSocket::invokeTrafficTrackerCallback(webSocketSendInfo.wireSize, false); |         WebSocket::invokeTrafficTrackerCallback(webSocketSendInfo.wireSize, false); | ||||||
|   | |||||||
| @@ -19,6 +19,7 @@ | |||||||
| #include "IXWebSocketSendInfo.h" | #include "IXWebSocketSendInfo.h" | ||||||
| #include "IXWebSocketPerMessageDeflateOptions.h" | #include "IXWebSocketPerMessageDeflateOptions.h" | ||||||
| #include "IXWebSocketHttpHeaders.h" | #include "IXWebSocketHttpHeaders.h" | ||||||
|  | #include "IXProgressCallback.h" | ||||||
|  |  | ||||||
| namespace ix | namespace ix | ||||||
| { | { | ||||||
| @@ -97,7 +98,8 @@ namespace ix | |||||||
|         WebSocketInitResult connect(int timeoutSecs); |         WebSocketInitResult connect(int timeoutSecs); | ||||||
|         void run(); |         void run(); | ||||||
|  |  | ||||||
|         WebSocketSendInfo send(const std::string& text); |         WebSocketSendInfo send(const std::string& text, | ||||||
|  |                                const OnProgressCallback& onProgressCallback = nullptr); | ||||||
|         WebSocketSendInfo ping(const std::string& text); |         WebSocketSendInfo ping(const std::string& text); | ||||||
|         void close(); |         void close(); | ||||||
|  |  | ||||||
| @@ -115,7 +117,9 @@ namespace ix | |||||||
|  |  | ||||||
|     private: |     private: | ||||||
|  |  | ||||||
|         WebSocketSendInfo sendMessage(const std::string& text, bool ping); |         WebSocketSendInfo sendMessage(const std::string& text, | ||||||
|  |                                       bool ping, | ||||||
|  |                                       const OnProgressCallback& callback = nullptr); | ||||||
|  |  | ||||||
|         bool isConnected() const; |         bool isConnected() const; | ||||||
|         bool isClosing() const; |         bool isClosing() const; | ||||||
|   | |||||||
| @@ -6,6 +6,7 @@ | |||||||
|  |  | ||||||
| #include "IXWebSocketHandshake.h" | #include "IXWebSocketHandshake.h" | ||||||
| #include "IXSocketConnect.h" | #include "IXSocketConnect.h" | ||||||
|  | #include "IXUrlParser.h" | ||||||
|  |  | ||||||
| #include "libwshandshake.hpp" | #include "libwshandshake.hpp" | ||||||
|  |  | ||||||
| @@ -32,90 +33,6 @@ namespace ix | |||||||
|  |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     bool WebSocketHandshake::parseUrl(const std::string& url, |  | ||||||
|                                       std::string& protocol, |  | ||||||
|                                       std::string& host, |  | ||||||
|                                       std::string& path, |  | ||||||
|                                       std::string& query, |  | ||||||
|                                       int& port) |  | ||||||
|     { |  | ||||||
|         std::regex ex("(ws|wss)://([^/ :]+):?([^/ ]*)(/?[^ #?]*)\\x3f?([^ #]*)#?([^ ]*)"); |  | ||||||
|         std::cmatch what; |  | ||||||
|         if (!regex_match(url.c_str(), what, ex)) |  | ||||||
|         { |  | ||||||
|             return false; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         std::string portStr; |  | ||||||
|  |  | ||||||
|         protocol = std::string(what[1].first, what[1].second); |  | ||||||
|         host     = std::string(what[2].first, what[2].second); |  | ||||||
|         portStr  = std::string(what[3].first, what[3].second); |  | ||||||
|         path     = std::string(what[4].first, what[4].second); |  | ||||||
|         query    = std::string(what[5].first, what[5].second); |  | ||||||
|  |  | ||||||
|         if (portStr.empty()) |  | ||||||
|         { |  | ||||||
|             if (protocol == "ws") |  | ||||||
|             { |  | ||||||
|                 port = 80; |  | ||||||
|             } |  | ||||||
|             else if (protocol == "wss") |  | ||||||
|             { |  | ||||||
|                 port = 443; |  | ||||||
|             } |  | ||||||
|             else |  | ||||||
|             { |  | ||||||
|                 // Invalid protocol. Should be caught by regex check |  | ||||||
|                 // but this missing branch trigger cpplint linter. |  | ||||||
|                 return false; |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|         else |  | ||||||
|         { |  | ||||||
|             std::stringstream ss; |  | ||||||
|             ss << portStr; |  | ||||||
|             ss >> port; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         if (path.empty()) |  | ||||||
|         { |  | ||||||
|             path = "/"; |  | ||||||
|         } |  | ||||||
|         else if (path[0] != '/') |  | ||||||
|         { |  | ||||||
|             path = '/' + path; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         if (!query.empty()) |  | ||||||
|         { |  | ||||||
|             path += "?"; |  | ||||||
|             path += query; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         return true; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     void WebSocketHandshake::printUrl(const std::string& url) |  | ||||||
|     { |  | ||||||
|         std::string protocol, host, path, query; |  | ||||||
|         int port {0}; |  | ||||||
|  |  | ||||||
|         if (!WebSocketHandshake::parseUrl(url, protocol, host, |  | ||||||
|                                           path, query, port)) |  | ||||||
|         { |  | ||||||
|             return; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         std::cout << "[" << url << "]" << std::endl; |  | ||||||
|         std::cout << protocol << std::endl; |  | ||||||
|         std::cout << host << std::endl; |  | ||||||
|         std::cout << port << std::endl; |  | ||||||
|         std::cout << path << std::endl; |  | ||||||
|         std::cout << query << std::endl; |  | ||||||
|         std::cout << "-------------------------------" << std::endl; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     std::string WebSocketHandshake::trim(const std::string& str) |     std::string WebSocketHandshake::trim(const std::string& str) | ||||||
|     { |     { | ||||||
|         std::string out(str); |         std::string out(str); | ||||||
| @@ -192,61 +109,6 @@ namespace ix | |||||||
|         return s; |         return s; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |  | ||||||
|     std::pair<bool, WebSocketHttpHeaders> WebSocketHandshake::parseHttpHeaders( |  | ||||||
|         const CancellationRequest& isCancellationRequested) |  | ||||||
|     { |  | ||||||
|         WebSocketHttpHeaders headers; |  | ||||||
|  |  | ||||||
|         char line[256]; |  | ||||||
|         int i; |  | ||||||
|  |  | ||||||
|         while (true)  |  | ||||||
|         { |  | ||||||
|             int colon = 0; |  | ||||||
|  |  | ||||||
|             for (i = 0; |  | ||||||
|                  i < 2 || (i < 255 && line[i-2] != '\r' && line[i-1] != '\n'); |  | ||||||
|                  ++i) |  | ||||||
|             { |  | ||||||
|                 if (!_socket->readByte(line+i, isCancellationRequested)) |  | ||||||
|                 { |  | ||||||
|                     return std::make_pair(false, headers); |  | ||||||
|                 } |  | ||||||
|  |  | ||||||
|                 if (line[i] == ':' && colon == 0) |  | ||||||
|                 { |  | ||||||
|                     colon = i; |  | ||||||
|                 } |  | ||||||
|             } |  | ||||||
|             if (line[0] == '\r' && line[1] == '\n') |  | ||||||
|             { |  | ||||||
|                 break; |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|             // line is a single header entry. split by ':', and add it to our |  | ||||||
|             // header map. ignore lines with no colon. |  | ||||||
|             if (colon > 0) |  | ||||||
|             { |  | ||||||
|                 line[i] = '\0'; |  | ||||||
|                 std::string lineStr(line); |  | ||||||
|                 // colon is ':', colon+1 is ' ', colon+2 is the start of the value. |  | ||||||
|                 // i is end of string (\0), i-colon is length of string minus key; |  | ||||||
|                 // subtract 1 for '\0', 1 for '\n', 1 for '\r', |  | ||||||
|                 // 1 for the ' ' after the ':', and total is -4 |  | ||||||
|                 std::string name(lineStr.substr(0, colon)); |  | ||||||
|                 std::string value(lineStr.substr(colon + 2, i - colon - 4)); |  | ||||||
|  |  | ||||||
|                 // Make the name lower case. |  | ||||||
|                 std::transform(name.begin(), name.end(), name.begin(), ::tolower); |  | ||||||
|  |  | ||||||
|                 headers[name] = value; |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         return std::make_pair(true, headers); |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     WebSocketInitResult WebSocketHandshake::sendErrorResponse(int code, const std::string& reason) |     WebSocketInitResult WebSocketHandshake::sendErrorResponse(int code, const std::string& reason) | ||||||
|     { |     { | ||||||
|         std::stringstream ss; |         std::stringstream ss; | ||||||
| @@ -355,7 +217,7 @@ namespace ix | |||||||
|             return WebSocketInitResult(false, status, ss.str()); |             return WebSocketInitResult(false, status, ss.str()); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         auto result = parseHttpHeaders(isCancellationRequested); |         auto result = parseHttpHeaders(_socket, isCancellationRequested); | ||||||
|         auto headersValid = result.first; |         auto headersValid = result.first; | ||||||
|         auto headers = result.second; |         auto headers = result.second; | ||||||
|  |  | ||||||
| @@ -450,7 +312,7 @@ namespace ix | |||||||
|         } |         } | ||||||
|  |  | ||||||
|         // Retrieve and validate HTTP headers |         // Retrieve and validate HTTP headers | ||||||
|         auto result = parseHttpHeaders(isCancellationRequested); |         auto result = parseHttpHeaders(_socket, isCancellationRequested); | ||||||
|         auto headersValid = result.first; |         auto headersValid = result.first; | ||||||
|         auto headers = result.second; |         auto headers = result.second; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -59,19 +59,10 @@ namespace ix | |||||||
|         WebSocketInitResult serverHandshake(int fd, |         WebSocketInitResult serverHandshake(int fd, | ||||||
|                                             int timeoutSecs); |                                             int timeoutSecs); | ||||||
|  |  | ||||||
|         static bool parseUrl(const std::string& url, |  | ||||||
|                              std::string& protocol, |  | ||||||
|                              std::string& host, |  | ||||||
|                              std::string& path, |  | ||||||
|                              std::string& query, |  | ||||||
|                              int& port); |  | ||||||
|  |  | ||||||
|     private: |     private: | ||||||
|         static void printUrl(const std::string& url); |  | ||||||
|         std::string genRandomString(const int len); |         std::string genRandomString(const int len); | ||||||
|  |  | ||||||
|         // Parse HTTP headers |         // Parse HTTP headers | ||||||
|         std::pair<bool, WebSocketHttpHeaders> parseHttpHeaders(const CancellationRequest& isCancellationRequested); |  | ||||||
|         WebSocketInitResult sendErrorResponse(int code, const std::string& reason); |         WebSocketInitResult sendErrorResponse(int code, const std::string& reason); | ||||||
|  |  | ||||||
|         std::tuple<std::string, std::string, std::string> parseRequestLine(const std::string& line); |         std::tuple<std::string, std::string, std::string> parseRequestLine(const std::string& line); | ||||||
|   | |||||||
							
								
								
									
										66
									
								
								ixwebsocket/IXWebSocketHttpHeaders.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										66
									
								
								ixwebsocket/IXWebSocketHttpHeaders.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,66 @@ | |||||||
|  | /* | ||||||
|  |  *  IXWebSocketHttpHeaders.h | ||||||
|  |  *  Author: Benjamin Sergeant | ||||||
|  |  *  Copyright (c) 2018 Machine Zone, Inc. All rights reserved. | ||||||
|  |  */ | ||||||
|  |  | ||||||
|  | #include "IXWebSocketHttpHeaders.h" | ||||||
|  | #include "IXSocket.h" | ||||||
|  |  | ||||||
|  | #include <string> | ||||||
|  | #include <unordered_map> | ||||||
|  |  | ||||||
|  | namespace ix | ||||||
|  | { | ||||||
|  |     std::pair<bool, WebSocketHttpHeaders> parseHttpHeaders( | ||||||
|  |         std::shared_ptr<Socket> socket, | ||||||
|  |         const CancellationRequest& isCancellationRequested) | ||||||
|  |     { | ||||||
|  |         WebSocketHttpHeaders headers; | ||||||
|  |  | ||||||
|  |         char line[1024]; | ||||||
|  |         int i; | ||||||
|  |  | ||||||
|  |         while (true) | ||||||
|  |         { | ||||||
|  |             int colon = 0; | ||||||
|  |  | ||||||
|  |             for (i = 0; | ||||||
|  |                  i < 2 || (i < 1023 && line[i-2] != '\r' && line[i-1] != '\n'); | ||||||
|  |                  ++i) | ||||||
|  |             { | ||||||
|  |                 if (!socket->readByte(line+i, isCancellationRequested)) | ||||||
|  |                 { | ||||||
|  |                     return std::make_pair(false, headers); | ||||||
|  |                 } | ||||||
|  |  | ||||||
|  |                 if (line[i] == ':' && colon == 0) | ||||||
|  |                 { | ||||||
|  |                     colon = i; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             if (line[0] == '\r' && line[1] == '\n') | ||||||
|  |             { | ||||||
|  |                 break; | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             // line is a single header entry. split by ':', and add it to our | ||||||
|  |             // header map. ignore lines with no colon. | ||||||
|  |             if (colon > 0) | ||||||
|  |             { | ||||||
|  |                 line[i] = '\0'; | ||||||
|  |                 std::string lineStr(line); | ||||||
|  |                 // colon is ':', colon+1 is ' ', colon+2 is the start of the value. | ||||||
|  |                 // i is end of string (\0), i-colon is length of string minus key; | ||||||
|  |                 // subtract 1 for '\0', 1 for '\n', 1 for '\r', | ||||||
|  |                 // 1 for the ' ' after the ':', and total is -4 | ||||||
|  |                 std::string name(lineStr.substr(0, colon)); | ||||||
|  |                 std::string value(lineStr.substr(colon + 2, i - colon - 4)); | ||||||
|  |  | ||||||
|  |                 headers[name] = value; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         return std::make_pair(true, headers); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -6,10 +6,40 @@ | |||||||
|  |  | ||||||
| #pragma once | #pragma once | ||||||
|  |  | ||||||
|  | #include "IXCancellationRequest.h" | ||||||
|  |  | ||||||
| #include <string> | #include <string> | ||||||
| #include <unordered_map> | #include <map> | ||||||
|  | #include <memory> | ||||||
|  | #include <algorithm> | ||||||
|  |  | ||||||
| namespace ix | namespace ix | ||||||
| { | { | ||||||
|     using WebSocketHttpHeaders = std::unordered_map<std::string, std::string>; |     class Socket; | ||||||
|  |  | ||||||
|  |     struct CaseInsensitiveLess | ||||||
|  |     { | ||||||
|  |         // Case Insensitive compare_less binary function | ||||||
|  |         struct NocaseCompare | ||||||
|  |         { | ||||||
|  |             bool operator() (const unsigned char& c1, const unsigned char& c2) const | ||||||
|  |             { | ||||||
|  |                 return std::tolower(c1) < std::tolower(c2); | ||||||
|  |             } | ||||||
|  |         }; | ||||||
|  |  | ||||||
|  |         bool operator() (const std::string & s1, const std::string & s2) const | ||||||
|  |         { | ||||||
|  |             return std::lexicographical_compare | ||||||
|  |                 (s1.begin(), s1.end(),   // source range | ||||||
|  |                  s2.begin(), s2.end(),   // dest range | ||||||
|  |                  NocaseCompare());  // comparison | ||||||
|  |         } | ||||||
|  |     }; | ||||||
|  |  | ||||||
|  |     using WebSocketHttpHeaders = std::map<std::string, std::string, CaseInsensitiveLess>; | ||||||
|  |  | ||||||
|  |     std::pair<bool, WebSocketHttpHeaders> parseHttpHeaders( | ||||||
|  |         std::shared_ptr<Socket> socket, | ||||||
|  |         const CancellationRequest& isCancellationRequested); | ||||||
| } | } | ||||||
|   | |||||||
| @@ -11,6 +11,8 @@ | |||||||
| #include "IXWebSocketTransport.h" | #include "IXWebSocketTransport.h" | ||||||
| #include "IXWebSocketHandshake.h" | #include "IXWebSocketHandshake.h" | ||||||
| #include "IXWebSocketHttpHeaders.h" | #include "IXWebSocketHttpHeaders.h" | ||||||
|  | #include "IXUrlParser.h" | ||||||
|  | #include "IXSocketFactory.h" | ||||||
|  |  | ||||||
| #ifdef IXWEBSOCKET_USE_TLS | #ifdef IXWEBSOCKET_USE_TLS | ||||||
| # ifdef __APPLE__ | # ifdef __APPLE__ | ||||||
| @@ -29,12 +31,15 @@ | |||||||
| #include <cstdarg> | #include <cstdarg> | ||||||
| #include <iostream> | #include <iostream> | ||||||
| #include <sstream> | #include <sstream> | ||||||
|  | #include <chrono> | ||||||
|  | #include <thread> | ||||||
|  |  | ||||||
|  |  | ||||||
| namespace ix | namespace ix | ||||||
| { | { | ||||||
|     const std::string WebSocketTransport::kHeartBeatPingMessage("ixwebsocket::hearbeat"); |     const std::string WebSocketTransport::kHeartBeatPingMessage("ixwebsocket::hearbeat"); | ||||||
|     const int WebSocketTransport::kDefaultHeartBeatPeriod(-1); |     const int WebSocketTransport::kDefaultHeartBeatPeriod(-1); | ||||||
|  |     constexpr size_t WebSocketTransport::kChunkSize; | ||||||
|  |  | ||||||
|     WebSocketTransport::WebSocketTransport() : |     WebSocketTransport::WebSocketTransport() : | ||||||
|         _readyState(CLOSED), |         _readyState(CLOSED), | ||||||
| @@ -45,7 +50,7 @@ namespace ix | |||||||
|         _heartBeatPeriod(kDefaultHeartBeatPeriod), |         _heartBeatPeriod(kDefaultHeartBeatPeriod), | ||||||
|         _lastSendTimePoint(std::chrono::steady_clock::now()) |         _lastSendTimePoint(std::chrono::steady_clock::now()) | ||||||
|     { |     { | ||||||
|  |         _readbuf.resize(kChunkSize); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     WebSocketTransport::~WebSocketTransport() |     WebSocketTransport::~WebSocketTransport() | ||||||
| @@ -67,31 +72,31 @@ namespace ix | |||||||
|     { |     { | ||||||
|         std::string protocol, host, path, query; |         std::string protocol, host, path, query; | ||||||
|         int port; |         int port; | ||||||
|  |         bool websocket = true; | ||||||
|  |  | ||||||
|         if (!WebSocketHandshake::parseUrl(url, protocol, host, |         if (!UrlParser::parse(url, protocol, host, path, query, port, websocket)) | ||||||
|                                           path, query, port)) |  | ||||||
|         { |         { | ||||||
|             return WebSocketInitResult(false, 0, |             return WebSocketInitResult(false, 0, | ||||||
|                                        std::string("Could not parse URL ") + url); |                                        std::string("Could not parse URL ") + url); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         if (protocol == "wss") |         if (protocol != "ws" && protocol != "wss") | ||||||
|         { |         { | ||||||
|             _socket.reset(); |             std::stringstream ss; | ||||||
| #ifdef IXWEBSOCKET_USE_TLS |             ss << "Invalid protocol: " << protocol | ||||||
| # ifdef __APPLE__ |                << " for url " << url | ||||||
|              _socket = std::make_shared<SocketAppleSSL>(); |                << " . Supported protocols are ws and wss"; | ||||||
| # else |  | ||||||
|              _socket = std::make_shared<SocketOpenSSL>(); |             return WebSocketInitResult(false, 0, ss.str()); | ||||||
| # endif |  | ||||||
| #else |  | ||||||
|             return WebSocketInitResult(false, 0, "TLS is not supported."); |  | ||||||
| #endif |  | ||||||
|         } |         } | ||||||
|         else |  | ||||||
|  |         bool tls = protocol == "wss"; | ||||||
|  |         std::string errorMsg; | ||||||
|  |         _socket = createSocket(tls, errorMsg); | ||||||
|  |  | ||||||
|  |         if (!_socket) | ||||||
|         { |         { | ||||||
|             _socket.reset(); |             return WebSocketInitResult(false, 0, errorMsg); | ||||||
|             _socket = std::make_shared<Socket>(); |  | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         WebSocketHandshake webSocketHandshake(_requestInitCancellation, |         WebSocketHandshake webSocketHandshake(_requestInitCancellation, | ||||||
| @@ -156,7 +161,9 @@ namespace ix | |||||||
|         _onCloseCallback = onCloseCallback; |         _onCloseCallback = onCloseCallback; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     bool WebSocketTransport::exceedSendHeartBeatTimeOut() |     // Only consider send time points for that computation. | ||||||
|  |     // The receive time points is taken into account in Socket::poll (second parameter). | ||||||
|  |     bool WebSocketTransport::heartBeatPeriodExceeded() | ||||||
|     { |     { | ||||||
|         std::lock_guard<std::mutex> lock(_lastSendTimePointMutex); |         std::lock_guard<std::mutex> lock(_lastSendTimePointMutex); | ||||||
|         auto now = std::chrono::steady_clock::now(); |         auto now = std::chrono::steady_clock::now(); | ||||||
| @@ -172,7 +179,7 @@ namespace ix | |||||||
|                 // send for a duration exceeding our heart-beat period, send a |                 // send for a duration exceeding our heart-beat period, send a | ||||||
|                 // ping to the server. |                 // ping to the server. | ||||||
|                 if (pollResult == PollResultType_Timeout && |                 if (pollResult == PollResultType_Timeout && | ||||||
|                     exceedSendHeartBeatTimeOut()) |                     heartBeatPeriodExceeded()) | ||||||
|                 { |                 { | ||||||
|                     std::stringstream ss; |                     std::stringstream ss; | ||||||
|                     ss << kHeartBeatPingMessage << "::" << _heartBeatPeriod << "s"; |                     ss << kHeartBeatPingMessage << "::" << _heartBeatPeriod << "s"; | ||||||
| @@ -182,27 +189,25 @@ namespace ix | |||||||
|  |  | ||||||
|                 while (true) |                 while (true) | ||||||
|                 { |                 { | ||||||
|                     int N = (int) _rxbuf.size(); |                     ssize_t ret = _socket->recv((char*)&_readbuf[0], _readbuf.size()); | ||||||
|  |  | ||||||
|                     _rxbuf.resize(N + 1500); |  | ||||||
|                     ssize_t ret = _socket->recv((char*)&_rxbuf[0] + N, 1500); |  | ||||||
|  |  | ||||||
|                     if (ret < 0 && (_socket->getErrno() == EWOULDBLOCK || |                     if (ret < 0 && (_socket->getErrno() == EWOULDBLOCK || | ||||||
|                                     _socket->getErrno() == EAGAIN)) { |                                     _socket->getErrno() == EAGAIN)) | ||||||
|                         _rxbuf.resize(N); |                     { | ||||||
|                         break; |                         break; | ||||||
|                     } |                     } | ||||||
|                     else if (ret <= 0) |                     else if (ret <= 0) | ||||||
|                     { |                     { | ||||||
|                         _rxbuf.resize(N); |                         _rxbuf.clear(); | ||||||
|  |  | ||||||
|                         _socket->close(); |                         _socket->close(); | ||||||
|                         setReadyState(CLOSED); |                         setReadyState(CLOSED); | ||||||
|                         break; |                         break; | ||||||
|                     } |                     } | ||||||
|                     else |                     else | ||||||
|                     { |                     { | ||||||
|                         _rxbuf.resize(N + ret); |                         _rxbuf.insert(_rxbuf.end(), | ||||||
|  |                                       _readbuf.begin(), | ||||||
|  |                                       _readbuf.begin() + ret); | ||||||
|                     } |                     } | ||||||
|                 } |                 } | ||||||
|  |  | ||||||
| @@ -358,17 +363,35 @@ namespace ix | |||||||
|                 || ws.opcode == wsheader_type::CONTINUATION |                 || ws.opcode == wsheader_type::CONTINUATION | ||||||
|             ) { |             ) { | ||||||
|                 unmaskReceiveBuffer(ws); |                 unmaskReceiveBuffer(ws); | ||||||
|                 _receivedData.insert(_receivedData.end(), |  | ||||||
|                                      _rxbuf.begin()+ws.header_size, |                 // | ||||||
|                                      _rxbuf.begin()+ws.header_size+(size_t)ws.N);// just feed |                 // Usual case. Small unfragmented messages | ||||||
|  |                 // | ||||||
|  |                 if (ws.fin && _chunks.empty()) | ||||||
|  |                 { | ||||||
|  |                     emitMessage(MSG, | ||||||
|  |                                 std::string(_rxbuf.begin()+ws.header_size, | ||||||
|  |                                             _rxbuf.begin()+ws.header_size+(size_t) ws.N), | ||||||
|  |                                 ws, | ||||||
|  |                                 onMessageCallback); | ||||||
|  |                 } | ||||||
|  |                 else | ||||||
|  |                 { | ||||||
|  |                     // | ||||||
|  |                     // Add intermediary message to our chunk list. | ||||||
|  |                     // We use a chunk list instead of a big buffer because resizing | ||||||
|  |                     // large buffer can be very costly when we need to re-allocate | ||||||
|  |                     // the internal buffer which is slow and can let the internal OS | ||||||
|  |                     // receive buffer fill out. | ||||||
|  |                     // | ||||||
|  |                     _chunks.emplace_back( | ||||||
|  |                         std::vector<uint8_t>(_rxbuf.begin()+ws.header_size, | ||||||
|  |                                              _rxbuf.begin()+ws.header_size+(size_t)ws.N)); | ||||||
|                     if (ws.fin) |                     if (ws.fin) | ||||||
|                     { |                     { | ||||||
|                     // fire callback with a string message |                         emitMessage(MSG, getMergedChunks(), ws, onMessageCallback); | ||||||
|                     std::string stringMessage(_receivedData.begin(), |                         _chunks.clear(); | ||||||
|                                               _receivedData.end()); |                     } | ||||||
|  |  | ||||||
|                     emitMessage(MSG, stringMessage, ws, onMessageCallback); |  | ||||||
|                     _receivedData.clear(); |  | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|             else if (ws.opcode == wsheader_type::PING) |             else if (ws.opcode == wsheader_type::PING) | ||||||
| @@ -418,11 +441,32 @@ namespace ix | |||||||
|                 close(); |                 close(); | ||||||
|             } |             } | ||||||
|  |  | ||||||
|  |             // Erase the message that has been processed from the input/read buffer | ||||||
|             _rxbuf.erase(_rxbuf.begin(), |             _rxbuf.erase(_rxbuf.begin(), | ||||||
|                          _rxbuf.begin() + ws.header_size + (size_t) ws.N); |                          _rxbuf.begin() + ws.header_size + (size_t) ws.N); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     std::string WebSocketTransport::getMergedChunks() const | ||||||
|  |     { | ||||||
|  |         size_t length = 0; | ||||||
|  |         for (auto&& chunk : _chunks) | ||||||
|  |         { | ||||||
|  |             length += chunk.size(); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         std::string msg; | ||||||
|  |         msg.reserve(length); | ||||||
|  |  | ||||||
|  |         for (auto&& chunk : _chunks) | ||||||
|  |         { | ||||||
|  |             std::string str(chunk.begin(), chunk.end()); | ||||||
|  |             msg += str; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         return msg; | ||||||
|  |     } | ||||||
|  |  | ||||||
|     void WebSocketTransport::emitMessage(MessageKind messageKind, |     void WebSocketTransport::emitMessage(MessageKind messageKind, | ||||||
|                                          const std::string& message, |                                          const std::string& message, | ||||||
|                                          const wsheader_type& ws, |                                          const wsheader_type& ws, | ||||||
| @@ -452,9 +496,11 @@ namespace ix | |||||||
|         return static_cast<unsigned>(seconds); |         return static_cast<unsigned>(seconds); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     WebSocketSendInfo WebSocketTransport::sendData(wsheader_type::opcode_type type,  |     WebSocketSendInfo WebSocketTransport::sendData( | ||||||
|  |         wsheader_type::opcode_type type, | ||||||
|         const std::string& message, |         const std::string& message, | ||||||
|                                                    bool compress) |         bool compress, | ||||||
|  |         const OnProgressCallback& onProgressCallback) | ||||||
|     { |     { | ||||||
|         if (_readyState == CLOSING || _readyState == CLOSED) |         if (_readyState == CLOSING || _readyState == CLOSED) | ||||||
|         { |         { | ||||||
| @@ -471,15 +517,81 @@ namespace ix | |||||||
|  |  | ||||||
|         if (compress) |         if (compress) | ||||||
|         { |         { | ||||||
|             bool success = _perMessageDeflate.compress(message, compressedMessage); |             if (!_perMessageDeflate.compress(message, compressedMessage)) | ||||||
|             compressionError = !success; |             { | ||||||
|  |                 bool success = false; | ||||||
|  |                 compressionError = true; | ||||||
|  |                 payloadSize = 0; | ||||||
|  |                 wireSize = 0; | ||||||
|  |                 return WebSocketSendInfo(success, compressionError, payloadSize, wireSize); | ||||||
|  |             } | ||||||
|  |             compressionError = false; | ||||||
|             wireSize = compressedMessage.size(); |             wireSize = compressedMessage.size(); | ||||||
|  |  | ||||||
|             message_begin = compressedMessage.begin(); |             message_begin = compressedMessage.begin(); | ||||||
|             message_end = compressedMessage.end(); |             message_end = compressedMessage.end(); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         uint64_t message_size = wireSize; |         // Common case for most message. No fragmentation required. | ||||||
|  |         if (wireSize < kChunkSize) | ||||||
|  |         { | ||||||
|  |             sendFragment(type, true, message_begin, message_end, compress); | ||||||
|  |         } | ||||||
|  |         else | ||||||
|  |         { | ||||||
|  |             // | ||||||
|  |             // Large messages need to be fragmented | ||||||
|  |             // | ||||||
|  |             // Rules: | ||||||
|  |             // First message needs to specify a proper type (BINARY or TEXT) | ||||||
|  |             // Intermediary and last messages need to be of type CONTINUATION | ||||||
|  |             // Last message must set the fin byte. | ||||||
|  |             // | ||||||
|  |             auto steps = wireSize / kChunkSize; | ||||||
|  |  | ||||||
|  |             std::string::const_iterator begin = message_begin; | ||||||
|  |             std::string::const_iterator end = message_end; | ||||||
|  |  | ||||||
|  |             for (uint64_t i = 0 ; i < steps; ++i) | ||||||
|  |             { | ||||||
|  |                 bool firstStep = i == 0; | ||||||
|  |                 bool lastStep = (i+1) == steps; | ||||||
|  |                 bool fin = lastStep; | ||||||
|  |  | ||||||
|  |                 end = begin + kChunkSize; | ||||||
|  |                 if (lastStep) | ||||||
|  |                 { | ||||||
|  |                     end = message_end; | ||||||
|  |                 } | ||||||
|  |  | ||||||
|  |                 auto opcodeType = type; | ||||||
|  |                 if (!firstStep) | ||||||
|  |                 { | ||||||
|  |                     opcodeType = wsheader_type::CONTINUATION; | ||||||
|  |                 } | ||||||
|  |  | ||||||
|  |                 // Send message | ||||||
|  |                 sendFragment(opcodeType, fin, begin, end, compress); | ||||||
|  |  | ||||||
|  |                 if (onProgressCallback && !onProgressCallback(i, steps)) | ||||||
|  |                 { | ||||||
|  |                     break; | ||||||
|  |                 } | ||||||
|  |  | ||||||
|  |                 begin += kChunkSize; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         return WebSocketSendInfo(true, compressionError, payloadSize, wireSize); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     void WebSocketTransport::sendFragment(wsheader_type::opcode_type type, | ||||||
|  |                                           bool fin, | ||||||
|  |                                           std::string::const_iterator message_begin, | ||||||
|  |                                           std::string::const_iterator message_end, | ||||||
|  |                                           bool compress) | ||||||
|  |     { | ||||||
|  |         auto message_size = message_end - message_begin; | ||||||
|  |  | ||||||
|         unsigned x = getRandomUnsigned(); |         unsigned x = getRandomUnsigned(); | ||||||
|         uint8_t masking_key[4] = {}; |         uint8_t masking_key[4] = {}; | ||||||
| @@ -492,7 +604,13 @@ namespace ix | |||||||
|         header.assign(2 + |         header.assign(2 + | ||||||
|                       (message_size >= 126 ? 2 : 0) + |                       (message_size >= 126 ? 2 : 0) + | ||||||
|                       (message_size >= 65536 ? 6 : 0) + 4, 0); |                       (message_size >= 65536 ? 6 : 0) + 4, 0); | ||||||
|         header[0] = 0x80 | type; |         header[0] = type; | ||||||
|  |  | ||||||
|  |         // The fin bit indicate that this is the last fragment. Fin is French for end. | ||||||
|  |         if (fin) | ||||||
|  |         { | ||||||
|  |             header[0] |= 0x80; | ||||||
|  |         } | ||||||
|  |  | ||||||
|         // This bit indicate that the frame is compressed |         // This bit indicate that the frame is compressed | ||||||
|         if (compress) |         if (compress) | ||||||
| @@ -544,8 +662,6 @@ namespace ix | |||||||
|  |  | ||||||
|         // Now actually send this data |         // Now actually send this data | ||||||
|         sendOnSocket(); |         sendOnSocket(); | ||||||
|  |  | ||||||
|         return WebSocketSendInfo(true, compressionError, payloadSize, wireSize); |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     WebSocketSendInfo WebSocketTransport::sendPing(const std::string& message) |     WebSocketSendInfo WebSocketTransport::sendPing(const std::string& message) | ||||||
| @@ -554,9 +670,13 @@ namespace ix | |||||||
|         return sendData(wsheader_type::PING, message, compress); |         return sendData(wsheader_type::PING, message, compress); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     WebSocketSendInfo WebSocketTransport::sendBinary(const std::string& message)  |     WebSocketSendInfo WebSocketTransport::sendBinary( | ||||||
|  |         const std::string& message, | ||||||
|  |         const OnProgressCallback& onProgressCallback) | ||||||
|  |  | ||||||
|     { |     { | ||||||
|         return sendData(wsheader_type::BINARY_FRAME, message, _enablePerMessageDeflate); |         return sendData(wsheader_type::BINARY_FRAME, message, | ||||||
|  |                         _enablePerMessageDeflate, onProgressCallback); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     void WebSocketTransport::sendOnSocket() |     void WebSocketTransport::sendOnSocket() | ||||||
|   | |||||||
| @@ -16,6 +16,7 @@ | |||||||
| #include <memory> | #include <memory> | ||||||
| #include <mutex> | #include <mutex> | ||||||
| #include <atomic> | #include <atomic> | ||||||
|  | #include <list> | ||||||
|  |  | ||||||
| #include "IXWebSocketSendInfo.h" | #include "IXWebSocketSendInfo.h" | ||||||
| #include "IXWebSocketPerMessageDeflate.h" | #include "IXWebSocketPerMessageDeflate.h" | ||||||
| @@ -23,6 +24,7 @@ | |||||||
| #include "IXWebSocketHttpHeaders.h" | #include "IXWebSocketHttpHeaders.h" | ||||||
| #include "IXCancellationRequest.h" | #include "IXCancellationRequest.h" | ||||||
| #include "IXWebSocketHandshake.h" | #include "IXWebSocketHandshake.h" | ||||||
|  | #include "IXProgressCallback.h" | ||||||
|  |  | ||||||
| namespace ix | namespace ix | ||||||
| { | { | ||||||
| @@ -66,7 +68,8 @@ namespace ix | |||||||
|                                             int timeoutSecs); |                                             int timeoutSecs); | ||||||
|  |  | ||||||
|         void poll(); |         void poll(); | ||||||
|         WebSocketSendInfo sendBinary(const std::string& message); |         WebSocketSendInfo sendBinary(const std::string& message, | ||||||
|  |                                      const OnProgressCallback& onProgressCallback); | ||||||
|         WebSocketSendInfo sendPing(const std::string& message); |         WebSocketSendInfo sendPing(const std::string& message); | ||||||
|         void close(); |         void close(); | ||||||
|         ReadyStateValues getReadyState() const; |         ReadyStateValues getReadyState() const; | ||||||
| @@ -76,7 +79,6 @@ namespace ix | |||||||
|  |  | ||||||
|     private: |     private: | ||||||
|         std::string _url; |         std::string _url; | ||||||
|         std::string _origin; |  | ||||||
|  |  | ||||||
|         struct wsheader_type { |         struct wsheader_type { | ||||||
|             unsigned header_size; |             unsigned header_size; | ||||||
| @@ -96,13 +98,31 @@ namespace ix | |||||||
|             uint8_t masking_key[4]; |             uint8_t masking_key[4]; | ||||||
|         }; |         }; | ||||||
|  |  | ||||||
|  |         // Buffer for reading from our socket. That buffer is never resized. | ||||||
|  |         std::vector<uint8_t> _readbuf; | ||||||
|  |  | ||||||
|  |         // Contains all messages that were fetched in the last socket read. | ||||||
|  |         // This could be a mix of control messages (Close, Ping, etc...) and | ||||||
|  |         // data messages. That buffer | ||||||
|         std::vector<uint8_t> _rxbuf; |         std::vector<uint8_t> _rxbuf; | ||||||
|  |  | ||||||
|  |         // Contains all messages that are waiting to be sent | ||||||
|         std::vector<uint8_t> _txbuf; |         std::vector<uint8_t> _txbuf; | ||||||
|         mutable std::mutex _txbufMutex; |         mutable std::mutex _txbufMutex; | ||||||
|         std::vector<uint8_t> _receivedData; |  | ||||||
|  |  | ||||||
|  |         // Hold fragments for multi-fragments messages in a list. We support receiving very large | ||||||
|  |         // messages (tested messages up to 700M) and we cannot put them in a single | ||||||
|  |         // buffer that is resized, as this operation can be slow when a buffer has its | ||||||
|  |         // size increased 2 fold, while appending to a list has a fixed cost. | ||||||
|  |         std::list<std::vector<uint8_t>> _chunks; | ||||||
|  |  | ||||||
|  |         // Fragments are 32K long | ||||||
|  |         static constexpr size_t kChunkSize = 1 << 15; | ||||||
|  |  | ||||||
|  |         // Underlying TCP socket | ||||||
|         std::shared_ptr<Socket> _socket; |         std::shared_ptr<Socket> _socket; | ||||||
|  |  | ||||||
|  |         // Hold the state of the connection (OPEN, CLOSED, etc...) | ||||||
|         std::atomic<ReadyStateValues> _readyState; |         std::atomic<ReadyStateValues> _readyState; | ||||||
|  |  | ||||||
|         OnCloseCallback _onCloseCallback; |         OnCloseCallback _onCloseCallback; | ||||||
| @@ -111,6 +131,7 @@ namespace ix | |||||||
|         size_t _closeWireSize; |         size_t _closeWireSize; | ||||||
|         mutable std::mutex _closeDataMutex; |         mutable std::mutex _closeDataMutex; | ||||||
|  |  | ||||||
|  |         // Data used for Per Message Deflate compression (with zlib) | ||||||
|         WebSocketPerMessageDeflate _perMessageDeflate; |         WebSocketPerMessageDeflate _perMessageDeflate; | ||||||
|         WebSocketPerMessageDeflateOptions _perMessageDeflateOptions; |         WebSocketPerMessageDeflateOptions _perMessageDeflateOptions; | ||||||
|         std::atomic<bool> _enablePerMessageDeflate; |         std::atomic<bool> _enablePerMessageDeflate; | ||||||
| @@ -126,11 +147,18 @@ namespace ix | |||||||
|         std::chrono::time_point<std::chrono::steady_clock> _lastSendTimePoint; |         std::chrono::time_point<std::chrono::steady_clock> _lastSendTimePoint; | ||||||
|  |  | ||||||
|         // No data was send through the socket for longer that the hearbeat period |         // No data was send through the socket for longer that the hearbeat period | ||||||
|         bool exceedSendHeartBeatTimeOut(); |         bool heartBeatPeriodExceeded(); | ||||||
|  |  | ||||||
|         void sendOnSocket(); |         void sendOnSocket(); | ||||||
|         WebSocketSendInfo sendData(wsheader_type::opcode_type type, |         WebSocketSendInfo sendData(wsheader_type::opcode_type type, | ||||||
|                                    const std::string& message, |                                    const std::string& message, | ||||||
|  |                                    bool compress, | ||||||
|  |                                    const OnProgressCallback& onProgressCallback = nullptr); | ||||||
|  |  | ||||||
|  |         void sendFragment(wsheader_type::opcode_type type, | ||||||
|  |                           bool fin, | ||||||
|  |                           std::string::const_iterator begin, | ||||||
|  |                           std::string::const_iterator end, | ||||||
|                           bool compress); |                           bool compress); | ||||||
|  |  | ||||||
|         void emitMessage(MessageKind messageKind, |         void emitMessage(MessageKind messageKind, | ||||||
| @@ -148,5 +176,7 @@ namespace ix | |||||||
|  |  | ||||||
|         unsigned getRandomUnsigned(); |         unsigned getRandomUnsigned(); | ||||||
|         void unmaskReceiveBuffer(const wsheader_type& ws); |         void unmaskReceiveBuffer(const wsheader_type& ws); | ||||||
|  |  | ||||||
|  |         std::string getMergedChunks() const; | ||||||
|     }; |     }; | ||||||
| } | } | ||||||
|   | |||||||
							
								
								
									
										17
									
								
								makefile
									
									
									
									
									
								
							
							
						
						
									
										17
									
								
								makefile
									
									
									
									
									
								
							| @@ -1,14 +1,21 @@ | |||||||
| # | # | ||||||
| # This makefile is just used to easily work with docker (linux build) | # This makefile is just used to easily work with docker (linux build) | ||||||
| # | # | ||||||
| all: run | all: brew | ||||||
|  |  | ||||||
|  | brew: | ||||||
|  | 	mkdir -p build && (cd build ; cmake .. ; make -j install) | ||||||
|  |  | ||||||
| .PHONY: docker | .PHONY: docker | ||||||
| docker: | docker: | ||||||
| 	docker build -t ws_connect:latest . | 	docker build -t broadcast_server:latest . | ||||||
|  |  | ||||||
| run: docker | run: | ||||||
| 	docker run --cap-add sys_ptrace -it ws_connect:latest bash | 	docker run --cap-add sys_ptrace -it broadcast_server:latest bash | ||||||
|  |  | ||||||
|  | # this is helpful to remove trailing whitespaces | ||||||
|  | trail: | ||||||
|  | 	sh third_party/remote_trailing_whitespaces.sh | ||||||
|  |  | ||||||
| build: | build: | ||||||
| 	(cd examples/satori_publisher ; mkdir -p build ; cd build ; cmake .. ; make) | 	(cd examples/satori_publisher ; mkdir -p build ; cd build ; cmake .. ; make) | ||||||
| @@ -24,7 +31,7 @@ test_server: | |||||||
| 	(cd test && npm i ws && node broadcast-server.js) | 	(cd test && npm i ws && node broadcast-server.js) | ||||||
|  |  | ||||||
| # env TEST=Websocket_server make test | # env TEST=Websocket_server make test | ||||||
| # env TEST=websocket_server make test | # env TEST=Websocket_chat make test | ||||||
| # env TEST=heartbeat make test | # env TEST=heartbeat make test | ||||||
| test: | test: | ||||||
| 	python test/run.py | 	python test/run.py | ||||||
|   | |||||||
| @@ -18,13 +18,14 @@ add_subdirectory(${PROJECT_SOURCE_DIR}/.. ixwebsocket) | |||||||
|  |  | ||||||
| include_directories( | include_directories( | ||||||
|   ${PROJECT_SOURCE_DIR}/Catch2/single_include |   ${PROJECT_SOURCE_DIR}/Catch2/single_include | ||||||
|  |   ../third_party/msgpack11 | ||||||
| ) | ) | ||||||
|  |  | ||||||
| # Shared sources | # Shared sources | ||||||
| set (SOURCES  | set (SOURCES  | ||||||
|   test_runner.cpp |   test_runner.cpp | ||||||
|   IXTest.cpp |   IXTest.cpp | ||||||
|   msgpack11.cpp |   ../third_party/msgpack11/msgpack11.cpp | ||||||
|  |  | ||||||
|   IXDNSLookupTest.cpp |   IXDNSLookupTest.cpp | ||||||
|   IXSocketTest.cpp |   IXSocketTest.cpp | ||||||
|   | |||||||
| @@ -6,6 +6,7 @@ | |||||||
|  |  | ||||||
| #include "IXTest.h" | #include "IXTest.h" | ||||||
| #include <ixwebsocket/IXWebSocket.h> | #include <ixwebsocket/IXWebSocket.h> | ||||||
|  | #include <ixwebsocket/IXNetSystem.h> | ||||||
|  |  | ||||||
| #include <chrono> | #include <chrono> | ||||||
| #include <thread> | #include <thread> | ||||||
| @@ -14,12 +15,14 @@ | |||||||
| #include <fstream> | #include <fstream> | ||||||
| #include <iostream> | #include <iostream> | ||||||
| #include <stdlib.h> | #include <stdlib.h> | ||||||
|  | #include <stack> | ||||||
|  |  | ||||||
| namespace ix | namespace ix | ||||||
| { | { | ||||||
|     std::atomic<size_t> incomingBytes(0); |     std::atomic<size_t> incomingBytes(0); | ||||||
|     std::atomic<size_t> outgoingBytes(0); |     std::atomic<size_t> outgoingBytes(0); | ||||||
|     std::mutex Logger::_mutex; |     std::mutex Logger::_mutex; | ||||||
|  |     std::stack<int> freePorts; | ||||||
|  |  | ||||||
|     void setupWebSocketTrafficTrackerCallback() |     void setupWebSocketTrafficTrackerCallback() | ||||||
|     { |     { | ||||||
| @@ -66,4 +69,71 @@ namespace ix | |||||||
|         Logger() << msg; |         Logger() << msg; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     int getAnyFreePort() | ||||||
|  |     { | ||||||
|  |         int defaultPort = 8090; | ||||||
|  |  | ||||||
|  |         int sockfd; | ||||||
|  |         if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) | ||||||
|  |         { | ||||||
|  |             log("Cannot compute a free port. socket error."); | ||||||
|  |             return defaultPort; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         int enable = 1; | ||||||
|  |         if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, | ||||||
|  |                        (char*) &enable, sizeof(enable)) < 0) | ||||||
|  |         { | ||||||
|  |             log("Cannot compute a free port. setsockopt error."); | ||||||
|  |             return defaultPort; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         // Bind to port 0. This is the standard way to get a free port. | ||||||
|  |         struct sockaddr_in server; // server address information | ||||||
|  |         server.sin_family = AF_INET; | ||||||
|  |         server.sin_port   = htons(0); | ||||||
|  |         server.sin_addr.s_addr = inet_addr("127.0.0.1"); | ||||||
|  |  | ||||||
|  |         if (bind(sockfd, (struct sockaddr *)&server, sizeof(server)) < 0) | ||||||
|  |         { | ||||||
|  |             log("Cannot compute a free port. bind error."); | ||||||
|  |  | ||||||
|  |             ::close(sockfd); | ||||||
|  |             return defaultPort; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         struct sockaddr_in sa; // server address information | ||||||
|  |         unsigned int len; | ||||||
|  |         if (getsockname(sockfd, (struct sockaddr *) &sa, &len) < 0) | ||||||
|  |         { | ||||||
|  |             log("Cannot compute a free port. getsockname error."); | ||||||
|  |  | ||||||
|  |             ::close(sockfd); | ||||||
|  |             return defaultPort; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         int port = ntohs(sa.sin_port); | ||||||
|  |         ::close(sockfd); | ||||||
|  |  | ||||||
|  |         return port; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     int getFreePort() | ||||||
|  |     { | ||||||
|  |         while (true) | ||||||
|  |         { | ||||||
|  |             int port = getAnyFreePort(); | ||||||
|  |  | ||||||
|  |             // | ||||||
|  |             // Only port above 1024 can be used by non root users, but for some | ||||||
|  |             // reason I got port 7 returned with macOS when binding on port 0... | ||||||
|  |             // | ||||||
|  |             if (port > 1024) | ||||||
|  |             { | ||||||
|  |                 return port; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         return -1; | ||||||
|  |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -51,4 +51,7 @@ namespace ix | |||||||
|     }; |     }; | ||||||
|  |  | ||||||
|     void log(const std::string& msg); |     void log(const std::string& msg); | ||||||
|  |  | ||||||
|  |     bool computeFreePorts(int count); | ||||||
|  |     int getFreePort(); | ||||||
| } | } | ||||||
|   | |||||||
| @@ -180,7 +180,7 @@ TEST_CASE("Websocket_heartbeat", "[heartbeat]") | |||||||
|     { |     { | ||||||
|         ix::setupWebSocketTrafficTrackerCallback(); |         ix::setupWebSocketTrafficTrackerCallback(); | ||||||
|  |  | ||||||
|         int port = 8093; |         int port = getFreePort(); | ||||||
|         ix::WebSocketServer server(port); |         ix::WebSocketServer server(port); | ||||||
|         std::atomic<int> serverReceivedPingMessages(0); |         std::atomic<int> serverReceivedPingMessages(0); | ||||||
|         REQUIRE(startServer(server, serverReceivedPingMessages)); |         REQUIRE(startServer(server, serverReceivedPingMessages)); | ||||||
|   | |||||||
| @@ -75,7 +75,7 @@ TEST_CASE("Websocket_server", "[websocket_server]") | |||||||
| { | { | ||||||
|     SECTION("Connect to the server, do not send anything. Should timeout and return 400") |     SECTION("Connect to the server, do not send anything. Should timeout and return 400") | ||||||
|     { |     { | ||||||
|         int port = 8091; |         int port = getFreePort(); | ||||||
|         ix::WebSocketServer server(port); |         ix::WebSocketServer server(port); | ||||||
|         REQUIRE(startServer(server)); |         REQUIRE(startServer(server)); | ||||||
|  |  | ||||||
| @@ -107,7 +107,7 @@ TEST_CASE("Websocket_server", "[websocket_server]") | |||||||
|  |  | ||||||
|     SECTION("Connect to the server. Send GET request without header. Should return 400") |     SECTION("Connect to the server. Send GET request without header. Should return 400") | ||||||
|     { |     { | ||||||
|         int port = 8092; |         int port = getFreePort(); | ||||||
|         ix::WebSocketServer server(port); |         ix::WebSocketServer server(port); | ||||||
|         REQUIRE(startServer(server)); |         REQUIRE(startServer(server)); | ||||||
|  |  | ||||||
| @@ -142,7 +142,7 @@ TEST_CASE("Websocket_server", "[websocket_server]") | |||||||
|  |  | ||||||
|     SECTION("Connect to the server. Send GET request with correct header") |     SECTION("Connect to the server. Send GET request with correct header") | ||||||
|     { |     { | ||||||
|         int port = 8093; |         int port = getFreePort(); | ||||||
|         ix::WebSocketServer server(port); |         ix::WebSocketServer server(port); | ||||||
|         REQUIRE(startServer(server)); |         REQUIRE(startServer(server)); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -4,9 +4,15 @@ | |||||||
|  *  Copyright (c) 2017 Machine Zone. All rights reserved. |  *  Copyright (c) 2017 Machine Zone. All rights reserved. | ||||||
|  */ |  */ | ||||||
|  |  | ||||||
|  | // | ||||||
|  | // Simple chat program that talks to the node.js server at | ||||||
|  | // websocket_chat_server/broacast-server.js | ||||||
|  | // | ||||||
|  |  | ||||||
| #include <iostream> | #include <iostream> | ||||||
| #include <sstream> | #include <sstream> | ||||||
| #include <queue> | #include <vector> | ||||||
|  | #include <mutex> | ||||||
| #include <ixwebsocket/IXWebSocket.h> | #include <ixwebsocket/IXWebSocket.h> | ||||||
| #include <ixwebsocket/IXWebSocketServer.h> | #include <ixwebsocket/IXWebSocketServer.h> | ||||||
| #include "msgpack11.hpp" | #include "msgpack11.hpp" | ||||||
| @@ -24,7 +30,8 @@ namespace | |||||||
|     { |     { | ||||||
|         public: |         public: | ||||||
|             WebSocketChat(const std::string& user, |             WebSocketChat(const std::string& user, | ||||||
|                        const std::string& session); |                           const std::string& session, | ||||||
|  |                           int port); | ||||||
|  |  | ||||||
|             void subscribe(const std::string& channel); |             void subscribe(const std::string& channel); | ||||||
|             void start(); |             void start(); | ||||||
| @@ -33,30 +40,49 @@ namespace | |||||||
|  |  | ||||||
|             void sendMessage(const std::string& text); |             void sendMessage(const std::string& text); | ||||||
|             size_t getReceivedMessagesCount() const; |             size_t getReceivedMessagesCount() const; | ||||||
|  |             const std::vector<std::string>& getReceivedMessages() const; | ||||||
|  |  | ||||||
|             std::string encodeMessage(const std::string& text); |             std::string encodeMessage(const std::string& text); | ||||||
|             std::pair<std::string, std::string> decodeMessage(const std::string& str); |             std::pair<std::string, std::string> decodeMessage(const std::string& str); | ||||||
|  |             void appendMessage(const std::string& message); | ||||||
|  |  | ||||||
|         private: |         private: | ||||||
|             std::string _user; |             std::string _user; | ||||||
|             std::string _session; |             std::string _session; | ||||||
|  |             int _port; | ||||||
|  |  | ||||||
|             ix::WebSocket _webSocket; |             ix::WebSocket _webSocket; | ||||||
|  |  | ||||||
|             std::queue<std::string> _receivedQueue; |             std::vector<std::string> _receivedMessages; | ||||||
|  |             mutable std::mutex _mutex; | ||||||
|     }; |     }; | ||||||
|  |  | ||||||
|     WebSocketChat::WebSocketChat(const std::string& user, |     WebSocketChat::WebSocketChat(const std::string& user, | ||||||
|                                  const std::string& session) : |                                  const std::string& session, | ||||||
|  |                                  int port) : | ||||||
|         _user(user), |         _user(user), | ||||||
|         _session(session) |         _session(session), | ||||||
|  |         _port(port) | ||||||
|     { |     { | ||||||
|         ; |         ; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     size_t WebSocketChat::getReceivedMessagesCount() const |     size_t WebSocketChat::getReceivedMessagesCount() const | ||||||
|     { |     { | ||||||
|         return _receivedQueue.size(); |         std::lock_guard<std::mutex> lock(_mutex); | ||||||
|  |         return _receivedMessages.size(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     const std::vector<std::string>& WebSocketChat::getReceivedMessages() const | ||||||
|  |     { | ||||||
|  |         std::lock_guard<std::mutex> lock(_mutex); | ||||||
|  |         return _receivedMessages; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     void WebSocketChat::appendMessage(const std::string& message) | ||||||
|  |     { | ||||||
|  |         std::lock_guard<std::mutex> lock(_mutex); | ||||||
|  |         _receivedMessages.push_back(message); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     bool WebSocketChat::isReady() const |     bool WebSocketChat::isReady() const | ||||||
| @@ -71,7 +97,17 @@ namespace | |||||||
|  |  | ||||||
|     void WebSocketChat::start() |     void WebSocketChat::start() | ||||||
|     { |     { | ||||||
|         std::string url("ws://localhost:8090/"); |         std::string url; | ||||||
|  |         { | ||||||
|  |             std::stringstream ss; | ||||||
|  |             ss << "ws://localhost:" | ||||||
|  |                << _port | ||||||
|  |                << "/" | ||||||
|  |                << _user; | ||||||
|  |  | ||||||
|  |             url = ss.str(); | ||||||
|  |         } | ||||||
|  |  | ||||||
|         _webSocket.setUrl(url); |         _webSocket.setUrl(url); | ||||||
|  |  | ||||||
|         std::stringstream ss; |         std::stringstream ss; | ||||||
| @@ -109,10 +145,16 @@ namespace | |||||||
|                     // as we do for the satori chat example. |                     // as we do for the satori chat example. | ||||||
|  |  | ||||||
|                     // store text |                     // store text | ||||||
|                     _receivedQueue.push(result.second); |                     appendMessage(result.second); | ||||||
|  |  | ||||||
|  |                     std::string payload = result.second; | ||||||
|  |                     if (payload.size() > 2000) | ||||||
|  |                     { | ||||||
|  |                         payload = "<message too large>"; | ||||||
|  |                     } | ||||||
|  |  | ||||||
|                     ss << std::endl |                     ss << std::endl | ||||||
|                        << result.first << " > " << result.second |                        << result.first << " > " << payload | ||||||
|                        << std::endl |                        << std::endl | ||||||
|                        << _user << " > "; |                        << _user << " > "; | ||||||
|                     log(ss.str()); |                     log(ss.str()); | ||||||
| @@ -226,8 +268,8 @@ TEST_CASE("Websocket_chat", "[websocket_chat]") | |||||||
|         REQUIRE(startServer(server)); |         REQUIRE(startServer(server)); | ||||||
|  |  | ||||||
|         std::string session = ix::generateSessionId(); |         std::string session = ix::generateSessionId(); | ||||||
|         WebSocketChat chatA("jean", session); |         WebSocketChat chatA("jean", session, port); | ||||||
|         WebSocketChat chatB("paul", session); |         WebSocketChat chatB("paul", session, port); | ||||||
|  |  | ||||||
|         chatA.start(); |         chatA.start(); | ||||||
|         chatB.start(); |         chatB.start(); | ||||||
| @@ -251,15 +293,36 @@ TEST_CASE("Websocket_chat", "[websocket_chat]") | |||||||
|         chatB.sendMessage("from B1"); |         chatB.sendMessage("from B1"); | ||||||
|         chatB.sendMessage("from B2"); |         chatB.sendMessage("from B2"); | ||||||
|  |  | ||||||
|         // Give us 1s for all messages to be received |         // Test large messages that needs to be broken into small fragments | ||||||
|  |         size_t size = 1 * 1024 * 1024; // ~1Mb | ||||||
|  |         std::string bigMessage(size, 'a'); | ||||||
|  |         chatB.sendMessage(bigMessage); | ||||||
|  |  | ||||||
|  |         log("Sent all messages"); | ||||||
|  |  | ||||||
|  |         // Wait until all messages are received. 10s timeout | ||||||
|  |         int attempts = 0; | ||||||
|  |         while (chatA.getReceivedMessagesCount() != 3 || | ||||||
|  |                chatB.getReceivedMessagesCount() != 3) | ||||||
|  |         { | ||||||
|  |             REQUIRE(attempts++ < 10); | ||||||
|             ix::msleep(1000); |             ix::msleep(1000); | ||||||
|  |         } | ||||||
|  |  | ||||||
|         chatA.stop(); |         chatA.stop(); | ||||||
|         chatB.stop(); |         chatB.stop(); | ||||||
|  |  | ||||||
|         REQUIRE(chatA.getReceivedMessagesCount() == 2); |         REQUIRE(chatA.getReceivedMessagesCount() == 3); | ||||||
|         REQUIRE(chatB.getReceivedMessagesCount() == 3); |         REQUIRE(chatB.getReceivedMessagesCount() == 3); | ||||||
|  |  | ||||||
|  |         REQUIRE(chatB.getReceivedMessages()[0] == "from A1"); | ||||||
|  |         REQUIRE(chatB.getReceivedMessages()[1] == "from A2"); | ||||||
|  |         REQUIRE(chatB.getReceivedMessages()[2] == "from A3"); | ||||||
|  |  | ||||||
|  |         REQUIRE(chatA.getReceivedMessages()[0] == "from B1"); | ||||||
|  |         REQUIRE(chatA.getReceivedMessages()[1] == "from B2"); | ||||||
|  |         REQUIRE(chatA.getReceivedMessages()[2].size() == bigMessage.size()); | ||||||
|  |  | ||||||
|         // Give us 500ms for the server to notice that clients went away |         // Give us 500ms for the server to notice that clients went away | ||||||
|         ix::msleep(500); |         ix::msleep(500); | ||||||
|         REQUIRE(server.getClients().size() == 0); |         REQUIRE(server.getClients().size() == 0); | ||||||
|   | |||||||
| @@ -34,6 +34,10 @@ if osName == 'Linux': | |||||||
|  |  | ||||||
| sanitizerFlags = sanitizersFlags[sanitizer] | sanitizerFlags = sanitizersFlags[sanitizer] | ||||||
|  |  | ||||||
|  | # if osName == 'Windows': | ||||||
|  | #     os.environ['CC'] = 'clang-cl' | ||||||
|  | #     os.environ['CXX'] = 'clang-cl' | ||||||
|  |  | ||||||
| cmakeCmd = 'cmake -DCMAKE_BUILD_TYPE=Debug {} {} ..'.format(generator, sanitizerFlags) | cmakeCmd = 'cmake -DCMAKE_BUILD_TYPE=Debug {} {} ..'.format(generator, sanitizerFlags) | ||||||
| print(cmakeCmd) | print(cmakeCmd) | ||||||
| ret = os.system(cmakeCmd) | ret = os.system(cmakeCmd) | ||||||
|   | |||||||
							
								
								
									
										4641
									
								
								third_party/cli11/CLI11.hpp
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										4641
									
								
								third_party/cli11/CLI11.hpp
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										333
									
								
								third_party/jsoncpp/json/json-forwards.h
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										333
									
								
								third_party/jsoncpp/json/json-forwards.h
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,333 @@ | |||||||
|  | /// Json-cpp amalgated forward header (http://jsoncpp.sourceforge.net/). | ||||||
|  | /// It is intended to be used with #include "json/json-forwards.h" | ||||||
|  | /// This header provides forward declaration for all JsonCpp types. | ||||||
|  |  | ||||||
|  | // ////////////////////////////////////////////////////////////////////// | ||||||
|  | // Beginning of content of file: LICENSE | ||||||
|  | // ////////////////////////////////////////////////////////////////////// | ||||||
|  |  | ||||||
|  | /* | ||||||
|  | The JsonCpp library's source code, including accompanying documentation, | ||||||
|  | tests and demonstration applications, are licensed under the following | ||||||
|  | conditions... | ||||||
|  |  | ||||||
|  | Baptiste Lepilleur and The JsonCpp Authors explicitly disclaim copyright in all | ||||||
|  | jurisdictions which recognize such a disclaimer. In such jurisdictions, | ||||||
|  | this software is released into the Public Domain. | ||||||
|  |  | ||||||
|  | In jurisdictions which do not recognize Public Domain property (e.g. Germany as of | ||||||
|  | 2010), this software is Copyright (c) 2007-2010 by Baptiste Lepilleur and | ||||||
|  | The JsonCpp Authors, and is released under the terms of the MIT License (see below). | ||||||
|  |  | ||||||
|  | In jurisdictions which recognize Public Domain property, the user of this | ||||||
|  | software may choose to accept it either as 1) Public Domain, 2) under the | ||||||
|  | conditions of the MIT License (see below), or 3) under the terms of dual | ||||||
|  | Public Domain/MIT License conditions described here, as they choose. | ||||||
|  |  | ||||||
|  | The MIT License is about as close to Public Domain as a license can get, and is | ||||||
|  | described in clear, concise terms at: | ||||||
|  |  | ||||||
|  |    http://en.wikipedia.org/wiki/MIT_License | ||||||
|  |  | ||||||
|  | The full text of the MIT License follows: | ||||||
|  |  | ||||||
|  | ======================================================================== | ||||||
|  | Copyright (c) 2007-2010 Baptiste Lepilleur and The JsonCpp Authors | ||||||
|  |  | ||||||
|  | Permission is hereby granted, free of charge, to any person | ||||||
|  | obtaining a copy of this software and associated documentation | ||||||
|  | files (the "Software"), to deal in the Software without | ||||||
|  | restriction, including without limitation the rights to use, copy, | ||||||
|  | modify, merge, publish, distribute, sublicense, and/or sell copies | ||||||
|  | of the Software, and to permit persons to whom the Software is | ||||||
|  | furnished to do so, subject to the following conditions: | ||||||
|  |  | ||||||
|  | The above copyright notice and this permission notice shall be | ||||||
|  | included in all copies or substantial portions of the Software. | ||||||
|  |  | ||||||
|  | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||||||
|  | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||||||
|  | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||||||
|  | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | ||||||
|  | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | ||||||
|  | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||||||
|  | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||||||
|  | SOFTWARE. | ||||||
|  | ======================================================================== | ||||||
|  | (END LICENSE TEXT) | ||||||
|  |  | ||||||
|  | The MIT license is compatible with both the GPL and commercial | ||||||
|  | software, affording one all of the rights of Public Domain with the | ||||||
|  | minor nuisance of being required to keep the above copyright notice | ||||||
|  | and license text in the source code. Note also that by accepting the | ||||||
|  | Public Domain "license" you can re-license your copy using whatever | ||||||
|  | license you like. | ||||||
|  |  | ||||||
|  | */ | ||||||
|  |  | ||||||
|  | // ////////////////////////////////////////////////////////////////////// | ||||||
|  | // End of content of file: LICENSE | ||||||
|  | // ////////////////////////////////////////////////////////////////////// | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  | #ifndef JSON_FORWARD_AMALGATED_H_INCLUDED | ||||||
|  | # define JSON_FORWARD_AMALGATED_H_INCLUDED | ||||||
|  | /// If defined, indicates that the source file is amalgated | ||||||
|  | /// to prevent private header inclusion. | ||||||
|  | #define JSON_IS_AMALGAMATION | ||||||
|  |  | ||||||
|  | // ////////////////////////////////////////////////////////////////////// | ||||||
|  | // Beginning of content of file: include/json/config.h | ||||||
|  | // ////////////////////////////////////////////////////////////////////// | ||||||
|  |  | ||||||
|  | // Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors | ||||||
|  | // Distributed under MIT license, or public domain if desired and | ||||||
|  | // recognized in your jurisdiction. | ||||||
|  | // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE | ||||||
|  |  | ||||||
|  | #ifndef JSON_CONFIG_H_INCLUDED | ||||||
|  | #define JSON_CONFIG_H_INCLUDED | ||||||
|  | #include <stddef.h> | ||||||
|  | #include <string> //typedef String | ||||||
|  | #include <stdint.h> //typedef int64_t, uint64_t | ||||||
|  |  | ||||||
|  | /// If defined, indicates that json library is embedded in CppTL library. | ||||||
|  | //# define JSON_IN_CPPTL 1 | ||||||
|  |  | ||||||
|  | /// If defined, indicates that json may leverage CppTL library | ||||||
|  | //#  define JSON_USE_CPPTL 1 | ||||||
|  | /// If defined, indicates that cpptl vector based map should be used instead of | ||||||
|  | /// std::map | ||||||
|  | /// as Value container. | ||||||
|  | //#  define JSON_USE_CPPTL_SMALLMAP 1 | ||||||
|  |  | ||||||
|  | // If non-zero, the library uses exceptions to report bad input instead of C | ||||||
|  | // assertion macros. The default is to use exceptions. | ||||||
|  | #ifndef JSON_USE_EXCEPTION | ||||||
|  | #define JSON_USE_EXCEPTION 1 | ||||||
|  | #endif | ||||||
|  |  | ||||||
|  | /// If defined, indicates that the source file is amalgated | ||||||
|  | /// to prevent private header inclusion. | ||||||
|  | /// Remarks: it is automatically defined in the generated amalgated header. | ||||||
|  | // #define JSON_IS_AMALGAMATION | ||||||
|  |  | ||||||
|  | #ifdef JSON_IN_CPPTL | ||||||
|  | #include <cpptl/config.h> | ||||||
|  | #ifndef JSON_USE_CPPTL | ||||||
|  | #define JSON_USE_CPPTL 1 | ||||||
|  | #endif | ||||||
|  | #endif | ||||||
|  |  | ||||||
|  | #ifdef JSON_IN_CPPTL | ||||||
|  | #define JSON_API CPPTL_API | ||||||
|  | #elif defined(JSON_DLL_BUILD) | ||||||
|  | #if defined(_MSC_VER) || defined(__MINGW32__) | ||||||
|  | #define JSON_API __declspec(dllexport) | ||||||
|  | #define JSONCPP_DISABLE_DLL_INTERFACE_WARNING | ||||||
|  | #endif // if defined(_MSC_VER) | ||||||
|  | #elif defined(JSON_DLL) | ||||||
|  | #if defined(_MSC_VER) || defined(__MINGW32__) | ||||||
|  | #define JSON_API __declspec(dllimport) | ||||||
|  | #define JSONCPP_DISABLE_DLL_INTERFACE_WARNING | ||||||
|  | #endif // if defined(_MSC_VER) | ||||||
|  | #endif // ifdef JSON_IN_CPPTL | ||||||
|  | #if !defined(JSON_API) | ||||||
|  | #define JSON_API | ||||||
|  | #endif | ||||||
|  |  | ||||||
|  | // If JSON_NO_INT64 is defined, then Json only support C++ "int" type for | ||||||
|  | // integer | ||||||
|  | // Storages, and 64 bits integer support is disabled. | ||||||
|  | // #define JSON_NO_INT64 1 | ||||||
|  |  | ||||||
|  | #if defined(_MSC_VER) // MSVC | ||||||
|  | #  if _MSC_VER <= 1200 // MSVC 6 | ||||||
|  |     // Microsoft Visual Studio 6 only support conversion from __int64 to double | ||||||
|  |     // (no conversion from unsigned __int64). | ||||||
|  | #    define JSON_USE_INT64_DOUBLE_CONVERSION 1 | ||||||
|  |     // Disable warning 4786 for VS6 caused by STL (identifier was truncated to '255' | ||||||
|  |     // characters in the debug information) | ||||||
|  |     // All projects I've ever seen with VS6 were using this globally (not bothering | ||||||
|  |     // with pragma push/pop). | ||||||
|  | #    pragma warning(disable : 4786) | ||||||
|  | #  endif // MSVC 6 | ||||||
|  |  | ||||||
|  | #  if _MSC_VER >= 1500 // MSVC 2008 | ||||||
|  |     /// Indicates that the following function is deprecated. | ||||||
|  | #    define JSONCPP_DEPRECATED(message) __declspec(deprecated(message)) | ||||||
|  | #  endif | ||||||
|  |  | ||||||
|  | #endif // defined(_MSC_VER) | ||||||
|  |  | ||||||
|  | // In c++11 the override keyword allows you to explicity define that a function | ||||||
|  | // is intended to override the base-class version.  This makes the code more | ||||||
|  | // managable and fixes a set of common hard-to-find bugs. | ||||||
|  | #if __cplusplus >= 201103L | ||||||
|  | # define JSONCPP_OVERRIDE override | ||||||
|  | # define JSONCPP_NOEXCEPT noexcept | ||||||
|  | #elif defined(_MSC_VER) && _MSC_VER > 1600 && _MSC_VER < 1900 | ||||||
|  | # define JSONCPP_OVERRIDE override | ||||||
|  | # define JSONCPP_NOEXCEPT throw() | ||||||
|  | #elif defined(_MSC_VER) && _MSC_VER >= 1900 | ||||||
|  | # define JSONCPP_OVERRIDE override | ||||||
|  | # define JSONCPP_NOEXCEPT noexcept | ||||||
|  | #else | ||||||
|  | # define JSONCPP_OVERRIDE | ||||||
|  | # define JSONCPP_NOEXCEPT throw() | ||||||
|  | #endif | ||||||
|  |  | ||||||
|  | #ifndef JSON_HAS_RVALUE_REFERENCES | ||||||
|  |  | ||||||
|  | #if defined(_MSC_VER) && _MSC_VER >= 1600 // MSVC >= 2010 | ||||||
|  | #define JSON_HAS_RVALUE_REFERENCES 1 | ||||||
|  | #endif // MSVC >= 2010 | ||||||
|  |  | ||||||
|  | #ifdef __clang__ | ||||||
|  | #if __has_feature(cxx_rvalue_references) | ||||||
|  | #define JSON_HAS_RVALUE_REFERENCES 1 | ||||||
|  | #endif  // has_feature | ||||||
|  |  | ||||||
|  | #elif defined __GNUC__ // not clang (gcc comes later since clang emulates gcc) | ||||||
|  | #if defined(__GXX_EXPERIMENTAL_CXX0X__) || (__cplusplus >= 201103L) | ||||||
|  | #define JSON_HAS_RVALUE_REFERENCES 1 | ||||||
|  | #endif  // GXX_EXPERIMENTAL | ||||||
|  |  | ||||||
|  | #endif // __clang__ || __GNUC__ | ||||||
|  |  | ||||||
|  | #endif // not defined JSON_HAS_RVALUE_REFERENCES | ||||||
|  |  | ||||||
|  | #ifndef JSON_HAS_RVALUE_REFERENCES | ||||||
|  | #define JSON_HAS_RVALUE_REFERENCES 0 | ||||||
|  | #endif | ||||||
|  |  | ||||||
|  | #ifdef __clang__ | ||||||
|  | #  if __has_extension(attribute_deprecated_with_message) | ||||||
|  | #    define JSONCPP_DEPRECATED(message)  __attribute__ ((deprecated(message))) | ||||||
|  | #  endif | ||||||
|  | #elif defined __GNUC__ // not clang (gcc comes later since clang emulates gcc) | ||||||
|  | #  if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)) | ||||||
|  | #    define JSONCPP_DEPRECATED(message)  __attribute__ ((deprecated(message))) | ||||||
|  | #  elif (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)) | ||||||
|  | #    define JSONCPP_DEPRECATED(message)  __attribute__((__deprecated__)) | ||||||
|  | #  endif  // GNUC version | ||||||
|  | #endif // __clang__ || __GNUC__ | ||||||
|  |  | ||||||
|  | #if !defined(JSONCPP_DEPRECATED) | ||||||
|  | #define JSONCPP_DEPRECATED(message) | ||||||
|  | #endif // if !defined(JSONCPP_DEPRECATED) | ||||||
|  |  | ||||||
|  | #if __GNUC__ >= 6 | ||||||
|  | #  define JSON_USE_INT64_DOUBLE_CONVERSION 1 | ||||||
|  | #endif | ||||||
|  |  | ||||||
|  | #if !defined(JSON_IS_AMALGAMATION) | ||||||
|  |  | ||||||
|  | # include "version.h" | ||||||
|  |  | ||||||
|  | # if JSONCPP_USING_SECURE_MEMORY | ||||||
|  | #  include "allocator.h" //typedef Allocator | ||||||
|  | # endif | ||||||
|  |  | ||||||
|  | #endif // if !defined(JSON_IS_AMALGAMATION) | ||||||
|  |  | ||||||
|  | namespace Json { | ||||||
|  | typedef int Int; | ||||||
|  | typedef unsigned int UInt; | ||||||
|  | #if defined(JSON_NO_INT64) | ||||||
|  | typedef int LargestInt; | ||||||
|  | typedef unsigned int LargestUInt; | ||||||
|  | #undef JSON_HAS_INT64 | ||||||
|  | #else                 // if defined(JSON_NO_INT64) | ||||||
|  | // For Microsoft Visual use specific types as long long is not supported | ||||||
|  | #if defined(_MSC_VER) // Microsoft Visual Studio | ||||||
|  | typedef __int64 Int64; | ||||||
|  | typedef unsigned __int64 UInt64; | ||||||
|  | #else                 // if defined(_MSC_VER) // Other platforms, use long long | ||||||
|  | typedef int64_t Int64; | ||||||
|  | typedef uint64_t UInt64; | ||||||
|  | #endif // if defined(_MSC_VER) | ||||||
|  | typedef Int64 LargestInt; | ||||||
|  | typedef UInt64 LargestUInt; | ||||||
|  | #define JSON_HAS_INT64 | ||||||
|  | #endif // if defined(JSON_NO_INT64) | ||||||
|  | #if JSONCPP_USING_SECURE_MEMORY | ||||||
|  | #define JSONCPP_STRING        std::basic_string<char, std::char_traits<char>, Json::SecureAllocator<char> > | ||||||
|  | #define JSONCPP_OSTRINGSTREAM std::basic_ostringstream<char, std::char_traits<char>, Json::SecureAllocator<char> > | ||||||
|  | #define JSONCPP_OSTREAM       std::basic_ostream<char, std::char_traits<char>> | ||||||
|  | #define JSONCPP_ISTRINGSTREAM std::basic_istringstream<char, std::char_traits<char>, Json::SecureAllocator<char> > | ||||||
|  | #define JSONCPP_ISTREAM       std::istream | ||||||
|  | #else | ||||||
|  | #define JSONCPP_STRING        std::string | ||||||
|  | #define JSONCPP_OSTRINGSTREAM std::ostringstream | ||||||
|  | #define JSONCPP_OSTREAM       std::ostream | ||||||
|  | #define JSONCPP_ISTRINGSTREAM std::istringstream | ||||||
|  | #define JSONCPP_ISTREAM       std::istream | ||||||
|  | #endif // if JSONCPP_USING_SECURE_MEMORY | ||||||
|  | } // end namespace Json | ||||||
|  |  | ||||||
|  | #endif // JSON_CONFIG_H_INCLUDED | ||||||
|  |  | ||||||
|  | // ////////////////////////////////////////////////////////////////////// | ||||||
|  | // End of content of file: include/json/config.h | ||||||
|  | // ////////////////////////////////////////////////////////////////////// | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  | // ////////////////////////////////////////////////////////////////////// | ||||||
|  | // Beginning of content of file: include/json/forwards.h | ||||||
|  | // ////////////////////////////////////////////////////////////////////// | ||||||
|  |  | ||||||
|  | // Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors | ||||||
|  | // Distributed under MIT license, or public domain if desired and | ||||||
|  | // recognized in your jurisdiction. | ||||||
|  | // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE | ||||||
|  |  | ||||||
|  | #ifndef JSON_FORWARDS_H_INCLUDED | ||||||
|  | #define JSON_FORWARDS_H_INCLUDED | ||||||
|  |  | ||||||
|  | #if !defined(JSON_IS_AMALGAMATION) | ||||||
|  | #include "config.h" | ||||||
|  | #endif // if !defined(JSON_IS_AMALGAMATION) | ||||||
|  |  | ||||||
|  | namespace Json { | ||||||
|  |  | ||||||
|  | // writer.h | ||||||
|  | class FastWriter; | ||||||
|  | class StyledWriter; | ||||||
|  |  | ||||||
|  | // reader.h | ||||||
|  | class Reader; | ||||||
|  |  | ||||||
|  | // features.h | ||||||
|  | class Features; | ||||||
|  |  | ||||||
|  | // value.h | ||||||
|  | typedef unsigned int ArrayIndex; | ||||||
|  | class StaticString; | ||||||
|  | class Path; | ||||||
|  | class PathArgument; | ||||||
|  | class Value; | ||||||
|  | class ValueIteratorBase; | ||||||
|  | class ValueIterator; | ||||||
|  | class ValueConstIterator; | ||||||
|  |  | ||||||
|  | } // namespace Json | ||||||
|  |  | ||||||
|  | #endif // JSON_FORWARDS_H_INCLUDED | ||||||
|  |  | ||||||
|  | // ////////////////////////////////////////////////////////////////////// | ||||||
|  | // End of content of file: include/json/forwards.h | ||||||
|  | // ////////////////////////////////////////////////////////////////////// | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  | #endif //ifndef JSON_FORWARD_AMALGATED_H_INCLUDED | ||||||
							
								
								
									
										2186
									
								
								third_party/jsoncpp/json/json.h
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										2186
									
								
								third_party/jsoncpp/json/json.h
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										5386
									
								
								third_party/jsoncpp/jsoncpp.cpp
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										5386
									
								
								third_party/jsoncpp/jsoncpp.cpp
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										2
									
								
								third_party/remove_trailing_whitespaces.sh
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								third_party/remove_trailing_whitespaces.sh
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,2 @@ | |||||||
|  | find . -type f -name '*.cpp' -exec sed -i '' 's/[[:space:]]*$//' {} \+ | ||||||
|  | find . -type f -name '*.h' -exec sed -i '' 's/[[:space:]]*$//' {} \+ | ||||||
							
								
								
									
										1
									
								
								ws/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								ws/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | |||||||
|  | build | ||||||
							
								
								
									
										42
									
								
								ws/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								ws/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,42 @@ | |||||||
|  | # | ||||||
|  | # Author: Benjamin Sergeant | ||||||
|  | # Copyright (c) 2019 Machine Zone, Inc. All rights reserved. | ||||||
|  | # | ||||||
|  |  | ||||||
|  | cmake_minimum_required (VERSION 3.4.1) | ||||||
|  | project (ws) | ||||||
|  |  | ||||||
|  | # There's -Weverything too for clang | ||||||
|  | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic") | ||||||
|  |  | ||||||
|  | set (CMAKE_CXX_STANDARD 14) | ||||||
|  |  | ||||||
|  | option(USE_TLS "Add TLS support" ON) | ||||||
|  |  | ||||||
|  | include_directories(ws .) | ||||||
|  | include_directories(ws ..) | ||||||
|  | include_directories(ws ../third_party) | ||||||
|  |  | ||||||
|  | add_executable(ws  | ||||||
|  |   ../third_party/msgpack11/msgpack11.cpp | ||||||
|  |   ixcrypto/IXBase64.cpp | ||||||
|  |   ixcrypto/IXHash.cpp | ||||||
|  |   ixcrypto/IXUuid.cpp | ||||||
|  |  | ||||||
|  |   ws_http_client.cpp | ||||||
|  |   ws_ping_pong.cpp | ||||||
|  |   ws_broadcast_server.cpp | ||||||
|  |   ws_echo_server.cpp | ||||||
|  |   ws_chat.cpp | ||||||
|  |   ws_connect.cpp | ||||||
|  |   ws_transfer.cpp | ||||||
|  |   ws_send.cpp | ||||||
|  |   ws_receive.cpp | ||||||
|  |   ws.cpp) | ||||||
|  |  | ||||||
|  | if (APPLE AND USE_TLS) | ||||||
|  |     target_link_libraries(ws "-framework foundation" "-framework security") | ||||||
|  | endif() | ||||||
|  |  | ||||||
|  | target_link_libraries(ws ixwebsocket) | ||||||
|  | install(TARGETS ws RUNTIME DESTINATION bin) | ||||||
							
								
								
									
										10
									
								
								ws/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								ws/README.md
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,10 @@ | |||||||
|  | ``` | ||||||
|  | # Start transfer server, which is just a broadcast server at this point | ||||||
|  | ./ws transfer # running on port 8080. | ||||||
|  |  | ||||||
|  | # Start receiver first | ||||||
|  | ./ws receive ws://localhost:8080  | ||||||
|  |  | ||||||
|  | # Then send a file. File will be received and written to disk by the receiver process | ||||||
|  | ./ws send ws://localhost:8080 /file/to/path | ||||||
|  | ``` | ||||||
| @@ -15,8 +15,6 @@ set (CMAKE_CXX_STANDARD 14) | |||||||
| 
 | 
 | ||||||
| option(USE_TLS "Add TLS support" ON) | option(USE_TLS "Add TLS support" ON) | ||||||
| 
 | 
 | ||||||
| add_subdirectory(${PROJECT_SOURCE_DIR}/../.. ixwebsocket) |  | ||||||
| 
 |  | ||||||
| include_directories(cobra_publisher ${OPENSSL_PREFIX}/include) | include_directories(cobra_publisher ${OPENSSL_PREFIX}/include) | ||||||
| include_directories(cobra_publisher .) | include_directories(cobra_publisher .) | ||||||
| 
 | 
 | ||||||
							
								
								
									
										333
									
								
								ws/cobra_publisher/jsoncpp/json/json-forwards.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										333
									
								
								ws/cobra_publisher/jsoncpp/json/json-forwards.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,333 @@ | |||||||
|  | /// Json-cpp amalgated forward header (http://jsoncpp.sourceforge.net/). | ||||||
|  | /// It is intended to be used with #include "json/json-forwards.h" | ||||||
|  | /// This header provides forward declaration for all JsonCpp types. | ||||||
|  |  | ||||||
|  | // ////////////////////////////////////////////////////////////////////// | ||||||
|  | // Beginning of content of file: LICENSE | ||||||
|  | // ////////////////////////////////////////////////////////////////////// | ||||||
|  |  | ||||||
|  | /* | ||||||
|  | The JsonCpp library's source code, including accompanying documentation, | ||||||
|  | tests and demonstration applications, are licensed under the following | ||||||
|  | conditions... | ||||||
|  |  | ||||||
|  | Baptiste Lepilleur and The JsonCpp Authors explicitly disclaim copyright in all | ||||||
|  | jurisdictions which recognize such a disclaimer. In such jurisdictions, | ||||||
|  | this software is released into the Public Domain. | ||||||
|  |  | ||||||
|  | In jurisdictions which do not recognize Public Domain property (e.g. Germany as of | ||||||
|  | 2010), this software is Copyright (c) 2007-2010 by Baptiste Lepilleur and | ||||||
|  | The JsonCpp Authors, and is released under the terms of the MIT License (see below). | ||||||
|  |  | ||||||
|  | In jurisdictions which recognize Public Domain property, the user of this | ||||||
|  | software may choose to accept it either as 1) Public Domain, 2) under the | ||||||
|  | conditions of the MIT License (see below), or 3) under the terms of dual | ||||||
|  | Public Domain/MIT License conditions described here, as they choose. | ||||||
|  |  | ||||||
|  | The MIT License is about as close to Public Domain as a license can get, and is | ||||||
|  | described in clear, concise terms at: | ||||||
|  |  | ||||||
|  |    http://en.wikipedia.org/wiki/MIT_License | ||||||
|  |  | ||||||
|  | The full text of the MIT License follows: | ||||||
|  |  | ||||||
|  | ======================================================================== | ||||||
|  | Copyright (c) 2007-2010 Baptiste Lepilleur and The JsonCpp Authors | ||||||
|  |  | ||||||
|  | Permission is hereby granted, free of charge, to any person | ||||||
|  | obtaining a copy of this software and associated documentation | ||||||
|  | files (the "Software"), to deal in the Software without | ||||||
|  | restriction, including without limitation the rights to use, copy, | ||||||
|  | modify, merge, publish, distribute, sublicense, and/or sell copies | ||||||
|  | of the Software, and to permit persons to whom the Software is | ||||||
|  | furnished to do so, subject to the following conditions: | ||||||
|  |  | ||||||
|  | The above copyright notice and this permission notice shall be | ||||||
|  | included in all copies or substantial portions of the Software. | ||||||
|  |  | ||||||
|  | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||||||
|  | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||||||
|  | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||||||
|  | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | ||||||
|  | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | ||||||
|  | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||||||
|  | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||||||
|  | SOFTWARE. | ||||||
|  | ======================================================================== | ||||||
|  | (END LICENSE TEXT) | ||||||
|  |  | ||||||
|  | The MIT license is compatible with both the GPL and commercial | ||||||
|  | software, affording one all of the rights of Public Domain with the | ||||||
|  | minor nuisance of being required to keep the above copyright notice | ||||||
|  | and license text in the source code. Note also that by accepting the | ||||||
|  | Public Domain "license" you can re-license your copy using whatever | ||||||
|  | license you like. | ||||||
|  |  | ||||||
|  | */ | ||||||
|  |  | ||||||
|  | // ////////////////////////////////////////////////////////////////////// | ||||||
|  | // End of content of file: LICENSE | ||||||
|  | // ////////////////////////////////////////////////////////////////////// | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  | #ifndef JSON_FORWARD_AMALGATED_H_INCLUDED | ||||||
|  | # define JSON_FORWARD_AMALGATED_H_INCLUDED | ||||||
|  | /// If defined, indicates that the source file is amalgated | ||||||
|  | /// to prevent private header inclusion. | ||||||
|  | #define JSON_IS_AMALGAMATION | ||||||
|  |  | ||||||
|  | // ////////////////////////////////////////////////////////////////////// | ||||||
|  | // Beginning of content of file: include/json/config.h | ||||||
|  | // ////////////////////////////////////////////////////////////////////// | ||||||
|  |  | ||||||
|  | // Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors | ||||||
|  | // Distributed under MIT license, or public domain if desired and | ||||||
|  | // recognized in your jurisdiction. | ||||||
|  | // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE | ||||||
|  |  | ||||||
|  | #ifndef JSON_CONFIG_H_INCLUDED | ||||||
|  | #define JSON_CONFIG_H_INCLUDED | ||||||
|  | #include <stddef.h> | ||||||
|  | #include <string> //typedef String | ||||||
|  | #include <stdint.h> //typedef int64_t, uint64_t | ||||||
|  |  | ||||||
|  | /// If defined, indicates that json library is embedded in CppTL library. | ||||||
|  | //# define JSON_IN_CPPTL 1 | ||||||
|  |  | ||||||
|  | /// If defined, indicates that json may leverage CppTL library | ||||||
|  | //#  define JSON_USE_CPPTL 1 | ||||||
|  | /// If defined, indicates that cpptl vector based map should be used instead of | ||||||
|  | /// std::map | ||||||
|  | /// as Value container. | ||||||
|  | //#  define JSON_USE_CPPTL_SMALLMAP 1 | ||||||
|  |  | ||||||
|  | // If non-zero, the library uses exceptions to report bad input instead of C | ||||||
|  | // assertion macros. The default is to use exceptions. | ||||||
|  | #ifndef JSON_USE_EXCEPTION | ||||||
|  | #define JSON_USE_EXCEPTION 1 | ||||||
|  | #endif | ||||||
|  |  | ||||||
|  | /// If defined, indicates that the source file is amalgated | ||||||
|  | /// to prevent private header inclusion. | ||||||
|  | /// Remarks: it is automatically defined in the generated amalgated header. | ||||||
|  | // #define JSON_IS_AMALGAMATION | ||||||
|  |  | ||||||
|  | #ifdef JSON_IN_CPPTL | ||||||
|  | #include <cpptl/config.h> | ||||||
|  | #ifndef JSON_USE_CPPTL | ||||||
|  | #define JSON_USE_CPPTL 1 | ||||||
|  | #endif | ||||||
|  | #endif | ||||||
|  |  | ||||||
|  | #ifdef JSON_IN_CPPTL | ||||||
|  | #define JSON_API CPPTL_API | ||||||
|  | #elif defined(JSON_DLL_BUILD) | ||||||
|  | #if defined(_MSC_VER) || defined(__MINGW32__) | ||||||
|  | #define JSON_API __declspec(dllexport) | ||||||
|  | #define JSONCPP_DISABLE_DLL_INTERFACE_WARNING | ||||||
|  | #endif // if defined(_MSC_VER) | ||||||
|  | #elif defined(JSON_DLL) | ||||||
|  | #if defined(_MSC_VER) || defined(__MINGW32__) | ||||||
|  | #define JSON_API __declspec(dllimport) | ||||||
|  | #define JSONCPP_DISABLE_DLL_INTERFACE_WARNING | ||||||
|  | #endif // if defined(_MSC_VER) | ||||||
|  | #endif // ifdef JSON_IN_CPPTL | ||||||
|  | #if !defined(JSON_API) | ||||||
|  | #define JSON_API | ||||||
|  | #endif | ||||||
|  |  | ||||||
|  | // If JSON_NO_INT64 is defined, then Json only support C++ "int" type for | ||||||
|  | // integer | ||||||
|  | // Storages, and 64 bits integer support is disabled. | ||||||
|  | // #define JSON_NO_INT64 1 | ||||||
|  |  | ||||||
|  | #if defined(_MSC_VER) // MSVC | ||||||
|  | #  if _MSC_VER <= 1200 // MSVC 6 | ||||||
|  |     // Microsoft Visual Studio 6 only support conversion from __int64 to double | ||||||
|  |     // (no conversion from unsigned __int64). | ||||||
|  | #    define JSON_USE_INT64_DOUBLE_CONVERSION 1 | ||||||
|  |     // Disable warning 4786 for VS6 caused by STL (identifier was truncated to '255' | ||||||
|  |     // characters in the debug information) | ||||||
|  |     // All projects I've ever seen with VS6 were using this globally (not bothering | ||||||
|  |     // with pragma push/pop). | ||||||
|  | #    pragma warning(disable : 4786) | ||||||
|  | #  endif // MSVC 6 | ||||||
|  |  | ||||||
|  | #  if _MSC_VER >= 1500 // MSVC 2008 | ||||||
|  |     /// Indicates that the following function is deprecated. | ||||||
|  | #    define JSONCPP_DEPRECATED(message) __declspec(deprecated(message)) | ||||||
|  | #  endif | ||||||
|  |  | ||||||
|  | #endif // defined(_MSC_VER) | ||||||
|  |  | ||||||
|  | // In c++11 the override keyword allows you to explicity define that a function | ||||||
|  | // is intended to override the base-class version.  This makes the code more | ||||||
|  | // managable and fixes a set of common hard-to-find bugs. | ||||||
|  | #if __cplusplus >= 201103L | ||||||
|  | # define JSONCPP_OVERRIDE override | ||||||
|  | # define JSONCPP_NOEXCEPT noexcept | ||||||
|  | #elif defined(_MSC_VER) && _MSC_VER > 1600 && _MSC_VER < 1900 | ||||||
|  | # define JSONCPP_OVERRIDE override | ||||||
|  | # define JSONCPP_NOEXCEPT throw() | ||||||
|  | #elif defined(_MSC_VER) && _MSC_VER >= 1900 | ||||||
|  | # define JSONCPP_OVERRIDE override | ||||||
|  | # define JSONCPP_NOEXCEPT noexcept | ||||||
|  | #else | ||||||
|  | # define JSONCPP_OVERRIDE | ||||||
|  | # define JSONCPP_NOEXCEPT throw() | ||||||
|  | #endif | ||||||
|  |  | ||||||
|  | #ifndef JSON_HAS_RVALUE_REFERENCES | ||||||
|  |  | ||||||
|  | #if defined(_MSC_VER) && _MSC_VER >= 1600 // MSVC >= 2010 | ||||||
|  | #define JSON_HAS_RVALUE_REFERENCES 1 | ||||||
|  | #endif // MSVC >= 2010 | ||||||
|  |  | ||||||
|  | #ifdef __clang__ | ||||||
|  | #if __has_feature(cxx_rvalue_references) | ||||||
|  | #define JSON_HAS_RVALUE_REFERENCES 1 | ||||||
|  | #endif  // has_feature | ||||||
|  |  | ||||||
|  | #elif defined __GNUC__ // not clang (gcc comes later since clang emulates gcc) | ||||||
|  | #if defined(__GXX_EXPERIMENTAL_CXX0X__) || (__cplusplus >= 201103L) | ||||||
|  | #define JSON_HAS_RVALUE_REFERENCES 1 | ||||||
|  | #endif  // GXX_EXPERIMENTAL | ||||||
|  |  | ||||||
|  | #endif // __clang__ || __GNUC__ | ||||||
|  |  | ||||||
|  | #endif // not defined JSON_HAS_RVALUE_REFERENCES | ||||||
|  |  | ||||||
|  | #ifndef JSON_HAS_RVALUE_REFERENCES | ||||||
|  | #define JSON_HAS_RVALUE_REFERENCES 0 | ||||||
|  | #endif | ||||||
|  |  | ||||||
|  | #ifdef __clang__ | ||||||
|  | #  if __has_extension(attribute_deprecated_with_message) | ||||||
|  | #    define JSONCPP_DEPRECATED(message)  __attribute__ ((deprecated(message))) | ||||||
|  | #  endif | ||||||
|  | #elif defined __GNUC__ // not clang (gcc comes later since clang emulates gcc) | ||||||
|  | #  if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)) | ||||||
|  | #    define JSONCPP_DEPRECATED(message)  __attribute__ ((deprecated(message))) | ||||||
|  | #  elif (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)) | ||||||
|  | #    define JSONCPP_DEPRECATED(message)  __attribute__((__deprecated__)) | ||||||
|  | #  endif  // GNUC version | ||||||
|  | #endif // __clang__ || __GNUC__ | ||||||
|  |  | ||||||
|  | #if !defined(JSONCPP_DEPRECATED) | ||||||
|  | #define JSONCPP_DEPRECATED(message) | ||||||
|  | #endif // if !defined(JSONCPP_DEPRECATED) | ||||||
|  |  | ||||||
|  | #if __GNUC__ >= 6 | ||||||
|  | #  define JSON_USE_INT64_DOUBLE_CONVERSION 1 | ||||||
|  | #endif | ||||||
|  |  | ||||||
|  | #if !defined(JSON_IS_AMALGAMATION) | ||||||
|  |  | ||||||
|  | # include "version.h" | ||||||
|  |  | ||||||
|  | # if JSONCPP_USING_SECURE_MEMORY | ||||||
|  | #  include "allocator.h" //typedef Allocator | ||||||
|  | # endif | ||||||
|  |  | ||||||
|  | #endif // if !defined(JSON_IS_AMALGAMATION) | ||||||
|  |  | ||||||
|  | namespace Json { | ||||||
|  | typedef int Int; | ||||||
|  | typedef unsigned int UInt; | ||||||
|  | #if defined(JSON_NO_INT64) | ||||||
|  | typedef int LargestInt; | ||||||
|  | typedef unsigned int LargestUInt; | ||||||
|  | #undef JSON_HAS_INT64 | ||||||
|  | #else                 // if defined(JSON_NO_INT64) | ||||||
|  | // For Microsoft Visual use specific types as long long is not supported | ||||||
|  | #if defined(_MSC_VER) // Microsoft Visual Studio | ||||||
|  | typedef __int64 Int64; | ||||||
|  | typedef unsigned __int64 UInt64; | ||||||
|  | #else                 // if defined(_MSC_VER) // Other platforms, use long long | ||||||
|  | typedef int64_t Int64; | ||||||
|  | typedef uint64_t UInt64; | ||||||
|  | #endif // if defined(_MSC_VER) | ||||||
|  | typedef Int64 LargestInt; | ||||||
|  | typedef UInt64 LargestUInt; | ||||||
|  | #define JSON_HAS_INT64 | ||||||
|  | #endif // if defined(JSON_NO_INT64) | ||||||
|  | #if JSONCPP_USING_SECURE_MEMORY | ||||||
|  | #define JSONCPP_STRING        std::basic_string<char, std::char_traits<char>, Json::SecureAllocator<char> > | ||||||
|  | #define JSONCPP_OSTRINGSTREAM std::basic_ostringstream<char, std::char_traits<char>, Json::SecureAllocator<char> > | ||||||
|  | #define JSONCPP_OSTREAM       std::basic_ostream<char, std::char_traits<char>> | ||||||
|  | #define JSONCPP_ISTRINGSTREAM std::basic_istringstream<char, std::char_traits<char>, Json::SecureAllocator<char> > | ||||||
|  | #define JSONCPP_ISTREAM       std::istream | ||||||
|  | #else | ||||||
|  | #define JSONCPP_STRING        std::string | ||||||
|  | #define JSONCPP_OSTRINGSTREAM std::ostringstream | ||||||
|  | #define JSONCPP_OSTREAM       std::ostream | ||||||
|  | #define JSONCPP_ISTRINGSTREAM std::istringstream | ||||||
|  | #define JSONCPP_ISTREAM       std::istream | ||||||
|  | #endif // if JSONCPP_USING_SECURE_MEMORY | ||||||
|  | } // end namespace Json | ||||||
|  |  | ||||||
|  | #endif // JSON_CONFIG_H_INCLUDED | ||||||
|  |  | ||||||
|  | // ////////////////////////////////////////////////////////////////////// | ||||||
|  | // End of content of file: include/json/config.h | ||||||
|  | // ////////////////////////////////////////////////////////////////////// | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  | // ////////////////////////////////////////////////////////////////////// | ||||||
|  | // Beginning of content of file: include/json/forwards.h | ||||||
|  | // ////////////////////////////////////////////////////////////////////// | ||||||
|  |  | ||||||
|  | // Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors | ||||||
|  | // Distributed under MIT license, or public domain if desired and | ||||||
|  | // recognized in your jurisdiction. | ||||||
|  | // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE | ||||||
|  |  | ||||||
|  | #ifndef JSON_FORWARDS_H_INCLUDED | ||||||
|  | #define JSON_FORWARDS_H_INCLUDED | ||||||
|  |  | ||||||
|  | #if !defined(JSON_IS_AMALGAMATION) | ||||||
|  | #include "config.h" | ||||||
|  | #endif // if !defined(JSON_IS_AMALGAMATION) | ||||||
|  |  | ||||||
|  | namespace Json { | ||||||
|  |  | ||||||
|  | // writer.h | ||||||
|  | class FastWriter; | ||||||
|  | class StyledWriter; | ||||||
|  |  | ||||||
|  | // reader.h | ||||||
|  | class Reader; | ||||||
|  |  | ||||||
|  | // features.h | ||||||
|  | class Features; | ||||||
|  |  | ||||||
|  | // value.h | ||||||
|  | typedef unsigned int ArrayIndex; | ||||||
|  | class StaticString; | ||||||
|  | class Path; | ||||||
|  | class PathArgument; | ||||||
|  | class Value; | ||||||
|  | class ValueIteratorBase; | ||||||
|  | class ValueIterator; | ||||||
|  | class ValueConstIterator; | ||||||
|  |  | ||||||
|  | } // namespace Json | ||||||
|  |  | ||||||
|  | #endif // JSON_FORWARDS_H_INCLUDED | ||||||
|  |  | ||||||
|  | // ////////////////////////////////////////////////////////////////////// | ||||||
|  | // End of content of file: include/json/forwards.h | ||||||
|  | // ////////////////////////////////////////////////////////////////////// | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  | #endif //ifndef JSON_FORWARD_AMALGATED_H_INCLUDED | ||||||
							
								
								
									
										2186
									
								
								ws/cobra_publisher/jsoncpp/json/json.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2186
									
								
								ws/cobra_publisher/jsoncpp/json/json.h
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										5386
									
								
								ws/cobra_publisher/jsoncpp/jsoncpp.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5386
									
								
								ws/cobra_publisher/jsoncpp/jsoncpp.cpp
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										44
									
								
								ws/docker_build.sh
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								ws/docker_build.sh
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,44 @@ | |||||||
|  | #!/bin/sh | ||||||
|  | # | ||||||
|  | # Author: Benjamin Sergeant | ||||||
|  | # Copyright (c) 2017-2018 Machine Zone, Inc. All rights reserved. | ||||||
|  | # | ||||||
|  |  | ||||||
|  | # 'manual' way of building. I cannot get CMake to work to build in a container. | ||||||
|  |  | ||||||
|  | g++ --std=c++14 \ | ||||||
|  |     -DIXWEBSOCKET_USE_TLS \ | ||||||
|  |     -g \ | ||||||
|  |     ../ixwebsocket/IXEventFd.cpp \ | ||||||
|  |     ../ixwebsocket/IXSocket.cpp \ | ||||||
|  |     ../ixwebsocket/IXSocketServer.cpp \ | ||||||
|  |     ../ixwebsocket/IXSocketConnect.cpp \ | ||||||
|  |     ../ixwebsocket/IXDNSLookup.cpp \ | ||||||
|  |     ../ixwebsocket/IXCancellationRequest.cpp \ | ||||||
|  |     ../ixwebsocket/IXWebSocket.cpp \ | ||||||
|  |     ../ixwebsocket/IXWebSocketServer.cpp \ | ||||||
|  |     ../ixwebsocket/IXWebSocketTransport.cpp \ | ||||||
|  |     ../ixwebsocket/IXWebSocketHandshake.cpp \ | ||||||
|  |     ../ixwebsocket/IXWebSocketPerMessageDeflate.cpp \ | ||||||
|  |     ../ixwebsocket/IXWebSocketPerMessageDeflateCodec.cpp \ | ||||||
|  |     ../ixwebsocket/IXWebSocketPerMessageDeflateOptions.cpp \ | ||||||
|  |     ../ixwebsocket/IXSocketOpenSSL.cpp \ | ||||||
|  |     ../ixwebsocket/linux/IXSetThreadName_linux.cpp \ | ||||||
|  |     ../third_party/jsoncpp/jsoncpp.cpp \ | ||||||
|  |     ixcrypto/IXBase64.cpp \ | ||||||
|  |     ixcrypto/IXHash.cpp \ | ||||||
|  |     ixcrypto/IXUuid.cpp \ | ||||||
|  |     ws_ping_pong.cpp \ | ||||||
|  |     ws_broadcast_server.cpp \ | ||||||
|  |     ws_echo_server.cpp \ | ||||||
|  |     ws_chat.cpp \ | ||||||
|  |     ws_connect.cpp \ | ||||||
|  |     ws_transfer.cpp \ | ||||||
|  |     ws_send.cpp \ | ||||||
|  |     ws_receive.cpp \ | ||||||
|  |     ws.cpp \ | ||||||
|  |     -I . \ | ||||||
|  |     -I .. \ | ||||||
|  |     -I ../third_party \ | ||||||
|  |     -o ws \ | ||||||
|  |     -lcrypto -lssl -lz -lpthread | ||||||
| @@ -81,4 +81,55 @@ namespace ix | |||||||
| 
 | 
 | ||||||
|         return ret; |         return ret; | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     static inline bool is_base64(unsigned char c) | ||||||
|  |     { | ||||||
|  |         return (isalnum(c) || (c == '+') || (c == '/')); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     std::string base64_decode(const std::string& encoded_string) | ||||||
|  |     { | ||||||
|  |         int in_len = (int)encoded_string.size(); | ||||||
|  |         int i = 0; | ||||||
|  |         int j = 0; | ||||||
|  |         int in_ = 0; | ||||||
|  |         unsigned char char_array_4[4], char_array_3[3]; | ||||||
|  |         std::string ret; | ||||||
|  | 
 | ||||||
|  |         while(in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) | ||||||
|  |         { | ||||||
|  |             char_array_4[i++] = encoded_string[in_]; in_++; | ||||||
|  |             if(i ==4) | ||||||
|  |             { | ||||||
|  |                 for(i = 0; i <4; i++) | ||||||
|  |                     char_array_4[i] = base64_chars.find(char_array_4[i]); | ||||||
|  | 
 | ||||||
|  |                 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); | ||||||
|  |                 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); | ||||||
|  |                 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; | ||||||
|  | 
 | ||||||
|  |                 for(i = 0; (i < 3); i++) | ||||||
|  |                     ret += char_array_3[i]; | ||||||
|  | 
 | ||||||
|  |                 i = 0; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         if(i) | ||||||
|  |         { | ||||||
|  |             for(j = i; j <4; j++) | ||||||
|  |                 char_array_4[j] = 0; | ||||||
|  | 
 | ||||||
|  |             for(j = 0; j <4; j++) | ||||||
|  |                 char_array_4[j] = base64_chars.find(char_array_4[j]); | ||||||
|  | 
 | ||||||
|  |             char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); | ||||||
|  |             char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); | ||||||
|  |             char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; | ||||||
|  | 
 | ||||||
|  |             for(j = 0; (j < i - 1); j++) ret += char_array_3[j]; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         return ret; | ||||||
|  |     } | ||||||
| } | } | ||||||
Some files were not shown because too many files have changed in this diff Show More
		Reference in New Issue
	
	Block a user