blib/lib/Apache/DoCoMoProxy.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Apache::DoCoMoProxy;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 $VERSION = '0.01';
6  
7 =head1 NAME
8  
9 Apache::DoCoMoProxy - NTT DoCoMo HTTP gateway clone for mod_proxy
10  
11 =head1 SYNOPSIS
12  
13 # in httpd.conf
14 PerlAuthenHandler Apache::DoCoMoProxy
15  
16 =head1 DESCRIPTION
17  
18 NTT DoCoMo i-mode terminals use original http gateway.
19 Apache::DoCoMoProxy emulates it. GET or POST uid=NULLGWDOCOMO
20 parameter changes terminal id.
21  
22 At first time of proxy request, basic auth required.
23 Input terminal id and user agent(comma separate).
24 password anyone.
25  
26 ex.)
27
28 account: AZ0826YK,DoCoMo/1.0/N503i/c30
29 password: (none)
30  
31 =head1 AUTHOR
32  
33 Hiroyuki Kobayashi <kobayasi@piano.gs>
34  
35 This library is free software; you can redistribute it and/or modify
36 it under the same terms as Perl itself.
37  
38 This module comes B<WITHOUT ANY WARRANTY>.
39  
40 =head1 SEE ALSO
41  
42 L<Apache::ProxyPassThru>
43  
44 =cut
45  
46 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 0 ▶use Apache::Constants qw(:common);
Subroutine BEGIN: called CC=1 SCAR=2.6
48 use Apache::Request;
49 use LWP::UserAgent();
50  
51 sub handler {
52 my $r = shift;
53 my ($res,$sent_pw) = $r->get_basic_auth_pw();
54 return $res if $res != OK;
55 my $user = $r->connection->user;
56  
57 $r->handler('perl-script');
58 $r->push_handlers(PerlHandler => \&proxy_handler);
59
60 return OK;
61 }
62  
63 sub proxy_handler
64 {
65 my ($r) = @_;
66 my($key,$val);
67  
68 my ($user,$ua) = split(m|,|,$r->connection->user);
69 $ua ||= 'DoCoMo/1.0/N503i/c30';
70  
71 my $request = new HTTP::Request $r->method, get_filter($user,$r->uri);
72  
73 my(%headers_in) = $r->headers_in;
74 while(($key,$val) = each %headers_in) {
75 $request->header($key,$val);
76 }
77  
78 $request->header('user-agent' => $ua);
79  
80 if ($r->method eq 'POST') {
81 my $len = $headers_in{'Content-Length'};
82 my $buff = '';
83 $r->read($buff,$len);
84 $request->content(post_filter($user,$buff,$r));
85 }
86  
87 my $res = (new LWP::UserAgent)->simple_request($request);
88 $r->content_type($res->header('Content-type'));
89  
90 $r->status($res->code);
91 $r->status_line($res->status_line);
92 $res->scan(sub {
93 $r->header_out(@_);
94 });
95  
96 $r->send_http_header();
97 $r->print($res->content);
98  
99 return OK;
100 }
101  
102 sub get_filter
103 {
104 my $uid = shift;
105 my $uri = shift;
106 my $url = URI::URL->new($uri);
107 my %args = $url->query_form();
108  
109 if( $args{uid} eq 'NULLGWDOCOMO'){
110 $args{uid} = $uid;
111 $url->query_form(%args);
112 }
113 return $url;
114 }
115  
116 sub post_filter
117 {
118 my ($uid,$buff,$r) = @_;
119 my $q = URI::URL->new("?$buff");
120 my %args = $q->query_form();
121
122 if( $args{uid} eq 'NULLGWDOCOMO' ){
123 $args{uid} = $uid;
124 }
125 $q->query_form(%args);
126  
127 return $q->query;
128 }
129  
130 1;
↑ Summary