The FastCGI protocol data packet is 8-byte aligned and consists of a header and a packet body. For example, to request a page named index. php, webserver first sends a request packet to webapp. The header has a request ID used to differentiate different requests in parallel operation.
Baotou
[Version: 1] [type: 1] [Request ID: 2] [Data Length: 2] [number of bytes filled: 1] [Reserved: 1]
Package body
[Role: 2] [parameter: 1] [Reserved: 5]
Then, a Params packet is sent to pass the parameters and environment variables required for the execution page.
Baotou
[Version: 1] [type: 1] [Request ID: 2] [Data Length: 2] [number of bytes filled: 1] [Reserved: 1]
Package body
[Name Length: 1 or 4] [value length: 1 or 4] [name: Variable Length] [value: Variable Length]...
The length of the name and value is variable, depending on whether the maximum bit of the first byte (high) is 1. If it is 1, the length is 4 bytes, otherwise, the value is 1 byte. That is, if the length cannot exceed 128 bytes, one byte is enough to save the length.
After the parameter is sent, a Params packet with no package body and no packet header is sent, which indicates that the parameter sending ends.
If the post method is used when a page is requested, the form data is also sent. This requires stdin data packets.
Baotou
[Version: 1] [type: 1] [Request ID: 2] [Data Length: 2] [number of bytes filled: 1] [Reserved: 1]
Package body
[Data content: Set the length in the header, 8-byte alignment]
Sometimes, if the post data is larger than or equal to 64 KB, it cannot be sent using an stdin data packet. Multiple stdin data packets must be used to transmit all the data. Like Params data packets, an stdin data packet with no packet body and no packet header is sent at the end, which indicates that the parameter sending ends.
At this point, the data that webserver provides to webapplication has been sent. Then we will receive the data from webapplication.
The data receiving package stdout is similar to stdin, which is not described here. However, the received data consists of the HTTP header and webpage data. The Webserver must process the data before sending it to the browser. Like stdin data packets, webserver receives an empty stdout data packet from webapplication, indicating that the received stdout data has been completed.
Finally, webapplication sends an endrequest packet containing the status. At this point, a page request is processed.
The following are some reference structures.
General Baotou:
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;typedef struct { unsigned char roleB1; unsigned char roleB0; unsigned char flags; unsigned char reserved[5];} FCGI_BeginRequestBody;typedef struct { FCGI_Header header; FCGI_BeginRequestBody body;} FCGI_BeginRequestRecord;typedef struct { unsigned char appStatusB3; unsigned char appStatusB2; unsigned char appStatusB1; unsigned char appStatusB0; unsigned char protocolStatus; unsigned char reserved[3];} FCGI_EndRequestBody;
Parameters passed to the PHP program each time the page is requested:
Script_filename,
QUERY_STRING,
Request_method,
Content_type,
Content_length,
Script_name,
Request_uri,
Document_uri,
Document_root,
Server_protocol,
Gateway_interface,
Server_software,
Remote_addr,
Remote_port,
Server_addr,
Server_port,
SERVER_NAME,
Redirect_status,
Http_accept,
Http_accept_language,
Http_accept_encoding,
Http_user_agent,
Http_host,
Http_connection,
Http_content_type,
Http_content_length,
Http_cache_control,
Http_cookie,
Http_fcgi_params_max
It seems that there are many, but many null values can be omitted, without sending them.
From: http://xiaoxia.org/2009/10/05/fastcgi-protocol-analysis/
References:
FastCGI Specification