Change these two bad boys from DOS line feeds to real one...
Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/0c8fba41 Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/0c8fba41 Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/0c8fba41 Branch: refs/heads/5.0.x Commit: 0c8fba41036958085585ebcceac0749a1d6159e5 Parents: 70c70ca Author: Leif Hedstrom <[email protected]> Authored: Thu Nov 21 11:00:59 2013 -0700 Committer: Leif Hedstrom <[email protected]> Committed: Thu Nov 21 11:00:59 2013 -0700 ---------------------------------------------------------------------- README-EC2 | 268 +++++----- plugins/experimental/mysql_remap/mysql_remap.cc | 498 +++++++++---------- 2 files changed, 383 insertions(+), 383 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/trafficserver/blob/0c8fba41/README-EC2 ---------------------------------------------------------------------- diff --git a/README-EC2 b/README-EC2 index 013e93f..52c8449 100644 --- a/README-EC2 +++ b/README-EC2 @@ -1,134 +1,134 @@ -Apache TrafficServer AMIs for EC2 -Author: Jason Giedymin - ---TOC-- -1.0 Ubuntu Based Images -1.1 Ubuntu AMI Details -1.2 What is installed? - -2.0 Fedora Based Images -2.1 Fedora AMI Details -2.2 What is installed? - -3.0 The ATS Installation -3.1 Additional Update Functionality -3.2 User customized settings -3.3 Script Breakout - -4.0 FAQ -4.1 I use EC2 and I'm still confused!? -4.2 What is EC2? -4.3 I don't use EC2 or have an AWS account, can I still use these scripts on - my Virtual Machine or dedicated Ubuntu/Fedora Linux Box? -4.4 How do I contact you for help? -4.5 Will these AMIs be updated in the future? - -------- - -1.0 Ubuntu Based Images - This is the prefered image as Ubuntu is kept up to date by Canonical. - -1.1 Ubuntu AMI Details - Name: Amuxbit-Karmic-ApacheTrafficServer-AMI - x86_64 AMI: ami-ada24cc4, kernel: aki-fd15f694, Ramdisk: ari-7b739e12 - i386 AMI: ami-5ba34d32, kernel: aki-5f15f636, Ramdisk: ari-d5709dbc - -1.2 What is installed? - All packages other than Apache TrafficServer are from the ec2 ubuntu - package repositories. All software installed should be open source. - * Official Canonical Ubuntu Karmic 9.10 EC2 AMI - * Dist Upgraded as of 4/1/2010 - * Apache TrafficServer - Trunk (can be updated at any time) - * openjdk-6-jre (for ec2 tools) - * ruby (for ec2 tools) - * libopenssl-ruby (for ec2 tools) - * Apache2 (httpd, used for testing) - -2.0 Fedora Based Images - -2.1 Fedora AMI Details - Name: Amuxbit-Fedora8-ApacheTrafficServer-AMI - x86_64 AMI: ami-c3a04eaa, kernel: aki-a3d737ca, Ramdisk: ari-7cb95a15 - i386 AMI: ami-53a04e3a, kernel: aki-6eaa4907, Ramdisk: ari-42b95a2b - -2.2 What is installed? - All packages other than Apache TrafficServer are from the ec2 fedora - package repositories. All software installed should be open source. - * Official Fedora Core 8 EC2 AMI - * Dist Upgraded as of 4/1/2010 - * Apache TrafficServer - Trunk (can be updated at any time) - * java-1.7.0-icedtea (openjdk for ec2 tools) - * openssl-devel (for ec2 tools) - * ruby (for ec2 tools) - * httpd (Apache web server, used for testing) - -3.0 The ATS Installation - Apache Traffic Server is installed in: '/ats'. - The installation script used to install ATS is contained within the contrib - directory, part of the ATS source distribution. The script is included - within the '/ats' folder for actual use. - -3.1 Additional Update Functionality - The EC2 images provided contain additional scripts to maintain freshness - of the install scripts. At one time the scripts provided 'auto-heal' - functionality but that has been removed. Running /ats/install_update.sh - will keep the ATS installation up to date. - -3.2 User customized settings - Any customized settings will be lost if the update/install scripts are run. - Upon executing the install/update scripts you may copy your files back into - the config directory. Users should periodically backup your config files, - and keep abrest of any config changes as new versions of ATS are released. - The latter reason being why I did not auto-copy configs. While I've tried - to lessen as much pain for any Sys Admins, ultimately you must take charge - of these critical settings as updates will and do happen frequently to the - ATS source. - - An example script which alters settings can be found in - '/ats/contrib/set_trafficserver.sh' - This has been used on the AMIs already provided. - -3.3 Script Breakout - - ats - |-- contrib - | -- install_trafficserver.sh : installs ATS. Try not to run this directly. - | -- set_trafficserver.sh : sample config/testing/benchmark base script. - You may want to learn from/use this script. - ` -- update_scripts.sh : used to update install_trafficserver.sh, - and set_trafficserver.sh from trunk. - |-- install_update.sh : the 'easy button' script to re-install ATS. - Executes update_scripts.sh, then install_trafficserver.sh. - This is what you want to run. - `-- readme : extra information. - -4.0 FAQ - -4.1 I use EC2 and I'm still confused!? - With your EC2 account, refer to one of the AMIs listed in this doc - (Ubuntu/Fedora). Image architectures provided are i386, and x86_64. - 64bit is offered for those who need to spin up a big high powered instances. - Once running (and it may take a while) you will find yourself with a Linux - distribution with Apache Traffic Server already installed. If you'd like - to re-install ATS against trunk, run '/ats/install_update.sh'. Note that - you will have to backup and/or optimize your configuration (see 3.2). - To further setup ATS see the TrafficServer wiki @ - - http://cwiki.apache.org/confluence/display/TS/ - -4.2 What is EC2? - See http://aws.amazon.com/ - -4.3 I don't use EC2 or have an AWS account, can I still use these scripts on - my Virtual Machine or dedicated Ubuntu/Fedora Linux Box? - Absolutely! The /contrib scripts are actually meant for Ubuntu/Linux - Installs first, EC2 second. You can find them on the ATS SVN/Git - repository under '/contrib'. - -4.4 How do I contact you for help? - For mailing lists, and IRC information, visit our web page at - - http://trafficserver.apache.org/ - -4.5 Will these AMIs be updated in the future? - Absolutely! +Apache TrafficServer AMIs for EC2 +Author: Jason Giedymin + +--TOC-- +1.0 Ubuntu Based Images +1.1 Ubuntu AMI Details +1.2 What is installed? + +2.0 Fedora Based Images +2.1 Fedora AMI Details +2.2 What is installed? + +3.0 The ATS Installation +3.1 Additional Update Functionality +3.2 User customized settings +3.3 Script Breakout + +4.0 FAQ +4.1 I use EC2 and I'm still confused!? +4.2 What is EC2? +4.3 I don't use EC2 or have an AWS account, can I still use these scripts on + my Virtual Machine or dedicated Ubuntu/Fedora Linux Box? +4.4 How do I contact you for help? +4.5 Will these AMIs be updated in the future? + +------- + +1.0 Ubuntu Based Images + This is the prefered image as Ubuntu is kept up to date by Canonical. + +1.1 Ubuntu AMI Details + Name: Amuxbit-Karmic-ApacheTrafficServer-AMI + x86_64 AMI: ami-ada24cc4, kernel: aki-fd15f694, Ramdisk: ari-7b739e12 + i386 AMI: ami-5ba34d32, kernel: aki-5f15f636, Ramdisk: ari-d5709dbc + +1.2 What is installed? + All packages other than Apache TrafficServer are from the ec2 ubuntu + package repositories. All software installed should be open source. + * Official Canonical Ubuntu Karmic 9.10 EC2 AMI + * Dist Upgraded as of 4/1/2010 + * Apache TrafficServer - Trunk (can be updated at any time) + * openjdk-6-jre (for ec2 tools) + * ruby (for ec2 tools) + * libopenssl-ruby (for ec2 tools) + * Apache2 (httpd, used for testing) + +2.0 Fedora Based Images + +2.1 Fedora AMI Details + Name: Amuxbit-Fedora8-ApacheTrafficServer-AMI + x86_64 AMI: ami-c3a04eaa, kernel: aki-a3d737ca, Ramdisk: ari-7cb95a15 + i386 AMI: ami-53a04e3a, kernel: aki-6eaa4907, Ramdisk: ari-42b95a2b + +2.2 What is installed? + All packages other than Apache TrafficServer are from the ec2 fedora + package repositories. All software installed should be open source. + * Official Fedora Core 8 EC2 AMI + * Dist Upgraded as of 4/1/2010 + * Apache TrafficServer - Trunk (can be updated at any time) + * java-1.7.0-icedtea (openjdk for ec2 tools) + * openssl-devel (for ec2 tools) + * ruby (for ec2 tools) + * httpd (Apache web server, used for testing) + +3.0 The ATS Installation + Apache Traffic Server is installed in: '/ats'. + The installation script used to install ATS is contained within the contrib + directory, part of the ATS source distribution. The script is included + within the '/ats' folder for actual use. + +3.1 Additional Update Functionality + The EC2 images provided contain additional scripts to maintain freshness + of the install scripts. At one time the scripts provided 'auto-heal' + functionality but that has been removed. Running /ats/install_update.sh + will keep the ATS installation up to date. + +3.2 User customized settings + Any customized settings will be lost if the update/install scripts are run. + Upon executing the install/update scripts you may copy your files back into + the config directory. Users should periodically backup your config files, + and keep abrest of any config changes as new versions of ATS are released. + The latter reason being why I did not auto-copy configs. While I've tried + to lessen as much pain for any Sys Admins, ultimately you must take charge + of these critical settings as updates will and do happen frequently to the + ATS source. + + An example script which alters settings can be found in + '/ats/contrib/set_trafficserver.sh' + This has been used on the AMIs already provided. + +3.3 Script Breakout + + ats + |-- contrib + | -- install_trafficserver.sh : installs ATS. Try not to run this directly. + | -- set_trafficserver.sh : sample config/testing/benchmark base script. + You may want to learn from/use this script. + ` -- update_scripts.sh : used to update install_trafficserver.sh, + and set_trafficserver.sh from trunk. + |-- install_update.sh : the 'easy button' script to re-install ATS. + Executes update_scripts.sh, then install_trafficserver.sh. + This is what you want to run. + `-- readme : extra information. + +4.0 FAQ + +4.1 I use EC2 and I'm still confused!? + With your EC2 account, refer to one of the AMIs listed in this doc + (Ubuntu/Fedora). Image architectures provided are i386, and x86_64. + 64bit is offered for those who need to spin up a big high powered instances. + Once running (and it may take a while) you will find yourself with a Linux + distribution with Apache Traffic Server already installed. If you'd like + to re-install ATS against trunk, run '/ats/install_update.sh'. Note that + you will have to backup and/or optimize your configuration (see 3.2). + To further setup ATS see the TrafficServer wiki @ + + http://cwiki.apache.org/confluence/display/TS/ + +4.2 What is EC2? + See http://aws.amazon.com/ + +4.3 I don't use EC2 or have an AWS account, can I still use these scripts on + my Virtual Machine or dedicated Ubuntu/Fedora Linux Box? + Absolutely! The /contrib scripts are actually meant for Ubuntu/Linux + Installs first, EC2 second. You can find them on the ATS SVN/Git + repository under '/contrib'. + +4.4 How do I contact you for help? + For mailing lists, and IRC information, visit our web page at + + http://trafficserver.apache.org/ + +4.5 Will these AMIs be updated in the future? + Absolutely! http://git-wip-us.apache.org/repos/asf/trafficserver/blob/0c8fba41/plugins/experimental/mysql_remap/mysql_remap.cc ---------------------------------------------------------------------- diff --git a/plugins/experimental/mysql_remap/mysql_remap.cc b/plugins/experimental/mysql_remap/mysql_remap.cc index c93274e..3dcc3fa 100644 --- a/plugins/experimental/mysql_remap/mysql_remap.cc +++ b/plugins/experimental/mysql_remap/mysql_remap.cc @@ -1,249 +1,249 @@ -/* - 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 <ts/remap.h> -#include <stdio.h> -#include <unistd.h> - -#include "mysql/mysql.h" -#include "lib/iniparser.h" -#include "default.h" - -MYSQL mysql; - -typedef struct { - char * query; -} my_data; - -bool do_mysql_remap(TSCont contp,TSHttpTxn txnp) { - TSMBuffer reqp; - TSMLoc hdr_loc, url_loc, field_loc; - bool ret_val = false; - - const char * request_host; - int request_host_length = 0; - const char * request_scheme; - int request_scheme_length = 0; - int request_port = 80; - char * query; - - MYSQL_ROW row; - MYSQL_RES *res; - - my_data * data = (my_data*) TSContDataGet(contp); - query = data->query; - - if (TSHttpTxnClientReqGet(txnp, &reqp, &hdr_loc) != TS_SUCCESS) { - TSDebug(PLUGIN_NAME,"could not get request data"); - return false; - } - - TSHttpHdrUrlGet(reqp, hdr_loc,&url_loc); - - if (!url_loc) { - TSDebug(PLUGIN_NAME,"couldn't retrieve request url"); - goto release_hdr; - } - - field_loc = TSMimeHdrFieldFind(reqp, hdr_loc, TS_MIME_FIELD_HOST, TS_MIME_LEN_HOST); - - if (!field_loc) { - TSDebug(PLUGIN_NAME,"couldn't retrieve request HOST header"); - goto release_url; - } - - request_host = TSMimeHdrFieldValueStringGet (reqp, hdr_loc, field_loc, -1, &request_host_length); - if (!request_host_length) { - TSDebug(PLUGIN_NAME,"couldn't find request HOST header"); - goto release_field; - } - - request_scheme = TSUrlSchemeGet(reqp,url_loc,&request_scheme_length); - request_port = TSUrlPortGet(reqp,url_loc); - - TSDebug(PLUGIN_NAME," +++++MYSQL REMAP+++++ "); - - TSDebug(PLUGIN_NAME,"\nINCOMING REQUEST ->\n ::: from_scheme_desc: %.*s\n ::: from_hostname: %.*s\n ::: from_port: %d",\ - request_scheme_length,\ - request_scheme,\ - request_host_length,\ - request_host,\ - request_port - ); - - snprintf(query,QSIZE," \ - SELECT \ - t_scheme.scheme_desc, \ - t_host.hostname, \ - to_port \ - FROM map \ - INNER JOIN scheme as t_scheme ON (map.to_scheme_id = t_scheme.id) \ - INNER JOIN scheme as f_scheme ON (map.from_scheme_id = f_scheme.id) \ - INNER JOIN hostname as t_host ON (map.to_hostname_id = t_host.id) \ - INNER JOIN hostname as f_host ON (map.from_hostname_id = f_host.id) \ - WHERE \ - is_enabled=1 \ - AND f_host.hostname = '%.*s' \ - AND f_scheme.id = %d \ - AND from_port = %d \ - LIMIT 1", \ - request_host_length, \ - request_host, \ - (strcmp(request_scheme,"https") == 0) ? 2:1, \ - request_port \ - ); - - mysql_real_query(&mysql,query,(unsigned int)strlen(query)); - res = mysql_use_result(&mysql); - - if (!res) goto not_found; //TODO: define a fallback - - do { - row = mysql_fetch_row(res); - if (!row) goto not_found; - TSDebug(PLUGIN_NAME,"\nOUTGOING REQUEST ->\n ::: to_scheme_desc: %s\n ::: to_hostname: %s\n ::: to_port: %s",row[0],row[1],row[2]); - TSMimeHdrFieldValueStringSet(reqp, hdr_loc, field_loc, 0,row[1],-1); - TSUrlHostSet(reqp,url_loc,row[1],-1); - TSUrlSchemeSet(reqp,url_loc,row[0],-1); - TSUrlPortSet(reqp,url_loc,atoi(row[2])); - } while(0); - - ret_val = true; - -not_found: - if (!ret_val) { - //lets build up a nice 404 message for someone - TSHttpHdrStatusSet(reqp,hdr_loc,TS_HTTP_STATUS_NOT_FOUND); - TSHttpTxnSetHttpRetStatus(txnp,TS_HTTP_STATUS_NOT_FOUND); - } - if (res) - mysql_free_result(res); -#if (TS_VERSION_NUMBER < 2001005) - if (request_host) - TSHandleStringRelease(reqp, hdr_loc, request_host); - if (request_scheme) - TSHandleStringRelease(reqp, hdr_loc, request_scheme); -#endif -release_field: - if (field_loc) - TSHandleMLocRelease(reqp, hdr_loc, field_loc); -release_url: - if (url_loc) - TSHandleMLocRelease(reqp, hdr_loc, url_loc); -release_hdr: - if (hdr_loc) - TSHandleMLocRelease(reqp, TS_NULL_MLOC, hdr_loc); - - return ret_val; -} - -static int -mysql_remap (TSCont contp, TSEvent event, void *edata) { - TSHttpTxn txnp = (TSHttpTxn) edata; - TSEvent reenable = TS_EVENT_HTTP_CONTINUE; - - switch(event) { - case TS_EVENT_HTTP_READ_REQUEST_HDR: - TSDebug(PLUGIN_NAME,"Reading Request"); - TSSkipRemappingSet(txnp,1); - if (!do_mysql_remap(contp,txnp)) { - reenable = TS_EVENT_HTTP_ERROR; - } - break; - default: - break; - } - - TSHttpTxnReenable(txnp, reenable); - return 1; -} - -void -TSPluginInit(int argc, const char *argv[]) { - dictionary * ini; - const char * host; - int port; - const char * username; - const char * password; - const char * db; - - my_data * data = (my_data*) malloc(1*sizeof(my_data)); - - TSPluginRegistrationInfo info; - my_bool reconnect = 1; - - info.plugin_name = const_cast<char*>(PLUGIN_NAME); - info.vendor_name = const_cast<char*>("Apache Software Foundation"); - info.support_email = const_cast<char*>("[email protected]"); - - if (TSPluginRegister(TS_SDK_VERSION_2_0 , &info) != TS_SUCCESS) { - TSError("mysql_remap: plugin registration failed.\n"); - } - - if (argc != 2) { - TSError( "usage: %s /path/to/sample.ini\n", argv[0] ); - return; - } - - ini = iniparser_load(argv[1]); - if (!ini) { - TSError("Error with ini file (1)"); - TSDebug(PLUGIN_NAME,"Error parsing ini file(1)"); - return; - } - - host = iniparser_getstring(ini, "mysql_remap:mysql_host", (char*)"localhost"); - port = iniparser_getint(ini,"mysql_remap:mysql_port",3306); - username = iniparser_getstring(ini, "mysql_remap:mysql_username", NULL); - password = iniparser_getstring(ini, "mysql_remap:mysql_password", NULL); - db = iniparser_getstring(ini, "mysql_remap:mysql_database", (char*)"mysql_remap"); - - if (mysql_library_init(0, NULL, NULL)) { - TSError("Error initializing mysql client library"); - TSDebug(PLUGIN_NAME,"Error initializing mysql client library"); - return; - } - - if (!mysql_init(&mysql)) { - TSError("Could not initialize MySQL"); - TSDebug(PLUGIN_NAME,"Could not initialize MySQL"); - return; - } - - mysql_options(&mysql, MYSQL_OPT_RECONNECT, &reconnect); - - if (!mysql_real_connect(&mysql,host,username,password,db,port,NULL,0)) { - TSError("Could not connect to mysql"); - TSDebug(PLUGIN_NAME,"Could not connect to mysql: %s",mysql_error(&mysql)); - return; - } - - data->query = (char*)TSmalloc(QSIZE * sizeof(char)); //TODO: malloc smarter sizes - - TSDebug(PLUGIN_NAME, "h: %s; u: %s; p: %s; p:%d; d:%s",host,username,password,port,db); - TSCont cont = TSContCreate(mysql_remap, TSMutexCreate()); - - TSHttpHookAdd(TS_HTTP_READ_REQUEST_HDR_HOOK, cont); - - TSContDataSet (cont, (void *)data); - - TSDebug(PLUGIN_NAME, "plugin is successfully initialized [plugin mode]"); - iniparser_freedict(ini); - return; -} +/* + 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 <ts/remap.h> +#include <stdio.h> +#include <unistd.h> + +#include "mysql/mysql.h" +#include "lib/iniparser.h" +#include "default.h" + +MYSQL mysql; + +typedef struct { + char * query; +} my_data; + +bool do_mysql_remap(TSCont contp,TSHttpTxn txnp) { + TSMBuffer reqp; + TSMLoc hdr_loc, url_loc, field_loc; + bool ret_val = false; + + const char * request_host; + int request_host_length = 0; + const char * request_scheme; + int request_scheme_length = 0; + int request_port = 80; + char * query; + + MYSQL_ROW row; + MYSQL_RES *res; + + my_data * data = (my_data*) TSContDataGet(contp); + query = data->query; + + if (TSHttpTxnClientReqGet(txnp, &reqp, &hdr_loc) != TS_SUCCESS) { + TSDebug(PLUGIN_NAME,"could not get request data"); + return false; + } + + TSHttpHdrUrlGet(reqp, hdr_loc,&url_loc); + + if (!url_loc) { + TSDebug(PLUGIN_NAME,"couldn't retrieve request url"); + goto release_hdr; + } + + field_loc = TSMimeHdrFieldFind(reqp, hdr_loc, TS_MIME_FIELD_HOST, TS_MIME_LEN_HOST); + + if (!field_loc) { + TSDebug(PLUGIN_NAME,"couldn't retrieve request HOST header"); + goto release_url; + } + + request_host = TSMimeHdrFieldValueStringGet (reqp, hdr_loc, field_loc, -1, &request_host_length); + if (!request_host_length) { + TSDebug(PLUGIN_NAME,"couldn't find request HOST header"); + goto release_field; + } + + request_scheme = TSUrlSchemeGet(reqp,url_loc,&request_scheme_length); + request_port = TSUrlPortGet(reqp,url_loc); + + TSDebug(PLUGIN_NAME," +++++MYSQL REMAP+++++ "); + + TSDebug(PLUGIN_NAME,"\nINCOMING REQUEST ->\n ::: from_scheme_desc: %.*s\n ::: from_hostname: %.*s\n ::: from_port: %d",\ + request_scheme_length,\ + request_scheme,\ + request_host_length,\ + request_host,\ + request_port + ); + + snprintf(query,QSIZE," \ + SELECT \ + t_scheme.scheme_desc, \ + t_host.hostname, \ + to_port \ + FROM map \ + INNER JOIN scheme as t_scheme ON (map.to_scheme_id = t_scheme.id) \ + INNER JOIN scheme as f_scheme ON (map.from_scheme_id = f_scheme.id) \ + INNER JOIN hostname as t_host ON (map.to_hostname_id = t_host.id) \ + INNER JOIN hostname as f_host ON (map.from_hostname_id = f_host.id) \ + WHERE \ + is_enabled=1 \ + AND f_host.hostname = '%.*s' \ + AND f_scheme.id = %d \ + AND from_port = %d \ + LIMIT 1", \ + request_host_length, \ + request_host, \ + (strcmp(request_scheme,"https") == 0) ? 2:1, \ + request_port \ + ); + + mysql_real_query(&mysql,query,(unsigned int)strlen(query)); + res = mysql_use_result(&mysql); + + if (!res) goto not_found; //TODO: define a fallback + + do { + row = mysql_fetch_row(res); + if (!row) goto not_found; + TSDebug(PLUGIN_NAME,"\nOUTGOING REQUEST ->\n ::: to_scheme_desc: %s\n ::: to_hostname: %s\n ::: to_port: %s",row[0],row[1],row[2]); + TSMimeHdrFieldValueStringSet(reqp, hdr_loc, field_loc, 0,row[1],-1); + TSUrlHostSet(reqp,url_loc,row[1],-1); + TSUrlSchemeSet(reqp,url_loc,row[0],-1); + TSUrlPortSet(reqp,url_loc,atoi(row[2])); + } while(0); + + ret_val = true; + +not_found: + if (!ret_val) { + //lets build up a nice 404 message for someone + TSHttpHdrStatusSet(reqp,hdr_loc,TS_HTTP_STATUS_NOT_FOUND); + TSHttpTxnSetHttpRetStatus(txnp,TS_HTTP_STATUS_NOT_FOUND); + } + if (res) + mysql_free_result(res); +#if (TS_VERSION_NUMBER < 2001005) + if (request_host) + TSHandleStringRelease(reqp, hdr_loc, request_host); + if (request_scheme) + TSHandleStringRelease(reqp, hdr_loc, request_scheme); +#endif +release_field: + if (field_loc) + TSHandleMLocRelease(reqp, hdr_loc, field_loc); +release_url: + if (url_loc) + TSHandleMLocRelease(reqp, hdr_loc, url_loc); +release_hdr: + if (hdr_loc) + TSHandleMLocRelease(reqp, TS_NULL_MLOC, hdr_loc); + + return ret_val; +} + +static int +mysql_remap (TSCont contp, TSEvent event, void *edata) { + TSHttpTxn txnp = (TSHttpTxn) edata; + TSEvent reenable = TS_EVENT_HTTP_CONTINUE; + + switch(event) { + case TS_EVENT_HTTP_READ_REQUEST_HDR: + TSDebug(PLUGIN_NAME,"Reading Request"); + TSSkipRemappingSet(txnp,1); + if (!do_mysql_remap(contp,txnp)) { + reenable = TS_EVENT_HTTP_ERROR; + } + break; + default: + break; + } + + TSHttpTxnReenable(txnp, reenable); + return 1; +} + +void +TSPluginInit(int argc, const char *argv[]) { + dictionary * ini; + const char * host; + int port; + const char * username; + const char * password; + const char * db; + + my_data * data = (my_data*) malloc(1*sizeof(my_data)); + + TSPluginRegistrationInfo info; + my_bool reconnect = 1; + + info.plugin_name = const_cast<char*>(PLUGIN_NAME); + info.vendor_name = const_cast<char*>("Apache Software Foundation"); + info.support_email = const_cast<char*>("[email protected]"); + + if (TSPluginRegister(TS_SDK_VERSION_2_0 , &info) != TS_SUCCESS) { + TSError("mysql_remap: plugin registration failed.\n"); + } + + if (argc != 2) { + TSError( "usage: %s /path/to/sample.ini\n", argv[0] ); + return; + } + + ini = iniparser_load(argv[1]); + if (!ini) { + TSError("Error with ini file (1)"); + TSDebug(PLUGIN_NAME,"Error parsing ini file(1)"); + return; + } + + host = iniparser_getstring(ini, "mysql_remap:mysql_host", (char*)"localhost"); + port = iniparser_getint(ini,"mysql_remap:mysql_port",3306); + username = iniparser_getstring(ini, "mysql_remap:mysql_username", NULL); + password = iniparser_getstring(ini, "mysql_remap:mysql_password", NULL); + db = iniparser_getstring(ini, "mysql_remap:mysql_database", (char*)"mysql_remap"); + + if (mysql_library_init(0, NULL, NULL)) { + TSError("Error initializing mysql client library"); + TSDebug(PLUGIN_NAME,"Error initializing mysql client library"); + return; + } + + if (!mysql_init(&mysql)) { + TSError("Could not initialize MySQL"); + TSDebug(PLUGIN_NAME,"Could not initialize MySQL"); + return; + } + + mysql_options(&mysql, MYSQL_OPT_RECONNECT, &reconnect); + + if (!mysql_real_connect(&mysql,host,username,password,db,port,NULL,0)) { + TSError("Could not connect to mysql"); + TSDebug(PLUGIN_NAME,"Could not connect to mysql: %s",mysql_error(&mysql)); + return; + } + + data->query = (char*)TSmalloc(QSIZE * sizeof(char)); //TODO: malloc smarter sizes + + TSDebug(PLUGIN_NAME, "h: %s; u: %s; p: %s; p:%d; d:%s",host,username,password,port,db); + TSCont cont = TSContCreate(mysql_remap, TSMutexCreate()); + + TSHttpHookAdd(TS_HTTP_READ_REQUEST_HDR_HOOK, cont); + + TSContDataSet (cont, (void *)data); + + TSDebug(PLUGIN_NAME, "plugin is successfully initialized [plugin mode]"); + iniparser_freedict(ini); + return; +}
