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
|
/* copyright (c) 2022 - 2026 grunfink et al. / MIT license */
/*
This is an intentionally-dead-simple FastCGI implementation;
only FCGI_RESPONDER type with *no* FCGI_KEEP_CON flag is supported.
This means only one simultaneous connection and no multiplexing.
It seems it's enough for nginx and OpenBSD's httpd, so here it goes.
Almost fully compatible with xs_httpd.h
*/
#ifndef _XS_FCGI_H
#define _XS_FCGI_H
xs_dict *xs_fcgi_request(FILE *f, xs_str **payload, int *p_size, int *id);
void xs_fcgi_response(FILE *f, int status, const xs_dict *headers, const xs_str *body, int b_size, int id);
#ifdef XS_IMPLEMENTATION
struct fcgi_record_header {
unsigned char version;
unsigned char type;
unsigned short id;
unsigned short content_len;
unsigned char padding_len;
unsigned char reserved;
} __attribute__((packed));
/* version */
#define FCGI_VERSION_1 1
/* types */
#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
#define FCGI_UNKNOWN_TYPE 11
#define FCGI_MAXTYPE (FCGI_UNKNOWN_TYPE)
struct fcgi_begin_request {
unsigned short role;
unsigned char flags;
unsigned char reserved[5];
} __attribute__((packed));
/* roles */
#define FCGI_RESPONDER 1
#define FCGI_AUTHORIZER 2
#define FCGI_FILTER 3
/* flags */
#define FCGI_KEEP_CONN 1
struct fcgi_end_request {
unsigned int app_status;
unsigned char protocol_status;
unsigned char reserved[3];
} __attribute__((packed));
/* protocol statuses */
#define FCGI_REQUEST_COMPLETE 0
#define FCGI_CANT_MPX_CONN 1
#define FCGI_OVERLOADED 2
#define FCGI_UNKNOWN_ROLE 3
xs_dict *xs_fcgi_request(FILE *f, xs_str **payload, int *p_size, int *fcgi_id)
/* keeps receiving FCGI packets until a complete request is finished */
{
unsigned char p_buf[100000];
struct fcgi_record_header hdr;
struct fcgi_begin_request *breq = (struct fcgi_begin_request *)&p_buf;
unsigned char *buf = NULL;
int b_size = 0;
xs_dict *req = NULL;
unsigned char p_status = FCGI_REQUEST_COMPLETE;
xs *q_vars = NULL;
xs *p_vars = NULL;
*fcgi_id = -1;
for (;;) {
int sz, psz;
/* read the packet header */
if (fread(&hdr, sizeof(hdr), 1, f) != 1)
break;
/* read the packet body */
if ((psz = ntohs(hdr.content_len)) > 0) {
if ((sz = fread(p_buf, 1, psz, f)) != psz)
break;
}
/* read (and drop) the padding */
if (hdr.padding_len > 0)
fread(p_buf + sz, 1, hdr.padding_len, f);
switch (hdr.type) {
case FCGI_BEGIN_REQUEST:
/* fail on unsupported roles */
if (ntohs(breq->role) != FCGI_RESPONDER) {
p_status = FCGI_UNKNOWN_ROLE;
goto end;
}
/* fail on unsupported flags */
if (breq->flags & FCGI_KEEP_CONN) {
p_status = FCGI_CANT_MPX_CONN;
goto end;
}
/* store the id for later */
*fcgi_id = (int) hdr.id;
break;
case FCGI_PARAMS:
/* unknown id? fail */
if (hdr.id != *fcgi_id) {
p_status = FCGI_CANT_MPX_CONN;
goto end;
}
if (psz) {
/* add to the buffer */
buf = xs_realloc(buf, b_size + psz);
memcpy(buf + b_size, p_buf, psz);
b_size += psz;
}
else {
/* no size, so the packet is complete; process it */
xs *cgi_vars = xs_dict_new();
req = xs_dict_new();
int offset = 0;
while (offset < b_size) {
unsigned int ksz = buf[offset++];
if (ksz & 0x80) {
ksz &= 0x7f;
ksz = (ksz << 8) | buf[offset++];
ksz = (ksz << 8) | buf[offset++];
ksz = (ksz << 8) | buf[offset++];
}
unsigned int vsz = buf[offset++];
if (vsz & 0x80) {
vsz &= 0x7f;
vsz = (vsz << 8) | buf[offset++];
vsz = (vsz << 8) | buf[offset++];
vsz = (vsz << 8) | buf[offset++];
}
/* get the key */
xs *k = xs_str_new_sz((char *)&buf[offset], ksz);
offset += ksz;
/* get the value */
xs *v = xs_str_new_sz((char *)&buf[offset], vsz);
offset += vsz;
if (!xs_is_string(k) || !xs_is_string(v))
continue;
cgi_vars = xs_dict_append(cgi_vars, k, v);
if (strcmp(k, "REQUEST_METHOD") == 0)
req = xs_dict_append(req, "method", v);
else
if (strcmp(k, "REQUEST_URI") == 0) {
req = xs_dict_append(req, "raw_path", v);
xs *pnv = xs_split_n(v, "?", 1);
/* store the path */
req = xs_dict_append(req, "path", xs_list_get(pnv, 0));
/* get the variables */
q_vars = xs_url_vars(xs_list_get(pnv, 1));
}
else
if (xs_match(k, "CONTENT_TYPE|CONTENT_LENGTH|REMOTE_ADDR|HTTP_*")) {
if (xs_startswith(k, "HTTP_"))
k = xs_crop_i(k, 5, 0);
k = xs_tolower_i(k);
k = xs_replace_i(k, "_", "-");
req = xs_dict_append(req, k, v);
}
}
req = xs_dict_append(req, "cgi_vars", cgi_vars);
buf = xs_free(buf);
b_size = 0;
}
break;
case FCGI_STDIN:
/* unknown id? fail */
if (hdr.id != *fcgi_id) {
p_status = FCGI_CANT_MPX_CONN;
goto end;
}
if (psz) {
/* add to the buffer */
buf = xs_realloc(buf, b_size + psz);
memcpy(buf + b_size, p_buf, psz);
b_size += psz;
}
else {
/* add an asciiz to be able to treat it as a string */
buf = xs_realloc(buf, _xs_blk_size(b_size + 1));
buf[b_size] = '\0';
/* fill the payload info and finish */
*payload = (xs_str *)buf;
*p_size = b_size;
const char *ct = xs_dict_get(req, "content-type");
if (*payload && ct && strcmp(ct, "application/x-www-form-urlencoded") == 0) {
p_vars = xs_url_vars(*payload);
}
else
if (*payload && ct && xs_startswith(ct, "multipart/form-data")) {
p_vars = xs_multipart_form_data(*payload, *p_size, ct);
}
else
p_vars = xs_dict_new();
if (q_vars == NULL)
q_vars = xs_dict_new();
req = xs_dict_append(req, "q_vars", q_vars);
req = xs_dict_append(req, "p_vars", p_vars);
/* disconnect the payload from the buf variable */
buf = NULL;
goto end;
}
break;
}
}
end:
/* any kind of error? notify and cleanup */
if (p_status != FCGI_REQUEST_COMPLETE) {
struct fcgi_end_request ereq = {0};
/* complete the connection */
ereq.app_status = 0;
ereq.protocol_status = p_status;
/* reuse header */
hdr.type = FCGI_ABORT_REQUEST;
hdr.content_len = htons(sizeof(ereq));
fwrite(&hdr, sizeof(hdr), 1, f);
fwrite(&ereq, sizeof(ereq), 1, f);
/* session closed */
*fcgi_id = -1;
/* request dict is not valid */
req = xs_free(req);
}
xs_free(buf);
return req;
}
void xs_fcgi_response(FILE *f, int status, const xs_dict *headers, const xs_str *body, int b_size, int fcgi_id)
/* writes an FCGI response */
{
struct fcgi_record_header hdr = {0};
struct fcgi_end_request ereq = {0};
xs *out = xs_str_new(NULL);
const xs_str *k;
const xs_str *v;
/* no previous id? it's an error */
if (fcgi_id == -1)
return;
/* create the headers */
{
xs *s1 = xs_fmt("status: %d\r\n", status);
out = xs_str_cat(out, s1);
}
xs_dict_foreach(headers, k, v) {
xs *s1 = xs_fmt("%s: %s\r\n", k, v);
out = xs_str_cat(out, s1);
}
if (b_size > 0) {
xs *s1 = xs_fmt("content-length: %d\r\n", b_size);
out = xs_str_cat(out, s1);
}
out = xs_str_cat(out, "\r\n");
/* everything is text by now */
int size = strlen(out);
/* add the body */
if (body != NULL && b_size > 0) {
out = xs_append_m(out, body, b_size);
size += b_size;
}
/* now send all the STDOUT in packets */
hdr.version = FCGI_VERSION_1;
hdr.type = FCGI_STDOUT;
hdr.id = fcgi_id;
int offset = 0;
while (offset < size) {
size_t sz = size - offset;
if (sz > 0xffff)
sz = 0xffff;
hdr.content_len = htons(sz);
/* write or fail */
if (!fwrite(&hdr, sizeof(hdr), 1, f) || fwrite(out + offset, 1, sz, f) != sz)
return;
offset += sz;
}
/* final STDOUT packet with 0 size */
hdr.content_len = 0;
if (!fwrite(&hdr, sizeof(hdr), 1, f))
return;
/* complete the connection */
ereq.app_status = 0;
ereq.protocol_status = FCGI_REQUEST_COMPLETE;
hdr.type = FCGI_END_REQUEST;
hdr.content_len = htons(sizeof(ereq));
if (fwrite(&hdr, sizeof(hdr), 1, f))
fwrite(&ereq, sizeof(ereq), 1, f);
}
#endif /* XS_IMPLEMENTATION */
#endif /* XS_URL_H */
|