예제 #1
0
파일: client.py 프로젝트: yume190/httpie
def finalize_headers(headers: RequestHeadersDict) -> RequestHeadersDict:
    final_headers = RequestHeadersDict()
    for name, value in headers.items():
        if value is not None:
            # “leading or trailing LWS MAY be removed without
            # changing the semantics of the field value”
            # <https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html>
            # Also, requests raises `InvalidHeader` for leading spaces.
            value = value.strip()
            if isinstance(value, str):
                # See <https://github.com/jakubroztocil/httpie/issues/212>
                value = value.encode('utf8')
        final_headers[name] = value
    return final_headers
예제 #2
0
    def update_headers(self, request_headers: RequestHeadersDict):
        """
        Update the session headers with the request ones while ignoring
        certain name prefixes.

        """
        headers = self.headers
        for name, value in request_headers.items():

            if value is None:
                continue  # Ignore explicitly unset headers

            if type(value) is not str:
                value = value.decode('utf8')

            if name.lower() == 'user-agent' and value.startswith('HTTPie/'):
                continue

            if name.lower() == 'cookie':
                for cookie_name, morsel in SimpleCookie(value).items():
                    self['cookies'][cookie_name] = {'value': morsel.value}
                del request_headers[name]
                continue

            for prefix in SESSION_IGNORED_HEADER_PREFIXES:
                if name.lower().startswith(prefix.lower()):
                    break
            else:
                headers[name] = value

        self['headers'] = dict(headers)
예제 #3
0
 def __init__(self, as_form=False):
     self.headers = RequestHeadersDict()
     self.data = RequestDataDict() if as_form else RequestJSONDataDict()
     self.files = RequestFilesDict()
     self.params = RequestQueryParamsDict()
     # To preserve the order of fields in file upload multipart requests.
     self.multipart_data = MultipartRequestDataDict()
예제 #4
0
파일: client.py 프로젝트: yume190/httpie
def make_default_headers(args: argparse.Namespace) -> RequestHeadersDict:
    default_headers = RequestHeadersDict({'User-Agent': DEFAULT_UA})

    auto_json = args.data and not args.form
    if args.json or auto_json:
        default_headers['Accept'] = JSON_ACCEPT
        if args.json or (auto_json and args.data):
            default_headers['Content-Type'] = JSON_CONTENT_TYPE

    elif args.form and not args.files:
        # If sending files, `requests` will set
        # the `Content-Type` for us.
        default_headers['Content-Type'] = FORM_CONTENT_TYPE
    return default_headers
예제 #5
0
파일: sessions.py 프로젝트: zjkanjie/httpie
    def update_headers(self, request_headers: RequestHeadersDict):
        """
        Update the session headers with the request ones while ignoring
        certain name prefixes.

        """
        headers = self.headers
        for name, value in request_headers.items():

            if value is None:
                continue  # Ignore explicitly unset headers

            value = value.decode('utf8')
            if name == 'User-Agent' and value.startswith('HTTPie/'):
                continue

            for prefix in SESSION_IGNORED_HEADER_PREFIXES:
                if name.lower().startswith(prefix.lower()):
                    break
            else:
                headers[name] = value

        self['headers'] = dict(headers)
예제 #6
0
파일: sessions.py 프로젝트: zjkanjie/httpie
 def headers(self) -> RequestHeadersDict:
     return RequestHeadersDict(self['headers'])
 def __init__(self, as_form=False):
     self.headers = RequestHeadersDict()
     self.data = RequestDataDict() if as_form else RequestJSONDataDict()
     self.files = RequestFilesDict()
     self.params = RequestQueryParamsDict()