def basic_auth_cmd(user=None): if not user: user = vfunc.input('Username: '******'Password: '******'.'), basic_auth(user, password))
def basic_auth_func(): user = vfunc.input('Username: '******'Password: ') return basic_auth(user, password)
def http(): lines = vim.current.buffer[:] line, _ = vim.current.window.cursor line -= 1 headers, templates = get_headers_and_templates(lines, line) pwd_func = lambda p: vfunc.inputsecret('{}: '.format(p)) input_func = lambda p: vfunc.input('{}: '.format(p)) try: method, url, query, body, tlist, rend = prepare_request( lines, line, headers, input_func, pwd_func) except PrepareException as e: echoerr(str(e)) return rctx = RequestContext() rctx.request(method, url, query, body, headers) cwin = vim.current.window win, buf = make_scratch('__vial_http_req__', title='Request') rlines = rctx.raw_request.splitlines() hlines = [] for r in rctx.history[:-1]: hlines.append( bstr('Redirect {} from {}'.format(r.status, r.request[1]), 'utf-8')) if hlines: hlines.append(b'----------------') buf[:] = hlines + rlines win.cursor = 1, 0 win, buf = make_scratch('__vial_http_hdr__', title='Response headers') if PY2: buf[:] = [r.rstrip('\r\n') for r in rctx.response.msg.headers] else: buf[:] = [ '{}: {}'.format(*r).encode('utf-8') for r in rctx.response.msg._headers ] win.cursor = 1, 0 size = len(rctx.content) win, buf = make_scratch('__vial_http_raw__', title='Raw Response') buf[:] = rctx.content.splitlines() win.cursor = 1, 0 if PY2: rcontent_type = rctx.response.msg.gettype() else: rcontent_type = rctx.response.msg.get_content_type() content, ctype, jdata = format_content(rcontent_type, rctx.content) win, buf = make_scratch('__vial_http__') win.options['statusline'] = 'Response: {} {} {}ms {}ms {}'.format( rctx.response.status, rctx.response.reason, rctx.ctime, rctx.rtime, sizeof_fmt(size)) vim.command('set filetype={}'.format(ctype)) buf[:] = content.splitlines(False) win.cursor = 1, 0 focus_window(cwin) def set_cookies(*args): cookies = rctx.cookies args = args or sorted(cookies.keys()) return 'Cookie: ' + ';'.join('{}={}'.format(k, cookies[k]) for k in args) ctx = { 'body': content, 'json': jdata, 'headers': Headers(rctx.response.getheaders()), 'cookies': rctx.cookies, 'rcookies': rctx.rcookies, 'set_cookies': set_cookies } for t in tlist: if t in templates: lines = render_template(templates[t], **ctx).splitlines() else: lines = ['ERROR: template {} not found'.format(t)] vfunc.append(rend + 1, [''] + lines) rend += 1 + len(lines)
def http(): lines = vim.current.buffer[:] line, _ = vim.current.window.cursor line -= 1 headers, templates = get_headers_and_templates(lines, line) pwd_func = lambda p: vfunc.inputsecret('{}: '.format(p)) input_func = lambda p: vfunc.input('{}: '.format(p)) try: method, url, query, body, tlist, rend = prepare_request(lines, line, headers, input_func, pwd_func) except PrepareException as e: echoerr(str(e)) return rctx = RequestContext() rctx.request(method, url, query, body, headers) cwin = vim.current.window win, buf = make_scratch('__vial_http_req__', title='Request') rlines = rctx.raw_request.splitlines() hlines = [] for r in rctx.history[:-1]: hlines.append(bstr('Redirect {} from {}'.format( r.status, r.request[1]), 'utf-8')) if hlines: hlines.append(b'----------------') buf[:] = hlines + rlines win.cursor = 1, 0 win, buf = make_scratch('__vial_http_hdr__', title='Response headers') if PY2: buf[:] = [r.rstrip('\r\n') for r in rctx.response.msg.headers] else: buf[:] = ['{}: {}'.format(*r).encode('utf-8') for r in rctx.response.msg._headers] win.cursor = 1, 0 size = len(rctx.content) win, buf = make_scratch('__vial_http_raw__', title='Raw Response') buf[:] = rctx.content.splitlines() win.cursor = 1, 0 if PY2: rcontent_type = rctx.response.msg.gettype() else: rcontent_type = rctx.response.msg.get_content_type() content, ctype, jdata = format_content(rcontent_type, rctx.content) win, buf = make_scratch('__vial_http__') win.options['statusline'] = 'Response: {} {} {}ms {}ms {}'.format( rctx.response.status, rctx.response.reason, rctx.ctime, rctx.rtime, sizeof_fmt(size)) vim.command('set filetype={}'.format(ctype)) buf[:] = content.splitlines(False) win.cursor = 1, 0 focus_window(cwin) def set_cookies(*args): cookies = rctx.cookies args = args or sorted(cookies.keys()) return 'Cookie: ' + ';'.join('{}={}'.format(k, cookies[k]) for k in args) ctx = {'body': content, 'json': jdata, 'headers': Headers(rctx.response.getheaders()), 'cookies': rctx.cookies, 'rcookies': rctx.rcookies, 'set_cookies': set_cookies} for t in tlist: if t in templates: lines = render_template(templates[t], **ctx).splitlines() else: lines = ['ERROR: template {} not found'.format(t)] vfunc.append(rend + 1, [''] + lines) rend += 1 + len(lines)
def http(): lines = vim.current.buffer[:] line, _ = vim.current.window.cursor line -= 1 headers, templates = get_headers_and_templates(lines, line) pwd_func = lambda p: vfunc.inputsecret('{}: '.format(p)) input_func = lambda p: vfunc.input('{}: '.format(p)) try: method, url, query, body, tlist, rend = prepare_request( lines, line, headers, input_func, pwd_func) except PrepareException as e: echoerr(str(e)) return u = urlparse.urlsplit(url) if not u.hostname: host = headers.pop('host', '') if not host.startswith('http://') and not host.startswith('https://'): host = 'http://' + host u = urlparse.urlsplit(host + url) path = u.path if u.query: path += '?' + u.query if query: path += ('&' if u.query else '?') + urllib.urlencode(query) if u.scheme == 'https': import ssl cn = httplib.HTTPSConnection(u.hostname, u.port or 443, timeout=CONNECT_TIMEOUT, context=ssl._create_unverified_context()) else: cn = httplib.HTTPConnection(u.hostname, u.port or 80, timeout=CONNECT_TIMEOUT) cn = send_collector(cn) start = time.time() cn.connect() ctime = int((time.time() - start) * 1000) cn.sock.settimeout(READ_TIMEOUT) cn.request(method, path, body, headers) response = cn.getresponse() rtime = int((time.time() - start) * 1000) cwin = vim.current.window win, buf = make_scratch('__vial_http_req__', title='Request') buf[:] = cn._sdata.splitlines() win.cursor = 1, 0 win, buf = make_scratch('__vial_http_hdr__', title='Response headers') if PY2: buf[:] = [r.rstrip('\r\n') for r in response.msg.headers] else: buf[:] = [ '{}: {}'.format(*r).encode('utf-8') for r in response.msg._headers ] win.cursor = 1, 0 content = response.read() size = len(content) win, buf = make_scratch('__vial_http_raw__', title='Raw Response') buf[:] = content.splitlines() win.cursor = 1, 0 if PY2: rcontent_type = response.msg.gettype() else: rcontent_type = response.msg.get_content_type() content, ctype, jdata = format_content(rcontent_type, content) win, buf = make_scratch('__vial_http__') win.options['statusline'] = 'Response: {} {} {}ms {}ms {}'.format( response.status, response.reason, ctime, rtime, sizeof_fmt(size)) vim.command('set filetype={}'.format(ctype)) buf[:] = content.splitlines(False) win.cursor = 1, 0 focus_window(cwin) cj = Cookie.SimpleCookie() if PY2: cheaders = response.msg.getheaders('set-cookie') else: cheaders = response.msg.get_all('set-cookie') for h in cheaders or []: cj.load(h) rcookies = {k: v.value for k, v in iteritems(cj)} cookies = {k: v.coded_value for k, v in iteritems(cj)} def set_cookies(*args): args = args or sorted(cookies.keys()) return 'Cookie: ' + ';'.join('{}={}'.format(k, cookies[k]) for k in args) ctx = { 'body': content, 'json': jdata, 'headers': Headers(response.getheaders()), 'cookies': cookies, 'rcookies': rcookies, 'set_cookies': set_cookies } for t in tlist: if t in templates: lines = render_template(templates[t], **ctx).splitlines() else: lines = ['ERROR: template {} not found'.format(t)] vfunc.append(rend + 1, [''] + lines) rend += 1 + len(lines)