Esempio n. 1
0
 def __init__(self, channel, request, command, uri, version, headers):
     self.number = Request.singleton.request_counter.increment()
     self._channel = channel
     # Top line of the request, like GET /index.html HTTP/1.0 for example.
     self._request = request
     # Type of command, like GET for example.
     self._command = command
     # Requested resource, like /index.html for example.
     self._uri = uri
     # HTTP version, like 1.0 for example.
     self._version = version
     # String of headers exactly as recevied.  Headers included
     #   are all line after request and before double \r\n.
     self.request_header_lines = self._headers = headers
     self.request_headers = None
     self.response_headers = {
         'Server': 'WebServer/%s' % VERSION_STRING,
         'Date': _http_date.build_http_date(time.time())
     }
     self._split_uri = None
     self._query_dictionary = {}
     self.request_cookies = None
     self._outgoing = asynchat.fifo()
     self._outgoing_content_length = 0
     self._collector = None
     self._user_object = None
     self._authentication_scheme = None
     default = self.server.authentication.lower()
     self._default_scheme = default
     self._default_authentication = {
         'digest': self.digest_challenge,
         'basic': self.basic_challenge,
         'form': self.form_challenge
     }[default]
     self.out = None
     self.response_length = 0
     self.response_cookies = []
     self.response_producer = None
     self.outgoing_producers = asynchat.fifo()
Esempio n. 2
0
 def __init__(self,channel,request,command,uri,version,headers):
     self.number = Request.singleton.request_counter.increment()
     self._channel = channel
     # Top line of the request, like GET /index.html HTTP/1.0 for example.
     self._request = request
     # Type of command, like GET for example.
     self._command = command
     # Requested resource, like /index.html for example.
     self._uri = uri
     # HTTP version, like 1.0 for example.
     self._version = version
     # String of headers exactly as recevied.  Headers included
     #   are all line after request and before double \r\n.
     self.request_header_lines = self._headers = headers
     self.request_headers = None
     self.response_headers = {
         'Server': 'WebServer/%s' % VERSION_STRING,
         'Date': _http_date.build_http_date(time.time())}
     self._split_uri = None
     self._query_dictionary = {}
     self.request_cookies = None
     self._outgoing = asynchat.fifo()
     self._outgoing_content_length = 0
     self._collector = None
     self._user_object = None
     self._authentication_scheme = None
     default = self.server.authentication.lower()
     self._default_scheme = default
     self._default_authentication = {'digest':self.digest_challenge,
                                     'basic':self.basic_challenge, 
                                     'form': self.form_challenge}[default]
     self.out = None
     self.response_length = 0
     self.response_cookies = []
     self.response_producer = None
     self.outgoing_producers = asynchat.fifo()
Esempio n. 3
0
    def _process_file_read(self, path, request):
        file_length = self._filesystem.stat(path)[stat.ST_SIZE]
        last_modified = self._filesystem.stat(path)[stat.ST_MTIME]
        ims_header = request.get_header_match(_IF_MODIFIED_SINCE)
        user_agent = request.get_header("user-agent")
        filename = os.path.basename(path)
        if ims_header:
            length = file_length
            if ims_header.group(4):
                try:
                    length = string.atoi(ims_header.group(4))
                except:
                    pass
            ims_time = parse_http_date(ims_header.group(1))
            if (length == file_length) and (last_modified <= ims_time):
                self._cache_counter.increment()
                request["Cache-Control"] = self.get_cache_control_header(filename, user_agent)
                # Finish by calling reply() method, which queues
                # response immediately, to include cache-controls.
                request.reply(304)
                return
        try:
            file = self._openread(path, "rb")
        except IOError:
            request.error(404)
            return
        request["Cache-Control"] = self.get_cache_control_header(filename, user_agent)
        request["Last-Modified"] = build_http_date(last_modified)
        request["Content-Length"] = file_length
        self._set_content_type(path, request)
        if request.get_command() in ("get", "GET"):
            # Check if compression mode is set to True
            ext = filename.split(".")[-1].lower()
            compressable = ["html", "htm", "js", "css"]

            if self.compress and ext in compressable and self.checkRequestAcceptGzip(request):
                gzipDir = "gzipped/"
                relativeGzipPath = gzipDir + path + ".gz"
                absGzipFilePath = self._filesystem.translate(relativeGzipPath)
                absFilePath = self._filesystem.translate(path)

                last_mtime_File = self._filesystem.modified(path)
                ctime_File = self._filesystem.created(path)
                # Check whether the Gzipped requested file exists on the gzipped directory or not
                if not os.path.isfile(absGzipFilePath):
                    self.compressFile(path, relativeGzipPath)
                else:
                    # If gzipped requested file already exists
                    ctime_GzipFile = self._filesystem.created(relativeGzipPath)
                    last_mtime_GzipFile = self._filesystem.modified(relativeGzipPath)
                    # Check whether the gzipped requested file which exists is old or not
                    if (last_mtime_GzipFile != last_mtime_File) or (ctime_GzipFile != ctime_File):
                        self.compressFile(path, relativeGzipPath)
                # Send gzipped file
                gzipFile_length = self._filesystem.stat(relativeGzipPath)[stat.ST_SIZE]
                try:
                    gzipfileRead = self._filesystem.open(relativeGzipPath, "rb")
                    request["Content-Encoding"] = "gzip"
                    request["Content-Length"] = gzipFile_length
                    request.push(FileProducer(gzipfileRead))
                    self._file_counter.increment()
                    request.done()
                    return
                except Exception, e:
                    msglog.log("broadway", msglog.types.INFO, "EXCEPTION in opening ZIP file in R mode: %r" % e)
Esempio n. 4
0
    def _process_file_read(self, path, request):
        file_length = self._filesystem.stat(path)[stat.ST_SIZE]
        last_modified = self._filesystem.stat(path)[stat.ST_MTIME]
        ims_header = request.get_header_match(_IF_MODIFIED_SINCE)
        user_agent = request.get_header('user-agent')
        filename = os.path.basename(path)
        if ims_header:
            length = file_length
            if ims_header.group(4):
                try:
                    length = string.atoi(ims_header.group(4))
                except:
                    pass
            ims_time = parse_http_date(ims_header.group(1))
            if (length == file_length) and (last_modified <= ims_time):
                self._cache_counter.increment()
                request['Cache-Control'] = self.get_cache_control_header(
                    filename, user_agent)
                # Finish by calling reply() method, which queues
                # response immediately, to include cache-controls.
                request.reply(304)
                return
        try:
            file = self._openread(path, 'rb')
        except IOError:
            request.error(404)
            return
        request['Cache-Control'] = self.get_cache_control_header(
            filename, user_agent)
        request['Last-Modified'] = build_http_date(last_modified)
        request['Content-Length'] = file_length
        self._set_content_type(path, request)
        if request.get_command() in ('get', 'GET'):
            #Check if compression mode is set to True
            ext = filename.split('.')[-1].lower()
            compressable = ['html', 'htm', 'js', 'css']

            if self.compress and ext in compressable and self.checkRequestAcceptGzip(
                    request):
                gzipDir = "gzipped/"
                relativeGzipPath = gzipDir + path + '.gz'
                absGzipFilePath = self._filesystem.translate(relativeGzipPath)
                absFilePath = self._filesystem.translate(path)

                last_mtime_File = self._filesystem.modified(path)
                ctime_File = self._filesystem.created(path)
                #Check whether the Gzipped requested file exists on the gzipped directory or not
                if not os.path.isfile(absGzipFilePath):
                    self.compressFile(path, relativeGzipPath)
                else:
                    #If gzipped requested file already exists
                    ctime_GzipFile = self._filesystem.created(relativeGzipPath)
                    last_mtime_GzipFile = self._filesystem.modified(
                        relativeGzipPath)
                    #Check whether the gzipped requested file which exists is old or not
                    if ((last_mtime_GzipFile != last_mtime_File)
                            or (ctime_GzipFile != ctime_File)):
                        self.compressFile(path, relativeGzipPath)
                #Send gzipped file
                gzipFile_length = self._filesystem.stat(relativeGzipPath)[
                    stat.ST_SIZE]
                try:
                    gzipfileRead = self._filesystem.open(
                        relativeGzipPath, 'rb')
                    request['Content-Encoding'] = 'gzip'
                    request['Content-Length'] = gzipFile_length
                    request.push(FileProducer(gzipfileRead))
                    self._file_counter.increment()
                    request.done()
                    return
                except Exception, e:
                    msglog.log(
                        "broadway", msglog.types.INFO,
                        "EXCEPTION in opening ZIP file in R mode: %r" % e)