You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@trafficserver.apache.org by ogoodman <gi...@git.apache.org> on 2016/05/10 06:04:59 UTC

[GitHub] trafficserver pull request: C++ API WebSocket example

GitHub user ogoodman opened a pull request:

    https://github.com/apache/trafficserver/pull/624

    C++ API WebSocket example

    There are two commits. The first one adds an API function to determine whether a transaction is a websocket request and adds code to the InterceptPlugin to make it behave appropriately. The second one provides a working demo of a websocket handler.


You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/ogoodman/trafficserver oag-websocket

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/trafficserver/pull/624.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #624
    
----
commit 129ad07ad9df67b187467e4004beec70dfc29c54
Author: Oliver Goodman <oa...@optusnet.com.au>
Date:   2016-05-10T05:52:20Z

    Adds an API call to identify WebSocket requests.

commit 7a45cce74a689ce9cea949f576a4a94ef2b0c34a
Author: Oliver Goodman <oa...@optusnet.com.au>
Date:   2016-05-10T05:55:21Z

    Adds a C++ API WebSocket example.

----


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] trafficserver issue #624: C++ API WebSocket example

Posted by ogoodman <gi...@git.apache.org>.
Github user ogoodman commented on the issue:

    https://github.com/apache/trafficserver/pull/624
  
    I'm going to close this particular PR now as it has become messy, doesn't reference any JIRA issue, and needs to be split into a PR on the TS API followed by one which adds the example to the C++ API.
    JIRA issues TS-4698 and TS-4699 have been created for the two improvements.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] trafficserver pull request: C++ API WebSocket example

Posted by bgaff <gi...@git.apache.org>.
Github user bgaff commented on the pull request:

    https://github.com/apache/trafficserver/pull/624
  
    @ogoodman would you mind also starting a thread on dev@trafficserver.apache.org for API review? I can also kick off this discussion if you'd like.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] trafficserver pull request: C++ API WebSocket example

Posted by bgaff <gi...@git.apache.org>.
Github user bgaff commented on a diff in the pull request:

    https://github.com/apache/trafficserver/pull/624#discussion_r62700530
  
    --- 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", "support@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_.
    +
    +            ws_buf_ += data;
    +            while (true) {
    +                if (!msg_len_) {
    +                    size_t avail = ws_buf_.size() - pos_;
    +
    +                    // Read the msg_length if we have enough data.
    +                    if (avail < 6) break; // 2 + 4 mask
    +                    ctrl_ = ws_buf_[pos_] & 0xF;
    +                    size_t msg_len = ws_buf_[pos_ + 1] & 0x7F;
    +                    if (msg_len == 0x7E) {
    +                        if (avail < 8) { // 2 + 2 + length bytes + 4 mask.
    +                            break;
    +                        }
    +                        msg_len = 0;
    +                        pos_ += 2;
    +                        for (int i = 0; i < 2; ++i, ++pos_) {
    +                            msg_len <<= 8;
    +                            msg_len += (unsigned char)(ws_buf_[pos_]);
    +                        }
    +                    } else if (msg_len == 0x7F) {
    +                        if (avail < 14) { // 2 + 8 length bytes + 4 mask.
    +                            break;
    +                        }
    +                        msg_len = 0;
    +                        pos_ += 2;
    +                        for (int i = 0; i < 8; ++i, ++pos_) {
    +                            msg_len <<= 8;
    +                            msg_len += (unsigned char)(ws_buf_[pos_]);
    +                        }
    +                    } else {
    +                        pos_ += 2;
    +                    }
    +                    msg_len_ = msg_len;
    +                    // Copy the mask.
    +                    for (size_t i = 0; i < 4; ++i, ++pos_) {
    +                        mask_[i] = ws_buf_[pos_];
    +                    }
    +                }
    +
    +                // Check if we have enough data to read the message.
    +                if (ws_buf_.size() < pos_ + msg_len_) break; // not enough data.
    +
    +                // Apply the mask.
    +                for (size_t i=0; i<msg_len_; ++i) {
    +                    ws_buf_[pos_ + i] ^= mask_[i & 3];
    +                }
    +
    +                std::string message = ws_buf_.substr(pos_, msg_len_);
    +
    --- End diff --
    
    For what it's worth you're not responding to PING frames (0x9), clients will likely close on you.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] trafficserver issue #624: C++ API WebSocket example

Posted by zwoop <gi...@git.apache.org>.
Github user zwoop commented on the issue:

    https://github.com/apache/trafficserver/pull/624
  
    Yeah, examples are great. So please create a Jira, if you are going to work on it. Any changes to APIs would need to go through API review though, so depending on what you are doing, you might want to split this up into multiple commits and/or multiple Jira's and PRs.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] trafficserver pull request: C++ API WebSocket example

Posted by zwoop <gi...@git.apache.org>.
Github user zwoop commented on the pull request:

    https://github.com/apache/trafficserver/pull/624#issuecomment-220376297
  
    
    
    .



---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] trafficserver issue #624: C++ API WebSocket example

Posted by zwoop <gi...@git.apache.org>.
Github user zwoop commented on the issue:

    https://github.com/apache/trafficserver/pull/624
  
    This needs a rebase as well before we can proceed.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] trafficserver pull request #624: C++ API WebSocket example

Posted by ogoodman <gi...@git.apache.org>.
Github user ogoodman closed the pull request at:

    https://github.com/apache/trafficserver/pull/624


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] trafficserver pull request: C++ API WebSocket example

Posted by bgaff <gi...@git.apache.org>.
Github user bgaff commented on a diff in the pull request:

    https://github.com/apache/trafficserver/pull/624#discussion_r62779253
  
    --- Diff: lib/atscppapi/examples/websocket/WebSocket.cc ---
    @@ -0,0 +1,311 @@
    +#include "WebSocket.hh"
    +
    +#include <iostream>
    +#include "ts/ink_base64.h"
    +#include "openssl/evp.h"
    +#include <netinet/in.h>
    +#include <arpa/inet.h>
    +
    +// DISCLAIMER: this is intended for demonstration purposes only and
    +// does not pretend to implement a complete (or useful) server.
    +
    +using namespace atscppapi;
    +
    +#define SAY(a) std::cout << a << std::endl;
    --- End diff --
    
    Please see the stuff we have available in atscppapi/Logger.h, such as TSDebug or creating a logger class.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] trafficserver pull request: C++ API WebSocket example

Posted by bgaff <gi...@git.apache.org>.
Github user bgaff commented on a diff in the pull request:

    https://github.com/apache/trafficserver/pull/624#discussion_r62697922
  
    --- 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", "support@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_.
    +
    +            ws_buf_ += data;
    +            while (true) {
    +                if (!msg_len_) {
    +                    size_t avail = ws_buf_.size() - pos_;
    +
    +                    // Read the msg_length if we have enough data.
    +                    if (avail < 6) break; // 2 + 4 mask
    +                    ctrl_ = ws_buf_[pos_] & 0xF;
    +                    size_t msg_len = ws_buf_[pos_ + 1] & 0x7F;
    +                    if (msg_len == 0x7E) {
    +                        if (avail < 8) { // 2 + 2 + length bytes + 4 mask.
    +                            break;
    +                        }
    +                        msg_len = 0;
    +                        pos_ += 2;
    +                        for (int i = 0; i < 2; ++i, ++pos_) {
    +                            msg_len <<= 8;
    +                            msg_len += (unsigned char)(ws_buf_[pos_]);
    +                        }
    +                    } else if (msg_len == 0x7F) {
    +                        if (avail < 14) { // 2 + 8 length bytes + 4 mask.
    +                            break;
    +                        }
    +                        msg_len = 0;
    +                        pos_ += 2;
    +                        for (int i = 0; i < 8; ++i, ++pos_) {
    --- End diff --
    
    Similarly you need to convert the 64bit quantity using network byte order conversions:
    
    be64toh((\*((uint64_t\* )(ws_buf_ + pos_))));


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] trafficserver pull request: C++ API WebSocket example

Posted by atsci <gi...@git.apache.org>.
Github user atsci commented on the pull request:

    https://github.com/apache/trafficserver/pull/624#issuecomment-219186487
  
    Can one of the admins verify this patch? Only approve PRs which have been reviewed.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] trafficserver pull request: C++ API WebSocket example

Posted by atsci <gi...@git.apache.org>.
Github user atsci commented on the pull request:

    https://github.com/apache/trafficserver/pull/624#issuecomment-219191864
  
    Can one of the admins verify this patch? Only approve PRs which have been reviewed.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] trafficserver issue #624: C++ API WebSocket example

Posted by zwoop <gi...@git.apache.org>.
Github user zwoop commented on the issue:

    https://github.com/apache/trafficserver/pull/624
  
    Also, repeating the question above, what is the Jira for 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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] trafficserver pull request: C++ API WebSocket example

Posted by shukitchan <gi...@git.apache.org>.
Github user shukitchan commented on the pull request:

    https://github.com/apache/trafficserver/pull/624
  
    also is there a jira ticket for 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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] trafficserver pull request: C++ API WebSocket example

Posted by bgaff <gi...@git.apache.org>.
Github user bgaff commented on a diff in the pull request:

    https://github.com/apache/trafficserver/pull/624#discussion_r62697756
  
    --- 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", "support@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_.
    +
    +            ws_buf_ += data;
    +            while (true) {
    +                if (!msg_len_) {
    +                    size_t avail = ws_buf_.size() - pos_;
    +
    +                    // Read the msg_length if we have enough data.
    +                    if (avail < 6) break; // 2 + 4 mask
    +                    ctrl_ = ws_buf_[pos_] & 0xF;
    +                    size_t msg_len = ws_buf_[pos_ + 1] & 0x7F;
    +                    if (msg_len == 0x7E) {
    +                        if (avail < 8) { // 2 + 2 + length bytes + 4 mask.
    +                            break;
    +                        }
    +                        msg_len = 0;
    +                        pos_ += 2;
    +                        for (int i = 0; i < 2; ++i, ++pos_) {
    +                            msg_len <<= 8;
    +                            msg_len += (unsigned char)(ws_buf_[pos_]);
    --- End diff --
    
    You're neglecting network byte order to host order conversions, you need to use nthos:
    
    ntohs(*(uint16_t*) (ws_buf_ + pos_));


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] trafficserver pull request: C++ API WebSocket example

Posted by atsci <gi...@git.apache.org>.
Github user atsci commented on the pull request:

    https://github.com/apache/trafficserver/pull/624#issuecomment-218782549
  
    Can one of the admins verify this patch? Only approve PRs which have been reviewed.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] trafficserver pull request: C++ API WebSocket example

Posted by ogoodman <gi...@git.apache.org>.
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", "support@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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] trafficserver pull request: C++ API WebSocket example

Posted by bgaff <gi...@git.apache.org>.
Github user bgaff commented on a diff in the pull request:

    https://github.com/apache/trafficserver/pull/624#discussion_r62698746
  
    --- 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", "support@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 --
    
    You're neglecting, Sec-WebSocket-Protocol, Chrome for example will send Sec-WebSocket-Protocol and if you're not returning it in the response it'll close the connection on you.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] trafficserver pull request: C++ API WebSocket example

Posted by ogoodman <gi...@git.apache.org>.
Github user ogoodman commented on a diff in the pull request:

    https://github.com/apache/trafficserver/pull/624#discussion_r62778999
  
    --- 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;
    --- End diff --
    
    Quite a few atscppapi examples appear to print to stdout, and I feel that this is appropriate for demonstration code. If you wanted to turn this into a provided plugin type, then this and quite a few other changes would of course be necessary.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] trafficserver issue #624: C++ API WebSocket example

Posted by ogoodman <gi...@git.apache.org>.
Github user ogoodman commented on the issue:

    https://github.com/apache/trafficserver/pull/624
  
    There is no Jira. I just thought it might be of interest to people as another example plugin. If there *is* interest I can open one describing this as an enhancement. Shall I do that?


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] trafficserver pull request: C++ API WebSocket example

Posted by bgaff <gi...@git.apache.org>.
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", "support@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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] trafficserver pull request: C++ API WebSocket example

Posted by bgaff <gi...@git.apache.org>.
Github user bgaff commented on a diff in the pull request:

    https://github.com/apache/trafficserver/pull/624#discussion_r62698874
  
    --- 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;
    --- End diff --
    
    Please use a Logger or use TSDebug rather than std::cout or printf()


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] trafficserver pull request: C++ API WebSocket example

Posted by bgaff <gi...@git.apache.org>.
Github user bgaff commented on the pull request:

    https://github.com/apache/trafficserver/pull/624
  
    Hi @ogoodman , I want to get this landed soon can you please just make one final change which puts it under an experimental/ folder in examples? After that I'm happy to commit this for you.
    
    Thanks


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] trafficserver pull request: C++ API WebSocket example

Posted by ogoodman <gi...@git.apache.org>.
Github user ogoodman commented on a diff in the pull request:

    https://github.com/apache/trafficserver/pull/624#discussion_r62778445
  
    --- Diff: proxy/InkAPI.cc ---
    @@ -4858,6 +4858,15 @@ TSHttpTxnInfoIntGet(TSHttpTxn txnp, TSHttpTxnInfoKey key, TSMgmtInt *value)
       return TS_SUCCESS;
     }
     
    +bool
    +TSHttpTxnIsWebsocket(TSHttpTxn txnp)
    +{
    +  sdk_assert(sdk_sanity_check_txn(txnp) == TS_SUCCESS);
    +
    +  HttpSM *sm = (HttpSM *)txnp;
    +  return sm->t_state.is_upgrade_request && sm->t_state.upgrade_token_wks == MIME_VALUE_WEBSOCKET;
    --- End diff --
    
    I'm not sure why but this doesn't work for detecting an incoming websocket connection. Perhaps some of those flags are only set when proxying after the origin server has started responding.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] trafficserver pull request: C++ API WebSocket example

Posted by ogoodman <gi...@git.apache.org>.
Github user ogoodman commented on a diff in the pull request:

    https://github.com/apache/trafficserver/pull/624#discussion_r62778716
  
    --- 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", "support@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_.
    +
    +            ws_buf_ += data;
    +            while (true) {
    +                if (!msg_len_) {
    +                    size_t avail = ws_buf_.size() - pos_;
    +
    +                    // Read the msg_length if we have enough data.
    +                    if (avail < 6) break; // 2 + 4 mask
    +                    ctrl_ = ws_buf_[pos_] & 0xF;
    +                    size_t msg_len = ws_buf_[pos_ + 1] & 0x7F;
    +                    if (msg_len == 0x7E) {
    +                        if (avail < 8) { // 2 + 2 + length bytes + 4 mask.
    +                            break;
    +                        }
    +                        msg_len = 0;
    +                        pos_ += 2;
    +                        for (int i = 0; i < 2; ++i, ++pos_) {
    +                            msg_len <<= 8;
    +                            msg_len += (unsigned char)(ws_buf_[pos_]);
    --- End diff --
    
    The code did handle network byte order correctly, but it is true that ntohs and friends are more efficient on big-endian machines.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] trafficserver pull request: C++ API WebSocket example

Posted by jpeach <gi...@git.apache.org>.
Github user jpeach commented on the pull request:

    https://github.com/apache/trafficserver/pull/624
  
    @bgaff The new API should go through API review. IMHO it would be best to make a separate JIRA and pull request for the API changes.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] trafficserver pull request: C++ API WebSocket example

Posted by atsci <gi...@git.apache.org>.
Github user atsci commented on the pull request:

    https://github.com/apache/trafficserver/pull/624#issuecomment-218525564
  
    Can one of the admins verify this patch?


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] trafficserver pull request: C++ API WebSocket example

Posted by bgaff <gi...@git.apache.org>.
Github user bgaff commented on the pull request:

    https://github.com/apache/trafficserver/pull/624#issuecomment-218336364
  
    With the C plugins we've had an experimental directory for plugins that were fairly new, we should probably consider adding this to a folder under atscppapi/examples/experimental for now and then we can always promote it later. Thanks for contributing.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] trafficserver pull request: C++ API WebSocket example

Posted by bgaff <gi...@git.apache.org>.
Github user bgaff commented on a diff in the pull request:

    https://github.com/apache/trafficserver/pull/624#discussion_r62699378
  
    --- 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", "support@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);
    --- End diff --
    
    So websockets actually have seperate timeouts that aren't exposed yet into the C++ api (which explains why you weren't seeing them apply).
    
    See: https://docs.trafficserver.apache.org/en/latest/admin-guide/files/records.config.en.html#proxy-config-websocket-no-activity-timeout
    
    Please feel free top open a pull request that adds them, otherwise please open a TS ticket and assign it to me and I'll add it.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] trafficserver pull request: C++ API WebSocket example

Posted by bgaff <gi...@git.apache.org>.
Github user bgaff commented on a diff in the pull request:

    https://github.com/apache/trafficserver/pull/624#discussion_r62779359
  
    --- Diff: lib/atscppapi/examples/websocket/WebSocket.cc ---
    @@ -0,0 +1,311 @@
    +#include "WebSocket.hh"
    +
    +#include <iostream>
    +#include "ts/ink_base64.h"
    +#include "openssl/evp.h"
    +#include <netinet/in.h>
    +#include <arpa/inet.h>
    +
    +// DISCLAIMER: this is intended for demonstration purposes only and
    +// does not pretend to implement a complete (or useful) server.
    +
    +using namespace atscppapi;
    +
    +#define SAY(a) std::cout << a << std::endl;
    +#define SHOW(a) std::cout << #a << " = " << a << std::endl
    +
    +enum ws_frametypes {
    +    WS_FRAME_CONTINUATION = 0x0,
    +    WS_FRAME_TEXT = 0x1,
    +    WS_FRAME_BINARY = 0x2,
    +    WS_FRAME_CLOSE = 0x8,
    +    WS_FRAME_PING = 0x9,
    +    WS_FRAME_PONG = 0xA
    +};
    +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
    +
    +void TSPluginInit(int argc, const char* argv[])
    +{
    +    RegisterGlobalPlugin("WebSocket", "Apache", "support@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)
    +    , mask_len_(0)
    +    , msg_len_(0)
    +    , ws_handshake_done_(false)
    +{
    +    if (isWebsocket()) {
    +        ws_key_ = transaction.getClientRequest().getHeaders().values("sec-websocket-key");
    +    }
    +}
    +
    +WebSocket::~WebSocket()
    +{
    +    SAY("WebSocket finished.");
    +}
    +
    +void WebSocket::ws_send(std::string const& data, int code)
    +{
    +    size_t len = data.length();
    +
    +    std::string frame;
    +    frame.reserve(len + 10);
    +    frame += char(code);
    +
    +    int len_len;
    +    if (len <= 125) {
    +        frame += char(len);
    +        len_len = 0;
    +    } else if (len <= UINT16_MAX) {
    +        frame += char(WS_16BIT_LEN);
    +        len_len = 2;
    +    } else {
    +        frame += char(WS_64BIT_LEN);
    +        len_len = 8;
    +    }
    +    // Convert length to big-endian bytes.
    +    while (--len_len >= 0) {
    +        frame += char((len >> (8*len_len)) & 0xFF);
    --- End diff --
    
    To convert the length to big endian just use:
    htons() or htobe64(), same idea as below.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] trafficserver pull request: C++ API WebSocket example

Posted by shukitchan <gi...@git.apache.org>.
Github user shukitchan commented on a diff in the pull request:

    https://github.com/apache/trafficserver/pull/624#discussion_r62701424
  
    --- Diff: proxy/api/ts/ts.h ---
    @@ -2328,6 +2328,7 @@ tsapi TSReturnCode TSHttpTxnCacheLookupUrlGet(TSHttpTxn txnp, TSMBuffer bufp, TS
     tsapi TSReturnCode TSHttpTxnCacheLookupUrlSet(TSHttpTxn txnp, TSMBuffer bufp, TSMLoc obj);
     tsapi TSReturnCode TSHttpTxnPrivateSessionSet(TSHttpTxn txnp, int private_session);
     tsapi int TSHttpTxnBackgroundFillStarted(TSHttpTxn txnp);
    +tsapi bool TSHttpTxnIsWebsocket(TSHttpTxn txnp);
    --- End diff --
    
    should be using "int" instead of "bool" to be consistent?
    also we need an API review?


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] trafficserver pull request: C++ API WebSocket example

Posted by atsci <gi...@git.apache.org>.
Github user atsci commented on the pull request:

    https://github.com/apache/trafficserver/pull/624#issuecomment-219190135
  
    Can one of the admins verify this patch? Only approve PRs which have been reviewed.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] trafficserver issue #624: C++ API WebSocket example

Posted by zwoop <gi...@git.apache.org>.
Github user zwoop commented on the issue:

    https://github.com/apache/trafficserver/pull/624
  
    Status on this PR? Is there a Jira? The branch has conflicts now, and likely might need a clang-format run as well.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] trafficserver pull request: C++ API WebSocket example

Posted by bgaff <gi...@git.apache.org>.
Github user bgaff commented on a diff in the pull request:

    https://github.com/apache/trafficserver/pull/624#discussion_r62701032
  
    --- Diff: proxy/InkAPI.cc ---
    @@ -4858,6 +4858,15 @@ TSHttpTxnInfoIntGet(TSHttpTxn txnp, TSHttpTxnInfoKey key, TSMgmtInt *value)
       return TS_SUCCESS;
     }
     
    +bool
    +TSHttpTxnIsWebsocket(TSHttpTxn txnp)
    +{
    +  sdk_assert(sdk_sanity_check_txn(txnp) == TS_SUCCESS);
    +
    +  HttpSM *sm = (HttpSM *)txnp;
    +  return sm->t_state.is_upgrade_request && sm->t_state.upgrade_token_wks == MIME_VALUE_WEBSOCKET;
    --- End diff --
    
    You should probably instead do, as is_websocket was set if all the required websocket headers were sent, also there is a check for the upgrade succeeding.
    
    return sm->t_state.is_upgrade_request && sm->t_state.did_upgrade_succeed && sm->t_state.is_websocket 


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] trafficserver pull request: C++ API WebSocket example

Posted by atsci <gi...@git.apache.org>.
Github user atsci commented on the pull request:

    https://github.com/apache/trafficserver/pull/624#issuecomment-219194912
  
    Can one of the admins verify this patch? Only approve PRs which have been reviewed.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] trafficserver pull request: C++ API WebSocket example

Posted by bgaff <gi...@git.apache.org>.
Github user bgaff commented on a diff in the pull request:

    https://github.com/apache/trafficserver/pull/624#discussion_r62699616
  
    --- 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", "support@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));
    --- End diff --
    
    I think instead you should probably do something like:
    
    std::string frame;
    frame.reserve(header_size + data.length()); 


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---