From 70f94c7171cac9659feccfb0f974f72d4bc33a80 Mon Sep 17 00:00:00 2001 From: arjun23496 Date: Fri, 26 Aug 2016 21:23:37 +0530 Subject: [PATCH 1/8] Added count argument for replace for contents in body --- netlib/http/message.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/netlib/http/message.py b/netlib/http/message.py index ce92bab15..b84f45c5f 100644 --- a/netlib/http/message.py +++ b/netlib/http/message.py @@ -260,7 +260,7 @@ class Message(basetypes.Serializable): if "content-encoding" not in self.headers: raise ValueError("Invalid content encoding {}".format(repr(e))) - def replace(self, pattern, repl, flags=0): + def replace(self, pattern, repl, flags=0, count=0): """ Replaces a regular expression pattern with repl in both the headers and the body of the message. Encoded body will be decoded @@ -276,7 +276,7 @@ class Message(basetypes.Serializable): replacements = 0 if self.content: self.content, replacements = re.subn( - pattern, repl, self.content, flags=flags + pattern, repl, self.content, flags=flags, count=count ) replacements += self.headers.replace(pattern, repl, flags) return replacements From 99744cc780a5100ac028bbbf43e8a42e1167047e Mon Sep 17 00:00:00 2001 From: arjun23496 Date: Mon, 29 Aug 2016 16:50:14 +0530 Subject: [PATCH 2/8] Added replace for request and headers - Fixed missing replace option --- netlib/http/headers.py | 5 +++-- netlib/http/request.py | 6 +++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/netlib/http/headers.py b/netlib/http/headers.py index 36e5060c7..60a36515b 100644 --- a/netlib/http/headers.py +++ b/netlib/http/headers.py @@ -158,7 +158,7 @@ class Headers(multidict.MultiDict): else: return super(Headers, self).items() - def replace(self, pattern, repl, flags=0): + def replace(self, pattern, repl, flags=0, count=0): """ Replaces a regular expression pattern with repl in each "name: value" header line. @@ -175,7 +175,7 @@ class Headers(multidict.MultiDict): fields = [] for name, value in self.fields: - line, n = pattern.subn(repl, name + b": " + value) + line, n = pattern.subn(repl, name + b": " + value, count=count) try: name, value = line.split(b": ", 1) except ValueError: @@ -183,6 +183,7 @@ class Headers(multidict.MultiDict): # There's not much we can do about this, so we just keep the header as-is. pass else: + count -= n replacements += n fields.append((name, value)) self.fields = tuple(fields) diff --git a/netlib/http/request.py b/netlib/http/request.py index 666a5869d..e0aaa8a9a 100644 --- a/netlib/http/request.py +++ b/netlib/http/request.py @@ -80,7 +80,7 @@ class Request(message.Message): self.method, hostport, path ) - def replace(self, pattern, repl, flags=0): + def replace(self, pattern, repl, flags=0, count=0): """ Replaces a regular expression pattern with repl in the headers, the request path and the body of the request. Encoded content will be @@ -94,9 +94,9 @@ class Request(message.Message): if isinstance(repl, six.text_type): repl = strutils.escaped_str_to_bytes(repl) - c = super(Request, self).replace(pattern, repl, flags) + c = super(Request, self).replace(pattern, repl, flags, count) self.path, pc = re.subn( - pattern, repl, self.data.path, flags=flags + pattern, repl, self.data.path, flags=flags, count=count ) c += pc return c From b3f05966526f46b3239bfa57fe32ed62252221b9 Mon Sep 17 00:00:00 2001 From: arjun23496 Date: Mon, 29 Aug 2016 16:57:20 +0530 Subject: [PATCH 3/8] Minor bug fix - messages.py replace --- netlib/http/message.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/netlib/http/message.py b/netlib/http/message.py index b84f45c5f..0b64d4a60 100644 --- a/netlib/http/message.py +++ b/netlib/http/message.py @@ -278,7 +278,7 @@ class Message(basetypes.Serializable): self.content, replacements = re.subn( pattern, repl, self.content, flags=flags, count=count ) - replacements += self.headers.replace(pattern, repl, flags) + replacements += self.headers.replace(pattern, repl, flags=flags, count=count) return replacements # Legacy From d2cfa5ca41c0d8b55000cc4c9fca924c28dfc14a Mon Sep 17 00:00:00 2001 From: arjun23496 Date: Wed, 31 Aug 2016 10:23:33 +0530 Subject: [PATCH 4/8] Added tests for replace in request,message,header --- netlib/http/headers.py | 2 ++ test/netlib/http/test_headers.py | 4 ++++ test/netlib/http/test_message.py | 9 +++++++++ test/netlib/http/test_request.py | 11 ++++++++++- 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/netlib/http/headers.py b/netlib/http/headers.py index 60a36515b..f4cf8b7b7 100644 --- a/netlib/http/headers.py +++ b/netlib/http/headers.py @@ -185,6 +185,8 @@ class Headers(multidict.MultiDict): else: count -= n replacements += n + if count == 0: + break; fields.append((name, value)) self.fields = tuple(fields) return replacements diff --git a/test/netlib/http/test_headers.py b/test/netlib/http/test_headers.py index 51537310f..b1085e5a7 100644 --- a/test/netlib/http/test_headers.py +++ b/test/netlib/http/test_headers.py @@ -75,6 +75,10 @@ class TestHeaders(object): assert replacements == 0 assert headers["Host"] == "example.com" + def test_replace_with_count(self): + headers = Headers(Host="foobarfoo.com", Accept="foo/bar") + replacements = headers.replace("foo","bar",count=1) + assert replacements == 1 def test_parse_content_type(): p = parse_content_type diff --git a/test/netlib/http/test_message.py b/test/netlib/http/test_message.py index 12e4706ce..df66ef6e2 100644 --- a/test/netlib/http/test_message.py +++ b/test/netlib/http/test_message.py @@ -99,6 +99,15 @@ class TestMessage(object): def test_http_version(self): _test_decoded_attr(tresp(), "http_version") + def test_replace(self): + r = tresp() + r.content = b"foofootoo" + r.replace(b"foo","gg") + assert r.content == b"ggggtoo" + + r.content = b"foofootoo" + r.replace(b"foo","gg",count=1) + assert r.content == b"ggfootoo" class TestMessageContentEncoding(object): def test_simple(self): diff --git a/test/netlib/http/test_request.py b/test/netlib/http/test_request.py index f3cd8b71a..1fc5dc24c 100644 --- a/test/netlib/http/test_request.py +++ b/test/netlib/http/test_request.py @@ -26,6 +26,16 @@ class TestRequestCore(object): request.host = None assert repr(request) == "Request(GET /path)" + def replace(self): + r = treq() + r.path = b"foobarfoo" + r.replace(b"foo","bar") + assert r.path == b"barbarbar" + + r.path = b"foobarfoo" + r.replace(b"foo","bar",count=1) + assert r.path == b"barbarfoo" + def test_first_line_format(self): _test_passthrough_attr(treq(), "first_line_format") @@ -87,7 +97,6 @@ class TestRequestCore(object): request.host = "example.org" assert request.headers["Host"] == "example.org" - class TestRequestUtils(object): """ Tests for additional convenience methods. From dd2e0d1df3eeb934c291dd6197f722e419dd3e43 Mon Sep 17 00:00:00 2001 From: arjun23496 Date: Wed, 31 Aug 2016 16:11:59 +0530 Subject: [PATCH 5/8] minor bug fix header replace --- netlib/http/headers.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/netlib/http/headers.py b/netlib/http/headers.py index f4cf8b7b7..5f724f6df 100644 --- a/netlib/http/headers.py +++ b/netlib/http/headers.py @@ -172,6 +172,7 @@ class Headers(multidict.MultiDict): repl = strutils.escaped_str_to_bytes(repl) pattern = re.compile(pattern, flags) replacements = 0 + flag_count = count > 0 fields = [] for name, value in self.fields: @@ -183,10 +184,13 @@ class Headers(multidict.MultiDict): # There's not much we can do about this, so we just keep the header as-is. pass else: - count -= n replacements += n - if count == 0: - break; + + if flag_count: + count -= n + if count == 0: + break; + fields.append((name, value)) self.fields = tuple(fields) return replacements From e5766550f1576a02facb95b452e4a52271780b63 Mon Sep 17 00:00:00 2001 From: arjun23496 Date: Wed, 31 Aug 2016 16:22:20 +0530 Subject: [PATCH 6/8] Lint fix - netlib headers --- netlib/http/headers.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/netlib/http/headers.py b/netlib/http/headers.py index 5f724f6df..6afe27c7f 100644 --- a/netlib/http/headers.py +++ b/netlib/http/headers.py @@ -173,8 +173,8 @@ class Headers(multidict.MultiDict): pattern = re.compile(pattern, flags) replacements = 0 flag_count = count > 0 - fields = [] + for name, value in self.fields: line, n = pattern.subn(repl, name + b": " + value, count=count) try: @@ -185,12 +185,10 @@ class Headers(multidict.MultiDict): pass else: replacements += n - if flag_count: count -= n if count == 0: - break; - + break fields.append((name, value)) self.fields = tuple(fields) return replacements From 2476809ff85e6f1b66c931a2eedf552169383573 Mon Sep 17 00:00:00 2001 From: arjun23496 Date: Wed, 31 Aug 2016 16:27:22 +0530 Subject: [PATCH 7/8] Lint fix - replace tests --- test/netlib/http/test_headers.py | 3 ++- test/netlib/http/test_message.py | 7 ++++--- test/netlib/http/test_request.py | 5 +++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/test/netlib/http/test_headers.py b/test/netlib/http/test_headers.py index b1085e5a7..ad2bc5487 100644 --- a/test/netlib/http/test_headers.py +++ b/test/netlib/http/test_headers.py @@ -77,9 +77,10 @@ class TestHeaders(object): def test_replace_with_count(self): headers = Headers(Host="foobarfoo.com", Accept="foo/bar") - replacements = headers.replace("foo","bar",count=1) + replacements = headers.replace("foo", "bar", count=1) assert replacements == 1 + def test_parse_content_type(): p = parse_content_type assert p("text/html") == ("text", "html", {}) diff --git a/test/netlib/http/test_message.py b/test/netlib/http/test_message.py index df66ef6e2..95fbb3d98 100644 --- a/test/netlib/http/test_message.py +++ b/test/netlib/http/test_message.py @@ -102,13 +102,14 @@ class TestMessage(object): def test_replace(self): r = tresp() r.content = b"foofootoo" - r.replace(b"foo","gg") - assert r.content == b"ggggtoo" + r.replace(b"foo", "gg") + assert r.content == b"ggggtoo" r.content = b"foofootoo" - r.replace(b"foo","gg",count=1) + r.replace(b"foo", "gg", count=1) assert r.content == b"ggfootoo" + class TestMessageContentEncoding(object): def test_simple(self): r = tresp() diff --git a/test/netlib/http/test_request.py b/test/netlib/http/test_request.py index 1fc5dc24c..9baabaa6d 100644 --- a/test/netlib/http/test_request.py +++ b/test/netlib/http/test_request.py @@ -29,11 +29,11 @@ class TestRequestCore(object): def replace(self): r = treq() r.path = b"foobarfoo" - r.replace(b"foo","bar") + r.replace(b"foo", "bar") assert r.path == b"barbarbar" r.path = b"foobarfoo" - r.replace(b"foo","bar",count=1) + r.replace(b"foo", "bar", count=1) assert r.path == b"barbarfoo" def test_first_line_format(self): @@ -97,6 +97,7 @@ class TestRequestCore(object): request.host = "example.org" assert request.headers["Host"] == "example.org" + class TestRequestUtils(object): """ Tests for additional convenience methods. From a8deed1f4ef5992b1c9c74ac69491bdb5f0e8490 Mon Sep 17 00:00:00 2001 From: arjun23496 Date: Wed, 31 Aug 2016 16:39:04 +0530 Subject: [PATCH 8/8] Lint fix - minor --- netlib/http/headers.py | 3 +-- test/netlib/http/test_message.py | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/netlib/http/headers.py b/netlib/http/headers.py index 6afe27c7f..131e8ce5f 100644 --- a/netlib/http/headers.py +++ b/netlib/http/headers.py @@ -174,7 +174,6 @@ class Headers(multidict.MultiDict): replacements = 0 flag_count = count > 0 fields = [] - for name, value in self.fields: line, n = pattern.subn(repl, name + b": " + value, count=count) try: @@ -188,7 +187,7 @@ class Headers(multidict.MultiDict): if flag_count: count -= n if count == 0: - break + break fields.append((name, value)) self.fields = tuple(fields) return replacements diff --git a/test/netlib/http/test_message.py b/test/netlib/http/test_message.py index 95fbb3d98..742723099 100644 --- a/test/netlib/http/test_message.py +++ b/test/netlib/http/test_message.py @@ -103,7 +103,7 @@ class TestMessage(object): r = tresp() r.content = b"foofootoo" r.replace(b"foo", "gg") - assert r.content == b"ggggtoo" + assert r.content == b"ggggtoo" r.content = b"foofootoo" r.replace(b"foo", "gg", count=1)