Ejemplo n.º 1
0
def application(environ, start_response):
    # https://www.python.org/dev/peps/pep-3333/#environ-variables
    REQUEST_METHOD = environ['REQUEST_METHOD']
    CONTENT_TYPE, CONTENT_TYPE_KWARGS = parse_http_content_type(environ)
    SERVER_PROTOCOL = environ['SERVER_PROTOCOL']
    HEADERS = parse_http_headers(environ)
    URI_PATH = environ['PATH_INFO']
    URI_QUERY = environ['QUERY_STRING']
    URI = parse_http_uri(environ)
    POST = parse_http_x_www_form_urlencoded_post_data(environ)
    GET = parse_http_get_data(environ)

    headers = [('Content-type', 'text/html; charset=utf-8')]

    controller_callback = router.resolve(URI_PATH)
    status, body = controller_callback(REQUEST_METHOD, GET, POST, headers)

    if URI_PATH.startswith(STATIC_URL):
        print('STATIC FILE DETECTED!')

    if DEBUG:
        print("{REQUEST_METHOD} {URI_PATH}?{URI_QUERY} {SERVER_PROTOCOL}\n"
              "CONTENT_TYPE: {CONTENT_TYPE}; {CONTENT_TYPE_KWARGS}\n"
              "POST: {POST}\n"
              "GET: {GET}\n"
              ":HEADERS:\n{HEADERS}\n"
              .format(**locals()))

    start_response(status, headers)
    return [body]
Ejemplo n.º 2
0
def application(environ, start_response):
    # https://www.python.org/dev/peps/pep-3333/#environ-variables
    REQUEST_METHOD = environ['REQUEST_METHOD']
    CONTENT_TYPE, CONTENT_TYPE_KWARGS = parse_http_content_type(environ)
    SERVER_PROTOCOL = environ['SERVER_PROTOCOL']
    HEADERS = parse_http_headers(environ)
    URI_PATH = environ['PATH_INFO']
    URI = parse_http_uri(environ)
    POST = parse_http_x_www_form_urlencoded_post_data(environ)
    GET = parse_http_get_data(environ)

    if URI_PATH.startswith(STATIC_URL):
        print('STATIC FILE DETECTED!',URI_PATH)
        try:
            body = open(URI_PATH[1:], 'rb').read()
            start_response('200 OK',[])
            return [body]
        except IOError:
            start_response("500 Internal Error",[])
            return []
    else:
        headers = [('Content-type', 'text/html; charset=utf-8')]
        controller_callback = router.resolve(URI_PATH)
        status, body = controller_callback(REQUEST_METHOD, GET, POST, headers)
        if DEBUG:
            print("{REQUEST_METHOD} {URI_PATH} {SERVER_PROTOCOL}\n"
                "CONTENT_TYPE: {CONTENT_TYPE}; {CONTENT_TYPE_KWARGS}\n"
                "POST: {POST}\n"
                "GET: {GET}\n"
                ":HEADERS:\n{HEADERS}\n"
                .format(**locals()))

        start_response(status, headers)
        return [body]
Ejemplo n.º 3
0
def application(environ, start_response):
    # https://www.python.org/dev/peps/pep-3333/#environ-variables
    REQUEST_METHOD = environ['REQUEST_METHOD']
    CONTENT_TYPE, CONTENT_TYPE_KWARGS = parse_http_content_type(environ)
    SERVER_PROTOCOL = environ['SERVER_PROTOCOL']
    HEADERS = parse_http_headers(environ)
    URI_PATH = environ['PATH_INFO']
    URI_QUERY = environ['QUERY_STRING']
    URI = parse_http_uri(environ)
    POST = parse_http_x_www_form_urlencoded_post_data(environ)
    GET = parse_http_get_data(environ)

    status = '200 OK'
    headers = [('Content-type', 'text/html; charset=utf-8')]

    if URI_PATH == '/favicon.ico':
        status = '404 Not Found'
        start_response(status, headers)
        return [b'']

    if URI_PATH.startswith(STATIC_URL):
        with_static_root = URI_PATH.replace(STATIC_URL,
                                            '{}/'.format(STATIC_ROOT), 1)
        absolute_path = path.abspath(with_static_root)
        if not absolute_path.startswith(
                path.abspath(STATIC_ROOT)) or not path.isfile(absolute_path):
            status = '404 Not Found'
            start_response(status, headers)
            return [b'']
        start_response(status, headers)
        #with open(absolute_path, 'br') as file:
        #    return [file.read()]
        return get_file_bytes(absolute_path)

    if DEBUG:
        print("{REQUEST_METHOD} {URI_PATH}?{URI_QUERY} {SERVER_PROTOCOL}\n"
              "CONTENT_TYPE: {CONTENT_TYPE}; {CONTENT_TYPE_KWARGS}\n"
              "POST: {POST}\n"
              "GET: {GET}\n"
              ":HEADERS:\n{HEADERS}\n".format(**locals()))

    with open('main.html', 'rb') as f:
        template_bytes = f.read()

    if REQUEST_METHOD == 'POST':
        status = '303 See Other'
        headers.append(('Location', '/'))
        name = get_first_element(POST, 'name', '')
        message = get_first_element(POST, 'message', '')
        data_message_text = "Name: {0}<br>Message: {1}".format(name, message)
        data_message_bytes = data_message_text.encode('utf-8')
        data_messages.append(data_message_bytes)
        start_response(status, headers)
        return [b'']

    messages = b'<hr>'.join(data_messages)
    template_bytes = template_bytes.replace(b'{{messages}}', messages)

    start_response(status, headers)
    return [template_bytes]
Ejemplo n.º 4
0
 def __init__(self, environ):
     # https://www.python.org/dev/peps/pep-3333/#environ-variables
     self.REQUEST_METHOD = environ['REQUEST_METHOD']
     self.CONTENT_TYPE, self.CONTENT_TYPE_KWARGS = parse_http_content_type(
         environ)
     self.SERVER_PROTOCOL = environ['SERVER_PROTOCOL']
     self.HEADERS = parse_http_headers(environ)
     self.URI_PATH = environ['PATH_INFO']
     self.URI_QUERY = environ['QUERY_STRING']
     self.URI = parse_http_uri(environ)
     self.POST = parse_http_x_www_form_urlencoded_post_data(environ)
     self.GET = parse_http_get_data(environ)
Ejemplo n.º 5
0
def application(environ, start_response):
    # https://www.python.org/dev/peps/pep-3333/#environ-variables
    REQUEST_METHOD = environ['REQUEST_METHOD']
    CONTENT_TYPE, CONTENT_TYPE_KWARGS = parse_http_content_type(environ)
    SERVER_PROTOCOL = environ['SERVER_PROTOCOL']
    HEADERS = parse_http_headers(environ)
    URI_PATH = environ['PATH_INFO']
    URI_QUERY = environ['QUERY_STRING']
    URI = parse_http_uri(environ)
    POST = cgi.FieldStorage(fp=environ['wsgi.input'],
                            environ=environ,
                            keep_blank_values=True)
    GET = parse_http_get_data(environ)

    headers = [('Content-type', 'text/html; charset=utf-8')]

    controller_callback = router.resolve(URI_PATH)
    status, body = controller_callback(REQUEST_METHOD, GET, POST, headers)

    if URI_PATH.startswith(STATIC_URL):
        URI_PATH = URI_PATH.replace(STATIC_URL, STATIC_ROOT)
        URI_PATH = os.path.normpath(URI_PATH)
        if os.path.exists(URI_PATH):
            with open(URI_PATH, 'rb') as f:
                contentLogo = f.read()
            status = '200 OK'
            headers = [('Content-type', "application/octet-stream")]
            start_response(status, headers)
            return [contentLogo]

        status = '404 Not Found'
        start_response(status, headers)
        return [b'']

    if DEBUG:
        print("{REQUEST_METHOD} {URI_PATH}?{URI_QUERY} {SERVER_PROTOCOL}\n"
              "CONTENT_TYPE: {CONTENT_TYPE}; {CONTENT_TYPE_KWARGS}\n"
              "POST: {POST}\n"
              "GET: {GET}\n"
              ":HEADERS:\n{HEADERS}\n".format(**locals()))

    start_response(status, headers)
    return [body]
Ejemplo n.º 6
0
def application(environ, start_response):
    # https://www.python.org/dev/peps/pep-3333/#environ-variables
    REQUEST_METHOD = environ['REQUEST_METHOD']
    CONTENT_TYPE, CONTENT_TYPE_KWARGS = parse_http_content_type(environ)
    SERVER_PROTOCOL = environ['SERVER_PROTOCOL']
    HEADERS = parse_http_headers(environ)
    URI_PATH = environ['PATH_INFO'].strip('/')
    URI = parse_http_uri(environ)
    POST = parse_http_x_www_form_urlencoded_post_data(environ)
    GET = parse_http_get_data(environ)

    headers = [('Content-type', 'text/html; charset=utf-8')]
    headers.extend([('Access-Control-Allow-Origin', '*')])
    params = {
        "request_method": REQUEST_METHOD,
        "uri_path": URI_PATH,
        "get": GET,
        "post": POST,
        "headers": headers,
    }
    if len(GET) > 0:
        query_string = environ['QUERY_STRING']
        get_params = query_string.split('&')
        for var in get_params:
            key, value = var.split('=')
            params[key] = value

    controller_callback = router.resolve(REQUEST_METHOD, URI_PATH)
    status, body = controller_callback(params, params)
    if URI_PATH.startswith(STATIC_URL):
        print('STATIC FILE DETECTED!')

    if DEBUG:
        print("{REQUEST_METHOD} {URI_PATH} {SERVER_PROTOCOL}\n"
              "CONTENT_TYPE: {CONTENT_TYPE}; {CONTENT_TYPE_KWARGS}\n"
              "POST: {POST}\n"
              "GET: {GET}\n"
              ":HEADERS:\n{HEADERS}\n"
              .format(**locals()))

    start_response(status, headers)
    return [body]
Ejemplo n.º 7
0
def application(environ, start_response):
    status = '200 OK'
    with open('index.html', encoding='utf-8') as f:
        response_template = f.read()

    GET = parse_http_get_data(environ)
    name = get_first_element(GET, "name", '')
    message = get_first_element(GET, "message", '')

    if message:
        db.messages.append({'name': name, 'message': message})
        start_response('302 Found', [('Content-Type', 'text/html'),
                                     ('Location', '/')])
        return [b'']

    response_body = response_template.format(name=name, messages=db.messages)

    response_headers = [('Content-Type', 'text/html'),
                        ('Content-Length', str(len(response_body)))]

    start_response(status, response_headers)
    return [response_body.encode('utf-8')]
Ejemplo n.º 8
0
def application(environ, start_response):
    # https://www.python.org/dev/peps/pep-3333/#environ-variables
    REQUEST_METHOD = environ['REQUEST_METHOD']
    CONTENT_TYPE, CONTENT_TYPE_KWARGS = parse_http_content_type(environ)
    SERVER_PROTOCOL = environ['SERVER_PROTOCOL']
    HEADERS = parse_http_headers(environ)
    URI_PATH = environ['PATH_INFO']
    URI_QUERY = environ['QUERY_STRING']
    URI = parse_http_uri(environ)
    POST = parse_http_x_www_form_urlencoded_post_data(environ)
    GET = parse_http_get_data(environ)

    status = '200 OK'
    headers = [('Content-type', 'text/html; charset=utf-8')]

    callback = router.resolve(URI_PATH)
    status, body = callback

    if URI_PATH == '/favicon.ico':
        status = '404 Not Found'
        start_response(status, headers)
        return [b'']

    if URI_PATH.startswith(STATIC_URL):
        # import os
        import os.path

        path = URI_PATH[len(STATIC_URL):]
        normpath = os.path.normpath(path)
        # print(normpath)
        download_path = STATIC_ROOT + normpath
        # print(download_path)
        if not os.path.exists(download_path):
            start_response("404 Not found", headers)
            return [b'']
        else:
            headers = [('Content-type', 'text/plain; charset=utf-8')]
            start_response(status, headers)
            return [open(download_path, 'rb').read()]
            # with open(path, 'rb') as f:
            # start_response(status, headers)
            # return [f.read()]

    DEBUG = False
    if DEBUG:
        print("{REQUEST_METHOD} {URI_PATH}?{URI_QUERY} {SERVER_PROTOCOL}\n"
              "CONTENT_TYPE: {CONTENT_TYPE}; {CONTENT_TYPE_KWARGS}\n"
              "POST: {POST}\n"
              "GET: {GET}\n"
              ":HEADERS:\n{HEADERS}\n".format(**locals()))

    with open('main.html', 'rb') as f:
        template_bytes = f.read()

    if REQUEST_METHOD == 'POST':
        status = '303 See Other'
        headers.append(('Location', '/'))
        name = get_first_element(POST, 'name', '')
        message = get_first_element(POST, 'message', '')
        data_message_text = "Name: {0}<br>Message: {1}".format(name, message)
        data_message_bytes = data_message_text.encode('utf-8')
        data_messages.append(data_message_bytes)
        start_response(status, headers)
        return [b'']

    messages = b'<hr>'.join(data_messages)
    template_bytes = template_bytes.replace(b'{{messages}}', messages)

    start_response(status, headers)
    return [template_bytes]