Beispiel #1
0
def generate(env, resp):
    long_url = env['args'].get('long_url')
    
    if not long_url.startswith("http"):
        long_url = "http://%s" % long_url
    
    if not su.valid_url(long_url) or long_url.startswith(domain):
        message = 'URL %s is not valid' % (long_url) 
        return response.send_response(resp,
                                      status='500 Internal Server Error',
                                      body=message)
    
    success, short_url = su.create(long_url)
    
    if success:
        message = """
        Long URL = {long_url} <br />
        Short URL = <a href="/{short_url}">{domain}/{short_url}</a>
        """.format(long_url=long_url, domain=domain, short_url=short_url) 
        return response.send_response(resp,
                                      status='200 Ok',
                                      body=message)
    else:
        return response.send_response(resp,
                                      status='500 Internal Server Error',
                                      body='Failed, Internal Server Error')
Beispiel #2
0
def redirect(env, resp):
    long_url = su.get(env['args'].get('short_url'))
    
    if long_url:
        return response.send_response(resp,
                                      status='301 Moved Permanently',
                                      body='301 Moved Permanently',
                                      location=long_url,
                                      content_type='text/plain; charset=utf-8')
    else:
        return response.send_response(resp,
                                      status='404 Not Found',
                                      body='404 Not Found',
                                      content_type='text/plain; charset=utf-8')
Beispiel #3
0
def home(env, resp):
    body = """
    <html>
        <head>
            <title>MindTalk Shorturl</title>
        </head>
        <body>
            <form action="/action/generate" method="post">
                <p>
                    <label desc="Long URL">Long URL</label>
                    <input type="text" name="long_url" value="" />
                </p>
                <p>
                    <input type="submit" value="Generate" />
                </p>
            </form>
            <form action="/action/inject" method="post">
                <p>
                    <label desc="Long URL">Long URL</label>
                    <input type="text" name="long_url" value="" />
                </p>
                <p>
                    <label desc="Short URL">Short URL</label>
                    <input type="text" name="short_url" value="" />
                </p>
                <p>
                    <input type="submit" value="Inject" />
                </p>
            </form>
        </body>
    </html>
    """
    
    return response.send_response(resp,
                                  status='200 Ok',
                                  body=body,
                                  content_type='text/html; charset=utf-8')