line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HTTP::Request::FromFetch; |
2
|
1
|
|
|
1
|
|
50700
|
use strict; |
|
1
|
|
|
|
|
11
|
|
|
1
|
|
|
|
|
31
|
|
3
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
25
|
|
4
|
1
|
|
|
1
|
|
6
|
use Filter::signatures; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
6
|
|
5
|
1
|
|
|
1
|
|
24
|
use feature 'signatures'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
66
|
|
6
|
1
|
|
|
1
|
|
6
|
no warnings 'experimental::signatures'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
34
|
|
7
|
1
|
|
|
1
|
|
5
|
use Carp 'croak'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
59
|
|
8
|
1
|
|
|
1
|
|
703
|
use JSON; |
|
1
|
|
|
|
|
10425
|
|
|
1
|
|
|
|
|
7
|
|
9
|
1
|
|
|
1
|
|
549
|
use PerlX::Maybe; |
|
1
|
|
|
|
|
2026
|
|
|
1
|
|
|
|
|
4
|
|
10
|
1
|
|
|
1
|
|
545
|
use HTTP::Request::CurlParameters; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
209
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '0.51'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 NAME |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
HTTP::Request::FromFetch - turn a Javascript fetch() statement into HTTP::Request |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 SYNOPSIS |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
my $ua = LWP::UserAgent->new(); |
21
|
|
|
|
|
|
|
my $req = HTTP::Request::FromFetch->new(<<'JS')->as_request; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
await fetch("https://www.example.com/index.html", { |
24
|
|
|
|
|
|
|
"credentials": "include", |
25
|
|
|
|
|
|
|
"headers": { |
26
|
|
|
|
|
|
|
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:74.0) Gecko/20100101 Firefox/74.0", |
27
|
|
|
|
|
|
|
"Accept": "text/javascript, text/html, application/xml, text/xml, */*", |
28
|
|
|
|
|
|
|
"Accept-Language": "de,en-US;q=0.7,en;q=0.3", |
29
|
|
|
|
|
|
|
"X-CSRF-Token": "secret", |
30
|
|
|
|
|
|
|
"X-Requested-With": "XMLHttpRequest" |
31
|
|
|
|
|
|
|
}, |
32
|
|
|
|
|
|
|
"referrer": "https://www.example.com/", |
33
|
|
|
|
|
|
|
"method": "GET", |
34
|
|
|
|
|
|
|
"mode": "cors" |
35
|
|
|
|
|
|
|
}); |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
JS |
38
|
|
|
|
|
|
|
$ua->request( $req ); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=head1 DESCRIPTION |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
This module parses a call to the L |
43
|
|
|
|
|
|
|
and returns an object that you can turn into a L to use |
44
|
|
|
|
|
|
|
with L or other user agents to perform a largely identical |
45
|
|
|
|
|
|
|
HTTP request. |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
The parsing of the Javascript stanza is done through a regular expression, so |
48
|
|
|
|
|
|
|
the test must largely follow the pattern shown in the synopsis. Usually, the |
49
|
|
|
|
|
|
|
C stanzas come from a browsers "Copy as fetch" context menu, so there |
50
|
|
|
|
|
|
|
is no problem parsing these. |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
This is mostly a factory class for L objects. |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=cut |
55
|
|
|
|
|
|
|
|
56
|
5
|
|
|
5
|
0
|
331484
|
sub new( $class, $fetch, @rest ) { |
|
5
|
|
|
|
|
17
|
|
|
5
|
|
|
|
|
12
|
|
|
5
|
|
|
|
|
15
|
|
|
5
|
|
|
|
|
10
|
|
57
|
5
|
|
|
|
|
14
|
my %options; |
58
|
|
|
|
|
|
|
|
59
|
5
|
50
|
|
|
|
23
|
if( @rest ) { |
60
|
0
|
|
|
|
|
0
|
%options = ($fetch, @rest); |
61
|
|
|
|
|
|
|
} else { |
62
|
5
|
|
|
|
|
54
|
$options{ fetch } = $fetch; |
63
|
|
|
|
|
|
|
}; |
64
|
|
|
|
|
|
|
|
65
|
5
|
|
|
|
|
21
|
$fetch = delete $options{ fetch }; |
66
|
|
|
|
|
|
|
|
67
|
5
|
50
|
|
|
|
122
|
$fetch =~ m!\A\s*(await\s+)? |
68
|
|
|
|
|
|
|
fetch\s*\(\s*"(?(?:[^[\\"]+|\\.)+)"\s*(?:,\s* |
69
|
|
|
|
|
|
|
(?\{.*\}))?\s* |
70
|
|
|
|
|
|
|
\)\s*;? |
71
|
|
|
|
|
|
|
\s*\z!msx |
72
|
|
|
|
|
|
|
or croak "Couldn't parse fetch string '$fetch'"; |
73
|
|
|
|
|
|
|
|
74
|
5
|
|
|
|
|
30
|
my $options; |
75
|
1
|
|
|
1
|
|
506
|
my $o = $+{options}; |
|
1
|
|
|
|
|
366
|
|
|
1
|
|
|
|
|
207
|
|
|
5
|
|
|
|
|
95
|
|
76
|
5
|
|
|
|
|
90
|
my $u = $+{uri}; |
77
|
5
|
100
|
66
|
|
|
82
|
if( defined $o and $o =~ /\S/ ) { |
78
|
3
|
|
|
|
|
77
|
$options = decode_json($o); |
79
|
|
|
|
|
|
|
} else { |
80
|
2
|
|
|
|
|
6
|
$options = {}; |
81
|
|
|
|
|
|
|
}; |
82
|
|
|
|
|
|
|
|
83
|
5
|
|
|
|
|
20
|
$options->{uri} = $u; |
84
|
5
|
|
100
|
|
|
44
|
$options->{method} ||= 'GET'; |
85
|
5
|
|
100
|
|
|
83
|
$options->{mode} ||= 'cors'; |
86
|
5
|
|
50
|
|
|
98
|
$options->{cache} ||= 'default'; |
87
|
5
|
|
100
|
|
|
69
|
$options->{credentials} ||= 'same-origin'; |
88
|
5
|
|
100
|
|
|
64
|
$options->{headers} ||= {}; |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
HTTP::Request::CurlParameters->new({ |
92
|
|
|
|
|
|
|
method => delete $options->{method} || 'GET', |
93
|
|
|
|
|
|
|
uri => $options->{uri}, |
94
|
|
|
|
|
|
|
headers => $options->{headers}, |
95
|
|
|
|
|
|
|
maybe body => $options->{body}, |
96
|
|
|
|
|
|
|
#maybe credentials => $options->{ user }, |
97
|
5
|
|
50
|
|
|
430
|
}); |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
1; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head1 SEE ALSO |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
L |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head1 REPOSITORY |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
The public repository of this module is |
109
|
|
|
|
|
|
|
L. |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head1 SUPPORT |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
The public support forum of this module is |
114
|
|
|
|
|
|
|
L. |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head1 BUG TRACKER |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
Please report bugs in this module via the RT CPAN bug queue at |
119
|
|
|
|
|
|
|
L |
120
|
|
|
|
|
|
|
or via mail to L. |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head1 AUTHOR |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
Max Maischein C |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head1 COPYRIGHT (c) |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
Copyright 2018 by Max Maischein C. |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head1 LICENSE |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
This module is released under the same terms as Perl itself. |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=cut |