maskit commented on code in PR #12995: URL: https://github.com/apache/trafficserver/pull/12995#discussion_r2969312734
########## plugins/experimental/jax_fingerprint/ja4h/ja4h_method.cc: ########## @@ -0,0 +1,173 @@ +/** @file + + @section license License + + 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 "ts/ts.h" + +#include "../plugin.h" +#include "../context.h" +#include "ja4h_method.h" +#include "ja4h.h" +#include "datasource.h" + +#include "openssl/sha.h" + +namespace ja4h_method +{ +void on_request(JAxContext *, TSHttpTxn); + +struct Method method = { + "JA4H", + Method::Type::REQUEST_BASED, + nullptr, + on_request, +}; + +class TxnDatasource : public Datasource +{ +public: + TxnDatasource(TSHttpTxn txnp); + ~TxnDatasource(); + std::string_view get_method() override; + int get_version() override; + bool has_cookie_field() override; + bool has_referer_field() override; + int get_field_count() override; + std::string_view get_accept_language() override; + void get_headers_hash(unsigned char out[32]) override; + +private: + TSHttpTxn _txn; + TSMBuffer _request = nullptr; + TSMLoc _req_hdr = nullptr; +}; + +TxnDatasource::TxnDatasource(TSHttpTxn txnp) : _txn(txnp) +{ + TSHttpTxnClientReqGet(txnp, &(this->_request), &(this->_req_hdr)); +} + +TxnDatasource::~TxnDatasource() +{ + if (this->_request != nullptr) { + TSHandleMLocRelease(this->_request, TS_NULL_MLOC, this->_req_hdr); + } +} + +std::string_view +TxnDatasource::get_method() +{ + if (this->_request == nullptr) { + return ""; + } + + int method_len; + const char *method = TSHttpHdrMethodGet(this->_request, this->_req_hdr, &method_len); + + return {method, static_cast<size_t>(method_len)}; +} + +int +TxnDatasource::get_version() +{ + if (TSHttpTxnClientProtocolStackContains(this->_txn, "h2")) { + return 2 << 16; + } else if (TSHttpTxnClientProtocolStackContains(this->_txn, "h3")) { + return 3 << 16; + } else { + return TSHttpHdrVersionGet(this->_request, this->_req_hdr); + } +} + +bool +TxnDatasource::has_cookie_field() +{ + TSMLoc mloc = TSMimeHdrFieldFind(this->_request, this->_req_hdr, TS_MIME_FIELD_COOKIE, TS_MIME_LEN_COOKIE); + if (mloc) { + TSHandleMLocRelease(this->_request, this->_req_hdr, mloc); + } + return mloc != TS_NULL_MLOC; Review Comment: It unlikely happens. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
