You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by zw...@apache.org on 2012/03/01 17:04:53 UTC
[11/11] git commit: Added regex remap plugin from Yahoo!
Added regex remap plugin from Yahoo!
git-svn-id: https://svn.apache.org/repos/asf/trafficserver/plugins/trunk@1198996 13f79535-47bb-0310-9956-ffa450edef68
Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo
Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/ae5cd07d
Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/ae5cd07d
Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/ae5cd07d
Branch: refs/heads/master
Commit: ae5cd07dfb7721269d9f267b98b3fc41abb2b2e7
Parents:
Author: Bryan Call <bc...@apache.org>
Authored: Mon Nov 7 23:10:38 2011 +0000
Committer: Bryan Call <bc...@apache.org>
Committed: Mon Nov 7 23:10:38 2011 +0000
----------------------------------------------------------------------
LICENSE | 427 ++++++++++++++++++++
Makefile | 35 ++
README | 146 +++++++
regex_remap.cc | 1093 +++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 1701 insertions(+), 0 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/ae5cd07d/LICENSE
----------------------------------------------------------------------
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..b2e6b61
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,427 @@
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright [yyyy] [name of copyright owner]
+
+ Licensed 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.
+
+~~~
+
+Copyright (C) 2009 Yahoo! Inc.
+
+Licensed 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
+
+~~~
+
+Mersenne Twister License
+
+ Copyright (C) 2004, Makoto Matsumoto and Takuji Nishimura,
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+
+ 1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ 3. The names of its contributors may not be used to endorse or promote
+ products derived from this software without specific prior written
+ permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+~~~
+
+TK 8.3 License
+
+This software is copyrighted by the Regents of the University of
+California, Sun Microsystems, Inc., and other parties. The following
+terms apply to all files associated with the software unless explicitly
+disclaimed in individual files.
+
+The authors hereby grant permission to use, copy, modify, distribute,
+and license this software and its documentation for any purpose, provided
+that existing copyright notices are retained in all copies and that this
+notice is included verbatim in any distributions. No written agreement,
+license, or royalty fee is required for any of the authorized uses.
+Modifications to this software may be copyrighted by their authors
+and need not follow the licensing terms described here, provided that
+the new terms are clearly indicated on the first page of each file where
+they apply.
+
+IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY
+FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
+ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY
+DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+
+THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
+INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE
+IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE
+NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
+MODIFICATIONS.
+
+GOVERNMENT USE: If you are acquiring this software on behalf of the
+U.S. government, the Government shall have only "Restricted Rights"
+in the software and related documentation as defined in the Federal
+Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2). If you
+are acquiring the software on behalf of the Department of Defense, the
+software shall be classified as "Commercial Computer Software" and the
+Government shall have only "Restricted Rights" as defined in Clause
+252.227-7013 (c) (1) of DFARs. Notwithstanding the foregoing, the
+authors grant the U.S. Government and others acting in its behalf
+permission to use and distribute the software in accordance with the
+terms specified in this license.
+
+~~~
+
+BIND license
+
+Copyright (c) 1985, 1989, 1993
+ The Regents of the University of California. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+3. Neither the name of the University nor the names of its contributors
+ may be used to endorse or promote products derived from this software
+ without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
+
+
+
+Portions Copyright (c) 1993 by Digital Equipment Corporation.
+
+Permission to use, copy, modify, and distribute this software for any
+purpose with or without fee is hereby granted, provided that the above
+copyright notice and this permission notice appear in all copies, and that
+the name of Digital Equipment Corporation not be used in advertising or
+publicity pertaining to distribution of the document or software without
+specific, written prior permission.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
+WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
+OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
+CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
+DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
+PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
+ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+SOFTWARE.
+
+
+
+Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
+Portions Copyright (c) 1996-1999 by Internet Software Consortium.
+
+Permission to use, copy, modify, and distribute this software for any
+purpose with or without fee is hereby granted, provided that the above
+copyright notice and this permission notice appear in all copies.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
+WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
+ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
+OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+~~~
+
+Copyright (c) 1994-2011 John Bradley Plevyak, All Rights Reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+3. The name of the author may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
+
+~~~
+
+Copyright (C) 2007 Ariya Hidayat (ariya@kde.org)
+Copyright (C) 2006 Ariya Hidayat (ariya@kde.org)
+Copyright (C) 2005 Ariya Hidayat (ariya@kde.org)
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+
+
+For the strlcat, strlcpy in inktomi++/ink_string.cc:
+
+Copyright (c) 1998 Todd C. Miller <To...@courtesan.com>
+
+Permission to use, copy, modify, and distribute this software for any
+purpose with or without fee is hereby granted, provided that the above
+copyright notice and this permission notice appear in all copies.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/ae5cd07d/Makefile
----------------------------------------------------------------------
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..e517796
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,35 @@
+#
+# Makefile for regex_remap plugin
+#
+# 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.
+#
+OPTFLAG=-O3 -g
+
+# Set INCFLAG to wherever you installed Apache TS include files
+# INCFLAG=-I/opt/trafficserver/include
+CXXFLAGS=-fPIC -Wall -Werror $(INCFLAG)
+LDFLAGS=-lpcre
+
+%.so : %.o
+ $(CXX) $(LDFLAGS) -shared -fPIC -o $@ $<
+
+all:: regex_remap.so
+
+regex_remap.so: regex_remap.o
+
+clean::
+ rm -f *.o *.so
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/ae5cd07d/README
----------------------------------------------------------------------
diff --git a/README b/README
new file mode 100644
index 0000000..c965848
--- /dev/null
+++ b/README
@@ -0,0 +1,146 @@
+This is a plugin for the Apache Traffic Server, that allows you to
+configure mapping rules based on regular expressions. This is similar to
+what you can accomplish using mod_rewrite in apache, but obviously not
+as flexible or sophisticated (yet).
+
+To use this plugin, configure a remap.config rule like
+
+ map http://a.com http://b.com @plugin=regex_remap.so @pparam=maps.reg
+
+
+An optional argument (@@pparam) with the string "profile" will enable
+profiling of this regex remap rule, e.g.
+
+ ... @pparam=maps.reg @pparam=profile
+
+
+Profiling is very low overhead, and the information is dumped to
+traffic.out, typically in /usr/local/var/logs/trafficserver/traffic.out.
+In order to force a profile dump, you can do
+
+ $ sudo touch /usr/local/etc/trafficserver/remap.config
+ $ sudo traffic_line -x
+
+
+By default, only the path and query string of the URL is
+provided for the regular expressions to match. The following optional
+parameters can be used to modify the plugin instance behavior:
+
+ @pparam=[no-]full-url [default: off]
+ @pparam=[no-]method [default: off]
+ @pparam=[no-]query-string [default: on]
+ @pparam=[no-]matrix-parameters [default: off]
+
+
+
+If you want the full (original) URL, use the parameter @pparam=full-url.
+For example:
+
+ ... @pparam=maps.reg @pparam=full-url
+
+
+The string that you will need to match against looks like
+
+ http://server/path?query=bar
+
+
+If you also wish to match on the HTTP method used (e.g. "GET"), you must
+use the option @pparam=method. For example:
+
+ ... @pparam=maps.reg @pparam=method
+
+
+With this enabled, the string that you will need to match will look like
+
+ GET/path?query=bar
+
+
+The "method" parameter can also be used in combination with "full-url",
+and the string to match against will then look like
+
+ GEThttp://server.com/path?query=bar
+
+
+The methods are always all upper-case, and always followed by one single
+space. There is no space between the method and the rest of the URL (or
+URI path).
+
+By default, the query string is part of the string that is matched again,
+to turn this off use the option 'no-query-string', e.g.
+
+ ... @pparam=maps.reg @pparam=no-query-string
+
+
+Finally, you can also include the matrix parameters in the string, using the
+option 'matrix-parameters', e.g.
+
+ ... @pparam=maps.reg @pparam=matrix-parameters
+
+
+Note that the path to the plugin must be absolute, and by default it is
+
+ /usr/local/libexec/trafficserver/regex_remap.so
+
+
+The config file (maps.reg above) can be placed anywhere, but unless you
+specify an absolute path (as above), it will default to the directory
+
+ /usr/local/etc/regex_remap
+
+
+A typical regex would look like
+
+ ^/(ogre.*)/more http://www.ogre.com/$h/$0/$1
+
+
+The regular expression must not contain any white spaces!
+
+When the regular expression is matched, only the URL path + query string is
+matched (without any of the optional configuration options). The path
+will always start with a "/". Various substitution strings are allowed
+on the right hand side:
+
+ $0 - The entire matched string
+ $1-9 - Regular expression groups ($1 first group etc.)
+ $h - The original host header from the request
+ $f - The host as used in the "from" portion of the remap rule
+ $t - The host as used in the "to" portion of the remap rule
+ $p - The original port number
+ $s - The scheme (e.g. http) of the request
+ $P - The entire path of the request
+ $q - The query part of the request
+ $r - The path parameters of the request (not implemented yet)
+ $c - The cookie string from the request
+ $i - The client IP for this request
+
+
+You can also provide options, similar to how you configure your
+remap.config. The following options are available
+
+ @status=<nnn> - Force the response code to <nnn>
+ @active_timeout=<nnn> - Active timeout (in ms)
+ @no_activity_timeout=<nnn> - No activity timeout (in ms)
+ @connect_timeout=<nnn> - Connect timeouts (in ms)
+ @dns_timeout=<nnn> - Connect timeouts (in ms)
+
+
+For example, this can be useful to force a particular response for some
+URLs, e.g.
+
+ ^/(ogre.*)/bad http://www.examle.com/ @status=404
+
+
+Or, to force a 302 redirect
+
+ ^/oldurl/(.*)$ http://news.example.com/new/$1 @status=302
+
+
+Note: Setting the status to 301 or 302 will force the new URL to be used
+as a redirect (Location:).
+
+
+RELEASES
+--------
+
+Version 2.0
+ - Initial Open Source release.
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/ae5cd07d/regex_remap.cc
----------------------------------------------------------------------
diff --git a/regex_remap.cc b/regex_remap.cc
new file mode 100644
index 0000000..efa33b0
--- /dev/null
+++ b/regex_remap.cc
@@ -0,0 +1,1093 @@
+/** @file
+
+ ATS plugin to do (simple) regular expression remap rules
+
+ @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.
+*/
+
+//////////////////////////////////////////////////////////////////////////////////////////////
+//
+// To use this plugin, configure a remap.config rule like
+//
+// map http://foo.com http://bar.com @plugin=.../libexec/regex_remap.so @pparam=maps.reg
+//
+// An optional argument (@@pparam) with the string "profile" will enable profiling
+// of this regex remap rule, e.g.
+//
+// ... @pparam=maps.reg @pparam=profile
+//
+// Profiling is very low overhead, and the information is dumped to traffic.out, which
+// is typically in /usr/local/var/logs/trafficserver/traffic.out. In order to force a profile
+// dump, you can do
+//
+// $ sudo touch /usr/local/etc/trafficserver/remap.config
+// $ sudo traffic_line -x
+//
+// By default, only the path (and query arguments etc.) of the URL is provided for the
+// regular expressions to match. If you want the full (original) URL, use the parameter
+// @pparam=full-url. For example:
+//
+// ... @pparam=maps.reg @pparam=full-url
+//
+// The string that you will need to match against looks like
+//
+// http://server.com/path?query=bar
+//
+// If you also wish to match on the HTTP method used (e.g. "GET"), you must use the
+// option @pparam=method. For example:
+//
+// ... @pparam=maps.reg @pparam=method
+//
+// With this enabled, the string that you will need to match will look like
+//
+// GET/path?query=bar
+//
+// The "method" parameter can also be used in combination with "full-url", and the
+// string to match against will then look like
+//
+// GEThttp://server.com/path?query=bar
+//
+// The methods are always all upper-case, and always followed by one single space. There
+// is no space between the method and the rest of the URL (or URI path).
+//
+//
+// Note that the path to the plugin itself must be absolute, and by default it is
+//
+// /usr/local/libexec/trafficserver/regex_remap.so
+//
+// The config file (maps.reg above) can be placed anywhere, but unless you specify an
+// absolute path (as above), it will default to the directory
+//
+// /usr/local/etc/regex_remap
+//
+
+#define UNUSED __attribute__ ((unused))
+static char UNUSED rcsId__regex_remap_cc[] = "@(#) $Id$ built on " __DATE__ " " __TIME__;
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <time.h>
+#include <string.h>
+#include <pcre.h>
+#include <ctype.h>
+#include <unistd.h>
+
+#include <ts/ts.h>
+#include <ts/remap.h>
+
+#include <iostream>
+#include <fstream>
+#include <string>
+
+
+// This is copied from libinktomi++/*.h, only works with gcc 4.x or later (and compatible).
+// TODO: We really ought to expose these data types and atomic functions to the plugin APIs.
+typedef int int32;
+typedef volatile int32 vint32;
+typedef vint32 *pvint32;
+
+static inline int ink_atomic_increment(pvint32 mem, int value)
+{
+ return __sync_fetch_and_add(mem, value);
+}
+
+
+// Constants
+const int OVECCOUNT = 30; // We support $0 - $9 x2 ints, and this needs to be 1.5x that
+const int MAX_SUBS = 32; // No more than 32 substitution variables in the subst string
+
+// TODO: This should be "autoconf'ed" or something ...
+#define DEFAULT_PATH "/usr/local/etc/regex_remap/"
+
+// Substitutions other than regex matches
+enum ExtraSubstitutions {
+ SUB_HOST = 11,
+ SUB_FROM_HOST = 12,
+ SUB_TO_HOST = 13,
+ SUB_PORT = 14,
+ SUB_SCHEME = 15,
+ SUB_PATH = 16,
+ SUB_QUERY = 17,
+ SUB_COOKIE = 18,
+ SUB_CLIENT_IP = 19
+};
+
+
+///////////////////////////////////////////////////////////////////////////////
+// Class encapsulating one regular expression (and the linked list).
+//
+class RemapRegex
+{
+ public:
+ RemapRegex(const std::string& reg, const std::string& sub, const std::string& opt) :
+ _num_subs(-1), _rex(NULL), _extra(NULL), _order(-1), _simple(false),
+ _active_timeout(-1), _no_activity_timeout(-1), _connect_timeout(-1), _dns_timeout(-1)
+ {
+ INKDebug("regex_remap", "Calling constructor");
+
+ _status = static_cast<INKHttpStatus>(0);
+
+ if (!reg.empty()) {
+ if (reg == ".") {
+ INKDebug("regex_remap", "Rule is simple, and fast!");
+ _simple = true;
+ }
+ _rex_string = INKstrdup(reg.c_str());
+ } else
+ _rex_string = NULL;
+
+ if (!sub.empty()) {
+ _subst = INKstrdup(sub.c_str());
+ _subst_len = sub.length();
+ } else {
+ _subst = NULL;
+ _subst_len = 0;
+ }
+
+ _hits = 0;
+
+ memset(_sub_pos, 0, sizeof(_sub_pos));
+ memset(_sub_ix, 0, sizeof(_sub_ix));
+ _next = NULL;
+
+ // Parse options
+ std::string::size_type start = opt.find_first_of("@");
+ std::string::size_type pos1, pos2;
+
+ while (start != std::string::npos) {
+ std::string opt_val;
+
+ ++start;
+ pos1 = opt.find_first_of("=", start);
+ if (pos1 == std::string::npos) {
+ INKError("Malformed options: %s", opt.c_str());
+ break;
+ }
+ ++pos1;
+ pos2 = opt.find_first_of(" \t\n", pos1);
+ if (pos2 == std::string::npos)
+ pos2 = opt.length();
+ opt_val = opt.substr(pos1, pos2-pos1);
+
+ if (opt.compare(start, 6, "status") == 0) {
+ _status = static_cast<INKHttpStatus>(atoi(opt_val.c_str()));
+ } else if (opt.compare(start, 14, "active_timeout") == 0) {
+ _active_timeout = atoi(opt_val.c_str());
+ } else if (opt.compare(start, 19, "no_activity_timeout") == 0) {
+ _no_activity_timeout = atoi(opt_val.c_str());
+ } else if (opt.compare(start, 15, "connect_timeout") == 0) {
+ _connect_timeout = atoi(opt_val.c_str());
+ } else if (opt.compare(start, 11, "dns_timeout") == 0) {
+ _dns_timeout = atoi(opt_val.c_str());
+ } else {
+ INKError("Unknown options: %s", opt.c_str());
+ }
+ start = opt.find_first_of("@", pos2);
+ }
+ };
+
+ ~RemapRegex()
+ {
+ INKDebug("regex_remap", "Calling destructor");
+ if (_rex_string)
+ INKfree(_rex_string);
+ if (_subst)
+ INKfree(_subst);
+
+ if (_rex)
+ pcre_free(_rex);
+ if (_extra)
+ pcre_free(_extra);
+ };
+
+ // For profiling information
+ inline void
+ print(int ix, int max, const char* now)
+ {
+ fprintf(stderr, "[%s]:\tRegex %d ( %s ): %.2f%%\n", now, ix, _rex_string, 100.0 * _hits / max);
+ }
+
+ inline void
+ increment()
+ {
+ ink_atomic_increment(&(_hits), 1);
+ }
+
+ // Compile and study the regular expression.
+ int
+ compile(const char** error, int* erroffset)
+ {
+ char* str;
+ int ccount;
+
+ _rex = pcre_compile(_rex_string, // the pattern
+ 0, // default options
+ error, // for error message
+ erroffset, // for error offset
+ NULL); // use default character tables
+
+ if (NULL == _rex)
+ return -1;
+
+ _extra = pcre_study(_rex, 0, error);
+ if ((_extra == NULL) && (*error != 0))
+ return -1;
+
+ if (pcre_fullinfo(_rex, _extra, PCRE_INFO_CAPTURECOUNT, &ccount) != 0)
+ return -1;
+
+ // Get some info for the string substitutions
+ str = _subst;
+ _num_subs = 0;
+
+ while (str && *str) {
+ if ((*str == '$')) {
+ int ix = -1;
+
+ if (isdigit(*(str+1))) {
+ ix = *(str + 1) - '0';
+ } else {
+ switch (*(str + 1)) {
+ case 'h':
+ ix = SUB_HOST;
+ break;
+ case 'f':
+ ix = SUB_FROM_HOST;
+ break;
+ case 't':
+ ix = SUB_TO_HOST;
+ break;
+ case 'p':
+ ix = SUB_PORT;
+ break;
+ case 's':
+ ix = SUB_SCHEME;
+ break;
+ case 'P':
+ ix = SUB_PATH;
+ break;
+ case 'q':
+ ix = SUB_QUERY;
+ break;
+ case 'c':
+ ix = SUB_COOKIE;
+ break;
+ case 'i':
+ ix = SUB_CLIENT_IP;
+ break;
+ default:
+ break;
+ }
+ }
+
+ if (ix > -1) {
+ if ((ix < 10) && (ix > ccount)) {
+ INKDebug("regex_remap", "Trying to use unavailable substitution, check the regex!");
+ return -1; // No substitutions available other than $0
+ }
+
+ _sub_ix[_num_subs] = ix;
+ _sub_pos[_num_subs] = (str - _subst);
+ str += 2;
+ ++_num_subs;
+ } else { // Not a valid substitution character, so just ignore it
+ ++str;
+ }
+ } else {
+ ++str;
+ }
+ }
+ return 0;
+ };
+
+ // Perform the regular expression matching against a string.
+ int
+ match(const char* str, int len, int ovector[])
+ {
+ return pcre_exec(_rex, // the compiled pattern
+ _extra, // Extra data from study (maybe)
+ str, // the subject string
+ len, // the length of the subject
+ 0, // start at offset 0 in the subject
+ 0, // default options
+ ovector, // output vector for substring information
+ OVECCOUNT); // number of elements in the output vector
+ };
+
+ // Get the lengths of the matching string(s), taking into account variable substitutions.
+ // We also calculate a total length for the new string, which is the max length the
+ // substituted string can have (use it to allocate a buffer before calling substitute() ).
+ int
+ get_lengths(const int ovector[], int lengths[], TSRemapRequestInfo *rri)
+ {
+ int len = _subst_len + 1; // Bigger then necessary
+
+ for (int i=0; i < _num_subs; i++) {
+ int ix = _sub_ix[i];
+
+ if (ix < 10) {
+ lengths[ix] = ovector[2*ix+1] - ovector[2*ix]; // -1 - -1 == 0
+ len += lengths[ix];
+ } else {
+ switch (ix) {
+ case SUB_HOST:
+ len += rri->request_host_size;
+ break;
+ case SUB_FROM_HOST:
+ len += rri->remap_from_host_size;
+ break;
+ case SUB_TO_HOST:
+ len += rri->remap_to_host_size;
+ break;
+ case SUB_PORT:
+ len += 6; // One extra for snprintf()
+ break;
+ case SUB_SCHEME:
+ len += rri->from_scheme_len;
+ break;
+ case SUB_PATH:
+ len += rri->request_path_size;
+ break;
+ case SUB_QUERY:
+ len += rri->request_query_size;
+ break;
+ case SUB_COOKIE:
+ len += rri->request_cookie_size;
+ break;
+ case SUB_CLIENT_IP:
+ len += 15; // Allow for 255.255.255.255
+ break;
+ default:
+ break;
+ }
+ }
+ }
+
+ return len;
+ };
+
+ // Perform substitution on the $0 - $9 variables in the "src" string. $0 is the entire
+ // regex that was matches, while $1 - $9 are the corresponding groups. Return the final
+ // length of the string as written to dest (not including the trailing '0').
+ int
+ substitute(char dest[], const char *src, const int ovector[], const int lengths[], TSRemapRequestInfo *rri)
+ {
+ if (_num_subs > 0) {
+ char* p1 = dest;
+ char* p2 = _subst;
+ int prev = 0;
+
+ for (int i=0; i < _num_subs; i++) {
+ int ix = _sub_ix[i];
+
+ memcpy(p1, p2, _sub_pos[i] - prev);
+ p1 += (_sub_pos[i] - prev);
+ if (ix < 10) {
+ memcpy(p1, src + ovector[2*ix], lengths[ix]);
+ p1 += lengths[ix];
+ } else {
+ switch (ix) {
+ case SUB_HOST:
+ memcpy(p1, rri->request_host, rri->request_host_size);
+ p1 += rri->request_host_size;
+ break;
+ case SUB_FROM_HOST:
+ memcpy(p1, rri->remap_from_host, rri->remap_from_host_size);
+ p1 += rri->remap_from_host_size;
+ break;
+ case SUB_TO_HOST:
+ memcpy(p1, rri->remap_to_host, rri->remap_to_host_size);
+ p1 += rri->remap_to_host_size;
+ break;
+ case SUB_PORT:
+ p1 += snprintf(p1, 6, "%u", rri->remap_from_port);
+ break;
+ case SUB_SCHEME:
+ memcpy(p1, rri->from_scheme, rri->from_scheme_len);
+ p1 += rri->from_scheme_len;
+ break;
+ case SUB_PATH:
+ memcpy(p1, rri->request_path, rri->request_path_size);
+ p1 += rri->request_path_size;
+ break;
+ case SUB_QUERY:
+ memcpy(p1, rri->request_query, rri->request_query_size);
+ p1 += rri->request_query_size;
+ break;
+ case SUB_COOKIE:
+ memcpy(p1, rri->request_cookie, rri->request_cookie_size);
+ p1 += rri->request_cookie_size;
+ break;
+ case SUB_CLIENT_IP:
+ {
+ unsigned char *ip = (unsigned char*)&rri->client_ip;
+
+ p1 += snprintf(p1, 15, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
+ }
+ break;
+ default:
+ break;
+ }
+ }
+ p2 += (_sub_pos[i] - prev + 2);
+ prev = _sub_pos[i] + 2;
+ }
+ memcpy(p1, p2, _subst_len - (p2 - _subst));
+ p1 += _subst_len - (p2 - _subst);
+ *p1 = 0; // Make sure it's NULL terminated (for safety).
+ return p1 - dest;
+ } else {
+ memcpy(dest, _subst, _subst_len + 1); // No substitutions in the string, copy it all
+ return _subst_len;
+ }
+
+ return 0; // Shouldn't happen.
+ };
+
+ // setter / getters for members the linked list.
+ inline void set_next(RemapRegex* next) { _next = next; };
+ inline RemapRegex* next() const { return _next; };
+
+ // setter / getters for order number within the linked list
+ inline void set_order(int order) { _order = order; };
+ inline int order() { return _order; };
+
+ // Various getters
+ inline const char* regex() const { return _rex_string; };
+ inline const char* substitution() const { return _subst; };
+ inline int substitutions_used() const { return _num_subs; }
+
+ inline bool is_simple() const { return _simple; }
+
+ inline INKHttpStatus status_option() const { return _status; };
+ inline int active_timeout_option() const { return _active_timeout; };
+ inline int no_activity_timeout_option() const { return _no_activity_timeout; };
+ inline int connect_timeout_option() const { return _connect_timeout; };
+ inline int dns_timeout_option() const { return _dns_timeout; };
+
+ private:
+ char* _rex_string;
+ char* _subst;
+ int _subst_len;
+ int _num_subs;
+ int _hits;
+
+ pcre* _rex;
+ pcre_extra* _extra;
+ int _sub_pos[MAX_SUBS];
+ int _sub_ix[MAX_SUBS];
+ RemapRegex* _next;
+ int _order;
+ INKHttpStatus _status;
+ bool _simple;
+ int _active_timeout;
+ int _no_activity_timeout;
+ int _connect_timeout;
+ int _dns_timeout;
+};
+
+struct RemapInstance
+{
+ RemapInstance() :
+ first(NULL), last(NULL), profile(false), full_url(false), method(false), query_string(true),
+ matrix_params(false), hits(0), misses(0),
+ filename("unknown")
+ { };
+
+ RemapRegex* first;
+ RemapRegex* last;
+ bool profile;
+ bool full_url;
+ bool method;
+ bool query_string;
+ bool matrix_params;
+ int hits;
+ int misses;
+ std::string filename;
+};
+
+///////////////////////////////////////////////////////////////////////////////
+// Helpers for memory management (to make sure pcre uses the INK APIs).
+//
+inline void*
+ink_malloc(size_t s)
+{
+ return INKmalloc(s);
+}
+
+inline void
+ink_free(void *s)
+{
+ return INKfree(s);
+}
+
+void
+setup_memory_allocation()
+{
+ pcre_malloc = &ink_malloc;
+ pcre_free = &ink_free;
+}
+
+
+///////////////////////////////////////////////////////////////////////////////
+// Initialize the plugin.
+//
+int
+tsremap_init(TSREMAP_INTERFACE *api_info, char *errbuf, int errbuf_size)
+{
+ if (!api_info) {
+ strncpy(errbuf, "[tsremap_init] - Invalid TSREMAP_INTERFACE argument", errbuf_size - 1);
+ return -1;
+ }
+
+ if (api_info->size < sizeof(TSREMAP_INTERFACE)) {
+ strncpy(errbuf, "[tsremap_init] - Incorrect size of TSREMAP_INTERFACE structure", errbuf_size - 1);
+ return -2;
+ }
+
+ if (api_info->tsremap_version < TSREMAP_VERSION) {
+ snprintf(errbuf, errbuf_size - 1, "[tsremap_init] - Incorrect API version %ld.%ld",
+ api_info->tsremap_version >> 16, (api_info->tsremap_version & 0xffff));
+ return -3;
+ }
+
+ setup_memory_allocation();
+
+ INKDebug("regex_remap", "plugin is succesfully initialized");
+ return 0; /* success */
+}
+
+
+///////////////////////////////////////////////////////////////////////////////
+// We don't have any specific "instances" here, at least not yet.
+//
+int
+tsremap_new_instance(int argc, char *argv[], ihandle *ih, char *errbuf, int errbuf_size)
+{
+ const char* error;
+ int erroffset;
+ RemapInstance* ri = new RemapInstance();
+
+ std::ifstream f;
+ int lineno = 0;
+ int count = 0;
+
+ *ih = (ihandle)ri;
+ if (ri == NULL) {
+ INKError("Unable to create remap instance");
+ return -5;
+ }
+
+ // Really simple (e.g. basic) config parser
+ for (int i=2; i < argc; ++i) {
+ if (strncmp(argv[i], "profile", 7) == 0) {
+ ri->profile = true;
+ } else if (strncmp(argv[i], "no-profile", 10) == 0) {
+ ri->profile = false;
+ } else if (strncmp(argv[i], "full-url", 8) == 0) {
+ ri->full_url = true;
+ } else if (strncmp(argv[i], "no-full-url", 11) == 0) {
+ ri->full_url = false;
+ } else if (strncmp(argv[i], "method", 6) == 0) {
+ ri->method = true;
+ } else if (strncmp(argv[i], "no-method", 9) == 0) {
+ ri->method = true;
+ } else if (strncmp(argv[i], "query-string", 12) == 0) {
+ ri->query_string = true;
+ } else if (strncmp(argv[i], "no-query-string", 15) == 0) {
+ ri->query_string = false;
+ } else if (strncmp(argv[i], "matrix-parameters", 15) == 0) {
+ ri->matrix_params = true;
+ } else if (strncmp(argv[i], "no-matrix-parameters", 18) == 0) {
+ ri->matrix_params = false;
+ } else {
+ if (0 != access(argv[2], R_OK)) {
+ ri->filename = DEFAULT_PATH;
+ ri->filename += argv[2];
+ } else {
+ ri->filename = argv[2];
+ }
+
+ f.open((ri->filename).c_str(), std::ios::in);
+ if (!f.is_open()) { // Try with the default path instead
+ INKError("unable to open %s", (ri->filename).c_str());
+ return -4;
+ }
+ INKDebug("regex_remap", "loading regular expression maps from %s", (ri->filename).c_str());
+
+ while (!f.eof()) {
+ std::string line, regex, subst, options;
+ std::string::size_type pos1, pos2;
+
+ getline(f, line);
+ ++lineno;
+ if (line.empty())
+ continue;
+ pos1 = line.find_first_not_of(" \t\n");
+ if (line[pos1] == '#')
+ continue; // Skip comment lines
+
+ if (pos1 != std::string::npos) {
+ pos2 = line.find_first_of(" \t\n", pos1);
+ if (pos2 != std::string::npos) {
+ regex = line.substr(pos1, pos2-pos1);
+ pos1 = line.find_first_not_of(" \t\n#", pos2);
+ if (pos1 != std::string::npos) {
+ pos2 = line.find_first_of(" \t\n", pos1);
+ if (pos2 == std::string::npos)
+ pos2 = line.length();
+ subst = line.substr(pos1, pos2-pos1);
+ pos1 = line.find_first_not_of(" \t\n#", pos2);
+ if (pos1 != std::string::npos) {
+ pos2 = line.find_first_of("\n#", pos1);
+ if (pos2 == std::string::npos)
+ pos2 = line.length();
+ options = line.substr(pos1, pos2-pos1);
+ }
+ }
+ }
+ }
+
+ if (regex.empty()) {
+ // No regex found on this line
+ INKError("no regexp found in %s: line %d", (ri->filename).c_str(), lineno);
+ continue;
+ }
+ if (subst.empty() && options.empty()) {
+ // No substitution found on this line (and no options)
+ INKError("no substitution string found in %s: line %d", (ri->filename).c_str(), lineno);
+ continue;
+ }
+
+ // Got a regex and substitution string
+ RemapRegex* cur = new RemapRegex(regex, subst, options);
+
+ if (cur == NULL) {
+ INKError("can't create a new regex remap rule");
+ continue;
+ }
+
+ if (cur->compile(&error, &erroffset) < 0) {
+ INKError("PCRE failed in %s (line %d) at offset %d: %s\n", (ri->filename).c_str(), lineno, erroffset, error);
+ delete(cur);
+ } else {
+ INKDebug("regex_remap", "added regex=%s with substitution=%s and options `%s'",
+ regex.c_str(), subst.c_str(), options.c_str());
+ cur->set_order(++count);
+ if (ri->first == NULL)
+ ri->first = cur;
+ else
+ ri->last->set_next(cur);
+ ri->last = cur;
+ }
+ }
+ }
+ }
+
+ // Make sure we got something...
+ if (ri->first == NULL) {
+ INKError("Got no regular expressions from the maps");
+ return -1;
+ }
+
+ return 0;
+}
+
+
+void
+tsremap_delete_instance(ihandle ih)
+{
+ RemapInstance* ri = static_cast<RemapInstance*>(ih);
+ RemapRegex* re = ri->first;
+ RemapRegex* tmp;
+ int ix = 1;
+ char now[64];
+ time_t tim = time(NULL);
+
+ if (ri->profile) {
+ if (ctime_r(&tim, now))
+ now[strlen(now) - 1] = '\0';
+ else {
+ memcpy(now, "unknown time", 12);
+ *(now + 12) = '\0';
+ }
+
+ fprintf(stderr, "[%s]: Profiling information for regex_remap file `%s':\n", now, (ri->filename).c_str());
+ fprintf(stderr, "[%s]:\tTotal hits (matches): %d\n", now, ri->hits);
+ fprintf(stderr, "[%s]:\tTotal missed (no regex matches): %d\n", now, ri->misses);
+ }
+
+ if (ri->hits > 0) { // Avoid divide by zeros...
+ while (re) {
+ if (ri->profile)
+ re->print(ix, ri->hits, now);
+ tmp = re->next();
+ delete re;
+ re = tmp;
+ ++ix;
+ }
+ }
+
+ delete ri;
+}
+
+
+///////////////////////////////////////////////////////////////////////////////
+// This is the main "entry" point for the plugin, called for every request.
+//
+int
+tsremap_remap(ihandle ih, rhandle rh, TSRemapRequestInfo *rri)
+{
+ if (NULL == ih) {
+ INKDebug("regex_remap", "Falling back to default URL on regex remap without rules");
+ return 0;
+ } else {
+ RemapInstance* ri = (RemapInstance*)ih;
+ int ovector[OVECCOUNT];
+ int lengths[OVECCOUNT/2 + 1];
+ int dest_len;
+ int retval = 1;
+ RemapRegex* re = ri->first;
+ char match_buf[rri->orig_url_size + 16 + 5]; // Worst case scenario and padded for /,? and ;
+ int match_len = 0;
+
+ if (ri->method) { // Prepend the URI path or URL with the HTTP method
+ INKMBuffer mBuf;
+ INKMLoc reqHttpHdrLoc;
+ const char *method;
+
+ // Note that Method can not be longer than 16 bytes, or we'll simply truncate it
+ if (INKHttpTxnClientReqGet(static_cast<INKHttpTxn>(rh), &mBuf, &reqHttpHdrLoc)) {
+ method = INKHttpHdrMethodGet(mBuf, reqHttpHdrLoc, &match_len);
+ if (method && (match_len > 0)) {
+ if (match_len > 16)
+ match_len = 16;
+ memcpy(match_buf, method, match_len);
+ INKHandleStringRelease(mBuf, reqHttpHdrLoc, method);
+ }
+ }
+ }
+
+ if (ri->full_url) { // This ignores all the other match string options
+ memcpy(match_buf + match_len, rri->orig_url, rri->orig_url_size);
+ match_len += rri->orig_url_size;
+ } else {
+ *(match_buf + match_len) = '/';
+ memcpy(match_buf + match_len + 1, rri->request_path, rri->request_path_size);
+ match_len += rri->request_path_size + 1;
+
+ // Todo: This is crazy, but for now, we have to parse the URL to extract (possibly) the
+ // matrix parameters from the original URL, and append them.
+ // XXX: This can now be fixed in Apache.
+ if (ri->matrix_params) {
+ INKMBuffer bufp = (INKMBuffer)INK_ERROR_PTR;
+ INKMLoc url_loc = (INKMLoc)INK_ERROR_PTR;
+ const char* start = rri->orig_url;
+ const char* params;
+ int params_len;
+
+ bufp = INKMBufferCreate();
+ if (bufp == INK_ERROR_PTR) {
+ INKHttpTxnSetHttpRetStatus((INKHttpTxn)rh, (INKHttpStatus)500);
+ INKError("can't create MBuffer");
+ goto param_error;
+ }
+
+ url_loc = INKUrlCreate(bufp);
+ if (url_loc == INK_ERROR_PTR) {
+ INKHttpTxnSetHttpRetStatus((INKHttpTxn)rh, (INKHttpStatus)500);
+ INKError("can't create URL buffer");
+ goto param_error;
+ }
+
+ if (INKUrlParse(bufp, url_loc, &start, start + rri->orig_url_size) == INK_PARSE_ERROR) {
+ INKHttpTxnSetHttpRetStatus((INKHttpTxn)rh, (INKHttpStatus)500);
+ INKError("can't parse original URL string");
+ goto param_error;
+ }
+
+ params = INKUrlHttpParamsGet(bufp, url_loc, ¶ms_len);
+ if (params && (params_len > 0)) {
+ *(match_buf + match_len) = ';';
+ memcpy(match_buf + match_len + 1 , params, params_len);
+ match_len += (params_len + 1);
+ INKHandleStringRelease(bufp, url_loc, params);
+ }
+
+ param_error:
+ if (url_loc != INK_ERROR_PTR) {
+ INKUrlDestroy(bufp, url_loc);
+ INKHandleMLocRelease(bufp, INK_NULL_MLOC, url_loc);
+ }
+ if (bufp != INK_ERROR_PTR)
+ INKMBufferDestroy(bufp);
+ }
+
+ if (ri->query_string && (rri->request_query_size > 0)) {
+ *(match_buf + match_len) = '?';
+ memcpy(match_buf + match_len + 1 , rri->request_query, rri->request_query_size);
+ match_len += (rri->request_query_size + 1);
+ }
+ }
+ match_buf[match_len] = '\0'; // NULL terminate the match string
+
+ INKDebug("regex_remap", "original match string is %s (length %d out of %d)", match_buf,
+ match_len, rri->orig_url_size + 16 + 5);
+ INKReleaseAssert(match_len < (rri->orig_url_size + 16 + 5)); // Just in case ...
+
+ // Apply the regular expressions, in order. First one wins.
+ while (re) {
+ // Since we check substitutions on parse time, we don't need to reset ovector
+ if (re->is_simple() || (re->match(match_buf, match_len, ovector) != (-1))) {
+ int new_len = re->get_lengths(ovector, lengths, rri);
+
+ // Set timeouts
+ if (re->active_timeout_option() > (-1)) {
+ INKDebug("regex_remap", "Setting active timeout to %d", re->active_timeout_option());
+ INKHttpTxnActiveTimeoutSet((INKHttpTxn)rh, re->active_timeout_option());
+ }
+ if (re->no_activity_timeout_option() > (-1)) {
+ INKDebug("regex_remap", "Setting no activity timeout to %d", re->no_activity_timeout_option());
+ INKHttpTxnNoActivityTimeoutSet((INKHttpTxn)rh, re->no_activity_timeout_option());
+ }
+ if (re->connect_timeout_option() > (-1)) {
+ INKDebug("regex_remap", "Setting connect timeout to %d", re->connect_timeout_option());
+ INKHttpTxnConnectTimeoutSet((INKHttpTxn)rh, re->connect_timeout_option());
+ }
+ if (re->dns_timeout_option() > (-1)) {
+ INKDebug("regex_remap", "Setting DNS timeout to %d", re->dns_timeout_option());
+ INKHttpTxnDNSTimeoutSet((INKHttpTxn)rh, re->dns_timeout_option());
+ }
+
+ // Update profiling if requested
+ if (ri->profile) {
+ re->increment();
+ ink_atomic_increment(&(ri->hits), 1);
+ }
+
+ if (new_len > 0) {
+ char dest[new_len+8];
+
+ dest_len = re->substitute(dest, match_buf, ovector, lengths, rri);
+
+ INKDebug("regex_remap", "New URL is estimated to be %d bytes long, or less\n", new_len);
+ INKDebug("regex_remap", "New URL is %s (length %d)", dest, dest_len);
+ INKDebug("regex_remap", " matched rule %d [%s]", re->order(), re->regex());
+
+ // Check for a quick response, if the status option is set
+ if (re->status_option() > 0) {
+ INKHttpTxnSetHttpRetStatus((INKHttpTxn)rh, re->status_option());
+ if ((re->status_option() == (INKHttpStatus)301) || (re->status_option() == (INKHttpStatus)302)) {
+ if (dest_len > TSREMAP_RRI_MAX_REDIRECT_URL) {
+ INKError("Redirect in target URL too long");
+ INKHttpTxnSetHttpRetStatus((INKHttpTxn)rh, INK_HTTP_STATUS_REQUEST_URI_TOO_LONG);
+ } else {
+ memcpy(rri->redirect_url, dest, dest_len);
+ rri->redirect_url_size = dest_len;
+ }
+ }
+ return 1;
+ }
+
+ if (dest_len > 0) {
+ INKMBuffer bufp = (INKMBuffer)INK_ERROR_PTR;
+ INKMLoc url_loc = (INKMLoc)INK_ERROR_PTR;
+ const char *temp;
+ const char *start = dest;
+ int len, port;
+
+ bufp = INKMBufferCreate();
+ if (bufp == INK_ERROR_PTR) {
+ INKHttpTxnSetHttpRetStatus((INKHttpTxn)rh, (INKHttpStatus)500);
+ INKError("can't create MBuffer");
+ goto error;
+ }
+
+ url_loc = INKUrlCreate(bufp);
+ if (url_loc == INK_ERROR_PTR) {
+ INKHttpTxnSetHttpRetStatus((INKHttpTxn)rh, (INKHttpStatus)500);
+ INKError("can't create URL buffer");
+ goto error;
+ }
+
+ if (INKUrlParse(bufp, url_loc, &start, start + dest_len) == INK_PARSE_ERROR) {
+ INKHttpTxnSetHttpRetStatus((INKHttpTxn)rh, (INKHttpStatus)500);
+ INKError("can't parse substituted URL string");
+ goto error;
+ }
+
+ // Update the Host (if necessary)
+ temp = INKUrlHostGet(bufp, url_loc, &len);
+ if (len > TSREMAP_RRI_MAX_HOST_SIZE) {
+ INKError("Host in target URL too long");
+ INKHttpTxnSetHttpRetStatus((INKHttpTxn)rh, INK_HTTP_STATUS_REQUEST_URI_TOO_LONG);
+ INKHandleStringRelease(bufp, url_loc, temp);
+ goto error;
+ } else {
+ if (temp && (len > 0) && ((len != rri->remap_to_host_size) || strncmp(temp, rri->remap_to_host, len))) {
+ INKDebug("regex_remap", "new host string: %s (len = %d)", temp, len);
+ memcpy(rri->new_host, temp, len);
+ rri->new_host_size = len;
+ INKHandleStringRelease(bufp, url_loc, temp);
+ }
+ }
+
+ // Update the Path (if necessary)
+ // ToDo: Should we maybe compare with rri->remap_to_path + rri->request_path ?
+ temp = INKUrlPathGet(bufp, url_loc, &len);
+ if (len > TSREMAP_RRI_MAX_PATH_SIZE) {
+ INKError("Path in target URL too long");
+ INKHttpTxnSetHttpRetStatus((INKHttpTxn)rh, INK_HTTP_STATUS_REQUEST_URI_TOO_LONG);
+ INKHandleStringRelease(bufp, url_loc, temp);
+ goto error;
+ } else {
+ if (!temp || (len <= 0)) {
+ if (rri->request_path_size > 0) {
+ rri->new_path_size = -1;
+ INKDebug("regex_remap", "new path is empty");
+ }
+ } else {
+ if (*temp == '/') {
+ ++temp;
+ --len;
+ }
+ // ToDo: This is an ugly hack, since we must "modify" the path for the parameter hack
+ // to function :/.
+ if ((len != rri->request_path_size) || strncmp(temp, rri->request_path, len)) {
+ INKDebug("regex_remap", "new path string: %s (len = %d)", temp, len);
+ }
+ memcpy(rri->new_path, temp, len);
+ rri->new_path_size = len;
+ INKHandleStringRelease(bufp, url_loc, temp);
+ }
+ }
+
+ // Update the matrix parameters (if necessary). TODO: This is very hacky right now,
+ // instead of appending to the new_path, we really ought to set the matrix parameters
+ // using a proper API (which is not supported at this point). This only works if
+ // there is a path as well.
+ if (rri->new_path_size != (-1)) {
+ temp = INKUrlHttpParamsGet(bufp, url_loc, &len);
+ if (len >= (TSREMAP_RRI_MAX_PATH_SIZE - rri->new_path_size - 3)) {
+ INKError("Matrix parameters in target URL too long");
+ INKHttpTxnSetHttpRetStatus((INKHttpTxn)rh, INK_HTTP_STATUS_REQUEST_URI_TOO_LONG);
+ INKHandleStringRelease(bufp, url_loc, temp);
+ goto error;
+ } else {
+ if (temp && (len > 0)) {
+ *(rri->new_path + rri->new_path_size) = ';';
+ memcpy(rri->new_path + rri->new_path_size + 1, temp, len);
+ rri->new_path_size += (len + 1);
+ INKHandleStringRelease(bufp, url_loc, temp);
+ INKDebug("regex_remap", "appending matrix parameters: %.*s", len, temp);
+ INKDebug("regex_remap", "new path string: %.*s", rri->new_path_size, rri->new_path);
+ }
+ }
+ }
+
+ // Update the Query string (if necessary)
+ temp = INKUrlHttpQueryGet(bufp, url_loc, &len);
+ if (len > TSREMAP_RRI_MAX_PATH_SIZE) {
+ INKError("Query in target URL too long");
+ INKHttpTxnSetHttpRetStatus((INKHttpTxn)rh, INK_HTTP_STATUS_REQUEST_URI_TOO_LONG);
+ INKHandleStringRelease(bufp, url_loc, temp);
+ goto error;
+ } else {
+ if (!temp || (len <= 0)) {
+ if (rri->request_query_size > 0) {
+ *(rri->new_query) = 0;
+ rri->new_query_size = -1; // Delete it
+ INKDebug("regex_remap", "new query is empty");
+ }
+ } else {
+ if (*temp == '?') {
+ ++temp;
+ --len;
+ }
+ if ((len != rri->request_query_size) || strncmp(temp, rri->request_query, len)) {
+ INKDebug("regex_remap", "new query string: %s (len = %d)", temp, len);
+ memcpy(rri->new_query, temp, len);
+ rri->new_query_size = len;
+ }
+ INKHandleStringRelease(bufp, url_loc, temp);
+ }
+ }
+
+ // Update the port number (if necessary)
+ port = INKUrlPortGet(bufp, url_loc);
+ if (port != rri->remap_to_port) {
+ INKDebug("regex_remap", "new port: %d", port);
+ rri->new_port = port;
+ }
+
+ // Update the scheme (HTTP or HTTPS)
+ temp = INKUrlSchemeGet(bufp, url_loc, &len);
+ if (temp && (len > 0) && (len != rri->to_scheme_len)) {
+ if (!strncmp(temp, INK_URL_SCHEME_HTTPS, INK_URL_LEN_HTTPS)) {
+ rri->require_ssl = 1;
+ INKDebug("regex_remap", "changing scheme to HTTPS");
+ } else {
+ rri->require_ssl = 0;
+ INKDebug("regex_remap", "changing scheme to HTTP");
+ }
+ INKHandleStringRelease(bufp, url_loc, temp);
+ }
+
+ // Cleanup
+ error:
+ if (url_loc != INK_ERROR_PTR) {
+ INKUrlDestroy(bufp, url_loc);
+ INKHandleMLocRelease(bufp, INK_NULL_MLOC, url_loc);
+ }
+ if (bufp != INK_ERROR_PTR)
+ INKMBufferDestroy(bufp);
+ }
+ break; // We're done
+ }
+ }
+ re = re->next();
+ if (re == NULL) {
+ retval = 0; // No match
+ if (ri->profile)
+ ink_atomic_increment(&(ri->misses), 1);
+ }
+ }
+ return retval;
+ }
+
+ // This shouldn't happen, but just in case
+ return 0;
+}
+
+
+
+/*
+ local variables:
+ mode: C++
+ indent-tabs-mode: nil
+ c-basic-offset: 2
+ c-comment-only-line-offset: 0
+ c-file-offsets: ((statement-block-intro . +)
+ (label . 0)
+ (statement-cont . +)
+ (innamespace . 0))
+ end:
+
+ Indent with: /usr/bin/indent -ncs -nut -npcs -l 120 logstats.cc
+*/
Re: [11/11] git commit: Added regex remap plugin from Yahoo!
Posted by Igor Galić <i....@brainsware.org>.
Ignore me.
i
--
Igor Galić
Tel: +43 (0) 664 886 22 883
Mail: i.galic@brainsware.org
URL: http://brainsware.org/
GPG: 6880 4155 74BD FD7C B515 2EA5 4B1D 9E08 A097 C9AE
Re: [11/11] git commit: Added regex remap plugin from Yahoo!
Posted by Igor Galić <i....@brainsware.org>.
----- Original Message -----
> Added regex remap plugin from Yahoo!
>
>
> git-svn-id:
> https://svn.apache.org/repos/asf/trafficserver/plugins/trunk@1198996
> 13f79535-47bb-0310-9956-ffa450edef68
I'm confused. Why is this here instead of trafficserver-plugins ?
i
--
Igor Galić
Tel: +43 (0) 664 886 22 883
Mail: i.galic@brainsware.org
URL: http://brainsware.org/
GPG: 6880 4155 74BD FD7C B515 2EA5 4B1D 9E08 A097 C9AE