uws-react-native 0.0.1-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +84 -0
- package/UwsReactNative.podspec +71 -0
- package/android/build.gradle.kts +138 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/id/sufeni/oss/uwsreactnative/UwsReactNativePackage.kt +18 -0
- package/apple/OnLoad.mm +17 -0
- package/cpp/CMakeLists.txt +154 -0
- package/cpp/UwsReactNativeModule.cpp +191 -0
- package/cpp/UwsReactNativeModule.h +30 -0
- package/cpp/app/AppHost.h +37 -0
- package/cpp/app/AppRunner.h +88 -0
- package/cpp/app/CMakeLists.txt +39 -0
- package/cpp/app/HttpRequestObject.h +173 -0
- package/cpp/app/HttpResponseObject.h +623 -0
- package/cpp/app/RecognizedString.h +71 -0
- package/cpp/app/TemplatedAppObject.h +472 -0
- package/cpp/bridging/ArrayBuffer.h +150 -0
- package/cpp/bridging/CMakeLists.txt +34 -0
- package/cpp/jsi/Buffer.h +91 -0
- package/cpp/jsi/CMakeLists.txt +34 -0
- package/cpp/uSockets/bsd.c +767 -0
- package/cpp/uSockets/context.c +524 -0
- package/cpp/uSockets/crypto/openssl.c +858 -0
- package/cpp/uSockets/crypto/sni_tree.cpp +218 -0
- package/cpp/uSockets/eventing/asio.cpp +475 -0
- package/cpp/uSockets/eventing/epoll_kqueue.c +458 -0
- package/cpp/uSockets/eventing/gcd.c +278 -0
- package/cpp/uSockets/eventing/libuv.c +305 -0
- package/cpp/uSockets/internal/eventing/asio.h +45 -0
- package/cpp/uSockets/internal/eventing/epoll_kqueue.h +67 -0
- package/cpp/uSockets/internal/eventing/gcd.h +40 -0
- package/cpp/uSockets/internal/eventing/libuv.h +45 -0
- package/cpp/uSockets/internal/internal.h +224 -0
- package/cpp/uSockets/internal/loop_data.h +38 -0
- package/cpp/uSockets/internal/networking/bsd.h +109 -0
- package/cpp/uSockets/io_uring/internal.h +98 -0
- package/cpp/uSockets/io_uring/io_context.c +302 -0
- package/cpp/uSockets/io_uring/io_loop.c +422 -0
- package/cpp/uSockets/io_uring/io_socket.c +139 -0
- package/cpp/uSockets/libusockets.h +350 -0
- package/cpp/uSockets/loop.c +396 -0
- package/cpp/uSockets/quic.c +1071 -0
- package/cpp/uSockets/quic.h +68 -0
- package/cpp/uSockets/socket.c +231 -0
- package/cpp/uSockets/udp.c +151 -0
- package/cpp/uWebSockets/uWebSockets/App.h +676 -0
- package/cpp/uWebSockets/uWebSockets/AsyncSocket.h +376 -0
- package/cpp/uWebSockets/uWebSockets/AsyncSocketData.h +94 -0
- package/cpp/uWebSockets/uWebSockets/BloomFilter.h +83 -0
- package/cpp/uWebSockets/uWebSockets/CachingApp.h +115 -0
- package/cpp/uWebSockets/uWebSockets/ChunkedEncoding.h +237 -0
- package/cpp/uWebSockets/uWebSockets/ClientApp.h +36 -0
- package/cpp/uWebSockets/uWebSockets/Http3App.h +140 -0
- package/cpp/uWebSockets/uWebSockets/Http3Context.h +157 -0
- package/cpp/uWebSockets/uWebSockets/Http3ContextData.h +21 -0
- package/cpp/uWebSockets/uWebSockets/Http3Request.h +22 -0
- package/cpp/uWebSockets/uWebSockets/Http3Response.h +120 -0
- package/cpp/uWebSockets/uWebSockets/Http3ResponseData.h +25 -0
- package/cpp/uWebSockets/uWebSockets/HttpContext.h +521 -0
- package/cpp/uWebSockets/uWebSockets/HttpContextData.h +60 -0
- package/cpp/uWebSockets/uWebSockets/HttpErrors.h +53 -0
- package/cpp/uWebSockets/uWebSockets/HttpParser.h +747 -0
- package/cpp/uWebSockets/uWebSockets/HttpResponse.h +605 -0
- package/cpp/uWebSockets/uWebSockets/HttpResponseData.h +123 -0
- package/cpp/uWebSockets/uWebSockets/HttpRouter.h +384 -0
- package/cpp/uWebSockets/uWebSockets/LocalCluster.h +64 -0
- package/cpp/uWebSockets/uWebSockets/Loop.h +238 -0
- package/cpp/uWebSockets/uWebSockets/LoopData.h +113 -0
- package/cpp/uWebSockets/uWebSockets/MessageParser.h +64 -0
- package/cpp/uWebSockets/uWebSockets/MoveOnlyFunction.h +389 -0
- package/cpp/uWebSockets/uWebSockets/Multipart.h +231 -0
- package/cpp/uWebSockets/uWebSockets/PerMessageDeflate.h +303 -0
- package/cpp/uWebSockets/uWebSockets/ProxyParser.h +187 -0
- package/cpp/uWebSockets/uWebSockets/QueryParser.h +120 -0
- package/cpp/uWebSockets/uWebSockets/TopicTree.h +364 -0
- package/cpp/uWebSockets/uWebSockets/Utilities.h +66 -0
- package/cpp/uWebSockets/uWebSockets/WebSocket.h +407 -0
- package/cpp/uWebSockets/uWebSockets/WebSocketContext.h +436 -0
- package/cpp/uWebSockets/uWebSockets/WebSocketContextData.h +109 -0
- package/cpp/uWebSockets/uWebSockets/WebSocketData.h +86 -0
- package/cpp/uWebSockets/uWebSockets/WebSocketExtensions.h +256 -0
- package/cpp/uWebSockets/uWebSockets/WebSocketHandshake.h +145 -0
- package/cpp/uWebSockets/uWebSockets/WebSocketProtocol.h +525 -0
- package/lib/commonjs/_internal/native-modules/NativeReactNativeUws.js +12 -0
- package/lib/commonjs/_internal/native-modules/NativeReactNativeUws.js.map +1 -0
- package/lib/commonjs/index.js +28 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/modules/App.js +17 -0
- package/lib/commonjs/modules/App.js.map +1 -0
- package/lib/commonjs/modules/CompressOptions.js +103 -0
- package/lib/commonjs/modules/CompressOptions.js.map +1 -0
- package/lib/commonjs/modules/EnvironmentVariables.js +17 -0
- package/lib/commonjs/modules/EnvironmentVariables.js.map +1 -0
- package/lib/commonjs/modules/ListenOptions.js +12 -0
- package/lib/commonjs/modules/ListenOptions.js.map +1 -0
- package/lib/commonjs/modules/SSLApp.js +15 -0
- package/lib/commonjs/modules/SSLApp.js.map +1 -0
- package/lib/commonjs/modules/getParts.js +28 -0
- package/lib/commonjs/modules/getParts.js.map +1 -0
- package/lib/commonjs/modules/index.js +61 -0
- package/lib/commonjs/modules/index.js.map +1 -0
- package/lib/commonjs/modules/us_listen_socket_close.js +21 -0
- package/lib/commonjs/modules/us_listen_socket_close.js.map +1 -0
- package/lib/commonjs/modules/us_socket_local_port.js +22 -0
- package/lib/commonjs/modules/us_socket_local_port.js.map +1 -0
- package/lib/commonjs/package.json +1 -0
- package/lib/commonjs/types/AppDescriptor.js +2 -0
- package/lib/commonjs/types/AppDescriptor.js.map +1 -0
- package/lib/commonjs/types/AppOptions.js +2 -0
- package/lib/commonjs/types/AppOptions.js.map +1 -0
- package/lib/commonjs/types/HttpRequest.js +6 -0
- package/lib/commonjs/types/HttpRequest.js.map +1 -0
- package/lib/commonjs/types/HttpResponse.js +6 -0
- package/lib/commonjs/types/HttpResponse.js.map +1 -0
- package/lib/commonjs/types/HttpRouterOptions.js +2 -0
- package/lib/commonjs/types/HttpRouterOptions.js.map +1 -0
- package/lib/commonjs/types/MultipartField.js +2 -0
- package/lib/commonjs/types/MultipartField.js.map +1 -0
- package/lib/commonjs/types/RecognizedString.js +2 -0
- package/lib/commonjs/types/RecognizedString.js.map +1 -0
- package/lib/commonjs/types/TemplatedApp.js +6 -0
- package/lib/commonjs/types/TemplatedApp.js.map +1 -0
- package/lib/commonjs/types/WebSocket.js +6 -0
- package/lib/commonjs/types/WebSocket.js.map +1 -0
- package/lib/commonjs/types/WebSocketBehaviour.js +139 -0
- package/lib/commonjs/types/WebSocketBehaviour.js.map +1 -0
- package/lib/commonjs/types/index.js +2 -0
- package/lib/commonjs/types/index.js.map +1 -0
- package/lib/commonjs/types/us_listen_socket.js +2 -0
- package/lib/commonjs/types/us_listen_socket.js.map +1 -0
- package/lib/commonjs/types/us_socket.js +2 -0
- package/lib/commonjs/types/us_socket.js.map +1 -0
- package/lib/commonjs/types/us_socket_context_t.js +2 -0
- package/lib/commonjs/types/us_socket_context_t.js.map +1 -0
- package/lib/module/_internal/native-modules/NativeReactNativeUws.js +10 -0
- package/lib/module/_internal/native-modules/NativeReactNativeUws.js.map +1 -0
- package/lib/module/index.js +5 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/modules/App.js +12 -0
- package/lib/module/modules/App.js.map +1 -0
- package/lib/module/modules/CompressOptions.js +103 -0
- package/lib/module/modules/CompressOptions.js.map +1 -0
- package/lib/module/modules/EnvironmentVariables.js +13 -0
- package/lib/module/modules/EnvironmentVariables.js.map +1 -0
- package/lib/module/modules/ListenOptions.js +8 -0
- package/lib/module/modules/ListenOptions.js.map +1 -0
- package/lib/module/modules/SSLApp.js +15 -0
- package/lib/module/modules/SSLApp.js.map +1 -0
- package/lib/module/modules/getParts.js +23 -0
- package/lib/module/modules/getParts.js.map +1 -0
- package/lib/module/modules/index.js +11 -0
- package/lib/module/modules/index.js.map +1 -0
- package/lib/module/modules/us_listen_socket_close.js +17 -0
- package/lib/module/modules/us_listen_socket_close.js.map +1 -0
- package/lib/module/modules/us_socket_local_port.js +18 -0
- package/lib/module/modules/us_socket_local_port.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/module/types/AppDescriptor.js +2 -0
- package/lib/module/types/AppDescriptor.js.map +1 -0
- package/lib/module/types/AppOptions.js +2 -0
- package/lib/module/types/AppOptions.js.map +1 -0
- package/lib/module/types/HttpRequest.js +4 -0
- package/lib/module/types/HttpRequest.js.map +1 -0
- package/lib/module/types/HttpResponse.js +4 -0
- package/lib/module/types/HttpResponse.js.map +1 -0
- package/lib/module/types/HttpRouterOptions.js +2 -0
- package/lib/module/types/HttpRouterOptions.js.map +1 -0
- package/lib/module/types/MultipartField.js +2 -0
- package/lib/module/types/MultipartField.js.map +1 -0
- package/lib/module/types/RecognizedString.js +2 -0
- package/lib/module/types/RecognizedString.js.map +1 -0
- package/lib/module/types/TemplatedApp.js +4 -0
- package/lib/module/types/TemplatedApp.js.map +1 -0
- package/lib/module/types/WebSocket.js +4 -0
- package/lib/module/types/WebSocket.js.map +1 -0
- package/lib/module/types/WebSocketBehaviour.js +139 -0
- package/lib/module/types/WebSocketBehaviour.js.map +1 -0
- package/lib/module/types/index.js +2 -0
- package/lib/module/types/index.js.map +1 -0
- package/lib/module/types/us_listen_socket.js +2 -0
- package/lib/module/types/us_listen_socket.js.map +1 -0
- package/lib/module/types/us_socket.js +2 -0
- package/lib/module/types/us_socket.js.map +1 -0
- package/lib/module/types/us_socket_context_t.js +2 -0
- package/lib/module/types/us_socket_context_t.js.map +1 -0
- package/lib/typescript/commonjs/_internal/native-modules/NativeReactNativeUws.d.ts +22 -0
- package/lib/typescript/commonjs/_internal/native-modules/NativeReactNativeUws.d.ts.map +1 -0
- package/lib/typescript/commonjs/index.d.ts +3 -0
- package/lib/typescript/commonjs/index.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/App.d.ts +7 -0
- package/lib/typescript/commonjs/modules/App.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/CompressOptions.d.ts +1 -0
- package/lib/typescript/commonjs/modules/CompressOptions.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/EnvironmentVariables.d.ts +16 -0
- package/lib/typescript/commonjs/modules/EnvironmentVariables.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/ListenOptions.d.ts +5 -0
- package/lib/typescript/commonjs/modules/ListenOptions.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/SSLApp.d.ts +1 -0
- package/lib/typescript/commonjs/modules/SSLApp.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/getParts.d.ts +6 -0
- package/lib/typescript/commonjs/modules/getParts.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/index.d.ts +6 -0
- package/lib/typescript/commonjs/modules/index.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/us_listen_socket_close.d.ts +5 -0
- package/lib/typescript/commonjs/modules/us_listen_socket_close.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/us_socket_local_port.d.ts +5 -0
- package/lib/typescript/commonjs/modules/us_socket_local_port.d.ts.map +1 -0
- package/lib/typescript/commonjs/package.json +1 -0
- package/lib/typescript/commonjs/types/AppDescriptor.d.ts +8 -0
- package/lib/typescript/commonjs/types/AppDescriptor.d.ts.map +1 -0
- package/lib/typescript/commonjs/types/AppOptions.d.ts +17 -0
- package/lib/typescript/commonjs/types/AppOptions.d.ts.map +1 -0
- package/lib/typescript/commonjs/types/HttpRequest.d.ts +54 -0
- package/lib/typescript/commonjs/types/HttpRequest.d.ts.map +1 -0
- package/lib/typescript/commonjs/types/HttpResponse.d.ts +169 -0
- package/lib/typescript/commonjs/types/HttpResponse.d.ts.map +1 -0
- package/lib/typescript/commonjs/types/HttpRouterOptions.d.ts +24 -0
- package/lib/typescript/commonjs/types/HttpRouterOptions.d.ts.map +1 -0
- package/lib/typescript/commonjs/types/MultipartField.d.ts +7 -0
- package/lib/typescript/commonjs/types/MultipartField.d.ts.map +1 -0
- package/lib/typescript/commonjs/types/RecognizedString.d.ts +10 -0
- package/lib/typescript/commonjs/types/RecognizedString.d.ts.map +1 -0
- package/lib/typescript/commonjs/types/TemplatedApp.d.ts +108 -0
- package/lib/typescript/commonjs/types/TemplatedApp.d.ts.map +1 -0
- package/lib/typescript/commonjs/types/WebSocket.d.ts +96 -0
- package/lib/typescript/commonjs/types/WebSocket.d.ts.map +1 -0
- package/lib/typescript/commonjs/types/WebSocketBehaviour.d.ts +1 -0
- package/lib/typescript/commonjs/types/WebSocketBehaviour.d.ts.map +1 -0
- package/lib/typescript/commonjs/types/index.d.ts +6 -0
- package/lib/typescript/commonjs/types/index.d.ts.map +1 -0
- package/lib/typescript/commonjs/types/us_listen_socket.d.ts +7 -0
- package/lib/typescript/commonjs/types/us_listen_socket.d.ts.map +1 -0
- package/lib/typescript/commonjs/types/us_socket.d.ts +7 -0
- package/lib/typescript/commonjs/types/us_socket.d.ts.map +1 -0
- package/lib/typescript/commonjs/types/us_socket_context_t.d.ts +7 -0
- package/lib/typescript/commonjs/types/us_socket_context_t.d.ts.map +1 -0
- package/lib/typescript/module/_internal/native-modules/NativeReactNativeUws.d.ts +22 -0
- package/lib/typescript/module/_internal/native-modules/NativeReactNativeUws.d.ts.map +1 -0
- package/lib/typescript/module/index.d.ts +3 -0
- package/lib/typescript/module/index.d.ts.map +1 -0
- package/lib/typescript/module/modules/App.d.ts +7 -0
- package/lib/typescript/module/modules/App.d.ts.map +1 -0
- package/lib/typescript/module/modules/CompressOptions.d.ts +1 -0
- package/lib/typescript/module/modules/CompressOptions.d.ts.map +1 -0
- package/lib/typescript/module/modules/EnvironmentVariables.d.ts +16 -0
- package/lib/typescript/module/modules/EnvironmentVariables.d.ts.map +1 -0
- package/lib/typescript/module/modules/ListenOptions.d.ts +5 -0
- package/lib/typescript/module/modules/ListenOptions.d.ts.map +1 -0
- package/lib/typescript/module/modules/SSLApp.d.ts +1 -0
- package/lib/typescript/module/modules/SSLApp.d.ts.map +1 -0
- package/lib/typescript/module/modules/getParts.d.ts +6 -0
- package/lib/typescript/module/modules/getParts.d.ts.map +1 -0
- package/lib/typescript/module/modules/index.d.ts +6 -0
- package/lib/typescript/module/modules/index.d.ts.map +1 -0
- package/lib/typescript/module/modules/us_listen_socket_close.d.ts +5 -0
- package/lib/typescript/module/modules/us_listen_socket_close.d.ts.map +1 -0
- package/lib/typescript/module/modules/us_socket_local_port.d.ts +5 -0
- package/lib/typescript/module/modules/us_socket_local_port.d.ts.map +1 -0
- package/lib/typescript/module/package.json +1 -0
- package/lib/typescript/module/types/AppDescriptor.d.ts +8 -0
- package/lib/typescript/module/types/AppDescriptor.d.ts.map +1 -0
- package/lib/typescript/module/types/AppOptions.d.ts +17 -0
- package/lib/typescript/module/types/AppOptions.d.ts.map +1 -0
- package/lib/typescript/module/types/HttpRequest.d.ts +54 -0
- package/lib/typescript/module/types/HttpRequest.d.ts.map +1 -0
- package/lib/typescript/module/types/HttpResponse.d.ts +169 -0
- package/lib/typescript/module/types/HttpResponse.d.ts.map +1 -0
- package/lib/typescript/module/types/HttpRouterOptions.d.ts +24 -0
- package/lib/typescript/module/types/HttpRouterOptions.d.ts.map +1 -0
- package/lib/typescript/module/types/MultipartField.d.ts +7 -0
- package/lib/typescript/module/types/MultipartField.d.ts.map +1 -0
- package/lib/typescript/module/types/RecognizedString.d.ts +10 -0
- package/lib/typescript/module/types/RecognizedString.d.ts.map +1 -0
- package/lib/typescript/module/types/TemplatedApp.d.ts +108 -0
- package/lib/typescript/module/types/TemplatedApp.d.ts.map +1 -0
- package/lib/typescript/module/types/WebSocket.d.ts +96 -0
- package/lib/typescript/module/types/WebSocket.d.ts.map +1 -0
- package/lib/typescript/module/types/WebSocketBehaviour.d.ts +1 -0
- package/lib/typescript/module/types/WebSocketBehaviour.d.ts.map +1 -0
- package/lib/typescript/module/types/index.d.ts +6 -0
- package/lib/typescript/module/types/index.d.ts.map +1 -0
- package/lib/typescript/module/types/us_listen_socket.d.ts +7 -0
- package/lib/typescript/module/types/us_listen_socket.d.ts.map +1 -0
- package/lib/typescript/module/types/us_socket.d.ts +7 -0
- package/lib/typescript/module/types/us_socket.d.ts.map +1 -0
- package/lib/typescript/module/types/us_socket_context_t.d.ts +7 -0
- package/lib/typescript/module/types/us_socket_context_t.d.ts.map +1 -0
- package/lib/typescript/tsconfig.tsbuildinfo +1 -0
- package/package.json +62 -0
- package/src/_internal/native-modules/NativeUwsReactNative.ts +40 -0
- package/src/index.ts +2 -0
- package/src/modules/App.ts +14 -0
- package/src/modules/CompressOptions.ts +101 -0
- package/src/modules/EnvironmentVariables.ts +16 -0
- package/src/modules/ListenOptions.ts +4 -0
- package/src/modules/SSLApp.ts +13 -0
- package/src/modules/getParts.ts +29 -0
- package/src/modules/index.ts +8 -0
- package/src/modules/us_listen_socket_close.ts +15 -0
- package/src/modules/us_socket_local_port.ts +16 -0
- package/src/types/AppDescriptor.ts +7 -0
- package/src/types/AppOptions.ts +22 -0
- package/src/types/HttpRequest.ts +74 -0
- package/src/types/HttpResponse.ts +309 -0
- package/src/types/HttpRouterOptions.ts +24 -0
- package/src/types/MultipartField.ts +6 -0
- package/src/types/RecognizedString.ts +11 -0
- package/src/types/TemplatedApp.ts +285 -0
- package/src/types/WebSocket.ts +147 -0
- package/src/types/WebSocketBehaviour.ts +137 -0
- package/src/types/index.ts +12 -0
- package/src/types/us_listen_socket.ts +6 -0
- package/src/types/us_socket.ts +6 -0
- package/src/types/us_socket_context_t.ts +6 -0
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Authored by Alex Hultman, 2018-2021.
|
|
3
|
+
* Intellectual property of third-party.
|
|
4
|
+
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/* This standalone module implements deflate / inflate streams */
|
|
19
|
+
|
|
20
|
+
#ifndef UWS_PERMESSAGEDEFLATE_H
|
|
21
|
+
#define UWS_PERMESSAGEDEFLATE_H
|
|
22
|
+
|
|
23
|
+
#include <cstdint>
|
|
24
|
+
#include <cstring>
|
|
25
|
+
|
|
26
|
+
/* We always define these options no matter if ZLIB is enabled or not */
|
|
27
|
+
namespace uWS {
|
|
28
|
+
/* Compressor mode is 8 lowest bits where HIGH4(windowBits), LOW4(memLevel).
|
|
29
|
+
* Decompressor mode is 8 highest bits LOW4(windowBits).
|
|
30
|
+
* If compressor or decompressor bits are 1, then they are shared.
|
|
31
|
+
* If everything is just simply 0, then everything is disabled. */
|
|
32
|
+
enum CompressOptions : uint16_t {
|
|
33
|
+
/* These are not actual compression options */
|
|
34
|
+
_COMPRESSOR_MASK = 0x00FF,
|
|
35
|
+
_DECOMPRESSOR_MASK = 0x0F00,
|
|
36
|
+
/* Disabled, shared, shared are "special" values */
|
|
37
|
+
DISABLED = 0,
|
|
38
|
+
SHARED_COMPRESSOR = 1,
|
|
39
|
+
SHARED_DECOMPRESSOR = 1 << 8,
|
|
40
|
+
/* Highest 4 bits describe decompressor */
|
|
41
|
+
DEDICATED_DECOMPRESSOR_32KB = 15 << 8,
|
|
42
|
+
DEDICATED_DECOMPRESSOR_16KB = 14 << 8,
|
|
43
|
+
DEDICATED_DECOMPRESSOR_8KB = 13 << 8,
|
|
44
|
+
DEDICATED_DECOMPRESSOR_4KB = 12 << 8,
|
|
45
|
+
DEDICATED_DECOMPRESSOR_2KB = 11 << 8,
|
|
46
|
+
DEDICATED_DECOMPRESSOR_1KB = 10 << 8,
|
|
47
|
+
DEDICATED_DECOMPRESSOR_512B = 9 << 8,
|
|
48
|
+
/* Same as 32kb */
|
|
49
|
+
DEDICATED_DECOMPRESSOR = 15 << 8,
|
|
50
|
+
|
|
51
|
+
/* Lowest 8 bit describe compressor */
|
|
52
|
+
DEDICATED_COMPRESSOR_3KB = 9 << 4 | 1,
|
|
53
|
+
DEDICATED_COMPRESSOR_4KB = 9 << 4 | 2,
|
|
54
|
+
DEDICATED_COMPRESSOR_8KB = 10 << 4 | 3,
|
|
55
|
+
DEDICATED_COMPRESSOR_16KB = 11 << 4 | 4,
|
|
56
|
+
DEDICATED_COMPRESSOR_32KB = 12 << 4 | 5,
|
|
57
|
+
DEDICATED_COMPRESSOR_64KB = 13 << 4 | 6,
|
|
58
|
+
DEDICATED_COMPRESSOR_128KB = 14 << 4 | 7,
|
|
59
|
+
DEDICATED_COMPRESSOR_256KB = 15 << 4 | 8,
|
|
60
|
+
/* Same as 256kb */
|
|
61
|
+
DEDICATED_COMPRESSOR = 15 << 4 | 8
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
#if !defined(UWS_NO_ZLIB) && !defined(UWS_MOCK_ZLIB)
|
|
66
|
+
#include <zlib.h>
|
|
67
|
+
#endif
|
|
68
|
+
|
|
69
|
+
#include <string>
|
|
70
|
+
#include <optional>
|
|
71
|
+
|
|
72
|
+
#ifdef UWS_USE_LIBDEFLATE
|
|
73
|
+
#include "libdeflate.h"
|
|
74
|
+
#include <cstring>
|
|
75
|
+
#endif
|
|
76
|
+
|
|
77
|
+
namespace uWS {
|
|
78
|
+
|
|
79
|
+
/* Do not compile this module if we don't want it */
|
|
80
|
+
#if defined(UWS_NO_ZLIB) || defined(UWS_MOCK_ZLIB)
|
|
81
|
+
struct ZlibContext {};
|
|
82
|
+
struct InflationStream {
|
|
83
|
+
std::optional<std::string_view> inflate(ZlibContext * /*zlibContext*/, std::string_view compressed, size_t maxPayloadLength, bool /*reset*/) {
|
|
84
|
+
return compressed.substr(0, std::min(maxPayloadLength, compressed.length()));
|
|
85
|
+
}
|
|
86
|
+
InflationStream(CompressOptions /*compressOptions*/) {
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
struct DeflationStream {
|
|
90
|
+
std::string_view deflate(ZlibContext * /*zlibContext*/, std::string_view raw, bool /*reset*/) {
|
|
91
|
+
return raw;
|
|
92
|
+
}
|
|
93
|
+
DeflationStream(CompressOptions /*compressOptions*/) {
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
#else
|
|
97
|
+
|
|
98
|
+
#define LARGE_BUFFER_SIZE 1024 * 16 // todo: fix this
|
|
99
|
+
|
|
100
|
+
struct ZlibContext {
|
|
101
|
+
/* Any returned data is valid until next same-class call.
|
|
102
|
+
* We need to have two classes to allow inflation followed
|
|
103
|
+
* by many deflations without modifying the inflation */
|
|
104
|
+
std::string dynamicDeflationBuffer;
|
|
105
|
+
std::string dynamicInflationBuffer;
|
|
106
|
+
char *deflationBuffer;
|
|
107
|
+
char *inflationBuffer;
|
|
108
|
+
|
|
109
|
+
#ifdef UWS_USE_LIBDEFLATE
|
|
110
|
+
libdeflate_decompressor *decompressor;
|
|
111
|
+
libdeflate_compressor *compressor;
|
|
112
|
+
#endif
|
|
113
|
+
|
|
114
|
+
ZlibContext() {
|
|
115
|
+
deflationBuffer = (char *) malloc(LARGE_BUFFER_SIZE);
|
|
116
|
+
inflationBuffer = (char *) malloc(LARGE_BUFFER_SIZE);
|
|
117
|
+
|
|
118
|
+
#ifdef UWS_USE_LIBDEFLATE
|
|
119
|
+
decompressor = libdeflate_alloc_decompressor();
|
|
120
|
+
compressor = libdeflate_alloc_compressor(6);
|
|
121
|
+
#endif
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
~ZlibContext() {
|
|
125
|
+
free(deflationBuffer);
|
|
126
|
+
free(inflationBuffer);
|
|
127
|
+
|
|
128
|
+
#ifdef UWS_USE_LIBDEFLATE
|
|
129
|
+
libdeflate_free_decompressor(decompressor);
|
|
130
|
+
libdeflate_free_compressor(compressor);
|
|
131
|
+
#endif
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
struct DeflationStream {
|
|
136
|
+
z_stream deflationStream = {};
|
|
137
|
+
|
|
138
|
+
DeflationStream(CompressOptions compressOptions) {
|
|
139
|
+
|
|
140
|
+
/* Sliding inflator should be about 44kb by default, less than compressor */
|
|
141
|
+
|
|
142
|
+
/* Memory usage is given by 2 ^ (windowBits + 2) + 2 ^ (memLevel + 9) */
|
|
143
|
+
int windowBits = -(int) ((compressOptions & _COMPRESSOR_MASK) >> 4), memLevel = compressOptions & 0xF;
|
|
144
|
+
|
|
145
|
+
//printf("windowBits: %d, memLevel: %d\n", windowBits, memLevel);
|
|
146
|
+
|
|
147
|
+
deflateInit2(&deflationStream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, windowBits, memLevel, Z_DEFAULT_STRATEGY);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/* Deflate and optionally reset. You must not deflate an empty string. */
|
|
151
|
+
std::string_view deflate(ZlibContext *zlibContext, std::string_view raw, bool reset) {
|
|
152
|
+
|
|
153
|
+
/* Odd place to clear this one, fix */
|
|
154
|
+
zlibContext->dynamicDeflationBuffer.clear();
|
|
155
|
+
|
|
156
|
+
deflationStream.next_in = (Bytef *) raw.data();
|
|
157
|
+
deflationStream.avail_in = (unsigned int) raw.length();
|
|
158
|
+
|
|
159
|
+
/* This buffer size has to be at least 6 bytes for Z_SYNC_FLUSH to work */
|
|
160
|
+
const int DEFLATE_OUTPUT_CHUNK = LARGE_BUFFER_SIZE;
|
|
161
|
+
|
|
162
|
+
int err;
|
|
163
|
+
do {
|
|
164
|
+
deflationStream.next_out = (Bytef *) zlibContext->deflationBuffer;
|
|
165
|
+
deflationStream.avail_out = DEFLATE_OUTPUT_CHUNK;
|
|
166
|
+
|
|
167
|
+
err = ::deflate(&deflationStream, Z_SYNC_FLUSH);
|
|
168
|
+
if (Z_OK == err && deflationStream.avail_out == 0) {
|
|
169
|
+
zlibContext->dynamicDeflationBuffer.append(zlibContext->deflationBuffer, DEFLATE_OUTPUT_CHUNK - deflationStream.avail_out);
|
|
170
|
+
continue;
|
|
171
|
+
} else {
|
|
172
|
+
break;
|
|
173
|
+
}
|
|
174
|
+
} while (true);
|
|
175
|
+
|
|
176
|
+
/* This must not change avail_out */
|
|
177
|
+
if (reset) {
|
|
178
|
+
deflateReset(&deflationStream);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (zlibContext->dynamicDeflationBuffer.length()) {
|
|
182
|
+
zlibContext->dynamicDeflationBuffer.append(zlibContext->deflationBuffer, DEFLATE_OUTPUT_CHUNK - deflationStream.avail_out);
|
|
183
|
+
|
|
184
|
+
return std::string_view((char *) zlibContext->dynamicDeflationBuffer.data(), zlibContext->dynamicDeflationBuffer.length() - 4);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/* Note: We will get an interger overflow resulting in heap buffer overflow if Z_BUF_ERROR is returned
|
|
188
|
+
* from passing 0 as avail_in. Therefore we must not deflate an empty string */
|
|
189
|
+
return {
|
|
190
|
+
zlibContext->deflationBuffer,
|
|
191
|
+
DEFLATE_OUTPUT_CHUNK - deflationStream.avail_out - 4
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
~DeflationStream() {
|
|
196
|
+
deflateEnd(&deflationStream);
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
struct InflationStream {
|
|
201
|
+
z_stream inflationStream = {};
|
|
202
|
+
|
|
203
|
+
InflationStream(CompressOptions compressOptions) {
|
|
204
|
+
/* Inflation windowBits are the top 8 bits of the 16 bit compressOptions */
|
|
205
|
+
inflateInit2(&inflationStream, -(compressOptions >> 8));
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
~InflationStream() {
|
|
209
|
+
inflateEnd(&inflationStream);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/* Zero length inflates are possible and valid */
|
|
213
|
+
std::optional<std::string_view> inflate(ZlibContext *zlibContext, std::string_view compressed, size_t maxPayloadLength, bool reset) {
|
|
214
|
+
|
|
215
|
+
#ifdef UWS_USE_LIBDEFLATE
|
|
216
|
+
if (reset) {
|
|
217
|
+
/* Try fast path first (assuming single DEFLATE block and shared compressor aka reset = true) */
|
|
218
|
+
size_t written = 0, consumed;
|
|
219
|
+
zlibContext->dynamicInflationBuffer.clear();
|
|
220
|
+
zlibContext->dynamicInflationBuffer.reserve(maxPayloadLength);
|
|
221
|
+
|
|
222
|
+
((char *)compressed.data())[0] |= 0x1; // BFINAL = 1
|
|
223
|
+
libdeflate_result res = libdeflate_deflate_decompress_ex(zlibContext->decompressor, compressed.data(), compressed.length(), zlibContext->dynamicInflationBuffer.data(), maxPayloadLength, &consumed, &written);
|
|
224
|
+
|
|
225
|
+
/* Still not entirely sure why 1 extra zero byte is optional and ignored by both zlib and libdeflate in some cases */
|
|
226
|
+
/* Minimal reproducer is load_test.c with 102 byte message size. It should be tested with Chrome at various message sizes as well. */
|
|
227
|
+
if (res == 0 && (consumed == compressed.length() || (consumed + 1 == compressed.length() && compressed[consumed] == '\0'))) {
|
|
228
|
+
return std::string_view(zlibContext->dynamicInflationBuffer.data(), written);
|
|
229
|
+
} else {
|
|
230
|
+
/* We can only end up here if the first DEFLATE block was not the last, so mark it as such */
|
|
231
|
+
((char *)compressed.data())[0] &= ~0x1; // BFINAL = 0
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
#endif
|
|
235
|
+
|
|
236
|
+
/* Save off the bytes we're about to overwrite */
|
|
237
|
+
char* tailLocation = (char*)compressed.data() + compressed.length();
|
|
238
|
+
char preTailBytes[4];
|
|
239
|
+
memcpy(preTailBytes, tailLocation, 4);
|
|
240
|
+
|
|
241
|
+
/* Append tail to chunk */
|
|
242
|
+
unsigned char tail[4] = {0x00, 0x00, 0xff, 0xff};
|
|
243
|
+
memcpy(tailLocation, tail, 4);
|
|
244
|
+
compressed = {compressed.data(), compressed.length() + 4};
|
|
245
|
+
|
|
246
|
+
/* We clear this one here, could be done better */
|
|
247
|
+
zlibContext->dynamicInflationBuffer.clear();
|
|
248
|
+
|
|
249
|
+
inflationStream.next_in = (Bytef *) compressed.data();
|
|
250
|
+
inflationStream.avail_in = (unsigned int) compressed.length();
|
|
251
|
+
|
|
252
|
+
int err;
|
|
253
|
+
do {
|
|
254
|
+
inflationStream.next_out = (Bytef *) zlibContext->inflationBuffer;
|
|
255
|
+
inflationStream.avail_out = LARGE_BUFFER_SIZE;
|
|
256
|
+
|
|
257
|
+
err = ::inflate(&inflationStream, Z_SYNC_FLUSH);
|
|
258
|
+
if (err == Z_OK && inflationStream.avail_out) {
|
|
259
|
+
break;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
zlibContext->dynamicInflationBuffer.append(zlibContext->inflationBuffer, LARGE_BUFFER_SIZE - inflationStream.avail_out);
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
} while (inflationStream.avail_out == 0 && zlibContext->dynamicInflationBuffer.length() <= maxPayloadLength);
|
|
266
|
+
|
|
267
|
+
if (reset) {
|
|
268
|
+
inflateReset(&inflationStream);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/* Restore the bytes we used for the tail */
|
|
272
|
+
memcpy(tailLocation, preTailBytes, 4);
|
|
273
|
+
|
|
274
|
+
if ((err != Z_BUF_ERROR && err != Z_OK) || zlibContext->dynamicInflationBuffer.length() > maxPayloadLength) {
|
|
275
|
+
return std::nullopt;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
if (zlibContext->dynamicInflationBuffer.length()) {
|
|
279
|
+
zlibContext->dynamicInflationBuffer.append(zlibContext->inflationBuffer, LARGE_BUFFER_SIZE - inflationStream.avail_out);
|
|
280
|
+
|
|
281
|
+
/* Let's be strict about the max size */
|
|
282
|
+
if (zlibContext->dynamicInflationBuffer.length() > maxPayloadLength) {
|
|
283
|
+
return std::nullopt;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
return std::string_view(zlibContext->dynamicInflationBuffer.data(), zlibContext->dynamicInflationBuffer.length());
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/* Let's be strict about the max size */
|
|
290
|
+
if ((LARGE_BUFFER_SIZE - inflationStream.avail_out) > maxPayloadLength) {
|
|
291
|
+
return std::nullopt;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
return std::string_view(zlibContext->inflationBuffer, LARGE_BUFFER_SIZE - inflationStream.avail_out);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
#endif
|
|
300
|
+
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
#endif // UWS_PERMESSAGEDEFLATE_H
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Authored by Alex Hultman, 2018-2026.
|
|
3
|
+
* Intellectual property of third-party.
|
|
4
|
+
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/* This module implements The PROXY Protocol v2 */
|
|
19
|
+
|
|
20
|
+
#ifndef UWS_PROXY_PARSER_H
|
|
21
|
+
#define UWS_PROXY_PARSER_H
|
|
22
|
+
|
|
23
|
+
#ifdef UWS_WITH_PROXY
|
|
24
|
+
|
|
25
|
+
namespace uWS {
|
|
26
|
+
|
|
27
|
+
struct proxy_hdr_v2 {
|
|
28
|
+
uint8_t sig[12]; /* hex 0D 0A 0D 0A 00 0D 0A 51 55 49 54 0A */
|
|
29
|
+
uint8_t ver_cmd; /* protocol version and command */
|
|
30
|
+
uint8_t fam; /* protocol family and address */
|
|
31
|
+
uint16_t len; /* number of following bytes part of the header */
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
union proxy_addr {
|
|
35
|
+
struct { /* for TCP/UDP over IPv4, len = 12 */
|
|
36
|
+
uint32_t src_addr;
|
|
37
|
+
uint32_t dst_addr;
|
|
38
|
+
uint16_t src_port;
|
|
39
|
+
uint16_t dst_port;
|
|
40
|
+
} ipv4_addr;
|
|
41
|
+
struct { /* for TCP/UDP over IPv6, len = 36 */
|
|
42
|
+
uint8_t src_addr[16];
|
|
43
|
+
uint8_t dst_addr[16];
|
|
44
|
+
uint16_t src_port;
|
|
45
|
+
uint16_t dst_port;
|
|
46
|
+
} ipv6_addr;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/* Byte swap for little-endian systems */
|
|
50
|
+
/* Todo: This functions should be shared with the one in WebSocketProtocol.h! */
|
|
51
|
+
template <typename T>
|
|
52
|
+
T _cond_byte_swap(T value) {
|
|
53
|
+
uint32_t endian_test = 1;
|
|
54
|
+
if (*((char *)&endian_test)) {
|
|
55
|
+
union {
|
|
56
|
+
T i;
|
|
57
|
+
uint8_t b[sizeof(T)];
|
|
58
|
+
} src = { value }, dst;
|
|
59
|
+
|
|
60
|
+
for (unsigned int i = 0; i < sizeof(value); i++) {
|
|
61
|
+
dst.b[i] = src.b[sizeof(value) - 1 - i];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return dst.i;
|
|
65
|
+
}
|
|
66
|
+
return value;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
struct ProxyParser {
|
|
70
|
+
private:
|
|
71
|
+
union proxy_addr addr = {};
|
|
72
|
+
/* We must always consume all PROXY v2 data, even if done, but we may not overwrite our
|
|
73
|
+
* parsed-out data one read for the first time. This property mainly fixes L4 TCP-only
|
|
74
|
+
* proxying where no HTTP-level cleaning is applied. */
|
|
75
|
+
bool done = false;
|
|
76
|
+
|
|
77
|
+
/* Default family of 0 signals no proxy address */
|
|
78
|
+
uint8_t family = 0;
|
|
79
|
+
|
|
80
|
+
public:
|
|
81
|
+
/* Returns 4 or 16 bytes source address */
|
|
82
|
+
std::string_view getSourceAddress() {
|
|
83
|
+
|
|
84
|
+
// UNSPEC family and protocol
|
|
85
|
+
if (family == 0) {
|
|
86
|
+
return {};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if ((family & 0xf0) >> 4 == 1) {
|
|
90
|
+
/* Family 1 is INET4 */
|
|
91
|
+
return {(char *) &addr.ipv4_addr.src_addr, 4};
|
|
92
|
+
} else {
|
|
93
|
+
/* Family 2 is INET6 */
|
|
94
|
+
return {(char *) &addr.ipv6_addr.src_addr, 16};
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
unsigned int getSourcePort() {
|
|
99
|
+
|
|
100
|
+
// UNSPEC family and protocol
|
|
101
|
+
if (family == 0) {
|
|
102
|
+
return {};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if ((family & 0xf0) >> 4 == 1) {
|
|
106
|
+
/* Family 1 is INET4 */
|
|
107
|
+
return addr.ipv4_addr.src_port;
|
|
108
|
+
} else {
|
|
109
|
+
/* Family 2 is INET6 */
|
|
110
|
+
return addr.ipv6_addr.src_port;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/* Returns [done, consumed] where done = false on failure */
|
|
115
|
+
std::pair<bool, unsigned int> parse(std::string_view data) {
|
|
116
|
+
|
|
117
|
+
/* We require at least four bytes to determine protocol */
|
|
118
|
+
if (data.length() < 4) {
|
|
119
|
+
return {false, 0};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/* HTTP can never start with "\r\n\r\n", but PROXY always does */
|
|
123
|
+
if (memcmp(data.data(), "\r\n\r\n", 4)) {
|
|
124
|
+
/* This is HTTP, so be done */
|
|
125
|
+
return {true, 0};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/* We assume we are parsing PROXY V2 here */
|
|
129
|
+
|
|
130
|
+
/* We require 16 bytes here */
|
|
131
|
+
if (data.length() < 16) {
|
|
132
|
+
return {false, 0};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/* Header is 16 bytes */
|
|
136
|
+
struct proxy_hdr_v2 header;
|
|
137
|
+
memcpy(&header, data.data(), 16);
|
|
138
|
+
|
|
139
|
+
if (memcmp(header.sig, "\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A", 12)) {
|
|
140
|
+
/* This is not PROXY protocol at all */
|
|
141
|
+
return {false, 0};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/* We only support version 2 */
|
|
145
|
+
if ((header.ver_cmd & 0xf0) >> 4 != 2) {
|
|
146
|
+
return {false, 0};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
//printf("Version: %d\n", (header.ver_cmd & 0xf0) >> 4);
|
|
150
|
+
//printf("Command: %d\n", (header.ver_cmd & 0x0f));
|
|
151
|
+
|
|
152
|
+
/* We get length in network byte order (todo: share this function with the rest) */
|
|
153
|
+
uint16_t hostLength = _cond_byte_swap<uint16_t>(header.len);
|
|
154
|
+
|
|
155
|
+
/* We must have all the data available */
|
|
156
|
+
if (data.length() < 16u + hostLength) {
|
|
157
|
+
return {false, 0};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/* Payload cannot be more than sizeof proxy_addr */
|
|
161
|
+
if (sizeof(proxy_addr) < hostLength) {
|
|
162
|
+
return {false, 0};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
//printf("Family: %d\n", (header.fam & 0xf0) >> 4);
|
|
166
|
+
//printf("Transport: %d\n", (header.fam & 0x0f));
|
|
167
|
+
|
|
168
|
+
/* Copy payload (only if not already done so before) */
|
|
169
|
+
if (!done) {
|
|
170
|
+
/* We have 0 family by default, and UNSPEC is 0 as well */
|
|
171
|
+
family = header.fam;
|
|
172
|
+
|
|
173
|
+
memcpy(&addr, data.data() + 16, hostLength);
|
|
174
|
+
|
|
175
|
+
done = true;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/* We consumed everything */
|
|
179
|
+
return {true, 16 + hostLength};
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
#endif
|
|
186
|
+
|
|
187
|
+
#endif // UWS_PROXY_PARSER_H
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Authored by Alex Hultman, 2018-2020.
|
|
3
|
+
* Intellectual property of third-party.
|
|
4
|
+
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/* This module implements URI query parsing and retrieval of value given key */
|
|
19
|
+
|
|
20
|
+
#ifndef UWS_QUERYPARSER_H
|
|
21
|
+
#define UWS_QUERYPARSER_H
|
|
22
|
+
|
|
23
|
+
#include <string_view>
|
|
24
|
+
|
|
25
|
+
namespace uWS {
|
|
26
|
+
|
|
27
|
+
/* Takes raw query including initial '?' sign. Will inplace decode, so input will mutate */
|
|
28
|
+
static inline std::string_view getDecodedQueryValue(std::string_view key, std::string_view rawQuery) {
|
|
29
|
+
|
|
30
|
+
/* Can't have a value without a key */
|
|
31
|
+
if (!key.length()) {
|
|
32
|
+
return {};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/* Start with the whole querystring including initial '?' */
|
|
36
|
+
std::string_view queryString = rawQuery;
|
|
37
|
+
|
|
38
|
+
/* List of key, value could be cached for repeated fetches similar to how headers are, todo! */
|
|
39
|
+
while (queryString.length()) {
|
|
40
|
+
/* Find boundaries of this statement */
|
|
41
|
+
std::string_view statement = queryString.substr(1, queryString.find('&', 1) - 1);
|
|
42
|
+
|
|
43
|
+
/* Only bother if first char of key match (early exit) */
|
|
44
|
+
if (statement.length() && statement[0] == key[0]) {
|
|
45
|
+
/* Equal sign must be present and not in the end of statement */
|
|
46
|
+
auto equality = statement.find('=');
|
|
47
|
+
if (equality != std::string_view::npos) {
|
|
48
|
+
|
|
49
|
+
std::string_view statementKey = statement.substr(0, equality);
|
|
50
|
+
std::string_view statementValue = statement.substr(equality + 1);
|
|
51
|
+
|
|
52
|
+
/* String comparison */
|
|
53
|
+
if (key == statementKey) {
|
|
54
|
+
|
|
55
|
+
/* Decode value inplace, put null at end if before length of original */
|
|
56
|
+
char *in = (char *) statementValue.data();
|
|
57
|
+
|
|
58
|
+
/* Write offset */
|
|
59
|
+
unsigned int out = 0;
|
|
60
|
+
|
|
61
|
+
/* Walk over all chars until end or null char, decoding in place */
|
|
62
|
+
for (unsigned int i = 0; i < statementValue.length() && in[i]; i++) {
|
|
63
|
+
/* Only bother with '%' */
|
|
64
|
+
if (in[i] == '%') {
|
|
65
|
+
/* Do we have enough data for two bytes hex? */
|
|
66
|
+
if (i + 2 >= statementValue.length()) {
|
|
67
|
+
return {};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/* Two bytes hex */
|
|
71
|
+
int hex1 = in[i + 1] - '0';
|
|
72
|
+
if (hex1 > 9) {
|
|
73
|
+
hex1 &= 223;
|
|
74
|
+
hex1 -= 7;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
int hex2 = in[i + 2] - '0';
|
|
78
|
+
if (hex2 > 9) {
|
|
79
|
+
hex2 &= 223;
|
|
80
|
+
hex2 -= 7;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
*((unsigned char *) &in[out]) = (unsigned char) (hex1 * 16 + hex2);
|
|
84
|
+
i += 2;
|
|
85
|
+
} else {
|
|
86
|
+
/* Is this even a rule? */
|
|
87
|
+
if (in[i] == '+') {
|
|
88
|
+
in[out] = ' ';
|
|
89
|
+
} else {
|
|
90
|
+
in[out] = in[i];
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/* We always only write one char */
|
|
95
|
+
out++;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/* If decoded string is shorter than original, put null char to stop next read */
|
|
99
|
+
if (out < statementValue.length()) {
|
|
100
|
+
in[out] = 0;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return statementValue.substr(0, out);
|
|
104
|
+
}
|
|
105
|
+
} else {
|
|
106
|
+
/* This querystring is invalid, cannot parse it */
|
|
107
|
+
return {nullptr, 0};
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
queryString.remove_prefix(statement.length() + 1);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/* Nothing found is given as nullptr, while empty string is given as some pointer to the given buffer */
|
|
115
|
+
return {nullptr, 0};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
#endif
|