金字塔替换 URL Matchdict 中的双正斜杠 [英] Pyramid Replacing Double Forward-Slash in URL Matchdict

查看:25
本文介绍了金字塔替换 URL Matchdict 中的双正斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本质上,我只是在 Pyramid 内部构建了一个 API 重定向路由,以在不使用 JSONP 的情况下处理跨域 AJAX 请求.

Essentially, I'm just building an API redirection route inside of Pyramid to process cross-domain AJAX requests without using JSONP.

我添加了一条路线,如下所示:

I've added a route, like so:

config.add_route("api","/api/{url:.*}")

我想用它来捕获这样的 URL:

with which I want to capture URLs like so:

http://domain.com/api/http://location.of/other/api

但是,当从 Request matchdict 中抓取捕获的 URL 后缀时,我得到以下信息:

However, when grabbing the captured URL suffix out of the Request matchdict, I get the following:

http:/location.of/other/api

我猜在 URL 处理/匹配过程中进行了一些转义?如何避免这种情况,并使用两个正斜杠获取所需的 URL?

I'm guessing some escaping has been done during URL processing/matching? How can I avoid this, and get the desired URL with two forward slashes?

即使我将 URL 作为 GET 参数传入,问题仍然存在.也许这与 Pyramid 的 multidicts 的工作方式有关?

推荐答案

这是任何基于 WSGI 的应用程序的基本限制.在将 URL 传递给 WSGI 应用程序之前,对 URL 进行 urldecode 并压缩斜杠.如果要保留斜杠,则需要对它们进行两次 urlencode.AFAIK 使用查询字符串无法解决此问题.

This is a fundamental limitation of any WSGI-based application. URLs are urldecoded and slashes are compacted before the URL is passed to the WSGI app. If you want to preserve the slashes you will need to urlencode them twice. AFAIK there is no way around this using a query string.

我想我应该指出原始 URL 是可用的,但是您必须从中解析出您自己关心的部分.它在 request.url 中.request.path_info 是 Pyramid 和大多数 WSGI 应用程序用来分派 URL 的,因为它只包含与应用程序安装位置相关的子路径.

I guess I should point out that the original URL is available, but from it you will have to parse out the part you care about yourself. It is in request.url. request.path_info is what Pyramid and most WSGI apps use to dispatch URLs because it contains only the sub-path that is relative to where the app is mounted.

这篇关于金字塔替换 URL Matchdict 中的双正斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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