forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHTTPServer.mm
682 lines (621 loc) · 30.2 KB
/
HTTPServer.mm
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
/*
* Copyright (C) 2019-2022 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#import "config.h"
#import "HTTPServer.h"
#import "Utilities.h"
#import <WebKit/WKWebsiteDataStorePrivate.h>
#import <WebKit/_WKWebsiteDataStoreConfiguration.h>
#import <wtf/BlockPtr.h>
#import <wtf/CallbackAggregator.h>
#import <wtf/CompletionHandler.h>
#import <wtf/RetainPtr.h>
#import <wtf/StdLibExtras.h>
#import <wtf/ThreadSafeRefCounted.h>
#import <wtf/text/Base64.h>
#import <wtf/text/MakeString.h>
#import <wtf/text/ParsingUtilities.h>
#import <wtf/text/StringBuilder.h>
#import <wtf/text/WTFString.h>
namespace TestWebKitAPI {
struct HTTPServer::RequestData : public ThreadSafeRefCounted<RequestData, WTF::DestructionThread::MainRunLoop> {
RequestData(std::initializer_list<std::pair<String, HTTPResponse>> responses)
: requestMap([](std::initializer_list<std::pair<String, HTTPResponse>> list) {
HashMap<String, HTTPResponse> map;
for (auto& pair : list)
map.add(pair.first, pair.second);
return map;
}(responses)) { }
size_t requestCount { 0 };
HashMap<String, HTTPResponse> requestMap;
Vector<Connection> connections;
Vector<CoroutineHandle<Task::promise_type>> coroutineHandles;
};
static RetainPtr<nw_protocol_definition_t> proxyDefinition(HTTPServer::Protocol protocol)
{
return adoptNS(nw_framer_create_definition("HttpsProxy", NW_FRAMER_CREATE_FLAGS_DEFAULT, [protocol] (nw_framer_t framer) -> nw_framer_start_result_t {
__block enum class State {
WillRequestCredentials,
DidRequestCredentials,
WillNotRequestCredentials,
PassThrough
} state { protocol == HTTPServer::Protocol::HttpsProxyWithAuthentication ? State::WillRequestCredentials : State::WillNotRequestCredentials };
nw_framer_set_input_handler(framer, ^size_t(nw_framer_t framer) {
__block RetainPtr<nw_framer_t> retainedFramer = framer;
nw_framer_pass_through_output(framer);
nw_framer_parse_input(framer, 1, std::numeric_limits<uint32_t>::max(), nullptr, ^size_t(uint8_t* buffer, size_t bufferLength, bool isComplete) {
switch (state) {
case State::WillRequestCredentials: {
const char* challengeResponse =
"HTTP/1.1 407 Proxy Authentication Required\r\n"
"Proxy-Authenticate: Basic realm=\"testrealm\"\r\n"
"Content-Length: 0\r\n"
"\r\n";
auto response = adoptNS(dispatch_data_create(challengeResponse, strlen(challengeResponse), nullptr, nullptr));
nw_framer_write_output_data(retainedFramer.get(), response.get());
state = State::DidRequestCredentials;
break;
}
case State::DidRequestCredentials:
EXPECT_TRUE(strnstr(byteCast<char>(buffer), "Proxy-Authorization: Basic dGVzdHVzZXI6dGVzdHBhc3N3b3Jk\r\n", bufferLength));
FALLTHROUGH;
case State::WillNotRequestCredentials: {
const char* negotiationResponse = ""
"HTTP/1.1 200 Connection Established\r\n"
"Connection: close\r\n\r\n";
auto response = adoptNS(dispatch_data_create(negotiationResponse, strlen(negotiationResponse), nullptr, nullptr));
nw_framer_write_output_data(retainedFramer.get(), response.get());
nw_framer_mark_ready(retainedFramer.get());
state = State::PassThrough;
break;
}
case State::PassThrough:
nw_framer_deliver_input_no_copy(retainedFramer.get(), bufferLength, adoptNS(nw_framer_message_create(retainedFramer.get())).get(), isComplete);
return 0;
}
return bufferLength;
});
return 0;
});
return nw_framer_start_result_will_mark_ready;
}));
}
static bool shouldDisableTLS(HTTPServer::Protocol protocol)
{
switch (protocol) {
case HTTPServer::Protocol::Http:
case HTTPServer::Protocol::HttpsProxy:
case HTTPServer::Protocol::HttpsProxyWithAuthentication:
return true;
case HTTPServer::Protocol::Https:
case HTTPServer::Protocol::HttpsWithLegacyTLS:
case HTTPServer::Protocol::Http2:
return false;
}
}
RetainPtr<nw_parameters_t> HTTPServer::listenerParameters(Protocol protocol, CertificateVerifier&& verifier, RetainPtr<SecIdentityRef>&& customTestIdentity, std::optional<uint16_t> port)
{
if (protocol != Protocol::Http && !customTestIdentity)
customTestIdentity = testIdentity();
auto configureTLS = [protocol, verifier = WTFMove(verifier), testIdentity = WTFMove(customTestIdentity)] (nw_protocol_options_t protocolOptions) mutable {
auto options = adoptNS(nw_tls_copy_sec_protocol_options(protocolOptions));
auto identity = adoptNS(sec_identity_create(testIdentity.get()));
sec_protocol_options_set_local_identity(options.get(), identity.get());
if (protocol == Protocol::HttpsWithLegacyTLS)
sec_protocol_options_set_max_tls_protocol_version(options.get(), tls_protocol_version_TLSv10);
if (verifier) {
sec_protocol_options_set_peer_authentication_required(options.get(), true);
sec_protocol_options_set_verify_block(options.get(), makeBlockPtr([verifier = WTFMove(verifier)](sec_protocol_metadata_t metadata, sec_trust_t trust, sec_protocol_verify_complete_t completion) {
verifier(metadata, trust, completion);
}).get(), dispatch_get_main_queue());
}
if (protocol == Protocol::Http2)
sec_protocol_options_add_tls_application_protocol(options.get(), "h2");
};
auto configureTLSBlock = shouldDisableTLS(protocol) ? makeBlockPtr(NW_PARAMETERS_DISABLE_PROTOCOL) : makeBlockPtr(WTFMove(configureTLS));
auto parameters = adoptNS(nw_parameters_create_secure_tcp(configureTLSBlock.get(), NW_PARAMETERS_DEFAULT_CONFIGURATION));
if (port)
nw_parameters_set_local_endpoint(parameters.get(), nw_endpoint_create_host("::", makeString(*port).utf8().data()));
if (protocol == Protocol::HttpsProxy || protocol == Protocol::HttpsProxyWithAuthentication) {
auto stack = adoptNS(nw_parameters_copy_default_protocol_stack(parameters.get()));
auto options = adoptNS(nw_framer_create_options(proxyDefinition(protocol).get()));
nw_protocol_stack_prepend_application_protocol(stack.get(), options.get());
auto tlsOptions = adoptNS(nw_tls_create_options());
configureTLS(tlsOptions.get());
nw_protocol_stack_prepend_application_protocol(stack.get(), tlsOptions.get());
}
return parameters;
}
static void startListening(nw_listener_t listener)
{
__block bool ready = false;
nw_listener_set_state_changed_handler(listener, ^(nw_listener_state_t state, nw_error_t error) {
ASSERT_UNUSED(error, !error);
if (state == nw_listener_state_ready)
ready = true;
});
nw_listener_start(listener);
Util::run(&ready);
}
void HTTPServer::cancel()
{
__block bool cancelled = false;
nw_listener_set_state_changed_handler(m_listener.get(), ^(nw_listener_state_t state, nw_error_t error) {
ASSERT_UNUSED(error, !error);
if (state == nw_listener_state_cancelled)
cancelled = true;
});
nw_listener_cancel(m_listener.get());
Util::run(&cancelled);
m_listener = nullptr;
bool done { false };
terminateAllConnections([&] {
done = true;
});
Util::run(&done);
}
void HTTPServer::terminateAllConnections(CompletionHandler<void()>&& completionHandler)
{
auto aggregator = CallbackAggregator::create(WTFMove(completionHandler));
for (auto& connection : std::exchange(m_requestData->connections, { }))
connection.terminate([aggregator] { });
}
HTTPServer::HTTPServer(std::initializer_list<std::pair<String, HTTPResponse>> responses, Protocol protocol, CertificateVerifier&& verifier, RetainPtr<SecIdentityRef>&& identity, std::optional<uint16_t> port)
: m_requestData(adoptRef(*new RequestData(responses)))
, m_listener(adoptNS(nw_listener_create(listenerParameters(protocol, WTFMove(verifier), WTFMove(identity), port).get())))
, m_protocol(protocol)
{
nw_listener_set_queue(m_listener.get(), dispatch_get_main_queue());
nw_listener_set_new_connection_handler(m_listener.get(), makeBlockPtr([requestData = m_requestData](nw_connection_t connection) {
requestData->connections.append(Connection(connection));
nw_connection_set_queue(connection, dispatch_get_main_queue());
nw_connection_start(connection);
respondToRequests(Connection(connection), requestData);
}).get());
startListening(m_listener.get());
}
HTTPServer::HTTPServer(Function<void(Connection)>&& connectionHandler, Protocol protocol)
: m_requestData(adoptRef(*new RequestData({ })))
, m_listener(adoptNS(nw_listener_create(listenerParameters(protocol, nullptr, nullptr, { }).get())))
, m_protocol(protocol)
{
nw_listener_set_queue(m_listener.get(), dispatch_get_main_queue());
nw_listener_set_new_connection_handler(m_listener.get(), makeBlockPtr([requestData = m_requestData, connectionHandler = WTFMove(connectionHandler)] (nw_connection_t connection) {
requestData->connections.append(Connection(connection));
nw_connection_set_queue(connection, dispatch_get_main_queue());
nw_connection_start(connection);
connectionHandler(Connection(connection));
}).get());
startListening(m_listener.get());
}
HTTPServer::HTTPServer(UseCoroutines, WTF::Function<Task(Connection)>&& connectionHandler, Protocol protocol)
: m_requestData(adoptRef(*new RequestData({ })))
, m_listener(adoptNS(nw_listener_create(listenerParameters(protocol, nullptr, nullptr, { }).get())))
, m_protocol(protocol)
{
nw_listener_set_queue(m_listener.get(), dispatch_get_main_queue());
nw_listener_set_new_connection_handler(m_listener.get(), makeBlockPtr([requestData = m_requestData, connectionHandler = WTFMove(connectionHandler)] (nw_connection_t connection) {
requestData->connections.append(Connection(connection));
nw_connection_set_queue(connection, dispatch_get_main_queue());
nw_connection_start(connection);
requestData->coroutineHandles.append(connectionHandler(Connection(connection)).handle);
}).get());
startListening(m_listener.get());
}
HTTPServer::~HTTPServer() = default;
void HTTPServer::addResponse(String&& path, HTTPResponse&& response)
{
RELEASE_ASSERT(!m_requestData->requestMap.contains(path));
m_requestData->requestMap.add(WTFMove(path), WTFMove(response));
}
void HTTPServer::setResponse(String&& path, HTTPResponse&& response)
{
ASSERT(m_requestData->requestMap.contains(path));
m_requestData->requestMap.set(WTFMove(path), WTFMove(response));
}
void HTTPServer::respondWithChallengeThenOK(Connection connection)
{
connection.receiveHTTPRequest([connection] (Vector<char>&&) {
constexpr auto challengeHeader =
"HTTP/1.1 401 Unauthorized\r\n"
"Date: Sat, 23 Mar 2019 06:29:01 GMT\r\n"
"Content-Length: 0\r\n"
"WWW-Authenticate: Basic realm=\"testrealm\"\r\n\r\n"_s;
connection.send(challengeHeader, [connection] {
connection.receiveHTTPRequest([connection] (Vector<char>&&) {
connection.send(
"HTTP/1.1 200 OK\r\n"
"Content-Length: 34\r\n\r\n"
"<script>alert('success!')</script>"_s, [connection] {
respondWithChallengeThenOK(connection);
}
);
});
});
});
}
void HTTPServer::respondWithOK(Connection connection)
{
connection.receiveHTTPRequest([connection] (Vector<char>&&) {
connection.send(
"HTTP/1.1 200 OK\r\n"
"Content-Length: 34\r\n\r\n"
"<script>alert('success!')</script>"_s
);
});
}
size_t HTTPServer::totalConnections() const
{
return m_requestData->connections.size();
}
size_t HTTPServer::totalRequests() const
{
return m_requestData->requestCount;
}
static ASCIILiteral statusText(unsigned statusCode)
{
switch (statusCode) {
case 101:
return "Switching Protocols"_s;
case 200:
return "OK"_s;
case 204:
return "No Content"_s;
case 301:
return "Moved Permanently"_s;
case 302:
return "Found"_s;
case 303:
return "See Other"_s;
case 304:
return "Not Modified"_s;
case 404:
return "Not Found"_s;
case 503:
return "Service Unavailable"_s;
}
ASSERT_NOT_REACHED();
return "Unknown Status Code"_s;
}
static Vector<uint8_t> toUTF8Vector(const String& string)
{
Vector<uint8_t> result;
string.tryGetUTF8([&](std::span<const char8_t> utf8) {
result.append(byteCast<uint8_t>(utf8));
return true;
});
return result;
}
static Vector<uint8_t> serialize304Response(const HashMap<String, String>& headerFields)
{
constexpr int statusCode = 304;
StringBuilder responseBuilder;
responseBuilder.append("HTTP/1.1 "_s, statusCode, ' ', statusText(statusCode), "\r\n"_s);
for (auto& pair : headerFields)
responseBuilder.append(pair.key, ": "_s, pair.value, "\r\n"_s);
responseBuilder.append("\r\n"_s);
return toUTF8Vector(responseBuilder.toString());
}
String HTTPServer::parsePath(const Vector<char>& request)
{
if (!request.size())
return { };
const char* getPathPrefix = "GET ";
const char* postPathPrefix = "POST ";
const char* pathSuffix = " HTTP/1.1\r\n";
const char* pathEnd = strnstr(request.data(), pathSuffix, request.size());
ASSERT_WITH_MESSAGE(pathEnd, "HTTPServer assumes request is HTTP 1.1");
size_t pathPrefixLength = 0;
if (!memcmp(request.data(), getPathPrefix, strlen(getPathPrefix)))
pathPrefixLength = strlen(getPathPrefix);
else if (!memcmp(request.data(), postPathPrefix, strlen(postPathPrefix)))
pathPrefixLength = strlen(postPathPrefix);
ASSERT_WITH_MESSAGE(pathPrefixLength, "HTTPServer assumes request is GET or POST");
size_t pathLength = pathEnd - request.data() - pathPrefixLength;
return request.subspan(pathPrefixLength, pathLength);
}
String HTTPServer::parseBody(const Vector<char>& request)
{
const char* headerEndBytes = "\r\n\r\n";
const char* headerEnd = strnstr(request.data(), headerEndBytes, request.size()) + strlen(headerEndBytes);
size_t headerLength = headerEnd - request.data();
return request.subspan(headerLength);
}
static bool isConditionalRequest(std::span<const char> request)
{
auto endOfHeaders = find(request, "\r\n\r\n"_span);
if (endOfHeaders == notFound)
return false;
auto headers = request.first(endOfHeaders);
constexpr auto newLine = "\r\n"_span;
while (!headers.empty()) {
if (spanHasPrefixIgnoringASCIICase(headers, "If-None-Match:"_span))
return true;
size_t endOfLine = find(headers, newLine);
if (endOfLine == notFound)
return false;
skip(headers, endOfLine + newLine.size());
}
return false;
}
void HTTPServer::respondToRequests(Connection connection, Ref<RequestData> requestData)
{
connection.receiveHTTPRequest([connection, requestData] (Vector<char>&& request) mutable {
if (!request.size())
return;
requestData->requestCount++;
auto path = parsePath(request);
ASSERT_WITH_MESSAGE(requestData->requestMap.contains(path), "This HTTPServer does not know how to respond to a request for %s", path.utf8().data());
auto response = requestData->requestMap.get(path);
if (response.shouldRespondWith304ToConditionalRequests) {
if (isConditionalRequest(request.span())) {
return connection.send(serialize304Response(response.headerFieldsFor304), [connection, requestData] {
respondToRequests(connection, requestData);
});
}
}
switch (response.behavior) {
case HTTPResponse::Behavior::TerminateConnectionAfterReceivingResponse:
return connection.terminate();
case HTTPResponse::Behavior::SendResponseNormally:
return connection.send(response.serialize(), [connection, requestData] {
respondToRequests(connection, requestData);
});
case HTTPResponse::Behavior::NeverSendResponse:
return respondToRequests(connection, requestData);
}
});
}
uint16_t HTTPServer::port() const
{
return nw_listener_get_port(m_listener.get());
}
const char* HTTPServer::scheme() const
{
const char* scheme = nullptr;
switch (m_protocol) {
case Protocol::Http:
scheme = "http";
break;
case Protocol::Https:
case Protocol::HttpsWithLegacyTLS:
case Protocol::Http2:
scheme = "https";
break;
case Protocol::HttpsProxy:
case Protocol::HttpsProxyWithAuthentication:
RELEASE_ASSERT_NOT_REACHED();
}
return scheme;
}
String HTTPServer::origin() const
{
return [NSString stringWithFormat:@"%s://127.0.0.1:%d", scheme(), port()];
}
NSURLRequest *HTTPServer::request(StringView path) const
{
return [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%s://127.0.0.1:%d%@", scheme(), port(), path.createNSString().get()]]];
}
NSURLRequest *HTTPServer::requestWithLocalhost(StringView path) const
{
return [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%s://localhost:%d%@", scheme(), port(), path.createNSString().get()]]];
}
WKWebViewConfiguration *HTTPServer::httpsProxyConfiguration() const
{
auto storeConfiguration = adoptNS([[_WKWebsiteDataStoreConfiguration alloc] initNonPersistentConfiguration]);
[storeConfiguration setHTTPSProxy:[NSURL URLWithString:[NSString stringWithFormat:@"https://127.0.0.1:%d/", port()]]];
auto viewConfiguration = adoptNS([WKWebViewConfiguration new]);
[viewConfiguration setWebsiteDataStore:adoptNS([[WKWebsiteDataStore alloc] _initWithConfiguration:storeConfiguration.get()]).get()];
return viewConfiguration.autorelease();
}
Vector<uint8_t> HTTPResponse::bodyFromString(const String& string)
{
return toUTF8Vector(string);
}
Vector<uint8_t> HTTPResponse::serialize(IncludeContentLength includeContentLength) const
{
StringBuilder responseBuilder;
responseBuilder.append("HTTP/1.1 "_s, statusCode, ' ', statusText(statusCode), "\r\n"_s);
if (includeContentLength == IncludeContentLength::Yes)
responseBuilder.append("Content-Length: "_s, body.size(), "\r\n"_s);
for (auto& pair : headerFields)
responseBuilder.append(pair.key, ": "_s, pair.value, "\r\n"_s);
responseBuilder.append("\r\n"_s);
auto bytesToSend = toUTF8Vector(responseBuilder.toString());
bytesToSend.appendVector(body);
return bytesToSend;
}
void H2::Connection::send(Frame&& frame, CompletionHandler<void()>&& completionHandler) const
{
auto frameType = frame.type();
auto sendFrame = [tlsConnection = m_tlsConnection, frame = WTFMove(frame), completionHandler = WTFMove(completionHandler)] () mutable {
// https://http2.github.io/http2-spec/#rfc.section.4.1
Vector<uint8_t> bytes;
constexpr size_t frameHeaderLength = 9;
bytes.reserveInitialCapacity(frameHeaderLength + frame.payload().size());
bytes.append(frame.payload().size() >> 16);
bytes.append(frame.payload().size() >> 8);
bytes.append(frame.payload().size() >> 0);
bytes.append(static_cast<uint8_t>(frame.type()));
bytes.append(frame.flags());
bytes.append(frame.streamID() >> 24);
bytes.append(frame.streamID() >> 16);
bytes.append(frame.streamID() >> 8);
bytes.append(frame.streamID() >> 0);
bytes.appendVector(frame.payload());
tlsConnection.send(WTFMove(bytes), WTFMove(completionHandler));
};
if (m_sendServerConnectionPreface && frameType != Frame::Type::Settings) {
// https://http2.github.io/http2-spec/#rfc.section.3.5
m_sendServerConnectionPreface = false;
send(Frame(Frame::Type::Settings, 0, 0, { }), WTFMove(sendFrame));
} else
sendFrame();
}
void H2::Connection::receive(CompletionHandler<void(Frame&&)>&& completionHandler) const
{
if (m_expectClientConnectionPreface) {
// https://http2.github.io/http2-spec/#rfc.section.3.5
constexpr size_t clientConnectionPrefaceLength = 24;
if (m_receiveBuffer.size() < clientConnectionPrefaceLength) {
m_tlsConnection.receiveBytes([this, protectedThis = Ref { *this }, completionHandler = WTFMove(completionHandler)] (Vector<uint8_t>&& bytes) mutable {
m_receiveBuffer.appendVector(bytes);
receive(WTFMove(completionHandler));
});
return;
}
ASSERT(!memcmp(m_receiveBuffer.data(), "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", clientConnectionPrefaceLength));
m_receiveBuffer.remove(0, clientConnectionPrefaceLength);
m_expectClientConnectionPreface = false;
return receive(WTFMove(completionHandler));
}
// https://http2.github.io/http2-spec/#rfc.section.4.1
constexpr size_t frameHeaderLength = 9;
if (m_receiveBuffer.size() >= frameHeaderLength) {
uint32_t payloadLength = (static_cast<uint32_t>(m_receiveBuffer[0]) << 16)
+ (static_cast<uint32_t>(m_receiveBuffer[1]) << 8)
+ (static_cast<uint32_t>(m_receiveBuffer[2]) << 0);
if (m_receiveBuffer.size() >= frameHeaderLength + payloadLength) {
auto type = static_cast<Frame::Type>(m_receiveBuffer[3]);
auto flags = m_receiveBuffer[4];
auto streamID = (static_cast<uint32_t>(m_receiveBuffer[5]) << 24)
+ (static_cast<uint32_t>(m_receiveBuffer[6]) << 16)
+ (static_cast<uint32_t>(m_receiveBuffer[7]) << 8)
+ (static_cast<uint32_t>(m_receiveBuffer[8]) << 0);
Vector<uint8_t> payload;
payload.append(m_receiveBuffer.subspan(frameHeaderLength, payloadLength));
m_receiveBuffer.remove(0, frameHeaderLength + payloadLength);
return completionHandler(Frame(type, flags, streamID, WTFMove(payload)));
}
}
m_tlsConnection.receiveBytes([this, protectedThis = Ref { *this }, completionHandler = WTFMove(completionHandler)] (Vector<uint8_t>&& bytes) mutable {
m_receiveBuffer.appendVector(bytes);
receive(WTFMove(completionHandler));
});
}
Vector<uint8_t> HTTPServer::testCertificate()
{
// Certificate and private key were generated by running this command:
// openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes
// and entering this information:
/*
Country Name (2 letter code) []:US
State or Province Name (full name) []:New Mexico
Locality Name (eg, city) []:Santa Fe
Organization Name (eg, company) []:Self
Organizational Unit Name (eg, section) []:Myself
Common Name (eg, fully qualified host name) []:Me
Email Address []:[email protected]
*/
String pemEncodedCertificate(""
"MIIFgDCCA2gCCQCKHiPRU5MQuDANBgkqhkiG9w0BAQsFADCBgTELMAkGA1UEBhMC"
"VVMxEzARBgNVBAgMCk5ldyBNZXhpY28xETAPBgNVBAcMCFNhbnRhIEZlMQ0wCwYD"
"VQQKDARTZWxmMQ8wDQYDVQQLDAZNeXNlbGYxCzAJBgNVBAMMAk1lMR0wGwYJKoZI"
"hvcNAQkBFg5tZUBleGFtcGxlLmNvbTAeFw0xOTAzMjMwNTUwMTRaFw0yMDAzMjIw"
"NTUwMTRaMIGBMQswCQYDVQQGEwJVUzETMBEGA1UECAwKTmV3IE1leGljbzERMA8G"
"A1UEBwwIU2FudGEgRmUxDTALBgNVBAoMBFNlbGYxDzANBgNVBAsMBk15c2VsZjEL"
"MAkGA1UEAwwCTWUxHTAbBgkqhkiG9w0BCQEWDm1lQGV4YW1wbGUuY29tMIIC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U"
"2fx+M8DfDcwFo7tSgLxSK3GCFpxPfiDt6Qk8c9tQn5S1gY3t6LJuwVCFwUIXlNkB"
"JD7+cZ1Z/tCrEhzj3YCk0uUU8CifoU+4FG+HGFP+SPztsYE055mSj3+Esh+oyoVB"
"gBH90sE2T1i0eNI8f61oSgwYFeHsf7fC71XEXLFR+GwNdmwqlmwlDZEpTu7BoNN+"
"q7+Tfk1MRkJlL1PH6Yu/IPhZiNh4tyIqDOtlYfzp577A+OUU+q5PPRFRIsqheOxt"
"mNlHx4Uzd4U3ITfmogJazjqwYO2viBZY4jUQmyZs75eH/jiUFHWRsha3AdnW5LWa"
"G3PFnYbW8urH0NSJG/W+/9DA+Y7Aa0cs4TPpuBGZ0NU1W94OoCMo4lkO6H/y6Leu"
"3vjZD3y9kZk7mre9XHwkI8MdK5s="_s);
auto decodedCertificate = base64Decode(pemEncodedCertificate);
return WTFMove(*decodedCertificate);
}
Vector<uint8_t> HTTPServer::testPrivateKey()
{
String pemEncodedPrivateKey(""
"MIIJQgIBADANBgkqhkiG9w0BAQEFAASCCSwwggkoAgEAAoICAQDeuE3hI+DxVj8+"
"0YM0pjdP0khKBvLxgYwnHS875hkVmbcbd+xNSzK8PIQfCqnlpsJlEH8HLU/BaQ2t"
"/Gf9c/w/Tfgk+UTqKtWK4BhCupSHqTtlHwKk4zkYRxFmABQkYZDA2U6QTBoecwZz"
"xKwe6uAM+HlcGmBsPK13sEm4HU2gj8omaFxqOQC5Xe9qtL648uh88c2p75wvyE2e"
"NlDAdZqs/jmSzh4Fw4Pu3CrG4edWb8VYSuljxazmzUys9OFR0NqGMQx/5h8Mn0ov"
"zLfTjJLwnx94LFh9wfgG+Au0mYE0cmCPd40P8QxmMmDPYWwGOsmTyQoPfVzD7han"
"xQXiyRZcJ/jr6Y04enXc95IJkvIXhkAdrNRWScM9xzZg2tZVXWLqfdPXmQjRPlnt"
"uy12YAu5KQTZy4lJU07qBH5mxVFOd65DHBZNTQw0SEA8U5fYGEY/l6KyAR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