1 /* This program is free software; you can redistribute it and/or modify
2  * it under the terms of the GNU General Public License as published by
3  * the Free Software Foundation; version 2 of the License. For a copy,
4  * see http://www.gnu.org/licenses/gpl-2.0.html.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  */
11 
12 #ifndef _CGI_H
13 #define _CGI_H
14 
15 #include <stdbool.h>
16 #include <time.h>
17 #include "liblist.h"
18 #include "ip.h"
19 #include "session.h"
20 
21 #define FCGI_VERSION_1           1
22 
23 #define FCGI_BEGIN_REQUEST       1
24 #define FCGI_ABORT_REQUEST       2
25 #define FCGI_END_REQUEST         3
26 #define FCGI_PARAMS              4
27 #define FCGI_STDIN               5
28 #define FCGI_STDOUT              6
29 #define FCGI_STDERR              7
30 #define FCGI_DATA                8
31 #define FCGI_GET_VALUES          9
32 #define FCGI_GET_VALUES_RESULT  10
33 #define FCGI_UNKNOWN_TYPE       11
34 #define FCGI_MAXTYPE (FCGI_UNKNOWN_TYPE)
35 
36 #define FCGI_HEADER_LENGTH       8
37 
38 typedef enum { cgi_TIMEOUT = -3, cgi_FORCE_QUIT, cgi_ERROR, cgi_OKE, cgi_END_OF_DATA } t_cgi_result;
39 
40 typedef struct type_cgi_info {
41 	t_cgi_type type;
42 	time_t deadline;
43 
44 	int to_cgi, from_cgi, cgi_error;
45 
46 	char *input_buffer, *error_buffer;
47 	int input_buffer_size, error_buffer_size;
48 	unsigned long input_len, error_len;
49 
50 	/* Normal CGI
51 	 */
52 	bool wrap_cgi;
53 
54 	/* FastCGI
55 	 */
56 	bool read_header;
57 	char header[FCGI_HEADER_LENGTH];
58 	size_t fcgi_data_len;
59 } t_cgi_info;
60 
61 int fix_crappy_cgi_headers(t_cgi_info *cgi_info);
62 char *find_cgi_header(char *buffer, int size, char *header);
63 
64 /* FastCGI server loadbalancer
65  */
66 int init_load_balancer(t_fcgi_server *fcgi_server);
67 t_connect_to *select_connect_to(t_fcgi_server *fcgi_server, t_ip_addr *client_ip);
68 t_fcgi_server *fcgi_server_match(t_fcgi_server **fcgi_server, char *extension);
69 t_fcgi_server *find_fcgi_server(t_fcgi_server *fcgi_server, char *id);
70 void manage_load_balancer(t_config *config, time_t now);
71 
72 /* Normal CGI
73  */
74 pid_t fork_cgi_process(t_session *session, t_cgi_info *cgi_info);
75 t_cgi_result read_from_cgi_process(t_session *session, t_cgi_info *cgi_info);
76 
77 /* FastCGI server
78  */
79 int connect_to_fcgi_server(t_connect_to *connect_to);
80 int send_fcgi_request(t_session *session, int sock);
81 t_cgi_result read_from_fcgi_server(t_session *session, t_cgi_info *cgi_info);
82 
83 #endif
84