def create_url(): if not request.is_json: abort(422) content = request.json redirect_url = content.get('url', '').strip() if not is_valid_url(redirect_url): abort(422) # Check if it already exists url = Url.query.filter(Url.redirect == redirect_url).first() if url: return jsonify({'shorter': url.get_full_short()}) next_id = db.session.execute(Sequence("urls_id_seq")) url = Url(id=next_id, redirect=redirect_url, slug=to_emoji_slug(next_id)) db.session.add(url) db.session.commit() return jsonify({'shorter': url.get_full_short()})
def test_valid_urls(self): valid_urls = [ 'https://mishnit.github.io/', 'https://github.com/mishnit', 'https://www.getdoozy.com/', ] self.assertTrue(all([is_valid_url(url) for url in valid_urls]))
def test_invalid_urls(self): invalid_urls = [ 'htp://codydjango.com', 'codydjango.com', 'www.codydjango.ca', 'http://subdomain', ] self.assertFalse(all([is_valid_url(url) for url in invalid_urls]))
def test_invalid_urls(self): invalid_urls = [ 'htp://mishnit.com', 'mishnit.com', 'www.mishnit.co', 'http://subdomain', ] self.assertFalse(all([is_valid_url(url) for url in invalid_urls]))
def test_valid_urls(self): valid_urls = [ 'http://codydjango.com', 'https://codydjango.com', 'http://codydjango.ca', 'http://subdomain.codydjango.co', 'https://codydjango.ca/a', 'https://codydjango.ca/a/asdf', 'https://codydjango.ca/a/asdf/', 'https://codydjango.net/a/asdf/?foo', 'https://codydjango.co/a/asdf/?foo=bar', 'https://codydjango.com/a/asdf/?foo=bar&baz=bat', 'https://codydjango.com/a/asdf#link?foo=bar&baz=bat', ] self.assertTrue(all([is_valid_url(url) for url in valid_urls]))
def mutate(self, info, redirect_url): redirect_url = redirect_url.strip() if not is_valid_url(redirect_url): return 'Malformed URL: {}'.format(redirect_url) # Check if it already exists url_obj = Url.query.filter(Url.redirect==redirect_url).first() if url_obj: ok = False # return false because it was found, but not created return CreateShortUrl(short=url_obj.get_full_short(), ok=ok) next_id = db.session.execute(Sequence("urls_id_seq")) url_obj = Url(id=next_id, redirect=redirect_url, slug=to_emoji_slug(next_id)) db.session.add(url_obj) db.session.commit() ok = True return CreateShortUrl(short=url_obj.get_full_short(), ok=ok)
def set_endpoint(props): # setEndpoint is valid both for creating and updating # so we check for each property (so we will update only # the ones that came in the request) values = {} # 'id' can be changed, this is the value of the new id # the old one, when it exists, comes in 'current_id' if 'id' in props: values['id'] = slugify(props['id'])[:30].strip() if 'description' in props: values['description'] = props['description'][:80].strip() if 'owner' in props: values['owner_id'] = props['owner'] if 'method' in props: values['method'] = props['method'].strip() if 'url' in props: if not props['url']: values['url'] = props['url'].strip() else: if not is_valid_url(props['url']): # url is not static, but a modifier valid, err = modifier_check(props['url']) if not valid: # oops, not a modifier return None, \ 'please provide a valid URL' \ ' or an URL modifier: %s' % err values['url_dynamic'] = True values['url'] = props['url'].strip() if 'definition' in props: valid, err = modifier_check(props['definition']) if not valid: return None, 'invalid modifier script: %s' % err values['definition'] = props['definition'].strip() if 'headers' in props: headers = json.loads(props['headers']) if not is_valid_headers(headers): return None, 'headers are invalid somehow' values['headers'] = json.dumps(dict(headers)) if 'pass_headers' in props: values['pass_headers'] = bool(props['pass_headers']) try: if 'current_id' in props: # updating pg.update('endpoints', set=values, where={'id': props['current_id']}) id = values.get('id', props['current_id']) else: # creating id = pg.insert('endpoints', values=values, return_id=True) pg.commit() return id, '' except psycopg2.IntegrityError as e: if e.pgcode == '23505': pg.rollback() # an endpoint like this already exists res = pg.select1('endpoints', what=['id'], where={ 'owner_id': values['owner'], 'definition': values['definition'], 'headers': values['headers'] }) return res['id'], '' else: raise e return None, 'mysterious error'
def trailer_youtube_url(self, trailer_youtube_url): is_valid_url(trailer_youtube_url) # pylint: disable=attribute-defined-outside-init self._trailer_youtube_url = trailer_youtube_url
def poster_image_url(self, poster_image_url): is_valid_url(poster_image_url) # pylint: disable=attribute-defined-outside-init self._poster_image_url = poster_image_url
def proxy(identifier, in_method, in_headers, data): # find endpoint endpoint = pg.select1( 'endpoints', what=['definition', 'method', 'pass_headers', 'headers', 'url', 'url_dynamic'], where={'id': identifier} ) if not endpoint: return 'endpoint not found, create it at ' \ '<a href="/dashboard">dashboard</a>', 404, {} event = { 'in': { 'time': time.time(), 'method': in_method, 'headers': dict(in_headers), 'body': data, 'replay': True }, 'out': { 'method': endpoint['method'], 'url': endpoint['url'], 'body': None, 'headers': {} }, 'response': { 'code': 0, 'body': '' } } mutated, error = jq(endpoint['definition'], data=data) if not mutated or error: event['out']['error'] = error.decode('utf-8') publish(identifier, event) return 'transmutated into null and aborted', 201, {} h = CaseInsensitiveDict({'Content-Type': 'application/json'}) if endpoint['pass_headers']: h.update(in_headers) h.update(endpoint['headers']) event['out']['headers'] = dict(h) # reformat the mutated data mutatedjson = json.loads(mutated.decode('utf-8')) if h.get('content-type') == 'application/x-www-form-urlencoded': # oops, not json mutated = urlencode(mutatedjson) else: mutated = json.dumps(mutatedjson) event['out']['body'] = mutated if endpoint['url_dynamic']: urlb, error = jq(endpoint['url'], data=data) print('URL', urlb, 'ERROR', error) if not urlb: event['out']['url_error'] = error.decode('utf-8') publish(identifier, event) return 'url building has failed', 200, {} url = urlb.decode('utf-8') event['out']['url'] = url else: url = endpoint['url'] # event['out'] is completed at this point # and we all have everything needed to perform the request if url and is_valid_url(url): # we will only perform a request if there is an URL and it is valid try: s = requests.Session() req = requests.Request(endpoint['method'], url, data=mutated, headers=h).prepare() resp = s.send(req, timeout=4) if not resp.ok: print('FAILED TO POST', resp.text, identifier, mutated) except requests.exceptions.RequestException as e: print(identifier, 'FAILED TO POST', mutated, 'TO URL', url) print(e) publish(identifier, event) return "<request failed: '%s'>" % e, 503, {} event['response']['code'] = resp.status_code event['response']['body'] = resp.text publish(identifier, event) return resp.text, resp.status_code, dict(resp.headers) else: # no valid URL, just testing publish(identifier, event) return 'no URL to send this to', 201, {}