OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # coding: utf-8 | 2 # coding: utf-8 |
3 | 3 |
4 # Copyright 2014 The Crashpad Authors. All rights reserved. | 4 # Copyright 2014 The Crashpad Authors. All rights reserved. |
5 # | 5 # |
6 # Licensed under the Apache License, Version 2.0 (the "License"); | 6 # Licensed under the Apache License, Version 2.0 (the "License"); |
7 # you may not use this file except in compliance with the License. | 7 # you may not use this file except in compliance with the License. |
8 # You may obtain a copy of the License at | 8 # You may obtain a copy of the License at |
9 # | 9 # |
10 # http://www.apache.org/licenses/LICENSE-2.0 | 10 # http://www.apache.org/licenses/LICENSE-2.0 |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 writer.write(self.rfile.buffer) | 70 writer.write(self.rfile.buffer) |
71 self.rfile.buffer = '' | 71 self.rfile.buffer = '' |
72 | 72 |
73 if self.headers.get('Transfer-Encoding', '') == 'Chunked': | 73 if self.headers.get('Transfer-Encoding', '') == 'Chunked': |
74 body = self.handle_chunked_encoding() | 74 body = self.handle_chunked_encoding() |
75 else: | 75 else: |
76 length = int(self.headers.get('Content-Length', -1)) | 76 length = int(self.headers.get('Content-Length', -1)) |
77 body = self.rfile.read(length) | 77 body = self.rfile.read(length) |
78 | 78 |
79 self.send_response(self.response_code) | 79 self.send_response(self.response_code) |
| 80 self.end_headers() |
| 81 |
80 writer.write(body) | 82 writer.write(body) |
81 writer.flush() | 83 writer.flush() |
82 | 84 |
83 def handle_chunked_encoding(self): | 85 def handle_chunked_encoding(self): |
84 """This parses a "Transfer-Encoding: Chunked" body in accordance with | 86 """This parses a "Transfer-Encoding: Chunked" body in accordance with |
85 RFC 7230 §4.1. This returns the result as a string. | 87 RFC 7230 §4.1. This returns the result as a string. |
86 """ | 88 """ |
87 body = '' | 89 body = '' |
88 chunk_size = self.read_chunk_size() | 90 chunk_size = self.read_chunk_size() |
89 while chunk_size > 0: | 91 while chunk_size > 0: |
(...skipping 21 matching lines...) Expand all Loading... |
111 if chunk_size_end == -1: | 113 if chunk_size_end == -1: |
112 # No chunk extensions; just encounter the end of line. | 114 # No chunk extensions; just encounter the end of line. |
113 chunk_size_end = chunk_size_and_ext_line.find('\r') | 115 chunk_size_end = chunk_size_and_ext_line.find('\r') |
114 if chunk_size_end == -1: | 116 if chunk_size_end == -1: |
115 self.send_response(400) # Bad request. | 117 self.send_response(400) # Bad request. |
116 return -1 | 118 return -1 |
117 return int(chunk_size_and_ext_line[:chunk_size_end], base=16) | 119 return int(chunk_size_and_ext_line[:chunk_size_end], base=16) |
118 | 120 |
119 | 121 |
120 def Main(): | 122 def Main(): |
| 123 if sys.platform == 'win32': |
| 124 import os, msvcrt |
| 125 msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) |
| 126 |
121 # Start the server. | 127 # Start the server. |
122 server = BaseHTTPServer.HTTPServer(('127.0.0.1', 0), RequestHandler) | 128 server = BaseHTTPServer.HTTPServer(('127.0.0.1', 0), RequestHandler) |
123 | 129 |
124 # Write the port as an unsigned short to the parent process. | 130 # Write the port as an unsigned short to the parent process. |
125 sys.stdout.write(struct.pack('=H', server.server_address[1])) | 131 sys.stdout.write(struct.pack('=H', server.server_address[1])) |
126 sys.stdout.flush() | 132 sys.stdout.flush() |
127 | 133 |
128 # Read the desired test response code as an unsigned short from the parent | 134 # Read the desired test response code as an unsigned short from the parent |
129 # process. | 135 # process. |
130 RequestHandler.response_code = \ | 136 RequestHandler.response_code = \ |
131 struct.unpack('=H', sys.stdin.read(struct.calcsize('=H')))[0] | 137 struct.unpack('=H', sys.stdin.read(struct.calcsize('=H')))[0] |
132 | 138 |
133 # Handle the request. | 139 # Handle the request. |
134 server.handle_request() | 140 server.handle_request() |
135 | 141 |
136 if __name__ == '__main__': | 142 if __name__ == '__main__': |
137 Main() | 143 Main() |
OLD | NEW |