Project

General

Profile

Bug #2913 » serverEncodeChunked.py

minimal python3 backend http server to recreate the issue - blowfist, 2018-10-07 02:43

 
1
# a very simple python3 HTTP responder
2
# which hands chunked encoded packets
3

    
4
import sys
5
from http.server import BaseHTTPRequestHandler, HTTPServer
6

    
7
def chunk_data(data, chunk_size):
8
    dl = len(data)
9
    ret = ""
10
    for i in range(dl // chunk_size):
11
        ret += "%s\r\n" % (hex(chunk_size)[2:])
12
        ret += "%s\r\n" % (data[i * chunk_size : (i + 1) * chunk_size])
13

    
14
    if len(data) % chunk_size != 0:
15
        ret += "%s\r\n" % (hex(len(data) % chunk_size)[2:])
16
        ret += "%s\r\n" % (data[-(len(data) % chunk_size):])
17

    
18
    ret += "0\r\n\r\n"
19
    return ret
20

    
21
class responderHTTPServerRequestHandler(BaseHTTPRequestHandler):
22
    def do_GET(self):
23
        self.send_response(200)
24

    
25
        self.send_header('Content-type', 'text/html')
26
        self.send_header('Transfer-Encoding', 'chunked')
27
        self.end_headers()
28

    
29
        result="<html>\n<body>\n<h1>This works!</h1>\n</body>\n</html>"
30

    
31
        #self.wfile.write(bytes(result, "utf-8"))
32
        self.wfile.write(bytes(chunk_data(result, 5), "utf-8"))
33
    def do_HEAD(self):
34
        self.do_GET()
35

    
36
def main():
37
    httpd = HTTPServer(('127.0.0.1', 6080), responderHTTPServerRequestHandler)
38
    httpd.serve_forever()
39

    
40
main()
(3-3/3)