1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 * @brief FastCGI protocol defitions and support routines
21 * @defgroup APACHE_CORE_FASTCGI FastCGI Tools
22 * @ingroup APACHE_CORE
26 #ifndef APACHE_UTIL_FCGI_H
27 #define APACHE_UTIL_FCGI_H
36 * @brief A structure that represents the fixed header fields
37 * at the beginning of a "FastCGI record" (i.e., the data prior
38 * to content data and padding).
41 /** See values for version, below */
42 unsigned char version;
43 /** See values for type, below */
45 /** request id, in two parts */
46 unsigned char requestIdB1;
47 unsigned char requestIdB0;
48 /** content length, in two parts */
49 unsigned char contentLengthB1;
50 unsigned char contentLengthB0;
52 unsigned char paddingLength;
53 /** 8-bit reserved field */
54 unsigned char reserved;
58 * Number of bytes in the header portion of a FastCGI record
59 * (i.e., ap_fcgi_header structure). Future versions of the
60 * protocol may increase the size.
62 #define AP_FCGI_HEADER_LEN 8
65 * Maximum number of bytes in the content portion of a FastCGI record.
67 #define AP_FCGI_MAX_CONTENT_LEN 65535
70 * Possible values for the version field of ap_fcgi_header
72 #define AP_FCGI_VERSION_1 1
75 * Possible values for the type field of ap_fcgi_header
77 #define AP_FCGI_BEGIN_REQUEST 1
78 #define AP_FCGI_ABORT_REQUEST 2
79 #define AP_FCGI_END_REQUEST 3
80 #define AP_FCGI_PARAMS 4
81 #define AP_FCGI_STDIN 5
82 #define AP_FCGI_STDOUT 6
83 #define AP_FCGI_STDERR 7
84 #define AP_FCGI_DATA 8
85 #define AP_FCGI_GET_VALUES 9
86 #define AP_FCGI_GET_VALUES_RESULT 10
87 #define AP_FCGI_UNKNOWN_TYPE 11
88 #define AP_FCGI_MAXTYPE (AP_FCGI_UNKNOWN_TYPE)
91 * Offsets of the various fields of ap_fcgi_header
93 #define AP_FCGI_HDR_VERSION_OFFSET 0
94 #define AP_FCGI_HDR_TYPE_OFFSET 1
95 #define AP_FCGI_HDR_REQUEST_ID_B1_OFFSET 2
96 #define AP_FCGI_HDR_REQUEST_ID_B0_OFFSET 3
97 #define AP_FCGI_HDR_CONTENT_LEN_B1_OFFSET 4
98 #define AP_FCGI_HDR_CONTENT_LEN_B0_OFFSET 5
99 #define AP_FCGI_HDR_PADDING_LEN_OFFSET 6
100 #define AP_FCGI_HDR_RESERVED_OFFSET 7
103 * @brief This represents the content data of the FastCGI record when
104 * the type is AP_FCGI_BEGIN_REQUEST.
109 * See values for role, below
111 unsigned char roleB1;
112 unsigned char roleB0;
115 * See values for flags bits, below
119 unsigned char reserved[5];
120 } ap_fcgi_begin_request_body;
123 * Values for role component of ap_fcgi_begin_request_body
125 #define AP_FCGI_RESPONDER 1
126 #define AP_FCGI_AUTHORIZER 2
127 #define AP_FCGI_FILTER 3
130 * Values for flags bits of ap_fcgi_begin_request_body
132 #define AP_FCGI_KEEP_CONN 1 /* otherwise the application closes */
135 * Offsets of the various fields of ap_fcgi_begin_request_body
137 #define AP_FCGI_BRB_ROLEB1_OFFSET 0
138 #define AP_FCGI_BRB_ROLEB0_OFFSET 1
139 #define AP_FCGI_BRB_FLAGS_OFFSET 2
140 #define AP_FCGI_BRB_RESERVED0_OFFSET 3
141 #define AP_FCGI_BRB_RESERVED1_OFFSET 4
142 #define AP_FCGI_BRB_RESERVED2_OFFSET 5
143 #define AP_FCGI_BRB_RESERVED3_OFFSET 6
144 #define AP_FCGI_BRB_RESERVED4_OFFSET 7
147 * Pack ap_fcgi_header
148 * @param h The header to read from
149 * @param a The array to write to, of size AP_FCGI_HEADER_LEN
151 AP_DECLARE(void) ap_fcgi_header_to_array(ap_fcgi_header *h,
155 * Unpack header of FastCGI record into ap_fcgi_header
156 * @param h The header to write to
157 * @param a The array to read from, of size AP_FCGI_HEADER_LEN
159 AP_DECLARE(void) ap_fcgi_header_from_array(ap_fcgi_header *h,
163 * Unpack header of FastCGI record into individual fields
164 * @param version The version, on output
165 * @param type The type, on output
166 * @param request_id The request id, on output
167 * @param content_len The content length, on output
168 * @param padding_len The amount of padding following the content, on output
169 * @param a The array to read from, of size AP_FCGI_HEADER_LEN
171 AP_DECLARE(void) ap_fcgi_header_fields_from_array(unsigned char *version,
173 apr_uint16_t *request_id,
174 apr_uint16_t *content_len,
175 unsigned char *padding_len,
179 * Pack ap_fcgi_begin_request_body
180 * @param h The begin-request body to read from
181 * @param a The array to write to, of size AP_FCGI_HEADER_LEN
183 AP_DECLARE(void) ap_fcgi_begin_request_body_to_array(ap_fcgi_begin_request_body *h,
187 * Fill in a FastCGI request header with the required field values.
188 * @param header The header to fill in
189 * @param type The type of record
190 * @param request_id The request id
191 * @param content_len The amount of content which follows the header
192 * @param padding_len The amount of padding which follows the content
194 * The header array must be at least AP_FCGI_HEADER_LEN bytes long.
196 AP_DECLARE(void) ap_fcgi_fill_in_header(ap_fcgi_header *header,
198 apr_uint16_t request_id,
199 apr_uint16_t content_len,
200 unsigned char padding_len);
203 * Fill in a FastCGI begin request body with the required field values.
204 * @param brb The begin-request-body to fill in
205 * @param role AP_FCGI_RESPONDER or other roles
206 * @param flags 0 or a combination of flags like AP_FCGI_KEEP_CONN
208 AP_DECLARE(void) ap_fcgi_fill_in_request_body(ap_fcgi_begin_request_body *brb,
210 unsigned char flags);
213 * Compute the buffer size needed to encode the next portion of
214 * the provided environment table.
215 * @param env The environment table
216 * @param maxlen The maximum buffer size allowable, capped at
217 * AP_FCGI_MAX_CONTENT_LEN.
218 * @param starting_elem On input, the next element of the table array
219 * to process in this FastCGI record. On output, the next element to
220 * process on the *next* FastCGI record.
221 * @return Size of buffer needed to encode the next part, or 0
222 * if no more can be encoded. When 0 is returned: If starting_elem
223 * has reached the end of the table array, all has been encoded;
224 * otherwise, the next envvar can't be encoded within the specified
226 * @note If an envvar can't be encoded within the specified limit,
227 * the caller can log a warning and increment starting_elem and try
228 * again or increase the limit or fail, as appropriate for the module.
230 AP_DECLARE(apr_size_t) ap_fcgi_encoded_env_len(apr_table_t *env,
235 * Encode the next portion of the provided environment table using
236 * a buffer previously allocated.
237 * @param r The request, for logging
238 * @param env The environment table
239 * @param buffer A buffer to contain the encoded environment table
240 * @param buflen The length of the buffer, previously computed by
241 * ap_fcgi_encoded_env_len().
242 * @param starting_elem On input, the next element of the table array
243 * to process in this FastCGI record. On output, the next element to
244 * process on the *next* FastCGI record.
245 * @return APR_SUCCESS if a section could be encoded or APR_ENOSPC
247 * @note The output starting_elem from ap_fcgi_encoded_env_len
248 * shouldn't be used as input to ap_fcgi_encode_env when building the
249 * same FastCGI record.
251 AP_DECLARE(apr_status_t) ap_fcgi_encode_env(request_rec *r,
258 * String forms for the value of the FCGI_ROLE envvar
260 #define AP_FCGI_RESPONDER_STR "RESPONDER"
261 #define AP_FCGI_AUTHORIZER_STR "AUTHORIZER"
262 #define AP_FCGI_FILTER_STR "FILTER"
265 * FastCGI implementations that implement the AUTHORIZER role
266 * for Apache httpd and allow the application to participate in
267 * any of the Apache httpd AAA phases typically set the variable
268 * FCGI_APACHE_ROLE to one of these strings to indicate the
269 * specific AAA phase.
271 #define AP_FCGI_APACHE_ROLE_AUTHENTICATOR_STR "AUTHENTICATOR"
272 #define AP_FCGI_APACHE_ROLE_AUTHORIZER_STR "AUTHORIZER"
273 #define AP_FCGI_APACHE_ROLE_ACCESS_CHECKER_STR "ACCESS_CHECKER"
279 #endif /* !APACHE_UTIL_FCGI_H */