1 Response Format
such as (16 binary mode display)
Sequence 0 1 2 3 4 5 6 7 ... Value 1D 03 00 ...
Sequence 0 (Value 01) is version, fixed to 1
Sequence 1 (value 06) is type, which represents Fcgi_stdout, which represents the output of the application
Sequence 2 3 (00 01) Represents the 2-byte request ID, which defaults to 1 (which is exactly the same as the ID sent when the app was requested, assuming that the request and response IDs are 1)
Sequence 4 5 (1D) represents 2 bytes of output length, up to 65535, for example the current content length is (0x01 << 8) + 0x1D = 285
Sequence 6 (03) represents the number of padding padding bytes (filled to an integer multiple of 8 bytes), such as the current fill (filled with 0) with a length of 8-285 8 = 3, that is, the number of bytes to skip after getting the contents of the output length (285), of course, if 8 does not need to be populated
Sequence 7 (00) is reserved bytes
8 bytes (sequence 7) followed by content (Contentdata) and fill contents (Paddingdata)
Finally, to notify the Web server request to end the record, the details are as follows
Sequence 0 1 2 3 4 5 6 7 ... Value 01 03 00 01 00 08 00 00 ...
Where sequence 1 (a) type represents fcgi_end_request, which is the end of the request, 8 bytes followed by Contentdata (Endrequestbody) and Paddingdata
Endrequestbody content is also more personality, is defined separately
typedef struct { unsigned char appStatusB3; unsigned char appStatusB2; unsigned char appStatusB1; unsigned char appStatusB0; unsigned char protocolstatus; unsigned char reserved[3];} Fcgi_endrequestbody;
Appstatus takes up four bytes, defined as the status code returned by the CGI by invoking the system (the application sets the Protocolstatus component to Fcgi_request_complete and the Appstatus component to the status code, the CGI program would has returned via the exit system call.) Linux normal programs exit by default is the return Back to 0 (should it?) I remember that ... )
The value of the protocolstatus can be
#define Fcgi_request_complete 0#define fcgi_cant_mpx_conn 1#define fcgi_overloaded 2#define FCGI_UNKNOWN_ROLE 3
So the last fcgi_end_request contentdata for
Sequence 0 1 2 3 4 5 6 7 value 00 00 00 00 00 00 00 00
0-3 Sequence for Appstatus
4 sequence protocolstatus is 0 (fcgi_request_complete)
5-7 sequence for reserved 3-byte reserved[3]
2 Request Format
Sequence 0 1 2 3 4 5 6 7 ... Value 01 01 00 01 00 08 00 00 ...
Sequence 0 (Value 01) is version
Sequence 1 (Value 01) is type, which represents Fcgi_begin_reques, which represents the start of a send request
Sequence 2 3 (00 01) Represents a 2-byte request ID, which takes 1 by default
The request starts with a slightly special record, and the content sent (Contentdata) is formatted as follows
typedef struct { unsigned char roleB1; unsigned char roleB0; unsigned char flags; unsigned char reserved[5];} Fcgi_beginrequestbody;
#role的可以取如下三个值 # define Fcgi_responder 1#define Fcgi_authorizer 2#define fcgi_filter 3
Why don't we take 1 (fcgi_responder)? Say it's the same as the classic cgi/1.1 (http those things)
The flags 0 indicates that the link is closed when the request is complete.
Sequence 0 1 2 3 4 5 6 7 value 00 01 00 00 00 00 00 00
0 and 1 sequences represent role 1 (fcgi_responder)
2 Sequence for flags 0
3-7 sequence of reserved[5]
Again, the fcgi_params in the protocol Name-value Pairs, the purpose is to provide the application layer some necessary variables (like HTTP header:headername-headervalue, of course, can be many), detailed definition see http:// www.fastcgi.com/devkit/doc/fcgi-spec.html#S3.4
One of the definition formats is as follows:
typedef struct { unsigned char nameLengthB0;/* nameLengthB0 >> 7 = = 0 */ unsigned char valueLengthB3;/* VA LueLengthB3 >> 7 = = 1 */ unsigned char valueLengthB2; unsigned char valueLengthB1; unsigned char valueLengthB0; unsigned char namedata[namelength]; unsigned char valuedata[valuelength ((B3 & 0x7f) << + (B2 << +) + (B1 << 8) + B0];} Fcgi_namevaluepair14;
Combined with examples to illustrate the
Sequence 0 1 2 3 4 5 6 7 ... Value: EB 05 00 ...
Sequence 1 (04) represents Fcgi_params
After sequence 7 is the corresponding name (name) length (namelength), value (values) length (valuelength), first name (Namedata), Value (Valuedata)
Where the length of the name or value is greater than 127 bytes, it is stored in 4 bytes, as follows
Sequence 0 1 2 3 4 5 6 7 ..... Value 0F/S C R ipt_filename/data/www/...
The 0F of sequence 0 is the decimal (length of script_filename), not greater than 127, so it takes one byte
Sequence 1 of 80 is the decimal 128, greater than 127, the description to occupy 4 bytes (80 00 00 91), the length of
((B3 & 0x7f) << + (B2 << +) + (B1 << 8) + B0
How much is the calculation equal? If the displacement, and other operating symbols are not familiar with, more detailed introduction to the previous article
3 other Instructions
For a detailed definition of each value see Http://www.fastcgi.com/devkit/doc/fcgi-spec.html#S8
Here are some summary notes.
The format of the record (Records, which can be sent sequentially or accept multiple records) is defined as follows
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;
#前八字节定义为Header (as you can understand, head information + response content, think of Header+body in the HTPP protocol to understand)
#协议说明中把这部分定义为FCGI_Header (the Red font section above), i.e.:
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;} Fcgi_header;
#version定义为1 # define FCGI_VERSION_1 1
#type具体值定义, the main concern Fcgi_begin_request (Request start) Fcgi_end_request (Request end) Fcgi_params (fastcgi parameter, that is, some server variables, such as Http_user_agent) Fcgi_stdout (fastcgi standard output, which is what is returned after the request)
#define Fcgi_begin_request 1#define fcgi_abort_request 2#define fcgi_end_request 3#define FCGI_PARAMS 4#define FCGI_ STDIN 5#define fcgi_stdout 6#define fcgi_stderr 7#define fcgi_data 8#define fcgi_get_values 9#define FCGI_GET_VALUES_ RESULT 10#define fcgi_unknown_type 11#define fcgi_maxtype (fcgi_unknown_type)
FASTCGI Official Document: http://www.fastcgi.com/devkit/doc/fcgi-spec.html
Chinese version: http://fuzhong1983.blog.163.com/blog/static/1684705201051002951763/
FAQ:
1 How do I see the header information sent to the FastCGI app by the Web server?
I use Python to listen to a port and then change the fastcgi configuration in Nginx to this port so that the accepted information can be saved as a file in Python. Of course, you can also directly change the Nginx code ...
2 How will the corresponding output be viewed after the request?
Now that you have sent the information, send it directly to the FASTCGI application, and the output will work with you.
3 How do I view request or response information?
Linux under the XXD command to view the binary output file, Windows UltraEdit can also (I use the unregistered version, the remaining days 21, registration to $59.95), free can also try PSPad (think of the game console)
*2011-07-05 Supplemental Notes
Fcgi_stdin and Fcgi_params data encoding at the end, be sure to attach the corresponding blank record
If the Fcgi_stdin corresponds to an additional empty record as
01 05 00 01 00 00 00 00
Fcgi_params for
01 04 00 01 00 00 00 00
Of course, if the fcgi_params itself is empty, there is no need to attach it again.
Interpretation and description of FASTCGI protocol definition