-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathHTTPSServer.cpp
More file actions
137 lines (116 loc) · 5.83 KB
/
HTTPSServer.cpp
File metadata and controls
137 lines (116 loc) · 5.83 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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <iostream>
#include <atomic>
#include <SFNUL.hpp>
int main() {
// Create our TCP listener socket.
auto listener = sfn::TcpListener::Create();
// Listen on 0.0.0.0:443
listener->Listen( sfn::Endpoint{ sfn::IpAddress{ "0.0.0.0" }, 443 } );
// Start 3 network processing threads.
sfn::Start( 3 );
// Just to make our lives easier...
typedef sfn::TlsConnection<sfn::TcpSocket, sfn::TlsEndpointType::Server, sfn::TlsVerificationType::None> Connection;
// A place to store all active connections.
std::deque<Connection::Ptr> connections;
const std::string certificate_string =
"-----BEGIN CERTIFICATE-----\r\n"
"MIIDPjCCAiagAwIBAgIBAjANBgkqhkiG9w0BAQUFADBFMQswCQYDVQQGEwJGUjEO\r\n"
"MAwGA1UEBxMFUGFyaXMxDjAMBgNVBAoTBVh5U1NMMRYwFAYDVQQDEw1YeVNTTCBU\r\n"
"ZXN0IENBMB4XDTA3MDcwNzA1MDEyOVoXDTA4MDcwNjA1MDEyOVowMTELMAkGA1UE\r\n"
"BhMCRlIxDjAMBgNVBAoTBVh5U1NMMRIwEAYDVQQDEwlsb2NhbGh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6b4a82oa3/2lK\r\n"
"pu1b4v9XBVZLIX5hiSXFlQPdQADKsNVwMQJrlL6Np3gKyL+VUe3m6VYhyRYvV8m9\r\n"
"p2fm5so0NwKBgD7vXFUY8/6WwWBOLK1+sLzmfauXgzGKcn37DQj4RvMIo0xga9OC\r\n"
"JTh0lqxIwR2IEqzUUwajt0XwXQEscXUiwhrkCHa1ci1ef3Xnij06JNBcyYrqqZFq\r\n"
"d4zp+E6h5oLBgCGkbFgimrH9evhM6GJqDjqMwxqmEB46/Di3UrZEPOI5AoGBALI8\r\n"
"IJTSDG7D+jWN1rYLFtnL0SZT96sRfT37Sf5M59ClIQ+r/P1ZrEAVj0t+x1nRmS2h\r\n"
"4eZrVs10veLoDcNYAzdhJDNAxE+bM5aepfFyCgIGaW/OTNp4uM7mmEygtAcmaZp+\r\n"
"+4Ibq7Gi/cXweLcvIdQXZzyTScvq5yYne+O7O7gBAoGAFIBIwAlLrcz1QkHURrPL\r\n"
"a/Y1bDTgKbM3FR9ase4ql38LaKThAq7SxE6v0qeQODHESGMObuVeGVbXY9TjbI5G\r\n"
"y3kjcRprUJ+Lgo8Wf+jO36I0FivN3xPK+duqR0QKXT4bYWj8RHg1EK/trbhY0Sw4\r\n"
"wbMZGrDlydImuNtktgxojIo=\r\n"
"-----END PRIVATE KEY-----\r\n";
// Load the certificate and key
auto certificate = sfn::TlsCertificate::Create( certificate_string );
auto key = sfn::TlsKey::Create( key_string );
std::atomic_bool exit{ false };
std::cout << "Press ENTER to exit.\n";
// Don't use sfn::Thread for your own projects, it is not what you think it is.
sfn::Thread exit_handler( [&]() { std::cin.get(); exit = true; } );
while( !exit ) {
{
Connection::Ptr connection;
// Dequeue any connections from the listener.
while( ( connection = listener->GetPendingConnection<Connection>() ) ) {
// Set the server certificate and key pair.
connection->SetCertificateKeyPair( certificate, key );
// Turn of connection lingering.
connection->SetLinger( 0 );
char response[] =
"HTTP/1.1 200 OK\r\n"
"Server: SFNUL HTTPS Server\r\n"
"Content-Type: text/html; charset=UTF-8\r\n"
"Connection: close\r\n\r\n"
"<html><head><title>SFNUL HTTPS Server Page</title></head>"
"<body>SFNUL HTTPS Server Document</body></html>\r\n\r\n";
// Send the HTTP response.
connection->Send( response, sizeof( response ) - 1 );
// Shutdown the connection for sending.
connection->Shutdown();
// Store the connection.
connections.push_back( connection );
}
}
for( auto connection_iter = connections.begin(); connection_iter != connections.end(); ) {
// Remove and thereby close all active connections that have been
// remotely shut down and don't have data left to send.
if( ( *connection_iter )->RemoteHasShutdown() && !( *connection_iter )->BytesToSend() ) {
//( *connection_iter )->Shutdown();
connection_iter = connections.erase( connection_iter );
}
else {
++connection_iter;
}
}
}
// Close the listener socket.
listener->Close();
// Stop all network processing threads.
sfn::Stop();
return 0;
}