示例#1
0
    def getLocalCache(self, replace_fn, head_only):
        log.info("cache redirected from %s to %s", self.path, replace_fn)
        log.debug("Dumping client headers:")
        for h in self.headers:
                log.debug("    %s: %s", h, self.headers[h])
        try:
            file_length = self.getFileLength(replace_fn)
            log.debug("file length: %d bytes", file_length)
            start, end = 0, file_length - 1
            lastModString = os.path.getmtime(replace_fn)
            lastModString = datetime.datetime.utcfromtimestamp(lastModString)
            lastModString = lastModString.strftime('%a, %d %b %Y %H:%M:%S GMT')
            dateString = os.path.getctime(replace_fn)
            dateString = datetime.datetime.utcfromtimestamp(dateString)
            dateString = dateString.strftime('%a, %d %b %Y %H:%M:%S GMT')
        except IOError as e:
            self.send_error(500, "Internal Server Error")
            return

        if 'Range' in self.headers:
            try:
                " 在http协议中,start和end为[start, end] 范围从0开始到文件大小-1 "
                start, end = self.getFileRange(replace_fn, self.headers['Range'], file_length)
            except RangeError as e:
                """ rfc2616:  A server sending a response with status code 416
                (Requested range not satisfiable) SHOULD include a Content-Range
                field with a byte-range- resp-spec of "*".
                The instance-length specifies the current length of the selected resource. """
                self.send_response(416, 'Requested Range Not Satisfiable')
                self.send_header("Content-Range", "bytes */%d" % (file_length))
                return

            self.send_response(206, "Partial Content")
            self.send_header("Content-Range", "bytes %d-%d/%d" % (start, end, file_length))
        else:
            self.send_response(200, "OK")

        content_length = end - start + 1
        self.send_header("Accept-Ranges", "bytes")
        self.send_header("Content-Type", "application/octet-stream")
        self.send_header("Content-Length", "%d" % (content_length))
        self.send_header("Connection", "close")
        self.send_header("Last-Modified", lastModString)
        self.send_header("Date", dateString)
        self.end_headers()

        if head_only:
            return

        log.debug("Range: from %u to %d", start, end)

        with open(replace_fn, "rb") as fd:
            if fallocate:
                log.debug("posix_fadvise: start from %d bytes, %d bytes length", start, content_length)
                fallocate.posix_fadvise(fd, start, content_length, fallocate.POSIX_FADV_SEQUENTIAL | fallocate.POSIX_FADV_WILLNEED)
            self._file_read_write(fd, start, end)
示例#2
0
文件: main.py 项目: dnasdw/vitaproxy
def main():
    try:
        config.load_configure()
    except:
        config.save_configure()

    parse_arguments(sys.argv)

    log.init_logger()

    log.debug("Dumping configure")
    log.debug(CONF)
    log.info("Any clients will be served...")

    proxy_server.start_server()
示例#3
0
    def _file_read_write(self, fd, start, end):
        offset = start
        content_length = end - start + 1

        if CONF['showSpeed']:
            tm_a = [time.time(), offset - start]
            tm_b = [time.time(), offset - start]

        fd.seek(offset)
        try:
            while content_length > 0:
                if sendfile:
                    sent = min(content_length, SENDFILE_MAXSIZE)
                    log.debug("sendfile: offset / size: %d / %d bytes", offset, sent)
                    sent = sendfile(self.connection.fileno(), fd.fileno(), offset, sent)
                    if sent <= 0:
                        break
                else:
                    data = fd.read(min(content_length, CONF['bufSize']))
                    if not data:
                        break

                    self.connection.send(data)
                    sent = len(data)

                offset += sent
                content_length -= sent

                if CONF['showSpeed']:
                    tm_b = [time.time(), offset - start]
                    delta = tm_b[0] - tm_a[0]
                    rest = end - offset + 1

                    if delta >= CONF['updateInterval'] or rest == 0:
                        speed = (tm_b[1] - tm_a[1]) / delta
                        log.info("SPD: %.2fKB/S, XFER / TODO: %d / %d bytes, ETA %d sec"
                                 % (speed / 1000, offset - start, rest, rest / speed))
                        tm_a = tm_b
        except (OSError, socket.error) as e:
            log.error("Connection dropped, %d bytes sent, reason: %s", offset - start, str(e))
示例#4
0
    def load_cache_list(self, fn):
        if not os.path.exists(fn):
            with open(fn, "w") as f:
                f.write("# format in cache.txt:\n\n")
                f.write("# replace_url->replace_file_path\n")
                f.write("# search:replace_filename->replace_file_path\n")
                f.write("# re:replace_url_regular_expression->replace_file_path\n\n")
                f.write("# if replace_file_path is relative then it will be lookup from download directory\n")
        try:
            with open(fn, "r") as f:
                for l in f:
                    l = l.decode(errors='ignore').strip()

                    if not l or l[0] == '#':
                        continue

                    if '->' in l:
                        url, fn = l.split('->')
                    else:
                        url = l
                        filename = url.split("?")[0].split("/")[-1]
                    if not fn:
                        continue

                    " fn如为相对路径名应加上下载目录路径 "
                    if not os.path.isabs(fn):
                        fn = os.path.join(CONF['downloadDIR'], fn)

                    try:
                        open(fn).close()
                        self.replace_dict[url] = fn
                    except IOError as e:
                        log.error("%s: %s", fn, str(e))

            log.info("%d local caches loaded" % (len(self.replace_dict)))
            log.debug("Dumping cache list:")
            log.debug(self.replace_dict)
        except IOError as e:
            log.error(str(e))
示例#5
0
 def send_header(self, keyword, value):
     log.debug("    %s: %s", keyword, value)
     ProxyHandler.__base.send_header(self, keyword, value)