Skip to content

Commit 4003295

Browse files
committed
Add HTTPS echo server sample
1 parent 760590d commit 4003295

File tree

11 files changed

+60
-39
lines changed

11 files changed

+60
-39
lines changed

src/http/include/scy/http/connection.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ class HTTP_API Connection : public net::SocketAdapter
7272
/// The flow is: Connection <-> ConnectionAdapter <-> Socket
7373
virtual void replaceAdapter(net::SocketAdapter* adapter);
7474

75+
/// Return true if the connection uses TLS/SSL.
76+
bool secure() const;
77+
7578
/// Return the underlying socket pointer.
7679
net::TCPSocket::Ptr& socket();
7780

src/http/include/scy/http/message.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,15 @@ class HTTP_API Message : public NVCollection
5555
/// or CHUNKED_TRANSFER_CODING.
5656
void setTransferEncoding(const std::string& transferEncoding);
5757

58-
/// Returns the transfer encoding used for this
59-
/// message.
58+
/// Returns the transfer encoding used for this message.
6059
///
6160
/// Normally, this is the value of the Transfer-Encoding
6261
/// header field. If no such field is present,
6362
/// returns IDENTITY_TRANSFER_CODING.
6463
const std::string& getTransferEncoding() const;
6564

6665
/// If flag is true, sets the Transfer-Encoding header to
67-
/// chunked. Otherwise, removes the Transfer-Encoding
68-
/// header.
66+
/// chunked. Otherwise, removes the Transfer-Encoding header.
6967
void setChunkedTransferEncoding(bool flag);
7068

7169
/// Returns true if the Transfer-Encoding header is set
@@ -91,8 +89,7 @@ class HTTP_API Message : public NVCollection
9189
void setKeepAlive(bool keepAlive);
9290

9391
/// Returns true if
94-
/// * the message has a Connection header field and its value is
95-
/// "Keep-Alive"
92+
/// * the message has a Connection header field and its value is "Keep-Alive"
9693
/// * the message is a HTTP/1.1 message and not Connection header is set
9794
/// Returns false otherwise.
9895
bool getKeepAlive() const;
@@ -129,7 +126,7 @@ class HTTP_API Message : public NVCollection
129126

130127
protected:
131128
std::string _version;
132-
129+
133130
/// Creates the Message with version HTTP/1.0.
134131
Message();
135132

src/http/include/scy/http/server.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,6 @@ class HTTP_API ServerResponder
110110
};
111111

112112

113-
114-
// -------------------------------------------------------------------
115-
//
116-
117113
/// This implementation of a ServerConnectionFactory
118114
/// is used by HTTP Server to create ServerConnection objects.
119115
class HTTP_API ServerConnectionFactory
@@ -138,9 +134,6 @@ class HTTP_API ServerConnectionFactory
138134
};
139135

140136

141-
// -------------------------------------------------------------------
142-
//
143-
144137
/// HTTP server implementation.
145138
///
146139
/// This HTTP server is not strictly standards compliant.

src/http/samples/httpechoserver/httpechoserver.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,23 @@ int main(int argc, char** argv)
1717
setenv("UV_THREADPOOL_SIZE", std::to_string(ncores).c_str(), 1);
1818
#endif
1919

20+
net::SSLManager::initNoVerifyServer();
21+
2022
// Logger::instance().add(new ConsoleChannel("debug", Level::Trace));
2123
// Logger::instance().setWriter(new AsyncLogWriter);
22-
// net::SSLManager::initNoVerifyServer();
23-
{
24-
raiseBenchmarkServer();
25-
// runMulticoreBenchmarkServers();
26-
// runMulticoreEchoServers();
27-
// rlibuv::raiseBenchmarkServer();
28-
24+
//
2925
// #if SCY_HAS_KERNEL_SOCKET_LOAD_BALANCING
30-
// runMulticoreBenchmarkServers();
26+
// runMulticoreBenchmarkServers();
3127
// #else
32-
// raiseBenchmarkServer();
28+
// raiseBenchmarkServer();
3329
// #endif
34-
}
35-
// net::SSLManager::instance().shutdown();
30+
// runMulticoreEchoServers();
31+
raiseHTTPSEchoServer();
32+
// rlibuv::raiseBenchmarkServer();
33+
34+
35+
net::SSLManager::instance().shutdown();
36+
3637
Logger::destroy();
3738
return 0;
3839
}

src/http/samples/httpechoserver/httpechoserver.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#include "scy/http/server.h"
2+
#include "scy/net/sslmanager.h"
3+
#include "scy/net/sslsocket.h"
24
#include "scy/application.h"
35

46

@@ -26,6 +28,24 @@ void raiseEchoServer()
2628
}
2729

2830

31+
void raiseHTTPSEchoServer()
32+
{
33+
http::Server srv(address, std::make_shared<net::SSLSocket>(
34+
net::SSLManager::instance().defaultServerContext()));
35+
srv.start();
36+
37+
srv.Connection += [](http::ServerConnection::Ptr conn) {
38+
conn->Payload += [](http::ServerConnection& conn, const MutableBuffer& buffer) {
39+
conn.send(bufferCast<const char*>(buffer), buffer.size());
40+
conn.close();
41+
};
42+
};
43+
44+
std::cout << "HTTPS server listening on " << address << std::endl;
45+
waitForShutdown();
46+
}
47+
48+
2949
void raiseMulticoreEchoServer()
3050
{
3151
auto loop = uv::createLoop();

src/http/src/connection.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,13 @@ void Connection::shouldSendHeader(bool flag)
217217
}
218218

219219

220+
bool Connection::secure() const
221+
{
222+
return _socket
223+
&& _socket->transport() == net::SSLTCP;
224+
}
225+
226+
220227
//
221228
// HTTP Client Connection Adapter
222229
//
@@ -267,7 +274,7 @@ ssize_t ConnectionAdapter::send(const char* data, size_t len, int flags)
267274

268275
// Send body / chunk
269276
return SocketAdapter::send(data, len, flags);
270-
// }
277+
// }
271278
// catch (std::exception& exc) {
272279
// LError("Send error: ", exc.what())
273280
//

src/http/src/parser.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -317,14 +317,14 @@ int Parser::on_headers_complete(http_parser* parser)
317317
self->onHeader(self->_lastHeaderField, self->_lastHeaderValue);
318318
}
319319

320-
/// HTTP version
320+
// HTTP version
321321
// start_line_.version(parser_.http_major, parser_.http_minor);
322322

323-
/// KeepAlive
324-
//self->_message->setKeepAlive(http_should_keep_alive(&parser) > 0);
325-
//self->_shouldKeepAlive = http_should_keep_alive(&parser) > 0;
323+
// KeepAlive
324+
// self->_message->setKeepAlive(http_should_keep_alive(&parser) > 0);
325+
// self->_shouldKeepAlive = http_should_keep_alive(&parser) > 0;
326326

327-
/// Request HTTP method
327+
// Request HTTP method
328328
if (self->_request)
329329
self->_request->setMethod(http_method_str(static_cast<http_method>(parser->method)));
330330

@@ -345,10 +345,11 @@ int Parser::on_body(http_parser* parser, const char* at, size_t len)
345345

346346
int Parser::on_message_complete(http_parser* parser)
347347
{
348-
// When http_parser finished receiving a message, signal message complete
349348
auto self = reinterpret_cast<Parser*>(parser->data);
350349
assert(self);
351350

351+
// Signal message complete when the http_parser
352+
// has finished receiving the message.
352353
self->onMessageEnd();
353354
return 0;
354355
}

src/http/src/server.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ void Server::onConnectionReady(ServerConnection& conn)
116116
void Server::onConnectionClose(ServerConnection& conn)
117117
{
118118
// LTrace("On connection closed")
119+
119120
for (auto it = _connections.begin(); it != _connections.end(); ++it) {
120121
if (it->get() == &conn) {
121122
_connections.erase(it);
@@ -157,6 +158,7 @@ ServerConnection::ServerConnection(Server& server, net::TCPSocket::Ptr socket)
157158
, _upgrade(false)
158159
{
159160
// LTrace("Create")
161+
160162
replaceAdapter(new ConnectionAdapter(this, HTTP_REQUEST));
161163
}
162164

@@ -168,7 +170,6 @@ ServerConnection::~ServerConnection()
168170
close();
169171

170172
if (_responder) {
171-
LTrace("Destroy: Responder: ", _responder)
172173
delete _responder;
173174
}
174175
}
@@ -241,8 +242,8 @@ void ServerConnection::onHeaders()
241242
_responder = _server.createResponder(*this);
242243

243244
// Upgraded connections don't receive the onHeaders callback
244-
if (_responder && !_upgrade)
245-
_responder->onHeaders(_request);
245+
if (_responder && !_upgrade)
246+
_responder->onHeaders(_request);
246247
}
247248

248249

src/http/tests/httptests.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,6 @@ int main(int argc, char** argv)
234234
expect(!conn->error().any());
235235
});
236236

237-
238237
//
239238
/// Google Drive Upload Test
240239
//

src/net/include/scy/net/sslcontext.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,7 @@ inline SSL_CTX* SSLContext::sslContext() const
303303

304304
/// Non-case sensitive conversion of a string to a VerificationMode enum.
305305
/// If verMode is illegal an ArgumentException is thrown.
306-
inline SSLContext::VerificationMode
307-
convertVerificationMode(const std::string& vMode)
306+
inline SSLContext::VerificationMode convertVerificationMode(const std::string& vMode)
308307
{
309308
std::string mode = util::toLower(vMode);
310309
SSLContext::VerificationMode verMode = SSLContext::VERIFY_STRICT;

src/net/samples/echoserver/echoserver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ int main(int argc, char** argv)
3434
srvs.udp.start("0.0.0.0", UdpPort);
3535

3636
std::cout << "TCP Lerver listening on " << srvs.tcp.server->address() << std::endl;
37-
std::cout << "LSL Server listening on " << srvs.ssl.server->address() << std::endl;
37+
std::cout << "SSL Server listening on " << srvs.ssl.server->address() << std::endl;
3838
std::cout << "UDP Lerver listening on " << srvs.udp.server->address() << std::endl;
3939

4040
waitForShutdown([&](void*) {

0 commit comments

Comments
 (0)