forked from chromium/web-page-replay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
replayspdyserver.py
executable file
·125 lines (113 loc) · 4.08 KB
/
replayspdyserver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python
# Copyright 2010 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import daemonserver
import logging
import httparchive
import os
import sys
import threading
import time
import third_party
from nbhttp import spdy_server
from nbhttp import push_tcp
from nbhttp import http_common
CONTENT_LENGTH = 'content-length'
STATUS = 'status'
VERSION = 'version'
class ReplaySpdyServer(daemonserver.DaemonServer):
def __init__(self, http_archive_fetch, custom_handlers,
host='localhost', port=80, certfile=None, keyfile=None):
"""Initialize ReplaySpdyServer.
The private key may be stored in |certfile|. If so, |keyfile|
may be left unset.
"""
#TODO(lzheng): figure out how to get the log level from main.
self.log = logging.getLogger('ReplaySpdyServer')
self.log.setLevel(logging.INFO)
self.http_archive_fetch = http_archive_fetch
self.custom_handlers = custom_handlers
self.host = host
self.port = port
self.use_ssl = certfile is not None
self.spdy_server = spdy_server.SpdyServer(
host, port, self.use_ssl, certfile, keyfile, self.request_handler,
self.log)
def serve_forever(self):
self.log.info('Replaying with SPDY on %s:%d', self.host, self.port)
push_tcp.run()
def cleanup(self):
push_tcp.stop()
self.log.info('Stopped spdy server')
def request_handler(self, method, uri, hdrs, res_start, req_pause):
"""
Based on method, host and uri to fetch the matching response and reply
to browser using spdy.
"""
dummy = http_common.dummy
def simple_responder(code, content):
res_hdrs = [('content-type', 'text/html'), ('version', 'HTTP/1.1')]
res_body, res_done = res_start(str(code), content, res_hdrs, dummy)
res_body(None)
res_done(None)
host = ''
for name, value in hdrs:
if name.lower() == 'host':
host = value
self.log.debug("request: %s, uri: %s, method: %s", host, uri, method)
if method == 'GET':
request = httparchive.ArchivedHttpRequest(
method, host, uri, None, dict(hdrs))
response_code = self.custom_handlers.handle(request)
if response_code:
simple_responder(response_code, "Handled by custom handlers")
return dummy, dummy
response = self.http_archive_fetch(request)
if response:
res_hdrs = [('version', 'HTTP/1.1')]
for name, value in response.headers:
name_lower = name.lower()
if name_lower == CONTENT_LENGTH:
res_hdrs.append((name, str(value)))
elif name_lower in (STATUS, VERSION):
pass
else:
res_hdrs.append((name_lower, value))
res_body, res_done = res_start(
str(response.status), response.reason, res_hdrs, dummy)
body = ''
for item in response.response_data:
res_body(item)
res_done(None)
else:
self.log.error("404 returned: %s %s", method, uri)
simple_responder(404, "file not found")
else:
# TODO(lzheng): Add support for other methods.
self.log.error("method: %s is not supported: %s", method, uri)
simple_responder(500, "Not supported")
return dummy, dummy
if __name__ == "__main__":
logging.basicConfig()
log = logging.getLogger('server')
log.setLevel(logging.INFO)
filename = sys.argv[1]
host = '127.0.0.1'
port = 8088
server = ReplaySpdyServer(filename, host, port)
server_thread = threading.Thread(target=server.serve_forever)
server_thread.setDaemon(True)
server_thread.start()
time.sleep(60)
server.cleanup();