path:
/server_fcgi.c
3.31 KB | 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <stddef.h>
#include <stdint.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <kcgi.h>
#include "fiche.h"
struct fiche_connection_fcgi {
struct fiche_connection base;
struct kreq *request;
struct kfcgi *fcgi;
char *post_data;
size_t post_len;
char *authenticated_user;
};
static int fcgi_read_data(struct fiche_connection *c) {
struct fiche_connection_fcgi *fcgi = (struct fiche_connection_fcgi *)c;
struct kreq *r = fcgi->request;
if (!r) return -1;
// Only handle POST requests
if (r->method != KMETHOD_POST) {
return -1;
}
if (!(r->fieldsz && r->fields[0].valsz)) {
return -1;
// FIXME show error log
}
// Allocate buffer
uint8_t* str = (uint8_t *) r->fields[0].val;
size_t str_len = r->fields[0].valsz;
// Zero-copy
c->buffer = str;
c->rx_len = str_len;
// Hallucinated
// Store authenticated user if available
// fcgi->authenticated_user = strdup(khttp_auth(fcgi->request));
return 0;
}
// FCGI-specific write implementation
ssize_t server_write(struct fiche_connection *c, const char *data, size_t len) {
struct fiche_connection_fcgi *fcgi = (struct fiche_connection_fcgi *)c;
struct kreq *req = fcgi->request;
// Send HTTP headers
khttp_head(req, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]);
khttp_head(req, kresps[KRESP_CONTENT_TYPE], "%s", "text/plain");
khttp_body(req);
// Send data
return khttp_write(req, data, len);
}
// FCGI-specific close implementation
void server_cleanup(struct fiche_connection *c) {
struct fiche_connection_fcgi *fcgi = (struct fiche_connection_fcgi *)c;
if (fcgi->request) {
khttp_free(fcgi->request);
fcgi->request = NULL;
}
if (c->buffer) {
free(c->buffer);
c->buffer = NULL;
}
if (fcgi->authenticated_user) {
free(fcgi->authenticated_user);
fcgi->authenticated_user = NULL;
}
}
// Create an FCGI connection
static struct fiche_connection_fcgi*
create_fcgi_connection(
struct kreq *req,
struct kfcgi *fcgi,
Fiche_Settings *settings) {
struct fiche_connection_fcgi *fcgi_conn = malloc(sizeof(*fcgi_conn));
if (!fcgi_conn) return NULL;
// Initialize base struct
fcgi_conn->base.buffer = NULL;
fcgi_conn->base.buffer_len = 0;
fcgi_conn->base.rx_len = 0;
fcgi_conn->base.settings = settings;
// Initialize FCGI-specific fields
fcgi_conn->request = req;
fcgi_conn->fcgi = fcgi;
fcgi_conn->post_data = NULL;
fcgi_conn->post_len = 0;
fcgi_conn->authenticated_user = NULL;
return fcgi_conn;
}
int start_server(Fiche_Settings *settings) {
struct kreq r;
struct kfcgi *fcgi;
enum kcgi_err er;
if (khttp_fcgi_init(&fcgi, NULL, 0, NULL, 0, 0) != KCGI_OK) {
fprintf(stderr, "Failed to initialize kcgi\n");
return -1;
}
printf("fiche FCGI server started\n");
for (;;) {
/* Set up the current HTTP context. */
er = khttp_fcgi_parse(fcgi, &r);
if (er == KCGI_EXIT)
fprintf(stderr, "khttp_fcgi_parse: terminate\n");
else if (er != KCGI_OK)
fprintf(stderr, "khttp_fcgi_parse: error: %d\n", er);
if (er != KCGI_OK)
break;
// Create FCGI connection with proper inheritance
struct fiche_connection_fcgi *fcgi_conn
= create_fcgi_connection(&r, fcgi, settings);
if (!fcgi_conn) {
khttp_free(&r);
continue;
}
post_handle_connection((struct fiche_connection *)fcgi_conn);
// Note: process_connection frees the connection
}
khttp_fcgi_free(fcgi);
return 0;
}