summaryrefslogtreecommitdiffstats
path: root/src/fluent-bit/lib/monkey/plugins/fastcgi/fcgi_handler.h
blob: a0083ac7d0db9fba1c2969bde99d827992c56499 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*  Monkey HTTP Server
 *  ==================
 *  Copyright 2001-2017 Eduardo Silva <eduardo@monkey.io>
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#ifndef MK_FASTCGI_HANDLER_H
#define MK_FASTCGI_HANDLER_H

#include <monkey/mk_api.h>

/*
 * Based on the information provided by the FastCGI spec, we use the
 * following adapted structures:
 *
 * http://www.fastcgi.com/drupal/node/6?q=node/22
 */
struct fcgi_record_header {
    uint8_t  version;
    uint8_t  type;
    uint16_t request_id;
    uint16_t content_length;
    uint8_t  padding_length;
    uint8_t  reserved;
};

struct fcgi_begin_request_body {
    uint16_t role;
    uint8_t  flags;
    uint8_t  reserved[5];
};

struct fcgi_begin_request_record {
    struct fcgi_record_header header;
    struct fcgi_begin_request_body body;
};

#define FCGI_VERSION_1               1
#define FCGI_RECORD_MAX_SIZE         65535
#define FCGI_RECORD_HEADER_SIZE      sizeof(struct fcgi_record_header)
#define FCGI_BUF_SIZE                FCGI_RECORD_MAX_SIZE + FCGI_RECORD_HEADER_SIZE
#define FCGI_BEGIN_REQUEST_BODY_SIZE sizeof(struct fcgi_begin_request_body)
#define FCGI_RESPONDER  1
#define FCGI_AUTHORIZER 2
#define FCGI_FILTER     3

/*
 * Values for type component of FCGI_Header
 */
#define FCGI_BEGIN_REQUEST       1
#define FCGI_ABORT_REQUEST       2
#define FCGI_END_REQUEST         3
#define FCGI_PARAMS              4
#define FCGI_STDIN               5
#define FCGI_STDOUT              6
#define FCGI_STDERR              7
#define FCGI_DATA                8
#define FCGI_GET_VALUES          9
#define FCGI_GET_VALUES_RESULT  10

/*
 * FastCGI Handler context, it keeps information of states and other
 * request/response references.
 */
struct fcgi_handler {
    struct mk_event event;       /* built-in event-loop data */

    int server_fd;               /* backend FastCGI server         */
    int chunked;                 /* chunked response ?             */
    int active;                  /* is this handler active ?       */
    int hangup;                  /* hangup connection once ready ? */
    int headers_set;             /* headers set ?                  */
    int eof;                     /* exiting: MK_TRUE / MK_FALSE    */

    /* stdin data */
    uint64_t stdin_length;
    uint64_t stdin_offset;
    char *stdin_buffer;

    struct mk_http_session *cs;  /* HTTP session context           */
    struct mk_http_request *sr;  /* HTTP request context           */

    /* FastCGI */
    struct fcgi_begin_request_record header_request;

    uint64_t write_rounds;
    unsigned int buf_len;
    char buf_data[FCGI_BUF_SIZE];

    /* Channel to stream request to the FCGI server */
    struct mk_channel fcgi_channel;
    struct mk_stream  fcgi_stream;

    struct mk_iov *iov;
    struct mk_list _head;

    /* TCP connection context */
    struct mk_net_connection *conn;
};

static inline void fcgi_encode16(void *a, unsigned b)
{
    unsigned char *c = a;

    c[0] = (unsigned char) (b >> 8);
    c[1] = (unsigned char) b;
}

struct fcgi_handler *fcgi_handler_new(struct mk_plugin *plugin,
                                      struct mk_http_session *cs,
                                      struct mk_http_request *sr);

int fcgi_exit(struct fcgi_handler *handler);

#endif