util_fcgi.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  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.
  15. */
  16. /**
  17. * @file util_fcgi.h
  18. * @brief FastCGI protocol defitions and support routines
  19. *
  20. * @defgroup APACHE_CORE_FASTCGI FastCGI Tools
  21. * @ingroup APACHE_CORE
  22. * @{
  23. */
  24. #ifndef APACHE_UTIL_FCGI_H
  25. #define APACHE_UTIL_FCGI_H
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. #include "httpd.h"
  30. /**
  31. * @brief A structure that represents the fixed header fields
  32. * at the beginning of a "FastCGI record" (i.e., the data prior
  33. * to content data and padding).
  34. */
  35. typedef struct {
  36. /** See values for version, below */
  37. unsigned char version;
  38. /** See values for type, below */
  39. unsigned char type;
  40. /** request id, in two parts */
  41. unsigned char requestIdB1;
  42. unsigned char requestIdB0;
  43. /** content length, in two parts */
  44. unsigned char contentLengthB1;
  45. unsigned char contentLengthB0;
  46. /** padding length */
  47. unsigned char paddingLength;
  48. /** 8-bit reserved field */
  49. unsigned char reserved;
  50. } ap_fcgi_header;
  51. /*
  52. * Number of bytes in the header portion of a FastCGI record
  53. * (i.e., ap_fcgi_header structure). Future versions of the
  54. * protocol may increase the size.
  55. */
  56. #define AP_FCGI_HEADER_LEN 8
  57. /*
  58. * Maximum number of bytes in the content portion of a FastCGI record.
  59. */
  60. #define AP_FCGI_MAX_CONTENT_LEN 65535
  61. /**
  62. * Possible values for the version field of ap_fcgi_header
  63. */
  64. #define AP_FCGI_VERSION_1 1
  65. /**
  66. * Possible values for the type field of ap_fcgi_header
  67. */
  68. #define AP_FCGI_BEGIN_REQUEST 1
  69. #define AP_FCGI_ABORT_REQUEST 2
  70. #define AP_FCGI_END_REQUEST 3
  71. #define AP_FCGI_PARAMS 4
  72. #define AP_FCGI_STDIN 5
  73. #define AP_FCGI_STDOUT 6
  74. #define AP_FCGI_STDERR 7
  75. #define AP_FCGI_DATA 8
  76. #define AP_FCGI_GET_VALUES 9
  77. #define AP_FCGI_GET_VALUES_RESULT 10
  78. #define AP_FCGI_UNKNOWN_TYPE 11
  79. #define AP_FCGI_MAXTYPE (AP_FCGI_UNKNOWN_TYPE)
  80. /**
  81. * Offsets of the various fields of ap_fcgi_header
  82. */
  83. #define AP_FCGI_HDR_VERSION_OFFSET 0
  84. #define AP_FCGI_HDR_TYPE_OFFSET 1
  85. #define AP_FCGI_HDR_REQUEST_ID_B1_OFFSET 2
  86. #define AP_FCGI_HDR_REQUEST_ID_B0_OFFSET 3
  87. #define AP_FCGI_HDR_CONTENT_LEN_B1_OFFSET 4
  88. #define AP_FCGI_HDR_CONTENT_LEN_B0_OFFSET 5
  89. #define AP_FCGI_HDR_PADDING_LEN_OFFSET 6
  90. #define AP_FCGI_HDR_RESERVED_OFFSET 7
  91. /**
  92. * @brief This represents the content data of the FastCGI record when
  93. * the type is AP_FCGI_BEGIN_REQUEST.
  94. */
  95. typedef struct {
  96. /**
  97. * role, in two parts
  98. * See values for role, below
  99. */
  100. unsigned char roleB1;
  101. unsigned char roleB0;
  102. /**
  103. * flags
  104. * See values for flags bits, below
  105. */
  106. unsigned char flags;
  107. /** reserved */
  108. unsigned char reserved[5];
  109. } ap_fcgi_begin_request_body;
  110. /*
  111. * Values for role component of ap_fcgi_begin_request_body
  112. */
  113. #define AP_FCGI_RESPONDER 1
  114. #define AP_FCGI_AUTHORIZER 2
  115. #define AP_FCGI_FILTER 3
  116. /*
  117. * Values for flags bits of ap_fcgi_begin_request_body
  118. */
  119. #define AP_FCGI_KEEP_CONN 1 /* otherwise the application closes */
  120. /**
  121. * Offsets of the various fields of ap_fcgi_begin_request_body
  122. */
  123. #define AP_FCGI_BRB_ROLEB1_OFFSET 0
  124. #define AP_FCGI_BRB_ROLEB0_OFFSET 1
  125. #define AP_FCGI_BRB_FLAGS_OFFSET 2
  126. #define AP_FCGI_BRB_RESERVED0_OFFSET 3
  127. #define AP_FCGI_BRB_RESERVED1_OFFSET 4
  128. #define AP_FCGI_BRB_RESERVED2_OFFSET 5
  129. #define AP_FCGI_BRB_RESERVED3_OFFSET 6
  130. #define AP_FCGI_BRB_RESERVED4_OFFSET 7
  131. /**
  132. * Pack ap_fcgi_header
  133. * @param h The header to read from
  134. * @param a The array to write to, of size AP_FCGI_HEADER_LEN
  135. */
  136. AP_DECLARE(void) ap_fcgi_header_to_array(ap_fcgi_header *h,
  137. unsigned char a[]);
  138. /**
  139. * Unpack header of FastCGI record into ap_fcgi_header
  140. * @param h The header to write to
  141. * @param a The array to read from, of size AP_FCGI_HEADER_LEN
  142. */
  143. AP_DECLARE(void) ap_fcgi_header_from_array(ap_fcgi_header *h,
  144. unsigned char a[]);
  145. /**
  146. * Unpack header of FastCGI record into individual fields
  147. * @param version The version, on output
  148. * @param type The type, on output
  149. * @param request_id The request id, on output
  150. * @param content_len The content length, on output
  151. * @param padding_len The amount of padding following the content, on output
  152. * @param a The array to read from, of size AP_FCGI_HEADER_LEN
  153. */
  154. AP_DECLARE(void) ap_fcgi_header_fields_from_array(unsigned char *version,
  155. unsigned char *type,
  156. apr_uint16_t *request_id,
  157. apr_uint16_t *content_len,
  158. unsigned char *padding_len,
  159. unsigned char a[]);
  160. /**
  161. * Pack ap_fcgi_begin_request_body
  162. * @param h The begin-request body to read from
  163. * @param a The array to write to, of size AP_FCGI_HEADER_LEN
  164. */
  165. AP_DECLARE(void) ap_fcgi_begin_request_body_to_array(ap_fcgi_begin_request_body *h,
  166. unsigned char a[]);
  167. /**
  168. * Fill in a FastCGI request header with the required field values.
  169. * @param header The header to fill in
  170. * @param type The type of record
  171. * @param request_id The request id
  172. * @param content_len The amount of content which follows the header
  173. * @param padding_len The amount of padding which follows the content
  174. *
  175. * The header array must be at least AP_FCGI_HEADER_LEN bytes long.
  176. */
  177. AP_DECLARE(void) ap_fcgi_fill_in_header(ap_fcgi_header *header,
  178. unsigned char type,
  179. apr_uint16_t request_id,
  180. apr_uint16_t content_len,
  181. unsigned char padding_len);
  182. /**
  183. * Fill in a FastCGI begin request body with the required field values.
  184. * @param brb The begin-request-body to fill in
  185. * @param role AP_FCGI_RESPONDER or other roles
  186. * @param flags 0 or a combination of flags like AP_FCGI_KEEP_CONN
  187. */
  188. AP_DECLARE(void) ap_fcgi_fill_in_request_body(ap_fcgi_begin_request_body *brb,
  189. int role,
  190. unsigned char flags);
  191. /**
  192. * Compute the buffer size needed to encode the next portion of
  193. * the provided environment table.
  194. * @param env The environment table
  195. * @param maxlen The maximum buffer size allowable, capped at
  196. * AP_FCGI_MAX_CONTENT_LEN.
  197. * @param starting_elem On input, the next element of the table array
  198. * to process in this FastCGI record. On output, the next element to
  199. * process on the *next* FastCGI record.
  200. * @return Size of buffer needed to encode the next part, or 0
  201. * if no more can be encoded. When 0 is returned: If starting_elem
  202. * has reached the end of the table array, all has been encoded;
  203. * otherwise, the next envvar can't be encoded within the specified
  204. * limit.
  205. * @note If an envvar can't be encoded within the specified limit,
  206. * the caller can log a warning and increment starting_elem and try
  207. * again or increase the limit or fail, as appropriate for the module.
  208. */
  209. AP_DECLARE(apr_size_t) ap_fcgi_encoded_env_len(apr_table_t *env,
  210. apr_size_t maxlen,
  211. int *starting_elem);
  212. /**
  213. * Encode the next portion of the provided environment table using
  214. * a buffer previously allocated.
  215. * @param r The request, for logging
  216. * @param env The environment table
  217. * @param buffer A buffer to contain the encoded environment table
  218. * @param buflen The length of the buffer, previously computed by
  219. * ap_fcgi_encoded_env_len().
  220. * @param starting_elem On input, the next element of the table array
  221. * to process in this FastCGI record. On output, the next element to
  222. * process on the *next* FastCGI record.
  223. * @return APR_SUCCESS if a section could be encoded or APR_ENOSPC
  224. * otherwise.
  225. * @note The output starting_elem from ap_fcgi_encoded_env_len
  226. * shouldn't be used as input to ap_fcgi_encode_env when building the
  227. * same FastCGI record.
  228. */
  229. AP_DECLARE(apr_status_t) ap_fcgi_encode_env(request_rec *r,
  230. apr_table_t *env,
  231. void *buffer,
  232. apr_size_t buflen,
  233. int *starting_elem);
  234. /**
  235. * String forms for the value of the FCGI_ROLE envvar
  236. */
  237. #define AP_FCGI_RESPONDER_STR "RESPONDER"
  238. #define AP_FCGI_AUTHORIZER_STR "AUTHORIZER"
  239. #define AP_FCGI_FILTER_STR "FILTER"
  240. /**
  241. * FastCGI implementations that implement the AUTHORIZER role
  242. * for Apache httpd and allow the application to participate in
  243. * any of the Apache httpd AAA phases typically set the variable
  244. * FCGI_APACHE_ROLE to one of these strings to indicate the
  245. * specific AAA phase.
  246. */
  247. #define AP_FCGI_APACHE_ROLE_AUTHENTICATOR_STR "AUTHENTICATOR"
  248. #define AP_FCGI_APACHE_ROLE_AUTHORIZER_STR "AUTHORIZER"
  249. #define AP_FCGI_APACHE_ROLE_ACCESS_CHECKER_STR "ACCESS_CHECKER"
  250. #ifdef __cplusplus
  251. }
  252. #endif
  253. #endif /* !APACHE_UTIL_FCGI_H */
  254. /** @} */