Github user ogoodman commented on a diff in the pull request:

    https://github.com/apache/trafficserver/pull/624#discussion_r62778854
  
    --- 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"
    --- End diff --
    
    I have tested with Chrome (on Mac) and have not seen the behaviour you 
speak of. I get the impression from the RFC that this header is for specifying 
the application level protocol and isn't really important for a simple demo 
like this.


---
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.
---

Reply via email to