在 Django 代理中间件中允许逐跳标头 [英] Allow hop-by-hop headers in Django proxy middleware

查看:59
本文介绍了在 Django 代理中间件中允许逐跳标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 Django 中实现一个 HTTP 代理,我的 Google safari 将我带到了一个名为 django 的项目-网络代理.

I need to implement a HTTP proxy in Django and my Google safari led me to a project called django-webproxy.

虽然不再维护,但很简单.大部分逻辑依赖于一个简单的代理中间件类,该类拦截所有对 Django WSGI 服务器的请求并进行处理.

Although no longer maintained, it's quite simple. Most of the logic relies on a simple proxy Middleware class that intercepts all requests to the Django WSGI server and handles it.

如果中间件返回任何数据,WSGI 服务器只是将它传回客户端,但如果它什么都不返回,Django 只是通过传递给其他中间件来处理请求.

If the Middleware returns any data, the WSGI server simply passes it back to the client but if it returns nothing, Django simply handles the request by passing to the other Middleware.

几乎一切正常,但我需要实现代理身份验证,这意味着我必须使用 Proxy-Authenticate 标头向客户端发送 407 状态代码.这是 Django 不允许的,因为它是一个逐跳的标头,并且 Django 会抛出异常.我怎样才能 hack/force/kludge Django 允许我发送 hop-by-hop 标头?

Everything works fine, pretty much, but I need to implement proxy authentication which mean i have to send a 407 status code to the client with a Proxy-Authenticate header. This sin't allowed by Django as it is a hop-by-hop header and Django throws an exception. How can i hack/force/kludge Django into allowing me to send hop-by-hop headers?

仅供参考,可以找到中间件类的代码此处.

FYI, ihe code for the middleware class can be found here.

推荐答案

from django.core.servers import basehttp

del basehttp._hop_headers['proxy-authenticate']
del basehttp._hop_headers['proxy-authorization']

这对我有用.

这篇关于在 Django 代理中间件中允许逐跳标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆