示例#1
0
 def get(self, path, include_body=True):
     self.path = self.parse_url_path(path)
     del path
     absolute_path = self.get_absolute_path(self.root, self.path)
     self.absolute_path = self.validate_absolute_path(self.root, absolute_path)
     if self.absolute_path is None:
         return
     self.modified - self.get_modified_time()
     self.set_headers()
     range_header = self.request.headers.get('Range')
     request_range = httputil._parse_request_range(range_header) if range_header else None
     size = self.get_content_size()
示例#2
0
    def get(self):
        job_ID_string = self.get_query_argument("job_id", default=None)
        client_ID_string = self.get_query_argument("client_id", default=None)

        # TODO: use validation function instead...
        if job_ID_string == None or not job_ID_string.isdigit() \
        or client_ID_string == None or not client_ID_string.isdigit():
            raise HTTPError(404)

        job_ID = int(job_ID_string)
        client_ID = int(client_ID_string)

        file_dir = self.application.settings["jobmonitorserver"]["download_dir"]
        file_name = job_ID_string + CATROBAT_FILE_EXT
        file_path = "%s/%s" % (file_dir, file_name)

        if not file_name or not os.path.exists(file_path):
            raise HTTPError(404)

        file_size = os.path.getsize(file_path)
        self.set_header('Content-Type', 'application/zip')
        self.set_header('Content-Disposition', 'attachment; filename="%s"' % file_name)

        with open(file_path, "rb") as f:
            range_header = self.request.headers.get("Range")
            request_range = None
            if range_header:
                # TODO: implement own parse request range helper method
                request_range = httputil._parse_request_range(range_header, file_size)

            if request_range:
                # TODO: support HTTP range + test
                # TODO: request_range.end
                self.set_header('Content-Range', 'bytes {}-{}/{}'.format(request_range.start, (file_size - 1), file_size))
                self.set_header('Content-Length', file_size - request_range.start + 1)#(request_range.end - request_range.start + 1))
                file.seek(request_range.start)
            else:
                self.set_header('Content-Length', file_size)

            try:
                while True:
                    write_buffer = f.read(4096) # XXX: what if file is smaller than this buffer-size?
                    if write_buffer:
                        self.write(write_buffer)
                    else:
                        self.finish()
                        _logger.info("Download of job {} finished (client: {})".format(job_ID, client_ID))
                        return
            except:
                raise HTTPError(404)

        raise HTTPError(500)
示例#3
0
    async def get(self, path, include_body=True):
        absolute_path = os.path.abspath(path)
        path = absolute_path
        if absolute_path is None:
            return

        request_range = None
        range_header = self.request.headers.get("Range")
        if range_header:
            request_range = _parse_request_range(range_header)
        if request_range:
            start, end = request_range
            await self.set_content_size(absolute_path)
            size = self.content_size
            if (start and (start >= size)) or end == 0:
                self.set_status(416)  # Range Not Satisfiable
                self.set_header("Content-Type", "text/plain")
                self.set_header("Content-Range", "bytes */%s" % (size, ))
                return
            if start and (start < 0):
                start += size
            if end and (end > size):
                end = size

            self.set_status(206)  # Partial Content
            self.set_header("Content-Range",
                            _get_content_range(start, end, size))
        else:
            start = end = None

        content = await self.get_content(absolute_path, start, end)
        for chunk in content:
            self.write(chunk)
            tornado.ioloop.IOLoop.current().add_callback(self.flush)
        print('sending success')
        tornado.ioloop.IOLoop.current().add_callback(self.finish)
示例#4
0
文件: podtube.py 项目: kaesi0/PodTube
    def get(self, audio):
        logging.info('Audio: %s (%s)', audio, self.request.remote_ip)
        mp3_file = './audio/{}.mp3'.format(audio)
        if not os.path.exists(mp3_file):
            if audio not in conversion_queue.keys():
                conversion_queue[audio] = {
                    'status': False,
                    'added': datetime.datetime.now()
                }
            while audio in conversion_queue:
                yield gen.sleep(0.5)
        request_range = None
        range_header = self.request.headers.get("Range")
        if range_header:
            # As per RFC 2616 14.16, if an invalid Range header is specified,
            # the request will be treated as if the header didn't exist.
            request_range = httputil._parse_request_range(range_header)

        size = os.stat(mp3_file).st_size
        if request_range:
            start, end = request_range
            if (start is not None and start >= size) or end == 0:
                # As per RFC 2616 14.35.1, a range is not satisfiable only: if
                # the first requested byte is equal to or greater than the
                # content, or when a suffix with length 0 is specified
                self.set_status(416)  # Range Not Satisfiable
                self.set_header("Content-Type", "audio/mpeg")
                self.set_header("Content-Range", "bytes */%s" % (size, ))
                return
            if start is not None and start < 0:
                start += size
            if end is not None and end > size:
                # Clients sometimes blindly use a large range to limit their
                # download size; cap the endpoint at the actual file size.
                end = size
            # Note: only return HTTP 206 if less than the entire range has been
            # requested. Not only is this semantically correct, but Chrome
            # refuses to play audio if it gets an HTTP 206 in response to
            # ``Range: bytes=0-``.
            if size != (end or size) - (start or 0):
                self.set_status(206)  # Partial Content
                self.set_header("Content-Range",
                                httputil._get_content_range(start, end, size))
        else:
            start = end = None
        if start is not None and end is not None:
            content_length = end - start
        elif end is not None:
            content_length = end
        elif start is not None:
            content_length = size - start
        else:
            content_length = size
        self.set_header("Accept-Ranges", "bytes")
        self.set_header("Content-Length", content_length)
        self.set_header('Content-Type', 'audio/mpeg')
        content = self.get_content(mp3_file, start, end)
        if isinstance(content, bytes):
            content = [content]
        for chunk in content:
            try:
                self.write(chunk)
                yield self.flush()
            except iostream.StreamClosedError:
                return
示例#5
0
    def view_link(self, link_id, include_body=True):
        dbo_temp_link = DboTempLink(self.application.sql_client)

        is_pass_check = True
        errorMessage = ""
        errorCode = 0

        TEMPLINK_EXPIRY_SECONDS = 4 * 60 * 60  # 4 hours.

        #logging.info("link_id:%s" % (link_id))
        if is_pass_check:
            if len(link_id) != 99:
                errorMessage = "temp link is wrong"
                errorCode = 1010
                is_pass_check = False

        temp_link_dict = None
        if is_pass_check:
            #logging.info('path %s' % (path))
            temp_link_dict = dbo_temp_link.pk_query(link_id)
            createdTime = temp_link_dict['createdTime']
            time_db = datetime.datetime.strptime(createdTime,
                                                 "%Y-%m-%d %H:%M:%S")
            #utc_datetime = datetime.datetime.now()
            utc_datetime = datetime.datetime.utcnow()
            time_diff = (utc_datetime - time_db).total_seconds()
            #print 'past: %d seconds, %d seconds is our limit' % (time_diff, TEMPLINK_EXPIRY_SECONDS)
            if time_diff > TEMPLINK_EXPIRY_SECONDS:
                errorMessage = "temp link expire"
                errorCode = 1012
                is_pass_check = False

        real_path = None
        if is_pass_check:
            dbo_metadata = None
            metadata_dict = None

            poolid = temp_link_dict['poolid']
            doc_id = temp_link_dict['doc_id']
            content_hash_old = temp_link_dict['content_hash']
            metadata_conn = self.open_metadata_db(poolid)
            dbo_metadata = DboMetadata(metadata_conn)
            metadata_dict = dbo_metadata.pk_query(doc_id)
            if not metadata_dict is None:
                if metadata_dict['content_hash'] != content_hash_old:
                    errorMessage = "file content changed"
                    errorCode = 1013
                    is_pass_check = False

                real_path = u'%s/storagepool/%s%s' % (
                    options.storage_access_point, poolid,
                    metadata_dict['path'])

        if is_pass_check:
            if not os.path.isfile(real_path):
                errorMessage = "file not exist"
                errorCode = 1014
                is_pass_check = False

        if is_pass_check:
            #print "real_path", real_path
            #self.start_stream(real_path, include_body)
            self.absolute_path = os.path.abspath(real_path)
            self.modified = self.get_modified_time()
            self.set_headers()

            request_range = None
            range_header = self.request.headers.get("Range")
            if range_header:
                # As per RFC 2616 14.16, if an invalid Range header is specified,
                # the request will be treated as if the header didn't exist.
                request_range = httputil._parse_request_range(range_header)

            size = self.get_content_size()
            if request_range:
                start, end = request_range
                if (start is not None and start >= size) or end == 0:
                    # As per RFC 2616 14.35.1, a range is not satisfiable only: if
                    # the first requested byte is equal to or greater than the
                    # content, or when a suffix with length 0 is specified
                    self.set_status(416)  # Range Not Satisfiable
                    self.set_header("Content-Type", "text/plain")
                    self.set_header("Content-Range", "bytes */%s" % (size, ))
                    return
                if start is not None and start < 0:
                    start += size
                if end is not None and end > size:
                    # Clients sometimes blindly use a large range to limit their
                    # download size; cap the endpoint at the actual file size.
                    end = size
                # Note: only return HTTP 206 if less than the entire range has been
                # requested. Not only is this semantically correct, but Chrome
                # refuses to play audio if it gets an HTTP 206 in response to
                # ``Range: bytes=0-``.
                if size != (end or size) - (start or 0):
                    self.set_status(206)  # Partial Content
                    self.set_header(
                        "Content-Range",
                        httputil._get_content_range(start, end, size))
            else:
                start = end = None

            if start is not None and end is not None:
                content_length = end - start
            elif end is not None:
                content_length = end
            elif start is not None:
                content_length = size - start
            else:
                content_length = size
            self.set_header("Content-Length", content_length)

            if include_body:
                content = self.get_content(self.absolute_path, start, end)
                if isinstance(content, bytes):
                    content = [content]
                for chunk in content:
                    try:
                        self.write(chunk)
                        yield self.flush()
                    except iostream.StreamClosedError:
                        #print "StreamClosedError"
                        return
            else:
                pass
                #assert self.request.method == "HEAD"

        else:
            if errorCode == 1012:
                self.set_status(410)
            else:
                if errorCode == 1013:
                    self.set_status(304)
                else:
                    self.set_header('Content-Type', 'application/json')
                    self.set_status(400)
                    self.write(
                        dict(error=dict(message=errorMessage, code=errorCode)))
                    logging.error('%s' % (str(
                        dict(error=dict(message=errorMessage, code=errorCode)))
                                          ))
示例#6
0
    def get(self, filepath, include_body=True):
        if not self.fs.exists(filepath):
            raise tornado.web.HTTPError(404)

        # Set up our path instance variables.
        self.filepath = filepath
        del filepath  # make sure we don't refer to filepath instead of self.filepath again

        self.set_headers()

        request_range = None
        range_header = self.request.headers.get("Range")
        if range_header:
            # As per RFC 2616 14.16, if an invalid Range header is specified,
            # the request will be treated as if the header didn't exist.
            request_range = httputil._parse_request_range(range_header)

        size = self.get_content_size()
        if request_range:
            start, end = request_range
            if (start is not None and start >= size) or end == 0:
                # As per RFC 2616 14.35.1, a range is not satisfiable only: if
                # the first requested byte is equal to or greater than the
                # content, or when a suffix with length 0 is specified
                self.set_status(416)  # Range Not Satisfiable
                self.set_header("Content-Type", "text/plain")
                self.set_header("Content-Range", "bytes */%s" % (size, ))
                return
            if start is not None and start < 0:
                start += size
            if end is not None and end > size:
                # Clients sometimes blindly use a large range to limit their
                # download size; cap the endpoint at the actual file size.
                end = size
            # Note: only return HTTP 206 if less than the entire range has been
            # requested. Not only is this semantically correct, but Chrome
            # refuses to play audio if it gets an HTTP 206 in response to
            # ``Range: bytes=0-``.
            if size != (end or size) - (start or 0):
                self.set_status(206)  # Partial Content
                self.set_header("Content-Range",
                                httputil._get_content_range(start, end, size))
        else:
            start = end = None

        if start is not None and end is not None:
            content_length = end - start
        elif end is not None:
            content_length = end
        elif start is not None:
            content_length = size - start
        else:
            content_length = size
        self.set_header("Content-Length", content_length)

        if include_body:
            content = self.get_content(start, end)
            if isinstance(content, bytes):
                content = [content]
            for chunk in content:
                try:
                    self.write(chunk)
                    yield self.flush()
                except iostream.StreamClosedError:
                    return
        else:
            assert self.request.method == "HEAD"
示例#7
0
    def get(self, path, include_body=True):
        # Set up our path instance variables.
        self.path = self.parse_url_path(path)
        del path  # make sure we don't refer to path instead of self.path again
        absolute_path = self.get_absolute_path(self.root, self.path)
        self.absolute_path = self.validate_absolute_path(
            self.root, absolute_path)
        if self.absolute_path is None:
            return

        self.modified = self.get_modified_time()
        self.set_headers()

        if self.should_return_304():
            self.set_status(304)
            return

        request_range = None
        range_header = self.request.headers.get("Range")
        if range_header:
            # As per RFC 2616 14.16, if an invalid Range header is specified,
            # the request will be treated as if the header didn't exist.
            request_range = httputil._parse_request_range(range_header)

        if request_range:
            start, end = request_range
            size = self.get_content_size()
            if (start is not None and start >= size) or end == 0:
                # As per RFC 2616 14.35.1, a range is not satisfiable only: if
                # the first requested byte is equal to or greater than the
                # content, or when a suffix with length 0 is specified
                self.set_status(416)  # Range Not Satisfiable
                self.set_header("Content-Type", "text/plain")
                self.set_header("Content-Range", "bytes */%s" %(size, ))
                return
            if start is not None and start < 0:
                start += size
            if end is not None and end > size:
                # Clients sometimes blindly use a large range to limit their
                # download size; cap the endpoint at the actual file size.
                end = size
            # Note: only return HTTP 206 if less than the entire range has been
            # requested. Not only is this semantically correct, but Chrome
            # refuses to play audio if it gets an HTTP 206 in response to
            # ``Range: bytes=0-``.
            if size != (end or size) - (start or 0):
                self.set_status(206)  # Partial Content
                self.set_header("Content-Range",
                                httputil._get_content_range(start, end, size))
        else:
            start = end = None
        content = self.get_content(self.absolute_path, start, end)
        if isinstance(content, bytes_type):
            content = [content]
        content_length = 0
        for chunk in content:
            if include_body:
                self.write(chunk)
            else:
                content_length += len(chunk)
        if not include_body:
            assert self.request.method == "HEAD"
            self.set_header("Content-Length", content_length)
示例#8
0
    def get(self):
        job_ID_string = self.get_query_argument("job_id", default=None)
        client_ID_string = self.get_query_argument("client_id", default=None)

        # TODO: use validation function instead...
        if job_ID_string == None or not job_ID_string.isdigit() \
        or client_ID_string == None or not client_ID_string.isdigit():
            raise HTTPError(404)

        job_ID = int(job_ID_string)
        client_ID = int(client_ID_string)

        file_dir = self.application.settings["jobmonitorserver"][
            "download_dir"]
        file_name = job_ID_string + CATROBAT_FILE_EXT
        file_path = "%s/%s" % (file_dir, file_name)

        if not file_name or not os.path.exists(file_path):
            raise HTTPError(404)

        file_size = os.path.getsize(file_path)
        self.set_header('Content-Type', 'application/zip')
        self.set_header('Content-Disposition',
                        'attachment; filename="%s"' % file_name)

        with open(file_path, "rb") as f:
            range_header = self.request.headers.get("Range")
            request_range = None
            if range_header:
                # TODO: implement own parse request range helper method
                request_range = httputil._parse_request_range(
                    range_header, file_size)

            if request_range:
                # TODO: support HTTP range + test
                # TODO: request_range.end
                self.set_header(
                    'Content-Range',
                    'bytes {}-{}/{}'.format(request_range.start,
                                            (file_size - 1), file_size))
                self.set_header(
                    'Content-Length', file_size - request_range.start +
                    1)  #(request_range.end - request_range.start + 1))
                file.seek(request_range.start)
            else:
                self.set_header('Content-Length', file_size)

            try:
                while True:
                    write_buffer = f.read(
                        4096
                    )  # XXX: what if file is smaller than this buffer-size?
                    if write_buffer:
                        self.write(write_buffer)
                    else:
                        self.finish()
                        _logger.info(
                            "Download of job {} finished (client: {})".format(
                                job_ID, client_ID))
                        return
            except:
                raise HTTPError(404)

        raise HTTPError(500)
示例#9
0
    def get(self, absolute_path, include_body=True):
        '''
        this is mostly copied from tornado
        '''
        # Set up our path instance variables.
        assert hasattr(self, 'path')
        assert hasattr(self, 'root')
        self.absolute_path = self.validate_absolute_path(self.root, absolute_path)
        if self.absolute_path is None:
            return

        self.modified = self.get_modified_time()
        self.set_headers()

        if self.should_return_304():
            self.set_status(304)
            return

        request_range = None
        range_header = self.request.headers.get("Range")
        if range_header:
            # As per RFC 2616 14.16, if an invalid Range header is specified,
            # the request will be treated as if the header didn't exist.
            request_range = httputil._parse_request_range(range_header)

        size = self.get_content_size()
        if request_range:
            start, end = request_range
            if (start is not None and start >= size) or end == 0:
                # As per RFC 2616 14.35.1, a range is not satisfiable only: if
                # the first requested byte is equal to or greater than the
                # content, or when a suffix with length 0 is specified
                self.set_status(416)  # Range Not Satisfiable
                self.set_header("Content-Type", "text/plain")
                self.set_header("Content-Range", "bytes */%s" % (size, ))
                return
            if start is not None and start < 0:
                start += size
            if end is not None and end > size:
                # Clients sometimes blindly use a large range to limit their
                # download size; cap the endpoint at the actual file size.
                end = size
            # Note: only return HTTP 206 if less than the entire range has been
            # requested. Not only is this semantically correct, but Chrome
            # refuses to play audio if it gets an HTTP 206 in response to
            # ``Range: bytes=0-``.
            if size != (end or size) - (start or 0):
                self.set_status(206)  # Partial Content
                self.set_header("Content-Range",
                                httputil._get_content_range(start, end, size))
        else:
            start = end = None

        if start is not None and end is not None:
            content_length = end - start
        elif end is not None:
            content_length = end
        elif start is not None:
            content_length = size - start
        else:
            content_length = size
        self.set_header("Content-Length", content_length)

        if include_body:
            content = self.get_content(self.absolute_path, start, end)
            if isinstance(content, bytes_type):
                content = [content]
            for chunk in content:
                self.write(chunk)
                self.flush()
        else:
            assert self.request.method == "HEAD"
示例#10
0
    def get(self, path, include_body=True):
        """
        Copy-paste instead of calling overridden method due to the problem with attributes (check StaticFileHandler
        pydoc).
        """
        # Set up our path instance variables.
        self.swagger_file = self.root
        self.path = self.parse_url_path(path)
        del path  # make sure we don't refer to path instead of self.path again

        if self.path == "" or self.path is None:
            self.path += "index.html"
        if self.path[-4:] == 'yaml' or self.path[-3:] == 'yml':
            self.absolute_path = self.swagger_file
        else:
            self.root = os.path.dirname(__file__) + "/static/"
            absolute_path = self.get_absolute_path(self.root, self.path)
            self.absolute_path = self.validate_absolute_path(
                self.root, absolute_path)
        if self.absolute_path is None:
            return

        self.modified = self.get_modified_time()
        self.set_headers()

        if self.should_return_304():
            self.set_status(304)
            return

        request_range = None
        range_header = self.request.headers.get("Range")
        if range_header:
            # As per RFC 2616 14.16, if an invalid Range header is specified,
            # the request will be treated as if the header didn't exist.
            request_range = httputil._parse_request_range(range_header)

        size = self.get_content_size()
        if request_range:
            start, end = request_range
            if (start is not None and start >= size) or end == 0:
                # As per RFC 2616 14.35.1, a range is not satisfiable only: if
                # the first requested byte is equal to or greater than the
                # content, or when a suffix with length 0 is specified
                self.set_status(416)  # Range Not Satisfiable
                self.set_header("Content-Type", "text/plain")
                self.set_header("Content-Range", "bytes */%s" % (size, ))
                return
            if start is not None and start < 0:
                start += size
            if end is not None and end > size:
                # Clients sometimes blindly use a large range to limit their
                # download size; cap the endpoint at the actual file size.
                end = size
            # Note: only return HTTP 206 if less than the entire range has been
            # requested. Not only is this semantically correct, but Chrome
            # refuses to play audio if it gets an HTTP 206 in response to
            # ``Range: bytes=0-``.
            if size != (end or size) - (start or 0):
                self.set_status(206)  # Partial Content
                self.set_header("Content-Range",
                                httputil._get_content_range(start, end, size))
        else:
            start = end = None

        if start is not None and end is not None:
            content_length = end - start
        elif end is not None:
            content_length = end
        elif start is not None:
            content_length = size - start
        else:
            content_length = size
        self.set_header("Content-Length", content_length)

        if include_body:
            content = self.get_content(self.absolute_path, start, end)
            if isinstance(content, bytes_type):
                content = [content]
            for chunk in content:
                self.write(chunk)
                yield self.flush()
        else:
            assert self.request.method == "HEAD"