Skip to content

Commit

Permalink
TS-4698: Adds an API call to identify WebSocket requests.
Browse files Browse the repository at this point in the history
Add the TSHttpTxnIsWebsocket API to make it possible for plugins
to detect incoming WebSocket connections and thereby provide WebSocket
termination if desired.

This closes apache#822.
  • Loading branch information
ogoodman authored and ericcarlschwartz committed Sep 29, 2016
1 parent 80620f1 commit 8a52958
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 0 deletions.
45 changes: 45 additions & 0 deletions doc/developer-guide/api/functions/TSHttpTxnIsWebsocket.en.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
.. Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
.. default-domain:: c

TSHttpTxnIsWebsocket
********************

Test whether a request is attempting to initiate Websocket connection.

Synopsis
========

`#include <ts/ts.h>`

.. function:: int TSHttpTxnIsWebsocket(TSHttpTxn txnp)

Description
===========

:func:`TSHttpTxnIsWebsocket` tests whether the transaction
is a WebSocket upgrade request.

Return Values
=============

A non-zero value is returned if the relevant header value is found.

See also
========

:manpage:`TSAPI(3ts)`
5 changes: 5 additions & 0 deletions lib/atscppapi/src/InterceptPlugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,11 @@ InterceptPlugin::doRead()
// Modify the input VIO to reflect how much data we've completed.
TSVIONDoneSet(state_->input_.vio_, TSVIONDoneGet(state_->input_.vio_) + consumed);

if (isWebsocket()) {
TSVIOReenable(state_->input_.vio_);
return true;
}

if ((state_->hdr_parsed_) && (state_->num_body_bytes_read_ >= state_->expected_body_size_)) {
LOG_DEBUG("Completely read body");
if (state_->num_body_bytes_read_ > state_->expected_body_size_) {
Expand Down
6 changes: 6 additions & 0 deletions lib/atscppapi/src/TransactionPlugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ TransactionPlugin::getMutex()
return state_->mutex_;
}

bool
TransactionPlugin::isWebsocket() const
{
return TSHttpTxnIsWebsocket(state_->ats_txn_handle_);
}

TransactionPlugin::~TransactionPlugin()
{
LOG_DEBUG("Destroying TransactionPlugin=%p", this);
Expand Down
2 changes: 2 additions & 0 deletions lib/atscppapi/src/include/atscppapi/TransactionPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ class TransactionPlugin : public Plugin
void registerHook(Plugin::HookType hook_type);
virtual ~TransactionPlugin();

bool isWebsocket() const;

protected:
TransactionPlugin(Transaction &transaction);

Expand Down
9 changes: 9 additions & 0 deletions proxy/InkAPI.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4994,6 +4994,15 @@ TSHttpTxnInfoIntGet(TSHttpTxn txnp, TSHttpTxnInfoKey key, TSMgmtInt *value)
return TS_SUCCESS;
}

int
TSHttpTxnIsWebsocket(TSHttpTxn txnp)
{
sdk_assert(sdk_sanity_check_txn(txnp) == TS_SUCCESS);

HttpSM *sm = (HttpSM *)txnp;
return sm->t_state.is_websocket;
}

TSReturnCode
TSHttpTxnCacheLookupUrlGet(TSHttpTxn txnp, TSMBuffer bufp, TSMLoc obj)
{
Expand Down
1 change: 1 addition & 0 deletions proxy/api/ts/ts.h
Original file line number Diff line number Diff line change
Expand Up @@ -2334,6 +2334,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 int TSHttpTxnIsWebsocket(TSHttpTxn txnp);

/* Get the Txn's (HttpSM's) unique identifier, which is a sequence number since server start) */
tsapi uint64_t TSHttpTxnIdGet(TSHttpTxn txnp);
Expand Down

0 comments on commit 8a52958

Please sign in to comment.