-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathspec.html
1499 lines (1335 loc) · 59.5 KB
/
spec.html
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" dir="ltr" lang="en"><head>
<title>FastCGI Specification | FastCGI - </title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link type="text/css" rel="stylesheet" media="all" href="files/node.css">
<link type="text/css" rel="stylesheet" media="all" href="files/defaults.css">
<link type="text/css" rel="stylesheet" media="all" href="files/system.css">
<link type="text/css" rel="stylesheet" media="all" href="files/system-menus.css">
<link type="text/css" rel="stylesheet" media="all" href="files/user.css">
<link type="text/css" rel="stylesheet" media="all" href="files/style.css">
</head>
<body>
<div id="wrapper">
<div id="logo"></div>
<div id="nav">
<ul class="nav-links primary-nav-links"><li class="menu-49 first"><a href="https://web.archive.org/web/20160306081510/http://fastcgi.com/drupal/node/2">FastCGI</a></li>
<li class="menu-53"><a href="https://web.archive.org/web/20160306081510/http://fastcgi.com/drupal/node/19">News</a></li>
<li class="menu-45"><a href="https://web.archive.org/web/20160306081510/http://fastcgi.com/drupal/node/6">Documentation</a></li>
<li class="menu-44"><a href="https://web.archive.org/web/20160306081510/http://fastcgi.com/drupal/node/5">API/Libraries</a></li>
<li class="menu-43"><a href="https://web.archive.org/web/20160306081510/http://fastcgi.com/drupal/node/3">FastCGI Servers</a></li>
<li class="menu-46"><a href="https://web.archive.org/web/20160306081510/http://fastcgi.com/drupal/node/7">Misc.</a></li>
<li class="menu-48"><a href="https://web.archive.org/web/20160306081510/http://fastcgi.com/drupal/node/12">Help!</a></li>
<li class="menu-47 last"><a href="https://web.archive.org/web/20160306081510/http://fastcgi.com/drupal/node/8">About Us</a></li>
</ul> </div>
<div id="heading">
FastCGI Specification </div> <!-- end heading -->
<div id="content">
<div id="node-22" class="node">
<div class="content">
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<!--Copyright (c) 1996 Open Market, Inc. -->
<!--See the file "LICENSE.TERMS" for information on usage and redistribution-->
<!--of this file, and for a DISCLAIMER OF ALL WARRANTIES. -->
<style type="text/css">
h5.c2 {text-align: center}
div.c1 {text-align: center}
</style>
<div class="c1">
<h2>
FastCGI Specification
</h2>
</div>
<div class="c1">
Mark R. Brown<br>
Open Market, Inc.<br>
<p>
Document Version: 1.0<br>
29 April 1996<br>
</p>
</div>
<p>
</p>
<h5 class="c2">
Copyright © 1996 Open Market, Inc. 245 First Street, Cambridge, MA 02142 U.S.A.<br>
Tel: 617-621-9500 Fax: 617-621-1703 URL: <a href="https://web.archive.org/web/20160306081510/http://www.openmarket.com/">http://www.openmarket.com/</a><br>
<br>
$Id: fcgi-spec.html,v 1.4 2002/02/25 00:42:59 robs Exp $
</h5>
<hr>
<ul type="square">
<li>
<a href="#S1">1. Introduction</a>
</li>
<li>
<a href="#S2">2. Initial Process State</a>
<ul type="square">
<li>
<a href="#S2.1">2.1 Argument list</a>
</li>
<li>
<a href="#S2.2">2.2 File descriptors</a>
</li>
<li>
<a href="#S2.3">2.3 Environment variables</a>
</li>
<li>
<a href="#S2.4">2.4 Other state</a>
</li>
</ul>
</li>
<li>
<a href="#S3">3. Protocol Basics</a>
<ul type="square">
<li>
<a href="#S3.1">3.1 Notation</a>
</li>
<li>
<a href="#S3.2">3.2 Accepting Transport Connections</a>
</li>
<li>
<a href="#S3.3">3.3 Records</a>
</li>
<li>
<a href="#S3.4">3.4 Name-Value Pairs</a>
</li>
<li>
<a href="#S3.5">3.5 Closing Transport Connections</a>
</li>
</ul>
</li>
<li>
<a href="#S4">4. Management Record Types</a>
<ul type="square">
<li>
<a href="#S4.1">4.1 <tt>FCGI_GET_VALUES, FCGI_GET_VALUES_RESULT</tt></a>
</li>
<li>
<a href="#S4.2">4.2 <tt>FCGI_UNKNOWN_TYPE</tt></a>
</li>
</ul>
</li>
<li>
<a href="#S5">5. Application Record Types</a>
<ul type="square">
<li>
<a href="#S5.1">5.1 <tt>FCGI_BEGIN_REQUEST</tt></a>
</li>
<li>
<a href="#S5.2">5.2 Name-Value Pair Streams: <tt>FCGI_PARAMS</tt>, <tt>FCGI_RESULTS</tt></a>
</li>
<li>
<a href="#S5.3">5.3 Byte Streams: <tt>FCGI_STDIN</tt>, <tt>FCGI_DATA</tt>, <tt>FCGI_STDOUT</tt>,
<tt>FCGI_STDERR</tt></a>
</li>
<li>
<a href="#S5.4">5.4 <tt>FCGI_ABORT_REQUEST</tt></a>
</li>
<li>
<a href="#S5.5">5.5 <tt>FCGI_END_REQUEST</tt></a>
</li>
</ul>
</li>
<li>
<a href="#S6">6. Roles</a>
<ul type="square">
<li>
<a href="#S6.1">6.1 Role Protocols</a>
</li>
<li>
<a href="#S6.2">6.2 Responder</a>
</li>
<li>
<a href="#S6.3">6.3 Authorizer</a>
</li>
<li>
<a href="#S6.4">6.4 Filter</a>
</li>
</ul>
</li>
<li>
<a href="#S7">7. Errors</a>
</li>
<li>
<a href="#S8">8. Types and Constants</a>
</li>
<li>
<a href="#S9">9. References</a>
</li>
<li>
<a href="#SA">A. Table: Properties of the record types</a>
</li>
<li>
<a href="#SB">B. Typical Protocol Message Flow</a>
</li>
</ul>
<p>
</p>
<hr>
<h3>
<a name="S1">1. Introduction</a>
</h3>
<p>
FastCGI is an open extension to CGI that provides high performance for all Internet applications without the
penalties of Web server APIs.
</p>
<p>
This specification has narrow goal: to specify, from an application perspective, the interface between a
FastCGI application and a Web server that supports FastCGI. Many Web server features related to FastCGI, e.g.
application management facilities, have nothing to do with the application to Web server interface, and are
not described here.
</p>
<p>
This specification is for Unix (more precisely, for POSIX systems that support Berkeley Sockets). The bulk of
the specification is a simple communications protocol that is independent of byte ordering and will extend to
other systems.
</p>
<p>
We'll introduce FastCGI by comparing it with conventional Unix implementations of CGI/1.1. FastCGI is
designed to support long-lived application processes, i.e. <i>application servers</i>. That's a major
difference compared with conventional Unix implementations of CGI/1.1, which construct an application process,
use it respond to one request, and have it exit.
</p>
<p>
The initial state of a FastCGI process is more spartan than the initial state of a CGI/1.1 process, because
the FastCGI process doesn't begin life connected to anything. It doesn't have the conventional open
files <tt>stdin</tt>, <tt>stdout</tt>, and <tt>stderr</tt>, and it doesn't receive much information
through environment variables. The key piece of initial state in a FastCGI process is a listening socket,
through which it accepts connections from a Web server.
</p>
<p>
After a FastCGI process accepts a connection on its listening socket, the process executes a simple protocol
to receive and send data. The protocol serves two purposes. First, the protocol multiplexes a single transport
connection between several independent FastCGI requests. This supports applications that are able to process
concurrent requests using event-driven or multi-threaded programming techniques. Second, within each request
the protocol provides several independent data streams in each direction. This way, for instance, both
<tt>stdout</tt> and <tt>stderr</tt> data pass over a single transport connection from the application to the
Web server, rather than requiring separate pipes as with CGI/1.1.
</p>
<p>
A FastCGI application plays one of several well-defined <i>roles</i>. The most familiar is the
<i>Responder</i> role, in which the application receives all the information associated with an HTTP request
and generates an HTTP response; that's the role CGI/1.1 programs play. A second role is <i>Authorizer</i>,
in which the application receives all the information associated with an HTTP request and generates an
authorized/unauthorized decision. A third role is <i>Filter</i>, in which the application receives all the
information associated with an HTTP request, plus an extra stream of data from a file stored on the Web
server, and generates a "filtered" version of the data stream as an HTTP response. The framework is
extensible so that more FastCGI can be defined later.
</p>
<p>
In the remainder of this specification the terms "FastCGI application," "application
process," or "application server" are abbreviated to "application" whenever that
won't cause confusion.
</p>
<p>
</p>
<h3>
<a name="S2">2. Initial Process State</a>
</h3>
<h4>
<a name="S2.1">2.1 Argument list</a>
</h4>
<p>
By default the Web server creates an argument list containing a single element, the name of the application,
taken to be the last component of the executable's path name. The Web server may provide a way to specify
a different application name, or a more elaborate argument list.
</p>
<p>
Note that the file executed by the Web server might be an interpreter file (a text file that starts with the
characters <tt>#!</tt>), in which case the application's argument list is constructed as described in the
<tt>execve</tt> manpage.
</p>
<p>
</p>
<h4>
<a name="S2.2">2.2 File descriptors</a>
</h4>
<p>
The Web server leaves a single file descriptor, <tt>FCGI_LISTENSOCK_FILENO</tt>, open when the application
begins execution. This descriptor refers to a listening socket created by the Web server.
</p>
<p>
<tt>FCGI_LISTENSOCK_FILENO</tt> equals <tt>STDIN_FILENO</tt>. The standard descriptors <tt>STDOUT_FILENO</tt>
and <tt>STDERR_FILENO</tt> are closed when the application begins execution. A reliable method for an
application to determine whether it was invoked using CGI or FastCGI is to call
<tt>getpeername(FCGI_LISTENSOCK_FILENO)</tt>, which returns -1 with <tt>errno</tt> set to <tt>ENOTCONN</tt>
for a FastCGI application.
</p>
<p>
The Web server's choice of reliable transport, Unix stream pipes (<tt>AF_UNIX</tt>) or TCP/IP
(<tt>AF_INET</tt>), is implicit in the internal state of the <tt>FCGI_LISTENSOCK_FILENO</tt> socket.
</p>
<p>
</p>
<h4>
<a name="S2.3">2.3 Environment variables</a>
</h4>
<p>
The Web server may use environment variables to pass parameters to the application. This specification defines
one such variable, <tt>FCGI_WEB_SERVER_ADDRS</tt>; we expect more to be defined as the specification evolves.
The Web server may provide a way to bind other environment variables, such as the <tt>PATH</tt> variable.
</p>
<p>
</p>
<h4>
<a name="S2.4">2.4 Other state</a>
</h4>
<p>
The Web server may provide a way to specify other components of an application's initial process state,
such as the priority, user ID, group ID, root directory, and working directory of the process.
</p>
<p>
</p>
<h3>
<a name="S3">3. Protocol Basics</a>
</h3>
<h4>
<a name="S3.1">3.1 Notation</a>
</h4>
<p>
We use C language notation to define protocol message formats. All structure elements are defined in terms of
the <tt>unsigned char</tt> type, and are arranged so that an ISO C compiler lays them out in the obvious
manner, with no padding. The first byte defined in the structure is transmitted first, the second byte second,
etc.
</p>
<p>
We use two conventions to abbreviate our definitions.
</p>
<p>
First, when two adjacent structure components are named identically except for the suffixes
"<tt>B1</tt>" and "<tt>B0</tt>," it means that the two components may be viewed as a
single number, computed as <tt>B1<<8 + B0</tt>. The name of this single number is the name of the
components, minus the suffixes. This convention generalizes in an obvious way to handle numbers represented in
more than two bytes.
</p>
<p>
Second, we extend C <tt>struct</tt>s to allow the form
</p>
<pre> struct {
unsigned char mumbleLengthB1;
unsigned char mumbleLengthB0;
... /* other stuff */
unsigned char mumbleData[mumbleLength];
};
</pre>
<p>
meaning a structure of varying length, where the length of a component is determined by the values of the
indicated earlier component or components.
</p>
<p>
</p>
<h4>
<a name="S3.2">3.2 Accepting Transport Connections</a>
</h4>
<p>
A FastCGI application calls <tt>accept()</tt> on the socket referred to by file descriptor
<tt>FCGI_LISTENSOCK_FILENO</tt> to accept a new transport connection. If the <tt>accept()</tt> succeeds, and
the <tt>FCGI_WEB_SERVER_ADDRS</tt> environment variable is bound, the application application immediately
performs the following special processing:
</p>
<p>
</p>
<ul type="square">
<li>
<tt>FCGI_WEB_SERVER_ADDRS</tt>: The value is a list of valid IP addresses for the Web server.
<p>
If <tt>FCGI_WEB_SERVER_ADDRS</tt> was bound, the application checks the peer IP address of the new
connection for membership in the list. If the check fails (including the possibility that the connection
didn't use TCP/IP transport), the application responds by closing the connection.
</p>
<p>
<tt>FCGI_WEB_SERVER_ADDRS</tt> is expressed as a comma-separated list of IP addresses. Each IP address
is written as four decimal numbers in the range [0..255] separated by decimal points. So one legal
binding for this variable is <tt>FCGI_WEB_SERVER_ADDRS=199.170.183.28,199.170.183.71</tt>.
</p>
<br>
<br>
</li>
</ul>
<p>
An application may accept several concurrent transport connections, but it need not do so.
</p>
<p>
</p>
<h4>
<a name="S3.3">3.3 Records</a>
</h4>
<p>
Applications execute requests from a Web server using a simple protocol. Details of the protocol depend upon
the application's role, but roughly speaking the Web server first sends parameters and other data to the
application, then the application sends result data to the Web server, and finally the application sends the
Web server an indication that the request is complete.
</p>
<p>
All data that flows over the transport connection is carried in <i>FastCGI records</i>. FastCGI records
accomplish two things. First, records multiplex the transport connection between several independent FastCGI
requests. This multiplexing supports applications that are able to process concurrent requests using
event-driven or multi-threaded programming techniques. Second, records provide several independent data
streams in each direction within a single request. This way, for instance, both <tt>stdout</tt> and
<tt>stderr</tt> data can pass over a single transport connection from the application to the Web server,
rather than requiring separate connections.
</p>
<p>
</p>
<pre> typedef struct {
unsigned char version;
unsigned char type;
unsigned char requestIdB1;
unsigned char requestIdB0;
unsigned char contentLengthB1;
unsigned char contentLengthB0;
unsigned char paddingLength;
unsigned char reserved;
unsigned char contentData[contentLength];
unsigned char paddingData[paddingLength];
} FCGI_Record;
</pre>
<p>
A FastCGI record consists of a fixed-length prefix followed by a variable number of content and padding bytes.
A record contains seven components:
</p>
<p>
</p>
<ul type="square">
<li>
<tt>version</tt>: Identifies the FastCGI protocol version. This specification documents
<tt>FCGI_VERSION_1</tt>.
<p>
</p>
</li>
<li>
<tt>type</tt>: Identifies the FastCGI record type, i.e. the general function that the record performs.
Specific record types and their functions are detailed in later sections.
<p>
</p>
</li>
<li>
<tt>requestId</tt>: Identifies the <i>FastCGI request</i> to which the record belongs.
<p>
</p>
</li>
<li>
<tt>contentLength</tt>: The number of bytes in the <tt>contentData</tt> component of the record.
<p>
</p>
</li>
<li>
<tt>paddingLength</tt>: The number of bytes in the <tt>paddingData</tt> component of the record.
<p>
</p>
</li>
<li>
<tt>contentData</tt>: Between 0 and 65535 bytes of data, interpreted according to the record type.
<p>
</p>
</li>
<li>
<tt>paddingData</tt>: Between 0 and 255 bytes of data, which are ignored.<br>
<br>
</li>
</ul>
<p>
We use a relaxed C <tt>struct</tt> initializer syntax to specify constant FastCGI records. We omit the
<tt>version</tt> component, ignore padding, and treat <tt>requestId</tt> as a number. Thus
<tt>{FCGI_END_REQUEST, 1, {FCGI_REQUEST_COMPLETE,0}}</tt> is a record with <tt>type == FCGI_END_REQUEST</tt>,
<tt>requestId == 1</tt>, and <tt>contentData == {FCGI_REQUEST_COMPLETE,0}</tt>.
</p>
<p>
</p>
<h5>
Padding
</h5>
<p>
The protocol allows senders to pad the records they send, and requires receivers to interpret the
<tt>paddingLength</tt> and skip the <tt>paddingData</tt>. Padding allows senders to keep data aligned for more
efficient processing. Experience with the X window system protocols shows the performance benefit of such
alignment.
</p>
<p>
We recommend that records be placed on boundaries that are multiples of eight bytes. The fixed-length portion
of a <tt>FCGI_Record</tt> is eight bytes.
</p>
<p>
</p>
<h5>
Managing Request IDs
</h5>
<p>
The Web server re-uses FastCGI request IDs; the application keeps track of the current state of each request
ID on a given transport connection. A request ID <tt>R</tt> becomes active when the application receives a
record <tt>{FCGI_BEGIN_REQUEST, R, ...}</tt> and becomes inactive when the application sends a record
<tt>{FCGI_END_REQUEST, R, ...}</tt> to the Web server.
</p>
<p>
While a request ID <tt>R</tt> is inactive, the application ignores records with <tt>requestId == R</tt>,
except for <tt>FCGI_BEGIN_REQUEST</tt> records as just described.
</p>
<p>
The Web server attempts to keep FastCGI request IDs small. That way the application can keep track of request
ID states using a short array rather than a long array or a hash table. An application also has the option of
accepting only one request at a time. In this case the application simply checks incoming <tt>requestId</tt>
values against the current request ID.
</p>
<p>
</p>
<h5>
Types of Record Types
</h5>
<p>
There are two useful ways of classifying FastCGI record types.
</p>
<p>
The first distinction is between <i>management</i> records and <i>application</i> records. A management record
contains information that is not specific to any Web server request, such as information about the protocol
capabilities of the application. An application record contains information about a particular request,
identified by the <tt>requestId</tt> component.
</p>
<p>
Management records have a <tt>requestId</tt> value of zero, also called the <i>null request ID</i>.
Application records have a nonzero <tt>requestId</tt>.
</p>
<p>
The second distinction is between <i>discrete</i> and <i>stream</i> records. A discrete record contains a
meaningful unit of data all by itself. A stream record is part of a <i>stream</i>, i.e. a series of zero or
more non-empty records (<tt>length != 0</tt>) of the stream type, followed by an empty record (<tt>length ==
0</tt>) of the stream type. The <tt>contentData</tt> components of a stream's records, when concatenated,
form a byte sequence; this byte sequence is the value of the stream. Therefore the value of a stream is
independent of how many records it contains or how its bytes are divided among the non-empty records.
</p>
<p>
These two classifications are independent. Among the record types defined in this version of the FastCGI
protocol, all management record types are also discrete record types, and nearly all application record types
are stream record types. But three application record types are discrete, and nothing prevents defining a
management record type that's a stream in some later version of the protocol.
</p>
<p>
</p>
<h4>
<a name="S3.4">3.4 Name-Value Pairs</a>
</h4>
<p>
In many of their roles, FastCGI applications need to read and write varying numbers of variable-length values.
So it is useful to adopt a standard format for encoding a name-value pair.
</p>
<p>
FastCGI transmits a name-value pair as the length of the name, followed by the length of the value, followed
by the name, followed by the value. Lengths of 127 bytes and less can be encoded in one byte, while longer
lengths are always encoded in four bytes:
</p>
<p>
</p>
<pre> typedef struct {
unsigned char nameLengthB0; /* nameLengthB0 >> 7 == 0 */
unsigned char valueLengthB0; /* valueLengthB0 >> 7 == 0 */
unsigned char nameData[nameLength];
unsigned char valueData[valueLength];
} FCGI_NameValuePair11;
typedef struct {
unsigned char nameLengthB0; /* nameLengthB0 >> 7 == 0 */
unsigned char valueLengthB3; /* valueLengthB3 >> 7 == 1 */
unsigned char valueLengthB2;
unsigned char valueLengthB1;
unsigned char valueLengthB0;
unsigned char nameData[nameLength];
unsigned char valueData[valueLength
((B3 & 0x7f) << 24) + (B2 << 16) + (B1 << 8) + B0];
} FCGI_NameValuePair14;
typedef struct {
unsigned char nameLengthB3; /* nameLengthB3 >> 7 == 1 */
unsigned char nameLengthB2;
unsigned char nameLengthB1;
unsigned char nameLengthB0;
unsigned char valueLengthB0; /* valueLengthB0 >> 7 == 0 */
unsigned char nameData[nameLength
((B3 & 0x7f) << 24) + (B2 << 16) + (B1 << 8) + B0];
unsigned char valueData[valueLength];
} FCGI_NameValuePair41;
typedef struct {
unsigned char nameLengthB3; /* nameLengthB3 >> 7 == 1 */
unsigned char nameLengthB2;
unsigned char nameLengthB1;
unsigned char nameLengthB0;
unsigned char valueLengthB3; /* valueLengthB3 >> 7 == 1 */
unsigned char valueLengthB2;
unsigned char valueLengthB1;
unsigned char valueLengthB0;
unsigned char nameData[nameLength
((B3 & 0x7f) << 24) + (B2 << 16) + (B1 << 8) + B0];
unsigned char valueData[valueLength
((B3 & 0x7f) << 24) + (B2 << 16) + (B1 << 8) + B0];
} FCGI_NameValuePair44;
</pre>
<p>
The high-order bit of the first byte of a length indicates the length's encoding. A high-order zero
implies a one-byte encoding, a one a four-byte encoding.
</p>
<p>
This name-value pair format allows the sender to transmit binary values without additional encoding, and
enables the receiver to allocate the correct amount of storage immediately even for large values.
</p>
<p>
</p>
<h4>
<a name="S3.5">3.5 Closing Transport Connections</a>
</h4>
<p>
The Web server controls the lifetime of transport connections. The Web server can close a connection when no
requests are active. Or the Web server can delegate close authority to the application (see
<tt>FCGI_BEGIN_REQUEST</tt>). In this case the application closes the connection at the end of a specified
request.
</p>
<p>
This flexibility accommodates a variety of application styles. Simple applications will process one request at
a time and accept a new transport connection for each request. More complex applications will process
concurrent requests, over one or multiple transport connections, and will keep transport connections open for
long periods of time.
</p>
<p>
A simple application gets a significant performance boost by closing the transport connection when it has
finished writing its response. The Web server needs to control the connection lifetime for long-lived
connections.
</p>
<p>
When an application closes a connection or finds that a connection has closed, the application initiates a new
connection.
</p>
<p>
</p>
<h3>
<a name="S4">4. Management Record Types</a>
</h3>
<h4>
<a name="S4.1">4.1 <tt>FCGI_GET_VALUES, FCGI_GET_VALUES_RESULT</tt></a>
</h4>
<p>
The Web server can query specific variables within the application. The server will typically perform a query
on application startup in order to to automate certain aspects of system configuration.
</p>
<p>
The application receives a query as a record <tt>{FCGI_GET_VALUES, 0, ...}</tt>. The <tt>contentData</tt>
portion of a <tt>FCGI_GET_VALUES</tt> record contains a sequence of name-value pairs with empty values.
</p>
<p>
The application responds by sending a record <tt>{FCGI_GET_VALUES_RESULT, 0, ...}</tt> with the values
supplied. If the application doesn't understand a variable name that was included in the query, it omits
that name from the response.
</p>
<p>
<tt>FCGI_GET_VALUES</tt> is designed to allow an open-ended set of variables. The initial set provides
information to help the server perform application and connection management:
</p>
<p>
</p>
<ul type="square">
<li>
<tt>FCGI_MAX_CONNS</tt>: The maximum number of concurrent transport connections this application will
accept, e.g. <tt>"1"</tt> or <tt>"10"</tt>.
<p>
</p>
</li>
<li>
<tt>FCGI_MAX_REQS</tt>: The maximum number of concurrent requests this application will accept, e.g.
<tt>"1"</tt> or <tt>"50"</tt>.
<p>
</p>
</li>
<li>
<tt>FCGI_MPXS_CONNS</tt>: <tt>"0"</tt> if this application does not multiplex connections (i.e.
handle concurrent requests over each connection), <tt>"1"</tt> otherwise.<br>
<br>
</li>
</ul>
<p>
An application may receive a <tt>FCGI_GET_VALUES</tt> record at any time. The application's response
should not involve the application proper but only the FastCGI library.
</p>
<p>
</p>
<h4>
<a name="S4.2">4.2 <tt>FCGI_UNKNOWN_TYPE</tt></a>
</h4>
<p>
The set of management record types is likely to grow in future versions of this protocol. To provide for this
evolution, the protocol includes the <tt>FCGI_UNKNOWN_TYPE</tt> management record. When an application
receives a management record whose type <tt>T</tt> it does not understand, the application responds with
<tt>{FCGI_UNKNOWN_TYPE, 0, {T}}</tt>.
</p>
<p>
The <tt>contentData</tt> component of a <tt>FCGI_UNKNOWN_TYPE</tt> record has the form:
</p>
<pre> typedef struct {
unsigned char type;
unsigned char reserved[7];
} FCGI_UnknownTypeBody;
</pre>
<p>
The <tt>type</tt> component is the type of the unrecognized management record.
</p>
<p>
</p>
<h3>
<a name="S5">5. Application Record Types</a>
</h3>
<h4>
<a name="S5.1">5.1 <tt>FCGI_BEGIN_REQUEST</tt></a>
</h4>
<p>
The Web server sends a <tt>FCGI_BEGIN_REQUEST</tt> record to start a request.
</p>
<p>
The <tt>contentData</tt> component of a <tt>FCGI_BEGIN_REQUEST</tt> record has the form:
</p>
<pre> typedef struct {
unsigned char roleB1;
unsigned char roleB0;
unsigned char flags;
unsigned char reserved[5];
} FCGI_BeginRequestBody;
</pre>
<p>
The <tt>role</tt> component sets the role the Web server expects the application to play. The
currently-defined roles are:
</p>
<p>
</p>
<ul type="square">
<li>
<tt>FCGI_RESPONDER</tt>
</li>
<li>
<tt>FCGI_AUTHORIZER</tt>
</li>
<li>
<tt>FCGI_FILTER</tt>
</li>
</ul>
<p>
Roles are described in more detail in <a href="#S6">Section 6</a> below.
</p>
<p>
The <tt>flags</tt> component contains a bit that controls connection shutdown:
</p>
<p>
</p>
<ul type="square">
<li>
<tt>flags & FCGI_KEEP_CONN</tt>: If zero, the application closes the connection after responding to
this request. If not zero, the application does not close the connection after responding to this request;
the Web server retains responsibility for the connection.<br>
<br>
</li>
</ul>
<h4>
<a name="S5.2">5.2 Name-Value Pair Stream: <tt>FCGI_PARAMS</tt></a>
</h4>
<tt>FCGI_PARAMS</tt>
<p>
is a stream record type used in sending name-value pairs from the Web server to the application. The
name-value pairs are sent down the stream one after the other, in no specified order.
</p>
<p>
</p>
<h4>
<a name="S5.3">5.3 Byte Streams: <tt>FCGI_STDIN</tt>, <tt>FCGI_DATA</tt>, <tt>FCGI_STDOUT</tt>,
<tt>FCGI_STDERR</tt></a>
</h4>
<tt>FCGI_STDIN</tt>
<p>
is a stream record type used in sending arbitrary data from the Web server to the application.
<tt>FCGI_DATA</tt> is a second stream record type used to send additional data to the application.
</p>
<p>
<tt>FCGI_STDOUT</tt> and <tt>FCGI_STDERR</tt> are stream record types for sending arbitrary data and error
data respectively from the application to the Web server.
</p>
<p>
</p>
<h4>
<a name="S5.4">5.4 <tt>FCGI_ABORT_REQUEST</tt></a>
</h4>
<p>
The Web server sends a <tt>FCGI_ABORT_REQUEST</tt> record to abort a request. After receiving
<tt>{FCGI_ABORT_REQUEST, R}</tt>, the application responds as soon as possible with <tt>{FCGI_END_REQUEST, R,
{FCGI_REQUEST_COMPLETE, appStatus}}</tt>. This is truly a response from the application, not a low-level
acknowledgement from the FastCGI library.
</p>
<p>
A Web server aborts a FastCGI request when an HTTP client closes its transport connection while the FastCGI
request is running on behalf of that client. The situation may seem unlikely; most FastCGI requests will have
short response times, with the Web server providing output buffering if the client is slow. But the FastCGI
application may be delayed communicating with another system, or performing a server push.
</p>
<p>
When a Web server is not multiplexing requests over a transport connection, the Web server can abort a request
by closing the request's transport connection. But with multiplexed requests, closing the transport
connection has the unfortunate effect of aborting <i>all</i> the requests on the connection.
</p>
<p>
</p>
<h4>
<a name="S5.5">5.5 <tt>FCGI_END_REQUEST</tt></a>
</h4>
<p>
The application sends a <tt>FCGI_END_REQUEST</tt> record to terminate a request, either because the
application has processed the request or because the application has rejected the request.
</p>
<p>
The <tt>contentData</tt> component of a <tt>FCGI_END_REQUEST</tt> record has the form:
</p>
<pre> typedef struct {
unsigned char appStatusB3;
unsigned char appStatusB2;
unsigned char appStatusB1;
unsigned char appStatusB0;
unsigned char protocolStatus;
unsigned char reserved[3];
} FCGI_EndRequestBody;
</pre>
<p>
The <tt>appStatus</tt> component is an application-level status code. Each role documents its usage of
<tt>appStatus</tt>.
</p>
<p>
The <tt>protocolStatus</tt> component is a protocol-level status code; the possible <tt>protocolStatus</tt>
values are:
</p>
<p>
</p>
<ul type="square">
<li>
<tt>FCGI_REQUEST_COMPLETE</tt>: normal end of request.
<p>
</p>
</li>
<li>
<tt>FCGI_CANT_MPX_CONN</tt>: rejecting a new request. This happens when a Web server sends concurrent
requests over one connection to an application that is designed to process one request at a time per
connection.
<p>
</p>
</li>
<li>
<tt>FCGI_OVERLOADED</tt>: rejecting a new request. This happens when the application runs out of some
resource, e.g. database connections.
<p>
</p>
</li>
<li>
<tt>FCGI_UNKNOWN_ROLE</tt>: rejecting a new request. This happens when the Web server has specified a role
that is unknown to the application.<br>
<br>
</li>
</ul>
<h3>
<a name="S6">6. Roles</a>
</h3>
<h4>
<a name="S6.1">6.1 Role Protocols</a>
</h4>
<p>
Role protocols only include records with application record types. They transfer essentially all data using
streams.
</p>
<p>
To make the protocols reliable and to simplify application programming, role protocols are designed to use
<i>nearly sequential marshalling</i>. In a protocol with strictly sequential marshalling, the application
receives its first input, then its second, etc. until it has received them all. Similarly, the application
sends its first output, then its second, etc. until it has sent them all. Inputs are not interleaved with each
other, and outputs are not interleaved with each other.
</p>
<p>
The sequential marshalling rule is too restrictive for some FastCGI roles, because CGI programs can write to
both <tt>stdout</tt> and <tt>stderr</tt> without timing restrictions. So role protocols that use both
<tt>FCGI_STDOUT</tt> and <tt>FCGI_STDERR</tt> allow these two streams to be interleaved.
</p>
<p>
All role protocols use the <tt>FCGI_STDERR</tt> stream just the way <tt>stderr</tt> is used in conventional
applications programming: to report application-level errors in an intelligible way. Use of the
<tt>FCGI_STDERR</tt> stream is always optional. If an application has no errors to report, it sends either no
<tt>FCGI_STDERR</tt> records or one zero-length <tt>FCGI_STDERR</tt> record.
</p>
<p>
When a role protocol calls for transmitting a stream other than <tt>FCGI_STDERR</tt>, at least one record of
the stream type is always transmitted, even if the stream is empty.
</p>
<p>
Again in the interests of reliable protocols and simplified application programming, role protocols are
designed to be <i>nearly request-response</i>. In a truly request-response protocol, the application receives
all of its input records before sending its first output record. Request-response protocols don't allow
pipelining.
</p>
<p>
The request-response rule is too restrictive for some FastCGI roles; after all, CGI programs aren't
restricted to read all of <tt>stdin</tt> before starting to write <tt>stdout</tt>. So some role protocols
allow that specific possibility. First the application receives all of its inputs except for a final stream
input. As the application begins to receive the final stream input, it can begin writing its output.
</p>
<p>
When a role protocol uses <tt>FCGI_PARAMS</tt> to transmit textual values, such as the values that CGI
programs obtain from environment variables, the length of the value does not include the terminating null
byte, and the value itself does not include a null byte. An application that needs to provide
<tt>environ(7)</tt> format name-value pairs must insert an equal sign between the name and value and append a
null byte after the value.
</p>
<p>
Role protocols do not support the non-parsed header feature of CGI. FastCGI applications set response status
using the <tt>Status</tt> and <tt>Location</tt> CGI headers.
</p>
<p>
</p>
<h4>
<a name="S6.2">6.2 Responder</a>
</h4>
<p>
A Responder FastCGI application has the same purpose as a CGI/1.1 program: It receives all the information
associated with an HTTP request and generates an HTTP response.
</p>
<p>
It suffices to explain how each element of CGI/1.1 is emulated by a Responder:
</p>
<br>
<br>
<ul type="square">
<li>
The Responder application receives CGI/1.1 environment variables from the Web server over
<tt>FCGI_PARAMS</tt>.
<p>
</p>
</li>
<li>
Next the Responder application receives CGI/1.1 <tt>stdin</tt> data from the Web server over
<tt>FCGI_STDIN</tt>. The application receives at most <tt>CONTENT_LENGTH</tt> bytes from this stream before
receiving the end-of-stream indication. (The application receives less than <tt>CONTENT_LENGTH</tt> bytes
only if the HTTP client fails to provide them, e.g. because the client crashed.)
<p>
</p>
</li>