Weixin/Request.pm untested

filter: Statementstmt 0.0% 0 / 152 Branchbran 0.0% 0 / 100 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 334 CC 51 SCAR 61.3
CC
51
Cov
0%
_http_request
930.0
_ua_debug
110.0
load_cookie
30.0
CRAP 457.9
1 package Mojo::Weixin::Request;
2 use Mojo::Util ();
3 use List::Util qw(first);
4 sub gen_url{
5 my $self = shift;
6 my ($url,@query_string) = @_;
7 my @query_string_pairs;
8 while(@query_string){
9 my $key = shift(@query_string);
10 my $val = shift(@query_string);
11 $key = "" if not defined $key;
12 $val = "" if not defined $val;
13 push @query_string_pairs , $key . "=" . $val;
14 }
15 return $url . '?' . join("&",@query_string_pairs);
16 }
17  
18 sub gen_url2{
19 my $self = shift;
20 my ($url,@query_string) = @_;
21 my @query_string_pairs;
22 while(@query_string){
23 my $key = shift(@query_string);
24 my $val = shift(@query_string);
25 $key = "" if not defined $key;
26 $val = "" if not defined $val;
27 push @query_string_pairs , $key . "=" . Mojo::Util::url_escape($val);
28 }
29 return $url . '?' . join("&",@query_string_pairs);
30 }
31  
32 sub http_get{
33 my $self = shift;
34 return $self->_http_request("get",@_);
35 }
36 sub http_post{
37 my $self = shift;
38 return $self->_http_request("post",@_);
39 }
40 sub _ua_debug {
41 my ($self,$ua,$tx,$opt,$is_blocking) = @_;
42 return if not $opt->{ua_debug};
43 $self->print("-- " . ($is_blocking?"Blocking":"Non-blocking"). " request (@{[$tx->req->url->to_abs]})\n");
44  
45 if($opt->{ua_debug_req_body}){#是否打印请求body
46 my $req_content_type = eval {$tx->req->headers->content_type};
47 if(defined $req_content_type and $req_content_type =~ /^multipart\/form-data; boundary=(.+?)$/){#对于文件上传不打印body中的二进制
48 my $body = $tx->req->build_body;
49 my $boundary = "--".$1;
50 my $filename_pos = index($body,"filename=");
51 if($filename_pos != -1){
52 my $binary_start_pos = index($body,"\r\n\r\n",$filename_pos);
53 if($binary_start_pos!=-1){
54 my $binary_end_pos = index($body,$boundary,$binary_start_pos);
55 substr($body,$binary_start_pos,$binary_end_pos-$binary_start_pos+1,"\r\n\r\n[binary data not shown]\r\n") if $binary_end_pos != -1;
56 }
57 }
58 $self->print("-- Client >>> Server (@{[$tx->req->url->to_abs]})\n@{[$tx->req->build_start_line . $tx->req->build_headers]}\n$body\n");
59 }
60 else{#其他非文件上传的请求,打印完整的header和body
61 $self->print("-- Client >>> Server (@{[$tx->req->url->to_abs]})\n@{[$tx->req->to_string]}\n");
62 }
63
64 }
65 else{
66 $self->print("-- Client >>> Server (@{[$tx->req->url->to_abs]})\n@{[$tx->req->build_start_line . $tx->req->build_headers]}\n[body data skipped]\n");
67 }
68  
69 if($opt->{ua_debug_res_body}){
70 my $res_content_type = eval {$tx->res->headers->content_type};
71 if(defined $res_content_type and $res_content_type =~m#^(image|video|auido)/|^application/octet-stream#){
72 $self->print("-- Server >>> Client (@{[$tx->req->url->to_abs]})\n@{[$tx->res->build_start_line . $tx->res->build_headers]}\n[binary data not shown]");
73 }
74 else{
75 $self->print("-- Server >>> Client (@{[$tx->req->url->to_abs]})\n@{[$tx->res->to_string]}\n");
76 }
77 }
78 else{
79 $self->print("-- Server >>> Client (@{[$tx->req->url->to_abs]})\n@{[$tx->res->build_start_line . $tx->res->build_headers]}\n[body data skipped]\n");
80 }
81 }
82 sub _http_request{
83 my $self = shift;
84 my $method = shift;
85 my %opt = (
86 json => 0,
87 blocking => 0,
88 ua_retry_times => $self->ua_retry_times,
89 #ua_connect_timeout => $self->ua_connect_timeout,
90 #ua_request_timeout => $self->ua_request_timeout,
91 #ua_inactivity_timeout => $self->ua_inactivity_timeout,
92 ua_debug => $self->ua_debug,
93 ua_debug_res_body => $self->ua_debug_res_body,
94 ua_debug_req_body => $self->ua_debug_req_body
95 );
96 if(ref $_[1] eq "HASH"){#with header or option
97 $opt{json} = delete $_[1]->{json} if defined $_[1]->{json};
98 $opt{blocking} = delete $_[1]->{blocking} if defined $_[1]->{blocking};
99 $opt{ua_retry_times} = delete $_[1]->{ua_retry_times} if defined $_[1]->{ua_retry_times};
100 $opt{ua_debug} = delete $_[1]->{ua_debug} if defined $_[1]->{ua_debug};
101 $opt{ua_debug_res_body} = delete $_[1]->{ua_debug_res_body} if defined $_[1]->{ua_debug_res_body};
102 $opt{ua_debug_req_body} = delete $_[1]->{ua_debug_req_body} if defined $_[1]->{ua_debug_req_body};
103 $opt{ua_connect_timeout} = delete $_[1]->{ua_connect_timeout} if defined $_[1]->{ua_connect_timeout};
104 $opt{ua_request_timeout} = delete $_[1]->{ua_request_timeout} if defined $_[1]->{ua_request_timeout};
105 $opt{ua_inactivity_timeout} = delete $_[1]->{ua_inactivity_timeout} if defined $_[1]->{ua_inactivity_timeout};
106 }
107 if(ref $_[-1] eq "CODE" and !$opt{blocking}){
108 $self->debug("issue request[$method] to [$_[0]]");
109 my $cb = pop;
110 return $self->ua->$method(@_,sub{
111 my($ua,$tx) = @_;
112 _ua_debug($self,$ua,$tx,\%opt,0) if $opt{ua_debug};
113 $self->save_cookie();
114 if(defined $tx and $tx->res->is_success){
115 my $r = $opt{json}?$self->from_json($tx->res->body):$tx->res->body;
116 $cb->($r,$ua,$tx);
117 }
118 elsif(defined $tx){
119 $self->warn($tx->req->url->to_abs . " 请求失败: " . ($tx->error->{code}||"-") . " " . $self->encode_utf8($tx->error->{message}));
120 $cb->(undef,$ua,$tx);
121 }
122 });
123 }
124 else{
125 my $tx;
126 my $cb = pop if ref $_[-1] eq "CODE";
127 for(my $i=0;$i<=$opt{ua_retry_times};$i++){
128  
129 $self->debug("issue request[$method] to [$_[0]]");
130 #fix bug Mojo::IOLoop already running Mojo/UserAgent.pm
131 #https://github.com/kraih/mojo/issues/1029
132 $self->ua->ioloop->stop if $self->ua->ioloop->is_running;
133  
134 if($opt{ua_connect_timeout} or $opt{ua_request_timeout} or $opt{ua_inactivity_timeout}){
135 my $connect_timeout = $self->ua->connect_timeout;
136 my $request_timeout = $self->ua->request_timeout;
137 my $inactivity_timeout = $self->ua->inactivity_timeout;
138 $self->ua->connect_timeout($opt{ua_connect_timeout}) if $opt{ua_connect_timeout};
139 $self->ua->request_timeout($opt{ua_request_timeout}) if $opt{ua_request_timeout};
140 $self->ua->inactivity_timeout($opt{ua_inactivity_timeout}) if $opt{ua_inactivity_timeout};
141 $tx = $self->ua->$method(@_);
142 $self->ua->connect_timeout($connect_timeout)
143 ->request_timeout($request_timeout)
144 ->inactivity_timeout($inactivity_timeout);
145 }
146 else{
147 $tx = $self->ua->$method(@_);
148 }
149 _ua_debug($self,$ua,$tx,\%opt,1) if $opt{ua_debug};
150 $self->save_cookie();
151 if(defined $tx and $tx->res->is_success){
152 my $r = $opt{json}?$self->from_json($tx->res->body):$tx->res->body;
153 $cb->($r,$ua,$tx) if defined $cb;
154 return wantarray?($r,$self->ua,$tx):$r;
155 }
156 elsif(defined $tx){
157 $self->warn($tx->req->url->to_abs . " 请求($i/$opt{ua_retry_times})失败: " . ($tx->error->{code} || "-") . " " . $self->encode_utf8($tx->error->{message}));
158 next;
159 }
160 }
161 #$self->warn($tx->req->url->to_abs . " 请求最终失败: " . ($tx->error->{code}||"-") . " " . $self->encode_utf8($tx->error->{message})) if defined $tx;
162 $cb->($r,$ua,$tx) if defined $cb;
163 return wantarray?(undef,$self->ua,$tx):undef;
164 }
165 }
166  
167 sub load_cookie{
168 my $self = shift;
169 return if not $self->keep_cookie;
170 my $cookie_jar;
171 my $cookie_path = $self->cookie_path;
172 return if not -f $cookie_path;
173 eval{require Storable;$cookie_jar = Storable::retrieve($cookie_path);};
174 if($@){
175 $self->warn("客户端加载cookie[ $cookie_path ]失败: $@");
176 return;
177 }
178 else{
179 $self->info("客户端加载cookie[ $cookie_path ]");
180 $self->ua->cookie_jar($cookie_jar);
181  
182 #更新账号主域名
183 my $domain = $self->search_cookie('wxuin','domain') // $self->search_cookie('wxsid','domain');
184 if ($domain and $domain ne $self->domain){
185 $self->domain( $domain );
186 $self->debug("账号域名更新为:$domain");
187 }
188 }
189  
190 }
191 sub save_cookie{
192 my $self = shift;
193 return if not $self->keep_cookie;
194 my $cookie_path = $self->cookie_path;
195 eval{require Storable;Storable::nstore($self->ua->cookie_jar,$cookie_path);};
196 $self->warn("客户端保存cookie[ $cookie_path ]失败: $@") if $@;
197 }
198  
199 sub search_cookie{
200 my $self = shift;
201 my $cookie = shift;
202 my $type = shift // 'value'; #默认查询cookie对应的值
203 my @cookies;
204 my @tmp = $self->ua->cookie_jar->all;
205 if(@tmp == 1 and ref $tmp[0] eq "ARRAY"){
206 @cookies = @{$tmp[0]};
207 }
208 else{
209 @cookies = @tmp;
210 }
211 my $c = first { $_->name eq $cookie} @cookies;
212 return defined $c?$c->$type:undef;
213 }
214 sub clear_cookie{
215 my $self = shift;
216 $self->info("客户端清除cookie[ " . $self->cookie_path . "]");
217 $self->ua->cookie_jar->empty;
218 $self->save_cookie();
219 }
220 1;