Skip to content

Commit d54907d

Browse files
committed
Improving keep-alive support
1 parent c8ccc31 commit d54907d

File tree

3 files changed

+48
-38
lines changed

3 files changed

+48
-38
lines changed

data/txt/sha256sums.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ c1cb56f2a43e9f2f6b25d5f3d504e856ea21df6fc14af5e37b1000feef2bdb5a lib/core/optio
188188
48797d6c34dd9bb8a53f7f3794c85f4288d82a9a1d6be7fcf317d388cb20d4b3 lib/core/replication.py
189189
0b8c38a01bb01f843d94a6c5f2075ee47520d0c4aa799cecea9c3e2c5a4a23a6 lib/core/revision.py
190190
888daba83fd4a34e9503fe21f01fef4cc730e5cde871b1d40e15d4cbc847d56c lib/core/session.py
191-
1418691b5449412e60c693b6afc2f12b00051c1e280d2261762a36f094e0da66 lib/core/settings.py
191+
bf818add365e18e378b15fb33db123d846acddc2969e05af52eacfe745cc335e lib/core/settings.py
192192
cd5a66deee8963ba8e7e9af3dd36eb5e8127d4d68698811c29e789655f507f82 lib/core/shell.py
193193
bcb5d8090d5e3e0ef2a586ba09ba80eef0c6d51feb0f611ed25299fbb254f725 lib/core/subprocessng.py
194194
d35650179816193164a5f177102f18379dfbe6bb6d40fbb67b78d907b41c8038 lib/core/target.py
@@ -618,7 +618,7 @@ edf23e7105539d700a1ae1bc52436e57e019b345a7d0227e4d85b6353ef535fa thirdparty/ide
618618
d846fdc47a11a58da9e463a948200f69265181f3dbc38148bfe4141fade10347 thirdparty/identywaf/LICENSE
619619
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 thirdparty/__init__.py
620620
879d96f2460bc6c79c0db46b5813080841c7403399292ce76fe1dc0a6ed353d8 thirdparty/keepalive/__init__.py
621-
f517561115b0cfaa509d0d4216cd91c7de92c6a5a30f1688fdca22e4cd52b8f8 thirdparty/keepalive/keepalive.py
621+
c7ac7253fa450030f9c42f11bb19689055bb8c39621bcfbeca856ba3c9342760 thirdparty/keepalive/keepalive.py
622622
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 thirdparty/magic/__init__.py
623623
4d89a52f809c28ce1dc17bb0c00c775475b8ce01c2165942877596a6180a2fd8 thirdparty/magic/magic.py
624624
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 thirdparty/multipart/__init__.py

lib/core/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from thirdparty import six
2121

2222
# sqlmap version (<major>.<minor>.<month>.<monthly commit>)
23-
VERSION = "1.10.1.85"
23+
VERSION = "1.10.2.0"
2424
TYPE = "dev" if VERSION.count('.') > 2 and VERSION.split('.')[-1] != '0' else "stable"
2525
TYPE_COLORS = {"dev": 33, "stable": 90, "pip": 34}
2626
VERSION_STRING = "sqlmap/%s#%s" % ('.'.join(VERSION.split('.')[:-1]) if VERSION.count('.') > 2 and VERSION.split('.')[-1] == '0' else VERSION, TYPE)

thirdparty/keepalive/keepalive.py

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
# Lesser General Public License for more details.
1313
#
1414
# You should have received a copy of the GNU Lesser General Public
15-
# License along with this library; if not, write to the
16-
# Free Software Foundation, Inc.,
17-
# 59 Temple Place, Suite 330,
15+
# License along with this library; if not, write to the
16+
# Free Software Foundation, Inc.,
17+
# 59 Temple Place, Suite 330,
1818
# Boston, MA 02111-1307 USA
1919

2020
# This file was part of urlgrabber, a high-level cross-protocol url-grabber
@@ -28,7 +28,7 @@
2828
>>> keepalive_handler = HTTPHandler()
2929
>>> opener = _urllib.request.build_opener(keepalive_handler)
3030
>>> _urllib.request.install_opener(opener)
31-
>>>
31+
>>>
3232
>>> fo = _urllib.request.urlopen('http://www.python.org')
3333
3434
If a connection to a given host is requested, and all of the existing
@@ -154,14 +154,18 @@ def remove(self, connection):
154154
else:
155155
del self._connmap[connection]
156156
del self._readymap[connection]
157-
self._hostmap[host].remove(connection)
157+
try:
158+
self._hostmap[host].remove(connection)
159+
except ValueError:
160+
pass
158161
if not self._hostmap[host]: del self._hostmap[host]
159162
finally:
160163
self._lock.release()
161164

162165
def set_ready(self, connection, ready):
163-
try: self._readymap[connection] = ready
164-
except KeyError: pass
166+
self._lock.acquire()
167+
if connection in self._readymap: self._readymap[connection] = ready
168+
self._lock.release()
165169

166170
def get_ready_conn(self, host):
167171
conn = None
@@ -178,10 +182,14 @@ def get_ready_conn(self, host):
178182
return conn
179183

180184
def get_all(self, host=None):
181-
if host:
182-
return list(self._hostmap.get(host, []))
183-
else:
184-
return dict(self._hostmap)
185+
self._lock.acquire()
186+
try:
187+
if host:
188+
return list(self._hostmap.get(host, []))
189+
else:
190+
return dict(self._hostmap)
191+
finally:
192+
self._lock.release()
185193

186194
class KeepAliveHandler:
187195
def __init__(self):
@@ -242,9 +250,9 @@ def do_open(self, req):
242250
h = self._get_connection(host)
243251
if DEBUG: DEBUG.info("creating new connection to %s (%d)",
244252
host, id(h))
245-
self._cm.add(host, h, 0)
246253
self._start_transaction(h, req)
247254
r = h.getresponse()
255+
self._cm.add(host, h, 0)
248256
except (socket.error, _http_client.HTTPException) as err:
249257
raise _urllib.error.URLError(err)
250258

@@ -254,20 +262,20 @@ def do_open(self, req):
254262
if r.will_close:
255263
if DEBUG: DEBUG.info('server will close connection, discarding')
256264
self._cm.remove(h)
265+
h.close()
257266

258267
r._handler = self
259268
r._host = host
260269
r._url = req.get_full_url()
261270
r._connection = h
262271
r.code = r.status
263272
r.headers = r.msg
264-
r.msg = r.reason
265273

266274
if r.status == 200 or not HANDLE_ERRORS:
267275
return r
268276
else:
269277
return self.parent.error('http', req, r,
270-
r.status, r.msg, r.headers)
278+
r.status, r.reason, r.headers)
271279

272280
def _reuse_connection(self, h, req, host):
273281
"""start the transaction with a re-used connection
@@ -283,7 +291,7 @@ def _reuse_connection(self, h, req, host):
283291
# worked. We'll check the version below, too.
284292
except (socket.error, _http_client.HTTPException):
285293
r = None
286-
except:
294+
except Exception:
287295
# adding this block just in case we've missed
288296
# something we will still raise the exception, but
289297
# lets try and close the connection and remove it
@@ -314,16 +322,16 @@ def _reuse_connection(self, h, req, host):
314322

315323
def _start_transaction(self, h, req):
316324
try:
317-
if req.data:
325+
if req.data is not None:
318326
data = req.data
319327
if hasattr(req, 'selector'):
320328
h.putrequest(req.get_method() or 'POST', req.selector, skip_host=req.has_header("Host"), skip_accept_encoding=req.has_header("Accept-encoding"))
321329
else:
322330
h.putrequest(req.get_method() or 'POST', req.get_selector(), skip_host=req.has_header("Host"), skip_accept_encoding=req.has_header("Accept-encoding"))
323-
if 'Content-type' not in req.headers:
331+
if not req.has_header('Content-type'):
324332
h.putheader('Content-type',
325333
'application/x-www-form-urlencoded')
326-
if 'Content-length' not in req.headers:
334+
if not req.has_header('Content-length'):
327335
h.putheader('Content-length', '%d' % len(data))
328336
else:
329337
if hasattr(req, 'selector'):
@@ -333,20 +341,20 @@ def _start_transaction(self, h, req):
333341
except (socket.error, _http_client.HTTPException) as err:
334342
raise _urllib.error.URLError(err)
335343

336-
if 'Connection' not in req.headers:
337-
req.headers['Connection'] = 'keep-alive'
344+
if not req.has_header('Connection'):
345+
h.putheader('Connection', 'keep-alive')
338346

339347
for args in self.parent.addheaders:
340-
if args[0] not in req.headers:
348+
if not req.has_header(args[0]):
341349
h.putheader(*args)
342350
for k, v in req.headers.items():
343351
h.putheader(k, v)
344352
h.endheaders()
345-
if req.data:
353+
if req.data is not None:
346354
h.send(data)
347355

348356
def _get_connection(self, host):
349-
return NotImplementedError
357+
raise NotImplementedError()
350358

351359
class HTTPHandler(KeepAliveHandler, _urllib.request.HTTPHandler):
352360
def __init__(self):
@@ -373,8 +381,10 @@ def https_open(self, req):
373381
return self.do_open(req)
374382

375383
def _get_connection(self, host):
376-
try: return self._ssl_factory.get_https_connection(host)
377-
except AttributeError: return HTTPSConnection(host)
384+
if self._ssl_factory:
385+
return self._ssl_factory.get_https_connection(host)
386+
else:
387+
return HTTPSConnection(host)
378388

379389
class HTTPResponse(_http_client.HTTPResponse):
380390
# we need to subclass HTTPResponse in order to
@@ -397,9 +407,9 @@ class HTTPResponse(_http_client.HTTPResponse):
397407

398408

399409
def __init__(self, sock, debuglevel=0, strict=0, method=None):
400-
if method: # the httplib in python 2.3 uses the method arg
401-
_http_client.HTTPResponse.__init__(self, sock, debuglevel, method)
402-
else: # 2.2 doesn't
410+
if method:
411+
_http_client.HTTPResponse.__init__(self, sock, debuglevel, method=method)
412+
else:
403413
_http_client.HTTPResponse.__init__(self, sock, debuglevel)
404414
self.fileno = sock.fileno
405415
self.code = None
@@ -453,11 +463,11 @@ def read(self, amt=None):
453463

454464
def readline(self, limit=-1):
455465
data = b""
456-
i = self._rbuf.find('\n')
466+
i = self._rbuf.find(b'\n')
457467
while i < 0 and not (0 < limit <= len(self._rbuf)):
458468
new = self._raw_read(self._rbufsize)
459469
if not new: break
460-
i = new.find('\n')
470+
i = new.find(b'\n')
461471
if i >= 0: i = i + len(self._rbuf)
462472
self._rbuf = self._rbuf + new
463473
if i < 0: i = len(self._rbuf)
@@ -468,15 +478,15 @@ def readline(self, limit=-1):
468478

469479
def readlines(self, sizehint = 0):
470480
total = 0
471-
list = []
481+
lines = []
472482
while 1:
473483
line = self.readline()
474484
if not line: break
475-
list.append(line)
485+
lines.append(line)
476486
total += len(line)
477487
if sizehint and total >= sizehint:
478488
break
479-
return list
489+
return lines
480490

481491

482492
class HTTPConnection(_http_client.HTTPConnection):
@@ -540,10 +550,10 @@ def continuity(url):
540550
print(format % ('keepalive read', m.hexdigest()))
541551

542552
fo = _urllib.request.urlopen(url)
543-
foo = ''
553+
foo = b''
544554
while 1:
545555
f = fo.readline()
546-
if f: foo = foo + f
556+
if f: foo += f
547557
else: break
548558
fo.close()
549559
m = md5(foo)

0 commit comments

Comments
 (0)