bch*_*gys 4 url perl encoding uri lwp
我正在谈论似乎是一个损坏的 HTTP 守护程序,我需要发出一个在 URLGET中包含管道|字符的请求。
LWP::UserAgent 在发送请求之前转义管道字符。
例如,传入的 URL 为:
https://hostname/url/doSomethingScript?ss=1234&activities=Lec1|01
Run Code Online (Sandbox Code Playgroud)
传递给 HTTP 守护程序作为
https://hostname/url/doSomethingScript?ss=1234&activities=Lec1%7C01
Run Code Online (Sandbox Code Playgroud)
这是正确的,但不适用于这个损坏的服务器。
如何覆盖或绕过 LWP 及其朋友正在执行的编码?
笔记
我已经在 StackOverflow 上看到并尝试了其他解决类似问题的答案。这里的区别似乎在于,这些答案是在处理POST请求,其中formfieldURL的部分可以作为键/值对数组或作为'Content' => $content参数传递。对于 LWP 请求,这些方法对我不起作用。
我还尝试构建一个HTTP::Request对象并将其传递给 LWP,并将完整的 URL 直接传递给LWP->get(). 两种方法都没有骰子。
为了响应鲍罗丁的要求,这是我正在使用的代码的清理版本
#!/usr/local/bin/perl -w
use HTTP::Cookies;
use LWP;
my $debug = 1;
# make a 'browser' object
my $browser = LWP::UserAgent->new();
# cookie handling...
$browser->cookie_jar(HTTP::Cookies->new(
'file' => '.cookie_jar.txt',
'autosave' => 1,
'ignore_discard' => 1,
));
# proxy, so we can watch...
if ($debug == 1) {
$browser->proxy(['http', 'ftp', 'https'], 'http://localhost:8080/');
}
# user agent string (pretend to be Firefox)
$agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.7.12) Gecko/20050919 Firefox/1.0.7';
# set the user agent
$browser->agent($agent);
# do some things here to log in to the web site, accept session cookies, etc.
# These are basic POSTs of filled forms. Works fine.
# [...]
my $baseURL = 'https://hostname/url/doSomethingScript?ss=1234&activities=VALUEA|VALUEB';
@values = ['Lec1', '01', 'Lec1', '02'];
while (1) {
if (scalar(@values) < 2) { last; }
my $vala = shift(@values);
my $valb = shift(@values);
my $url = $basEURL;
$url =~ s/VALUEA/$vala/g;
$url =~ s/VALUEB/$valb/g;
# simplified. Would usually check request for '200' response, etc...
$content = $browser->get($url)->content();
# do something here with the content
# [...]
# fails because the '|' character in the url is escaped after it's handed
# to LWP
}
# end
Run Code Online (Sandbox Code Playgroud)
正如@bchgys 在他的评论中提到的,这(几乎)在链接的线程中得到了回答。这里有两个解决方案:
第一个并且可以说是最干净的方法是在本地覆盖 URI::Escape 中的转义映射以不修改管道字符:
use URI;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new();
my $res;
{
# Violate RFC 2396 by forcing broken query string
# local makes the override take effect only in the current code block
local $URI::Escape::escapes{'|'} = '|';
$res = $ua->get('http://server/script?q=a|b');
}
print $res->request->as_string, "\n";
Run Code Online (Sandbox Code Playgroud)
或者,您可以通过在创建请求后直接在请求中修改 URI 来简单地撤消转义:
use HTTP::Request;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new();
my $req = HTTP::Request->new(GET => 'http://server/script?q=a|b');
# Violate RFC 2396 by forcing broken query string
${$req->uri} =~ s/%7C/|/;
my $res = $ua->request($req);
print $res->request->as_string, "\n";
Run Code Online (Sandbox Code Playgroud)
第一个解决方案几乎肯定是更可取的,因为它至少依赖于%URI::Escape::escapes导出和记录的包变量,因此这可能与您将要使用受支持的 API 执行此操作一样接近。
请注意,无论哪种情况,您都违反了 RFC 2396,但如前所述,在与您无法控制的损坏服务器交谈时,您可能别无选择。
| 归档时间: |
|
| 查看次数: |
1872 次 |
| 最近记录: |