def navigation_visit(self, request, navigation): '''Accessed by the :mod:`lux.extensions.sitemap` extension. It adds the ``edit`` or ``exit`` links to the user links if the user is authenticated and has valid permissions. ''' if request.cache.session: user = request.cache.session.user if user.is_authenticated(): edit = self.get_route('edit') if request.cache.app_handler == edit: route = self.get_route('exit edit') if route: url = iri_to_uri(route.path(**request.urlargs), request.url_data) navigation.user.insert( 0, navigation.item(url, icon=self.exit_icon, text='exit')) else: page = request.cache.page if page: url = iri_to_uri(edit.path(id=page.id), request.urlargs) navigation.user.insert( 0, navigation.item(url, icon=self.edit_icon, text='edit'))
async def upload(self, release, filename, content_type=None): """Upload a file to a release :param filename: filename to upload :param content_type: optional content type :return: json object from github """ release = self.as_id(release) name = os.path.basename(filename) if not content_type: content_type, _ = mimetypes.guess_type(name) if not content_type: raise ValueError('content_type not known') inputs = {'name': name} url = '%s%s/%s/assets' % (self.uploads_url, urlsplit( self.api_url).path, release) url = iri_to_uri(url, inputs) info = os.stat(filename) size = info[stat.ST_SIZE] response = await self.http.post(url, data=stream_upload(filename), auth=self.auth, headers={ 'content-type': content_type, 'content-length': str(size) }) response.raise_for_status() return response.json()
def test_wsgi_environ(path='/', method=None, headers=None, extra=None, secure=False, loop=None): '''An function to create a WSGI environment dictionary for testing. :param url: the resource in the ``PATH_INFO``. :param method: the ``REQUEST_METHOD``. :param headers: optional request headers :params secure: a secure connection? :param extra: additional dictionary of parameters to add. :return: a valid WSGI environ dictionary. ''' parser = http_parser(kind=0) method = (method or 'GET').upper() path = iri_to_uri(path) data = '%s %s HTTP/1.1\r\n\r\n' % (method, path) data = data.encode('latin1') parser.execute(data, len(data)) request_headers = Headers(headers, kind='client') # Add Host if not available parsed = urlparse(path) if parsed.netloc and 'host' not in request_headers: request_headers['host'] = parsed.netloc # headers = Headers() stream = StreamReader(request_headers, parser) extra = extra or {} extra['pulsar.connection'] = FakeConnection(loop=loop) return wsgi_environ(stream, ('127.0.0.1', 8060), '777.777.777.777:8080', headers, https=secure, extra=extra)
def test_wsgi_environ(path=None, method=None, headers=None, extra=None, secure=False, loop=None, body=None): '''An function to create a WSGI environment dictionary for testing. :param url: the resource in the ``PATH_INFO``. :param method: the ``REQUEST_METHOD``. :param headers: optional request headers :params secure: a secure connection? :param extra: additional dictionary of parameters to add. :return: a valid WSGI environ dictionary. ''' parser = http_parser(kind=0) method = (method or 'GET').upper() path = iri_to_uri(path or '/') request_headers = Headers(headers, kind='client') # Add Host if not available parsed = urlparse(path) if 'host' not in request_headers: if not parsed.netloc: path = '%s%s' % ('https://:443' if secure else 'http://:80', path) else: request_headers['host'] = parsed.netloc # data = '%s %s HTTP/1.1\r\n\r\n' % (method, path) data = data.encode('latin1') parser.execute(data, len(data)) # stream = io.BytesIO(body or b'') return wsgi_environ(stream, parser, request_headers, ('127.0.0.1', 8060), '255.0.1.2:8080', Headers(), https=secure, extra=extra)
def test_wsgi_environ(path=None, method=None, headers=None, extra=None, secure=False, loop=None, body=None): """An function to create a WSGI environment dictionary for testing. :param url: the resource in the ``PATH_INFO``. :param method: the ``REQUEST_METHOD``. :param headers: optional request headers :params secure: a secure connection? :param extra: additional dictionary of parameters to add. :return: a valid WSGI environ dictionary. """ parser = http_parser(kind=0) method = (method or "GET").upper() path = iri_to_uri(path or "/") request_headers = Headers(headers, kind="client") # Add Host if not available parsed = urlparse(path) if "host" not in request_headers: if not parsed.netloc: path = "%s%s" % ("https://:443" if secure else "http://:80", path) else: request_headers["host"] = parsed.netloc # data = "%s %s HTTP/1.1\r\n\r\n" % (method, path) data = data.encode("latin1") parser.execute(data, len(data)) # stream = io.BytesIO(body or b"") return wsgi_environ( stream, parser, request_headers, ("127.0.0.1", 8060), "255.0.1.2:8080", Headers(), https=secure, extra=extra )
def link(self, request, offset, limit): params = request.url_data.copy() cfg = request.config params.update({cfg['API_OFFSET_KEY']: offset, cfg['API_LIMIT_KEY']: limit}) location = iri_to_uri(request.path, params) return request.absolute_uri(location)
async def upload(self, release, filename, content_type=None): """Upload a file to a release :param filename: filename to upload :param content_type: optional content type :return: json object from github """ release = self.as_id(release) name = os.path.basename(filename) if not content_type: content_type, _ = mimetypes.guess_type(name) if not content_type: raise ValueError('content_type not known') inputs = {'name': name} url = '%s%s/%s/assets' % (self.uploads_url, urlsplit(self.api_url).path, release) url = iri_to_uri(url, inputs) info = os.stat(filename) size = info[stat.ST_SIZE] response = await self.http.post( url, data=stream_upload(filename), auth=self.auth, headers={'content-type': content_type, 'content-length': str(size)}) response.raise_for_status() return response.json()
def test_wsgi_environ(path=None, method=None, headers=None, extra=None, secure=False, loop=None, body=None): '''An function to create a WSGI environment dictionary for testing. :param url: the resource in the ``PATH_INFO``. :param method: the ``REQUEST_METHOD``. :param headers: optional request headers :params secure: a secure connection? :param extra: additional dictionary of parameters to add. :return: a valid WSGI environ dictionary. ''' parser = http_parser(kind=0) method = (method or 'GET').upper() path = iri_to_uri(path or '/') request_headers = Headers(headers, kind='client') # Add Host if not available parsed = urlparse(path) if 'host' not in request_headers: if not parsed.netloc: scheme = ('https' if secure else 'http') path = '%s://127.0.0.1%s' % (scheme, path) else: request_headers['host'] = parsed.netloc # data = '%s %s HTTP/1.1\r\n\r\n' % (method, path) data = data.encode('latin1') parser.execute(data, len(data)) # stream = io.BytesIO(body or b'') return wsgi_environ(stream, parser, request_headers, ('127.0.0.1', 8060), '255.0.1.2:8080', Headers(), https=secure, extra=extra)
def test_wsgi_environ(path=None, method=None, headers=None, extra=None, secure=False, loop=None, body=None): '''An function to create a WSGI environment dictionary for testing. :param url: the resource in the ``PATH_INFO``. :param method: the ``REQUEST_METHOD``. :param headers: optional request headers :params secure: a secure connection? :param extra: additional dictionary of parameters to add. :return: a valid WSGI environ dictionary. ''' parser = http_parser(kind=0) method = (method or 'GET').upper() path = iri_to_uri(path or '/') request_headers = Headers(headers, kind='client') # Add Host if not available parsed = urlparse(path) if 'host' not in request_headers: if not parsed.netloc: path = '%s%s' % ('https://:443' if secure else 'http://:80', path) else: request_headers['host'] = parsed.netloc # data = '%s %s HTTP/1.1\r\n\r\n' % (method, path) data = data.encode('latin1') parser.execute(data, len(data)) # headers = Headers() stream = StreamReader(request_headers, parser) stream.buffer = body or b'' stream.on_message_complete.set_result(None) extra = extra or {} return wsgi_environ(stream, ('127.0.0.1', 8060), '777.777.777.777:8080', headers, https=secure, extra=extra)
def test_200_get_data(self): http = self.client() response = yield from http.get(self.httpbin("get"), data={"bla": "foo"}) result = response.json() self.assertEqual(response.status_code, 200) self.assertEqual(response.headers["content-type"], "application/json; charset=utf-8") self.assertEqual(result["args"], {"bla": "foo"}) self.assertEqual(response.url, self.httpbin(iri_to_uri("get", {"bla": "foo"}))) self._check_pool(http, response)
def link(self, request, offset, limit): params = request.url_data.copy() cfg = request.config params.update({ cfg['API_OFFSET_KEY']: offset, cfg['API_LIMIT_KEY']: limit }) location = iri_to_uri(request.path, params) return request.absolute_uri(location)
def test_200_get_data(self): http = self.client() response = yield http.get(self.httpbin('get'), data={'bla': 'foo'}).on_finished result = response.json() self.assertEqual(response.status_code, 200) self.assertEqual(response.headers['content-type'], 'application/json') self.assertEqual(result['args'], {'bla': 'foo'}) self.assertEqual(response.url, self.httpbin(httpurl.iri_to_uri('get',{'bla': 'foo'}))) self._check_pool(http, response)
def test_200_get_data(self): http = self.client() r = make_async(http.get(self.httpbin('get',''), data={'bla':'foo'})) yield r r = r.result self.assertEqual(r.status_code, 200) self.assertEqual(r.response, 'OK') result = r.content_json() self.assertEqual(result['args'], {'bla':['foo']}) self.assertEqual(r.url, self.httpbin(httpurl.iri_to_uri('get/',{'bla':'foo'})))
def full_path(self, *args, **query): """Return a full path""" path = None if args: if len(args) > 1: raise TypeError("full_url() takes exactly 1 argument " "(%s given)" % len(args)) path = args[0] if not path: path = self.path elif not path.startswith("/"): path = remove_double_slash("%s/%s" % (self.path, path)) return iri_to_uri(path, query)
def test_200_get_data(self): http = self.client() response = yield from http.get(self.httpbin('get'), data={'bla': 'foo'}) result = response.json() self.assertEqual(response.status_code, 200) self.assertEqual(response.headers['content-type'], 'application/json; charset=utf-8') self.assertEqual(result['args'], {'bla': 'foo'}) self.assertEqual(response.url, self.httpbin(iri_to_uri('get', {'bla': 'foo'}))) self._check_pool(http, response)
def full_path(self, *args, **query): """Return a full path""" path = None if args: if len(args) > 1: raise TypeError("full_url() takes exactly 1 argument " "(%s given)" % len(args)) path = args[0] if not path: path = self.path elif not path.startswith('/'): path = remove_double_slash('%s/%s' % (self.path, path)) return iri_to_uri(path, query)
def absolute_uri(self, location=None, scheme=None): '''Builds an absolute URI from the ``location`` and the variables available in this request. If no location is specified, the relative URI is built from :meth:`full_path`.''' if not location or not absolute_http_url_re.match(location): location = self.full_path(location) if not scheme: scheme = self.is_secure and 'https' or 'http' base = '%s://%s' % (scheme, self.get_host()) return '%s%s' % (base, location) elif not scheme: return iri_to_uri(location) else: raise ValueError('Absolute location with scheme not valid')
def full_path(self, *args, **query): '''Return a full path''' path = None if args: if len(args) > 1: raise TypeError("full_url() takes exactly 1 argument " "(%s given)" % len(args)) path = args[0] if path is None: path = self.path if not query: query = self.url_data elif not path.startswith('/'): path = remove_double_slash('%s/%s' % (self.path, path)) return iri_to_uri(path, **query)
def navigation_visit(self, request, navigation): '''Accessed by the :mod:`lux.extensions.sitemap` extension. It adds the ``edit`` or ``exit`` links to the user links if the user is authenticated and has valid permissions. ''' if request.cache.session: user = request.cache.session.user if user.is_authenticated(): edit = self.get_route('edit') if request.cache.app_handler == edit: route = self.get_route('exit edit') if route: url = iri_to_uri(route.path(**request.urlargs), request.url_data) navigation.user.insert(0, navigation.item( url, icon=self.exit_icon, text='exit')) else: page = request.cache.page if page: url = iri_to_uri(edit.path(id=page.id), request.urlargs) navigation.user.insert(0, navigation.item( url, icon=self.edit_icon, text='edit'))
def absolute_uri(self, location=None, scheme=None, **query): """Builds an absolute URI from ``location`` and variables available in this request. If no ``location`` is specified, the relative URI is built from :meth:`full_path`. """ if not is_absolute_uri(location): location = self.full_path(location, **query) if not scheme: scheme = self.is_secure and 'https' or 'http' base = '%s://%s' % (scheme, self.get_host()) return '%s%s' % (base, location) elif not scheme: return iri_to_uri(location) else: raise ValueError('Absolute location with scheme not valid')
def absolute_uri(self, location=None, scheme=None): """Builds an absolute URI from ``location`` and variables available in this request. If no ``location`` is specified, the relative URI is built from :meth:`full_path`. """ if not location or not absolute_http_url_re.match(location): location = self.full_path(location) if not scheme: scheme = self.is_secure and "https" or "http" base = "%s://%s" % (scheme, self.get_host()) return "%s%s" % (base, location) elif not scheme: return iri_to_uri(location) else: raise ValueError("Absolute location with scheme not valid")
def absolute_uri(self, location=None, scheme=None, **query): """Builds an absolute URI from ``location`` and variables available in this request. If no ``location`` is specified, the relative URI is built from :meth:`full_path`. """ if not is_absolute_uri(location): if location or location is None: location = self.full_path(location, **query) if not scheme: scheme = self.is_secure and 'https' or 'http' base = '%s://%s' % (scheme, self.get_host()) return '%s%s' % (base, location) elif not scheme: return iri_to_uri(location) else: raise ValueError('Absolute location with scheme not valid')
def to_url(self, value): return iri_to_uri(value)