| OLD | NEW |
| 1 #!/usr/bin/python2.4 | 1 #!/usr/bin/python2.4 |
| 2 # Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """This is a simple HTTP server used for testing Chrome. | 6 """This is a simple HTTP server used for testing Chrome. |
| 7 | 7 |
| 8 It supports several test URLs, as specified by the handlers in TestPageHandler. | 8 It supports several test URLs, as specified by the handlers in TestPageHandler. |
| 9 It defaults to living on localhost:8888. | 9 It defaults to living on localhost:8888. |
| 10 It can use https if you specify the flag --https=CERT where CERT is the path | 10 It can use https if you specify the flag --https=CERT where CERT is the path |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 self._post_handlers = [ | 102 self._post_handlers = [ |
| 103 self.EchoTitleHandler, | 103 self.EchoTitleHandler, |
| 104 self.EchoAllHandler, | 104 self.EchoAllHandler, |
| 105 self.EchoHandler] + self._get_handlers | 105 self.EchoHandler] + self._get_handlers |
| 106 | 106 |
| 107 self._mime_types = { 'gif': 'image/gif', 'jpeg' : 'image/jpeg', 'jpg' : 'im
age/jpeg' } | 107 self._mime_types = { 'gif': 'image/gif', 'jpeg' : 'image/jpeg', 'jpg' : 'im
age/jpeg' } |
| 108 self._default_mime_type = 'text/html' | 108 self._default_mime_type = 'text/html' |
| 109 | 109 |
| 110 BaseHTTPServer.BaseHTTPRequestHandler.__init__(self, request, client_address
, socket_server) | 110 BaseHTTPServer.BaseHTTPRequestHandler.__init__(self, request, client_address
, socket_server) |
| 111 | 111 |
| 112 def _ShouldHandleRequest(self, handler_name): |
| 113 """Determines if the path can be handled by the handler. |
| 114 |
| 115 We consider a handler valid if the path begins with the |
| 116 handler name. It can optionally be followed by "?*", "/*". |
| 117 """ |
| 118 |
| 119 pattern = re.compile('%s($|\?|/).*' % handler_name) |
| 120 return pattern.match(self.path) |
| 121 |
| 112 def GetMIMETypeFromName(self, file_name): | 122 def GetMIMETypeFromName(self, file_name): |
| 113 """Returns the mime type for the specified file_name. So far it only looks | 123 """Returns the mime type for the specified file_name. So far it only looks |
| 114 at the file extension.""" | 124 at the file extension.""" |
| 115 | 125 |
| 116 (shortname, extension) = os.path.splitext(file_name) | 126 (shortname, extension) = os.path.splitext(file_name) |
| 117 if len(extension) == 0: | 127 if len(extension) == 0: |
| 118 # no extension. | 128 # no extension. |
| 119 return self._default_mime_type | 129 return self._default_mime_type |
| 120 | 130 |
| 121 return self._mime_types.get(extension, self._default_mime_type) | 131 return self._mime_types.get(extension, self._default_mime_type) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 133 self.end_headers() | 143 self.end_headers() |
| 134 self.wfile.write("Time to die") | 144 self.wfile.write("Time to die") |
| 135 self.server.stop = True | 145 self.server.stop = True |
| 136 | 146 |
| 137 return True | 147 return True |
| 138 | 148 |
| 139 def NoCacheMaxAgeTimeHandler(self): | 149 def NoCacheMaxAgeTimeHandler(self): |
| 140 """This request handler yields a page with the title set to the current | 150 """This request handler yields a page with the title set to the current |
| 141 system time, and no caching requested.""" | 151 system time, and no caching requested.""" |
| 142 | 152 |
| 143 if (self.path.find("/nocachetime/maxage") != 0): | 153 if not self._ShouldHandleRequest("/nocachetime/maxage"): |
| 144 return False | 154 return False |
| 145 | 155 |
| 146 self.send_response(200) | 156 self.send_response(200) |
| 147 self.send_header('Cache-Control', 'max-age=0') | 157 self.send_header('Cache-Control', 'max-age=0') |
| 148 self.send_header('Content-type', 'text/html') | 158 self.send_header('Content-type', 'text/html') |
| 149 self.end_headers() | 159 self.end_headers() |
| 150 | 160 |
| 151 self.wfile.write('<html><head><title>%s</title></head></html>' % time.time()
) | 161 self.wfile.write('<html><head><title>%s</title></head></html>' % time.time()
) |
| 152 | 162 |
| 153 return True | 163 return True |
| 154 | 164 |
| 155 def NoCacheTimeHandler(self): | 165 def NoCacheTimeHandler(self): |
| 156 """This request handler yields a page with the title set to the current | 166 """This request handler yields a page with the title set to the current |
| 157 system time, and no caching requested.""" | 167 system time, and no caching requested.""" |
| 158 | 168 |
| 159 if (self.path.find("/nocachetime") != 0): | 169 if not self._ShouldHandleRequest("/nocachetime"): |
| 160 return False | 170 return False |
| 161 | 171 |
| 162 self.send_response(200) | 172 self.send_response(200) |
| 163 self.send_header('Cache-Control', 'no-cache') | 173 self.send_header('Cache-Control', 'no-cache') |
| 164 self.send_header('Content-type', 'text/html') | 174 self.send_header('Content-type', 'text/html') |
| 165 self.end_headers() | 175 self.end_headers() |
| 166 | 176 |
| 167 self.wfile.write('<html><head><title>%s</title></head></html>' % time.time()
) | 177 self.wfile.write('<html><head><title>%s</title></head></html>' % time.time()
) |
| 168 | 178 |
| 169 return True | 179 return True |
| 170 | 180 |
| 171 def CacheTimeHandler(self): | 181 def CacheTimeHandler(self): |
| 172 """This request handler yields a page with the title set to the current | 182 """This request handler yields a page with the title set to the current |
| 173 system time, and allows caching for one minute.""" | 183 system time, and allows caching for one minute.""" |
| 174 | 184 |
| 175 if self.path.find("/cachetime") != 0: | 185 if not self._ShouldHandleRequest("/cachetime"): |
| 176 return False | 186 return False |
| 177 | 187 |
| 178 self.send_response(200) | 188 self.send_response(200) |
| 179 self.send_header('Cache-Control', 'max-age=60') | 189 self.send_header('Cache-Control', 'max-age=60') |
| 180 self.send_header('Content-type', 'text/html') | 190 self.send_header('Content-type', 'text/html') |
| 181 self.end_headers() | 191 self.end_headers() |
| 182 | 192 |
| 183 self.wfile.write('<html><head><title>%s</title></head></html>' % time.time()
) | 193 self.wfile.write('<html><head><title>%s</title></head></html>' % time.time()
) |
| 184 | 194 |
| 185 return True | 195 return True |
| 186 | 196 |
| 187 def CacheExpiresHandler(self): | 197 def CacheExpiresHandler(self): |
| 188 """This request handler yields a page with the title set to the current | 198 """This request handler yields a page with the title set to the current |
| 189 system time, and set the page to expire on 1 Jan 2099.""" | 199 system time, and set the page to expire on 1 Jan 2099.""" |
| 190 | 200 |
| 191 if (self.path.find("/cache/expires") != 0): | 201 if not self._ShouldHandleRequest("/cache/expires"): |
| 192 return False | 202 return False |
| 193 | 203 |
| 194 self.send_response(200) | 204 self.send_response(200) |
| 195 self.send_header('Expires', 'Thu, 1 Jan 2099 00:00:00 GMT') | 205 self.send_header('Expires', 'Thu, 1 Jan 2099 00:00:00 GMT') |
| 196 self.send_header('Content-type', 'text/html') | 206 self.send_header('Content-type', 'text/html') |
| 197 self.end_headers() | 207 self.end_headers() |
| 198 | 208 |
| 199 self.wfile.write('<html><head><title>%s</title></head></html>' % time.time()
) | 209 self.wfile.write('<html><head><title>%s</title></head></html>' % time.time()
) |
| 200 | 210 |
| 201 return True | 211 return True |
| 202 | 212 |
| 203 def CacheProxyRevalidateHandler(self): | 213 def CacheProxyRevalidateHandler(self): |
| 204 """This request handler yields a page with the title set to the current | 214 """This request handler yields a page with the title set to the current |
| 205 system time, and allows caching for 60 seconds""" | 215 system time, and allows caching for 60 seconds""" |
| 206 | 216 |
| 207 if (self.path.find("/cache/proxy-revalidate") != 0): | 217 if not self._ShouldHandleRequest("/cache/proxy-revalidate"): |
| 208 return False | 218 return False |
| 209 | 219 |
| 210 self.send_response(200) | 220 self.send_response(200) |
| 211 self.send_header('Content-type', 'text/html') | 221 self.send_header('Content-type', 'text/html') |
| 212 self.send_header('Cache-Control', 'max-age=60, proxy-revalidate') | 222 self.send_header('Cache-Control', 'max-age=60, proxy-revalidate') |
| 213 self.end_headers() | 223 self.end_headers() |
| 214 | 224 |
| 215 self.wfile.write('<html><head><title>%s</title></head></html>' % time.time()
) | 225 self.wfile.write('<html><head><title>%s</title></head></html>' % time.time()
) |
| 216 | 226 |
| 217 return True | 227 return True |
| 218 | 228 |
| 219 def CachePrivateHandler(self): | 229 def CachePrivateHandler(self): |
| 220 """This request handler yields a page with the title set to the current | 230 """This request handler yields a page with the title set to the current |
| 221 system time, and allows caching for 5 seconds.""" | 231 system time, and allows caching for 5 seconds.""" |
| 222 | 232 |
| 223 if (self.path.find("/cache/private") != 0): | 233 if not self._ShouldHandleRequest("/cache/private"): |
| 224 return False | 234 return False |
| 225 | 235 |
| 226 self.send_response(200) | 236 self.send_response(200) |
| 227 self.send_header('Content-type', 'text/html') | 237 self.send_header('Content-type', 'text/html') |
| 228 self.send_header('Cache-Control', 'max-age=5, private') | 238 self.send_header('Cache-Control', 'max-age=5, private') |
| 229 self.end_headers() | 239 self.end_headers() |
| 230 | 240 |
| 231 self.wfile.write('<html><head><title>%s</title></head></html>' % time.time()
) | 241 self.wfile.write('<html><head><title>%s</title></head></html>' % time.time()
) |
| 232 | 242 |
| 233 return True | 243 return True |
| 234 | 244 |
| 235 def CachePublicHandler(self): | 245 def CachePublicHandler(self): |
| 236 """This request handler yields a page with the title set to the current | 246 """This request handler yields a page with the title set to the current |
| 237 system time, and allows caching for 5 seconds.""" | 247 system time, and allows caching for 5 seconds.""" |
| 238 | 248 |
| 239 if (self.path.find("/cache/public") != 0): | 249 if not self._ShouldHandleRequest("/cache/public"): |
| 240 return False | 250 return False |
| 241 | 251 |
| 242 self.send_response(200) | 252 self.send_response(200) |
| 243 self.send_header('Content-type', 'text/html') | 253 self.send_header('Content-type', 'text/html') |
| 244 self.send_header('Cache-Control', 'max-age=5, public') | 254 self.send_header('Cache-Control', 'max-age=5, public') |
| 245 self.end_headers() | 255 self.end_headers() |
| 246 | 256 |
| 247 self.wfile.write('<html><head><title>%s</title></head></html>' % time.time()
) | 257 self.wfile.write('<html><head><title>%s</title></head></html>' % time.time()
) |
| 248 | 258 |
| 249 return True | 259 return True |
| 250 | 260 |
| 251 def CacheSMaxAgeHandler(self): | 261 def CacheSMaxAgeHandler(self): |
| 252 """This request handler yields a page with the title set to the current | 262 """This request handler yields a page with the title set to the current |
| 253 system time, and does not allow for caching.""" | 263 system time, and does not allow for caching.""" |
| 254 | 264 |
| 255 if (self.path.find("/cache/s-maxage") != 0): | 265 if not self._ShouldHandleRequest("/cache/s-maxage"): |
| 256 return False | 266 return False |
| 257 | 267 |
| 258 self.send_response(200) | 268 self.send_response(200) |
| 259 self.send_header('Content-type', 'text/html') | 269 self.send_header('Content-type', 'text/html') |
| 260 self.send_header('Cache-Control', 'public, s-maxage = 60, max-age = 0') | 270 self.send_header('Cache-Control', 'public, s-maxage = 60, max-age = 0') |
| 261 self.end_headers() | 271 self.end_headers() |
| 262 | 272 |
| 263 self.wfile.write('<html><head><title>%s</title></head></html>' % time.time()
) | 273 self.wfile.write('<html><head><title>%s</title></head></html>' % time.time()
) |
| 264 | 274 |
| 265 return True | 275 return True |
| 266 | 276 |
| 267 def CacheMustRevalidateHandler(self): | 277 def CacheMustRevalidateHandler(self): |
| 268 """This request handler yields a page with the title set to the current | 278 """This request handler yields a page with the title set to the current |
| 269 system time, and does not allow caching.""" | 279 system time, and does not allow caching.""" |
| 270 | 280 |
| 271 if (self.path.find("/cache/must-revalidate") != 0): | 281 if not self._ShouldHandleRequest("/cache/must-revalidate"): |
| 272 return False | 282 return False |
| 273 | 283 |
| 274 self.send_response(200) | 284 self.send_response(200) |
| 275 self.send_header('Content-type', 'text/html') | 285 self.send_header('Content-type', 'text/html') |
| 276 self.send_header('Cache-Control', 'must-revalidate') | 286 self.send_header('Cache-Control', 'must-revalidate') |
| 277 self.end_headers() | 287 self.end_headers() |
| 278 | 288 |
| 279 self.wfile.write('<html><head><title>%s</title></head></html>' % time.time()
) | 289 self.wfile.write('<html><head><title>%s</title></head></html>' % time.time()
) |
| 280 | 290 |
| 281 return True | 291 return True |
| 282 | 292 |
| 283 def CacheMustRevalidateMaxAgeHandler(self): | 293 def CacheMustRevalidateMaxAgeHandler(self): |
| 284 """This request handler yields a page with the title set to the current | 294 """This request handler yields a page with the title set to the current |
| 285 system time, and does not allow caching event though max-age of 60 | 295 system time, and does not allow caching event though max-age of 60 |
| 286 seconds is specified.""" | 296 seconds is specified.""" |
| 287 | 297 |
| 288 if (self.path.find("/cache/must-revalidate/max-age") != 0): | 298 if not self._ShouldHandleRequest("/cache/must-revalidate/max-age"): |
| 289 return False | 299 return False |
| 290 | 300 |
| 291 self.send_response(200) | 301 self.send_response(200) |
| 292 self.send_header('Content-type', 'text/html') | 302 self.send_header('Content-type', 'text/html') |
| 293 self.send_header('Cache-Control', 'max-age=60, must-revalidate') | 303 self.send_header('Cache-Control', 'max-age=60, must-revalidate') |
| 294 self.end_headers() | 304 self.end_headers() |
| 295 | 305 |
| 296 self.wfile.write('<html><head><title>%s</title></head></html>' % time.time()
) | 306 self.wfile.write('<html><head><title>%s</title></head></html>' % time.time()
) |
| 297 | 307 |
| 298 return True | 308 return True |
| 299 | 309 |
| 300 | |
| 301 def CacheNoStoreHandler(self): | 310 def CacheNoStoreHandler(self): |
| 302 """This request handler yields a page with the title set to the current | 311 """This request handler yields a page with the title set to the current |
| 303 system time, and does not allow the page to be stored.""" | 312 system time, and does not allow the page to be stored.""" |
| 304 | 313 |
| 305 if (self.path.find("/cache/no-store") != 0): | 314 if not self._ShouldHandleRequest("/cache/no-store"): |
| 306 return False | 315 return False |
| 307 | 316 |
| 308 self.send_response(200) | 317 self.send_response(200) |
| 309 self.send_header('Content-type', 'text/html') | 318 self.send_header('Content-type', 'text/html') |
| 310 self.send_header('Cache-Control', 'no-store') | 319 self.send_header('Cache-Control', 'no-store') |
| 311 self.end_headers() | 320 self.end_headers() |
| 312 | 321 |
| 313 self.wfile.write('<html><head><title>%s</title></head></html>' % time.time()
) | 322 self.wfile.write('<html><head><title>%s</title></head></html>' % time.time()
) |
| 314 | 323 |
| 315 return True | 324 return True |
| 316 | 325 |
| 317 def CacheNoStoreMaxAgeHandler(self): | 326 def CacheNoStoreMaxAgeHandler(self): |
| 318 """This request handler yields a page with the title set to the current | 327 """This request handler yields a page with the title set to the current |
| 319 system time, and does not allow the page to be stored even though max-age | 328 system time, and does not allow the page to be stored even though max-age |
| 320 of 60 seconds is specified.""" | 329 of 60 seconds is specified.""" |
| 321 | 330 |
| 322 if (self.path.find("/cache/no-store/max-age") != 0): | 331 if not self._ShouldHandleRequest("/cache/no-store/max-age"): |
| 323 return False | 332 return False |
| 324 | 333 |
| 325 self.send_response(200) | 334 self.send_response(200) |
| 326 self.send_header('Content-type', 'text/html') | 335 self.send_header('Content-type', 'text/html') |
| 327 self.send_header('Cache-Control', 'max-age=60, no-store') | 336 self.send_header('Cache-Control', 'max-age=60, no-store') |
| 328 self.end_headers() | 337 self.end_headers() |
| 329 | 338 |
| 330 self.wfile.write('<html><head><title>%s</title></head></html>' % time.time()
) | 339 self.wfile.write('<html><head><title>%s</title></head></html>' % time.time()
) |
| 331 | 340 |
| 332 return True | 341 return True |
| 333 | 342 |
| 334 | 343 |
| 335 def CacheNoTransformHandler(self): | 344 def CacheNoTransformHandler(self): |
| 336 """This request handler yields a page with the title set to the current | 345 """This request handler yields a page with the title set to the current |
| 337 system time, and does not allow the content to transformed during | 346 system time, and does not allow the content to transformed during |
| 338 user-agent caching""" | 347 user-agent caching""" |
| 339 | 348 |
| 340 if (self.path.find("/cache/no-transform") != 0): | 349 if not self._ShouldHandleRequest("/cache/no-transform"): |
| 341 return False | 350 return False |
| 342 | 351 |
| 343 self.send_response(200) | 352 self.send_response(200) |
| 344 self.send_header('Content-type', 'text/html') | 353 self.send_header('Content-type', 'text/html') |
| 345 self.send_header('Cache-Control', 'no-transform') | 354 self.send_header('Cache-Control', 'no-transform') |
| 346 self.end_headers() | 355 self.end_headers() |
| 347 | 356 |
| 348 self.wfile.write('<html><head><title>%s</title></head></html>' % time.time()
) | 357 self.wfile.write('<html><head><title>%s</title></head></html>' % time.time()
) |
| 349 | 358 |
| 350 return True | 359 return True |
| 351 | 360 |
| 352 def EchoHeader(self): | 361 def EchoHeader(self): |
| 353 """This handler echoes back the value of a specific request header.""" | 362 """This handler echoes back the value of a specific request header.""" |
| 354 | 363 |
| 355 if self.path.find("/echoheader") != 0: | 364 if not self._ShouldHandleRequest("/echoheader"): |
| 356 return False | 365 return False |
| 357 | 366 |
| 358 query_char = self.path.find('?') | 367 query_char = self.path.find('?') |
| 359 if query_char != -1: | 368 if query_char != -1: |
| 360 header_name = self.path[query_char+1:] | 369 header_name = self.path[query_char+1:] |
| 361 | 370 |
| 362 self.send_response(200) | 371 self.send_response(200) |
| 363 self.send_header('Content-type', 'text/plain') | 372 self.send_header('Content-type', 'text/plain') |
| 364 self.send_header('Cache-control', 'max-age=60000') | 373 self.send_header('Cache-control', 'max-age=60000') |
| 365 # insert a vary header to properly indicate that the cachability of this | 374 # insert a vary header to properly indicate that the cachability of this |
| 366 # request is subject to value of the request header being echoed. | 375 # request is subject to value of the request header being echoed. |
| 367 if len(header_name) > 0: | 376 if len(header_name) > 0: |
| 368 self.send_header('Vary', header_name) | 377 self.send_header('Vary', header_name) |
| 369 self.end_headers() | 378 self.end_headers() |
| 370 | 379 |
| 371 if len(header_name) > 0: | 380 if len(header_name) > 0: |
| 372 self.wfile.write(self.headers.getheader(header_name)) | 381 self.wfile.write(self.headers.getheader(header_name)) |
| 373 | 382 |
| 374 return True | 383 return True |
| 375 | 384 |
| 376 def EchoHandler(self): | 385 def EchoHandler(self): |
| 377 """This handler just echoes back the payload of the request, for testing | 386 """This handler just echoes back the payload of the request, for testing |
| 378 form submission.""" | 387 form submission.""" |
| 379 | 388 |
| 380 if self.path.find("/echo") != 0: | 389 if not self._ShouldHandleRequest("/echo"): |
| 381 return False | 390 return False |
| 382 | 391 |
| 383 self.send_response(200) | 392 self.send_response(200) |
| 384 self.send_header('Content-type', 'text/html') | 393 self.send_header('Content-type', 'text/html') |
| 385 self.end_headers() | 394 self.end_headers() |
| 386 length = int(self.headers.getheader('content-length')) | 395 length = int(self.headers.getheader('content-length')) |
| 387 request = self.rfile.read(length) | 396 request = self.rfile.read(length) |
| 388 self.wfile.write(request) | 397 self.wfile.write(request) |
| 389 return True | 398 return True |
| 390 | 399 |
| 391 def EchoTitleHandler(self): | 400 def EchoTitleHandler(self): |
| 392 """This handler is like Echo, but sets the page title to the request.""" | 401 """This handler is like Echo, but sets the page title to the request.""" |
| 393 | 402 |
| 394 if self.path.find("/echotitle") != 0: | 403 if not self._ShouldHandleRequest("/echotitle"): |
| 395 return False | 404 return False |
| 396 | 405 |
| 397 self.send_response(200) | 406 self.send_response(200) |
| 398 self.send_header('Content-type', 'text/html') | 407 self.send_header('Content-type', 'text/html') |
| 399 self.end_headers() | 408 self.end_headers() |
| 400 length = int(self.headers.getheader('content-length')) | 409 length = int(self.headers.getheader('content-length')) |
| 401 request = self.rfile.read(length) | 410 request = self.rfile.read(length) |
| 402 self.wfile.write('<html><head><title>') | 411 self.wfile.write('<html><head><title>') |
| 403 self.wfile.write(request) | 412 self.wfile.write(request) |
| 404 self.wfile.write('</title></head></html>') | 413 self.wfile.write('</title></head></html>') |
| 405 return True | 414 return True |
| 406 | 415 |
| 407 def EchoAllHandler(self): | 416 def EchoAllHandler(self): |
| 408 """This handler yields a (more) human-readable page listing information | 417 """This handler yields a (more) human-readable page listing information |
| 409 about the request header & contents.""" | 418 about the request header & contents.""" |
| 410 | 419 |
| 411 if self.path.find("/echoall") != 0: | 420 if not self._ShouldHandleRequest("/echoall"): |
| 412 return False | 421 return False |
| 413 | 422 |
| 414 self.send_response(200) | 423 self.send_response(200) |
| 415 self.send_header('Content-type', 'text/html') | 424 self.send_header('Content-type', 'text/html') |
| 416 self.end_headers() | 425 self.end_headers() |
| 417 self.wfile.write('<html><head><style>' | 426 self.wfile.write('<html><head><style>' |
| 418 'pre { border: 1px solid black; margin: 5px; padding: 5px }' | 427 'pre { border: 1px solid black; margin: 5px; padding: 5px }' |
| 419 '</style></head><body>' | 428 '</style></head><body>' |
| 420 '<div style="float: right">' | 429 '<div style="float: right">' |
| 421 '<a href="http://localhost:8888/echo">back to referring page</a></div>' | 430 '<a href="http://localhost:8888/echo">back to referring page</a></div>' |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 474 while self.server.waitForDownload: | 483 while self.server.waitForDownload: |
| 475 self.server.handle_request() | 484 self.server.handle_request() |
| 476 | 485 |
| 477 # Second chunk of data: | 486 # Second chunk of data: |
| 478 self.wfile.write("*" * size_chunk2) | 487 self.wfile.write("*" * size_chunk2) |
| 479 return True | 488 return True |
| 480 | 489 |
| 481 def DownloadFinishHandler(self): | 490 def DownloadFinishHandler(self): |
| 482 """This handler just tells the server to finish the current download.""" | 491 """This handler just tells the server to finish the current download.""" |
| 483 | 492 |
| 484 if not self.path.startswith("/download-finish"): | 493 if not self._ShouldHandleRequest("/download-finish"): |
| 485 return False | 494 return False |
| 486 | 495 |
| 487 self.server.waitForDownload = False | 496 self.server.waitForDownload = False |
| 488 self.send_response(200) | 497 self.send_response(200) |
| 489 self.send_header('Content-type', 'text/html') | 498 self.send_header('Content-type', 'text/html') |
| 490 self.send_header('Cache-Control', 'max-age=0') | 499 self.send_header('Cache-Control', 'max-age=0') |
| 491 self.end_headers() | 500 self.end_headers() |
| 492 return True | 501 return True |
| 493 | 502 |
| 494 def FileHandler(self): | 503 def FileHandler(self): |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 609 self.wfile.write("you do not support bzip2 encoding") | 618 self.wfile.write("you do not support bzip2 encoding") |
| 610 except: | 619 except: |
| 611 self.send_error(404) | 620 self.send_error(404) |
| 612 | 621 |
| 613 return True | 622 return True |
| 614 | 623 |
| 615 def AuthBasicHandler(self): | 624 def AuthBasicHandler(self): |
| 616 """This handler tests 'Basic' authentication. It just sends a page with | 625 """This handler tests 'Basic' authentication. It just sends a page with |
| 617 title 'user/pass' if you succeed.""" | 626 title 'user/pass' if you succeed.""" |
| 618 | 627 |
| 619 if not self.path.startswith("/auth-basic"): | 628 if not self._ShouldHandleRequest("/auth-basic"): |
| 620 return False | 629 return False |
| 621 | 630 |
| 622 username = userpass = password = b64str = "" | 631 username = userpass = password = b64str = "" |
| 623 | 632 |
| 624 auth = self.headers.getheader('authorization') | 633 auth = self.headers.getheader('authorization') |
| 625 try: | 634 try: |
| 626 if not auth: | 635 if not auth: |
| 627 raise Exception('no auth') | 636 raise Exception('no auth') |
| 628 b64str = re.findall(r'Basic (\S+)', auth)[0] | 637 b64str = re.findall(r'Basic (\S+)', auth)[0] |
| 629 userpass = base64.b64decode(b64str) | 638 userpass = base64.b64decode(b64str) |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 665 self.wfile.write('</head><body>') | 674 self.wfile.write('</head><body>') |
| 666 self.wfile.write('auth=%s<p>' % auth) | 675 self.wfile.write('auth=%s<p>' % auth) |
| 667 self.wfile.write('</body></html>') | 676 self.wfile.write('</body></html>') |
| 668 | 677 |
| 669 return True | 678 return True |
| 670 | 679 |
| 671 def AuthDigestHandler(self): | 680 def AuthDigestHandler(self): |
| 672 """This handler tests 'Digest' authentication. It just sends a page with | 681 """This handler tests 'Digest' authentication. It just sends a page with |
| 673 title 'user/pass' if you succeed.""" | 682 title 'user/pass' if you succeed.""" |
| 674 | 683 |
| 675 if not self.path.startswith("/auth-digest"): | 684 if not self._ShouldHandleRequest("/auth-digest"): |
| 676 return False | 685 return False |
| 677 | 686 |
| 678 # Periodically generate a new nonce. Technically we should incorporate | 687 # Periodically generate a new nonce. Technically we should incorporate |
| 679 # the request URL into this, but we don't care for testing. | 688 # the request URL into this, but we don't care for testing. |
| 680 nonce_life = 10 | 689 nonce_life = 10 |
| 681 stale = False | 690 stale = False |
| 682 if not self.server.nonce or (time.time() - self.server.nonce_time > nonce_li
fe): | 691 if not self.server.nonce or (time.time() - self.server.nonce_time > nonce_li
fe): |
| 683 if self.server.nonce: | 692 if self.server.nonce: |
| 684 stale = True | 693 stale = True |
| 685 self.server.nonce_time = time.time() | 694 self.server.nonce_time = time.time() |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 753 self.wfile.write('</head><body>') | 762 self.wfile.write('</head><body>') |
| 754 self.wfile.write('auth=%s<p>' % auth) | 763 self.wfile.write('auth=%s<p>' % auth) |
| 755 self.wfile.write('pairs=%s<p>' % pairs) | 764 self.wfile.write('pairs=%s<p>' % pairs) |
| 756 self.wfile.write('</body></html>') | 765 self.wfile.write('</body></html>') |
| 757 | 766 |
| 758 return True | 767 return True |
| 759 | 768 |
| 760 def SlowServerHandler(self): | 769 def SlowServerHandler(self): |
| 761 """Wait for the user suggested time before responding. The syntax is | 770 """Wait for the user suggested time before responding. The syntax is |
| 762 /slow?0.5 to wait for half a second.""" | 771 /slow?0.5 to wait for half a second.""" |
| 763 if not self.path.startswith("/slow"): | 772 if not self._ShouldHandleRequest("/slow"): |
| 764 return False | 773 return False |
| 765 query_char = self.path.find('?') | 774 query_char = self.path.find('?') |
| 766 wait_sec = 1.0 | 775 wait_sec = 1.0 |
| 767 if query_char >= 0: | 776 if query_char >= 0: |
| 768 try: | 777 try: |
| 769 wait_sec = int(self.path[query_char + 1:]) | 778 wait_sec = int(self.path[query_char + 1:]) |
| 770 except ValueError: | 779 except ValueError: |
| 771 pass | 780 pass |
| 772 time.sleep(wait_sec) | 781 time.sleep(wait_sec) |
| 773 self.send_response(200) | 782 self.send_response(200) |
| 774 self.send_header('Content-type', 'text/plain') | 783 self.send_header('Content-type', 'text/plain') |
| 775 self.end_headers() | 784 self.end_headers() |
| 776 self.wfile.write("waited %d seconds" % wait_sec) | 785 self.wfile.write("waited %d seconds" % wait_sec) |
| 777 return True | 786 return True |
| 778 | 787 |
| 779 def ContentTypeHandler(self): | 788 def ContentTypeHandler(self): |
| 780 """Returns a string of html with the given content type. E.g., | 789 """Returns a string of html with the given content type. E.g., |
| 781 /contenttype?text/css returns an html file with the Content-Type | 790 /contenttype?text/css returns an html file with the Content-Type |
| 782 header set to text/css.""" | 791 header set to text/css.""" |
| 783 if not self.path.startswith('/contenttype'): | 792 if not self._ShouldHandleRequest("/contenttype"): |
| 784 return False | 793 return False |
| 785 query_char = self.path.find('?') | 794 query_char = self.path.find('?') |
| 786 content_type = self.path[query_char + 1:].strip() | 795 content_type = self.path[query_char + 1:].strip() |
| 787 if not content_type: | 796 if not content_type: |
| 788 content_type = 'text/html' | 797 content_type = 'text/html' |
| 789 self.send_response(200) | 798 self.send_response(200) |
| 790 self.send_header('Content-Type', content_type) | 799 self.send_header('Content-Type', content_type) |
| 791 self.end_headers() | 800 self.end_headers() |
| 792 self.wfile.write("<html>\n<body>\n<p>HTML text</p>\n</body>\n</html>\n"); | 801 self.wfile.write("<html>\n<body>\n<p>HTML text</p>\n</body>\n</html>\n"); |
| 793 return True | 802 return True |
| 794 | 803 |
| 795 def ServerRedirectHandler(self): | 804 def ServerRedirectHandler(self): |
| 796 """Sends a server redirect to the given URL. The syntax is | 805 """Sends a server redirect to the given URL. The syntax is |
| 797 '/server-redirect?http://foo.bar/asdf' to redirect to 'http://foo.bar/asdf'"
"" | 806 '/server-redirect?http://foo.bar/asdf' to redirect to 'http://foo.bar/asdf'"
"" |
| 798 | 807 |
| 799 test_name = "/server-redirect" | 808 test_name = "/server-redirect" |
| 800 if not self.path.startswith(test_name): | 809 if not self._ShouldHandleRequest(test_name): |
| 801 return False | 810 return False |
| 802 | 811 |
| 803 query_char = self.path.find('?') | 812 query_char = self.path.find('?') |
| 804 if query_char < 0 or len(self.path) <= query_char + 1: | 813 if query_char < 0 or len(self.path) <= query_char + 1: |
| 805 self.sendRedirectHelp(test_name) | 814 self.sendRedirectHelp(test_name) |
| 806 return True | 815 return True |
| 807 dest = self.path[query_char + 1:] | 816 dest = self.path[query_char + 1:] |
| 808 | 817 |
| 809 self.send_response(301) # moved permanently | 818 self.send_response(301) # moved permanently |
| 810 self.send_header('Location', dest) | 819 self.send_header('Location', dest) |
| 811 self.send_header('Content-type', 'text/html') | 820 self.send_header('Content-type', 'text/html') |
| 812 self.end_headers() | 821 self.end_headers() |
| 813 self.wfile.write('<html><head>') | 822 self.wfile.write('<html><head>') |
| 814 self.wfile.write('</head><body>Redirecting to %s</body></html>' % dest) | 823 self.wfile.write('</head><body>Redirecting to %s</body></html>' % dest) |
| 815 | 824 |
| 816 return True; | 825 return True; |
| 817 | 826 |
| 818 def ClientRedirectHandler(self): | 827 def ClientRedirectHandler(self): |
| 819 """Sends a client redirect to the given URL. The syntax is | 828 """Sends a client redirect to the given URL. The syntax is |
| 820 '/client-redirect?http://foo.bar/asdf' to redirect to 'http://foo.bar/asdf'"
"" | 829 '/client-redirect?http://foo.bar/asdf' to redirect to 'http://foo.bar/asdf'"
"" |
| 821 | 830 |
| 822 test_name = "/client-redirect" | 831 test_name = "/client-redirect" |
| 823 if not self.path.startswith(test_name): | 832 if not self._ShouldHandleRequest(test_name): |
| 824 return False | 833 return False |
| 825 | 834 |
| 826 query_char = self.path.find('?'); | 835 query_char = self.path.find('?'); |
| 827 if query_char < 0 or len(self.path) <= query_char + 1: | 836 if query_char < 0 or len(self.path) <= query_char + 1: |
| 828 self.sendRedirectHelp(test_name) | 837 self.sendRedirectHelp(test_name) |
| 829 return True | 838 return True |
| 830 dest = self.path[query_char + 1:] | 839 dest = self.path[query_char + 1:] |
| 831 | 840 |
| 832 self.send_response(200) | 841 self.send_response(200) |
| 833 self.send_header('Content-type', 'text/html') | 842 self.send_header('Content-type', 'text/html') |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 913 option_parser.add_option('', '--data-dir', dest='data_dir', | 922 option_parser.add_option('', '--data-dir', dest='data_dir', |
| 914 help='Directory from which to read the files') | 923 help='Directory from which to read the files') |
| 915 option_parser.add_option('', '--https', dest='cert', | 924 option_parser.add_option('', '--https', dest='cert', |
| 916 help='Specify that https should be used, specify ' | 925 help='Specify that https should be used, specify ' |
| 917 'the path to the cert containing the private key ' | 926 'the path to the cert containing the private key ' |
| 918 'the server should use') | 927 'the server should use') |
| 919 options, args = option_parser.parse_args() | 928 options, args = option_parser.parse_args() |
| 920 | 929 |
| 921 sys.exit(main(options, args)) | 930 sys.exit(main(options, args)) |
| 922 | 931 |
| OLD | NEW |