Christopher Faulet | 63bbf28 | 2019-08-11 23:08:53 +0200 | [diff] [blame] | 1 | /* |
| 2 | * include/common/fcgi.h |
| 3 | * This file contains FastCGI protocol definitions. |
| 4 | * |
| 5 | * Copyright (C) 2019 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com> |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation, version 2.1 |
| 10 | * exclusively. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #ifndef _COMMON_FCGI_H |
| 23 | #define _COMMON_FCGI_H |
| 24 | |
| 25 | #include <inttypes.h> |
| 26 | #include <stdio.h> |
| 27 | #include <common/config.h> |
| 28 | #include <common/standard.h> |
| 29 | #include <common/buf.h> |
| 30 | #include <common/ist.h> |
| 31 | |
| 32 | /* FCGI protocol version */ |
| 33 | #define FCGI_VERSION 0x1 |
| 34 | |
| 35 | /* flags for FCGI_BEGIN_REQUEST records */ |
| 36 | #define FCGI_KEEP_CONN 0x01 |
| 37 | |
| 38 | /* FCGI record's type */ |
| 39 | enum fcgi_record_type { |
| 40 | FCGI_BEGIN_REQUEST = 1, |
| 41 | FCGI_ABORT_REQUEST = 2, |
| 42 | FCGI_END_REQUEST = 3, |
| 43 | FCGI_PARAMS = 4, |
| 44 | FCGI_STDIN = 5, |
| 45 | FCGI_STDOUT = 6, |
| 46 | FCGI_STDERR = 7, |
| 47 | FCGI_DATA = 8, |
| 48 | FCGI_GET_VALUES = 9, |
| 49 | FCGI_GET_VALUES_RESULT = 10, |
| 50 | FCGI_UNKNOWN_TYPE = 11, |
| 51 | FCGI_ENTRIES |
| 52 | } __attribute__((packed)); |
| 53 | |
Christopher Faulet | c5a3eb4 | 2019-10-04 15:20:47 +0200 | [diff] [blame] | 54 | |
Christopher Faulet | 63bbf28 | 2019-08-11 23:08:53 +0200 | [diff] [blame] | 55 | enum fcgi_role { |
| 56 | FCGI_RESPONDER = 1, |
| 57 | FCGI_AUTHORIZER = 2, /* Unsupported */ |
| 58 | FCGI_FILTER = 3, /* Unsupported */ |
| 59 | } __attribute__((packed)); |
| 60 | |
| 61 | /* Protocol status */ |
| 62 | enum fcgi_proto_status { |
| 63 | FCGI_PS_REQUEST_COMPLETE = 0, |
| 64 | FCGI_PS_CANT_MPX_CONN = 1, |
| 65 | FCGI_PS_OVERLOADED = 2, |
| 66 | FCGI_PS_UNKNOWN_ROLE = 3, |
| 67 | FCGI_PS_ENTRIES, |
| 68 | } __attribute__((packed)); |
| 69 | |
| 70 | struct fcgi_header { |
| 71 | uint8_t vsn; |
| 72 | uint8_t type; |
| 73 | uint16_t id; |
| 74 | uint16_t len; |
| 75 | uint8_t padding; |
| 76 | uint8_t rsv; |
| 77 | }; |
| 78 | |
| 79 | struct fcgi_param { |
| 80 | struct ist n; |
| 81 | struct ist v; |
| 82 | }; |
| 83 | |
| 84 | struct fcgi_begin_request { |
| 85 | enum fcgi_role role; |
| 86 | uint8_t flags; |
| 87 | }; |
| 88 | |
| 89 | struct fcgi_end_request { |
| 90 | uint32_t status; |
| 91 | uint8_t errcode; |
| 92 | }; |
| 93 | |
| 94 | struct fcgi_unknown_type { |
| 95 | uint8_t type; |
| 96 | }; |
| 97 | |
| 98 | |
Christopher Faulet | c5a3eb4 | 2019-10-04 15:20:47 +0200 | [diff] [blame] | 99 | static inline const char *fcgi_rt_str(int type) |
| 100 | { |
| 101 | switch (type) { |
| 102 | case FCGI_BEGIN_REQUEST : return "BEGIN_REQUEST"; |
| 103 | case FCGI_ABORT_REQUEST : return "ABORT_REQUEST"; |
| 104 | case FCGI_END_REQUEST : return "END_REQUEST"; |
| 105 | case FCGI_PARAMS : return "PARAMS"; |
| 106 | case FCGI_STDIN : return "STDIN"; |
| 107 | case FCGI_STDOUT : return "STDOUT"; |
| 108 | case FCGI_STDERR : return "STDERR"; |
| 109 | case FCGI_DATA : return "DATA"; |
| 110 | case FCGI_GET_VALUES : return "GET_VALUES"; |
| 111 | case FCGI_GET_VALUES_RESULT : return "GET_VALUES_RESULT"; |
| 112 | case FCGI_UNKNOWN_TYPE : return "UNKNOWN_TYPE"; |
| 113 | default : return "_UNKNOWN_"; |
| 114 | } |
| 115 | } |
| 116 | |
Christopher Faulet | 63bbf28 | 2019-08-11 23:08:53 +0200 | [diff] [blame] | 117 | |
| 118 | int fcgi_encode_record_hdr(struct buffer *out, const struct fcgi_header *h); |
| 119 | size_t fcgi_decode_record_hdr(const struct buffer *in, size_t o, struct fcgi_header *h); |
| 120 | |
| 121 | int fcgi_encode_begin_request(struct buffer *out, const struct fcgi_begin_request *r); |
| 122 | |
| 123 | int fcgi_encode_param(struct buffer *out, const struct fcgi_param *p); |
| 124 | size_t fcgi_decode_param(const struct buffer *in, size_t o, struct fcgi_param *p); |
| 125 | size_t fcgi_aligned_decode_param(const struct buffer *in, size_t o, struct fcgi_param *p); |
| 126 | |
| 127 | size_t fcgi_decode_end_request(const struct buffer *in, size_t o, struct fcgi_end_request *r); |
| 128 | |
| 129 | #endif /* _COMMON_FCGI_H */ |
| 130 | |
| 131 | /* |
| 132 | * Local variables: |
| 133 | * c-indent-level: 8 |
| 134 | * c-basic-offset: 8 |
| 135 | * End: |
| 136 | */ |