Ejemplo n.º 1
0
 def clear_cookie(self, name: str):
     """
     Clear a cookie with the name.
     """
     if self._initial_written:
         raise HTTPError(500, "You cannot clear a cookie after the "
                              "initial is written.")
     self.set_cookie(name=name, value="", expires=format_timestamp(0))
Ejemplo n.º 2
0
 def test_format_timestamp_with_none(self):
     timestamp = time.time()
     timestamp_future = time.time() + 1
     formatted_timestamp = email.utils.formatdate(timestamp,
                                                  usegmt=True)
     formatted_timestamp_future = email.utils.formatdate(timestamp,
                                                         usegmt=True)
     self.assertIn(format_timestamp(),
                   [formatted_timestamp, formatted_timestamp_future])
Ejemplo n.º 3
0
    def write_initial(self):
        if self._initial_written:
            raise HTTPError(500, "Cannot write initial twice.")
        if "content-type" not in self._headers.keys():
            self.set_header("content-type", "text/html; charset=utf-8;")

        if self.connection._can_keep_alive:
            if "connection" not in self._headers:
                self.set_header("connection", "Keep-Alive")
        else:
            self.set_header("connection", "Close")

        if self._headers["connection"] == "Keep-Alive":
            self.set_header("transfer-encoding", "Chunked")
            if "content-length" in self._headers.keys():
                del self._headers["content-length"]

        if "date" not in self._headers.keys():
            self.set_header("date", format_timestamp())

        self._headers.accept_cookies_for_response(self._cookies)

        if self.settings.get("csrf_protect", False):
            self._csrf_value

        if "etag" not in self._headers and self._status_code == 200:
            self._body_etag

        if self.check_etag_header():
            self._status_code = 304
            self._response_body.clear()
            for header_name in ("allow", "content-encoding",
                                "content-language", "content-length",
                                "content-md5", "content-range", "content-type",
                                "last-modified"):
                self.clear_header(header_name)

        self.connection.write_initial(
            http_version=self.http_version,
            status_code=self._status_code, headers=self._headers)

        self._initial_written = True
Ejemplo n.º 4
0
    def write_initial(self):
        """
        Send the Initial Part(e.g.: Headers) of a Response to the remote.

        This function should be only called once.

        Usually this function is called by `RequestHandler.flush`
        automatically.

        After this function is called, you cannot add any new headers, cookies,
        or change the status_code. If an error is raised after this function
        is called, FutureFinity is going to close the connection directly.
        """
        if self._initial_written:
            raise HTTPError(500, "Cannot write initial twice.")
        if "content-type" not in self._headers.keys():
            self.set_header("content-type", "text/html; charset=utf-8;")

        if self.connection._can_keep_alive:
            if "connection" not in self._headers:
                self.set_header("connection", "Keep-Alive")
        else:
            self.set_header("connection", "Close")

        if self._headers["connection"] == "Keep-Alive":
            self.set_header("transfer-encoding", "Chunked")
            if "content-length" in self._headers.keys():
                del self._headers["content-length"]

        if "date" not in self._headers.keys():
            self.set_header("date", format_timestamp())

        self._headers.accept_cookies_for_response(self._cookies)

        if self.settings.get("csrf_protect", False):
            self.set_csrf_value()

        self.connection.write_initial(
            http_version=self.http_version,
            status_code=self._status_code, headers=self._headers)

        self._initial_written = True
Ejemplo n.º 5
0
 def clear_cookie(self, name: str):
     """
     Clear a cookie with the name.
     """
     self.set_cookie(name=name, value="", expires=format_timestamp(0))
Ejemplo n.º 6
0
 def test_format_timestamp_with_tuple(self):
     time_tuple = tuple(time.gmtime())
     timestamp = calendar.timegm(time_tuple)
     formatted_timestamp = email.utils.formatdate(timestamp,
                                                  usegmt=True)
     self.assertEqual(formatted_timestamp, format_timestamp(time_tuple))
Ejemplo n.º 7
0
 def test_format_timestamp_with_struct_time(self):
     struct_time = time.gmtime()
     timestamp = calendar.timegm(struct_time)
     formatted_timestamp = email.utils.formatdate(timestamp,
                                                  usegmt=True)
     self.assertEqual(formatted_timestamp, format_timestamp(struct_time))
Ejemplo n.º 8
0
 def test_format_timestamp_with_real_number(self):
     timestamp = time.time()
     formatted_timestamp = email.utils.formatdate(timestamp,
                                                  usegmt=True)
     self.assertEqual(formatted_timestamp, format_timestamp(timestamp))
Ejemplo n.º 9
0
 def test_format_timestamp_with_datetime(self):
     datetime_time = datetime.datetime.utcnow()
     timestamp = calendar.timegm(datetime_time.utctimetuple())
     formatted_timestamp = email.utils.formatdate(timestamp,
                                                  usegmt=True)
     self.assertEqual(formatted_timestamp, format_timestamp(datetime_time))