Ejemplo n.º 1
0
    def commit(self):
        def w(txt):
            """Decode as UTF-8 and write to client connection"""
            self.conn.write(bytes(txt, 'UTF-8'))

        # TT: blatt6 - default values have been moved to constructor
        # TT: to make them available to logging

        from server.statuscodes import statuscode

        (phrase, explanation) = statuscode(self.code)
        w("HTTP/1.1 %d %s\n" % (self.code, phrase))
        log(1, "HTTP/1.1 %d %s (%s)\n" % (self.code, phrase, explanation))

        import datetime

        w("Date: %s\n" %
          datetime.datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S"))
        w("Connection: close\n")

        # send cookies
        for c in self.cookies:
            w(c.get_header())

        # send other headers
        for key, value in self.headers.items():
            w("%s: %s\n" % (key, value))
        w("\n")  # extra leerzeile schliesst header ab

        if self.body:
            if isinstance(self.body, str):  # UTF-8
                w(self.body)
            else:  # bytes, e.g. binary data
                self.conn.write(self.body)
Ejemplo n.º 2
0
    def send(self, code=None, headers=None, body=""):
        if not headers:
            headers = {}

        def w(txt):
            """Decode as UTF-8 and write to client connection"""
            self.conn.write(bytes(txt, 'UTF-8'))

        # method parameters overwrite/add to instance variables
        if code:
            self.code = code
        if body:
            self.body = body
        if headers:
            self.headers.update(headers)

        # Set ETag in Header
        tag = self.generate_eTag(self.body)
        if tag == self.etag:
            self.body = None
            self.code = 304

        # default values
        if not self.code:
            self.code = 200
        if 'Content-Type' not in self.headers:
            self.headers['Content-Type'] = "text/html; charset=utf-8"

        from server.statuscodes import statuscode

        (phrase, explanation) = statuscode(self.code)
        w("HTTP/1.1 %d %s\n" % (self.code, phrase))
        log(1, "HTTP/1.1 %d %s (%s)\n" % (self.code, phrase, explanation))

        import datetime

        w("Date: %s\n" %
          datetime.datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S"))
        w("Content-Type: %s\n" % self.headers['Content-Type'])
        w("Connection: close\n")
        for key, value in self.headers.items():
            w("%s: %s\n" % (key, value))

        #Etag Header setzen
        w("ETag: {}\n".format(tag))

        # Addition: Set proper Content Length
        if self.body and not 'Content-Length' in self.headers:
            w("Content-Length: %d\n" % len(self.body))

        w("\n")  # extra leerzeile schliesst header ab

        if self.body:
            if isinstance(self.body, str):
                w(self.body)
            else:
                self.conn.write(self.body)
Ejemplo n.º 3
0
    def send(self, code=None, headers=None, body=""):

        if not headers:
            headers = {}

        def w(txt):
            """Decode as UTF-8 and write to client connection"""
            self.conn.write(bytes(txt, 'UTF-8'))

        # method parameters overwrite/add to instance variables
        if code:
            self.code = code
        if body:
            self.body = body
        if headers:
            self.headers.update(headers)

        # default values
        if not self.code:
            self.code = 200
        if 'Content-Type' not in self.headers:
            self.headers['Content-Type'] = "text/html; charset=UTF-8"

        from server.statuscodes import statuscode

        (phrase, explanation) = statuscode(self.code)
        w("HTTP/1.1 %d %s\n" % (self.code, phrase))
        log(1, "HTTP/1.1 %d %s (%s)\n" % (self.code, phrase, explanation))

        import datetime

        w("Date: %s\n" %
          datetime.datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S"))
        w("Connection: close\n")
        for key, value in self.headers.items():
            w("%s: %s\n" % (key, value))

        # Addition: Set proper Content Length
        if self.body and 'Content-Length' not in self.headers:
            if isinstance(
                    self.body, str
            ):  # we need length in bytes, so we might have to convert it first
                length = len(bytes(self.body, 'UTF8'))
            else:
                length = len(self.body)
            w("Content-Length: %d\n" % length)

        w("\n")  # extra leerzeile schliesst header ab

        if self.body:
            if isinstance(self.body, str):
                w(self.body)
            else:
                self.conn.write(self.body)
Ejemplo n.º 4
0
    def commit(self):
        def w(txt):
            """Decode as UTF-8 and write to client connection"""
            self.conn.write(bytes(txt, 'UTF-8'))

        # default values
        if not self.code:
            self.code = 200
        if 'Content-Type' not in self.headers:
            self.headers['Content-Type'] = "text/html; charset=UTF-8"

        from server.statuscodes import statuscode

        (phrase, explanation) = statuscode(self.code)
        w("HTTP/1.1 %d %s\n" % (self.code, phrase))
        log(1, "HTTP/1.1 %d %s (%s)\n" % (self.code, phrase, explanation))

        import datetime

        w("Date: %s\n" %
          datetime.datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S"))
        w("Connection: close\n")

        # send cookies
        for c in self.cookies:
            w(c.get_header())

        # send other headers
        for key, value in self.headers.items():
            w("%s: %s\n" % (key, value))

        # Addition: Set proper Content Length
        if self.body and 'Content-Length' not in self.headers:
            if isinstance(
                    self.body, str
            ):  # we need length in bytes, so we might have to convert it first
                length = len(bytes(self.body, 'UTF8'))
            else:
                length = len(self.body)
            w("Content-Length: %d\n" % length)

        w("\n")  # extra leerzeile schliesst header ab

        if self.body:
            if isinstance(self.body, str):  # UTF-8
                w(self.body)
            else:  # bytes, e.g. binary data
                self.conn.write(self.body)
Ejemplo n.º 5
0
    def send(self, code=None, headers=None, body=""):

        if not headers:
            headers = {}

        def w(txt):
            """Decode as UTF-8 and write to client connection"""
            self.conn.write(bytes(txt, 'UTF-8'))

        # method parameters overwrite/add to instance variables
        if code:
            self.code = code
        if body:
            self.body = body
        if headers:
            self.headers.update(headers)

        # default values
        if not self.code:
            self.code = 200
        if 'Content-Type' not in self.headers:
            self.headers['Content-Type'] = "text/html; charset=utf-8"

        # Blatt 03: ETag-Handling
        etag = None
        if self.body and self.code != 304:
            import hashlib
            if isinstance(self.body, str):
                bytes_body = bytes(self.body, 'utf-8')
            else:
                bytes_body = self.body
            etag = hashlib.sha256(bytes_body).hexdigest()

            print("Etag computed: %s\nEtag from request: %s\n" %
                  (etag, self.etag))
            if etag == self.etag:  # Match found, just send 'not modified' information
                return self.send(304)

        from server.statuscodes import statuscode

        (phrase, explanation) = statuscode(self.code)
        w("HTTP/1.1 %d %s\n" % (self.code, phrase))
        log(1, "HTTP/1.1 %d %s (%s)\n" % (self.code, phrase, explanation))

        import datetime

        w("Date: %s\n" %
          datetime.datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S"))
        w("Content-Type: %s\n" % self.headers['Content-Type'])
        w("Connection: close\n")
        for key, value in self.headers.items():
            w("%s: %s\n" % (key, value))

        # Addition: Set proper Content Length
        if self.body and 'Content-Length' not in self.headers:
            w("Content-Length: %d\n" % len(self.body))

        # Add Etag-Header
        if self.body and self.code != 304:
            w('Etag: "%s"\n' % etag)

        w("\n")  # extra leerzeile schließt header ab

        if self.body:
            if isinstance(self.body, str):
                w(self.body)
            else:
                self.conn.write(self.body)