From 01e2797dd474e3e37cc1c9cb8476642324958e13 Mon Sep 17 00:00:00 2001 From: Pranav Tekchand Date: Wed, 21 Mar 2012 15:41:41 +0530 Subject: [PATCH 1/2] * The Modify action now supports replacing a header based on a regex in the value field. Any string in the value for a Modify action which matches the form /foo/bar/ triggers the code. foo can be a regular expression and bar is the string which replaces the match. Example: Action: Modify Name: Host Value: /^w{0,3}\.?domain.com$/192.168.254.65/ This will change www.domain.com and domain.com to 192.168.254.65 if found in the Host header. Warning: This wouldn't work for matching '/' (such as in http://) --- releasenotes.txt | 14 ++++++++++++++ src/components/modifyheaders-service.js | 16 ++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/releasenotes.txt b/releasenotes.txt index bb2a7bd..4ae1833 100644 --- a/releasenotes.txt +++ b/releasenotes.txt @@ -1,5 +1,19 @@ -- Release Notes -- +Release 0.7.1.2, 21 March 2012 + +* The Modify action now supports replacing a header based on a regex in the value field. + Any string in the value for a Modify action which matches the form /foo/bar/ triggers the code. + foo can be a regular expression and bar is the string which replaces the match. + Example: + Action: Modify + Name: Host + Value: /^w{0,3}\.?domain.com$/192.168.254.65/ + This will change www.domain.com and domain.com to 192.168.254.65 if found in the Host header. + Warning: This wouldn't work for matching '/' (such as in http://) + +-- + Release 0.7.1.1, 30 November 2011 * Fixed bug 24593: Addon toolbar is displayed every time a new window is opened diff --git a/src/components/modifyheaders-service.js b/src/components/modifyheaders-service.js index e82f503..8cebd70 100644 --- a/src/components/modifyheaders-service.js +++ b/src/components/modifyheaders-service.js @@ -273,8 +273,20 @@ if (!ModifyHeaders.Proxy) { // This is the default for action = Modify var headerValue = headers[i].value; var headerAppend = false; - - if (headers[i].action == "Add") { + + // Could add a new action "Regex" instead. + // For the following code to run, the value supplied by the user + // should be of the form: /foo/bar/ + // where 'foo' can be a regular expression + // and 'bar' is the string to replace the match with. + if(headers[i].action == "Modify") { + var isRegex = headerValue.match(/^\/(.*?)\/(.*?)\/$/); + if(isRegex.length>0) { // OR == 2 to be stricter + var currentHeaderValue = subject.getRequestHeader(headerName); + // 0 = /foo/bar/; 1 = foo; 2 = bar; + headerValue = currentHeaderValue.replace(new RegExp(isRegex[1]), isRegex[2]); + } + } else if (headers[i].action == "Add") { headerAppend = true; } else if (headers[i].action == "Filter") { headerValue = ""; From 7363f019ea6e1b2d4f6ef709a70dae1a45917861 Mon Sep 17 00:00:00 2001 From: Pranav Tekchand Date: Wed, 28 Mar 2012 00:13:33 +0530 Subject: [PATCH 2/2] The value field should be of the form {RegEx}{replacement} Changed from forward slash since users may want to match them in URLs such as in the Referer header. --- releasenotes.txt | 4 ++-- src/components/modifyheaders-service.js | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/releasenotes.txt b/releasenotes.txt index 4ae1833..922bac6 100644 --- a/releasenotes.txt +++ b/releasenotes.txt @@ -8,9 +8,9 @@ Release 0.7.1.2, 21 March 2012 Example: Action: Modify Name: Host - Value: /^w{0,3}\.?domain.com$/192.168.254.65/ + Value: {^w{0,3}\.?domain.com$}{192.168.254.65} This will change www.domain.com and domain.com to 192.168.254.65 if found in the Host header. - Warning: This wouldn't work for matching '/' (such as in http://) + (Updated 28th) -- diff --git a/src/components/modifyheaders-service.js b/src/components/modifyheaders-service.js index 8cebd70..6c87fb8 100644 --- a/src/components/modifyheaders-service.js +++ b/src/components/modifyheaders-service.js @@ -276,14 +276,14 @@ if (!ModifyHeaders.Proxy) { // Could add a new action "Regex" instead. // For the following code to run, the value supplied by the user - // should be of the form: /foo/bar/ + // should be of the form: {foo}{bar} // where 'foo' can be a regular expression // and 'bar' is the string to replace the match with. if(headers[i].action == "Modify") { - var isRegex = headerValue.match(/^\/(.*?)\/(.*?)\/$/); + var isRegex = headerValue.match(/^\{(.*?)\}\{(.*?)\}$/); if(isRegex.length>0) { // OR == 2 to be stricter var currentHeaderValue = subject.getRequestHeader(headerName); - // 0 = /foo/bar/; 1 = foo; 2 = bar; + // 0 = {foo}{bar}; 1 = foo; 2 = bar; headerValue = currentHeaderValue.replace(new RegExp(isRegex[1]), isRegex[2]); } } else if (headers[i].action == "Add") {