-
Notifications
You must be signed in to change notification settings - Fork 6
/
fcgi.c
417 lines (392 loc) · 13.3 KB
/
fcgi.c
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/**
* @file fcgi.c
* FastCGI handler for Apteryx-rest
*
* Copyright 2018, Allied Telesis Labs New Zealand, Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "internal.h"
#include <sys/socket.h>
#include <poll.h>
#undef PACKAGE
#undef VERSION
#include <fcgi_config.h>
#include <fcgiapp.h>
#define UNKNOWN_STR "unknown"
static req_callback g_cb;
static const char *g_socket = NULL;
static int g_sock = -1;
static GThread *g_thread = NULL;
static void
dump_request (FCGX_Request * r)
{
char **envp = r->envp;
VERBOSE ("FCGI_PARAMS:\n");
while (envp && *envp) {
VERBOSE ("%s\n", *envp);
envp++;
}
return;
}
static int
get_flags (FCGX_Request * r)
{
int flags = 0;
char *param;
/* Method */
param = FCGX_GetParam ("REQUEST_METHOD", r->envp);
if (g_strcmp0 (param, "GET") == 0)
flags |= FLAGS_METHOD_GET;
else if (g_strcmp0 (param, "POST") == 0)
flags |= FLAGS_METHOD_POST;
else if (g_strcmp0 (param, "PUT") == 0)
flags |= FLAGS_METHOD_PUT;
else if (g_strcmp0 (param, "PATCH") == 0)
flags |= FLAGS_METHOD_PATCH;
else if (g_strcmp0 (param, "DELETE") == 0)
flags |= FLAGS_METHOD_DELETE;
else if (g_strcmp0 (param, "HEAD") == 0)
flags |= FLAGS_METHOD_HEAD;
else if (g_strcmp0 (param, "OPTIONS") == 0)
flags |= FLAGS_METHOD_OPTIONS;
else
{
ERROR ("Method \"%s\" not allowed\n", param);
return -405;
}
/* Parse content type */
param = FCGX_GetParam ("HTTP_CONTENT_TYPE", r->envp);
if (!param)
param = FCGX_GetParam ("CONTENT_TYPE", r->envp);
if (param && strlen(param) > 0)
{
/* Some clients will encode raw values as text/plain,
but we can pretend it is json encoded */
if (g_strcmp0 (param, "application/json") == 0 ||
g_strcmp0 (param, "text/plain") == 0)
flags |= FLAGS_CONTENT_JSON;
else if (g_strcmp0 (param, "application/yang-data+json") == 0)
flags |= FLAGS_CONTENT_JSON | FLAGS_RESTCONF;
// else if (g_strcmp0 (param, "application/xml") == 0)
// flags |= FLAGS_CONTENT_XML;
// else if (g_strcmp0 (param, "application/yang.data+xml") == 0)
// flags |= FLAGS_CONTENT_XML | FLAGS_RESTCONF;
else
{
ERROR ("Media-Type \"%s\" not allowed\n", param);
return -415;
}
}
else if (flags & (FLAGS_METHOD_POST|FLAGS_METHOD_PUT|FLAGS_METHOD_PATCH))
{
/* Assume default encoding */
flags |= default_content_encoding;
}
/* Parse accept types */
param = FCGX_GetParam ("HTTP_ACCEPT", r->envp);
if (!param)
param = FCGX_GetParam ("HTTP_Accept", r->envp);
if (param)
{
if (g_strrstr (param, "application/json") != 0)
flags |= FLAGS_ACCEPT_JSON;
else if (g_strrstr (param, "application/yang-data+json") != 0)
flags |= FLAGS_ACCEPT_JSON | FLAGS_RESTCONF;
// if (g_strrstr (param, "application/xml") != 0)
// flags |= FLAGS_ACCEPT_XML;
// if (g_strrstr (param, "application/yang-data+xml") != 0)
// flags |= FLAGS_ACCEPT_XML | FLAGS_RESTCONF;
else if (g_strrstr (param, "text/event-stream") != 0)
flags |= FLAGS_EVENT_STREAM | FLAGS_ACCEPT_JSON;
else if (g_strrstr (param, "application/stream+json") != 0)
flags |= FLAGS_APPLICATION_STREAM | FLAGS_ACCEPT_JSON;
/* Any encoding - use default */
else if (g_strrstr (param, "*/*") != 0)
flags |= default_accept_encoding;
else
{
ERROR ("Media-Type \"%s\" not allowed\n", param);
return -406;
}
}
/* JSON formatinng */
param = FCGX_GetParam ("HTTP_X_JSON_ROOT", r->envp);
if (!param)
param = FCGX_GetParam ("HTTP_X-JSON-Root", r->envp);
flags |= FLAGS_JSON_FORMAT_ROOT;
if (param && strcmp (param, "off") == 0)
flags &= ~FLAGS_JSON_FORMAT_ROOT;
param = FCGX_GetParam ("HTTP_X_JSON_MULTI", r->envp);
if (!param)
param = FCGX_GetParam ("HTTP_X-JSON-Multi", r->envp);
if (param && strcmp (param, "on") == 0)
flags |= FLAGS_JSON_FORMAT_MULTI;
/* Format lists as JSON arrays */
if (rest_use_arrays)
flags |= FLAGS_JSON_FORMAT_ARRAYS;
param = FCGX_GetParam ("HTTP_X_JSON_ARRAY", r->envp);
if (!param)
param = FCGX_GetParam ("HTTP_X-JSON-Array", r->envp);
if (flags & FLAGS_RESTCONF || (param && strcmp (param, "on") == 0))
flags |= FLAGS_JSON_FORMAT_ARRAYS;
if (param && strcmp (param, "off") == 0)
flags &= ~FLAGS_JSON_FORMAT_ARRAYS;
/* Encode values as JSON types */
if (rest_use_types)
flags |= FLAGS_JSON_FORMAT_TYPES;
param = FCGX_GetParam ("HTTP_X_JSON_TYPES", r->envp);
if (!param)
param = FCGX_GetParam ("HTTP_X-JSON-Types", r->envp);
if (flags & FLAGS_RESTCONF || (param && strcmp (param, "on") == 0))
flags |= FLAGS_JSON_FORMAT_TYPES;
if (param && strcmp (param, "off") == 0)
flags &= ~FLAGS_JSON_FORMAT_TYPES;
/* Process config only nodes */
param = FCGX_GetParam ("HTTP_X_CONFIG_ONLY", r->envp);
if (!param)
param = FCGX_GetParam ("HTTP_X-Config-Only", r->envp);
if (!(flags & FLAGS_RESTCONF) || (param && strcmp (param, "on") == 0))
flags |= FLAGS_CONFIG_ONLY;
if (param && strcmp (param, "off") == 0)
flags &= ~FLAGS_CONFIG_ONLY;
/* Prefix model names */
param = FCGX_GetParam ("HTTP_X_JSON_NAMESPACE", r->envp);
if (!param)
param = FCGX_GetParam ("HTTP_X-JSON-Namespace", r->envp);
if (flags & FLAGS_RESTCONF || (param && strcmp (param, "on") == 0))
flags |= FLAGS_JSON_FORMAT_NS;
if (param && strcmp (param, "off") == 0)
flags &= ~FLAGS_JSON_FORMAT_NS;
if (flags & FLAGS_RESTCONF)
flags |= FLAGS_CONDITIONS | FLAGS_IDREF_VALUES;
/* PUT flags for RESTCONF compliance */
if (flags & FLAGS_RESTCONF)
flags |= FLAGS_PUT_KEY_VALUE_DATA | FLAGS_PUT_REPLACE;
return flags;
}
/* Implementation of
https://datatracker.ietf.org/doc/html/rfc3986#section-5.2.4
Most web servers will have already done this for us
but it is probably faster to do again then trying to
figure out if it is even needed.
*/
static char *
normalise_path(const char *path)
{
GString *normalised = g_string_new(NULL);
char *copy = g_strdup(path);
char *saveptr = NULL;
char *token = strtok_r((char *)copy, "/", &saveptr);
char *last = NULL;
while (token) {
if (strcmp(token, ".") == 0) {
/* Ignore . */
} else if (strcmp(token, "..") == 0) {
/* Remove the last segment */
g_string_truncate(normalised, strlen(last) + 1);
} else {
/* Add the new segment */
g_string_append_printf(normalised, "/%s", token);
}
last = token;
token = strtok_r(NULL, "/", &saveptr);
}
free(copy);
if (path[strlen(path) - 1] == '/')
g_string_append_c(normalised, '/');
return g_string_free(normalised, false);
}
static void *
handle_http (void *arg)
{
FCGX_Request *request = (FCGX_Request *) arg;
char *rpath, *uri, *path, *length, *if_match, *if_none_match, *if_modified_since, *if_unmodified_since;
char *server_name, *server_port, *remote_addr, *remote_user;
int flags;
char *data = NULL;
int len = 0;
int i;
int rc = 0;
DEBUG ("FCGI(%p): New connection\n", request);
/* Debug */
if (verbose)
{
dump_request (request);
}
/* Process the request */
rpath = FCGX_GetParam ("DOCUMENT_ROOT", request->envp);
if (!rpath)
rpath = FCGX_GetParam ("DOCUMENTS", request->envp);
uri = FCGX_GetParam ("REQUEST_URI", request->envp);
path = uri ? normalise_path(uri) : NULL;
if (!path)
{
ERROR ("Failed to parse URI path '%s'\n", uri);
rc = 500;
goto exit;
}
flags = get_flags (request);
if (flags < 0)
{
rc = -(flags);
goto exit;
}
server_port = FCGX_GetParam ("SERVER_PORT", request->envp);
server_name = FCGX_GetParam ("SERVER_NAME", request->envp);
if (server_name && g_strcmp0 (server_name, "*:80") == 0)
server_name = NULL;
if (!server_name)
server_name = FCGX_GetParam ("SERVER_ADDR", request->envp);
remote_addr = FCGX_GetParam("REMOTE_ADDR", request->envp);
if (!remote_addr)
remote_addr = UNKNOWN_STR;
remote_user = FCGX_GetParam("REMOTE_USER", request->envp);
if (!remote_user)
remote_user = UNKNOWN_STR;
if_match = FCGX_GetParam ("HTTP_IF_MATCH", request->envp);
if (!if_match)
if_match = FCGX_GetParam ("HTTP_If-Match", request->envp);
if_none_match = FCGX_GetParam ("HTTP_IF_NONE_MATCH", request->envp);
if (!if_none_match)
if_none_match = FCGX_GetParam ("HTTP_If-None-Match", request->envp);
if_modified_since = FCGX_GetParam ("HTTP_IF_MODIFIED_SINCE", request->envp);
if (!if_modified_since)
if_modified_since = FCGX_GetParam ("HTTP_If-Modified-Since", request->envp);
if_unmodified_since = FCGX_GetParam ("HTTP_IF_UNMODIFIED_SINCE", request->envp);
if (!if_unmodified_since)
if_unmodified_since = FCGX_GetParam ("HTTP_If-Unmodified-Since", request->envp);
length = FCGX_GetParam ("CONTENT_LENGTH", request->envp);
if (!rpath || !path)
{
ERROR ("Invalid server configuration (flags:0x%x, rpath:%s, path:%s)\n",
flags, rpath, path);
rc = 500;
goto exit;
}
if (length != NULL)
{
len = strtol (length, NULL, 10);
data = calloc (len + 1, 1);
for (i = 0; i < len; i++)
{
if ((data[i] = FCGX_GetChar (request->in)) < 0)
{
ERROR ("ERROR: Not enough bytes received on standard input\n");
break;
}
}
}
g_cb ((req_handle) request, flags, rpath, path, if_match, if_none_match, if_modified_since,
if_unmodified_since, server_name, server_port, remote_addr, remote_user, data, len);
exit:
if (rc)
{
char *resp = g_strdup_printf ("Status: %d\r\n"
"Content-Type: text/html\r\n\r\n"
"Error. Check device log for more detail\n",
rc);
VERBOSE ("RESP:\n%s\n", resp);
send_response (request, resp, false);
free (resp);
}
free (data);
DEBUG ("FCGI(%p): Closing connection\n", request);
FCGX_Finish_r (request);
g_free (request);
free(path);
return NULL;
}
static void *
handle_fcgi (void *arg)
{
GThreadPool *workers = g_thread_pool_new ((GFunc) handle_http, NULL, -1, FALSE, NULL);
while (workers)
{
FCGX_Request *request = g_malloc0 (sizeof (FCGX_Request));
FCGX_InitRequest (request, g_sock, FCGI_FAIL_ACCEPT_ON_INTR);
if (FCGX_Accept_r (request) < 0)
{
DEBUG ("FCGX_Accept_r: %s\n", strerror (errno));
g_free (request);
break;
}
g_thread_pool_push (workers, request, NULL);
}
DEBUG ("Stopping FCGI handler\n");
g_thread_pool_free (workers, true, false);
return NULL;
}
bool
fcgi_start (const char *socket, req_callback cb)
{
DEBUG ("Starting FCGI handler on %s\n", socket);
g_socket = socket;
g_cb = cb;
/* Initialise the fastcgi library */
if (FCGX_Init () != 0)
{
ERROR ("FCGX_Init failed: %s\n", strerror (errno));
return false;
}
/* Open the user provided socket */
if ((g_sock = FCGX_OpenSocket (g_socket, 10)) < 0)
{
ERROR ("FCGX_OpenSocket failed: %s\n", strerror (errno));
return false;
}
/* Create a thread to handle requests */
if ((g_thread = g_thread_new ("fcgi handler", &handle_fcgi, NULL)) == NULL)
{
ERROR ("Failed to launch FCGI handler thread\n");
return false;
}
return true;
}
void
send_response (req_handle handle, const char *data, bool flush)
{
FCGX_Request *request = (FCGX_Request *) handle;
DEBUG ("FCGI(%p): send %lu bytes\n", request, strlen (data));
FCGX_PutS (data, request->out);
if (flush)
FCGX_FFlush (request->out);
}
bool
is_connected (req_handle handle, bool block)
{
FCGX_Request *request = (FCGX_Request *) handle;
struct pollfd pfd;
pfd.fd = request->ipcFd;
pfd.events = POLLERR | POLLHUP;
pfd.revents = 0;
poll (&pfd, 1, block ? -1 : 1000);
if (pfd.revents & (POLLERR | POLLHUP))
return false;
return true;
}
void
fcgi_stop (void)
{
/* Shutdown any pending requests */
FCGX_ShutdownPending ();
/* Shutdown the socket */
if (g_sock != -1)
{
shutdown (g_sock, SHUT_RD);
unlink (g_socket);
}
/* Wait for the thread to complete */
g_thread_join (g_thread);
}