示例#1
0
        def build_response(response):
            cookies = cookie_utils.CookieSession()
            cookies.extract_cookies_to_jar(response.request, response)

            encoding = utils.find_encoding(response.body, response.headers)
            if not response.headers.get('content-type'):
                response.headers['content-type'] = 'text/plain'
            if 'charset=' not in response.headers.get('content-type', ''):
                response.headers['content-type'] += '; charset='+encoding

            return dict(
                    status = response.code,
                    statusText = response.reason,
                    headers = build_headers(response.headers),
                    cookies = cookies.to_json(),
                    content = dict(
                        size = len(response.body),
                        mimeType = response.headers.get('content-type'),
                        text = base64.b64encode(response.body),
                        decoded = utils.decode(response.body, response.headers),
                        ),
                    redirectURL = response.headers.get('Location'),
                    headersSize = -1,
                    bodySize = -1,
                    )
示例#2
0
文件: fetcher.py 项目: ysice/qiandao
        def build_response(response):
            cookies = cookie_utils.CookieSession()
            cookies.extract_cookies_to_jar(response.request, response)

            encoding = utils.find_encoding(response.body, response.headers)
            if not response.headers.get('content-type'):
                response.headers['content-type'] = 'text/plain'
            if 'charset=' not in response.headers.get('content-type', ''):
                response.headers['content-type'] += '; charset='+encoding

            return dict(
                    status = response.code,
                    statusText = response.reason,
                    headers = build_headers(response.headers),
                    cookies = cookies.to_json(),
                    content = dict(
                        size = len(response.body),
                        mimeType = response.headers.get('content-type'),
                        text = base64.b64encode(response.body),
                        decoded = utils.decode(response.body, response.headers),
                        ),
                    redirectURL = response.headers.get('Location'),
                    headersSize = -1,
                    bodySize = -1,
                    )
示例#3
0
文件: subscribe.py 项目: zdqd/qiandao
    async def get(self, userid):
        msg = ''
        user = self.current_user
        adminflg = False
        if (user['id'] == int(userid)) and (user['role'] == u'admin'):
            adminflg = True
        repos = json.loads(self.db.site.get(1, fields=('repos'))['repos'])
        try:
            if proxies:
                proxy = random.choice(proxies)
            else:
                proxy = {}
            now_ts = int(time.time())
            # 如果上次更新时间大于1天则更新模板仓库
            if (now_ts - int(repos['lastupdate']) > 24 * 3600):
                for repo in repos['repos']:
                    if repo['repoacc']:
                        url = '{0}@{1}'.format(
                            repo['repourl'].replace(
                                'https://github.com/',
                                'https://cdn.jsdelivr.net/gh/'),
                            repo['repobranch'])
                    else:
                        if (repo['repourl'].find('https://github.com/') > -1):
                            url = '{0}/{1}'.format(
                                repo['repourl'].replace(
                                    'https://github.com/',
                                    'https://raw.githubusercontent.com/'),
                                repo['repobranch'])
                        else:
                            url = repo['repourl']

                    hfile_link = url + '/tpls_history.json'
                    obj = {
                        'request': {
                            'method': 'GET',
                            'url': hfile_link,
                            'headers': [],
                            'cookies': []
                        },
                        'rule': {
                            'success_asserts': [],
                            'failed_asserts': [],
                            'extract_variables': []
                        },
                        'env': {
                            'variables': {},
                            'session': []
                        }
                    }
                    _, _, res = await gen.convert_yielded(
                        fetcher.build_response(obj=obj, proxy=proxy))
                    if res.code == 200:
                        hfile = json.loads(
                            res.body.decode(
                                find_encoding(res.body, res.headers),
                                'replace'))
                        for har in hfile['har'].values():
                            if (har['content'] == ''):
                                obj['request']['url'] = "{0}/{1}".format(
                                    url, quote(har['filename']))
                                _, _, har_res = await gen.convert_yielded(
                                    fetcher.build_response(obj, proxy=proxy))
                                if har_res.code == 200:
                                    har['content'] = base64.b64encode(
                                        har_res.body).decode()
                                else:
                                    msg += '{pre}\r\n打开链接错误{link}\r\n'.format(
                                        pre=msg, link=obj['request']['url'])
                                    continue

                            for k, v in repo.items():
                                har[k] = v
                            tpl = self.db.pubtpl.list(
                                name=har['name'],
                                reponame=har['reponame'],
                                repourl=har['repourl'],
                                repobranch=har['repobranch'],
                                fields=('id', 'name', 'version'))

                            if (len(tpl) > 0):
                                if (int(tpl[0]['version']) < int(
                                        har['version'])):
                                    har['update'] = True
                                    self.db.pubtpl.mod(tpl[0]['id'], **har)
                            else:
                                self.db.pubtpl.add(har)
                    else:
                        msg += '{pre}\r\n打开链接错误{link}\r\n'.format(
                            pre=msg, link=obj['request']['url'])
            repos["lastupdate"] = now_ts
            self.db.site.mod(1,
                             repos=json.dumps(repos,
                                              ensure_ascii=False,
                                              indent=4))

            if msg:
                raise Exception(msg)

            tpls = self.db.pubtpl.list()

            await self.render('pubtpl_subscribe.html',
                              tpls=tpls,
                              user=user,
                              userid=user['id'],
                              adminflg=adminflg,
                              repos=repos['repos'],
                              msg=msg)
            return

        except Exception as e:
            traceback.print_exc()
            user = self.current_user
            tpls = self.db.pubtpl.list()
            await self.render('pubtpl_subscribe.html',
                              tpls=tpls,
                              user=user,
                              userid=user['id'],
                              adminflg=adminflg,
                              repos=repos['repos'],
                              msg=str(e))
            return