public
Last active

test_http_conns.py - Tests for how clients handle HTTP pconns and pipelining

  • Download Gist
test_http_conns.py
Python
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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
#!/usr/bin/env python
 
"""
Tests for how persistent connections are handled, including
pipelining.
 
This script will set up a server which summarises how a browser connecting
to it behaves. For example, FireFox with pipelining turned on will give:
 
--->8---
 
~> ./test_http_conns.py 8001
PID: 11458
Point your browser at port 8001.
request-uri: /
request-uri: /image?1
[...]
request-uri: /image?27
^C
Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-GB; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8
conn 1 - 5 HTTP/1.1 requests, then idle 2 seconds (pipeline held at most 3)
conn 2 - 5 HTTP/1.1 requests, then idle 2 seconds (pipeline held at most 3)
conn 3 - 6 HTTP/1.1 requests, then idle 2 seconds (pipeline held at most 3)
conn 4 - 5 HTTP/1.1 requests, then idle 2 seconds (pipeline held at most 3)
conn 5 - 5 HTTP/1.1 requests, then idle 2 seconds (pipeline held at most 3)
conn 6 - 5 HTTP/1.1 requests, then idle 2 seconds (pipeline held at most 3)
 
---8<---
 
Requires:
* Twisted - <http://twistedmatrix.com/>
"""
 
__author__ = "Mark Nottingham <mnot@mnot.net>"
__copyright__ = """\
Copyright (c) 2008 Mark Nottingham
 
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
 
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
 
 
import re
import time
from urllib import urlopen
from twisted.internet import protocol
from twisted.protocols import basic
 
try:
from twisted.internet import epollreactor
epollreactor.install()
except ImportError:
pass
 
from twisted.internet import reactor
 
# Where to get the image from.
image_url = "http://www.mnot.net/photo/2006/06/edinburgh/Thumbs/_DSC0029.jpg"
 
# How long to delay before processing requests; useful if you're testing
# on the same host and the client doesn't get a chance to pipeline.
delay = 0.1
 
CRLF = re.compile(r"\r?\n", re.M)
MSG_DELIMITER = re.compile(r"\r?\n\r?\n", re.M)
 
class ConnectionServer(basic.LineReceiver):
""" assume GET for now. """
line_mode = 0
connections = {}
conn_count = 0
responses = {
'/': (200, "OK", "OK", "text/plain"),
'/error/404': (404, "Not Found", "sorry, not found", "text/plain"),
'/error/500': (500, "Internal Server Error", "sorry", "text/plain"),
'/image': (200, "OK", urlopen(image_url).read(), "image/jpeg"),
}
def __init__(self, root_body=None, root_type="text/html"):
if root_body:
self.responses['/'] = (200, "OK", root_body, root_type)
self.input = ""
self.request_queue = []
self.id = id(self)
 
def rawDataReceived(self, data):
self.input += data
if len(re.findall(MSG_DELIMITER, self.input)) > 0:
requests = filter(None, re.split(MSG_DELIMITER, self.input))
self.input = ""
for request in requests:
self.request_queue.append(request)
reactor.callLater(delay, self.process_request)
def process_request(self):
inp = self.request_queue.pop()
conn_data = ConnectionServer.connections[self.id]
conn_data['max_queue'] = max(
len(self.request_queue), conn_data['max_queue']
)
request_lines = re.split(CRLF, inp)
if conn_data['request_counter'] == 0:
for line in request_lines:
if line[:11].lower() == "user-agent:":
conn_data['user_agent'] = line[11:].strip()
break
request_line = request_lines[0]
request_method, _rest = request_line.split(None, 1)
request_uri, http_version = _rest.rsplit(None, 1)
conn_data['http_version'] = http_version
sys.stderr.write(" request-uri: %s\n" % request_uri)
request_path = request_uri.split("?", 1)[0]
conn_data['request_counter'] += 1
status_code, status_phrase, response_body, content_type = \
self.responses.get(request_path, (200, "OK", "default", 'text/plain'))
self.sendLine("HTTP/1.1 %s %s" % (status_code, status_phrase))
self.sendLine("Content-Type: %s" % content_type)
self.sendLine("Connection: keep-alive")
self.sendLine("Content-Length: %s" % len(response_body))
# http://lxr.mozilla.org/mozilla/source/netwerk/protocol/http/src/nsHttpConnection.cpp#247
self.sendLine("Server: Apache/2.4")
self.sendLine("")
self.transport.write(response_body)
conn_data['start_time'] = time.time()
def connectionMade(self):
ConnectionServer.conn_count += 1
try:
assert(not ConnectionServer.connections.has_key(self.id))
except AssertionError:
sys.stderr.write("WTF! %s\n" % self.id)
ConnectionServer.conn_count -= 1
self.transport.loseConnection()
ConnectionServer.connections[self.id] = {
'request_counter': 0,
'start_time': time.time(),
'stop_time': 0,
'max_queue': 0,
'user_agent': "",
'http_version': "",
}
def connectionLost(self, reason):
ConnectionServer.connections[self.id]['stop_time'] = time.time()
ConnectionServer.conn_count -= 1
reactor.callLater(1, self.checkDone)
 
def checkDone(self):
if ConnectionServer.conn_count == 0:
sys.stderr.write("All connections closed.\n")
try:
reactor.stop()
except RuntimeError:
pass
 
class ConnectionServerFactory(protocol.ServerFactory):
protocol = ConnectionServer
 
 
if __name__ == "__main__":
import sys, os
root_body = """\
<html>
<head>
</head>
<body>
<img src="/image?1"/>
<img src="/image?2"/>
<img src="/image?3"/>
<img src="/image?4"/>
<img src="/image?5"/>
<img src="/image?6"/>
<img src="/image?7"/>
<img src="/image?8"/>
<img src="/image?9"/>
<img src="/image?10"/>
<img src="/image?11"/>
<img src="/image?12"/>
<img src="/image?13"/>
<img src="/image?14"/>
<img src="/image?15"/>
<img src="/image?16"/>
<img src="/image?17"/>
<img src="/image?18"/>
<img src="/image?19"/>
<img src="/image?20"/>
<img src="/image?21"/>
<img src="/image?22"/>
<img src="/image?23"/>
<img src="/image?24"/>
<img src="/image?25"/>
<img src="/image?26"/>
<img src="/image?27"/>
<img src="/image?28"/>
<img src="/image?29"/>
<img src="/image?30"/>
</body>
</html>"""
if not len(sys.argv) == 2:
print "Usage: %s port" % sys.argv[0]
sys.exit(1)
port = int(sys.argv[1])
ConnectionServer.responses['/'] = (200, "OK", root_body, "text/html")
sys.stderr.write("PID: %s\n" % os.getpid())
sys.stderr.write("Point your browser at port %i.\n" % port)
reactor.listenTCP(port, ConnectionServerFactory())
try:
reactor.run()
finally:
print
conns = ConnectionServer.connections.items()
i = 0
print ConnectionServer.connections.values()[0]['user_agent']
for cid, conn in conns:
i += 1
pipelining = ""
if conn['max_queue'] > 0:
pipelining = "(pipeline held at most %s)" % conn['max_queue']
print "connection %i - %i %s requests, then idle %i seconds %s" % (
i,
conn['request_counter'],
conn['http_version'],
(conn['stop_time'] - conn['start_time']),
pipelining
)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.