forked from pforemski/rpcd
/
read.c
218 lines (172 loc) · 5.02 KB
/
read.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
/*
* Copyright (C) 2009-2010 Pawel Foremski <pawel@foremski.pl>
* Licensed under GPLv3
*/
#include "common.h"
/** Common part of request parser, usually after JSON representation is made available in req->params
* @param req the request
* @param leave leave the req->params */
static bool common(struct req *req, bool leave)
{
ut *ut;
/* guarantee that req->params is ok */
if (!ut_ok(req->params)) {
req->reply = req->params;
req->params = NULL;
return false;
} else if (ut_type(req->params) != T_HASH) {
return errcode(JSON_RPC_INVALID_REQUEST);
}
/* JSON-RPC argument check */
if ((ut = uth_get(req->params, "service"))) /* used by Qooxdoo */
req->service = ut_char(ut);
if ((ut = uth_get(req->params, "method")))
req->method = ut_char(ut);
if ((ut = uth_get(req->params, "id")))
req->id = ut_char(ut);
/* XXX: dont check jsonrpc=2.0 */
if (!leave) {
if ((ut = uth_get(req->params, "params")))
req->params = ut;
else
req->params = uth_set_thash(req->params, "params", NULL); /* create empty hash */
if (ut_is_tlist(req->params) && thash_get(req->http.headers, "X-Qooxdoo-Response-Type")) {
tlist *tl = ut_tlist(req->params);
req->params = tlist_shift(tl);
}
}
return true;
}
static bool readjson_len(struct req *req, int len)
{
char buf[BUFSIZ];
xstr *xs = xstr_create("", req);
json *js;
if (len < 0) {
while (fgets(buf, sizeof(buf), stdin)) {
if (!buf[0] || buf[0] == '\n') break;
xstr_append(xs, buf);
}
} else {
int r;
while ((r = fread(buf, 1, MIN(len, sizeof(buf)), stdin))) {
if (r < 0) {
dbg(5, "fread() returned %d, len=%d\n", r, len);
break;
}
/* always appends \0 */
xstr_append_size(xs, buf, r);
len -= r;
if (len <= 0)
break;
}
}
/* eof? */
if (xstr_length(xs) == 0) exit(0);
js = json_create(req);
req->params = json_parse(js, xstr_string(xs));
return common(req, false);
}
bool readjson(struct req *req)
{
return readjson_len(req, -1);
}
bool read822(struct req *req)
{
char buf[BUFSIZ];
xstr *input = xstr_create("", req);
while (fgets(buf, sizeof(buf), stdin)) {
if (!buf[0] || buf[0] == '\n') break;
xstr_append(input, buf);
}
/* eof? */
if (xstr_length(input) == 0) exit(0);
dbg(8, "parsing %s\n", xstr_string(input));
req->params = ut_new_thash(
rfc822_parse(xstr_string(input), req),
req);
return common(req, true);
}
bool readhttp(struct req *req)
{
enum http_type ht;
char first[256], buf[BUFSIZ], *ct, *cl, *ac, *uri, *auth;
int len;
xstr *xs = xstr_create("", req);
/* read query */
if (!fgets(first, sizeof(first), stdin) || first[0] == '\n')
exit(0); /* eof */
if (strncmp(first, "POST ", 5) == 0) {
ht = POST;
uri = first + 5;
} else if (strncmp(first, "OPTIONS ", 8) == 0) {
ht = OPTIONS;
uri = first + 8;
} else if (strncmp(first, "GET ", 4) == 0 && O.http.htdocs) { /* @1 */
ht = GET;
uri = first + 4;
} else {
dbg(4, "invalid method: %s\n", first);
return errmsg("Invalid HTTP method");
}
/* read headers */
while (fgets(buf, sizeof(buf), stdin)) {
if (!buf[0] || buf[0] == '\n' || buf[0] == '\r') break;
xstr_append(xs, buf);
}
req->http.headers = rfc822_parse(xstr_string(xs), req);
/* fetch authentication information ASAP */
auth = thash_get(req->http.headers, "Authorization");
if (auth && strncmp(auth, "Basic ", 6) == 0) {
xstr *ad = asn_b64_dec(auth+6, req);
char *pass = strchr(xstr_string(ad), ':');
if (pass) {
*pass++ = '\0';
req->http.user = xstr_string(ad);
req->http.pass = pass;
}
}
const char *cc = thash_get(req->http.headers, "Connection");
if (cc && (streq(cc, "close") || streq(cc, "Close")))
req->last = true;
if (ht == OPTIONS)
return errcode(JSON_RPC_HTTP_OPTIONS);
/* handle static query, note that htdocs!=NULL checked @1 */
if (ht == GET) {
req->http.needauth = true;
char *space = strchr(uri, ' ');
if (space) *space = '\0';
char *params = strchr(uri, '?');
if (params) *params = '\0';
if (streq(uri, "/")) {
uri = "/index.html";
} else if (strstr(uri, "..")) {
dbg(4, "invalid uri: '%s'\n", uri);
return errcode(JSON_RPC_HTTP_NOT_FOUND);
}
req->http.uripath = mmatic_printf(req, "%s%s", O.http.htdocs, uri);
if (asn_isfile(req->http.uripath) > 0) {
dbg(4, "GET '%s'\n", req->http.uripath);
return errcode(JSON_RPC_HTTP_GET);
}
dbg(4, "not found: '%s'\n", uri);
req->http.uripath = NULL;
return errcode(JSON_RPC_HTTP_NOT_FOUND);
}
/* = POST - ie. normal RPC call = */
ct = thash_get(req->http.headers, "Content-Type");
if (!ct) return errmsg("Content-Type needed");
if (strncmp(ct, "application/json", 16) != 0)
return errmsg("Unsupported Content-Type");
ac = thash_get(req->http.headers, "Accept");
if (!ac) return errmsg("Accept needed");
if (!strstr(ac, "application/json") && !strstr(ac, "*/*"))
return errmsg("Unsupported Accept");
/* read the query */
cl = thash_get(req->http.headers, "Content-Length");
if (!cl) return errmsg("Content-Length needed");
len = atoi(cl);
if (len < 0)
return errmsg("Unsupported Content-Length");
return readjson_len(req, len);
}