Example #1
0
def fake_request(path='/', method='GET', user=None, extra={}):
    params = {
        'REQUEST_METHOD': method,
        'PATH_INFO': path,
        'wsgi.input': StringIO()
    }
    params.update(extra)

    req = WSGIRequest(params)

    req.user = user or AnonymousUser()
    req.user.id = ''
    req.user.username = '******'
    req.user.first_name = 'mock_user'
    req.user.is_superuser = True
    req.user.groups.all = lambda: [GroupFactory(id=1)]

    req.build_absolute_uri = lambda x=None: '/'

    # for sessions middleware
    req.session = build_fake_session()

    # for messages middleware
    req._messages = default_storage(req)

    req.get_host = lambda x=None: 'localhost'

    return req
Example #2
0
def fake_request(path='/', method='GET', user=True, extra={}):
    params = {
        'REQUEST_METHOD': method,
        'PATH_INFO': path,
        'wsgi.input': StringIO()
    }
    params.update(extra)

    req = WSGIRequest(params)

    if user:
        req.user = UserFactory.create(groups=(GroupFactory(), GroupFactory()))
    else:
        req.user = AnonymousUser()

    req.build_absolute_uri = lambda x=None: '/'

    # for sessions middleware
    req.session = build_fake_session()

    # for messages middleware
    req._messages = default_storage(req)

    req.get_host = lambda x=None: 'localhost'

    return req
Example #3
0
def fake_request(path='/', method='GET', user=None, extra={}):
    params = {
        'REQUEST_METHOD': method,
        'PATH_INFO': path,
        'wsgi.input': StringIO()
    }
    params.update(extra)

    req = WSGIRequest(params)

    req.user = user or AnonymousUser()
    req.user.id = ''
    req.user.username = '******'
    req.user.first_name = 'mock_user'
    req.user.is_superuser = True
    req.user.groups.all = lambda: [GroupFactory(id=1)]

    req.build_absolute_uri = lambda x=None: '/'

    # for sessions middleware
    req.session = build_fake_session()

    # for messages middleware
    req._messages = default_storage(req)

    req.get_host = lambda x=None: 'localhost'

    return req
Example #4
0
 def process_request(self, request: wsgi.WSGIRequest):
     next_url = request.path_info
     print(next_url[0:len(self.api_prefix)])
     print(request.get_host())
     if next_url[0:len(self.
                       api_prefix)] == self.api_prefix and request.get_host(
                       ) != "127.0.0.1:8001":
         print(next_url[0:len(self.login_api)])
         print(next_url)
         if next_url[0:len(self.login_api)] == self.login_api:
             return
         if request.session.get('is_login'):
             return
         else:
             ret = {"code": code.LOGIN_FILTER_NOT_LOGIN, "msg": "not login"}
             return HttpResponse(json.dumps(ret))
     else:
         return
Example #5
0
def application(environ, start_response):
    from apps.core.helpers import import_class

    # Get and initialize if needed defined offload handler
    offload_handler = environ.get('OFFLOAD_HANDLER')

    if offload_handler not in HANDLERS:
        handler = import_class(offload_handler)()
        HANDLERS[offload_handler] = handler
    else:
        handler = HANDLERS[offload_handler]

    propagator = get_tracer_propagator()
    tracer = create_tracer(propagator.from_headers(environ))

    request = WSGIRequest(environ)

    with tracer.span(name='Async.' + str(request.path)) as span:
        response = handler.application(request)

        span.span_kind = span_module.SpanKind.SERVER
        span.add_attribute(attribute_key=HTTP_HOST,
                           attribute_value=request.get_host())
        span.add_attribute(attribute_key=HTTP_METHOD,
                           attribute_value=request.method)
        span.add_attribute(attribute_key=HTTP_PATH,
                           attribute_value=str(request.path))
        span.add_attribute(attribute_key=HTTP_ROUTE,
                           attribute_value=str(request.path))
        span.add_attribute(attribute_key=HTTP_URL,
                           attribute_value=str(request.build_absolute_uri()))
        span.add_attribute(attribute_key='async.offload_handler',
                           attribute_value=offload_handler)

        if response:
            span.add_attribute(attribute_key=HTTP_STATUS_CODE,
                               attribute_value=response.status_code)

    if not isinstance(response, HttpResponseBase):
        return response

    for k, v in DEFAULT_HEADERS:
        response[k] = v

    # If we're dealing with cross origin request, add necessary headers
    if environ.get('HTTP_ORIGIN'):
        for k, v in CORS_HEADER:
            response[k] = v

    http_status = '%s %s' % (response.status_code, response.reason_phrase)
    start_response(http_status, list(response.items()))
    return response
Example #6
0
def set_semester(request: WSGIRequest) -> HttpResponse:
    redirect_url: Optional[str] = request.POST.get("next") or request.GET.get(
        "next")
    if not url_has_allowed_host_and_scheme(url=redirect_url,
                                           allowed_hosts=request.get_host(),
                                           require_https=True):
        redirect_url = request.META.get("HTTP_REFERER")
        if not url_has_allowed_host_and_scheme(
                url=redirect_url,
                allowed_hosts=request.get_host(),
                require_https=True):
            redirect_url = "/"  # should not happen :)
    if request.method == "POST":
        semester_pk = int(request.POST.get("semester")
                          or -1)  # semester is always present
        try:
            Semester.objects.get(pk=semester_pk)
        except Semester.DoesNotExist:
            pass
        else:
            request.session[SEMESTER_SESSION_KEY] = semester_pk
    return HttpResponseRedirect(redirect_url or "/")
Example #7
0
def _get_current_domain(req: WSGIRequest) -> str:
    if 'ASSERTION_URL' in SAML2_AUTH_CONFIG:
        return SAML2_AUTH_CONFIG['ASSERTION_URL']

    return '{scheme}://{host}'.format(
        scheme='https' if req.is_secure() else 'http', host=req.get_host())
Example #8
0
def get_host(request: WSGIRequest) -> str:
    protocol = 'http'
    if request.is_secure():
        protocol = 'https'
    host = "{}://{}".format(protocol, request.get_host())
    return host