Updated Branches: refs/heads/master 58205af14 -> a13d99b65
Plugin that will allow the origin server to set a header that ATS will redirect to. This redirect is handled by ATS and wont be seen by the client. Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/a13d99b6 Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/a13d99b6 Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/a13d99b6 Branch: refs/heads/master Commit: a13d99b6505bd0045aee3c8178bdf7a2b5377bf1 Parents: 58205af Author: Bryan Call <[email protected]> Authored: Fri Aug 10 15:59:00 2012 -0700 Committer: Bryan Call <[email protected]> Committed: Fri Aug 10 15:59:00 2012 -0700 ---------------------------------------------------------------------- plugins/experimental/custom_redirect/README | 41 ++++ .../custom_redirect/custom_redirect.cc | 171 +++++++++++++++ 2 files changed, 212 insertions(+), 0 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/trafficserver/blob/a13d99b6/plugins/experimental/custom_redirect/README ---------------------------------------------------------------------- diff --git a/plugins/experimental/custom_redirect/README b/plugins/experimental/custom_redirect/README new file mode 100644 index 0000000..05bf447 --- /dev/null +++ b/plugins/experimental/custom_redirect/README @@ -0,0 +1,41 @@ +In /home/y/conf/yts/plugin.config, you can add + 1) custom_redirect.so + or + 2) custom_redirect.so wretch-redirect-url + or + 3) custom_redirect.so 507 + +Case 1) means using the default header (x-redirect-url) to specify a URL to redirect to; +Case 2) user specifies their specific header name (to replace the default one) to specify a URL to redirect to; +Case 3) user sepcifies the specific return code, if return code matches, then plugin will force to redirect to the +URL specified in standard "Location" header. + +For simplicity, we recommend to use case 1) + +Version 1.0.0 (8/9/2012, bcall) + - Ported to ATS + +Version 0.5.1 (12/07/10, vmamidi) + - fixing the bug with redirect + +Version 0.4 (12/06/10, vmamidi) + - enabling the redirect only for GETs + +Version 0.3 (11/19/09, wenjing) + - Remove verbose log + +Version 0.2 (11/09/09, wenjing) + - Add more instruction + +This is a plugin for YTS (Yahoo Traffic Server) that allows OS (Origin Server) +to do either of the following two things: + + 1) specify a particular response header (default: x-redirect-url) with a URL + that forces YTS redirect to that URL; + + 2) specify a particular return error code (such as 507) with a standard Location + header which forces YTS to redirect to that location URL. + +Version 0.1 (09/17/09, wenjing) + - Initial version. + http://git-wip-us.apache.org/repos/asf/trafficserver/blob/a13d99b6/plugins/experimental/custom_redirect/custom_redirect.cc ---------------------------------------------------------------------- diff --git a/plugins/experimental/custom_redirect/custom_redirect.cc b/plugins/experimental/custom_redirect/custom_redirect.cc new file mode 100644 index 0000000..bd5e193 --- /dev/null +++ b/plugins/experimental/custom_redirect/custom_redirect.cc @@ -0,0 +1,171 @@ +/** @file + + A brief file description + + @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. + */ + +/* custom_redirect.cc: Allows read header set by origin for internal redirects + */ + +#include <stdio.h> +#include <string> +#include <iostream> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <netdb.h> +#include <ts/ts.h> +#include <string.h> +#include <stdlib.h> + +static char* redirect_url_header = NULL; +static int redirect_url_header_len = 0; +static int return_code = TS_HTTP_STATUS_NONE; + +static void +handle_response (TSHttpTxn txnp,TSCont contp) +{ + TSMBuffer resp_bufp; + TSMLoc resp_loc; + TSMBuffer req_bufp; + TSMLoc req_loc; + TSMLoc redirect_url_loc; + TSHttpStatus status; + const char *redirect_url_str; + int redirect_url_length; + + if (TSHttpTxnServerRespGet (txnp, &resp_bufp, &resp_loc) != TS_SUCCESS) { + TSError ("couldn't retrieve server response header\n"); + } + else { + if ( (status = TSHttpHdrStatusGet (resp_bufp, resp_loc)) == TS_HTTP_STATUS_NONE ) { + TSError ("couldn't retrieve status from client response header\n"); + } + else { + if(TSHttpTxnClientReqGet (txnp, &req_bufp, &req_loc) != TS_SUCCESS) { + TSError ("couldn't retrieve server response header\n"); + } + else { + int method_len; + const char *method = TSHttpHdrMethodGet(req_bufp, req_loc, &method_len); + if ((return_code == TS_HTTP_STATUS_NONE || return_code == status) && ((strncasecmp(method, TS_HTTP_METHOD_GET, TS_HTTP_LEN_GET) == 0))) { + redirect_url_loc = TSMimeHdrFieldFind (resp_bufp, resp_loc, redirect_url_header, redirect_url_header_len); + + if (redirect_url_loc) { + redirect_url_str = TSMimeHdrFieldValueStringGet (resp_bufp, resp_loc, redirect_url_loc, 0, &redirect_url_length); + if (redirect_url_str) { + if (redirect_url_length > 0) { + TSRedirectUrlSet(txnp, redirect_url_str, redirect_url_length); + } + //TSHandleStringRelease(resp_bufp, redirect_url_loc, redirect_url_str); + } + TSHandleMLocRelease (resp_bufp, resp_loc, redirect_url_loc); + } + } + //TSHandleStringRelease(req_bufp, req_loc, method); + TSHandleMLocRelease (req_bufp, TS_NULL_MLOC, req_loc); + } + } + TSHandleMLocRelease (resp_bufp, TS_NULL_MLOC, resp_loc); + } + TSHttpTxnReenable (txnp, TS_EVENT_HTTP_CONTINUE); +} + + +static int +plugin_main_handler (TSCont contp, TSEvent event, void *edata) +{ + switch (event) + { + case TS_EVENT_HTTP_READ_RESPONSE_HDR: + { + TSHttpTxn txnp = (TSHttpTxn) edata; + TSDebug( "[custom_redirect1]", "MAIN_HANDLER::TS_HTTP_READ_RESPONSE_HDR_HOOK" ); + handle_response(txnp, contp); + break; + } + + + default: + { + TSDebug( "[custom_redirect]", "default event"); + break; + } + } + + return 0; +} + +bool +isNumber(const char *str) +{ + for (int i=0; str[i] != '\0'; i++) { + if (!isdigit(str[i])) { + return false; + } + } + return true; +} + +void +TSPluginInit (int argc, const char *argv[]) +{ + //TSPluginRegistrationInfo info; + +/* + info.plugin_name = (char*)""; + info.vendor_name = (char*)""; + info.support_email = (char*)""; +*/ + /* This plugin supports following types of url redirect here: + * + * 1. User can specify a particular redirect-url header name in the plugin command line, + * in which case plugin will just look for that header in response and redirect to it. + * + *OR: + * 2. User can also specify a return error code, in which case if the code matches with + * the response, plugin will look for the standard "Location" header and redirect to it + * + *OR: + * 3. If nothing specified, plugin will assume the first case and use the default redirect-url + * header name "x-redirect-url" + */ + if (argc > 1) { + if (isNumber(argv[1])) { + return_code = atoi(argv[1]); + redirect_url_header = TSstrdup(TS_MIME_FIELD_LOCATION); + } else { + redirect_url_header = TSstrdup(argv[1]); + } + } + else { + // default header name is x-redirect-url + redirect_url_header = TSstrdup("x-redirect-url"); + redirect_url_header_len = strlen(redirect_url_header); + } + /* + if (!TSPluginRegister (TS_SDK_VERSION_5_2 , &info)) { + TSError ("[custom_redirect] Plugin registration failed."); + } + */ + TSError("[custom_redirect] Plugin registered successfully."); + TSCont mainCont = TSContCreate(plugin_main_handler, NULL); + TSHttpHookAdd (TS_HTTP_READ_RESPONSE_HDR_HOOK, mainCont); +} +
