def http_connect(host, method, path, headers=None, query_string=None, scheme="http"): if isinstance(path, text_type): try: path = path.encode('utf-8') except UnicodeError as e: logger.exception('ERROR encoding to UTF-8: %s', text_type(e)) if path.startswith(b'/'): path = quote(path) else: path = quote(b'/' + path) if scheme == "https": conn = CustomHttpsConnection(host, context=ssl._create_unverified_context()) else: conn = CustomHttpConnection(host) if query_string: path += b'?' + query_string conn.path = path conn.putrequest(method, path) if headers: for header, value in headers.items(): if isinstance(value, list): for k in value: conn.putheader(header, k) else: conn.putheader(header, value) conn.endheaders() return conn
def export(self, row_id): rows = self._get(row_id) if rows: url, content_type, title, thumb = rows[0] play_path = { 'mode': MODES.PLAY, 'player': 'false', 'history': 'false', 'path': quote(url), 'thumb': quote(thumb) } strm = kodi.get_plugin_url(play_path) if strm: log_utils.log( 'STRMUtils.export writing .m3u: |{0!s}|'.format( self.filename), log_utils.LOGDEBUG) if not PY2: strm = bytes(strm, encoding='utf-8') try: with open(self.filename, 'wb+') as f: f.write(strm) log_utils.log('STRMUtils.export writing .m3u completed.', log_utils.LOGDEBUG) kodi.notify(msg=kodi.i18n('export_success'), sound=False) return except: log_utils.log('STRMUtils.export failed to write .strm', log_utils.LOGDEBUG) kodi.notify(msg=kodi.i18n('export_fail'), sound=False) return log_utils.log('STRMUtils.export no item for export to .strm', log_utils.LOGDEBUG) kodi.notify(msg=kodi.i18n('no_items_export'), sound=False)
def inject(self, span_context, carrier): if not isinstance(carrier, dict): raise InvalidCarrierException('carrier not a collection') # Note: we do not url-encode the trace ID because the ':' separator # is not a problem for HTTP header values carrier[self.trace_id_header] = span_context_to_string( trace_id=span_context.trace_id, span_id=span_context.span_id, parent_id=span_context.parent_id, flags=span_context.flags) baggage = span_context.baggage if baggage: for key, value in six.iteritems(baggage): encoded_key = key if self.url_encoding: if six.PY2 and isinstance(value, six.text_type): encoded_value = urllib_parse.quote(value.encode('utf-8')) else: encoded_value = urllib_parse.quote(value) # we assume that self.url_encoding means we are injecting # into HTTP headers. httplib does not like unicode strings # so we convert the key to utf-8. The URL-encoded value is # already a plain string. if six.PY2 and isinstance(key, six.text_type): encoded_key = key.encode('utf-8') else: if six.PY3 and isinstance(value, six.binary_type): encoded_value = str(value, 'utf-8') else: encoded_value = value if six.PY3 and isinstance(key, six.binary_type): encoded_key = str(key, 'utf-8') # Leave the below print(), you will thank me next time you debug unicode strings # print('adding baggage', key, '=>', value, 'as', encoded_key, '=>', encoded_value) header_key = '%s%s' % (self.baggage_prefix, encoded_key) carrier[header_key] = encoded_value
def getDiff(self, branch_id, new, old=None, context_lines=None, logger=None): """See `IBranchHostingClient`.""" self._checkRevision(old) self._checkRevision(new) try: if logger is not None: if old is None: logger.info( "Requesting diff for %s from parent of %s to %s" % (branch_id, new, new)) else: logger.info("Requesting diff for %s from %s to %s" % (branch_id, old, new)) quoted_tail = "diff/%s" % quote(new, safe="") if old is not None: quoted_tail += "/%s" % quote(old, safe="") return self._get(branch_id, quoted_tail, as_json=False, params={"context_lines": context_lines}) except requests.RequestException as e: raise BranchHostingFault( "Failed to get diff from Bazaar branch: %s" % e)
def reconstruct_url(environ): secure = environ['wsgi.url_scheme'] == 'https' if secure: url = 'wss://' else: url = 'ws://' if environ.get('HTTP_HOST'): url += environ['HTTP_HOST'] else: url += environ['SERVER_NAME'] if secure: if environ['SERVER_PORT'] != '443': url += ':' + environ['SERVER_PORT'] else: if environ['SERVER_PORT'] != '80': url += ':' + environ['SERVER_PORT'] url += quote(environ.get('SCRIPT_NAME', '')) url += quote(environ.get('PATH_INFO', '')) if environ.get('QUERY_STRING'): url += '?' + environ['QUERY_STRING'] return url
def test_successful_configuration(self): """We can successfully request ProtocolDeviations""" t = self.create_request_object('Mediflex', 'Dev') self.assertEqual('datasets/SDTMDataDictionaries.csv?studyid=%s' % quote(t.studyname_environment()), t.url_path()) t = self.create_request_object('Mediflex', 'Dev', dataset_format="xml") self.assertEqual('datasets/SDTMDataDictionaries?studyid=%s' % quote(t.studyname_environment()), t.url_path())
def test_users_request(self): """Create a UsersRequest""" t = self.create_request_object() self.assertEqual('datasets/Users.odm?studyoid=%s' % quote(t.studyoid), t.url_path()) t = self.create_request_object(location_oid="1002") self.assertTrue('locationoid=%s' % quote("1002") in t.url_path()) self.assertTrue('studyoid=%s' % quote(t.studyoid) in t.url_path())
def _get_object_endpoint(self, container, obj=None, query_string=None): endpoint = urllib_parse.quote(container) if obj: endpoint = '{endpoint}/{object}'.format( endpoint=endpoint, object=urllib_parse.quote(obj)) if query_string: endpoint = '{endpoint}?{query_string}'.format( endpoint=endpoint, query_string=query_string) return endpoint
def _ldap_auth_string(ucr): # type: (dict) -> str """Build extended LDAP query URI part containing bind credentials.""" account = ucr.get('bind/binddn', ucr.get('ldap/hostdn')) pwdfile = ucr.get('bind/bindpw', '/etc/machine.secret') with open(pwdfile) as fd: return '????!bindname=%s,!x-bindpw=%s,x-tls' % ( quote(account), quote(fd.readline().rstrip()))
def _get_canonical_query(self, req): req_params = urlparse.parse_qsl(urlparse.urlparse(req.url).query, keep_blank_values=True) params = [] for key, val in sorted(req_params or []): params.append('='.join((urlparse.quote(key, safe='~-_.'), urlparse.quote(val, safe='~-_.')))) c_params = '&'.join(params) self.log.debug('canonical query: %s', c_params) return c_params
def generic(d): """Converts any dict into a URL. The domain (netlog) is always example.org, and all keys/values of the dict is turned into a querystring. >>> generic({'foo':'1', 'bar':'2'}) "http://example.org/?foo=1&bar=2" """ querystring = "&".join([quote(k) + "=" + quote(v) for (k, v) in d.items()]) return "http://example.org/?%s" % querystring
def get_url_headers(self, headers=None, cookies=None): string = '' if headers: for key in headers: string += u'{0}={1}&'.format(key, quote(u'{}'.format(headers[key]).encode('utf8'))) if cookies: string += 'Cookie=' for key in cookies: string += u'{0}%3D{1}; '.format(key, quote(u'{}'.format(cookies[key]).encode('utf8'))) return string.strip().strip('&')
def get_url_headers(self): string = '' for key in self.headers: string += '{0}={1}&'.format(key, quote(self.headers[key])) if self.cookies: string += 'Cookie=' for key in self.cookies: string += '{0}%3D{1}; '.format(key, quote(self.cookies[key])) return string.strip('&')
def construct_url(environ, with_query_string=True, with_path_info=True, script_name=None, path_info=None, querystring=None): """Reconstructs the URL from the WSGI environment. You may override SCRIPT_NAME, PATH_INFO, and QUERYSTRING with the keyword arguments. """ url = environ['wsgi.url_scheme']+'://' if environ.get('HTTP_HOST'): host = environ['HTTP_HOST'] port = None if ':' in host: host, port = host.split(':', 1) if environ['wsgi.url_scheme'] == 'https': if port == '443': port = None elif environ['wsgi.url_scheme'] == 'http': if port == '80': port = None url += host if port: url += ':%s' % port else: url += environ['SERVER_NAME'] server_port = str(environ['SERVER_PORT']) if environ['wsgi.url_scheme'] == 'https': if server_port != '443': url += ':' + server_port else: if server_port != '80': url += ':' + server_port if script_name is None: url += quote(environ.get('SCRIPT_NAME','')) else: url += quote(script_name) if with_path_info: if path_info is None: url += quote(maybe_encode(environ.get('PATH_INFO',''))) else: url += quote(maybe_encode(path_info)) if with_query_string: if querystring is None: if environ.get('QUERY_STRING'): url += '?' + environ['QUERY_STRING'] elif querystring: url += '?' + querystring return url
def remote_play(source): rpc_client = HttpJSONRPC() command = {'jsonrpc': '2.0', 'id': 1, 'method': 'Player.GetActivePlayers'} response = rpc_client.execute_rpc(command) if 'error' in response: kodi.notify(kodi.get_name(), response['error'], duration=7000) return try: player_id = response['result'][0]['playerid'] except IndexError: player_id = None if player_id == 2: # stop picture player if active, it will block command = { 'jsonrpc': '2.0', 'id': 1, 'method': 'Player.Stop', 'params': { 'playerid': player_id } } response = rpc_client.execute_rpc(command) if 'error' in response: kodi.notify(kodi.get_name(), response['error'], duration=7000) return if source['is_dash']: filename = kodi.get_plugin_url({ 'mode': MODES.PLAY, 'player': 'false', 'path': quote(source['url']), 'thumb': quote(source['art']['thumb']), 'title': quote(source['info']['title']) }) else: filename = source['url'] command = { 'jsonrpc': '2.0', 'id': 1, 'method': 'Player.Open', 'params': { 'item': { 'file': filename } } } response = rpc_client.execute_rpc(command) if 'error' in response: kodi.notify(kodi.get_name(), response['error'], duration=7000) else: if 'No Response' not in response['result']: kodi.notify(kodi.get_name(), kodi.i18n('send_success'))
def encode_fullpath(account, container, path, version, content_id): for k, v in locals().items(): if not v: raise ValueError("Can't encode fullpath: missing %s" % k) if isinstance(account, text_type): account = account.encode('utf-8') if isinstance(container, text_type): container = container.encode('utf-8') if isinstance(path, text_type): path = path.encode('utf-8') return '{0}/{1}/{2}/{3}/{4}'.format(quote(account, ''), quote(container, ''), quote(path, ''), quote(str(version), ''), quote(content_id, ''))
def edit(permission_id): """Edit existing permission.""" if not (current_user.is_admin or current_user.is_cataloging_admin): abort(403) permission = Permission.get_by_id(permission_id) if not permission: flash(_('Permission ID "%(permission_id)s" does not exist', permission_id=permission_id), 'danger') return redirect(get_redirect_target()) edit_permission_form = EditForm(current_user, permission_id, request.form) if request.method == 'POST': if edit_permission_form.validate_on_submit(): permission.update_as(current_user, **edit_permission_form.data) flash(_('Updated permissions for "%(username)s" on collection "%(code)s".', username=permission.user.email, code=permission.collection.code), 'success') return redirect(get_redirect_target()) else: flash_errors(edit_permission_form) if request.referrer and url_for('user.register') in request.referrer: new_user_id = User.query.filter_by(created_by=current_user).order_by(-User.id).all()[0].id edit_permission_form.set_defaults(permission, new_user_id=new_user_id) else: edit_permission_form.set_defaults(permission) return render_template('permissions/edit.html', permission=permission, edit_permission_form=edit_permission_form, full_path_quoted=quote(request.full_path), next_redirect_url=request.args.get('next'))
def register(user_id, collection_id): """Register new permission.""" if not (current_user.is_admin or current_user.is_cataloging_admin): abort(403) register_permission_form = RegisterForm(current_user, request.form) if request.method == 'POST': if register_permission_form.validate_on_submit(): permission = Permission.create_as( current_user, user_id=register_permission_form.user_id.data, collection_id=register_permission_form.collection_id.data, registrant=register_permission_form.registrant.data, cataloger=register_permission_form.cataloger.data, cataloging_admin=register_permission_form.cataloging_admin.data, global_registrant=register_permission_form.global_registrant.data) flash(_('Added permissions for "%(username)s" on collection "%(code)s".', username=permission.user.email, code=permission.collection.code), 'success') return redirect(get_redirect_target()) else: flash_errors(register_permission_form) if request.referrer and url_for('user.register') in request.referrer: user_id = User.query.filter_by(created_by=current_user).order_by(-User.id).all()[0].id register_permission_form.set_defaults(user_id, collection_id) return render_template('permissions/register.html', register_permission_form=register_permission_form, full_path_quoted=quote(request.full_path), next_redirect_url=get_redirect_target())
def dynamic_links_short_url(url): post_url = 'https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=%s' % b64decode( __key) data = { 'longDynamicLink': 'https://twitchaddon.page.link/?link=%s' % quote(url), 'suffix': { 'option': 'SHORT' } } headers = {'content-type': 'application/json'} request = requests.post(post_url, data=json.dumps(data), headers=headers) json_data = request.json() if 'shortLink' in json_data: return json_data['shortLink'] else: if 'error' in json_data: if 'errors' in json_data['error']: errors = '' for err in json_data['error']['errors']: errors += '%s |%s| ' % (err['message'], err['reason']) log_utils.log('Error: %s' % errors, log_utils.LOGERROR) else: log_utils.log( 'Error: %s |%s|' % (json_data['error']['code'], json_data['error']['message']), log_utils.LOGERROR) return None
def get(self, request, *args, **kwargs): if not settings.PROJECT: return Response('Project name is not set', status=HTTP_400_BAD_REQUEST) if not settings.TOKEN: return Response('Project token is not set', status=HTTP_400_BAD_REQUEST) token = request.get_argument('token', '') install_type = request.get_argument('install_type', '') if settings.WEB_BASE_URL.startswith( 'https') and not request.full_url().startswith('https'): web_base_url = 'http{}'.format(settings.WEB_BASE_URL[5:]) else: web_base_url = settings.WEB_BASE_URL if token: url = '{}/projects/register/{}'.format(web_base_url, token) else: url = '{}/projects/register'.format(web_base_url) parameters = [ ['project', settings.PROJECT], ['referrer', request.full_url().encode('utf8')], ] if install_type: parameters.append(['install_type', install_type]) query_string = '&'.join( map(lambda x: '{}={}'.format(x[0], quote(x[1])), parameters)) return RedirectResponse('%s?%s' % (url, query_string))
def setup(self, api_map=None, **kwargs): """ Set up the url with required parameters and method of the request Params: `api_map`: Dict with urls and methods for the request `kwargs`: Optional additional parameters to be attached to the url """ if api_map is None: raise Exception('Resolve must be called with `api_map` argument') elif api_map.get('url') is None or api_map.get('method') is None: raise Exception( 'Resolve must be called with a map with `url` and `method`') url = api_map['url'] method = api_map['method'] try: url = url.format(**kwargs) except KeyError as param: raise exceptions.MissingParam(param=param) # We percent encode the url so that if any string has spaces, # commas or any other special character the url will be correctly formed anyway self.url = urllib_parse.quote(url) self.method = method
def locate_objects(self, objects): reqid = self.app.request_id(self.reqid_prefix) for ct, obj, vers in objects: obj_item = '/'.join( quote(x) for x in (self.app.options.account, ct, obj, str(vers))) try: obj_md, chunks = self.storage.object_locate( self.app.options.account, ct, obj, version=vers, chunk_info=True, reqid=reqid) obj_item = encode_fullpath(self.app.options.account, ct, obj, obj_md['version'], obj_md['id']) except exceptions.NoSuchContainer as err: self.success = False self.logger.warn('Failed to locate object %s: %s', obj_item, err) # Already reported by upper level continue except exceptions.NoSuchObject as err: self.success = False yield ('rawx', obj_item, None, None, None, 'error', err) continue except Exception as err: self.success = False self.logger.warn('Failed to locate object %s: %s', obj_item, err) continue finally: reqid = self.app.request_id(self.reqid_prefix) for chunk in self.format_chunks(chunks, obj_item): yield chunk
def test_sig_def_requests(self): """Create a SignatureDefinitionsRequest""" project_name = "Mediflex(Prod)" t = self.create_request_object(project_name) self.assertEqual( 'datasets/Signatures.odm?studyid=%s' % quote(project_name), t.url_path())
def get_outgoing_url(url): """ Bounce a URL off an outgoing URL redirector, such as outgoing.prod.mozaws.net. """ if not settings.REDIRECT_URL: return url # django.utils.http._urlparse is a copy of python's urlparse() # "but uses fixed urlsplit() function". parsed_url = django_urlparse(url) url_netloc = parsed_url.netloc # This prevents a link like javascript://addons.mozilla.org... # being returned unchanged since the netloc matches the # safe list see bug 1251023 if parsed_url.scheme not in ['http', 'https']: return '/' # No double-escaping, and some domain names are excluded. if (url_netloc == django_urlparse(settings.REDIRECT_URL).netloc or url_netloc in settings.REDIRECT_URL_ALLOW_LIST): return url url = force_bytes(jinja2.utils.Markup(url).unescape()) sig = hmac.new(force_bytes(settings.REDIRECT_SECRET_KEY), msg=url, digestmod=hashlib.sha256).hexdigest() # Let '&=' through so query params aren't escaped. We probably shouldn't # bother to quote the query part at all. return '/'.join([settings.REDIRECT_URL.rstrip('/'), sig, quote(url, safe='/&=')])
def get(self, *args, **kwargs): try: session = Session() token, created = register_token(session) if not token: return if is_token_activated(session): response = Response( {'message': 'Project token is already activated'}) self.write_response(response) return if settings.WEB_BASE_URL.startswith( 'https' ) and not self.request.full_url().startswith('https'): web_base_url = 'http{}'.format(settings.WEB_BASE_URL[5:]) else: web_base_url = settings.WEB_BASE_URL url = '{}/projects/register/{}'.format(web_base_url, token.token) query_string = 'referrer={}'.format( quote(self.request.full_url().encode('utf8'))) self.redirect('%s?%s' % (url, query_string)) finally: session.close()
def setUp(self): super(ListMembersOptionsTest, self).setUp() self.domain = get_client().create_domain('example.com') self.foo_list = self.domain.create_list('foo') self.user = User.objects.create_user('testuser', '*****@*****.**', 'testpass') self.superuser = User.objects.create_superuser('testsu', '*****@*****.**', 'testpass') self.owner = User.objects.create_user('testowner', '*****@*****.**', 'testpass') self.moderator = User.objects.create_user('testmoderator', '*****@*****.**', 'testpass') self.foo_list.add_owner('*****@*****.**') self.foo_list.add_moderator('*****@*****.**') self.mm_user = get_client().create_user('*****@*****.**', '') self.mm_user.addresses[0].verify() self.foo_list.subscribe('*****@*****.**', pre_verified=True, pre_confirmed=True, pre_approved=True) self.url = reverse('list_member_options', args=( self.foo_list.list_id, '*****@*****.**', )) self.url = quote(self.url)
def basefile_to_pathfrag(self, basefile): """Given a basefile, returns a string that can safely be used as a fragment of the path for any representation of that file. The default implementation recognizes a number of characters that are unsafe to use in file names and replaces them with HTTP percent-style encoding. Example: >>> d = DocumentStore("/tmp") >>> realsep = os.sep >>> os.sep = "/" >>> d.basefile_to_pathfrag('1998:204') == '1998/%3A204' True >>> os.sep = realsep If you wish to override how document files are stored in directories, you can override this method, but you should make sure to also override :py:meth:`~ferenda.DocumentStore.pathfrag_to_basefile` to work as the inverse of this method. :param basefile: The basefile to encode :type basefile: str :returns: The encoded path fragment :rtype: str """ safe = '/;@&=+,' if sys.version_info < (2, 7, 0): # urllib.quote in python 2.6 cannot handle unicode values # for the safe parameter. FIXME: We should create a shim # as ferenda.compat.quote and use that safe = safe.encode('ascii') # pragma: no cover return quote(basefile, safe=safe).replace('%', os.sep + '%')
def get_input(self): got_input = kodi.get_keyboard(kodi.i18n('enter_for_playback'), '') if got_input: got_input = got_input.strip() got_input = quote(re.sub(r'\s+', ' ', got_input)) return got_input return ''
def home(): """Root path with login form.""" login_form = LoginForm(request.form) username = login_form.username.data # Handle logging in. if request.method == 'POST': if username and FailedLoginAttempt.too_many_recent_failures_for(username): response = make_response(render_template('429.html'), 429) response.headers['X-Username'] = username return response if login_form.validate_on_submit(): redirect_url = get_redirect_target() or url_for('user.profile') login_user(login_form.user) current_user.update_last_login() if current_user.tos_approved_at: flash(_('You are logged in.'), 'success') return redirect(redirect_url) else: return redirect( url_for('user.approve_tos') + '?next={}'.format(quote(redirect_url))) else: if username: FailedLoginAttempt(username, get_remote_addr()).save() flash_errors(login_form) response = make_response(render_template('public/home.html', login_form=login_form, next_redirect_url=get_redirect_target())) if username: response.headers['X-Username'] = username return response
def parse(src): """ Returns an element tree create by `LXML <http://lxml.de/>`_. :param src: A readable object such as a :class:`wex.response.Response`. """ if not hasattr(src, 'read'): return src etree = _ElementTree() try: stream = HTMLStream(src) # Sometimes we get URLs containing characters that aren't # acceptable to lxml (e.g. "http:/foo.com/bar?this=array[]"). # When this happens lxml will quote the whole URL. # We don't want to have to check for this so we just always # quote it here and then unquote it in the `base_url` function. quoted_base_url = quote(src.url) if src.url else src.url while True: try: parser = HTMLParser() fp = replace_invalid_ncr(stream) etree.parse(fp, parser=parser, base_url=quoted_base_url) break except UnicodeDecodeError as exc: stream.next_encoding() except IOError as exc: logger = logging.getLogger(__name__) logger.warning("IOError parsing %s (%s)", src.url, exc) root = etree.getroot() if root is None: etree._setroot(UNPARSEABLE) return etree
def HQLIST(url): try: link = utils.getHtml(url, '') except: return None match = re.compile( r'<a\s*href="([^"]+)"\s*class="image\s*featured\s*non.+?src="([^"]+)"\s*alt="([^"]+).+?data">(\d[^<]+)', re.DOTALL | re.IGNORECASE).findall(link) for url, img, name, duration in match: name = utils.cleantext(name).title() videourl = urllib_parse.quote(site.url + url, safe=':/') if img.startswith('//'): img = 'https:' + img site.add_download_link(name, videourl, 'HQPLAY', img, name, duration=duration) try: nextp = re.compile('<a href="([^"]+)"[^>]+>Next', re.DOTALL | re.IGNORECASE).findall(link) nextp = "https://www.hqporner.com" + nextp[0] site.add_dir('Next Page', nextp, 'HQLIST', site.img_next) except: pass utils.eod()
def destroy_session(self, sessionId): """ Destroys session `sessionId` """ self._DELETE('/sessions/%s' % (urllib.quote(sessionId), )) logger.debug('Successfully deleted session %s on %s:%s', sessionId, self.host, self.port)
def setup(self, api_map=None, **kwargs): """ Set up the url with required parameters and method of the request Params: `api_map`: Dict with urls and methods for the request `kwargs`: Optional additional parameters to be attached to the url """ if api_map is None: raise Exception('Resolve must be called with `api_map` argument') elif api_map.get('url') is None or api_map.get('method') is None: raise Exception('Resolve must be called with a map with `url` and `method`') url = api_map['url'] method = api_map['method'] try: url = url.format(**kwargs) except KeyError as ke: raise exceptions.MissingParam('Missing url sdk parameter: %s' % str(ke)) # We percent encode the url so that if any string has spaces, # commas or any other special character the url will be correctly formed anyway self.url = urllib_parse.quote(url) self.method = method
def encode_plot(P, pad=None, pad_inches=0.1, bbox_inches=None, remove_axes = False, transparent=False, axes_pad=None): """ Convert a plot object to base64-encoded png format. pad is passed down to matplotlib's tight_layout; pad_inches and bbox_inches to savefig. The resulting object is a base64-encoded version of the png formatted plot, which can be displayed in web pages with no further intervention. """ if PY3: from io import BytesIO as IO else: from StringIO import StringIO as IO from matplotlib.backends.backend_agg import FigureCanvasAgg from base64 import b64encode from six.moves.urllib_parse import quote virtual_file = IO() fig = P.matplotlib(axes_pad=axes_pad) fig.set_canvas(FigureCanvasAgg(fig)) if remove_axes: for a in fig.axes: a.axis('off') if pad is not None: fig.tight_layout(pad=pad) fig.savefig(virtual_file, format='png', pad_inches=pad_inches, bbox_inches=bbox_inches, transparent=transparent) virtual_file.seek(0) if PY3: buf = virtual_file.getbuffer() else: buf = virtual_file.buf return "data:image/png;base64," + quote(b64encode(buf))
def ulib(string, enc=False): try: string = urllib_parse.quote(string) if enc else urllib_parse.unquote( string) return string except: return string
def get_outgoing_url(url): """ Bounce a URL off an outgoing URL redirector, such as outgoing.prod.mozaws.net. """ if not settings.REDIRECT_URL: return url # django.utils.http._urlparse is a copy of python's urlparse() # "but uses fixed urlsplit() function". parsed_url = django_urlparse(url) url_netloc = parsed_url.netloc # This prevents a link like javascript://addons.mozilla.org... # being returned unchanged since the netloc matches the # safe list see bug 1251023 if parsed_url.scheme not in ['http', 'https']: return '/' # No double-escaping, and some domain names are excluded. if (url_netloc == django_urlparse(settings.REDIRECT_URL).netloc or url_netloc in settings.REDIRECT_URL_ALLOW_LIST): return url url = force_bytes(jinja2.utils.Markup(url).unescape()) sig = hmac.new(force_bytes(settings.REDIRECT_SECRET_KEY), msg=url, digestmod=hashlib.sha256).hexdigest() # Let '&=' through so query params aren't escaped. We probably shouldn't # bother to quote the query part at all. return '/'.join( [settings.REDIRECT_URL.rstrip('/'), sig, quote(url, safe='/&=')])
def _extractContentInfo(self, item): request = self.request rename_ids = {} if "container_rename_button" in request: for rename_id in request.get('ids', ()): rename_ids[rename_id] = rename_id elif "rename_ids" in request: for rename_id in request.get('rename_ids', ()): rename_ids[rename_id] = rename_id retitle_id = request.get('retitle_id') id, obj = item info = {} info['id'] = info['cb_id'] = id info['object'] = obj info['url'] = urllib.quote(id.encode('utf-8')) info['rename'] = rename_ids.get(id) info['retitle'] = id == retitle_id zmi_icon = queryMultiAdapter((obj, self.request), name='zmi_icon') if zmi_icon is None: info['icon'] = None else: info['icon'] = zmi_icon() dc = IZopeDublinCore(obj, None) if dc is not None: info['retitleable'] = canWrite(dc, 'title') info['plaintitle'] = not info['retitleable'] title = self.safe_getattr(dc, 'title', None) if title: info['title'] = title formatter = self.request.locale.dates.getFormatter( 'dateTime', 'short') created = self.safe_getattr(dc, 'created', None) if created is not None: info['created'] = formatter.format(created) modified = self.safe_getattr(dc, 'modified', None) if modified is not None: info['modified'] = formatter.format(modified) else: info['retitleable'] = 0 info['plaintitle'] = 1 sized_adapter = ISized(obj, None) if sized_adapter is not None: info['size'] = sized_adapter return info
def _cli_call(context, name, args): if context.output_names: with context.io_manager.with_stdout() as fp: fp.write('Item Name: ') fp.write(parse.quote(name.encode('utf8'))) fp.write('\n') fp.flush() return CLI()(context.original_main_args + args)
def testQueryEncoding(self): method_config = base_api.ApiMethodInfo(request_type_name="MessageWithTime", query_params=["timestamp"]) service = FakeService() request = MessageWithTime(timestamp=datetime.datetime(2014, 10, 0o7, 12, 53, 13)) http_request = service.PrepareHttpRequest(method_config, request) url_timestamp = urllib_parse.quote(request.timestamp.isoformat()) self.assertTrue(http_request.url.endswith(url_timestamp))
def test_build_url_proxied(self): imdb_fr = Imdb(locale="en_FR", cache=False, anonymize=True, proxy_uri="http://someproxywebsite.co.uk?url={0}") imdb_fr.timestamp = time.mktime(datetime.date.today().timetuple()) url = imdb_fr._build_url(path="/title/maindetails", params={"tconst": "tt1111111"}) expected_url = "http://someproxywebsite.co.uk?url=" + quote("https://app.imdb.com/title/maindetails") assert url.startswith(expected_url) is True
def dirnames(self): encoded = self.encode('utf-8') hexdigest = md5(encoded).hexdigest() names = [self.parsed.scheme, self.parsed.netloc] names.extend(self.parsed.path.split('/')) if self.parsed.query: names.extend(self.parsed.query.split('&')) names.append(hexdigest) return [quote(name, safe='')[:PC_NAME_MAX] for name in names]
def renderer(self, *args, **kwargs): if restricted and 'username' not in cherrypy.session: raise cherrypy.HTTPRedirect('/login?return_to=' + quote(cherrypy.request.app.script_name)) output = method(self, *args, **kwargs) if isinstance(output, dict) and output.get('jsonrpc') != '2.0': return self.env.get_template(method.__name__ + '.html').render(**output) else: return output
def test_create_a_sites_metadata_request(self): """Create a SitesMetadataRequest""" t = self.create_request_object() self.assertEqual('datasets/Sites.odm?studyoid=%s' % quote(t.studyoid), t.url_path()) t = self.create_request_object(project_name=None, environment_name=None) self.assertEqual('datasets/Sites.odm', t.url_path())
def test_can_handle_non_ascii_URLs(self): from OFS.Image import manage_addFile manage_addFile(self.app, 'täst', u'çöńtêñt'.encode('utf-8')) browser = Testing.testbrowser.Browser() browser.login('manager', 'manager_pass') browser.open('http://localhost/{}'.format(quote('täst'))) self.assertEqual(browser.contents.decode('utf-8'), u'çöńtêñt')
def _delete(self, series_type, series_val, start, end, options): params = { 'start': start.isoformat(), 'end': end.isoformat(), } params.update(options) url = '/series/%s/%s/data/' % (series_type, quote(series_val, "")) json = self.request(url, method='DELETE', params=params) return json
def test_create_audit_records_request(self): """We can create an AuditRecordsRequest""" t = self.create_request_object() self.assertTrue('datasets/ClinicalAuditRecords.odm' in t.url_path()) self.assertTrue('studyoid=%s' % quote(t.studyoid) in t.url_path()) self.assertTrue('startid=1' in t.url_path()) self.assertTrue('per_page=100' in t.url_path()) t = self.create_request_object(startid=2569, per_page=50) self.assertTrue('startid=2569' in t.url_path()) self.assertTrue('per_page=50' in t.url_path())
def test_slug_replace_no_conflict(self): path = u'http://testserver/addon/{id}/reviews/{id}345/path' self.request.path = path.format(id=self.addon.id) res = self.view(self.request, str(self.addon.id)) redirection = ( u'http://testserver/addon/{slug}/reviews/{id}345/path'.format( id=self.addon.id, slug=quote(self.addon.slug.encode('utf8')))) self.assert3xx(res, redirection, 301)
def delete_pending(self, **params): if 'person_email' not in params: raise ValueError("You must pass person_email") self._set_client(params) escaped_email = quote(params['person_email']) url = self.path % escaped_email return self.client.request('delete', url)
def quote(value, safe='/:'): """ Much like parse.quote in that it returns a URL encoded string for the given value, protecting the safe characters; but this version also ensures the value is UTF-8 encoded. """ if isinstance(value, six.text_type): value = value.encode('utf8') elif not isinstance(value, six.string_types): value = str(value) return parse.quote(value, safe)
def _ConvertIdToHeader(self, request_id): """Convert an id to a Content-ID header value. Args: request_id: String identifier for a individual request. Returns: A Content-ID header with the id_ encoded into it. A UUID is prepended to the value because Content-ID headers are supposed to be universally unique. """ return '<%s+%s>' % (self.__base_id, urllib_parse.quote(request_id))
def apply_to_request(self, req, service): parsed = urlparse.urlparse(req.url) if req.method == 'POST': # This is probably going to break when given multipart data. params = urlparse.parse_qs(req.body or '', keep_blank_values=True) else: params = urlparse.parse_qs(parsed.query, keep_blank_values=True) params = dict((key, vals[0]) for key, vals in params.iteritems()) params['AWSAccessKeyId'] = self.args['key_id'] params['SignatureVersion'] = 2 params['SignatureMethod'] = 'HmacSHA256' params['Timestamp'] = time.strftime(ISO8601, time.gmtime()) if self.args.get('security_token'): params['SecurityToken'] = self.args['security_token'] # Needed for retries so old signatures aren't included in to_sign params.pop('Signature', None) to_sign = '{method}\n{host}\n{path}\n'.format( method=req.method, host=parsed.netloc.lower(), path=(parsed.path or '/')) quoted_params = [] for key in sorted(params): val = six.text_type(params[key]) quoted_params.append(urlparse.quote(key, safe='') + '=' + urlparse.quote(val, safe='-_~')) query_string = '&'.join(quoted_params) to_sign += query_string # Redact passwords redacted_to_sign = re.sub('assword=[^&]*', 'assword=<redacted>', to_sign) self.log.debug('string to sign: %s', repr(redacted_to_sign)) signature = self.sign_string(to_sign) self.log.debug('b64-encoded signature: %s', signature) params['Signature'] = signature if req.method == 'POST': req.prepare_body(params, {}) else: req.prepare_url(_remove_params_from_url(req.url), params) return req
def count(self, query, type): assert self.access_token resp = self.s.get( 'https://api.zoomeye.org/search/count?q={}&t={}'.format( quote(query), type, ), headers={ 'Authorization': 'JWT {}'.format(self.access_token), }, ) return resp.json()
def setUp(self): super(TestAddonView, self).setUp() self.addon = addon_factory() self.func = mock.Mock() self.func.return_value = mock.sentinel.OK self.func.__name__ = 'mock_function' self.view = dec.addon_view(self.func) self.request = mock.Mock() self.slug_path = ( 'http://testserver/addon/%s/reviews' % quote(self.addon.slug.encode('utf-8'))) self.request.path = self.id_path = ( u'http://testserver/addon/%s/reviews' % self.addon.id) self.request.GET = {}
def toggle_offline(self, message=None): """Toggles the online status of this Node If the current state of this Node is "offline" it will be toggled to "online" and vice-versa. :param str message: optional descriptive message explaining the reason this node has been taken offline. """ post_cmd = self._api.url + "toggleOffline" if message is not None: post_cmd += "?offlineMessage=" + urllib_parse.quote(message) self._api.post(post_cmd)
def login_redirect(request): """Load current redirect to context.""" value = request.method == 'POST' and \ request.POST.get(REDIRECT_FIELD_NAME) or \ request.GET.get(REDIRECT_FIELD_NAME) if value: value = quote(value) querystring = REDIRECT_FIELD_NAME + '=' + value else: querystring = '' return { 'REDIRECT_FIELD_NAME': REDIRECT_FIELD_NAME, 'REDIRECT_FIELD_VALUE': value, 'REDIRECT_QUERYSTRING': querystring }
def random_blogs(rand, now, blogger): count = rand.randint(0, len(blog_title_choices) - 1) titles = rand.sample(blog_title_choices, count) for title in titles: page_url = '%(proto)s://%(subdomain)s.example.%(tld)s/%(title)s/' % { 'title': quote(title), 'tld': rand.choice(tld_choices), 'proto': rand.choice(proto_choices), 'subdomain': blogger.name.lower(), } feed_url = page_url + 'feed.xml' blog = Blog(blogger=blogger, title=title, page_url=page_url, feed_url=feed_url) db.session.add(blog)