File: Client.pm

package info (click to toggle)
tiarra 20100212-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,732 kB
  • ctags: 1,712
  • sloc: perl: 32,032; lisp: 193; sh: 109; makefile: 10
file content (185 lines) | stat: -rw-r--r-- 4,327 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
## ----------------------------------------------------------------------------
#  Tools::HTTPServer.
# -----------------------------------------------------------------------------
# Mastering programmed by YAMASHINA Hio
#
# Copyright 2008 YAMASHINA Hio
# -----------------------------------------------------------------------------
# $Id: Client.pm 9215 2008-04-09 14:37:50Z hio $
# -----------------------------------------------------------------------------
package Tools::HTTPServer::Client;
use strict;
use warnings;
use Tiarra::Socket::Buffered;
use base 'Tiarra::Socket::Buffered';
use Tools::HTTPParser;
use Module::Use qw(Tools::HTTPParser);

use Scalar::Util qw(weaken);

our $HAS_HTTP_PARSER ||= do{
  eval {
    local($SIG{__DIE__}) = "DEFAULT";
    require HTTP::Parser;
    1;
  };
  $@ ? 0 : 1;
};
print __PACKAGE__."#INIT, has HTTP::Parser: ".($HAS_HTTP_PARSER?"yes":"no")."\n";

1;

# -----------------------------------------------------------------------------
# $pkg->new().
#
sub new
{
  my $pkg  = shift;
  my %opts = @_;
  $pkg->_increment_caller(__PACKAGE__, \%opts);
  my $this   = $pkg->SUPER::new(%opts);

  $this->{callback_object} = undef;
  $this->{remote_addr} = undef;
  if( $HAS_HTTP_PARSER )
  {
    $this->{parser} = HTTP::Parser->new(request=>1);
  }else
  {
    $this->{parser} = Tools::HTTPParser->new(request=>1);
  }

  $this;
}

# -----------------------------------------------------------------------------
# $obj->start(%opts).
# $opts{Socket}         = $sock. # IO::Socket.
# $opts{CallbackObject} = $obj.  # Tools::HTTPServer.
#
# callbacks:
#  $cbo->_on_request($cli, $req);
#  $cbo->_on_close_client($cli);
#
# on_request() には HTTPClient と似た形式のHASHREF が渡される.
#
# {
#   Protocol => 'HTTP/1.x',
#   Path     => "/path/to/request",
#   Method   => 'GET' / 'POST',
#   Header   => {}.
#   Content  => $content,
#   RemoteAddr => '127.0.0.1',
# };
#
sub start
{
  my $this = shift;
  my $opts = {@_};

  $this->{remote_addr} = $opts->{Socket}->peerhost;
  $this->attach($opts->{Socket});
  $this->install();

  $this->{callback_object} = $opts->{CallbackObject};
  if( !$opts->{CallbackObjectNoWeaken} )
  {
    weaken($this->{callback_object});
  }

  $this;
}

# -----------------------------------------------------------------------------
# (impl:tiarra-socket)
#
sub read
{
  my $this = shift;
  my $par  = $this->{callback_object};
  if( !$par )
  {
    RunLoop->shared_loop->notify_error(__PACKAGE__."->read(), no callback_object");
    $this->close();
    return;
  }
  $this->SUPER::read(@_);
  my $recv = $this->recvbuf;
  $this->recvbuf = '';
  my $status = eval{ $this->{parser}->add($recv) };
  if( $@ )
  {
    RunLoop->shared_loop->notify_error(__PACKAGE__."->read(), $@");
    $this->close();
    return;
  }
  if( $status == 0 )
  {
    my $req = $this->{parser}->object();
    #print Dumper($req);use Data::Dumper;
    if( UNIVERSAL::isa($req, 'HTTP::Message') )
    {
      $req = Tools::HTTPParser->from_lwp($req);
    }
    $req->{RemoteAddr} = $this->{remote_addr};
    $par->_on_request($this, $req);
  }elsif( $status == -2 )
  {
    #print "need line data\n";
  }elsif( $status == -1 )
  {
    #print "need more data\n";
  }else
  {
    # $status > 0
    #print "need $status byte(s)\n";
  }
}

# -----------------------------------------------------------------------------
# (impl:tiarra-socket)
#
sub close
{
  my $this = shift;
  my $par  = $this->{callback_object};
  $this->SUPER::close(@_);
  if( !$par )
  {
    RunLoop->shared_loop->notify_error(__PACKAGE__."->close(), no callback_object");
    return;
  }
  $par->_on_close_client($this);
}

# -----------------------------------------------------------------------------
# $obj->response($res).
#
sub response
{
  my $this = shift;
  my $res  = shift;

  $this->append( Tools::HTTPParser->to_string($res) );

  $this;
}

# -----------------------------------------------------------------------------
# End of Module.
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# End of File.
# -----------------------------------------------------------------------------
__END__

=encoding utf8

=for stopwords
	YAMASHINA
	Hio
	ACKNOWLEDGEMENTS
	AnnoCPAN
	CPAN
	RT