Example #1
0
def view_title(filepath):
    """View an album's images"""
    filepath = Path(url_unquote(filepath))
    page_num = int(request.query.page or '1')
    resize = bool(int(request.query.resize or '0'))
    _path_query = '{path!s}?{query!s}' #constant for the url, to make "path/to/comic?query=data"
    
    album_path = root_path / filepath
    if is_useful_file(album_path):
        with ZipFile(str(album_path), 'r', ZIP_BZIP2) as zf:
            number_of_pages = len(list( filter(is_image_file, zf.namelist()) ))
        source = Path('/', 'static', 'compressed', filepath)
    elif is_image_album(album_path):
        number_of_pages = len(list( filter(is_image_file, album_path.iterdir()) ))
        source = Path('/', 'static', 'uncompressed', filepath)
    page_num = min(page_num, number_of_pages)
    return {
        'image' : _path_query.format(
            path = url_quote( source.as_posix() ),
            query = urlencode( {'page' : page_num, 'resize' : int(resize)} )),
        'next' : _path_query.format(
            path = url_quote( filepath.name ),
            query = urlencode( {'page' : min(number_of_pages, page_num+1), 'resize' : int(resize)} )),
        'prev' : _path_query.format(
            path = url_quote( filepath.name ),
            query = urlencode( {'page' : max(1, page_num-1), 'resize' : int(resize)} )),
        'up' : url_quote( Path('/', 'browse', filepath).parent.as_posix() + '/'),
        'resize_link' : {
            'href' : _path_query.format(
                path = url_quote( filepath.name ),
                query = urlencode( {'page' : page_num, 'resize' : int(not resize)} )),
            'title' : 'original' if resize else 'smaller',
            },
        }
Example #2
0
 def escape(text: str, safe: str = None):
     if safe is not None:
         # noinspection PyDeprecation
         return url_quote(text, safe=safe)
     else:
         # noinspection PyDeprecation
         return url_quote(text)
Example #3
0
 def authenticate(self, environ):
     """ This function takes a WSGI environment and authenticates
         the request returning authenticated user or error.
     """
     method = REQUEST_METHOD(environ)
     fullpath = url_quote(SCRIPT_NAME(environ)) + url_quote(PATH_INFO(environ))
     authorization = AUTHORIZATION(environ)
     if not authorization:
         return self.build_authentication()
     (authmeth, auth) = authorization.split(" ", 1)
     if 'digest' != authmeth.lower():
         return self.build_authentication()
     amap = dict(_auth_to_kv_pairs(auth))
     try:
         username = amap['username']
         authpath = amap['uri']
         nonce    = amap['nonce']
         realm    = amap['realm']
         response = amap['response']
         assert authpath.split("?", 1)[0] in fullpath
         assert realm == self.realm
         qop      = amap.get('qop', '')
         cnonce   = amap.get('cnonce', '')
         nc       = amap.get('nc', '00000000')
         if qop:
             assert 'auth' == qop
             assert nonce and nc
     except:
         return self.build_authentication()
     ha1 = self.authfunc(environ, realm, username)
     return self.compute(ha1, username, response, method, authpath,
                         nonce, nc, cnonce, qop)
Example #4
0
def create_link(href, base_dir, runResult=None, href_base=None):
    def get_replacements(source_file):
        return [
            ('inputfile_name', os.path.basename(source_file)),
            ('inputfile_path', os.path.dirname(source_file) or '.'),
            ('inputfile_path_abs', os.path.dirname(os.path.abspath(source_file))),
            # The following are deprecated: do not use anymore.
            ('sourcefile_name', os.path.basename(source_file)),
            ('sourcefile_path', os.path.dirname(source_file) or '.'),
            ('sourcefile_path_abs', os.path.dirname(os.path.abspath(source_file))),
        ] + ([
            ('logfile_name',     os.path.basename(runResult.log_file)),
            ('logfile_path',     os.path.dirname(os.path.relpath(runResult.log_file, href_base or '.')) or '.'),
            ('logfile_path_abs', os.path.dirname(os.path.abspath(runResult.log_file))),
        ] if runResult.log_file else [])

    source_file = os.path.relpath(runResult.task_id[0], href_base or '.') if runResult else None

    if is_url(href):
        # quote special characters only in inserted variable values, not full URL
        if source_file:
            source_file = url_quote(source_file)
            href = benchexec.util.substitute_vars(href, get_replacements(source_file))
        return href

    # quote special characters everywhere (but not twice in source_file!)
    if source_file:
        href = benchexec.util.substitute_vars(href, get_replacements(source_file))
    return url_quote(os.path.relpath(href, base_dir))
Example #5
0
def add_group(lti=lti):
    ''' Adds a group to a course'''
    # Get arguments
    course_id = request.values.get('course_id', None)
    if course_id is None:
        return jsonify(success=False, message="No course id")
    new_name = request.values.get('name', None)
    if new_name is None:
        return jsonify(success=False, message="No name given.")
    # Verify permissions
    if not g.user.is_instructor(int(course_id)):
        return jsonify(
            success=False,
            message="You are not an instructor in this group's course.")
    # Perform action
    assignment_group = AssignmentGroup.new(owner_id=g.user.id,
                                           course_id=int(course_id),
                                           name=new_name)

    menu = request.values.get('menu', "select")
    launch_type = 'lti_launch_url' if menu != 'embed' else 'iframe'
    endpoint = 'assignments.load'
    select = url_quote(
        url_for(endpoint,
                assignment_group_id=assignment_group.id,
                _external=True,
                embed=menu == 'embed')
    ) + "&return_type=" + launch_type + "&title=" + url_quote(
        assignment_group.name
    ) + "&text=BlockPy%20Exercise&width=100%25&height=600"
    # Result
    return jsonify(success=True,
                   id=assignment_group.id,
                   name=assignment_group.name,
                   select=select)
Example #6
0
def get_forecast_data(latitude, longitude):
    latitude, longitude = url_quote(str(latitude)), url_quote(str(longitude))
    url = 'https://api.forecast.io/forecast/{}/{},{}'.format(FORECAST_API_KEY, latitude, longitude)
    response = requests.get(url)
    if response.status_code == 200:
        return WeatherData(response.json())
    else:
        raise ValueError('Could not fetch weather forecast.', latitude, longitude, response)
Example #7
0
 def key_func(self, endpoint, *args, **kwargs):
     if include_host:
         return '{}__{}__{}'.format(self.host,
                                    datetime.now().strftime(date_fmt),
                                    url_quote(endpoint, ''))
     else:
         return '{}__{}'.format(datetime.now().strftime(date_fmt),
                                url_quote(endpoint, ''))
Example #8
0
def get_links(query, no_ssl):
    if no_ssl:
        search_url = "http://" + SEARCH_URL.format(url_quote(query))
    else:
        search_url = "https://" + SEARCH_URL.format(url_quote(query))
    result = get_result(search_url)
    html = pq(result)
    return [a.attrib['href'] for a in html('.l')] or \
        [a.attrib['href'] for a in html('.r')('a')]
Example #9
0
def make_tahoe_subdirectory(nodeurl, parent_writecap, name):
    url = nodeurl + "/".join([
        "uri",
        url_quote(parent_writecap),
        url_quote(unicode_to_url(name)),
    ]) + "?t=mkdir"
    resp = do_http("POST", url)
    if resp.status in (200, 201):
        return resp.read().strip()
    raise HTTPError("Error during mkdir", resp)
Example #10
0
def api_url(path, query=None):
    api_path = get_settings().get("api-path").rstrip('/')
    path = path.lstrip('/')
    query_str = "&".join(
        "{}={}".format(url_quote(str(k), safe=''), url_quote(str(v), safe=''))
        for k, v in query.items()) if query else None
    url = "{}/{}?{}".format(api_path, path,
                            query_str) if query_str else "{}/{}".format(
                                api_path, path)
    return url
Example #11
0
def get_forecast_data(latitude, longitude):
    latitude, longitude = url_quote(str(latitude)), url_quote(str(longitude))
    url = 'https://api.forecast.io/forecast/{}/{},{}'.format(
        FORECAST_API_KEY, latitude, longitude)
    response = requests.get(url)
    if response.status_code == 200:
        return WeatherData(response.json())
    else:
        raise ValueError('Could not fetch weather forecast.', latitude,
                         longitude, response)
 def _stash_urls(res):
     aliases = get_aliases(self.get_clientdir())
     node_url_file = os.path.join(self.get_clientdir(), "node.url")
     nodeurl = fileutil.read(node_url_file, mode="r").strip()
     self.welcome_url = nodeurl
     uribase = nodeurl + "uri/"
     self.tahoe_url = uribase + url_quote(aliases["tahoe"])
     self.tahoe_subdir_url = self.tahoe_url + "/subdir"
     self.two_url = uribase + url_quote(aliases["two"])
     self.two_uri = aliases["two"]
Example #13
0
def transform(soup: BeautifulSoup):
    # create section anchors
    for tag in soup.find_all('a', attrs={'class': 'headerlink'}):
        if tag.parent.name == 'h1':
            continue

        tag['name'] = '//apple_ref/cpp/Section/' + url_quote(tag.parent.contents[0])
        tag['class'] += ['dashAnchor']

    def select_section(section: str, rest: str):
        """
        At some point, the AWS CLI documentation started using <span id="section">
        instead of <div class="section" id="section">. This helper provides
        support for both at the same time.
        """

        return itertools.chain(
            soup.select(f'div.section#{section} {rest}'),
            soup.select(f'section#{section} {rest}'),
        )

    # create option anchors
    for span in select_section('options', '> p > code.docutils.literal:first-child > span'):
        anchor = soup.new_tag('a')
        anchor['name'] = '//apple_ref/cpp/Option/' + url_quote(span.string)
        anchor['class'] = ['dashAnchor']
        span.insert_before(anchor)

    # create example anchors
    for strong in select_section('examples', '> p > strong:first-child'):
        anchor = soup.new_tag('a')
        anchor['name'] = '//apple_ref/cpp/Guide/' + url_quote(strong.string)
        anchor['class'] = ['dashAnchor']
        strong.insert_before(anchor)

    # remove navbar
    soup.find('div', attrs={'class': 'navbar-fixed-top'}).clear()
    body_tag = soup.find('body')
    body_tag['style'] = body_tag.get('style', []) + ['padding-top: 0;']

    # remove sidebar
    soup.find('div', attrs={'class': 'sphinxsidebar'}).clear()
    body_div = soup.find('div', attrs={'class': 'body'})
    body_div['style'] = body_div.get('style', []) + ['width: 100%']

    # override title (drop the initial "aws" prefix)
    breadcrumbs = [
        span.string
        for span in soup.select('div.body > p:first-child a span')]
    if len(breadcrumbs) > 0 and breadcrumbs[0] == 'aws':
        breadcrumbs = breadcrumbs[1:]
    title = soup.find('title')
    breadcrumbs.append(title.string.split('—')[0].strip())
    title.string = ' '.join(breadcrumbs)
Example #14
0
    def _get_authorization(self, request, httpclient):
        uri = self.host
        uri = url_quote(uri, '').lower()
        expiry = str(self._get_expiry())

        to_sign = uri + '\n' + expiry
        signature = url_quote(_sign_string(self.key_value, to_sign, 1))

        auth_format = 'SharedAccessSignature sig={0}&se={1}&skn={2}&sr={3}'
        auth = auth_format.format(signature, expiry, self.key_name, self.host)
        return auth
Example #15
0
    def _get_authorization(self, request, httpclient):
        uri = self.host
        uri = url_quote(uri, '').lower()
        expiry = str(self._get_expiry())

        to_sign = uri + '\n' + expiry
        signature = url_quote(_sign_string(self.key_value, to_sign, 1))

        auth_format = 'SharedAccessSignature sig={0}&se={1}&skn={2}&sr={3}'
        auth = auth_format.format(signature, expiry, self.key_name, self.host)
        return auth
Example #16
0
def get_select_menu_link(id, title, is_embedded, is_group):
    launch_type = 'iframe' if is_embedded else 'lti_launch_url'
    base_url = url_quote(
        url_for('assignments.load',
                assignment_group_id=id,
                _external=True,
                embed=is_embedded))
    return '&'.join([
        base_url, "return_type=" + launch_type, "title=" + url_quote(title),
        "text=BlockPy%20Exercise", "width=100%25", "height=600"
    ])
Example #17
0
def create_link(runResult, base_dir, column):
    source_file = runResult.task_id[0]
    href = column.href or runResult.log_file

    if href.startswith("http://") or href.startswith("https://") or href.startswith("file:"):
        # quote special characters only in inserted variable values, not full URL
        source_file = url_quote(source_file)
        href = model.substitute_vars([href], None, source_file)[0]
        return href

    # quote special characters everywhere (but not twice in source_file!)
    href = model.substitute_vars([href], None, source_file)[0]
    return url_quote(os.path.relpath(href, base_dir))
Example #18
0
 def test_get_uri_for_bounty_submission(self):
     """ tests that the get_uri_for_bounty_submission method works
         as expected.
     """
     self.client = BugcrowdClient('api-token')
     submission = get_example_submission()
     submission['bounty_code'] = '<bounty_code>'
     submission['reference_number'] = '<reference_number>'
     expected_uri = 'https://tracker.bugcrowd.com/%s/submissions/%s' % (
         url_quote(submission['bounty_code']),
         url_quote(submission['reference_number']))
     self.assertEqual(get_uri_for_bounty_submission(submission),
                      expected_uri)
def prompt_for_code(request):
    ''' Prompt user for the verification code for the message user wants to verify.'''

    response = None
    form = VerifyMessageForm()
    form_template = 'mail/verify_message.html'
    if request.method == 'POST':
        form = VerifyMessageForm(request.POST)
        if form.is_valid():
            template = 'mail/verified_decrypted.html'
            log_message('verification code: {}'.format(
                form.cleaned_data['verification_code']))
            params, status = get_crypted_params(
                request.user.email, form.cleaned_data['verification_code'])
            if 'error_message' in params and params[
                    'error_message'] is not None:
                log_message('retry verification code: {}'.format(
                    url_quote(form.cleaned_data['verification_code'])))
                retry_params, __ = get_crypted_params(
                    request.user.email,
                    url_quote(form.cleaned_data['verification_code']))
                if 'error_message' in retry_params and retry_params[
                        'error_message'] is None:
                    params = retry_params
                log_message('retry params: {}'.format(retry_params))
            elif status == ENCRYPTED_STATUS:
                template = 'mail/verified_encrypted.html'
                log_message('using encrypted verification page')

            response = render_to_response(
                template, params, context_instance=RequestContext(request))
        else:
            log_message('form not valid')

        if response is None:
            log_message('post: {}'.format(request.POST))

    if response is None:
        log_message(
            'no response for verifying message crypted so redisplaying main page'
        )
        params = {
            'form': form,
            'main_headline': 'Verify Message',
            'url': 'verify_crypted'
        }
        response = render_to_response(form_template,
                                      params,
                                      context_instance=RequestContext(request))

    return response
def _update_request(request):
    # Verify body
    if request.body:
        request.body = _get_data_bytes_or_stream_only('request.body', request.body)
        length = _len_plus(request.body)

        # only scenario where this case is plausible is if the stream object is not seekable.
        if length is None:
            raise ValueError(_ERROR_VALUE_SHOULD_BE_SEEKABLE_STREAM)

        # if it is PUT, POST, MERGE, DELETE, need to add content-length to header.
        if request.method in ['PUT', 'POST', 'MERGE', 'DELETE']:
            request.headers['Content-Length'] = str(length)

    # append addtional headers based on the service
    request.headers['x-ms-version'] = X_MS_VERSION
    request.headers['User-Agent'] = USER_AGENT_STRING
    request.headers['x-ms-client-request-id'] = str(uuid.uuid1())

    # If the host has a path component (ex local storage), move it
    path = request.host.split('/', 1)
    if len(path) == 2:
        request.host = path[0]
        request.path = '/{}{}'.format(path[1], request.path)

    # Encode and optionally add local storage prefix to path
    request.path = url_quote(request.path, '/()$=\',~')
Example #21
0
def _get_links(query):
    search_engine = os.getenv('HOWDOI_SEARCH_ENGINE', 'google')
    search_url = _get_search_url(search_engine).format(URL, url_quote(query))

    logging.info('Searching %s with URL: %s', search_engine, search_url)

    try:
        result = _get_result(search_url)
    except requests.HTTPError:
        logging.info('Received HTTPError')
        result = None
    if not result or _is_blocked(result):
        logging.error(
            '%sUnable to find an answer because the search engine temporarily blocked the request. '
            'Attempting to use a different search engine.%s', RED, END_FORMAT)
        raise BlockError('Temporary block by search engine')

    html = pq(result)
    links = _extract_links(html, search_engine)
    if len(links) == 0:
        logging.info(
            'Search engine %s found no StackOverflow links, returned HTML is:',
            search_engine)
        logging.info(result)
    return list(dict.fromkeys(links))  # remove any duplicates
Example #22
0
	def _get_feed_episodes(self, show_key, **kwargs):
		"""
		Always returns a list.
		"""
		info("Getting episodes for Nyaa/{}".format(show_key))
		if "domain" not in self.config or not self.config["domain"]:
			error("  Domain not specified in config")
			return list()
		
		# Send request
		query = re.sub("[`~!@#$%^&*()+=:;,.<>?/|\\'\"]+", " ", show_key)
		query = re.sub("  ", " ", query)
		debug("  query={}".format(query))
		query = url_quote(query, safe="", errors="ignore")
		
		url = self._search_base.format(domain=self.config["domain"], q=query)
		response = self.request(url, rss=True, **kwargs)
		if response is None:
			error("Cannot get latest show for Nyaa/{}".format(show_key))
			return list()
		
		# Parse RSS feed
		if not _verify_feed(response):
			warning("Parsed feed could not be verified, may have unexpected results")
		return response.get("entries", list())
Example #23
0
    def _get_feed_episodes(self, show_key, **kwargs):
        """
		Always returns a list.
		"""
        info("Getting episodes for Nyaa/{}".format(show_key))
        if "domain" not in self.config or not self.config["domain"]:
            error("  Domain not specified in config")
            return list()

        # Send request
        query = re.sub("[-`~!@#$%^&*()+=:;,.<>?/|\\'\"]+", " ", show_key)
        query = re.sub("season", " ", query, flags=re.I)
        query = re.sub(" +", " ", query)
        debug("  query={}".format(query))
        query = url_quote(query, safe="", errors="ignore")

        domain = self.config.get("domain", "nyaa.si")
        filter_ = self.config.get("filter", "2")
        excludes = self.config.get("excluded_users", "").replace(" ", "")
        url = self._search_base.format(domain=domain,
                                       filter=filter_,
                                       excludes=excludes,
                                       q=query)
        response = self.request(url, rss=True, **kwargs)
        if response is None:
            error("Cannot get latest show for Nyaa/{}".format(show_key))
            return list()

        # Parse RSS feed
        if not _verify_feed(response):
            warning(
                "Parsed feed could not be verified, may have unexpected results"
            )
        return response.get("entries", list())
Example #24
0
    def check_backupdb_file(self, childpath):
        if not self.backupdb:
            return True, None
        use_timestamps = not self.options["ignore-timestamps"]
        r = self.backupdb.check_file(childpath, use_timestamps)

        if not r.was_uploaded():
            return True, r

        if not r.should_check():
            # the file was uploaded or checked recently, so we can just use
            # it
            return False, r

        # we must check the file before using the results
        filecap = r.was_uploaded()
        self.verboseprint("checking %s" % quote_output(filecap))
        nodeurl = self.options['node-url']
        checkurl = nodeurl + "uri/%s?t=check&output=JSON" % url_quote(filecap)
        self._files_checked += 1
        resp = do_http("POST", checkurl)
        if resp.status != 200:
            # can't check, so we must assume it's bad
            return True, r

        cr = json.loads(resp.read())
        healthy = cr["results"]["healthy"]
        if not healthy:
            # must upload
            return True, r
        # file is healthy, no need to upload
        r.did_check_healthy(cr)
        return False, r
Example #25
0
    def run(self, options):
        stderr = options.stderr
        self.options = options
        self.ophandle = ophandle = ensure_str(base32.b2a(os.urandom(16)))
        nodeurl = options['node-url']
        if not nodeurl.endswith("/"):
            nodeurl += "/"
        self.nodeurl = nodeurl
        where = options.where
        try:
            rootcap, path = get_alias(options.aliases, where, DEFAULT_ALIAS)
        except UnknownAliasError as e:
            e.display(stderr)
            return 1
        path = str(path, "utf-8")
        if path == '/':
            path = ''
        url = nodeurl + "uri/%s" % url_quote(rootcap)
        if path:
            url += "/" + escape_path(path)
        # todo: should it end with a slash?
        url = self.make_url(url, ophandle)
        resp = do_http("POST", url)
        if resp.status not in (200, 302):
            print(format_http_error("ERROR", resp), file=stderr)
            return 1
        # now we poll for results. We nominally poll at t=1, 5, 10, 30, 60,
        # 90, k*120 seconds, but if the poll takes non-zero time, that will
        # be slightly longer. I'm not worried about trying to make up for
        # that time.

        return self.wait_for_results()
Example #26
0
def unlink(options, command="unlink"):
    """
    @return: a Deferred which eventually fires with the exit code
    """
    nodeurl = options['node-url']
    aliases = options.aliases
    where = options.where
    stdout = options.stdout
    stderr = options.stderr

    if nodeurl[-1] != "/":
        nodeurl += "/"
    try:
        rootcap, path = get_alias(aliases, where, DEFAULT_ALIAS)
    except UnknownAliasError as e:
        e.display(stderr)
        return 1
    if not path:
        print("""
'tahoe %s' can only unlink directory entries, so a path must be given.""" %
              (command, ),
              file=stderr)
        return 1

    url = nodeurl + "uri/%s" % url_quote(rootcap)
    url += "/" + escape_path(path)

    resp = do_http("DELETE", url)

    if resp.status in (200, ):
        print(format_http_success(resp), file=stdout)
        return 0

    print(format_http_error("ERROR", resp), file=stderr)
    return 1
Example #27
0
 def get_links(self, query):
     localization_url = LOCALIZATON_URLS[self.lang]
     result = self.get_result(
         SEARCH_URL.format(localization_url, url_quote(query)))
     html = pq(result)
     return [a.attrib['href'] for a in html('.l')
             ] or [a.attrib['href'] for a in html('.r')('a')]
Example #28
0
def webopen(options, opener=None):
    nodeurl = options['node-url']
    stderr = options.stderr
    if not nodeurl.endswith("/"):
        nodeurl += "/"
    where = options.where
    if where:
        try:
            rootcap, path = get_alias(options.aliases, where, DEFAULT_ALIAS)
        except UnknownAliasError as e:
            e.display(stderr)
            return 1
        path = str(path, "utf-8")
        if path == '/':
            path = ''
        url = nodeurl + "uri/%s" % url_quote(rootcap)
        if path:
            url += "/" + escape_path(path)
    else:
        url = nodeurl
    if options['info']:
        url += "?t=info"
    if not opener:
        import webbrowser
        opener = webbrowser.open
    opener(url)
    return 0
Example #29
0
 def cancelCollection(self, collection_id, config_id=None):
     if not collection_id:
         raise SemantriaError(
             'Parameter \'collection_id\' must not be empty')
     collection_id = url_quote(collection_id, safe='')
     url = self.makeUrl('/collection/{0}'.format(collection_id), config_id)
     return self._runRequest("DELETE", url)
def wrangle_experiment_name(rids):
    """Returns a 2-item list [label, link] for the Experiment Name, or just a name
       if there is no link to make.
    """
    expname_from_xml = rids.get('ExperimentName')
    expname_from_ss = rids.get('ExperimentSS')

    if not expname_from_xml:
        # No linky??
        if expname_from_ss:
            # consistent with summarize_lane_contents.py
            return expname_from_ss or 'unknown ({})'.format(expname_from_ss)
        else:
            return 'unknown'

    else:
        # Hyperlink the expt name to BaseSpace. We can't do this directly since there is an unpredictable
        # number in the URL but this will do. This always uses the expname_from_xml value.
        linky = "https://basespace.illumina.com/search/?type=run&query=" + url_quote(
            expname_from_xml)

        if expname_from_ss and expname_from_ss != expname_from_xml:
            # Name conflict
            return [
                "[{}] ({})".format(expname_from_xml, expname_from_ss), linky
            ]
        else:
            # All consistent, or expname_from_ss is just missing
            return [expname_from_xml, linky]
    def test_path_and_url(path, url):
        def _urlopen(url, auth=None):
            req = Request(url)
            if auth:
                req.add_header(
                    "Authorization", b"Basic " + base64.standard_b64encode(
                        '{0}:{1}'.format(*auth).encode('utf-8')))
            return urlopen(req)

        # @serve_ should remove http_proxy from the os.environ if was present
        if not on_windows:
            assert_false('http_proxy' in os.environ)
        # get the "dir-view"
        dirurl = url + test_fpath.parent.as_posix()
        u = _urlopen(dirurl, auth)
        assert_true(u.getcode() == 200)
        html = u.read()
        # get the actual content
        file_html = _urlopen(url + url_quote(test_fpath.as_posix()),
                             auth).read().decode()
        # verify we got the right one
        eq_(file_html, test_fpath_full.read_text())

        if bs4 is None:
            return

        # MIH is not sure what this part below is supposed to do
        # possibly some kind of internal consistency test
        soup = bs4.BeautifulSoup(html, "html.parser")
        href_links = [txt.get('href') for txt in soup.find_all('a')]
        assert_true(len(href_links) == 1)
        parsed_url = f"{dirurl}/{href_links[0]}"
        u = _urlopen(parsed_url, auth)
        html = u.read().decode()
        eq_(html, file_html)
Example #32
0
 def update_payment(self, payment_ref, payment_update):
     if not payment_ref:
         raise ValueError('payment_ref must be provided')
     response = self.api_session.patch('/payments/%s/' %
                                       url_quote(payment_ref),
                                       json=payment_update)
     return response.json()
Example #33
0
def register_basic_user(request):
    """
    Register a user with username and password
    """
    if request.method != 'POST':
        return HttpResponseNotAllowed(['POST'])
    if request.JSON is None:
        return JsonResponse(
            {'error': 'Expected a json request'}, status=400)
    for key in ('email', 'username', 'password'):
        if key not in request.JSON:
            return JsonResponse(
                {'error': 'Required key {0} not found in body'.format(key)},
                status=400)
    username = request.JSON['username']
    try:
        user = User.objects.get_by_natural_key(User.Type.BASIC, username)
        return JsonResponse({'error': 'User already exists'},
                            status=400)
    except User.DoesNotExist:
        user = User.objects.create_basic_user(username=username,
                                              email=request.JSON['email'],
                                              password=request.JSON['password'])

    response = partial_json_response(request, USER_RESOURCE.to_json(user))
    auth_token = user.get_auth_token(request.JSON['password'])
    user = authenticate(username=username, force=True)

    response.set_cookie(
        'authToken',
        url_quote(auth_token),
        ## TODO: auth token should be http only.
        # httponly=True
    )
    return response
Example #34
0
 def get_payment(self, payment_ref):
     try:
         if payment_ref:
             return self.api_session.get('/payments/%s/' %
                                         url_quote(payment_ref)).json()
     except HttpNotFoundError:
         pass
Example #35
0
 def post(self, url, recipient_public_key):
     """
     Actually send the message to an HTTP/HTTPs endpoint.
     """
     xml = url_quote(self.create_salmon_envelope(recipient_public_key))
     data = urlencode({"xml": xml})
     return urlopen(url, data.encode("ascii"))
	def get_data_url(self, max_width = None):
		data_url = b'data:{};base64,{}'
		
		if self.is_image():
			image_file = ImageBufferIO(self.file_data)
			output = ImageBufferIO()
			
			try:
				image = Image.open(image_file)
				
				s = image.size
				
				if max_width and s[0] > max_width:
					ratio = max_width / s[0]
					width = s[0] * ratio
					height = s[1] * ratio
					self._generate_thumbnail(image, output, (width, height))
					
					file_data = output.getvalue()
				else:
					file_data = image_file.getvalue()
			except IOError:
				file_data = self.file_data
				logger.exception('Error when trying to resize image for data: url')
		else:
			file_data = self.file_data
			
		data = bytes(url_quote(file_data.encode('base64')))
		
		return data_url.format(self.content_type, data)
Example #37
0
def get_links(query):
    localization_url = LOCALIZATON_URLS[LOCALIZATION]

    pr_debug(url_quote(query))
    pr_debug(2, SEARCH_URL.format(localization_url, url_quote(query)))
    result = get_result(SEARCH_URL.format(localization_url, url_quote(query)))
    html = pq(result)
    #return [a.attrib['href'] for a in html('.l')] or \
        #[a.attrib['href'] for a in html('.r')('a')]
    for a in html('.l'):
        pr_debug("123", a.text())
    links = [a.attrib['href'] for a in html('.l')] or \
        [a.attrib['href'] for a in html('.r')('a')]

    pr_debug(' '.join(links))
    return links
Example #38
0
    def check_backupdb_directory(self, compare_contents):
        if not self.backupdb:
            return True, None
        r = self.backupdb.check_directory(compare_contents)

        if not r.was_created():
            return True, r

        if not r.should_check():
            # the file was uploaded or checked recently, so we can just use
            # it
            return False, r

        # we must check the directory before re-using it
        dircap = r.was_created()
        self.verboseprint("checking %s" % quote_output(dircap))
        nodeurl = self.options['node-url']
        checkurl = nodeurl + "uri/%s?t=check&output=JSON" % url_quote(dircap)
        self._directories_checked += 1
        resp = do_http("POST", checkurl)
        if resp.status != 200:
            # can't check, so we must assume it's bad
            return True, r

        cr = json.loads(resp.read())
        healthy = cr["results"]["healthy"]
        if not healthy:
            # must create
            return True, r
        # directory is healthy, no need to upload
        r.did_check_healthy(cr)
        return False, r
Example #39
0
def _update_request(request):
    # Verify body
    if request.body:
        request.body = _get_data_bytes_or_stream_only('request.body',
                                                      request.body)
        length = _len_plus(request.body)

        # only scenario where this case is plausible is if the stream object is not seekable.
        if length is None:
            raise ValueError(_ERROR_VALUE_SHOULD_BE_SEEKABLE_STREAM)

        # if it is PUT, POST, MERGE, DELETE, need to add content-length to header.
        if request.method in ['PUT', 'POST', 'MERGE', 'DELETE']:
            request.headers['Content-Length'] = str(length)

    # append addtional headers based on the service
    request.headers['x-ms-version'] = X_MS_VERSION
    request.headers['User-Agent'] = USER_AGENT_STRING
    request.headers['x-ms-client-request-id'] = str(uuid.uuid1())

    # If the host has a path component (ex local storage), move it
    path = request.host.split('/', 1)
    if len(path) == 2:
        request.host = path[0]
        request.path = '/{}{}'.format(path[1], request.path)

    # Encode and optionally add local storage prefix to path
    request.path = url_quote(request.path, '/()$=\',~')
Example #40
0
 def get_item_data(self, item, parent_data):
     item['url'] = reverse('extract:trademark-usage-list') + \
                   '?trademark_search={}'.format(url_quote(item['trademark']))
     if "document_pk" in self.request.GET:
         item['trademark_data'] = [i for i in parent_data
                                   if i['trademark'] == item['trademark']]
     return item
Example #41
0
def show_search(show):
    show = url_quote(show)
    url = endpoints.show_search.format(show)
    q = query_endpoint(url)
    if q:
        return q
    else:
        raise ShowNotFound(str(show) + ' not found')
Example #42
0
    def getBaseUrl(self):
        '''Return a file: URL that probably points to the basedir.

        This is used as a halfway sane default when the base URL is not
        provided; not perfect, but should work in most cases.'''
        components = util.splitpath(os.path.abspath(self.basepath))
        url = '/'.join([url_quote(component, '') for component in components])
        return 'file:///' + url + '/'
Example #43
0
def people_search(person):
    person = url_quote(person)
    url = endpoints.people_search.format(person)
    q = query_endpoint(url)
    if q:
        return q
    else:
        raise PersonNotFound('Couldn\'t find person: ' + str(person))
Example #44
0
    def test_keeps_extension_filename(self, api_client, storage):
        xpi = LegacyAddonFileFactory()
        res = self._upload_extension(api_client, xpi.path)
        assert res.status_code == 201, f"body of unexpected response: {res.data}"  # created
        _, filename = os.path.split(xpi.path)
        assert res.data["xpi"].split("/")[-1] == url_quote(filename)

        # Download the XPI to make sure the url is actually good
        res = api_client.get(res.data["xpi"], follow=True)
        assert res.status_code == 200
Example #45
0
def hfs_quote(path):
    if isinstance(path, unicode):
        raise TypeError('bytes are required')
    try:
        path.decode('utf-8')
    except UnicodeDecodeError:
        path = url_quote(path) # Not UTF-8
        if sys.version_info >= (3,):
            path = path.encode('ascii')
    return path
Example #46
0
    def get_time_entries(self, start=None, end=None):
        """Get the list of entries for the specified time range,
        or the latest entries if no dates specified"""
        # Fetch the data or die trying.
        # Toggle has the start/end dates creating a confusing
        # backwards range. Swap them here.
        url = "%s/time_entries.json" % self.base_url
        if start is not None and end is not None:
            url = "%s?start_date=%s&end_date=%s" % \
                    (url, url_quote(str(end)), url_quote(str(start)))
        if self.verbose:
            print(url)
        r = requests.get(url, auth=self.auth)
        self._raise_if_error(r)

        if self.verbose:
            print(r.text)

        return [TogglEntry(e) for e in json.loads(r.text)['data']]
def get_formatted_verification_link(verification_code, partial_link):
    ''' Create a link for the verification code. '''

    try:
        code = verification_code
        quoted_code = url_quote(code)
        link = '/mail/{}/{}'.format(partial_link, quoted_code, code)
    except:
        link = code

    return link
Example #48
0
def show_single_search(show, embed=None):
    show = url_quote(show)
    if embed:
        url = endpoints.show_single_search.format(show) + '&embed=' + embed
    else:
        url = endpoints.show_single_search.format(show)
    q = query_endpoint(url)
    if q:
        return q
    else:
        raise ShowNotFound(str(show) + ' not found')
def prompt_for_code(request):
    ''' Prompt user for the verification code for the message user wants to verify.'''

    response = None
    form = VerifyMessageForm()
    form_template = 'mail/verify_message.html'
    if request.method == 'POST':
        form = VerifyMessageForm(request.POST)
        if form.is_valid():
            template = 'mail/verified_decrypted.html'
            log_message('verification code: {}'.format(form.cleaned_data['verification_code']))
            params, status = get_crypted_params(
               request.user.email, form.cleaned_data['verification_code'])
            if 'error_message' in params and params['error_message'] is not None:
                log_message('retry verification code: {}'.format(url_quote(form.cleaned_data['verification_code'])))
                retry_params, __ = get_crypted_params(
                   request.user.email, url_quote(form.cleaned_data['verification_code']))
                if 'error_message' in retry_params and retry_params['error_message'] is None:
                    params = retry_params
                log_message('retry params: {}'.format(retry_params))
            elif status == ENCRYPTED_STATUS:
                template = 'mail/verified_encrypted.html'
                log_message('using encrypted verification page')

            response = render_to_response(
                template, params, context_instance=RequestContext(request))
        else:
            log_message('form not valid')

        if response is None:
            log_message('post: {}'.format(request.POST))

    if response is None:
        log_message('no response for verifying message crypted so redisplaying main page')
        params = {'form': form,
                  'main_headline': 'Verify Message',
                  'url': 'verify_crypted'}
        response = render_to_response(
            form_template, params, context_instance=RequestContext(request))

    return response
Example #50
0
def get_output(word):
    result = get_result(SEARCH_URL.format(url_quote(word)))
    p = pq(result)
    info = p('.info-article.info-base')
    word_list = info('.base-list')
    elements = word_list('li').not_('.change')
    output = []
    for element in elements:
        prop = pq(element)('.prop').text()
        content = pq(element)('p').text()
        output.append((prop, content))
    return output
Example #51
0
 def post(self, url, recipient_public_key):
     """
     Actually send the message to an HTTP/HTTPs endpoint.
     """
     xml = url_quote(
         self.create_salmon_envelope(recipient_public_key))
     data = urlencode({
         'xml': xml
     })
     req = Request(url)
     req.add_header('User-Agent', USER_AGENT)
     return urlopen(req, data.encode("ascii"), timeout=60)
Example #52
0
    def cancelDocument(self, doc_id, config_id=None):
        if not doc_id:
            raise SemantriaError('Parameter not found: %s' % doc_id)

        doc_id = url_quote(doc_id, safe='')

        if config_id:
            url = '{0}/document/{1}.{2}?config_id={3}'.format(self.host, doc_id, self.format, config_id)
        else:
            url = '{0}/document/{1}.{2}'.format(self.host, doc_id, self.format)

        return self._runRequest("DELETE", url)
Example #53
0
    def cancelCollection(self, coll_id, config_id=None):
        if not coll_id:
            raise SemantriaError('Parameter not found: %s' % coll_id)

        coll_id = url_quote(coll_id, safe='')

        if config_id:
            url = '{0}/collection/{1}.{2}?config_id={3}'.format(self.host, coll_id, self.format, config_id)
        else:
            url = '{0}/collection/{1}.{2}'.format(self.host, coll_id, self.format)

        return self._runRequest("DELETE", url)
Example #54
0
def login_user(request):
    """
    Log in a user via basic authentication
    """
    if request.method == 'GET':
        context = {}
        context.update(csrf(request))
        return render2('auth/login.html', context)
    elif request.method == 'POST':
        if request.JSON is None:
            return JsonResponse(
                {'error': 'not a json request'},
                400
            )
        resource = USER_RESOURCE.to_python(request.JSON)
        username = resource.get('username')
        if username is not None:
            password = resource.get('password')
            if password is None:
                return HttpResponseForbidden('password required for user resource')

            user = authenticate(username=username, password=password)
            if user is None:
                return HttpResponseForbidden('invalid username or password')
            login(request, user)

            remembered = resource.get('remembered', False)

            result_resource = USER_RESOURCE.to_json(user)
            del result_resource['password']
            response = partial_json_response(request, result_resource)

            auth_token = user.get_auth_token(resource['password'])

            auth_token_expires = None
            if remembered:
                auth_token_expires = datetime.now() + timedelta(weeks=1)

            response.set_cookie(
                'authToken',
                url_quote(auth_token),
                expires=auth_token_expires,
                ## TODO: Authentication token should be httponly
                #httponly=True
            )

            return response
        else:
            ## TODO: Handle other types of login
            raise NotImplementedError()
    else:
        return HttpResponseNotAllowed(['GET', 'POST'])
Example #55
0
def add_verification_tag(crypto_message, verification_code):
    ''' Add the verification tag to the message. '''

    goodcrypto_server_url = options.goodcrypto_server_url()
    if goodcrypto_server_url and len(goodcrypto_server_url) > 0:
        quoted_code = url_quote(verification_code)
        tag = '{} {}mail/msg-decrypted/{}'.format(
           MESSAGE_VERIFY_PREFIX, goodcrypto_server_url, quoted_code)
    else:
        tag = '{}: {}'.format(MESSAGE_VERIFICATION_PREFIX, verification_code)

    crypto_message.add_tag_once(tag)
    log_message(tag)
Example #56
0
    def test_query_string_decoding(self):
        URI_TMPL = '/reqparams?q={q}'

        europoundUtf8_2_bytes = europoundUnicode.encode('utf-8')
        europoundUtf8_2nd_byte = europoundUtf8_2_bytes[1:2]

        # Encoded utf8 query strings MUST be parsed correctly.
        # Here, q is the POUND SIGN U+00A3 encoded in utf8 and then %HEX
        self.getPage(URI_TMPL.format(q=url_quote(europoundUtf8_2_bytes)))
        # The return value will be encoded as utf8.
        self.assertBody(b'q: ' + europoundUtf8_2_bytes)

        # Query strings that are incorrectly encoded MUST raise 404.
        # Here, q is the second byte of POUND SIGN U+A3 encoded in utf8
        # and then %HEX
        # TODO: check whether this shouldn't raise 400 Bad Request instead
        self.getPage(URI_TMPL.format(q=url_quote(europoundUtf8_2nd_byte)))
        self.assertStatus(404)
        self.assertErrorPage(
            404,
            'The given query string could not be processed. Query '
            "strings for this resource must be encoded with 'utf8'.")
def _update_request(request):
    # Verify body
    if request.body:
        assert isinstance(request.body, bytes)

    # if it is PUT, POST, MERGE, DELETE, need to add content-length to header.
    if request.method in ["PUT", "POST", "MERGE", "DELETE"]:
        request.headers.append(("Content-Length", str(len(request.body))))

    # append addtional headers based on the service
    current_time = format_date_time(time())
    request.headers.append(("x-ms-date", current_time))
    request.headers.append(("x-ms-version", X_MS_VERSION))
    request.headers.append(("Accept-Encoding", "identity"))

    # append x-ms-meta name, values to header
    for name, value in request.headers:
        if "x-ms-meta-name-values" in name and value:
            for meta_name, meta_value in value.items():
                request.headers.append(("x-ms-meta-" + meta_name, meta_value))
            request.headers.remove((name, value))
            break

    # If the host has a path component (ex local storage), move it
    path = request.host.split("/", 1)
    if len(path) == 2:
        request.host = path[0]
        request.path = "/{}{}".format(path[1], request.path)

    # Encode and optionally add local storage prefix to path
    request.path = url_quote(request.path, "/()$=',~")

    # Add query params to path
    if request.query:
        request.path += "?"
        for name, value in request.query:
            if value is not None:
                request.path += name + "=" + url_quote(value, "~") + "&"
        request.path = request.path[:-1]
Example #58
0
def unicode_urlencode(obj, charset='utf-8'):
    """URL escapes a single bytestring or unicode string with the
    given charset if applicable to URL safe quoting under all rules
    that need to be considered under all supported Python versions.

    If non strings are provided they are converted to their unicode
    representation first.
    """
    if not isinstance(obj, basestring):
        obj = unicode(obj)
    if isinstance(obj, unicode):
        obj = obj.encode(charset)
    return unicode(url_quote(obj))