Skip to content

Commit f077561

Browse files
committed
blank out headers after sending request, or the next request will be polluted.
1 parent 2728d39 commit f077561

File tree

5 files changed

+51
-32
lines changed

5 files changed

+51
-32
lines changed

qiniu/auth/digest.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ def __init__(self, host, mac=None):
5555
super(Client, self).__init__(host)
5656
self.mac = mac
5757

58-
def round_tripper(self, method, path, body):
58+
def round_tripper(self, method, path, body, header={}):
5959
token = self.mac.sign_request(
60-
path, body, self._header.get("Content-Type"))
61-
self.set_header("Authorization", "QBox %s" % token)
62-
return super(Client, self).round_tripper(method, path, body)
60+
path, body, header.get("Content-Type"))
61+
header["Authorization"] = "QBox %s" % token
62+
return super(Client, self).round_tripper(method, path, body, header)

qiniu/auth/up.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ def __init__(self, up_token, host=None):
1414
self.up_token = up_token
1515
super(Client, self).__init__(host)
1616

17-
def round_tripper(self, method, path, body):
18-
self.set_header("Authorization", "UpToken %s" % self.up_token)
19-
return super(Client, self).round_tripper(method, path, body)
17+
def round_tripper(self, method, path, body, header={}):
18+
header["Authorization"] = "UpToken %s" % self.up_token
19+
return super(Client, self).round_tripper(method, path, body, header)

qiniu/rpc.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import httplib
44

5-
if getattr(httplib, "_IMPLEMENTATION", None) != "gae": # httplib._IMPLEMENTATION is "gae" on GAE
5+
if getattr(httplib, "_IMPLEMENTATION", None) != "gae":
6+
# httplib._IMPLEMENTATION is "gae" on GAE
67
import httplib_chunk as httplib
78

89
import json
@@ -18,25 +19,31 @@ def __init__(self, host):
1819
self._conn = httplib.HTTPConnection(host)
1920
self._header = {}
2021

21-
def round_tripper(self, method, path, body):
22-
self._conn.request(method, path, body, self._header)
22+
def round_tripper(self, method, path, body, header={}):
23+
header = self.merged_headers(header)
24+
self._conn.request(method, path, body, header)
2325
resp = self._conn.getresponse()
2426
return resp
2527

28+
def merged_headers(self, header):
29+
_header = self._header.copy()
30+
_header.update(header)
31+
return _header
32+
2633
def call(self, path):
2734
return self.call_with(path, None)
2835

2936
def call_with(self, path, body, content_type=None, content_length=None):
3037
ret = None
3138

32-
self.set_header("User-Agent", conf.USER_AGENT)
39+
header = {"User-Agent": conf.USER_AGENT}
3340
if content_type is not None:
34-
self.set_header("Content-Type", content_type)
41+
header["Content-Type"] = content_type
3542

3643
if content_length is not None:
37-
self.set_header("Content-Length", content_length)
44+
header["Content-Length"] = content_length
3845

39-
resp = self.round_tripper("POST", path, body)
46+
resp = self.round_tripper("POST", path, body, header)
4047
try:
4148
ret = resp.read()
4249
ret = json.loads(ret)

qiniu/test/resumable_io_test.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import urllib
1414
import shutil
1515
import StringIO
16+
from tempfile import mktemp
1617

1718
from qiniu import conf
1819
from qiniu.auth import up
@@ -67,9 +68,9 @@ def test_put(self):
6768
src = urllib.urlopen("http://cheneya.qiniudn.com/hello_jpg")
6869
ostype = platform.system()
6970
if ostype.lower().find("windows") != -1:
70-
tmpf = "".join([os.getcwd(), os.tmpnam()])
71+
tmpf = "".join([os.getcwd(), mktemp()])
7172
else:
72-
tmpf = os.tmpnam()
73+
tmpf = mktemp()
7374
dst = open(tmpf, 'wb')
7475
shutil.copyfileobj(src, dst)
7576
src.close()
@@ -95,9 +96,9 @@ def test_put_4m(self):
9596
return
9697
ostype = platform.system()
9798
if ostype.lower().find("windows") != -1:
98-
tmpf = "".join([os.getcwd(), os.tmpnam()])
99+
tmpf = "".join([os.getcwd(), mktemp()])
99100
else:
100-
tmpf = os.tmpnam()
101+
tmpf = mktemp()
101102
dst = open(tmpf, 'wb')
102103
dst.write("abcd" * 1024 * 1024)
103104
dst.flush()

qiniu/test/rpc_test.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
from qiniu import conf
77

88

9-
def round_tripper(client, method, path, body):
9+
def round_tripper(client, method, path, body, header={}):
1010
pass
1111

1212

1313
class ClsTestClient(rpc.Client):
1414

15-
def round_tripper(self, method, path, body):
16-
round_tripper(self, method, path, body)
17-
return super(ClsTestClient, self).round_tripper(method, path, body)
15+
def round_tripper(self, method, path, body, header={}):
16+
round_tripper(self, method, path, body, header)
17+
return super(ClsTestClient, self).round_tripper(method, path, body, header)
1818

1919
client = ClsTestClient(conf.RS_HOST)
2020

@@ -24,7 +24,7 @@ class TestClient(unittest.TestCase):
2424
def test_call(self):
2525
global round_tripper
2626

27-
def tripper(client, method, path, body):
27+
def tripper(client, method, path, body, header={}):
2828
self.assertEqual(path, "/hello")
2929
assert body is None
3030

@@ -34,7 +34,7 @@ def tripper(client, method, path, body):
3434
def test_call_with(self):
3535
global round_tripper
3636

37-
def tripper(client, method, path, body):
37+
def tripper(client, method, path, body, header={}):
3838
self.assertEqual(body, "body")
3939

4040
round_tripper = tripper
@@ -43,16 +43,16 @@ def tripper(client, method, path, body):
4343
def test_call_with_multipart(self):
4444
global round_tripper
4545

46-
def tripper(client, method, path, body):
46+
def tripper(client, method, path, body, header={}):
4747
target_type = "multipart/form-data"
4848
self.assertTrue(
49-
client._header["Content-Type"].startswith(target_type))
50-
start_index = client._header["Content-Type"].find("boundary")
51-
boundary = client._header["Content-Type"][start_index + 9:]
49+
header["Content-Type"].startswith(target_type))
50+
start_index = header["Content-Type"].find("boundary")
51+
boundary = header["Content-Type"][start_index + 9:]
5252
dispostion = 'Content-Disposition: form-data; name="auth"'
5353
tpl = "--%s\r\n%s\r\n\r\n%s\r\n--%s--\r\n" % (boundary, dispostion,
5454
"auth_string", boundary)
55-
self.assertEqual(len(tpl), client._header["Content-Length"])
55+
self.assertEqual(len(tpl), header["Content-Length"])
5656
self.assertEqual(len(tpl), body.length())
5757

5858
round_tripper = tripper
@@ -61,15 +61,26 @@ def tripper(client, method, path, body):
6161
def test_call_with_form(self):
6262
global round_tripper
6363

64-
def tripper(client, method, path, body):
64+
def tripper(client, method, path, body, header={}):
6565
self.assertEqual(body, "action=a&op=a&op=b")
6666
target_type = "application/x-www-form-urlencoded"
67-
self.assertEqual(client._header["Content-Type"], target_type)
68-
self.assertEqual(client._header["Content-Length"], len(body))
67+
self.assertEqual(header["Content-Type"], target_type)
68+
self.assertEqual(header["Content-Length"], len(body))
6969

7070
round_tripper = tripper
7171
client.call_with_form("/hello", dict(op=["a", "b"], action="a"))
7272

73+
def test_call_after_call_with_form(self):
74+
# test case for https://github.com/qiniu/python-sdk/issues/112
75+
global round_tripper
76+
77+
def tripper(client, method, path, body, header={}):
78+
pass
79+
80+
round_tripper = tripper
81+
client.call_with_form("/hello", dict(op=["a", "b"], action="a"))
82+
client.call("/hello")
83+
7384

7485
class TestMultiReader(unittest.TestCase):
7586

0 commit comments

Comments
 (0)