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

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: Upload before commit. Created 9 years, 3 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 1240 matching lines...) Expand 10 before | Expand all | Expand 10 after
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,
1418 self.ChromiumSyncErrorOpHandler] 1425 self.ChromiumSyncErrorOpHandler,
1426 self.ChromiumSyncCredHandler]
1419 1427
1420 post_handlers = [self.ChromiumSyncCommandHandler, 1428 post_handlers = [self.ChromiumSyncCommandHandler,
1421 self.ChromiumSyncTimeHandler] 1429 self.ChromiumSyncTimeHandler]
1422 BasePageHandler.__init__(self, request, client_address, 1430 BasePageHandler.__init__(self, request, client_address,
1423 sync_http_server, [], get_handlers, 1431 sync_http_server, [], get_handlers,
1424 post_handlers, []) 1432 post_handlers, [])
1425 1433
1434
1426 def ChromiumSyncTimeHandler(self): 1435 def ChromiumSyncTimeHandler(self):
1427 """Handle Chromium sync .../time requests. 1436 """Handle Chromium sync .../time requests.
1428 1437
1429 The syncer sometimes checks server reachability by examining /time. 1438 The syncer sometimes checks server reachability by examining /time.
1430 """ 1439 """
1431 test_name = "/chromiumsync/time" 1440 test_name = "/chromiumsync/time"
1432 if not self._ShouldHandleRequest(test_name): 1441 if not self._ShouldHandleRequest(test_name):
1433 return False 1442 return False
1434 1443
1435 # Chrome hates it if we send a response before reading the request. 1444 # Chrome hates it if we send a response before reading the request.
(...skipping 12 matching lines...) Expand all
1448 1457
1449 This covers all sync protocol commands: authentication, getupdates, and 1458 This covers all sync protocol commands: authentication, getupdates, and
1450 commit. 1459 commit.
1451 """ 1460 """
1452 test_name = "/chromiumsync/command" 1461 test_name = "/chromiumsync/command"
1453 if not self._ShouldHandleRequest(test_name): 1462 if not self._ShouldHandleRequest(test_name):
1454 return False 1463 return False
1455 1464
1456 length = int(self.headers.getheader('content-length')) 1465 length = int(self.headers.getheader('content-length'))
1457 raw_request = self.rfile.read(length) 1466 raw_request = self.rfile.read(length)
1467 http_response = 200
1468 raw_reply = None
1469 if not self.server.GetAuthenticated():
1470 http_response = 401
1471 challenge = 'GoogleLogin realm="http://127.0.0.1", service="chromiumsync"'
1472 else:
1473 http_response, raw_reply = self.server.HandleCommand(
1474 self.path, raw_request)
1458 1475
1459 http_response, raw_reply = self.server.HandleCommand( 1476 ### Now send the response to the client. ###
1460 self.path, raw_request)
1461 self.send_response(http_response) 1477 self.send_response(http_response)
1478 if http_response == 401:
1479 self.send_header('www-Authenticate', challenge)
1462 self.end_headers() 1480 self.end_headers()
1463 self.wfile.write(raw_reply) 1481 self.wfile.write(raw_reply)
1464 return True 1482 return True
1465 1483
1466 def ChromiumSyncMigrationOpHandler(self): 1484 def ChromiumSyncMigrationOpHandler(self):
1467 test_name = "/chromiumsync/migrate" 1485 test_name = "/chromiumsync/migrate"
1468 if not self._ShouldHandleRequest(test_name): 1486 if not self._ShouldHandleRequest(test_name):
1469 return False 1487 return False
1470 1488
1471 http_response, raw_reply = self.server._sync_handler.HandleMigrate( 1489 http_response, raw_reply = self.server._sync_handler.HandleMigrate(
1472 self.path) 1490 self.path)
1473 self.send_response(http_response) 1491 self.send_response(http_response)
1474 self.send_header('Content-Type', 'text/html') 1492 self.send_header('Content-Type', 'text/html')
1475 self.send_header('Content-Length', len(raw_reply)) 1493 self.send_header('Content-Length', len(raw_reply))
1476 self.end_headers() 1494 self.end_headers()
1477 self.wfile.write(raw_reply) 1495 self.wfile.write(raw_reply)
1478 return True 1496 return True
1479 1497
1498 def ChromiumSyncCredHandler(self):
1499 test_name = "/chromiumsync/cred"
1500 if not self._ShouldHandleRequest(test_name):
1501 return False
1502 try:
1503 query = urlparse.urlparse(self.path)[4]
1504 cred_valid = urlparse.parse_qs(query)['valid']
1505 if cred_valid[0] == 'True':
1506 self.server.SetAuthenticated(True)
1507 else:
1508 self.server.SetAuthenticated(False)
1509 except:
1510 self.server.SetAuthenticated(False)
1511
1512 http_response = 200
1513 raw_reply = 'Authenticated: %s ' % self.server.GetAuthenticated()
1514 self.send_response(http_response)
1515 self.send_header('Content-Type', 'text/html')
1516 self.send_header('Content-Length', len(raw_reply))
1517 self.end_headers()
1518 self.wfile.write(raw_reply)
1519 return True
1520
1480 def ChromiumSyncDisableNotificationsOpHandler(self): 1521 def ChromiumSyncDisableNotificationsOpHandler(self):
1481 test_name = "/chromiumsync/disablenotifications" 1522 test_name = "/chromiumsync/disablenotifications"
1482 if not self._ShouldHandleRequest(test_name): 1523 if not self._ShouldHandleRequest(test_name):
1483 return False 1524 return False
1484 self.server.GetXmppServer().DisableNotifications() 1525 self.server.GetXmppServer().DisableNotifications()
1485 result = 200 1526 result = 200
1486 raw_reply = ('<html><title>Notifications disabled</title>' 1527 raw_reply = ('<html><title>Notifications disabled</title>'
1487 '<H1>Notifications disabled</H1></html>') 1528 '<H1>Notifications disabled</H1></html>')
1488 self.send_response(result) 1529 self.send_response(result)
1489 self.send_header('Content-Type', 'text/html') 1530 self.send_header('Content-Type', 'text/html')
(...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after
1839 'random key if none is specified on the command ' 1880 'random key if none is specified on the command '
1840 'line.') 1881 'line.')
1841 option_parser.add_option('', '--policy-user', default='user@example.com', 1882 option_parser.add_option('', '--policy-user', default='user@example.com',
1842 dest='policy_user', 1883 dest='policy_user',
1843 help='Specify the user name the server should ' 1884 help='Specify the user name the server should '
1844 'report back to the client as the user owning the ' 1885 'report back to the client as the user owning the '
1845 'token used for making the policy request.') 1886 'token used for making the policy request.')
1846 options, args = option_parser.parse_args() 1887 options, args = option_parser.parse_args()
1847 1888
1848 sys.exit(main(options, args)) 1889 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