|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +""" |
| 4 | +$Id$ |
| 5 | +
|
| 6 | +Copyright (c) 2006-2010 sqlmap developers (http://sqlmap.sourceforge.net/) |
| 7 | +See the file 'doc/COPYING' for copying permission |
| 8 | +""" |
| 9 | + |
| 10 | +import urllib |
| 11 | +import urllib2 |
| 12 | + |
| 13 | +from lib.core.exception import sqlmapConnectionException |
| 14 | + |
| 15 | +class HTTPRangeHandler(urllib2.BaseHandler): |
| 16 | + """ |
| 17 | + Handler that enables HTTP Range headers. |
| 18 | +
|
| 19 | + Reference: http://stackoverflow.com/questions/1971240/python-seek-on-remote-file |
| 20 | +
|
| 21 | + This was extremely simple. The Range header is a HTTP feature to |
| 22 | + begin with so all this class does is tell urllib2 that the |
| 23 | + "206 Partial Content" reponse from the HTTP server is what we |
| 24 | + expected. |
| 25 | +
|
| 26 | + Example: |
| 27 | + import urllib2 |
| 28 | + import byterange |
| 29 | +
|
| 30 | + range_handler = range.HTTPRangeHandler() |
| 31 | + opener = urllib2.build_opener(range_handler) |
| 32 | +
|
| 33 | + # install it |
| 34 | + urllib2.install_opener(opener) |
| 35 | +
|
| 36 | + # create Request and set Range header |
| 37 | + req = urllib2.Request('http://www.python.org/') |
| 38 | + req.header['Range'] = 'bytes=30-50' |
| 39 | + f = urllib2.urlopen(req) |
| 40 | + """ |
| 41 | + |
| 42 | + def http_error_206(self, req, fp, code, msg, hdrs): |
| 43 | + # 206 Partial Content Response |
| 44 | + r = urllib.addinfourl(fp, hdrs, req.get_full_url()) |
| 45 | + r.code = code |
| 46 | + r.msg = msg |
| 47 | + return r |
| 48 | + |
| 49 | + def http_error_416(self, req, fp, code, msg, hdrs): |
| 50 | + # HTTP's Range Not Satisfiable error |
| 51 | + errMsg = "Invalid range" |
| 52 | + raise sqlmapConnectionException, errMsg |
0 commit comments