Skip to content

Commit

Permalink
Merge pull request #2 from startersclan/fix/python-fix-http-library-t…
Browse files Browse the repository at this point in the history
…o-support-chunked-responses

Fix (python): Fix http library to support chunked responses
  • Loading branch information
leojonathanoh authored Oct 7, 2022
2 parents a99eec5 + 176de4d commit b21652e
Showing 1 changed file with 41 additions and 13 deletions.
54 changes: 41 additions & 13 deletions stats/miniclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,34 @@ def http_get(host, port = 80, document = "/"):
print "MiniClient: Non-numeric status code (%s)" % str(status[1])

#Extract Headers
headers = []
headers = {}
while 1:
line = http.readline()
if not line:
break
headers.append(line)

http.shutdown() # be nice, tell the http server we're done sending the request
http.close() # all done
split = line.split(":")
headers[split[0].strip()] = split[1].strip()

#Check we got a valid HTTP response
if statusCode == 200:
return http.read()
body = ""
if "Content-Length" in headers:
content_length = headers["Content-Length"]
body = http.read()
elif headers["Transfer-Encoding"] == "chunked":
while 1:
chunk_length = int(http.readline(), 16)
if chunk_length != 0:
body += http.read(chunk_length)
http.readline() # CRLF
if chunk_length == 0:
break
http.shutdown() # be nice, tell the http server we're done sending the request
http.close() # all done
return body
else:
http.shutdown() # be nice, tell the http server we're done sending the request
http.close() # all done
return "E\nH\terr\nD\tHTTP Error %s \"%s\"\n$\tERR\t$" % (str(statusCode), str(status[2]))

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

#Extract Headers
headers = []
headers = {}
while 1:
line = http.readline()
if not line:
break
headers.append(line)

http.shutdown() # be nice, tell the http server we're done sending the request
http.close() # all done

split = line.split(":")
headers[split[0].strip()] = split[1].strip()

if statusCode == 200:
return http.read()
body = ""
if "Content-Length" in headers:
content_length = headers["Content-Length"]
body = http.read()
elif headers["Transfer-Encoding"] == "chunked":
while 1:
chunk_length = int(http.readline(), 16)
if chunk_length != 0:
body += http.read(chunk_length)
http.readline() # CRLF
if chunk_length == 0:
break
http.shutdown() # be nice, tell the http server we're done sending the request
http.close() # all done
return body
else:
http.shutdown() # be nice, tell the http server we're done sending the request
http.close() # all done
return "E\nH\terr\nD\tHTTP Error %s \"%s\"\n$\tERR\t$" % (str(statusCode), str(status[2]))

except Exception, e:
Expand Down

0 comments on commit b21652e

Please sign in to comment.