Source

django-simpleproxy / views.py

# -*- coding: utf-8 -*-
"""
===========================
views
===========================
:create_date: 11/09/15 
"""
# builtin
import socket
from urllib import unquote

# django
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseNotFound
from django.conf import settings

# ...
from .utils import url_read, join, exists_url


def viewer(request, uid, fqdn, path_info):

    env = request.META

    # 未使用
    exists_url(uid)

#    if exists_url(uid) is False:
#        return HttpResponse()
#    if request.user:
#        return HttpResponse()

    proxy_url = 'http://%s/' % env['HTTP_HOST']
    method = env['REQUEST_METHOD']
    wsgi_input = env['wsgi.input']
    path = "%(fqdn)s/%(path_info)s%(query_string)s" % {
        'fqdn': fqdn,
        'path_info': path_info,
        'query_string': env['QUERY_STRING'] and '?'+env['QUERY_STRING']
    }

    if (method, path) == ('GET', ''):
        res = HttpResponse()

    elif method == 'GET':
        res = HttpResponse(url_read(path))

    elif (method, path) == ('POST', ''):
        key, value = wsgi_input.read().strip().split('=')
        assert key == 'url', repr(key)
        res = HttpResponseRedirect(join(proxy_url, unquote(value)))

    elif method == 'POST':
        res = HttpResponseNotFound()
        
    else:
        res = HttpResponse()
        res.status_code = 501

    return res