Skip to content

Commit

Permalink
Add TS_HTTP_TUNNEL_START_HOOK and TSHttpTxnTypeGet API
Browse files Browse the repository at this point in the history
  • Loading branch information
shinrich committed Aug 29, 2023
1 parent 67f020a commit bb39107
Show file tree
Hide file tree
Showing 13 changed files with 413 additions and 1 deletion.
37 changes: 37 additions & 0 deletions doc/developer-guide/api/functions/TSHttpTxnTypeGet.en.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
.. 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.
.. include:: ../../../common.defs

.. default-domain:: c

TSHttpTxnTypeGet
****************

Synopsis
========

.. code-block:: cpp
#include <ts/ts.h>
.. function:: TSTxnType TSHttpTxnTypeGet(TSHttpTxn txnp)

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

Returns the type of the txnp argument.

59 changes: 59 additions & 0 deletions doc/developer-guide/api/types/TSTxnType.en.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
.. 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.
.. include:: ../../../common.defs

TSTxnType
************

Synopsis
========

.. code-block:: cpp
#include <ts/apidefs.h>
+ typedef enum {
+ TS_TXN_TYPE_UNKNOWN,
+ TS_TXN_TYPE_HTTP,
+ TS_TXN_TYPE_EXPLICIT_TUNNEL,
+ TS_TXN_TYPE_TR_PASS_TUNNEL,
+ } TSTxnType;
.. c:enum:: TSTxnType
Specify the type of a transaction argument

.. c:enumerator:: TS_TXN_TYPE_UNKNOWN
Invalid value. This is used to indicate a failure or for initialization.

.. c:enumerator:: TS_TXN_TYPE_HTTP
A HTTP transaction.

.. c:enumerator:: TS_TXN_TYPE_EXPLICIT_TUNNEL
A blind tunnel transaction created based on a configuration file or an API call.

.. c:enumerator:: TS_TXN_TYPE_TR_PASS_TUNNEL
A blind tunnel created based on a parse error for a server port with tr-pass set.

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

Specify the type of a transaction. Plugins can use this to determine if they are interacting with tunnels
or parsed HTTP requests.
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ values for ``TSHttpHookID`` are:
available on the connection, or a previous client connection that
was left open for keep alive has new data available.

``TS_HTTP_TUNNEL_START_HOOK``
Called when the transaction is processed as a tunnel. The tunnel can be
due to an API call, a sni.yaml configuration, or a parse failure allowed due
to tr-pass. With this hook, a plugin can examine other information to
determine whether the tunnel should be allowed or not.

The function you use to add a global HTTP hook is
:c:func:`TSHttpHookAdd`.

9 changes: 9 additions & 0 deletions include/ts/apidefs.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ char const TS_VERSION_STRING[] = "@TS_VERSION_STRING@";
TS_EVENT_HTTP_REQUEST_BUFFER_READ_COMPLETE = 60018,
TS_EVENT_HTTP_RESPONSE_CLIENT = 60019,
TS_EVENT_HTTP_REQUEST_CLIENT = 60020,
TS_EVENT_HTTP_TUNNEL_START = 60021,

TS_EVENT_LIFECYCLE_PORTS_INITIALIZED = 60100,
TS_EVENT_LIFECYCLE_PORTS_READY = 60101,
Expand Down Expand Up @@ -478,6 +479,7 @@ char const TS_VERSION_STRING[] = "@TS_VERSION_STRING@";
DERIVE_HOOK_VALUE_FROM_EVENT(REQUEST_BUFFER_READ_COMPLETE), // TS_HTTP_REQUEST_BUFFER_READ_COMPLETE_HOOK
DERIVE_HOOK_VALUE_FROM_EVENT(RESPONSE_CLIENT), // TS_HTTP_RESPONSE_CLIENT_HOOK
DERIVE_HOOK_VALUE_FROM_EVENT(REQUEST_CLIENT), // TS_HTTP_REQUEST_CLIENT_HOOK
DERIVE_HOOK_VALUE_FROM_EVENT(TUNNEL_START), // TS_HTTP_TUNNEL_START_HOOK

// NOTE:
// If adding any TS_HTTP hooks, be sure to understand the note above in the
Expand Down Expand Up @@ -1503,6 +1505,13 @@ char const TS_VERSION_STRING[] = "@TS_VERSION_STRING@";
TSYaml value_node; ///< YAML::Node pointer.
};

typedef enum {
TS_TXN_TYPE_UNKNOWN,
TS_TXN_TYPE_HTTP,
TS_TXN_TYPE_EXPLICIT_TUNNEL,
TS_TXN_TYPE_TR_PASS_TUNNEL,
} TSTxnType;

} // end namespace c
} // end namespace tsapi

Expand Down
10 changes: 10 additions & 0 deletions include/ts/ts.h
Original file line number Diff line number Diff line change
Expand Up @@ -2895,6 +2895,16 @@ namespace c
/* IP addr parsing. This is a candidate for deprecation in v10.0.0, in favor of libswoc */
TSReturnCode TSIpStringToAddr(const char *str, size_t str_len, struct sockaddr *addr);

/**
* Return information about the type of the transaction. Is it a tunnel transaction or fully parsed?
* If tunneled is it due to parse failures and TR_PASS or is it due to an explicit configuration.
*
* @param[in] txnp The Transaction for which the type should be retrieved.
*
* @return enun value of type TSTxnType
*/
TSTxnType TSHttpTxnTypeGet(TSHttpTxn txnp);

} // end namespace c

template <typename T>
Expand Down
1 change: 1 addition & 0 deletions proxy/ProxySession.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ static const TSEvent eventmap[TS_HTTP_LAST_HOOK + 1] = {
TS_EVENT_HTTP_POST_REMAP, // TS_HTTP_POST_REMAP_HOOK
TS_EVENT_HTTP_RESPONSE_CLIENT, // TS_HTTP_RESPONSE_CLIENT_HOOK
TS_EVENT_HTTP_REQUEST_CLIENT, // TS_HTTP_REQUEST_CLIENT_HOOK
TS_EVENT_HTTP_TUNNEL_START, // TS_HTTP_TUNNEL_START_HOOK
TS_EVENT_NONE, // TS_HTTP_LAST_HOOK
};

Expand Down
7 changes: 7 additions & 0 deletions proxy/http/HttpDebugNames.cc
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ HttpDebugNames::get_event_name(int event)
return "TS_EVENT_HTTP_PRE_REMAP";
case TS_EVENT_HTTP_POST_REMAP:
return "TS_EVENT_HTTP_POST_REMAP";
case TS_EVENT_HTTP_TUNNEL_START:
return "TS_EVENT_HTTP_TUNNEL_START";
case TS_EVENT_LIFECYCLE_PORTS_INITIALIZED:
return "TS_EVENT_LIFECYCLE_PORTS_INITIALIZED";
case TS_EVENT_LIFECYCLE_PORTS_READY:
Expand Down Expand Up @@ -449,6 +451,9 @@ HttpDebugNames::get_action_name(HttpTransact::StateMachineAction_t e)
case HttpTransact::SM_ACTION_API_READ_REQUEST_HDR:
return ("SM_ACTION_API_READ_REQUEST_HDR");

case HttpTransact::SM_ACTION_API_TUNNEL_START:
return ("SM_ACTION_API_TUNNEL_START");

case HttpTransact::SM_ACTION_API_OS_DNS:
return ("SM_ACTION_API_OS_DNS");

Expand Down Expand Up @@ -553,6 +558,8 @@ HttpDebugNames::get_api_hook_name(TSHttpHookID t)
switch (t) {
case TS_HTTP_READ_REQUEST_HDR_HOOK:
return "TS_HTTP_READ_REQUEST_HDR_HOOK";
case TS_HTTP_TUNNEL_START_HOOK:
return "TS_HTTP_TUNNEL_START_HOOK";
case TS_HTTP_OS_DNS_HOOK:
return "TS_HTTP_OS_DNS_HOOK";
case TS_HTTP_SEND_REQUEST_HDR_HOOK:
Expand Down
10 changes: 9 additions & 1 deletion proxy/http/HttpSM.cc
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,8 @@ HttpSM::setup_blind_tunnel_port()
t_state.hdr_info.client_request.url_get()->host_set(new_host, strlen(new_host));
t_state.hdr_info.client_request.url_get()->port_set(netvc->get_local_port());
}
call_transact_and_set_next_state(HttpTransact::HandleBlindTunnel);
t_state.api_next_action = HttpTransact::SM_ACTION_API_TUNNEL_START;
do_api_callout();
}

int
Expand Down Expand Up @@ -1563,6 +1564,10 @@ HttpSM::handle_api_return()
case HttpTransact::SM_ACTION_API_READ_RESPONSE_HDR:
call_transact_and_set_next_state(nullptr);
return;
case HttpTransact::SM_ACTION_API_TUNNEL_START:
// Finished the Tunnel start callback. Go ahead and do the HandleBlindTunnel
call_transact_and_set_next_state(HttpTransact::HandleBlindTunnel);
return;
case HttpTransact::SM_ACTION_API_SEND_REQUEST_HDR:
setup_server_send_request();
return;
Expand Down Expand Up @@ -5645,6 +5650,9 @@ HttpSM::do_api_callout_internal()
case HttpTransact::SM_ACTION_API_POST_REMAP:
cur_hook_id = TS_HTTP_POST_REMAP_HOOK;
break;
case HttpTransact::SM_ACTION_API_TUNNEL_START:
cur_hook_id = TS_HTTP_TUNNEL_START_HOOK;
break;
case HttpTransact::SM_ACTION_API_READ_REQUEST_HDR:
cur_hook_id = TS_HTTP_READ_REQUEST_HDR_HOOK;
break;
Expand Down
1 change: 1 addition & 0 deletions proxy/http/HttpTransact.h
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ class HttpTransact

SM_ACTION_API_SM_START,
SM_ACTION_API_READ_REQUEST_HDR,
SM_ACTION_API_TUNNEL_START,
SM_ACTION_API_PRE_REMAP,
SM_ACTION_API_POST_REMAP,
SM_ACTION_API_OS_DNS,
Expand Down
18 changes: 18 additions & 0 deletions src/traffic_server/InkAPI.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10153,3 +10153,21 @@ tsapi::c::TSRecYAMLConfigParse(TSYaml node, TSYAMLRecNodeHandler handler, void *

return err.empty() ? TS_SUCCESS : TS_ERROR;
}

TSTxnType
tsapi::c::TSHttpTxnTypeGet(TSHttpTxn txnp)
{
sdk_assert(sdk_sanity_check_txn(txnp) == TS_SUCCESS);
HttpSM *sm = (HttpSM *)txnp;
TSTxnType retval = TS_TXN_TYPE_UNKNOWN;
if (sm != nullptr) {
if (sm->t_state.transparent_passthrough) {
retval = TS_TXN_TYPE_TR_PASS_TUNNEL;
} else if (sm->t_state.client_info.port_attribute == HttpProxyPort::TRANSPORT_BLIND_TUNNEL) {
retval = TS_TXN_TYPE_EXPLICIT_TUNNEL;
} else {
retval = TS_TXN_TYPE_HTTP;
}
}
return retval;
}
Loading

0 comments on commit bb39107

Please sign in to comment.