Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(111)

Side by Side Diff: net/tools/testserver/testserver.py

Issue 7978043: Sending auth error from python server. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: For review. Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/python2.4 1 #!/usr/bin/python2.4
2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2011 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/FTP/SYNC/TCP/UDP/ server used for testing Chrome. 6 """This is a simple HTTP/FTP/SYNC/TCP/UDP/ 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 By default, it listens on an ephemeral port and sends the port number back to 9 By default, it listens on an ephemeral port and sends the port number back to
10 the originating process over a pipe. The originating process can specify an 10 the originating process over a pipe. The originating process can specify an
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 # We import here to avoid pulling in chromiumsync's dependencies 125 # We import here to avoid pulling in chromiumsync's dependencies
126 # unless strictly necessary. 126 # unless strictly necessary.
127 import chromiumsync 127 import chromiumsync
128 import xmppserver 128 import xmppserver
129 StoppableHTTPServer.__init__(self, server_address, request_handler_class) 129 StoppableHTTPServer.__init__(self, server_address, request_handler_class)
130 self._sync_handler = chromiumsync.TestServer() 130 self._sync_handler = chromiumsync.TestServer()
131 self._xmpp_socket_map = {} 131 self._xmpp_socket_map = {}
132 self._xmpp_server = xmppserver.XmppServer( 132 self._xmpp_server = xmppserver.XmppServer(
133 self._xmpp_socket_map, ('localhost', 0)) 133 self._xmpp_socket_map, ('localhost', 0))
134 self.xmpp_port = self._xmpp_server.getsockname()[1] 134 self.xmpp_port = self._xmpp_server.getsockname()[1]
135 self.authenticated = True
135 136
136 def GetXmppServer(self): 137 def GetXmppServer(self):
137 return self._xmpp_server 138 return self._xmpp_server
138 139
139 def HandleCommand(self, query, raw_request): 140 def HandleCommand(self, query, raw_request):
140 return self._sync_handler.HandleCommand(query, raw_request) 141 return self._sync_handler.HandleCommand(query, raw_request)
141 142
142 def HandleRequestNoBlock(self): 143 def HandleRequestNoBlock(self):
143 """Handles a single request. 144 """Handles a single request.
144 145
145 Copied from SocketServer._handle_request_noblock(). 146 Copied from SocketServer._handle_request_noblock().
146 """ 147 """
147 try: 148 try:
148 request, client_address = self.get_request() 149 request, client_address = self.get_request()
149 except socket.error: 150 except socket.error:
150 return 151 return
151 if self.verify_request(request, client_address): 152 if self.verify_request(request, client_address):
152 try: 153 try:
153 self.process_request(request, client_address) 154 self.process_request(request, client_address)
154 except: 155 except:
155 self.handle_error(request, client_address) 156 self.handle_error(request, client_address)
156 self.close_request(request) 157 self.close_request(request)
157 158
159 def SetAuthenticated(self, auth_valid):
160 self.authenticated = auth_valid
161
162 def GetAuthenticated(self):
163 return self.authenticated
164
158 def serve_forever(self): 165 def serve_forever(self):
159 """This is a merge of asyncore.loop() and SocketServer.serve_forever(). 166 """This is a merge of asyncore.loop() and SocketServer.serve_forever().
160 """ 167 """
161 168
162 def HandleXmppSocket(fd, socket_map, handler): 169 def HandleXmppSocket(fd, socket_map, handler):
163 """Runs the handler for the xmpp connection for fd. 170 """Runs the handler for the xmpp connection for fd.
164 171
165 Adapted from asyncore.read() et al. 172 Adapted from asyncore.read() et al.
166 """ 173 """
167 xmpp_connection = socket_map.get(fd) 174 xmpp_connection = socket_map.get(fd)
(...skipping 1239 matching lines...) Expand 10 before | Expand all | Expand 10 after
1407 """Handler for the main HTTP sync server.""" 1414 """Handler for the main HTTP sync server."""
1408 1415
1409 def __init__(self, request, client_address, sync_http_server): 1416 def __init__(self, request, client_address, sync_http_server):
1410 get_handlers = [self.ChromiumSyncMigrationOpHandler, 1417 get_handlers = [self.ChromiumSyncMigrationOpHandler,
1411 self.ChromiumSyncTimeHandler, 1418 self.ChromiumSyncTimeHandler,
1412 self.ChromiumSyncDisableNotificationsOpHandler, 1419 self.ChromiumSyncDisableNotificationsOpHandler,
1413 self.ChromiumSyncEnableNotificationsOpHandler, 1420 self.ChromiumSyncEnableNotificationsOpHandler,
1414 self.ChromiumSyncSendNotificationOpHandler, 1421 self.ChromiumSyncSendNotificationOpHandler,
1415 self.ChromiumSyncBirthdayErrorOpHandler, 1422 self.ChromiumSyncBirthdayErrorOpHandler,
1416 self.ChromiumSyncTransientErrorOpHandler, 1423 self.ChromiumSyncTransientErrorOpHandler,
1417 self.ChromiumSyncSyncTabsOpHandler] 1424 self.ChromiumSyncSyncTabsOpHandler,
1425 self.ChromiumSyncCredHandler]
1418 1426
1419 post_handlers = [self.ChromiumSyncCommandHandler, 1427 post_handlers = [self.ChromiumSyncCommandHandler,
1420 self.ChromiumSyncTimeHandler] 1428 self.ChromiumSyncTimeHandler]
1421 BasePageHandler.__init__(self, request, client_address, 1429 BasePageHandler.__init__(self, request, client_address,
1422 sync_http_server, [], get_handlers, 1430 sync_http_server, [], get_handlers,
1423 post_handlers, []) 1431 post_handlers, [])
1424 1432
1433
1425 def ChromiumSyncTimeHandler(self): 1434 def ChromiumSyncTimeHandler(self):
1426 """Handle Chromium sync .../time requests. 1435 """Handle Chromium sync .../time requests.
1427 1436
1428 The syncer sometimes checks server reachability by examining /time. 1437 The syncer sometimes checks server reachability by examining /time.
1429 """ 1438 """
1430 test_name = "/chromiumsync/time" 1439 test_name = "/chromiumsync/time"
1431 if not self._ShouldHandleRequest(test_name): 1440 if not self._ShouldHandleRequest(test_name):
1432 return False 1441 return False
1433 1442
1434 # Chrome hates it if we send a response before reading the request. 1443 # Chrome hates it if we send a response before reading the request.
(...skipping 12 matching lines...) Expand all
1447 1456
1448 This covers all sync protocol commands: authentication, getupdates, and 1457 This covers all sync protocol commands: authentication, getupdates, and
1449 commit. 1458 commit.
1450 """ 1459 """
1451 test_name = "/chromiumsync/command" 1460 test_name = "/chromiumsync/command"
1452 if not self._ShouldHandleRequest(test_name): 1461 if not self._ShouldHandleRequest(test_name):
1453 return False 1462 return False
1454 1463
1455 length = int(self.headers.getheader('content-length')) 1464 length = int(self.headers.getheader('content-length'))
1456 raw_request = self.rfile.read(length) 1465 raw_request = self.rfile.read(length)
1466 http_response = 200
1467 raw_reply = None
1468 if not self.server.GetAuthenticated():
1469 http_response = 401
1470 challenge = 'GoogleLogin realm="http://127.0.0.1", service="chromiumsync"'
1471 else:
1472 http_response, raw_reply = self.server.HandleCommand(
1473 self.path, raw_request)
1457 1474
1458 http_response, raw_reply = self.server.HandleCommand( 1475 ### Now send the response to the client. ###
1459 self.path, raw_request)
1460 self.send_response(http_response) 1476 self.send_response(http_response)
1477 if http_response == 401:
1478 self.send_header('www-Authenticate', challenge)
1461 self.end_headers() 1479 self.end_headers()
1462 self.wfile.write(raw_reply) 1480 self.wfile.write(raw_reply)
1463 return True 1481 return True
1464 1482
1465 def ChromiumSyncMigrationOpHandler(self): 1483 def ChromiumSyncMigrationOpHandler(self):
1466 test_name = "/chromiumsync/migrate" 1484 test_name = "/chromiumsync/migrate"
1467 if not self._ShouldHandleRequest(test_name): 1485 if not self._ShouldHandleRequest(test_name):
1468 return False 1486 return False
1469 1487
1470 http_response, raw_reply = self.server._sync_handler.HandleMigrate( 1488 http_response, raw_reply = self.server._sync_handler.HandleMigrate(
1471 self.path) 1489 self.path)
1472 self.send_response(http_response) 1490 self.send_response(http_response)
1473 self.send_header('Content-Type', 'text/html') 1491 self.send_header('Content-Type', 'text/html')
1474 self.send_header('Content-Length', len(raw_reply)) 1492 self.send_header('Content-Length', len(raw_reply))
1475 self.end_headers() 1493 self.end_headers()
1476 self.wfile.write(raw_reply) 1494 self.wfile.write(raw_reply)
1477 return True 1495 return True
1478 1496
1497 def ChromiumSyncCredHandler(self):
1498 test_name = "/chromiumsync/cred"
1499 if not self._ShouldHandleRequest(test_name):
1500 return False
1501 try:
1502 query = urlparse.urlparse(self.path)[4]
1503 cred_valid = urlparse.parse_qs(query)['valid']
1504 if cred_valid[0] == 'True':
1505 self.server.SetAuthenticated(True)
1506 else:
1507 self.server.SetAuthenticated(False)
1508 except:
1509 self.server.SetAuthenticated(False)
1510
1511 http_response = 200
1512 raw_reply = 'Authenicated: %s ' % self.server.GetAuthenticated()
ncarter (slow) 2011/09/22 20:22:19 "Authenicated" -> misspelled.
1513 self.send_response(http_response)
1514 self.send_header('Content-Type', 'text/html')
1515 self.send_header('Content-Length', len(raw_reply))
1516 self.end_headers()
1517 self.wfile.write(raw_reply)
1518 return True
1519
1479 def ChromiumSyncDisableNotificationsOpHandler(self): 1520 def ChromiumSyncDisableNotificationsOpHandler(self):
1480 test_name = "/chromiumsync/disablenotifications" 1521 test_name = "/chromiumsync/disablenotifications"
1481 if not self._ShouldHandleRequest(test_name): 1522 if not self._ShouldHandleRequest(test_name):
1482 return False 1523 return False
1483 self.server.GetXmppServer().DisableNotifications() 1524 self.server.GetXmppServer().DisableNotifications()
1484 result = 200 1525 result = 200
1485 raw_reply = ('<html><title>Notifications disabled</title>' 1526 raw_reply = ('<html><title>Notifications disabled</title>'
1486 '<H1>Notifications disabled</H1></html>') 1527 '<H1>Notifications disabled</H1></html>')
1487 self.send_response(result) 1528 self.send_response(result)
1488 self.send_header('Content-Type', 'text/html') 1529 self.send_header('Content-Type', 'text/html')
(...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after
1825 'random key if none is specified on the command ' 1866 'random key if none is specified on the command '
1826 'line.') 1867 'line.')
1827 option_parser.add_option('', '--policy-user', default='user@example.com', 1868 option_parser.add_option('', '--policy-user', default='user@example.com',
1828 dest='policy_user', 1869 dest='policy_user',
1829 help='Specify the user name the server should ' 1870 help='Specify the user name the server should '
1830 'report back to the client as the user owning the ' 1871 'report back to the client as the user owning the '
1831 'token used for making the policy request.') 1872 'token used for making the policy request.')
1832 options, args = option_parser.parse_args() 1873 options, args = option_parser.parse_args()
1833 1874
1834 sys.exit(main(options, args)) 1875 sys.exit(main(options, args))
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698