This repository was archived by the owner on Mar 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathfcgi_request.c
More file actions
194 lines (146 loc) · 4.68 KB
/
fcgi_request.c
File metadata and controls
194 lines (146 loc) · 4.68 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <sys/un.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <apr_strings.h>
#include <httpd.h>
#include <http_config.h>
#include <http_log.h>
#include "fcgi_header.h"
#include "fcgi_request.h"
#include "mod_fastcgi_handler.h"
#ifndef SUN_LEN
#define SUN_LEN(sock) \
(sizeof(*(sock)) - sizeof((sock)->sun_path) + strlen((sock)->sun_path))
#endif
static
const char *fcgi_util_socket_make_domain_addr(apr_pool_t *p,
struct sockaddr_un **socket_addr, int *socket_addr_len,
const char *socket_path)
{
size_t socket_path_len = strlen(socket_path);
if (socket_path_len >= sizeof((*socket_addr)->sun_path)) {
return apr_pstrcat(p, "path \"", socket_path,
"\" is too long for a Domain socket", NULL);
}
if (*socket_addr == NULL)
*socket_addr = apr_pcalloc(p, sizeof(struct sockaddr_un));
else
memset(*socket_addr, 0, sizeof(struct sockaddr_un));
(*socket_addr)->sun_family = AF_UNIX;
strcpy((*socket_addr)->sun_path, socket_path);
*socket_addr_len = SUN_LEN(*socket_addr);
return NULL;
}
static
int convert_string_to_in_addr(const char *hostname, struct in_addr *addr)
{
if (inet_aton(hostname, addr) == 0) {
struct hostent *hp;
if ((hp = gethostbyname(hostname)) == NULL)
return -1;
memcpy((char *) addr, hp->h_addr, hp->h_length);
int count = 0;
while (hp->h_addr_list[count] != 0)
count++;
return count == 1 ? 0 : -1;
}
return 0;
}
static
const char *fcgi_util_socket_make_inet_addr(apr_pool_t *p,
struct sockaddr_in **socket_addr, int *socket_addr_len,
const char *host, unsigned short port)
{
if (*socket_addr == NULL)
*socket_addr = apr_pcalloc(p, sizeof(struct sockaddr_in));
else
memset(*socket_addr, 0, sizeof(struct sockaddr_in));
(*socket_addr)->sin_family = AF_INET;
(*socket_addr)->sin_port = htons(port);
/* get an in_addr represention of the host */
if (convert_string_to_in_addr(host, &(*socket_addr)->sin_addr) == -1) {
return apr_pstrcat(p, "failed to resolve \"", host,
"\" to exactly one IP address", NULL);
}
*socket_addr_len = sizeof(struct sockaddr_in);
return NULL;
}
static
const char *fcgi_util_socket_make_addr(apr_pool_t *p, fcgi_request_t *fr)
{
if (!fr->server || !*fr->server)
return apr_pstrdup(p, "empty");
if (*fr->server == '/') {
return fcgi_util_socket_make_domain_addr(p,
(struct sockaddr_un **)&fr->socket_addr,
&fr->socket_addr_len, fr->server);
}
char *host = apr_pstrdup(p, fr->server);
char *port_str = strchr(host, ':');
if (!port_str) {
return apr_pstrdup(p, "no port specified");
} else {
*port_str++ = '\0';
}
unsigned short port = atoi(port_str);
if (port <= 0)
return apr_pstrdup(p, "invalid port sepcified");
return fcgi_util_socket_make_inet_addr(p,
(struct sockaddr_in **)&fr->socket_addr,
&fr->socket_addr_len, host, port);
}
int fcgi_request_create(request_rec *r, fcgi_request_t **frP)
{
apr_pool_t *p = r->pool;
fcgi_request_t *fr = apr_pcalloc(p, sizeof(fcgi_request_t));
/* setup server socket struct */
fr->server = apr_pstrdup(p, r->handler + 5);
const char *err = fcgi_util_socket_make_addr(p, fr);
if (err) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
"FastCGI: invalid server address: '%s': %s",
fr->server, err);
return HTTP_INTERNAL_SERVER_ERROR;
}
/* keep a pointer to cfg and r for convenience */
fr->cfg = ap_get_module_config(r->per_dir_config, &fastcgi_handler_module);
fr->r = r;
*frP = fr;
return OK;
}
#define CHECK(e) do { \
status = e; \
if (status != OK) { \
goto err; \
} \
} while (0)
int fcgi_request_process(fcgi_request_t *fr)
{
apr_pool_t *p = fr->r->pool;
int status;
unsigned int request_id = (fr->r->connection->id & 0xffff) + 1;
/* allocate a buffer for at least FCGI_MAX_LENGTH+FCGI_HEADER_LEN bytes */
uint8_t *record_buffer = apr_pcalloc(p, (FCGI_MAX_LENGTH + FCGI_HEADER_LEN + 1) * 2);
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, fr->r,
"FastCGI: ==> STEP 1 - send FCGI_BEGIN_REQUEST(id=%u)",
request_id);
CHECK(fcgi_server_send_begin_record(fr, request_id, record_buffer));
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, fr->r,
"FastCGI: ==> STEP 2 - send FCGI_PARAMS(id=%u)",
request_id);
CHECK(fcgi_server_send_params_record(fr, request_id, record_buffer));
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, fr->r,
"FastCGI: ==> STEP 3 - send FCGI_STDIN(id=%u)",
request_id);
CHECK(fcgi_server_send_stdin_record(fr, request_id, record_buffer));
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, fr->r,
"FastCGI: ==> STEP 4 - recv FCGI_STDOUT(id=%u)",
request_id);
CHECK(fcgi_server_recv_stdout_stderr_record(fr, request_id, record_buffer));
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, fr->r,
"FastCGI: ==> STEP 5 - return OK(id=%u)",
request_id);
err:
fcgi_server_disconnect(fr);
return status;
}