Github user bgaff commented on a diff in the pull request: https://github.com/apache/trafficserver/pull/624#discussion_r62698040 --- Diff: lib/atscppapi/examples/websocket/WebSocket.cc --- @@ -0,0 +1,246 @@ +#include "WebSocket.hh" + +#include <iostream> +#include "ts/ink_base64.h" +#include "openssl/evp.h" + +using namespace atscppapi; + +#define SAY(a) std::cout << a << std::endl; +#define SHOW(a) std::cout << #a << " = " << a << std::endl + +void TSPluginInit(int argc, const char* argv[]) +{ + RegisterGlobalPlugin("WebSocket", "Apache", "supp...@example.com"); + new WebSocketInstaller(); +} + + +// WebSocketInstaller + +WebSocketInstaller::WebSocketInstaller() + : GlobalPlugin(true /* ignore internal transactions */) +{ + GlobalPlugin::registerHook(Plugin::HOOK_READ_REQUEST_HEADERS_PRE_REMAP); +} + +#define WS_DIGEST_MAX ATS_BASE64_ENCODE_DSTLEN(20) +static const std::string magic = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; + +static std::string ws_digest(std::string const& key) { + EVP_MD_CTX digest; + EVP_MD_CTX_init(&digest); + + if (!EVP_DigestInit_ex(&digest, EVP_sha1(), NULL)) { + EVP_MD_CTX_cleanup(&digest); + return "init-failed"; + } + if (!EVP_DigestUpdate(&digest, key.data(), key.length())) { + EVP_MD_CTX_cleanup(&digest); + return "update1-failed"; + } + if (!EVP_DigestUpdate(&digest, magic.data(), magic.length())) { + EVP_MD_CTX_cleanup(&digest); + return "update2-failed"; + } + + unsigned char hash_buf[EVP_MAX_MD_SIZE]; + unsigned int hash_len = 0; + if (!EVP_DigestFinal_ex(&digest, hash_buf, &hash_len)) { + EVP_MD_CTX_cleanup(&digest); + return "final-failed"; + } + EVP_MD_CTX_cleanup(&digest); + if (hash_len != 20) { + return "bad-hash-length"; + } + + char digest_buf[WS_DIGEST_MAX]; + size_t digest_len = 0; + + ats_base64_encode(hash_buf, hash_len, digest_buf, WS_DIGEST_MAX, &digest_len); + + return std::string((char*)digest_buf, digest_len); +} + + +void WebSocketInstaller::handleReadRequestHeadersPreRemap(Transaction &transaction) +{ + SAY("Incoming request."); + transaction.addPlugin(new WebSocket(transaction)); + transaction.resume(); +} + +// WebSocket implementation. + +WebSocket::WebSocket(Transaction& transaction) + : InterceptPlugin(transaction, InterceptPlugin::SERVER_INTERCEPT) + , pos_(0) + , ctrl_(0) + , msg_len_(0) + , ws_handshake_done_(false) +{ + ws_key_ = transaction.getClientRequest().getHeaders().values("sec-websocket-key"); + if (ws_key_.size()) { + printf("setting timeouts\n"); + transaction.setTimeout(Transaction::TIMEOUT_ACTIVE, 844000000); + transaction.setTimeout(Transaction::TIMEOUT_NO_ACTIVITY, 84400000); + } +} + +WebSocket::~WebSocket() +{ + SAY("WebSocket finished."); +} + +void WebSocket::ws_send(std::string const& data, int code) +{ + std::string frame(1, char(code)); + size_t len = data.length(); + if (len <= 125) { + frame += char(len); + } else { + int len_len; + if (len <= 65535) { + frame += char(126); + len_len = 2; + } else { + frame += char(127); + len_len = 8; + } + while (--len_len >= 0) { + frame += char((len >> (8*len_len)) & 0xFF); + } + } + produce(frame + data); +} + +void WebSocket::consume(const std::string &data, InterceptPlugin::RequestDataType type) +{ + if (isWebsocket()) { + if (!ws_handshake_done_) { + std::string digest = ws_digest(ws_key_); + + printf("WS key digest: %s %s\n", ws_key_.c_str(), digest.c_str()); + + std::string headers; + headers += + "HTTP/1.1 101 Switching Protocols\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + "Sec-WebSocket-Accept: " + digest + "\r\n\r\n"; + produce(headers); + ws_handshake_done_ = true; + } + } + + // auto tid = std::this_thread::get_id(); + if (type == InterceptPlugin::REQUEST_HEADER) { + headers_ += data; + } else { + // cout << tid << ": Read request body" << endl << data << endl; + if (isWebsocket()) { + + // When we've read as much as we can for any incoming + // data chunk we remove the head of ws_buf_ and reset pos_ + // to 0. + // + // If we can't read a complete length + masks, we leave + // pos_ unchanged and just append what we got to ws_buf_. + // + // When we get the length, we update pos_ and length. + // While length is non-zero we wait for ws_buf_ to have + // msg_len_ bytes beyond pos_. + --- End diff -- Perhaps an enum and some constants will make your code more readable: enum ws_frametypes { WS_FRAME_CONTINUATION = 0x0, WS_FRAME_TEXT = 0x1, WS_FRAME_BINARY = 0x2, WS_FRAME_CLOSE = 0x08, WS_FRAME_PING = 0x09, WS_FRAME_PONG = 0x0A }; typedef enum ws_frametypes WS_FRAMETYPES; #define WS_RSV1 0x40 #define WS_RSV2 0x20 #define WS_RSV3 0x10 #define WS_MASKED 0x80 #define WS_OPCODE 0x0F #define WS_FIN 0x80 #define WS_LENGTH 0x7F #define WS_16BIT_LEN 126 #define WS_64BIT_LEN 127
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---