コード例 #1
0
def gae_urlfetch(method, url, headers, payload, getfast=None):
    # GAE 代理请求不允许设置 Host 头域
    if 'Host' in headers:
        del headers['Host']
    metadata = '%s %s HTTP/1.1\r\n' % (method, url)
    metadata += ''.join('%s: %s\r\n' % (k, v) for k, v in headers.items())
    metadata += gae_options
    if not isinstance(metadata, bytes):
        metadata = metadata.encode()
    metadata = zlib.compress(metadata)[2:-4]
    metadata = struct.pack('!h', len(metadata)) + metadata
    length = len(metadata) + int(headers.get('Content-Length', 0))
    if payload:
        if hasattr(payload, 'read'):
            payload = _PaddedFile(payload, metadata)
        else:
            if not isinstance(payload, bytes):
                payload = payload.encode()
            payload = metadata + payload
    else:
        payload = metadata
    realurl = 'GAE-' + url
    response = LimitGAE()
    _response = _gae_urlfetch(response.appid, payload, getfast, method, realurl, length)
    return response(_response)
コード例 #2
0
 def __init__(self, fp):
     self.fp = fp
     magic = fp.read(2)
     # This is a compatible, some streams has no magic.
     if magic != b'\170\234':
         fp = _PaddedFile(fp, magic)
     super().__init__(fp, zlib.decompressobj, wbits=-zlib.MAX_WBITS)
     self._buffer = None
     self._length = 0
     self._read = 0
コード例 #3
0
ファイル: decompress.py プロジェクト: zhizunbao84/GotoX
 def __init__(self, fp):
     self.fp = fp
     CMF, FLG = magic = fp.read(2)
     # This is a compatible, some streams has no magic.
     if CMF & 0x0F != 8 or \
        CMF & 0x80 != 0 or \
        ((CMF << 8) + FLG) % 31 > 0:
         fp = _PaddedFile(fp, magic)
     DecompressReader.__init__(self,
                               fp,
                               zlib.decompressobj,
                               wbits=-zlib.MAX_WBITS)
     self._buffer = None
     self._length = 0
     self._read = 0
コード例 #4
0
ファイル: CFWFetch.py プロジェクト: lshilshi/GotoX
def cfw_fetch(method, host, url, headers, payload=b'', options=None):
    set_dns()
    if url[:2] == 'ws':
        return cfw_ws_fetch(host, url, headers)
    ae = headers.get('Accept-Encoding', '')
    if 'Range' in headers and 'gzip' not in ae:
        ae += ', gzip'
        headers['Accept-Encoding'] = ae
    if 'gzip' not in ae and 'br' not in ae:
        ae = 'gzip'
    metadata = ['%s %s' % (method, url)]
    metadata += ['%s\t%s' % header for header in headers.items()]
    metadata = '\n'.join(metadata).encode()
    metadata = zlib.compress(metadata)[2:-4]
    metadata = struct.pack('!h', len(metadata)) + metadata
    length = len(metadata) + int(headers.get('Content-Length', 0))
    if payload:
        if hasattr(payload, 'read'):
            payload = _PaddedFile(payload, metadata)
        else:
            if not isinstance(payload, bytes):
                payload = payload.encode()
            payload = metadata + payload
    else:
        payload = metadata
    if options:
        _options = cfw_options.copy()
        _options.update(options)
        options_str = json.dumps(_options)
    else:
        options_str = cfw_options_str
    worker_params = get_worker_params()
    request_headers = {
        'Host': worker_params.host,
        'User-Agent': 'GotoX/ls/0.6',
        'Accept-Encoding': ae,
        'Content-Length': str(length),
        'X-Fetch-Options': options_str,
    }
    realurl = 'CFW-' + url
    while True:
        response = http_cfw.request(worker_params, payload, request_headers,
                                    connection_cache_key=worker_params.connection_cache_key,
                                    realmethod=method,
                                    realurl=realurl)
        status = check_response(response, worker_params)
        if status == 'ok':
            response.http_util = http_cfw
            response.connection_cache_key = worker_params.connection_cache_key
            if response.status == 206 and response.headers.get('Content-Encoding') == 'gzip':
                if response.headers['Content-Range'].startswith('bytes 0-'):
                    response.fp = gzipfp = GzipFile(fileobj=BytesIO(response.read()))
                    response.length = length = len(gzipfp.read())
                    response.headers.replace_header('Content-Range', f'bytes 0-{length - 1}/{length}')
                    del response.headers['Content-Encoding']
                    gzipfp.rewind()
                else:
                    response.status = 501
                    response.reason = 'Not Implemented'
                    content = b'CloudFlare Workers not support gziped response returned by range request which not starts with zero.'
                    response.fp = BytesIO(content)
                    response.length = len(content)
        elif status == 'retry':
            continue
        return response