def __init__(self, version, status_code, status_reason): self.version = version self.status_code = status_code self.status_reason = status_reason self.fields = NameValueRecord() self.body = Body() self.url_info = None
def __init__(self, method, resource_url, version='HTTP/1.1'): self.method = method self.resource_url = resource_url self.url_info = None self.version = version self.fields = NameValueRecord() self.body = Body() self.address = None
def factory(*args, **kwargs): # TODO: Response should be dependency injected response = Response(*args, **kwargs) root = self._processor.root_path response.body.content_file = Body.new_temp_file(root) if self._file_writer_session: self._file_writer_session.process_response(response) return response
def factory(*args, **kwargs): # TODO: Response should be dependency injected response = Response(*args, **kwargs) # FIXME: we should be using --directory-prefix instead of CWD. response.body.content_file = Body.new_temp_file(os.getcwd()) if self._file_writer_session: self._file_writer_session.process_response(response) return response
class Response(BaseResponse): '''Represents the HTTP response. Attributes: version (str): The HTTP version in the status line. For example, ``HTTP/1.1``. status_code (int): The status code in the status line. status_reason (str): The status reason string in the status line. fields (NameValueRecord): An instance of :class:`.namevalue.NameValueRecord` containing the HTTP header (and trailer, if present) fields. body (Body): An instance of :class:`.conversation.Body`. url_info (URLInfo): An instance of :class:`.url.URLInfo` for the of the corresponding request. ''' def __init__(self, version, status_code, status_reason): self.version = version self.status_code = status_code self.status_reason = status_reason self.fields = NameValueRecord() self.body = Body() self.url_info = None @classmethod def parse_status_line(cls, string): '''Parse the status line bytes. Returns: tuple: An tuple representing the version, code, and reason. ''' match = re.match( br'(HTTP/1\.[01])[ \t]+([0-9]{1,3})[ \t]*([^\r\n]*)', string ) if match: groups = match.groups() if len(groups) == 3: return wpull.string.to_str( (groups[0], int(groups[1]), groups[2]), encoding='latin-1', ) raise ProtocolError("Error parsing status line '{0}'".format(string)) def header(self): '''Return the HTTP header as bytes.''' return '{0} {1} {2}\r\n{3}\r\n'.format( self.version, self.status_code, self.status_reason, str(self.fields) ).encode('utf-8') def __repr__(self): return '<Response({version}, {code}, {reason})>'.format( version=self.version, code=self.status_code, reason=self.status_reason ) def to_dict(self): '''Convert the response to a :class:`dict`.''' return { 'version': self.version, 'status_code': self.status_code, 'status_reason': self.status_reason, 'body': self.body.to_dict(), 'url_info': self.url_info.to_dict() if self.url_info else None }
class Response(BaseResponse): '''Represents the HTTP response. Attributes: version (str): The HTTP version in the status line. For example, ``HTTP/1.1``. status_code (int): The status code in the status line. status_reason (str): The status reason string in the status line. fields (NameValueRecord): An instance of :class:`.namevalue.NameValueRecord` containing the HTTP header (and trailer, if present) fields. body (Body): An instance of :class:`.conversation.Body`. url_info (URLInfo): An instance of :class:`.url.URLInfo` for the of the corresponding request. ''' def __init__(self, version, status_code, status_reason): self.version = version self.status_code = status_code self.status_reason = status_reason self.fields = NameValueRecord() self.body = Body() self.url_info = None @classmethod def parse_status_line(cls, string): '''Parse the status line bytes. Returns: tuple: An tuple representing the version, code, and reason. ''' match = re.match(br'(HTTP/1\.[01])[ \t]+([0-9]{1,3})[ \t]*([^\r\n]*)', string) if match: groups = match.groups() if len(groups) == 3: return wpull.string.to_str( (groups[0], int(groups[1]), groups[2]), encoding='latin-1', ) raise ProtocolError("Error parsing status line '{0}'".format(string)) def header(self): '''Return the HTTP header as bytes.''' return '{0} {1} {2}\r\n{3}\r\n'.format( self.version, self.status_code, self.status_reason, str(self.fields)).encode('utf-8') def __repr__(self): return '<Response({version}, {code}, {reason})>'.format( version=self.version, code=self.status_code, reason=self.status_reason) def to_dict(self): '''Convert the response to a :class:`dict`.''' return { 'version': self.version, 'status_code': self.status_code, 'status_reason': self.status_reason, 'body': self.body.to_dict(), 'url_info': self.url_info.to_dict() if self.url_info else None }