-
Notifications
You must be signed in to change notification settings - Fork 851
Fixing TSHttpTxnServerAddrSet #10189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
176 changes: 176 additions & 0 deletions
176
tests/gold_tests/pluginTest/tsapi/test_TSHttpTxnServerAddrSet.cc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| /* | ||
| * 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 <arpa/inet.h> | ||
| #include <netinet/in.h> | ||
| #include <sys/socket.h> | ||
|
|
||
| #include <cstdlib> | ||
|
|
||
| #include <memory> | ||
| #include <string_view> | ||
| #include <string> | ||
|
|
||
| namespace | ||
| { | ||
|
|
||
| std::string_view PNAME = "test_plugin"; | ||
|
|
||
| /** Represents the origin address and port that the user specified to which to | ||
| * redirect requests. */ | ||
| class TargetAddress | ||
| { | ||
| public: | ||
| /** Constructs a TargetAddress object from the given address and port. | ||
| * | ||
| * @param address The address to which to redirect requests. | ||
| * @param port The port to which to redirect requests. | ||
| */ | ||
| TargetAddress(char const *address, int port) : _address(address), _port(port) | ||
| { | ||
| _sockaddr.sin_family = AF_INET; | ||
| _sockaddr.sin_port = htons(port); | ||
| if (inet_pton(AF_INET, address, &_sockaddr.sin_addr) != 1) { | ||
| TSError("Invalid address %s", address); | ||
| _is_valid = false; | ||
| } else { | ||
| _is_valid = true; | ||
| } | ||
| } | ||
|
|
||
| /** Returns the address to which to redirect requests. | ||
| * | ||
| * @return The address to which to redirect requests. | ||
| */ | ||
| std::string_view | ||
| get_address() const | ||
| { | ||
| return _address; | ||
| } | ||
|
|
||
| /** Returns the port to which to redirect requests. | ||
| * | ||
| * @return The port to which to redirect requests. | ||
| */ | ||
| int | ||
| get_port() const | ||
| { | ||
| return _port; | ||
| } | ||
|
|
||
| /** Returns whether the address and port are valid. | ||
| * | ||
| * @return Whether the address and port are valid. | ||
| */ | ||
| bool | ||
| is_valid() const | ||
| { | ||
| return _is_valid; | ||
| } | ||
|
|
||
| /** Returns the sockaddr representing the user's specified origin address and | ||
| * port. | ||
| * | ||
| * @return The sockaddr representing the user's specified origin address and | ||
| * port. | ||
| */ | ||
| sockaddr const * | ||
| get_sockaddr() const | ||
| { | ||
| return reinterpret_cast<sockaddr const *>(&_sockaddr); | ||
| } | ||
|
|
||
| private: | ||
| std::string _address; | ||
| int _port; | ||
| sockaddr_in _sockaddr; | ||
| bool _is_valid; | ||
| }; | ||
|
|
||
| /** The user specified origin to which requests are redirected. */ | ||
| std::unique_ptr<TargetAddress> g_target_address{nullptr}; | ||
|
|
||
| /** Parse the plugin's command line arguments. */ | ||
| bool | ||
| parse_arguments(int argc, char const *argv[]) | ||
| { | ||
| if (argc != 3) { | ||
| TSError("Must provide the address and port for TSHttpTxnServerAddrSet."); | ||
| return false; | ||
| } | ||
| char const *address = argv[1]; | ||
| int port = atoi(argv[2]); | ||
| if (port <= 0 || port >= 65536) { | ||
| TSError("Invalid port number %s", argv[2]); | ||
| return false; | ||
| } | ||
| g_target_address = std::make_unique<TargetAddress>(address, port); | ||
| if (!g_target_address->is_valid()) { | ||
| TSError("Invalid address %s:%d", address, port); | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /** The handler which sets the user-specified origin. */ | ||
| int | ||
| set_origin(TSCont cont, TSEvent event, void *edata) | ||
| { | ||
| if (event != TS_EVENT_HTTP_CACHE_LOOKUP_COMPLETE) { | ||
| TSError("Unexpected event: %d", event); | ||
| return TS_ERROR; | ||
| } | ||
|
|
||
| TSHttpTxn txnp = (TSHttpTxn)edata; | ||
| if (TSHttpTxnServerAddrSet(txnp, g_target_address->get_sockaddr()) != TS_SUCCESS) { | ||
| TSError("Failed to set a transaction's origin to %s:%d", g_target_address->get_address().data(), g_target_address->get_port()); | ||
| TSHttpTxnReenable(txnp, TS_EVENT_HTTP_ERROR); | ||
| return TS_ERROR; | ||
| } | ||
| TSDebug(PNAME.data(), "Successfully set a transaction's origin to %s:%d", g_target_address->get_address().data(), | ||
| g_target_address->get_port()); | ||
| TSHttpTxnReenable(txnp, TS_EVENT_HTTP_CONTINUE); | ||
| return TS_SUCCESS; | ||
| } | ||
|
|
||
| } // anonymous namespace | ||
|
|
||
| void | ||
| TSPluginInit(int argc, char const *argv[]) | ||
| { | ||
| TSDebug(PNAME.data(), "Initializing plugin."); | ||
|
|
||
| TSPluginRegistrationInfo info; | ||
| info.plugin_name = PNAME.data(); | ||
| info.vendor_name = "Apache Software Foundation"; | ||
| info.support_email = "dev@trafficserver.apache.org"; | ||
| TSReleaseAssert(TSPluginRegister(&info) == TS_SUCCESS); | ||
|
|
||
| if (!parse_arguments(argc, argv)) { | ||
| TSError("Failed to parse arguments."); | ||
| return; | ||
| } | ||
|
|
||
| TSDebug(PNAME.data(), "Redirecting all requests to %s:%s", argv[1], argv[2]); | ||
|
|
||
| TSCont contp = TSContCreate(set_origin, nullptr); | ||
| TSHttpHookAdd(TS_HTTP_CACHE_LOOKUP_COMPLETE_HOOK, contp); | ||
| } |
34 changes: 34 additions & 0 deletions
34
tests/gold_tests/pluginTest/tsapi/test_TSHttpTxnServerAddrSet.replay.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| meta: | ||
| version: '1.0' | ||
|
|
||
| sessions: | ||
|
|
||
| - transactions: | ||
|
|
||
| - client-request: | ||
| method: GET | ||
| url: /pictures/flower.jpeg | ||
| version: '1.1' | ||
| headers: | ||
| fields: | ||
| - [ Host, www.example.com ] | ||
| - [ uuid, first-request ] | ||
|
|
||
| server-response: | ||
| status: 200 | ||
| reason: OK | ||
| headers: | ||
| fields: | ||
| - [ Date, "Sat, 16 Mar 2019 03:11:36 GMT" ] | ||
| - [ Content-Type, image/jpeg ] | ||
| - [ Transfer-Encoding, chunked ] | ||
| - [ Connection, keep-alive ] | ||
| - [ X-Response, redirect-succeeded ] | ||
| content: | ||
| size: 45 | ||
|
|
||
| proxy-response: | ||
| status: 200 | ||
| headers: | ||
| fields: | ||
| - [ X-Response, { value: redirect-succeeded, as: equal } ] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, TSHttpTxnServerAddrSet() is called in the CACHE_LOOKUP_COMPLETE hook in the new Au test, which presumably comes after DNS lookup?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct. Yahoo's internal plugin relies upon redirecting after CACHE_LOOKUP_COMPLETE in order to work. Fortunately it seems to work. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, it looks like CACHE_LOOKUP comes before DNS lookup. Here's the relevant debug logs from the autest that exercises this (note that
SM_ACTION_CACHE_LOOKUPcomes beforeSM_ACTION_DNS_LOOKUP):There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this diagram isn't accurate then: https://docs.trafficserver.apache.org/developer-guide/plugins/hooks-and-transactions/index.en.html#http-transaction-state-diagram