Prepare HTTP Cookie, HTTP User-Agent and HTTP Referer headers to use when performing the HTTP requests
(items=None, base=None)
| 63 | |
| 64 | @lockedmethod |
| 65 | def forgeHeaders(items=None, base=None): |
| 66 | """ |
| 67 | Prepare HTTP Cookie, HTTP User-Agent and HTTP Referer headers to use when performing |
| 68 | the HTTP requests |
| 69 | """ |
| 70 | |
| 71 | items = items or {} |
| 72 | |
| 73 | for _ in list(items.keys()): |
| 74 | if items[_] is None: |
| 75 | del items[_] |
| 76 | |
| 77 | headers = OrderedDict(conf.httpHeaders if base is None else base) |
| 78 | headers.update(items.items()) |
| 79 | |
| 80 | class _str(str): |
| 81 | def capitalize(self): |
| 82 | return _str(self) |
| 83 | |
| 84 | def title(self): |
| 85 | return _str(self) |
| 86 | |
| 87 | _ = headers |
| 88 | headers = OrderedDict() |
| 89 | for key, value in _.items(): |
| 90 | success = False |
| 91 | |
| 92 | for _ in headers: |
| 93 | if _.upper() == key.upper(): |
| 94 | del headers[_] |
| 95 | break |
| 96 | |
| 97 | if key.upper() not in (_.upper() for _ in getPublicTypeMembers(HTTP_HEADER, True)): |
| 98 | try: |
| 99 | headers[_str(key)] = value # dirty hack for http://bugs.python.org/issue12455 |
| 100 | except UnicodeEncodeError: # don't do the hack on non-ASCII header names (they have to be properly encoded later on) |
| 101 | pass |
| 102 | else: |
| 103 | success = True |
| 104 | if not success: |
| 105 | key = '-'.join(_.capitalize() for _ in key.split('-')) |
| 106 | headers[key] = value |
| 107 | |
| 108 | if conf.cj: |
| 109 | if HTTP_HEADER.COOKIE in headers: |
| 110 | for cookie in conf.cj: |
| 111 | if cookie is None or cookie.domain_specified and not (conf.hostname or "").endswith(cookie.domain): |
| 112 | continue |
| 113 | |
| 114 | if ("%s=" % getUnicode(cookie.name)) in getUnicode(headers[HTTP_HEADER.COOKIE]): |
| 115 | if conf.loadCookies: |
| 116 | conf.httpHeaders = filterNone((item if item[0] != HTTP_HEADER.COOKIE else None) for item in conf.httpHeaders) |
| 117 | elif kb.mergeCookies is None: |
| 118 | message = "you provided a HTTP %s header value, while " % HTTP_HEADER.COOKIE |
| 119 | message += "target URL provides its own cookies within " |
| 120 | message += "HTTP %s header which intersect with yours. " % HTTP_HEADER.SET_COOKIE |
| 121 | message += "Do you want to merge them in further requests? [Y/n] " |
| 122 |
no test coverage detected
searching dependent graphs…