-
Notifications
You must be signed in to change notification settings - Fork 625
Expand file tree
/
Copy pathUtilities.h
More file actions
275 lines (230 loc) · 9.16 KB
/
Copy pathUtilities.h
File metadata and controls
275 lines (230 loc) · 9.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
* Authored by Alex Hultman, 2018-2026.
* Intellectual property of third-party.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ADDON_UTILITIES_H
#define ADDON_UTILITIES_H
#include <openssl/ssl.h>
#include <openssl/x509.h>
#include <v8.h>
using namespace v8;
/* Getting internal pointer is different in recent V8 versions */
#if (V8_MAJOR_VERSION == 14)
void *getInternalPointer(const Local<Object> &holder) {
return holder->GetAlignedPointerFromInternalField(0, 0);
}
void setInternalPointer(const Local<Object> &holder, void *value) {
holder->SetAlignedPointerInInternalField(0, value, 0);
}
#else
void *getInternalPointer(const Local<Object> &holder) {
return holder->GetAlignedPointerFromInternalField(0);
}
void setInternalPointer(const Local<Object> &holder, void *value) {
holder->SetAlignedPointerInInternalField(0, value);
}
#endif
/* Unfortunately we _have_ to depend on Node.js crap */
#include <node.h>
MaybeLocal<Value> CallJS(Isolate *isolate, Local<Function> f, int argc, Local<Value> *argv) {
extern int calledIntoJS;
extern thread_local int insideCorkCallback;
/* All calls we do into JS are properly corked, except for res.cork, where we increase the counter explicitly */
insideCorkCallback++;
/* Slow path */
auto ret = node::MakeCallback(isolate, isolate->GetCurrentContext()->Global(), f, argc, argv, {0, 0});
insideCorkCallback--;
return ret;
}
Local<v8::ArrayBuffer> ArrayBuffer_New(Isolate *isolate, void *data, size_t length) {
std::unique_ptr<BackingStore> backingStore = ArrayBuffer::NewBackingStore(data, length, [](void* data, size_t length, void* deleter_data) {}, nullptr);
return ArrayBuffer::New(isolate, std::shared_ptr<BackingStore>(backingStore.release()));
}
Local<v8::ArrayBuffer> ArrayBuffer_NewCopy(Isolate *isolate, void *data, size_t length) {
Local<ArrayBuffer> ab = ArrayBuffer::New(isolate, length);
memcpy(ab->GetBackingStore()->Data(), data, length);
return ab;
}
struct PerSocketData {
UniquePersistent<Object> socketPf;
};
struct PerContextData {
Isolate *isolate;
UniquePersistent<Object> reqTemplate[2]; // 0 = non-SSL/SSL, 1 = Http3
UniquePersistent<Object> resTemplate[4]; // 0 = non-SSL, 1 = SSL, 2 = Http3
UniquePersistent<Object> wsTemplate[2];
/* We hold all apps until free */
std::vector<std::unique_ptr<uWS::App>> apps;
std::vector<std::unique_ptr<uWS::SSLApp>> sslApps;
};
template <class APP>
static constexpr int getAppTypeIndex() {
/* Returns 1 for SSLApp and 0 for App */
//return std::is_same<APP, uWS::SSLApp>::value;
/* Returns 2 for H3App */
if constexpr (std::is_same<APP, uWS::App>::value) {
return 0;
} else if constexpr (std::is_same<APP, uWS::SSLApp>::value) {
return 1;
} else if constexpr (std::is_same<APP, uWS::H3App>::value) {
return 2;
} else {
// why does this fail?
//static_assert(false);
}
}
static inline bool missingArguments(int length, const FunctionCallbackInfo<Value> &args) {
if (args.Length() < length) {
std::string message = "Function requires at least ";
message += std::to_string(length);
message += " arguments.";
args.GetReturnValue().Set(args.GetIsolate()->ThrowException(v8::Exception::Error(String::NewFromUtf8(args.GetIsolate(), message.c_str(), NewStringType::kNormal).ToLocalChecked())));
return true;
}
return false;
}
struct Callback {
bool invalid = false;
UniquePersistent<Function> f;
Callback(Isolate *isolate, const Local<Value> &value) {
if (!value->IsFunction()) {
invalid = true;
return;
}
f.Reset(isolate, Local<Function>::Cast(value));
}
bool isInvalid(const FunctionCallbackInfo<Value> &args) {
if (invalid) {
args.GetReturnValue().Set(args.GetIsolate()->ThrowException(v8::Exception::Error(String::NewFromUtf8(args.GetIsolate(), "Passed callback is not a valid function.", NewStringType::kNormal).ToLocalChecked())));
}
return invalid;
}
UniquePersistent<Function> &&getFunction() {
return std::move(f);
}
};
template <bool AllowStringView = false>
class NativeString {
char *data;
size_t length;
bool allocated = false;
bool invalid = false;
// Static thread-local state shared by all NativeString instances on this thread
inline static thread_local std::vector<char> pool = std::vector<char>(128 * 1024);
inline static thread_local size_t pool_offset = 0;
inline static thread_local int ref_count = 0;
static char* alloc(size_t size) {
// Ensure size is a multiple of 8
size = (size + 7) & ~7;
// Fallback for allocations larger than the remaining pool space
if (pool_offset + size > pool.size()) {
// Mark for external cleanup if using instance-based logic
// (Note: In a pure static alloc, you'd need a way to track this)
return (char*)std::malloc(size);
}
char* ptr = pool.data() + pool_offset;
pool_offset += size;
return ptr;
}
// Provided for completeness, though the "pool" doesn't actually free individual slices
static void free(char* ptr) {
if (ptr < pool.data() || ptr >= pool.data() + pool.size()) {
::free(ptr);
}
}
public:
NativeString(Isolate *isolate, const Local<Value> &value) {
if (ref_count == 0) {
pool_offset = 0; // Reset the "stack" when entering the first scope
}
ref_count++;
if (value->IsUndefined()) {
data = nullptr;
length = 0;
} else if (value->IsString()) {
Local<String> string = Local<String>::Cast(value);
/* StringView path is Latin-1, not Utf-8 */
#if (V8_MAJOR_VERSION == 14)
// Fallback
length = string->Utf8LengthV2(isolate);
data = alloc(length);
allocated = true;
string->WriteUtf8V2(isolate, data, length);
#else
// Fallback
length = string->Utf8Length(isolate);
data = alloc(length);
allocated = true;
string->WriteUtf8(isolate, data, length, nullptr, String::WriteOptions::NO_NULL_TERMINATION);
#endif
} else if (value->IsArrayBufferView()) { /* DataView or TypedArray */
Local<ArrayBufferView> arrayBufferView = Local<ArrayBufferView>::Cast(value);
auto contents = arrayBufferView->Buffer()->GetBackingStore();
length = arrayBufferView->ByteLength();
data = (char *) contents->Data() + arrayBufferView->ByteOffset();
} else if (value->IsArrayBuffer()) {
Local<ArrayBuffer> arrayBuffer = Local<ArrayBuffer>::Cast(value);
auto contents = arrayBuffer->GetBackingStore();
length = contents->ByteLength();
data = (char *) contents->Data();
} else if (value->IsSharedArrayBuffer()) {
Local<SharedArrayBuffer> arrayBuffer = Local<SharedArrayBuffer>::Cast(value);
auto contents = arrayBuffer->GetBackingStore();
length = contents->ByteLength();
data = (char *) contents->Data();
} else {
invalid = true;
}
}
bool isInvalid(const FunctionCallbackInfo<Value> &args) {
if (invalid) {
args.GetReturnValue().Set(args.GetIsolate()->ThrowException(v8::Exception::Error(String::NewFromUtf8(args.GetIsolate(), "Text and data can only be passed by String, ArrayBuffer or ArrayBufferView.", NewStringType::kNormal).ToLocalChecked())));
}
return invalid;
}
std::string_view getString() {
return {data, length};
}
~NativeString() {
ref_count--;
if (allocated) {
free(data);
}
}
};
// Utility function to extract raw certificate data
std::string extractX509PemCertificate(SSL* ssl) {
std::string pemCertificate;
if (!ssl) {
return pemCertificate;
}
// Get the peer certificate
X509* peerCertificate = SSL_get_peer_certificate(ssl);
if (!peerCertificate) {
// No peer certificate available
return pemCertificate;
}
// Convert X509 certificate to PEM format
BIO* bio = BIO_new(BIO_s_mem());
if(bio) {
if (PEM_write_bio_X509(bio, peerCertificate)) {
char* buffer;
long length = BIO_get_mem_data(bio, &buffer);
pemCertificate.assign(buffer, length);
}
BIO_free(bio);
}
// Free the peer certificate
X509_free(peerCertificate);
return pemCertificate;
}
#endif