Perl :: Dancer如何在URI中包含文件路径作为参数 [英] Perl::Dancer how to include a file path as a parameter in the URI

查看:114
本文介绍了Perl :: Dancer如何在URI中包含文件路径作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Dancer框架和Web应用程序一般还是陌生的.我有一个Dancer项目,其中有一个接受多个参数的路线.到目前为止,没有汗水.但是,如果其中一个参数将文件路径作为其值,则找不到路由.

I'm new to the Dancer framework and web apps in general. I have a Dancer project in which I have a route that accepts multiple parameters. So far, no sweat. However, if one of the parameters has a file path as its value then the route is not found.

我尝试如下编码参数字符串以消除正斜杠:

I have tried encoding the parameter string as follows to eliminate the forward slashes:

$paramString =~ s/\//%2F/g;

并且确实按预期对斜杠进行了编码(我将其打印在日志中以确保).

and this does encode the slashes as expected (I print it out in the log to make sure).

但是,在将参数字符串附加到我感兴趣的路由的基本URI之后,该URI以未编码的形式显示在浏览器中,并引发404错误,并且日志显示未编码的路由可以找不到.

However, after the parameter string is appended to the base URI for the route I'm interested in, the URI shows up in the browser in unencoded form, a 404 error is raised and the log says that the unencoded route can't be found.

我查看了Request.pm模块,发现在init方法中调用了一个名为_url_decode的私有方法,该方法删除了编码.有不需要的时候可以禁用它吗?

I looked into the Request.pm module and found that in the init method a private method called _url_decode is called which removes the encoding. Is there a way to disable this when it is not desired?

我还尝试使用uri_for方法创建URI.在这种情况下,已编码的URI确实会显示在浏览器中,但是,仍然找不到路由,并且日志表明正在使用未编码的形式(带有正斜杠)来搜索路由

I also tried using the uri_for method to create the URI. In this case, the encoded URI does show up in the browser, however, the route is still not found and the log indicates that the unencoded form (with the forward slashes) is being used to search for the route

Trying to match 'GET /exome_proj_config/project_type=exome&project_root=/usr/local/projects/users/pdagosto/projects&analysis_project_name=Test' against /^\/exome_proj_config\/([^\/]+)$/ (generated from '/exome_proj_config/:project_type:project_root:analysis_project_name') in /home/pdagosto/perl5/lib/perl5/Dancer/Route.pm l. 84 here

由于用于匹配的正则表达式显然是在基本URI末尾的字符串之后没有正斜杠的情况下寻找字符串,因此很显然,永远不会找到该路由.

Since the regex used for the match is clearly looking for a string without any forward slashes following the one at the end of the base URI it's clear that the route will never be found.

是否可以使用包含路径的URI参数?还是必须使用其他方法?

Is there a way to have a URI parameter that contains a path or must some other approach be used?

推荐答案

如果参数是查询字符串而不是路径的一部分,则可以在文件名中包含带有文件路径或斜杠的URI. (请参见 http://en.wikipedia.org/wiki/Uniform_resource_locator .)

It is possible to have a URI with a file path or slashes in the parameter provided that the parameter is part of the query string rather than the path. (See http://en.wikipedia.org/wiki/Uniform_resource_locator.)

例如,请参见以下Dancer脚本:

For example see this Dancer script:

use strict;
use warnings;
use Dancer;

get '/file/action/:action' => sub {
    my $filename = param('filename');
    my $action = param('action');
    return "Filename is $filename and action is $action";
};

dance;

如果将此字符串放入浏览器

If you put this string into the browser

http://localhost:3000/file/action/delete?filename=/folder/filename.txt

然后您应该会看到以下内容:

then you should expect to see this:

Filename is /folder/filename.txt and action is delete

在您的问题中,您显示的网址使用&字符来分隔参数,但是看起来您需要一个?字符优先,以将查询字符串与路径分开.从您的问题尚不清楚如何创建请求-但是只要您可以将文件名放在URL的查询字符串部分中,那么上述方法就可以使用.

In your question the URL you show uses the & character to separate parameters but it looks like you need a ? character first to separate the query string from the path. It is unclear from your question how you are creating your requests - but provided you can put the filename in the query string part of the URL then the approach above should work.

这篇关于Perl :: Dancer如何在URI中包含文件路径作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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