File: HTTPServer.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 (269 lines) | stat: -rw-r--r-- 5,911 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
## ----------------------------------------------------------------------------
#  Tools::HTTPServer.
# -----------------------------------------------------------------------------
# Mastering programmed by YAMASHINA Hio
#
# Copyright 2008 YAMASHINA Hio
# -----------------------------------------------------------------------------
# $Id: HTTPServer.pm 9215 2008-04-09 14:37:50Z hio $
# -----------------------------------------------------------------------------
package Tools::HTTPServer;
use strict;
use warnings;
use Tiarra::Socket;
use base 'Tiarra::Socket';

use Tools::HTTPServer::Client;
use Module::Use qw(Tools::HTTPServer::Client);

use Scalar::Util qw(weaken);

our $DEBUG = 0;

1;

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

  $this->{host}   = undef;
  $this->{port}   = undef;
  $this->{listen} = undef;
  $this->{path}   = undef;

  $this->{clients} = [];
  $this->{callback_object} = undef;

  $this;
}

# -----------------------------------------------------------------------------
# (destructor).
#
sub DESTROY
{
  my $this = shift;
  if( $this->sock )
  {
    $this->detach();
  }
}

# -----------------------------------------------------------------------------
# $obj->start(%opts).
# Host:   $host.
# Port:   $port.
# Listen: $backlog.
#
sub start
{
  my $this = shift;
  my $opts = {@_};

  $this->{host}   = $opts->{Host}   || '127.0.0.1';
  $this->{port}   = $opts->{Port}   || 8080;
  $this->{listen} = $opts->{Listen} || 5;

  # 処理するパス.
  # /path/to/process/ の最初も最後も / がついている形に正規化.
  my $path   = $opts->{Path} || '/';
  $path =~ m{^/} or $path = "/$path";
  $path =~ m{/$} or $path = "$path/";
  $this->{path} = $path;

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

  my $sock = IO::Socket::INET->new(
    LocalHost => $this->{host},
    LocalPort => $this->{port},
    Listen    => $this->{listen},
    ReuseAddr => 1,
  );

  if( $sock )
  {
    $this->attach($sock);
    $this->install();

    my $pkg   = ref($this);
    my $name  = $this->name;
    my $where = $this->where;
    $name =~ s/^(?:\Q$pkg\E)?/$pkg ($where)/;
    $this->name($name);
  }

  $this;
}

# -----------------------------------------------------------------------------
# $loc = $obj->where().
# $loc = 'http://host:port/path/'.
#
sub where
{
  my $this = shift;
  if( $this->sock )
  {
    my $host = $this->{host};
    my $port = $this->{port};
    my $path = $this->{path};
    "http://$host:$port$path";
  }else
  {
    undef;
  }
}

# -----------------------------------------------------------------------------
# (impl:tiarra-socket)
#
sub want_to_write { 0 }
#sub write         {} # never used.
#sub exception     {} # never used.
sub read
{
  my $this = shift;

  my $sock = $this->sock->accept();
  if( !$sock )
  {
    RunLoop->shared_loop->notify_error(__PACKAGE__.", accept failed: $!/$@");
    return;
  }

  $this->_on_accept($sock);
}

sub close
{
  my $this = shift;
  $this->SUPER::close(@_);

  my $list = $this->{clients};
  foreach my $cli (@$list)
  {
    $cli and $cli->close();
  }
  @$list = ();
}

# -----------------------------------------------------------------------------
# $this->_on_accept($sock).
# (private).
#
sub _on_accept
{
  my $this = shift;
  my $sock  = shift;

  # 接続元制限とかいれたければこのあたりでいれてもいいのかも?

  $this->_start_client($sock);
}

# -----------------------------------------------------------------------------
# $this->_start_client($sock).
#
sub _start_client
{
  my $this = shift;
  my $sock = shift;

  my $peer = $sock->peerhost.':'.$sock->peerport;
  $DEBUG and $this->_debug("start client $peer");

  my $cli = Tools::HTTPServer::Client->new();
  push(@{$this->{clients}}, $cli);

  $cli->start(
    Socket         => $sock,
    CallbackObject => $this,
  );

  $this;
}

# -----------------------------------------------------------------------------
# (impl:callback-from-Tools::HTTPServer::Client).
#
sub _on_request
{
  my $this = shift;
  my $cli  = shift;
  my $req  = shift;

  # このオブジェクトからのコールバックを起動.
  my $par  = $this->{callback_object};
  if( !$par )
  {
    RunLoop->shared_loop->notify_error(__PACKAGE__."->_on_request(), no callback_object");
    return;
  }
  $par->_on_request($cli, $req);
}

# -----------------------------------------------------------------------------
# (impl:callback-from-Tools::HTTPServer::Client).
#
sub _on_close_client
{
  my $this = shift;
  my $cli  = shift;

  # 保持しているクライアント一覧から除去.
  my $list = $this->{clients};
  @$list = grep { $_ && $_ ne $cli } @$list;

  # このオブジェクトからのコールバックを起動.
  my $par  = $this->{callback_object};
  if( !$par )
  {
    RunLoop->shared_loop->notify_error(__PACKAGE__."->_on_close_client(), no callback_object");
    return;
  }
  my $sub = $par->can('_on_close_client');
  if( $sub )
  {
    $par->$sub($cli);
  }
}

# -----------------------------------------------------------------------------
# $this->_debug($msg).
#
sub _debug
{
  my $this = shift;
  my $msg = shift;
  RunLoop->shared_loop->notify_msg($msg);
}


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

=encoding utf8

=for stopwords
	YAMASHINA
	Hio
	ACKNOWLEDGEMENTS
	AnnoCPAN
	CPAN
	RT