Skip to content

Commit b21652e

Browse files
Merge pull request #2 from startersclan/fix/python-fix-http-library-to-support-chunked-responses
Fix (python): Fix http library to support chunked responses
2 parents a99eec5 + 176de4d commit b21652e

1 file changed

Lines changed: 41 additions & 13 deletions

File tree

stats/miniclient.py

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,34 @@ def http_get(host, port = 80, document = "/"):
3737
print "MiniClient: Non-numeric status code (%s)" % str(status[1])
3838

3939
#Extract Headers
40-
headers = []
40+
headers = {}
4141
while 1:
4242
line = http.readline()
4343
if not line:
4444
break
45-
headers.append(line)
46-
47-
http.shutdown() # be nice, tell the http server we're done sending the request
48-
http.close() # all done
45+
split = line.split(":")
46+
headers[split[0].strip()] = split[1].strip()
4947

5048
#Check we got a valid HTTP response
5149
if statusCode == 200:
52-
return http.read()
50+
body = ""
51+
if "Content-Length" in headers:
52+
content_length = headers["Content-Length"]
53+
body = http.read()
54+
elif headers["Transfer-Encoding"] == "chunked":
55+
while 1:
56+
chunk_length = int(http.readline(), 16)
57+
if chunk_length != 0:
58+
body += http.read(chunk_length)
59+
http.readline() # CRLF
60+
if chunk_length == 0:
61+
break
62+
http.shutdown() # be nice, tell the http server we're done sending the request
63+
http.close() # all done
64+
return body
5365
else:
66+
http.shutdown() # be nice, tell the http server we're done sending the request
67+
http.close() # all done
5468
return "E\nH\terr\nD\tHTTP Error %s \"%s\"\n$\tERR\t$" % (str(statusCode), str(status[2]))
5569

5670
except Exception, e:
@@ -96,19 +110,33 @@ def http_postSnapshot(host, port = 80, document = "/", snapshot = ""):
96110
print "MiniClient: Non-numeric status code (%s)" % str(status[1])
97111

98112
#Extract Headers
99-
headers = []
113+
headers = {}
100114
while 1:
101115
line = http.readline()
102116
if not line:
103117
break
104-
headers.append(line)
105-
106-
http.shutdown() # be nice, tell the http server we're done sending the request
107-
http.close() # all done
108-
118+
split = line.split(":")
119+
headers[split[0].strip()] = split[1].strip()
120+
109121
if statusCode == 200:
110-
return http.read()
122+
body = ""
123+
if "Content-Length" in headers:
124+
content_length = headers["Content-Length"]
125+
body = http.read()
126+
elif headers["Transfer-Encoding"] == "chunked":
127+
while 1:
128+
chunk_length = int(http.readline(), 16)
129+
if chunk_length != 0:
130+
body += http.read(chunk_length)
131+
http.readline() # CRLF
132+
if chunk_length == 0:
133+
break
134+
http.shutdown() # be nice, tell the http server we're done sending the request
135+
http.close() # all done
136+
return body
111137
else:
138+
http.shutdown() # be nice, tell the http server we're done sending the request
139+
http.close() # all done
112140
return "E\nH\terr\nD\tHTTP Error %s \"%s\"\n$\tERR\t$" % (str(statusCode), str(status[2]))
113141

114142
except Exception, e:

0 commit comments

Comments
 (0)