| 1 | package Mojo::Webqq::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 | my $cb = pop; | ||
| 109 | return $self->ua->$method(@_,sub{ | ||
| 110 | my($ua,$tx) = @_; | ||
| 111 | _ua_debug($self,$ua,$tx,\%opt,0) if $opt{ua_debug}; | ||
| 112 | $self->save_cookie(); | ||
| 113 | if(defined $tx and $tx->res->is_success){ | ||
| 114 | my $r = $opt{json}?$self->from_json($tx->res->body):$tx->res->body; | ||
| 115 | $cb->($r,$ua,$tx); | ||
| 116 | } | ||
| 117 | elsif(defined $tx){ | ||
| 118 | unless( $tx->req->url->host eq 'd1.web2.qq.com' | ||
| 119 | and $tx->req->url->path eq '/channel/poll2' | ||
| 120 | and first { $tx->res->code == $_ } @{$self->ignore_poll_http_code} | ||
| 121 | ){ | ||
| 122 | $self->warn($tx->req->url->to_abs . " 请求失败: " . ($tx->error->{code}||"-") . " " . $self->encode_utf8($tx->error->{message})); | ||
| 123 | $cb->(undef,$ua,$tx); | ||
| 124 | } | ||
| 125 | } | ||
| 126 | }); | ||
| 127 | } | ||
| 128 | else{ | ||
| 129 | my $tx; | ||
| 130 | my $cb = pop if ref $_[-1] eq "CODE"; | ||
| 131 | for(my $i=1;$i<=$opt{ua_retry_times};$i++){ | ||
| 132 | |||
| 133 | #fix bug Mojo::IOLoop already running Mojo/UserAgent.pm | ||
| 134 | #https://github.com/kraih/mojo/issues/1029 | ||
| 135 | $self->ua->ioloop->stop if $self->ua->ioloop->is_running; | ||
| 136 | |||
| 137 | if($opt{ua_connect_timeout} or $opt{ua_request_timeout} or $opt{ua_inactivity_timeout}){ | ||
| 138 | my $connect_timeout = $self->ua->connect_timeout; | ||
| 139 | my $request_timeout = $self->ua->request_timeout; | ||
| 140 | my $inactivity_timeout = $self->ua->inactivity_timeout; | ||
| 141 | $self->ua->connect_timeout($opt{ua_connect_timeout}) if $opt{ua_connect_timeout}; | ||
| 142 | $self->ua->request_timeout($opt{ua_request_timeout}) if $opt{ua_request_timeout}; | ||
| 143 | $self->ua->inactivity_timeout($opt{ua_inactivity_timeout}) if $opt{ua_inactivity_timeout}; | ||
| 144 | $tx = $self->ua->$method(@_); | ||
| 145 | $self->ua->connect_timeout($connect_timeout) | ||
| 146 | ->request_timeout($request_timeout) | ||
| 147 | ->inactivity_timeout($inactivity_timeout); | ||
| 148 | } | ||
| 149 | else{ | ||
| 150 | $tx = $self->ua->$method(@_); | ||
| 151 | } | ||
| 152 | _ua_debug($self,$ua,$tx,\%opt,1) if $opt{ua_debug}; | ||
| 153 | $self->save_cookie(); | ||
| 154 | if(defined $tx and $tx->res->is_success){ | ||
| 155 | my $r = $opt{json}?$self->from_json($tx->res->body):$tx->res->body; | ||
| 156 | $cb->($r,$ua,$tx) if defined $cb; | ||
| 157 | return wantarray?($r,$self->ua,$tx):$r; | ||
| 158 | } | ||
| 159 | elsif(defined $tx){ | ||
| 160 | $self->warn($tx->req->url->to_abs . " 请求($i/$opt{ua_retry_times})失败: " . ($tx->error->{code} || "-") . " " . $self->encode_utf8($tx->error->{message})); | ||
| 161 | next; | ||
| 162 | } | ||
| 163 | } | ||
| 164 | #$self->warn($tx->req->url->to_abs . " 请求最终失败: " . ($tx->error->{code}||"-") . " " . $self->encode_utf8($tx->error->{message})) if defined $tx; | ||
| 165 | $cb->($r,$ua,$tx) if defined $cb; | ||
| 166 | return wantarray?(undef,$self->ua,$tx):undef; | ||
| 167 | } | ||
| 168 | } | ||
| 169 | |||
| 170 | sub load_cookie{ | ||
| 171 | my $self = shift; | ||
| 172 | return if not $self->keep_cookie; | ||
| 173 | my $cookie_jar; | ||
| 174 | my $cookie_path = $self->cookie_path; | ||
| 175 | return if not -f $cookie_path; | ||
| 176 | eval{require Storable;$cookie_jar = Storable::retrieve($cookie_path);}; | ||
| 177 | if($@){ | ||
| 178 | $self->warn("客户端加载cookie[ $cookie_path ]失败: $@"); | ||
| 179 | return; | ||
| 180 | } | ||
| 181 | else{ | ||
| 182 | $self->info("客户端加载cookie[ $cookie_path ]"); | ||
| 183 | $self->ua->cookie_jar($cookie_jar); | ||
| 184 | } | ||
| 185 | } | ||
| 186 | sub save_cookie{ | ||
| 187 | my $self = shift; | ||
| 188 | return if not $self->keep_cookie; | ||
| 189 | my $cookie_path = $self->cookie_path; | ||
| 190 | eval{require Storable;Storable::nstore($self->ua->cookie_jar,$cookie_path);}; | ||
| 191 | $self->warn("客户端保存cookie[ $cookie_path ]失败: $@") if $@; | ||
| 192 | } | ||
| 193 | |||
| 194 | sub search_cookie{ | ||
| 195 | my $self = shift; | ||
| 196 | my $cookie = shift; | ||
| 197 | my @cookies; | ||
| 198 | my @tmp = $self->ua->cookie_jar->all; | ||
| 199 | if(@tmp == 1 and ref $tmp[0] eq "ARRAY"){ | ||
| 200 | @cookies = @{$tmp[0]}; | ||
| 201 | } | ||
| 202 | else{ | ||
| 203 | @cookies = @tmp; | ||
| 204 | } | ||
| 205 | my $c = first { $_->name eq $cookie} @cookies; | ||
| 206 | return defined $c?$c->value:undef; | ||
| 207 | } | ||
| 208 | sub clear_cookie{ | ||
| 209 | my $self = shift; | ||
| 210 | $self->ua->cookie_jar->empty; | ||
| 211 | $self->save_cookie(); | ||
| 212 | } | ||
| 213 | 1; |