• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

ReverseProxy.cc

Go to the documentation of this file.
00001 /** @file
00002 
00003   Definitions for reverse proxy
00004 
00005   @section license License
00006 
00007   Licensed to the Apache Software Foundation (ASF) under one
00008   or more contributor license agreements.  See the NOTICE file
00009   distributed with this work for additional information
00010   regarding copyright ownership.  The ASF licenses this file
00011   to you under the Apache License, Version 2.0 (the
00012   "License"); you may not use this file except in compliance
00013   with the License.  You may obtain a copy of the License at
00014 
00015       http://www.apache.org/licenses/LICENSE-2.0
00016 
00017   Unless required by applicable law or agreed to in writing, software
00018   distributed under the License is distributed on an "AS IS" BASIS,
00019   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00020   See the License for the specific language governing permissions and
00021   limitations under the License.
00022 
00023   @section details Details
00024 
00025   Implements code necessary for Reverse Proxy which mostly consists of
00026   general purpose hostname substitution in URLs.
00027 
00028  */
00029 
00030 #include "libts.h"
00031 #include <dlfcn.h>
00032 #include "Main.h"
00033 #include "Error.h"
00034 #include "P_EventSystem.h"
00035 #include "StatSystem.h"
00036 #include "P_Cache.h"
00037 #include "ProxyConfig.h"
00038 #include "ReverseProxy.h"
00039 #include "MatcherUtils.h"
00040 #include "Tokenizer.h"
00041 #include "api/ts/remap.h"
00042 #include "RemapPluginInfo.h"
00043 #include "RemapProcessor.h"
00044 #include "UrlRewrite.h"
00045 #include "UrlMapping.h"
00046 
00047 /** Time till we free the old stuff after a reconfiguration. */
00048 #define URL_REWRITE_TIMEOUT            (HRTIME_SECOND*60)
00049 
00050 // Global Ptrs
00051 static Ptr<ProxyMutex> reconfig_mutex;
00052 UrlRewrite *rewrite_table = NULL;
00053 remap_plugin_info *remap_pi_list; // We never reload the remap plugins, just append to 'em.
00054 
00055 // Tokens for the Callback function
00056 #define FILE_CHANGED 0
00057 #define REVERSE_CHANGED 1
00058 #define TSNAME_CHANGED 2
00059 #define AC_PORT_CHANGED 3
00060 #define TRANS_CHANGED 4
00061 #define DEFAULT_TO_PAC_CHANGED 5
00062 #define DEFAULT_TO_PAC_PORT_CHANGED 7
00063 #define URL_REMAP_MODE_CHANGED 8
00064 #define HTTP_DEFAULT_REDIRECT_CHANGED 9
00065 
00066 int url_remap_mode;
00067 
00068 //
00069 // Begin API Functions
00070 //
00071 
00072 int
00073 init_reverse_proxy()
00074 {
00075   ink_assert(rewrite_table == NULL);
00076   reconfig_mutex = new_ProxyMutex();
00077   rewrite_table = new UrlRewrite();
00078 
00079   if (!rewrite_table->is_valid()) {
00080     Warning("Can not load the remap table, exiting out!");
00081     // TODO: For now, I _exit() out of here, because otherwise we'll keep generating
00082     // core files (if enabled) when starting up with a bad remap.config file.
00083     _exit(-1);
00084   }
00085 
00086   REC_RegisterConfigUpdateFunc("proxy.config.url_remap.filename", url_rewrite_CB, (void *) FILE_CHANGED);
00087   REC_RegisterConfigUpdateFunc("proxy.config.proxy_name", url_rewrite_CB, (void *) TSNAME_CHANGED);
00088   REC_RegisterConfigUpdateFunc("proxy.config.reverse_proxy.enabled", url_rewrite_CB, (void *) REVERSE_CHANGED);
00089   REC_RegisterConfigUpdateFunc("proxy.config.admin.autoconf_port", url_rewrite_CB, (void *) AC_PORT_CHANGED);
00090   REC_RegisterConfigUpdateFunc("proxy.config.url_remap.default_to_server_pac", url_rewrite_CB, (void *) DEFAULT_TO_PAC_CHANGED);
00091   REC_RegisterConfigUpdateFunc("proxy.config.url_remap.default_to_server_pac_port", url_rewrite_CB, (void *) DEFAULT_TO_PAC_PORT_CHANGED);
00092   REC_RegisterConfigUpdateFunc("proxy.config.url_remap.url_remap_mode", url_rewrite_CB, (void *) URL_REMAP_MODE_CHANGED);
00093   REC_RegisterConfigUpdateFunc("proxy.config.http.referer_default_redirect", url_rewrite_CB, (void *) HTTP_DEFAULT_REDIRECT_CHANGED);
00094   return 0;
00095 }
00096 
00097 // TODO: This function needs to be rewritten (or replaced) with something that uses the new
00098 // Remap Processor properly. Right now, we effectively don't support "remap" rules on a few
00099 // odd ball configs, for example if you use the "CONNECT" method, or if you set
00100 // set proxy.config.url_remap.url_remap_mode to "2" (which is a completely undocumented "feature").
00101 bool
00102 request_url_remap(HttpTransact::State * /* s ATS_UNUSED */, HTTPHdr * /* request_header ATS_UNUSED */,
00103                   char ** /* redirect_url ATS_UNUSED */, unsigned int /* filter_mask ATS_UNUSED */)
00104 {
00105   return false;
00106   // return rewrite_table ? rewrite_table->Remap(s, request_header, redirect_url, orig_url, tag, filter_mask) : false;
00107 }
00108 
00109 /**
00110    This function is used to figure out if a URL needs to be remapped
00111    according to the rules in remap.config.
00112 */
00113 mapping_type
00114 request_url_remap_redirect(HTTPHdr *request_header, URL *redirect_url)
00115 {
00116   return rewrite_table ? rewrite_table->Remap_redirect(request_header, redirect_url) : NONE;
00117 }
00118 
00119 bool
00120 response_url_remap(HTTPHdr *response_header)
00121 {
00122   return rewrite_table ? rewrite_table->ReverseMap(response_header) : false;
00123 }
00124  
00125 
00126 //
00127 //
00128 //  End API Functions
00129 //
00130 
00131 /** Used to read the remap.config file after the manager signals a change. */
00132 struct UR_UpdateContinuation;
00133 typedef int (UR_UpdateContinuation::*UR_UpdContHandler) (int, void *);
00134 struct UR_UpdateContinuation: public Continuation
00135 {
00136   int file_update_handler(int /* etype ATS_UNUSED */, void * /* data ATS_UNUSED */)
00137   {
00138     reloadUrlRewrite();
00139     delete this;
00140     return EVENT_DONE;
00141   }
00142   UR_UpdateContinuation(ProxyMutex * m)
00143     : Continuation(m)
00144   {
00145     SET_HANDLER((UR_UpdContHandler) & UR_UpdateContinuation::file_update_handler);
00146   }
00147 };
00148 
00149 /**
00150   Called when the remap.config file changes. Since it called infrequently,
00151   we do the load of new file as blocking I/O and lock aquire is also
00152   blocking.
00153 
00154 */
00155 void
00156 reloadUrlRewrite()
00157 {
00158   UrlRewrite *newTable;
00159 
00160   Debug("url_rewrite", "remap.config updated, reloading...");
00161   newTable = new UrlRewrite();
00162   if (newTable->is_valid()) {
00163     new_Deleter(rewrite_table, URL_REWRITE_TIMEOUT);
00164     Debug("url_rewrite", "remap.config done reloading!");
00165     ink_atomic_swap(&rewrite_table, newTable);
00166   } else {
00167     static const char* msg = "failed to reload remap.config, not replacing!";
00168     delete newTable;
00169     Debug("url_rewrite", "%s", msg);
00170     Warning("%s", msg);
00171   }
00172 }
00173 
00174 int
00175 url_rewrite_CB(const char * /* name ATS_UNUSED */, RecDataT /* data_type ATS_UNUSED */, RecData data, void *cookie)
00176 {
00177   int my_token = (int) (long) cookie;
00178 
00179   switch (my_token) {
00180   case REVERSE_CHANGED:
00181     rewrite_table->SetReverseFlag(data.rec_int);
00182     break;
00183 
00184   case TSNAME_CHANGED:
00185   case DEFAULT_TO_PAC_CHANGED:
00186   case DEFAULT_TO_PAC_PORT_CHANGED:
00187   case FILE_CHANGED:
00188   case HTTP_DEFAULT_REDIRECT_CHANGED:
00189     eventProcessor.schedule_imm(new UR_UpdateContinuation(reconfig_mutex), ET_TASK);
00190     break;
00191 
00192   case AC_PORT_CHANGED:
00193     // The AutoConf port does not current change on manager except at restart
00194     break;
00195 
00196   case URL_REMAP_MODE_CHANGED:
00197     // You need to restart TS.
00198     break;
00199 
00200   default:
00201     ink_assert(0);
00202     break;
00203   }
00204 
00205   return 0;
00206 }
00207 

Generated by  doxygen 1.7.1