Blame include/util_fcgi.h

Packit 90a5c9
/* Licensed to the Apache Software Foundation (ASF) under one or more
Packit 90a5c9
 * contributor license agreements.  See the NOTICE file distributed with
Packit 90a5c9
 * this work for additional information regarding copyright ownership.
Packit 90a5c9
 * The ASF licenses this file to You under the Apache License, Version 2.0
Packit 90a5c9
 * (the "License"); you may not use this file except in compliance with
Packit 90a5c9
 * the License.  You may obtain a copy of the License at
Packit 90a5c9
 *
Packit 90a5c9
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 90a5c9
 *
Packit 90a5c9
 * Unless required by applicable law or agreed to in writing, software
Packit 90a5c9
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 90a5c9
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 90a5c9
 * See the License for the specific language governing permissions and
Packit 90a5c9
 * limitations under the License.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * @file  util_fcgi.h
Packit 90a5c9
 * @brief FastCGI protocol defitions and support routines
Packit 90a5c9
 *
Packit 90a5c9
 * @defgroup APACHE_CORE_FASTCGI FastCGI Tools
Packit 90a5c9
 * @ingroup  APACHE_CORE
Packit 90a5c9
 * @{
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
#ifndef APACHE_UTIL_FCGI_H
Packit 90a5c9
#define APACHE_UTIL_FCGI_H
Packit 90a5c9
Packit 90a5c9
#ifdef __cplusplus
Packit 90a5c9
extern "C" {
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
#include "httpd.h"
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * @brief A structure that represents the fixed header fields
Packit 90a5c9
 * at the beginning of a "FastCGI record" (i.e., the data prior
Packit 90a5c9
 * to content data and padding).
Packit 90a5c9
 */
Packit 90a5c9
typedef struct {
Packit 90a5c9
    /** See values for version, below */
Packit 90a5c9
    unsigned char version;
Packit 90a5c9
    /** See values for type, below */
Packit 90a5c9
    unsigned char type;
Packit 90a5c9
    /** request id, in two parts */
Packit 90a5c9
    unsigned char requestIdB1;
Packit 90a5c9
    unsigned char requestIdB0;
Packit 90a5c9
    /** content length, in two parts */
Packit 90a5c9
    unsigned char contentLengthB1;
Packit 90a5c9
    unsigned char contentLengthB0;
Packit 90a5c9
    /** padding length */
Packit 90a5c9
    unsigned char paddingLength;
Packit 90a5c9
    /** 8-bit reserved field */
Packit 90a5c9
    unsigned char reserved;
Packit 90a5c9
} ap_fcgi_header;
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * Number of bytes in the header portion of a FastCGI record
Packit 90a5c9
 * (i.e., ap_fcgi_header structure).  Future versions of the
Packit 90a5c9
 * protocol may increase the size.
Packit 90a5c9
 */
Packit 90a5c9
#define AP_FCGI_HEADER_LEN  8
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * Maximum number of bytes in the content portion of a FastCGI record.
Packit 90a5c9
 */
Packit 90a5c9
#define AP_FCGI_MAX_CONTENT_LEN 65535
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Possible values for the version field of ap_fcgi_header
Packit 90a5c9
 */
Packit 90a5c9
#define AP_FCGI_VERSION_1 1
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Possible values for the type field of ap_fcgi_header
Packit 90a5c9
 */
Packit 90a5c9
#define AP_FCGI_BEGIN_REQUEST       1
Packit 90a5c9
#define AP_FCGI_ABORT_REQUEST       2
Packit 90a5c9
#define AP_FCGI_END_REQUEST         3
Packit 90a5c9
#define AP_FCGI_PARAMS              4
Packit 90a5c9
#define AP_FCGI_STDIN               5
Packit 90a5c9
#define AP_FCGI_STDOUT              6
Packit 90a5c9
#define AP_FCGI_STDERR              7
Packit 90a5c9
#define AP_FCGI_DATA                8
Packit 90a5c9
#define AP_FCGI_GET_VALUES          9
Packit 90a5c9
#define AP_FCGI_GET_VALUES_RESULT  10
Packit 90a5c9
#define AP_FCGI_UNKNOWN_TYPE       11
Packit 90a5c9
#define AP_FCGI_MAXTYPE (AP_FCGI_UNKNOWN_TYPE)
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Offsets of the various fields of ap_fcgi_header
Packit 90a5c9
 */
Packit 90a5c9
#define AP_FCGI_HDR_VERSION_OFFSET         0
Packit 90a5c9
#define AP_FCGI_HDR_TYPE_OFFSET            1
Packit 90a5c9
#define AP_FCGI_HDR_REQUEST_ID_B1_OFFSET   2
Packit 90a5c9
#define AP_FCGI_HDR_REQUEST_ID_B0_OFFSET   3
Packit 90a5c9
#define AP_FCGI_HDR_CONTENT_LEN_B1_OFFSET  4
Packit 90a5c9
#define AP_FCGI_HDR_CONTENT_LEN_B0_OFFSET  5
Packit 90a5c9
#define AP_FCGI_HDR_PADDING_LEN_OFFSET     6
Packit 90a5c9
#define AP_FCGI_HDR_RESERVED_OFFSET        7
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * @brief This represents the content data of the FastCGI record when
Packit 90a5c9
 * the type is AP_FCGI_BEGIN_REQUEST.
Packit 90a5c9
 */
Packit 90a5c9
typedef struct {
Packit 90a5c9
    /**
Packit 90a5c9
     * role, in two parts
Packit 90a5c9
     * See values for role, below
Packit 90a5c9
     */
Packit 90a5c9
    unsigned char roleB1;
Packit 90a5c9
    unsigned char roleB0;
Packit 90a5c9
    /**
Packit 90a5c9
     * flags
Packit 90a5c9
     * See values for flags bits, below
Packit 90a5c9
     */
Packit 90a5c9
    unsigned char flags;
Packit 90a5c9
    /** reserved */
Packit 90a5c9
    unsigned char reserved[5];
Packit 90a5c9
} ap_fcgi_begin_request_body;
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * Values for role component of ap_fcgi_begin_request_body
Packit 90a5c9
 */
Packit 90a5c9
#define AP_FCGI_RESPONDER  1
Packit 90a5c9
#define AP_FCGI_AUTHORIZER 2
Packit 90a5c9
#define AP_FCGI_FILTER     3
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * Values for flags bits of ap_fcgi_begin_request_body
Packit 90a5c9
 */
Packit 90a5c9
#define AP_FCGI_KEEP_CONN  1  /* otherwise the application closes */
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Offsets of the various fields of ap_fcgi_begin_request_body
Packit 90a5c9
 */
Packit 90a5c9
#define AP_FCGI_BRB_ROLEB1_OFFSET       0
Packit 90a5c9
#define AP_FCGI_BRB_ROLEB0_OFFSET       1
Packit 90a5c9
#define AP_FCGI_BRB_FLAGS_OFFSET        2
Packit 90a5c9
#define AP_FCGI_BRB_RESERVED0_OFFSET    3
Packit 90a5c9
#define AP_FCGI_BRB_RESERVED1_OFFSET    4
Packit 90a5c9
#define AP_FCGI_BRB_RESERVED2_OFFSET    5
Packit 90a5c9
#define AP_FCGI_BRB_RESERVED3_OFFSET    6
Packit 90a5c9
#define AP_FCGI_BRB_RESERVED4_OFFSET    7
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Pack ap_fcgi_header
Packit 90a5c9
 * @param h The header to read from
Packit 90a5c9
 * @param a The array to write to, of size AP_FCGI_HEADER_LEN
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(void) ap_fcgi_header_to_array(ap_fcgi_header *h,
Packit 90a5c9
                                         unsigned char a[]);
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Unpack header of FastCGI record into ap_fcgi_header
Packit 90a5c9
 * @param h The header to write to
Packit 90a5c9
 * @param a The array to read from, of size AP_FCGI_HEADER_LEN
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(void) ap_fcgi_header_from_array(ap_fcgi_header *h,
Packit 90a5c9
                                           unsigned char a[]);
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Unpack header of FastCGI record into individual fields
Packit 90a5c9
 * @param version The version, on output
Packit 90a5c9
 * @param type The type, on output
Packit 90a5c9
 * @param request_id The request id, on output
Packit 90a5c9
 * @param content_len The content length, on output
Packit 90a5c9
 * @param padding_len The amount of padding following the content, on output
Packit 90a5c9
 * @param a The array to read from, of size AP_FCGI_HEADER_LEN
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(void) ap_fcgi_header_fields_from_array(unsigned char *version,
Packit 90a5c9
                                                  unsigned char *type,
Packit 90a5c9
                                                  apr_uint16_t *request_id,
Packit 90a5c9
                                                  apr_uint16_t *content_len,
Packit 90a5c9
                                                  unsigned char *padding_len,
Packit 90a5c9
                                                  unsigned char a[]);
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Pack ap_fcgi_begin_request_body
Packit 90a5c9
 * @param h The begin-request body to read from
Packit 90a5c9
 * @param a The array to write to, of size AP_FCGI_HEADER_LEN
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(void) ap_fcgi_begin_request_body_to_array(ap_fcgi_begin_request_body *h,
Packit 90a5c9
                                                     unsigned char a[]);
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Fill in a FastCGI request header with the required field values.
Packit 90a5c9
 * @param header The header to fill in
Packit 90a5c9
 * @param type The type of record
Packit 90a5c9
 * @param request_id The request id
Packit 90a5c9
 * @param content_len The amount of content which follows the header
Packit 90a5c9
 * @param padding_len The amount of padding which follows the content
Packit 90a5c9
 *
Packit 90a5c9
 * The header array must be at least AP_FCGI_HEADER_LEN bytes long.
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(void) ap_fcgi_fill_in_header(ap_fcgi_header *header,
Packit 90a5c9
                                        unsigned char type,
Packit 90a5c9
                                        apr_uint16_t request_id,
Packit 90a5c9
                                        apr_uint16_t content_len,
Packit 90a5c9
                                        unsigned char padding_len);
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Fill in a FastCGI begin request body with the required field values.
Packit 90a5c9
 * @param brb The begin-request-body to fill in
Packit 90a5c9
 * @param role AP_FCGI_RESPONDER or other roles
Packit 90a5c9
 * @param flags 0 or a combination of flags like AP_FCGI_KEEP_CONN
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(void) ap_fcgi_fill_in_request_body(ap_fcgi_begin_request_body *brb,
Packit 90a5c9
                                              int role,
Packit 90a5c9
                                              unsigned char flags);
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Compute the buffer size needed to encode the next portion of
Packit 90a5c9
 * the provided environment table.
Packit 90a5c9
 * @param env The environment table
Packit 90a5c9
 * @param maxlen The maximum buffer size allowable, capped at 
Packit 90a5c9
 * AP_FCGI_MAX_CONTENT_LEN.
Packit 90a5c9
 * @param starting_elem On input, the next element of the table array
Packit 90a5c9
 * to process in this FastCGI record.  On output, the next element to
Packit 90a5c9
 * process on the *next* FastCGI record.
Packit 90a5c9
 * @return Size of buffer needed to encode the next part, or 0
Packit 90a5c9
 * if no more can be encoded.  When 0 is returned: If starting_elem
Packit 90a5c9
 * has reached the end of the table array, all has been encoded;
Packit 90a5c9
 * otherwise, the next envvar can't be encoded within the specified
Packit 90a5c9
 * limit.
Packit 90a5c9
 * @note If an envvar can't be encoded within the specified limit,
Packit 90a5c9
 * the caller can log a warning and increment starting_elem and try 
Packit 90a5c9
 * again or increase the limit or fail, as appropriate for the module.
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(apr_size_t) ap_fcgi_encoded_env_len(apr_table_t *env,
Packit 90a5c9
                                               apr_size_t maxlen,
Packit 90a5c9
                                               int *starting_elem);
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Encode the next portion of the provided environment table using
Packit 90a5c9
 * a buffer previously allocated.
Packit 90a5c9
 * @param r The request, for logging
Packit 90a5c9
 * @param env The environment table
Packit 90a5c9
 * @param buffer A buffer to contain the encoded environment table
Packit 90a5c9
 * @param buflen The length of the buffer, previously computed by
Packit 90a5c9
 * ap_fcgi_encoded_env_len().
Packit 90a5c9
 * @param starting_elem On input, the next element of the table array
Packit 90a5c9
 * to process in this FastCGI record.  On output, the next element to
Packit 90a5c9
 * process on the *next* FastCGI record.
Packit 90a5c9
 * @return APR_SUCCESS if a section could be encoded or APR_ENOSPC
Packit 90a5c9
 * otherwise.
Packit 90a5c9
 * @note The output starting_elem from ap_fcgi_encoded_env_len
Packit 90a5c9
 * shouldn't be used as input to ap_fcgi_encode_env when building the
Packit 90a5c9
 * same FastCGI record.
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(apr_status_t) ap_fcgi_encode_env(request_rec *r,
Packit 90a5c9
                                            apr_table_t *env,
Packit 90a5c9
                                            void *buffer,
Packit 90a5c9
                                            apr_size_t buflen,
Packit 90a5c9
                                            int *starting_elem);
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * String forms for the value of the FCGI_ROLE envvar
Packit 90a5c9
 */
Packit 90a5c9
#define AP_FCGI_RESPONDER_STR   "RESPONDER"
Packit 90a5c9
#define AP_FCGI_AUTHORIZER_STR  "AUTHORIZER"
Packit 90a5c9
#define AP_FCGI_FILTER_STR      "FILTER"
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * FastCGI implementations that implement the AUTHORIZER role
Packit 90a5c9
 * for Apache httpd and allow the application to participate in
Packit 90a5c9
 * any of the Apache httpd AAA phases typically set the variable
Packit 90a5c9
 * FCGI_APACHE_ROLE to one of these strings to indicate the
Packit 90a5c9
 * specific AAA phase.
Packit 90a5c9
 */
Packit 90a5c9
#define AP_FCGI_APACHE_ROLE_AUTHENTICATOR_STR  "AUTHENTICATOR"
Packit 90a5c9
#define AP_FCGI_APACHE_ROLE_AUTHORIZER_STR     "AUTHORIZER"
Packit 90a5c9
#define AP_FCGI_APACHE_ROLE_ACCESS_CHECKER_STR "ACCESS_CHECKER"
Packit 90a5c9
Packit 90a5c9
#ifdef __cplusplus
Packit 90a5c9
}
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
#endif  /* !APACHE_UTIL_FCGI_H */
Packit 90a5c9
/** @} */