(url, agent, etag, modified, referrer, auth, request_headers)
| 2901 | return _StringIO(str(url_file_stream_or_string)) |
| 2902 | |
| 2903 | def _build_urllib2_request(url, agent, etag, modified, referrer, auth, request_headers): |
| 2904 | request = urllib2.Request(url) |
| 2905 | request.add_header('User-Agent', agent) |
| 2906 | if etag: |
| 2907 | request.add_header('If-None-Match', etag) |
| 2908 | if type(modified) == type(''): |
| 2909 | modified = _parse_date(modified) |
| 2910 | elif isinstance(modified, datetime.datetime): |
| 2911 | modified = modified.utctimetuple() |
| 2912 | if modified: |
| 2913 | # format into an RFC 1123-compliant timestamp. We can't use |
| 2914 | # time.strftime() since the %a and %b directives can be affected |
| 2915 | # by the current locale, but RFC 2616 states that dates must be |
| 2916 | # in English. |
| 2917 | short_weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] |
| 2918 | months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] |
| 2919 | request.add_header('If-Modified-Since', '%s, %02d %s %04d %02d:%02d:%02d GMT' % (short_weekdays[modified[6]], modified[2], months[modified[1] - 1], modified[0], modified[3], modified[4], modified[5])) |
| 2920 | if referrer: |
| 2921 | request.add_header('Referer', referrer) |
| 2922 | if gzip and zlib: |
| 2923 | request.add_header('Accept-encoding', 'gzip, deflate') |
| 2924 | elif gzip: |
| 2925 | request.add_header('Accept-encoding', 'gzip') |
| 2926 | elif zlib: |
| 2927 | request.add_header('Accept-encoding', 'deflate') |
| 2928 | else: |
| 2929 | request.add_header('Accept-encoding', '') |
| 2930 | if auth: |
| 2931 | request.add_header('Authorization', 'Basic %s' % auth) |
| 2932 | if ACCEPT_HEADER: |
| 2933 | request.add_header('Accept', ACCEPT_HEADER) |
| 2934 | # use this for whatever -- cookies, special headers, etc |
| 2935 | # [('Cookie','Something'),('x-special-header','Another Value')] |
| 2936 | for header_name, header_value in request_headers.items(): |
| 2937 | request.add_header(header_name, header_value) |
| 2938 | request.add_header('A-IM', 'feed') # RFC 3229 support |
| 2939 | return request |
| 2940 | |
| 2941 | _date_handlers = [] |
| 2942 | def registerDateHandler(func): |
no test coverage detected