summaryrefslogtreecommitdiffstats
path: root/web/server/h2o/libh2o/misc/p5-net-fastcgi/lib/Net/FastCGI/IO.pod
blob: 84a9f097328eeedbd2b99c7c595c9b36880de25d (plain)
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
=head1 NAME

Net::FastCGI::IO - Provides functions to read and write FastCGI messages.

=head1 SYNOPSIS

    # FCGI_Header
    @values = read_header($fh);
    $header = read_header($fh);
    $result = write_header($fh, $type, $request_id, $content_length, $padding_length);
    
    # FCGI_Record
    @values = read_record($fh);
    $record = read_record($fh);
    $result = write_record($fh, $type, $request_id);
    $result = write_record($fh, $type, $request_id, $content);
    
    # FCGI_Record Stream
    $result = write_stream($fh, $type, $request_id, $content);
    $result = write_stream($fh, $type, $request_id, $content, $terminate);
    
    # I/O ready
    $result = can_read($fh, $timeout);
    $result = can_write($fh, $timeout);

=head1 DESCRIPTION

Provides unbuffered blocking I/O functions to read and write FastCGI messages.

=head1 FUNCTIONS

=head2 read_header

Attempts to read a C<FCGI_Header> from file handle C<$fh>.

I<Usage>

    ($type, $request_id, $content_length, $padding_length)
      = read_header($fh);
    
    $header = read_header($fh);
    say $header->{type};
    say $header->{request_id};
    say $header->{content_length};
    say $header->{padding_length};

I<Arguments>

=over 4

=item C<$fh>

The file handle to read from. Must be a file handle with a file descriptor. File handle 
mode should be set to binary.

=back

I<Returns>

Upon successful completion, the return value of L<Net::FastCGI::Protocol/parse_header>. 
Otherwise, a false value (C<undef> in scalar context and an empty list in list context).

If C<read_header> reaches end-of-file before reading any octets, it returns a 
false value. If unsuccessful, C<read_header> returns a false value and C<$!> 
contains the error from the C<sysread> call. If C<read_header> encounters 
end-of-file after some but not all of the needed octets, the function returns 
a false value and sets C<$!> to C<EPIPE>.

I<Implementation>

The implementation calls C<sysread> in a loop, restarting if C<sysread> 
returns C<undef> with C<$!> set to C<EINTR>. If C<sysread> does not provide 
all the requested octets, C<read_header> continues to call C<sysread> until 
either all the octets have been read, reaches end-of-file or an error occurs.

=head2 read_record

Attempts to read a C<FCGI_Record> from file handle C<$fh>.

I<Usage>

    ($type, $request_id, $content)
      = read_record($fh);
    
    $record = read_record($fh);
    say $record->{type};
    say $record->{request_id};

I<Arguments>

=over 4

=item C<$fh>

The file handle to read from. Must be a file handle with a file descriptor. 
File handle mode should be set to binary.

=back

I<Returns>

Upon successful completion, the return value of L<Net::FastCGI::Protocol/parse_record>. 
Otherwise, a false value (C<undef> in scalar context and an empty list in list context).

If C<read_record> reaches end-of-file before reading any octets, it returns a 
false value. If unsuccessful, C<read_record> returns a false value and C<$!> 
contains the error from the C<sysread> call. If C<read_record> encounters 
end-of-file after some but not all of the needed octets, the function returns 
a false value and sets C<$!> to C<EPIPE>.

I<Implementation>

The implementation calls C<sysread> in a loop, restarting if C<sysread> 
returns C<undef> with C<$!> set to C<EINTR>. If C<sysread> does not provide 
all the requested octets, C<read_record> continues to call C<sysread> until 
either all the octets have been read, reaches end-of-file or an error occurs.

=head2 write_header

Attempts to write a C<FCGI_Header> to file handle C<$fh>.

I<Usage>

    $result = write_header($fh, $type, $request_id, $content_length, $padding_length);

I<Arguments>

=over 4

=item C<$fh>

The file handle to write to. Must be a file handle with a file descriptor. File handle 
mode should be set to binary.

=item C<$type>

An unsigned 8-bit integer.

=item C<$request_id>

An unsigned 16-bit integer.

=item C<$content_length>

An unsigned 16-bit integer.

=item C<$padding_length>

An unsigned 8-bit integer.

=back

I<Returns>

=over 4

=item C<$result>

Upon successful completion, the number of octets actually written. Otherwise, 
C<undef> and C<$!> contains the error from the C<syswrite> call.

=back

I<Implementation>

The implementation calls C<syswrite> in a loop, restarting if C<syswrite> 
returns C<undef> with C<$!> set to C<EINTR>. If C<syswrite> does not output 
all the requested octets, C<write_header> continues to call C<syswrite> until 
all the octets have been written or an error occurs.

=head2 write_record

Attempts to write a C<FCGI_Record> to file handle C<$fh>.

I<Usage>

    $result = write_record($fh, $type, $request_id);
    $result = write_record($fh, $type, $request_id, $content);

I<Arguments>

=over 4

=item C<$fh>

The file handle to write to. Must be a file handle with a file descriptor. File handle 
mode should be set to binary.

=item C<$type>

An unsigned 8-bit integer.

=item C<$request_id>

An unsigned 16-bit integer.

=item C<$content> (optional)

A string of octets containing the content, cannot exceed 65535 octets in length.

=back

I<Returns>

=over 4

=item C<$result>

Upon successful completion, the number of octets actually written. Otherwise, 
C<undef> and C<$!> contains the error from the C<syswrite> call.

=back

I<Implementation>

The implementation calls C<syswrite> in a loop, restarting if C<syswrite> 
returns C<undef> with C<$!> set to C<EINTR>. If C<syswrite> does not output 
all the requested octets, C<write_record> continues to call C<syswrite> until 
all the octets have been written or an error occurs.

=head2 write_stream

Attempts to write a C<FCGI_Record> stream to file handle C<$fh>.

I<Usage>

    $result = write_stream($fh, $type, $request_id, $content);
    $result = write_stream($fh, $type, $request_id, $content, $terminate);

I<Arguments>

=over 4

=item C<$fh>

The file handle to write to. Must be a file handle with a file descriptor. File handle 
mode should be set to binary.

=item C<$type>

An unsigned 8-bit integer.

=item C<$request_id>

An unsigned 16-bit integer.

=item C<$content>

A string of octets containing the stream content.

=item C<$terminate> (optional)

A boolean indicating whether or not the stream should be terminated.
Defaults to false.

=back

I<Returns>

=over 4

=item C<$result>

Upon successful completion, the number of octets actually written. Otherwise, 
C<undef> and C<$!> contains the error from the C<syswrite> call.

=back

I<Implementation>

The implementation calls C<syswrite> in a loop, restarting if C<syswrite> 
returns C<undef> with C<$!> set to C<EINTR>. If C<syswrite> does not output 
all the requested octets, C<write_stream> continues to call C<syswrite> until 
all the octets have been written or an error occurs.

=head2 can_read

Determines wheter or not the given file handle C<$fh> is ready for reading 
within the given timeout C<$timeout>.

I<Usage>

    $result = can_read($fh, $timeout);

I<Arguments>

=over 4

=item C<$fh>

The file handle to test for readiness. Must be a file handle with a file descriptor.

=item C<$timeout>

Maximum interval to wait. Can be set to either a non-negative numeric value or 
C<undef> for infinite wait.

=back

I<Returns>

Upon successful completion, C<0> or C<1>. Otherwise, C<undef> and C<$!> contains 
the C<select> error.

I<Implementation>

The implementation calls C<select> in a loop, restarting if C<select> returns 
C<-1> with C<$!> set to C<EINTR> and C<$timeout> has not elapsed.

=head2 can_write

Determines wheter or not the given file handle C<$fh> is ready for writing 
within the given timeout C<$timeout>.

I<Usage>

    $result = can_write($fh, $timeout);

I<Arguments>

=over 4

=item C<$fh>

The file handle to test for readiness. Must be a file handle with a file descriptor.

=item C<$timeout>

Maximum interval to wait. Can be set to either a non-negative numeric value or 
C<undef> for infinite wait.

=back

I<Returns>

Upon successful completion, C<0> or C<1>. Otherwise, C<undef> and C<$!> contains 
the C<select> error.

I<Implementation>

The implementation calls C<select> in a loop, restarting if C<select> returns 
C<-1> with C<$!> set to C<EINTR> and C<$timeout> has not elapsed.

=head1 EXPORTS

None by default. All functions can be exported using the C<:all> tag or individually.

=head1 DIAGNOSTICS

=over 4

=item B<(F)> Usage: %s

Subroutine called with wrong number of arguments.

=item B<(W Net::FastCGI::IO)> FastCGI: Could not read %s

=item B<(W Net::FastCGI::IO)> FastCGI: Could not write %s

=back

=head1 SEE ALSO

=over 4

=item FastCGI Specification Version 1.0

L<http://www.fastcgi.com/devkit/doc/fcgi-spec.html>

=item The Common Gateway Interface (CGI) Version 1.1

L<http://tools.ietf.org/html/rfc3875>

=item L<Net::FastCGI::Constant>

=item L<Net::FastCGI::Protocol>

=back

=head1 AUTHOR

Christian Hansen C<chansen@cpan.org>

=head1 COPYRIGHT

Copyright 2008-2010 by Christian Hansen.

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