File: CGI.pm

package info (click to toggle)
libjavascript-rpc-perl 0.10-2
  • links: PTS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 136 kB
  • sloc: javascript: 256; perl: 151; makefile: 2
file content (336 lines) | stat: -rw-r--r-- 6,664 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package JavaScript::RPC::Server::CGI;

use strict;
use Carp;

our $VERSION = '0.1';

=head1 NAME

JavaScript::RPC::Server::CGI - Remote procedure calls from JavaScript

=head1 SYNOPSIS

	package MyJSRPC;
	
	use Carp;
	use base qw( JavaScript::RPC::Server::CGI );
	
	sub add {
		my $self = shift;
		my @args = @_;
		unless(
			@args == 2 and
			$args[ 0 ] =~ /^\d+$/ and
			$args[ 1 ] =~ /^\d+$/
		) {
			croak( 'inputs must be digits only' ); 
		}
		return $args[ 0 ] + $args[ 1 ];
	}
	
	sub subtract {
		my $self = shift;
		my @args = @_;
		unless(
			@args == 2 and
			$args[ 0 ] =~ /^\d+$/ and
			$args[ 1 ] =~ /^\d+$/
		) {
			croak( 'inputs must be digits only' );
		}
		return $args[ 0 ] - $args[ 1 ];
	}
	
	package main;
	
	use strict;
	
	my $server = MyJSRPC->new;
	$server->process;

=head1 DESCRIPTION

JavaScript::RPC::Server::CGI is a CGI-based server library for use with Brent
Ashley's JavaScript Remote Scripting (JSRS) client library. It works
asynchronously and uses DHTML to deal with the payload.

In order to add your custom meothds, this module should be subclassed.

The most current version (as of the release of this module) of the client
library as well as a demo application have been included in this
distribution.

=head1 INSTALLATION

To install this module via Module::Build:

	perl Build.PL
	./Build         # or `perl Build`
	./Build test    # or `perl Build test`
	./Build install # or `perl Build install`

To install this module via ExtUtils::MakeMaker:

	perl Makefile.PL
	make
	make test
	make install

=head1 METHODS

=head2 new()

Creates a new instance of the module. No further options are available at
this time.

=cut

sub new {
	my $class = shift;
	my $self  = {
		env => {}
	};

	bless $self, $class;

	return $self;
}

=head2 query()

Gets / sets the query object. This has the side effect of extracting the
env() data.

=cut

sub query {
	my $self  = shift;
	my $query = shift;

	unless( $query or $self->{ query } ) {
		$query = $self->get_new_query;
	}

	if( $query ) {
		my $method  = $query->param( 'F' ) || undef;
		my $uid     = $query->param( 'U' ) || undef;
		my $context = $query->param( 'C' ) || undef;

		my( $param, @params );
		my $i = 0;

		# Extract parameters
		while( defined( $param = $query->param( "P$i" ) ) ) {
			$param =~ s/^\[(.*)\]$/$1/s;
			push @params, $param;
			$i++;
		}

		$self->env(
			method  => $method,
			uid     => $uid,
			context => $context,
			params  => \@params
		);

		$self->{ query } = $query;
	}

	return $self->{ query };
}

=head2 get_new_query()

This method generates a new query object. It is used internally by the
query() method. This method should only be used if you want to supply
a query object other than the standard CGI.pm object. However, it must
be a CGI.pm compatible object. Here's an example using CGI::Simple.

	sub get_new_query {
		require CGI::Simple;
		my $q = CGI::Simple->new();

		return $q;
	}

=cut

sub get_new_query {
	require CGI;
	my $q = CGI->new();

	return $q;
}

=head2 env()

Gets / sets a hash of information related to the currently query. The
resulting structure contains four items:

=over 4 

=item * method - the method called

=item * params - an array of parameters for the method

=item * uid - the unique id for this query

=item * context - the context id

=back

=cut

sub env {
	my $self  = shift;

	if( @_ ) {
		if( @_ % 2 == 0 ) {
			my %env  = @_;
			for( keys %env ) {
				$self->{ env }->{ $_ } = $env{ $_ };
			}
		}
		else {
			return $self->{ env }->{ $_[ 0 ] };
		}
	}
	else {
		$self->query;
	}

	return %{ $self->{ env } };
}

=head2 error_message()

Get / sets the error message sent to the client if an error occurred.

=cut

sub error_message {
	my $self    = shift;
	my $message = shift;

	$self->{ error_message } = $message if $message;

	return $self->{ error_message };
}

=head2 process()

Processes the current query and either returns the result from the appropriate
method, or an error to the client and returns either true or false, respectively,
to the caller. An error will occur if the method name is blank, or the method
has not been defined. This function takes an optional CGI.pm compatible object
as an input.

Your subclass' method will be evaled and will either return an error to the
caller if it died, or return a valid result payload on success.

=cut

sub process {
	my $self    = shift;
	my $query   = $self->query;
	my $method  = $self->env( 'method' );
	my @params  = @{ $self->env( 'params' ) };

	print $query->header;

	return $self->error( 'No function specified' ) unless $method;
	return $self->error( 'Specified function not implemented' ) unless $self->can( $method );

        eval {
                return $self->result( $self->$method( @params ) );
        };

        return $self->error( $@ ) if $@;
}

=head2 error()

Returns a valid error payload to the client and false to the caller. It will
automatically call error_message() for you.

=cut

sub error {
	my $self    = shift;
	my $message = shift;
        $message    =~ s/(.+) at (.+?)\n*$/$1/;
        my $msg_esc = _js_escape( $message );
	my %env     = $self->env;

	$self->error_message( $message );
	carp( $message );

	print <<"EO_ERROR";
<html>
<head></head>
<body onload="p = document.layers?parentlayer:window.parent; p.jsrsError( '$env{ context }', '$msg_esc' );">$message</body>
</html>
EO_ERROR

	return 0;
}

=head2 result()

Returns a valid result payload to the client and true to the caller.

=cut

sub result {
	my $self    = shift;
	my $message = shift;
	my %env     = $self->env;

	print <<"EO_RESULT";
<html>
<head></head>
<body onload="p = document.layers?parentLayer:window.parent; p.jsrsLoaded( '$env{ context }' );">jsrsPayload:<br />
<form name="jsrs_Form">
<textarea name="jsrs_Payload" id="jsrs_payload">$message</textarea>
</form>
</body>
</html>
EO_RESULT

	return 1;
}

=head1 SEE ALSO

=over 4 

=item * http://www.ashleyit.com/rs

=back

=head1 AUTHOR

=over 4 

=item * Brian Cassidy E<lt>brian@alternation.netE<gt>

=back

=head1 COPYRIGHT AND LICENSE

Copyright 2005 by Brian Cassidy

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself. 

=cut

sub _js_escape {
        my $string = shift;
        $string =~ s/'/\\'/g;
        $string =~ s/\n/\\r/gs;
        return $string;
}

1;