2020-03-04 07:54:04 -06:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* 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/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-03-19 10:54:28 -05:00
|
|
|
#include <memory>
|
2020-03-04 07:54:04 -06:00
|
|
|
#include <net/Socket.hpp>
|
|
|
|
|
2020-03-19 10:54:28 -05:00
|
|
|
/**
|
|
|
|
* Implementation that builds a websocket like protocol from many
|
|
|
|
* individual proxied HTTP requests back to back.
|
|
|
|
*
|
2020-04-18 12:40:59 -05:00
|
|
|
* we use a trivial framing: [T(ext)|B(inary)]<hex-serial->\n<hex-length>\n<content>\n
|
2020-03-19 10:54:28 -05:00
|
|
|
*/
|
2020-03-04 07:54:04 -06:00
|
|
|
class ProxyProtocolHandler : public ProtocolHandlerInterface
|
|
|
|
{
|
|
|
|
public:
|
2020-04-18 12:40:59 -05:00
|
|
|
ProxyProtocolHandler() :
|
|
|
|
_inSerial(0),
|
|
|
|
_outSerial(0)
|
|
|
|
{
|
|
|
|
}
|
2020-03-04 07:54:04 -06:00
|
|
|
|
2020-03-19 10:54:28 -05:00
|
|
|
virtual ~ProxyProtocolHandler() { }
|
2020-03-04 07:54:04 -06:00
|
|
|
|
|
|
|
/// Will be called exactly once by setHandler
|
2020-05-09 13:41:40 -05:00
|
|
|
void onConnect(const std::shared_ptr<StreamSocket>& /* socket */) override {}
|
2020-03-04 07:54:04 -06:00
|
|
|
|
|
|
|
/// Called after successful socket reads.
|
2020-03-19 10:54:28 -05:00
|
|
|
void handleIncomingMessage(SocketDisposition &/* disposition */) override;
|
2020-03-04 07:54:04 -06:00
|
|
|
|
|
|
|
int getPollEvents(std::chrono::steady_clock::time_point /* now */,
|
2020-03-20 15:45:38 -05:00
|
|
|
int64_t &/* timeoutMaxMs */) override;
|
2020-03-04 07:54:04 -06:00
|
|
|
|
|
|
|
void checkTimeout(std::chrono::steady_clock::time_point /* now */) override
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-03-07 11:57:13 -06:00
|
|
|
void performWrites(std::size_t capacity) override;
|
2020-03-04 07:54:04 -06:00
|
|
|
|
|
|
|
void onDisconnect() override
|
|
|
|
{
|
|
|
|
// connections & sockets come and go a lot.
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
/// Clear all external references
|
|
|
|
void dispose() override { _msgHandler.reset(); }
|
|
|
|
|
2020-03-19 10:54:28 -05:00
|
|
|
int sendTextMessage(const char *msg, const size_t len, bool flush = false) const override;
|
|
|
|
int sendBinaryMessage(const char *data, const size_t len, bool flush = false) const override;
|
|
|
|
void shutdown(bool goingAway = false, const std::string &statusMessage = "") override;
|
|
|
|
void getIOStats(uint64_t &sent, uint64_t &recv) override;
|
2020-06-03 11:14:03 -05:00
|
|
|
// don't duplicate ourselves for every socket
|
2023-04-04 19:58:37 -05:00
|
|
|
void dumpState(std::ostream&) const override {}
|
2020-06-03 11:14:03 -05:00
|
|
|
// instead do it centrally.
|
|
|
|
void dumpProxyState(std::ostream& os);
|
2020-03-19 10:54:28 -05:00
|
|
|
bool parseEmitIncoming(const std::shared_ptr<StreamSocket> &socket);
|
2020-05-09 13:41:40 -05:00
|
|
|
|
2020-03-20 15:15:08 -05:00
|
|
|
void handleRequest(bool isWaiting, const std::shared_ptr<Socket> &socket);
|
2020-03-04 07:54:04 -06:00
|
|
|
|
2020-05-09 13:41:40 -05:00
|
|
|
/// tell our handler we've received a close.
|
|
|
|
void notifyDisconnected();
|
|
|
|
|
2020-03-04 07:54:04 -06:00
|
|
|
private:
|
2020-03-21 10:07:10 -05:00
|
|
|
std::shared_ptr<StreamSocket> popOutSocket();
|
2020-05-09 13:41:40 -05:00
|
|
|
/// can we find anything to send back if we try ?
|
2021-03-07 11:57:13 -06:00
|
|
|
bool slurpHasMessages(std::size_t capacity);
|
2020-03-19 10:54:28 -05:00
|
|
|
int sendMessage(const char *msg, const size_t len, bool text, bool flush);
|
|
|
|
bool flushQueueTo(const std::shared_ptr<StreamSocket> &socket);
|
|
|
|
|
|
|
|
struct Message : public std::vector<char>
|
|
|
|
{
|
2020-05-24 08:10:18 -05:00
|
|
|
Message(const char *msg, const std::size_t len, bool text, uint64_t serial)
|
2020-03-19 10:54:28 -05:00
|
|
|
{
|
|
|
|
const char *type = text ? "T" : "B";
|
|
|
|
insert(end(), type, type + 1);
|
|
|
|
std::ostringstream os;
|
2020-05-24 08:10:18 -05:00
|
|
|
os << std::hex << "0x" << serial << "\n0x" << len << '\n';
|
|
|
|
const std::string str = os.str();
|
2020-03-19 10:54:28 -05:00
|
|
|
insert(end(), str.c_str(), str.c_str() + str.size());
|
|
|
|
insert(end(), msg, msg + len);
|
2020-05-24 08:10:18 -05:00
|
|
|
constexpr const char *terminator = "\n";
|
2020-03-19 10:54:28 -05:00
|
|
|
insert(end(), terminator, terminator + 1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
/// queue things when we have no socket to hand.
|
|
|
|
std::vector<std::shared_ptr<Message>> _writeQueue;
|
2020-03-20 15:15:08 -05:00
|
|
|
std::vector<std::weak_ptr<StreamSocket>> _outSockets;
|
2020-04-18 12:40:59 -05:00
|
|
|
uint64_t _inSerial;
|
|
|
|
uint64_t _outSerial;
|
2020-03-04 07:54:04 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|