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 |
11 # | 11 # |
12 # Unless required by applicable law or agreed to in writing, software | 12 # Unless required by applicable law or agreed to in writing, software |
13 # distributed under the License is distributed on an "AS IS" BASIS, | 13 # distributed under the License is distributed on an "AS IS" BASIS, |
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 # See the License for the specific language governing permissions and | 15 # See the License for the specific language governing permissions and |
16 # limitations under the License. | 16 # limitations under the License. |
17 | 17 |
18 """A one-shot testing webserver. | 18 """A one-shot testing webserver. |
19 | 19 |
20 When invoked, this server will write a short integer to stdout, indiciating on | 20 When invoked, this server will write a short integer to stdout, indiciating on |
21 which port the server is listening. It will then read one integer from stdin, | 21 which port the server is listening. It will then read one integer from stdin, |
22 indiciating the response code to be sent in response to a request. It also reads | 22 indiciating the response code to be sent in response to a request. It also reads |
23 8 characters from stdin, which, after having "\r\n" appended, will form the | 23 16 characters from stdin, which, after having "\r\n" appended, will form the |
24 response body in a successful response (one with code 200). The server will | 24 response body in a successful response (one with code 200). The server will |
25 process one HTTP request, deliver the prearranged response to the client, and | 25 process one HTTP request, deliver the prearranged response to the client, and |
26 write the entire request to stdout. It will then terminate. | 26 write the entire request to stdout. It will then terminate. |
27 | 27 |
28 This server is written in Python since it provides a simple HTTP stack, and | 28 This server is written in Python since it provides a simple HTTP stack, and |
29 because parsing Chunked encoding is safer and easier in a memory-safe language. | 29 because parsing Chunked encoding is safer and easier in a memory-safe language. |
30 This could easily have been written in C++ instead. | 30 This could easily have been written in C++ instead. |
31 """ | 31 """ |
32 | 32 |
33 import BaseHTTPServer | 33 import BaseHTTPServer |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) | 137 msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) |
138 | 138 |
139 # Start the server. | 139 # Start the server. |
140 server = BaseHTTPServer.HTTPServer(('127.0.0.1', 0), RequestHandler) | 140 server = BaseHTTPServer.HTTPServer(('127.0.0.1', 0), RequestHandler) |
141 | 141 |
142 # Write the port as an unsigned short to the parent process. | 142 # Write the port as an unsigned short to the parent process. |
143 sys.stdout.write(struct.pack('=H', server.server_address[1])) | 143 sys.stdout.write(struct.pack('=H', server.server_address[1])) |
144 sys.stdout.flush() | 144 sys.stdout.flush() |
145 | 145 |
146 # Read the desired test response code as an unsigned short and the desired | 146 # Read the desired test response code as an unsigned short and the desired |
147 # response body as an 8-byte string from the parent process. | 147 # response body as a 16-byte string from the parent process. |
148 RequestHandler.response_code, RequestHandler.response_body = \ | 148 RequestHandler.response_code, RequestHandler.response_body = \ |
149 struct.unpack('=H8s', sys.stdin.read(struct.calcsize('=H8s'))) | 149 struct.unpack('=H16s', sys.stdin.read(struct.calcsize('=H16s'))) |
150 | 150 |
151 # Handle the request. | 151 # Handle the request. |
152 server.handle_request() | 152 server.handle_request() |
153 | 153 |
154 # Share the entire request with the test program, which will validate it. | 154 # Share the entire request with the test program, which will validate it. |
155 sys.stdout.write(RequestHandler.raw_request) | 155 sys.stdout.write(RequestHandler.raw_request) |
156 sys.stdout.flush() | 156 sys.stdout.flush() |
157 | 157 |
158 if __name__ == '__main__': | 158 if __name__ == '__main__': |
159 Main() | 159 Main() |
OLD | NEW |