Skip to content

Commit f78ebc3

Browse files
authored
Merge pull request #11 from Buzz2d0/fix#9
Fix#10
2 parents 9962a49 + 8779ad8 commit f78ebc3

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

HackRequests/HackRequests.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ def httpraw(self, raw: str, **kwargs):
149149
proxy = kwargs.get("proxy", None)
150150
real_host = kwargs.get("real_host", None)
151151
ssl = kwargs.get("ssl", False)
152+
location = kwargs.get("location", True)
152153

153154
scheme = 'http'
154155
port = 80
@@ -245,6 +246,13 @@ def httpraw(self, raw: str, **kwargs):
245246
_url = "{scheme}://{host}{path}".format(scheme=scheme, host=host, path=path)
246247
else:
247248
_url = "{scheme}://{host}{path}".format(scheme=scheme, host=host + ":" + port, path=path)
249+
250+
redirect = rep.msg.get('location', None) # handle 301/302
251+
if redirect and location:
252+
if not redirect.startswith('http'):
253+
redirect = parse.urljoin(_url, redirect)
254+
return self.http(redirect, post=None, method=method, headers=headers, location=True, locationcount=1)
255+
248256
return response(rep, _url, log, )
249257

250258
def http(self, url, **kwargs):
@@ -368,7 +376,7 @@ def __init__(self, rep, redirect, log, oldcookie=''):
368376
self.cookies = {}
369377

370378
self.headers = _header_dict
371-
self.header = self.rep.msg # response header
379+
self.header = str(self.rep.msg) # response header
372380
self.log = log
373381
charset = self.rep.msg.get('content-type', 'utf-8')
374382
try:

tests/test_http_raw.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,23 @@ def test_chunked(self):
8989
r = self.hack.httpraw(raw)
9090
print(r.text())
9191
self.assertTrue("helloworld!helloworld hellowor ld" in r.text())
92+
93+
def test_redirect(self):
94+
raw = '''
95+
GET / HTTP/1.1
96+
Host: www.python.org
97+
Connection: Keep-Alive
98+
Cache-Control: max-age=0
99+
Upgrade-Insecure-Requests: 1
100+
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
101+
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
102+
Accept-Encoding: gzip, deflate
103+
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8
104+
'''
105+
r = self.hack.httpraw(raw)
106+
self.assertEqual(r.status_code, 200)
107+
self.assertIn('class="python home"', r.text())
108+
109+
r = self.hack.httpraw(raw, location=False)
110+
self.assertEqual(r.status_code, 301)
111+
self.assertTrue(r.text() == "")

0 commit comments

Comments
 (0)