Skip to content

#112: blank out headers after sending request #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 28, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions qiniu/auth/digest.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ def __init__(self, host, mac=None):
super(Client, self).__init__(host)
self.mac = mac

def round_tripper(self, method, path, body):
def round_tripper(self, method, path, body, header={}):
token = self.mac.sign_request(
path, body, self._header.get("Content-Type"))
self.set_header("Authorization", "QBox %s" % token)
return super(Client, self).round_tripper(method, path, body)
path, body, header.get("Content-Type"))
header["Authorization"] = "QBox %s" % token
return super(Client, self).round_tripper(method, path, body, header)
6 changes: 3 additions & 3 deletions qiniu/auth/up.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ def __init__(self, up_token, host=None):
self.up_token = up_token
super(Client, self).__init__(host)

def round_tripper(self, method, path, body):
self.set_header("Authorization", "UpToken %s" % self.up_token)
return super(Client, self).round_tripper(method, path, body)
def round_tripper(self, method, path, body, header={}):
header["Authorization"] = "UpToken %s" % self.up_token
return super(Client, self).round_tripper(method, path, body, header)
21 changes: 14 additions & 7 deletions qiniu/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import httplib

if getattr(httplib, "_IMPLEMENTATION", None) != "gae": # httplib._IMPLEMENTATION is "gae" on GAE
if getattr(httplib, "_IMPLEMENTATION", None) != "gae":
# httplib._IMPLEMENTATION is "gae" on GAE
import httplib_chunk as httplib

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

def round_tripper(self, method, path, body):
self._conn.request(method, path, body, self._header)
def round_tripper(self, method, path, body, header={}):
header = self.merged_headers(header)
self._conn.request(method, path, body, header)
resp = self._conn.getresponse()
return resp

def merged_headers(self, header):
_header = self._header.copy()
_header.update(header)
return _header

def call(self, path):
return self.call_with(path, None)

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

self.set_header("User-Agent", conf.USER_AGENT)
header = {"User-Agent": conf.USER_AGENT}
if content_type is not None:
self.set_header("Content-Type", content_type)
header["Content-Type"] = content_type

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

resp = self.round_tripper("POST", path, body)
resp = self.round_tripper("POST", path, body, header)
try:
ret = resp.read()
ret = json.loads(ret)
Expand Down
9 changes: 5 additions & 4 deletions qiniu/test/resumable_io_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import urllib
import shutil
import StringIO
from tempfile import mktemp

from qiniu import conf
from qiniu.auth import up
Expand Down Expand Up @@ -67,9 +68,9 @@ def test_put(self):
src = urllib.urlopen("http://cheneya.qiniudn.com/hello_jpg")
ostype = platform.system()
if ostype.lower().find("windows") != -1:
tmpf = "".join([os.getcwd(), os.tmpnam()])
tmpf = "".join([os.getcwd(), mktemp()])
else:
tmpf = os.tmpnam()
tmpf = mktemp()
dst = open(tmpf, 'wb')
shutil.copyfileobj(src, dst)
src.close()
Expand All @@ -95,9 +96,9 @@ def test_put_4m(self):
return
ostype = platform.system()
if ostype.lower().find("windows") != -1:
tmpf = "".join([os.getcwd(), os.tmpnam()])
tmpf = "".join([os.getcwd(), mktemp()])
else:
tmpf = os.tmpnam()
tmpf = mktemp()
dst = open(tmpf, 'wb')
dst.write("abcd" * 1024 * 1024)
dst.flush()
Expand Down
39 changes: 25 additions & 14 deletions qiniu/test/rpc_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
from qiniu import conf


def round_tripper(client, method, path, body):
def round_tripper(client, method, path, body, header={}):
pass


class ClsTestClient(rpc.Client):

def round_tripper(self, method, path, body):
round_tripper(self, method, path, body)
return super(ClsTestClient, self).round_tripper(method, path, body)
def round_tripper(self, method, path, body, header={}):
round_tripper(self, method, path, body, header)
return super(ClsTestClient, self).round_tripper(method, path, body, header)

client = ClsTestClient(conf.RS_HOST)

Expand All @@ -24,7 +24,7 @@ class TestClient(unittest.TestCase):
def test_call(self):
global round_tripper

def tripper(client, method, path, body):
def tripper(client, method, path, body, header={}):
self.assertEqual(path, "/hello")
assert body is None

Expand All @@ -34,7 +34,7 @@ def tripper(client, method, path, body):
def test_call_with(self):
global round_tripper

def tripper(client, method, path, body):
def tripper(client, method, path, body, header={}):
self.assertEqual(body, "body")

round_tripper = tripper
Expand All @@ -43,16 +43,16 @@ def tripper(client, method, path, body):
def test_call_with_multipart(self):
global round_tripper

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

round_tripper = tripper
Expand All @@ -61,15 +61,26 @@ def tripper(client, method, path, body):
def test_call_with_form(self):
global round_tripper

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

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

def test_call_after_call_with_form(self):
# test case for https://github.com/qiniu/python-sdk/issues/112
global round_tripper

def tripper(client, method, path, body, header={}):
pass

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


class TestMultiReader(unittest.TestCase):

Expand Down