-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.c
More file actions
403 lines (375 loc) · 10.5 KB
/
request.c
File metadata and controls
403 lines (375 loc) · 10.5 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
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
/* This engine processes requests for coreference graphs populated
* by Twine's "spindle" post-processing module.
*
* Author: Mo McRoberts <mo.mcroberts@bbc.co.uk>
*
* Copyright (c) 2014-2015 BBC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "p_patchwork.h"
static int patchwork_request_is_query_(QUILTREQ *req);
static const char *patchwork_request_is_lookup_(QUILTREQ *req);
static int patchwork_request_is_partition_(QUILTREQ *req, char **qclass);
static int patchwork_request_is_item_(QUILTREQ *req);
static struct patchwork_dynamic_endpoint *patchwork_request_is_dynamic_(QUILTREQ *req);
static int patchwork_request_audiences_(QUILTREQ *req, struct patchwork_dynamic_endpoint *endpoint);
static struct patchwork_dynamic_endpoint patchwork_endpoints[] =
{
{ "/audiences", 10, patchwork_request_audiences_ },
{ NULL, 0, NULL }
};
int
patchwork_process(QUILTREQ *request)
{
char *qclass;
int r;
const char *uri;
struct patchwork_dynamic_endpoint *endpoint;
/* Process a request and determine how it should be handled.
*
* In order of preference:
*
* - Requests for partitions (look-up against our static list)
* - Requests for items (pattern match)
* - Requests for endpoints that are generated on the fly
* (such as /audiences)
* - URI lookup queries
* - Queries at the index, if no path parameters
*/
qclass = NULL;
if(patchwork_request_is_partition_(request, &qclass))
{
r = patchwork_index(request, qclass);
free(qclass);
return r;
}
if(patchwork_request_is_item_(request))
{
return patchwork_item(request);
}
if((endpoint = patchwork_request_is_dynamic_(request)))
{
return endpoint->process(request, endpoint);
}
uri = patchwork_request_is_lookup_(request);
if(uri)
{
return patchwork_lookup(request, uri);
}
if(patchwork_request_is_query_(request))
{
return patchwork_index(request, NULL);
}
if(request->home)
{
return patchwork_home(request);
}
return 404;
}
/* Add information to the model about relationship between the concrete and
* abstract documents
*/
int
patchwork_add_concrete(QUILTREQ *request)
{
const char *s;
char *subject, *abstract, *concrete, *typebuf;
librdf_statement *st;
librdf_node *graph;
int explicit;
graph = quilt_request_graph(request);
explicit = (request->ext != NULL);
abstract = quilt_canon_str(request->canonical, (explicit ? QCO_ABSTRACT : QCO_REQUEST));
concrete = quilt_canon_str(request->canonical, (explicit ? QCO_REQUEST : QCO_CONCRETE));
subject = quilt_canon_str(request->canonical, QCO_NOEXT|QCO_FRAGMENT);
/* abstract foaf:primaryTopic subject */
if(strchr(subject, '#'))
{
st = quilt_st_create_uri(abstract, NS_FOAF "primaryTopic", subject);
librdf_model_context_add_statement(request->model, graph, st);
librdf_free_statement(st);
}
/* abstract dct:hasFormat concrete */
st = quilt_st_create_uri(abstract, NS_DCTERMS "hasFormat", concrete);
librdf_model_context_add_statement(request->model, graph, st);
librdf_free_statement(st);
/* concrete rdf:type ... */
st = quilt_st_create_uri(concrete, NS_RDF "type", NS_DCMITYPE "Text");
librdf_model_context_add_statement(request->model, graph, st);
librdf_free_statement(st);
s = NULL;
if(!strcmp(request->type, "text/turtle"))
{
s = NS_FORMATS "Turtle";
}
else if(!strcmp(request->type, "application/rdf+xml"))
{
s = NS_FORMATS "RDF_XML";
}
else if(!strcmp(request->type, "text/rdf+n3"))
{
s = NS_FORMATS "N3";
}
if(s)
{
st = quilt_st_create_uri(concrete, NS_RDF "type", s);
librdf_model_context_add_statement(request->model, graph, st);
librdf_free_statement(st);
}
typebuf = (char *) malloc(strlen(NS_MIME) + strlen(request->type) + 1);
if(typebuf)
{
strcpy(typebuf, NS_MIME);
strcat(typebuf, request->type);
st = quilt_st_create_uri(concrete, NS_DCTERMS "format", typebuf);
librdf_model_context_add_statement(request->model, graph, st);
librdf_free_statement(st);
free(typebuf);
}
free(abstract);
free(concrete);
return 200;
}
/* Is this a request constituting a query for something against the index?
*
* Note that this only applies at the root - if we already know it's a
* non-home index then the query will be performed automatically
*/
static int
patchwork_request_is_query_(QUILTREQ *request)
{
const char *t;
if(!request->home)
{
return 0;
}
if(((t = quilt_request_getparam(request, "q")) && t[0]) ||
((t = quilt_request_getparam(request, "media")) && t[0]) ||
((t = quilt_request_getparam(request, "for")) && t[0]) ||
((t = quilt_request_getparam(request, "type")) && t[0]) ||
((t = quilt_request_getparam(request, "duration-min")) && t[0]) ||
((t = quilt_request_getparam(request, "duration-max")) && t[0]) ||
((t = quilt_request_getparam(request, "about")) && t[0]))
{
request->index = 1;
request->home = 0;
return 1;
}
return 0;
}
/* Is this a request for a (potential) item? */
static int
patchwork_request_is_item_(QUILTREQ *request)
{
size_t l;
const char *t;
for(t = request->path; *t == '/'; t++);
if(!*t)
{
return 0;
}
for(l = 0; isalnum(*t); t++)
{
l++;
}
if(*t && *t != '/')
{
return 0;
}
if(l != 32)
{
return 0;
}
return 1;
}
/* Is this a request for a class partition? If so, return the URI string */
static int
patchwork_request_is_partition_(QUILTREQ *request, char **qclass)
{
const char *t;
size_t c;
*qclass = NULL;
/* First check to determine whether there's a match against the list */
for(c = 0; patchwork->indices && patchwork->indices[c].uri; c++)
{
if(!strcmp(request->path, patchwork->indices[c].uri))
{
if(patchwork->indices[c].qclass)
{
*qclass = (char *) calloc(1, 32 + strlen(patchwork->indices[c].qclass));
if(patchwork->db)
{
strcpy(*qclass, patchwork->indices[c].qclass);
}
else
{
sprintf(*qclass, "FILTER ( ?class = <%s> )", patchwork->indices[c].qclass);
}
}
request->indextitle = patchwork->indices[c].title;
request->index = 1;
request->home = 0;
quilt_canon_add_path(request->canonical, patchwork->indices[c].uri);
return 1;
}
}
/* Check for an explicit ?class=... parameter at the root */
t = quilt_request_getparam(request, "class");
if(t && request->home)
{
quilt_canon_set_param(request->canonical, "class", t);
*qclass = (char *) calloc(1, 32 + strlen(t));
if(patchwork->db)
{
strcpy(*qclass, t);
}
else
{
sprintf(*qclass, "FILTER ( ?class = <%s> )", t);
}
if(!request->indextitle)
{
request->indextitle = t;
}
request->index = 1;
request->home = 0;
return 1;
}
return 0;
}
static const char *
patchwork_request_is_lookup_(QUILTREQ *request)
{
const char *t;
if(!request->home)
{
return NULL;
}
t = quilt_request_getparam(request, "uri");
if(t && t[0])
{
return t;
}
return NULL;
}
static struct patchwork_dynamic_endpoint *
patchwork_request_is_dynamic_(QUILTREQ *req)
{
size_t c;
for(c = 0; patchwork_endpoints[c].path; c++)
{
if(!strncmp(req->path, patchwork_endpoints[c].path, patchwork_endpoints[c].pathlen))
{
if(!req->path[patchwork_endpoints[c].pathlen] ||
req->path[patchwork_endpoints[c].pathlen] == '/')
{
return &(patchwork_endpoints[c]);
}
}
}
return NULL;
}
static int
patchwork_request_audiences_(QUILTREQ *req, struct patchwork_dynamic_endpoint *endpoint)
{
librdf_statement *st;
QUILTCANON *entry, *dest;
char *entrystr, *deststr, *self;
int r;
struct query_struct query;
librdf_node *graph;
graph = quilt_request_graph(req);
patchwork_query_init(&query);
req->index = 1;
req->home = 0;
/* There are no subsidiary resources, only accept the actual endpoint as
* a request-URI
*/
quilt_canon_add_path(req->canonical, req->path);
if(strcmp(req->path, endpoint->path))
{
return 404;
}
if(req->offset)
{
quilt_canon_set_param_int(req->canonical, "offset", req->offset);
}
if(req->limit != req->deflimit)
{
quilt_canon_set_param_int(req->canonical, "limit", req->limit);
}
req->indextitle = "Audiences";
self = quilt_canon_str(req->canonical, (req->ext ? QCO_ABSTRACT : QCO_REQUEST));
/* st = quilt_st_create_literal(self, NS_RDFS "label", "Audiences", "en-gb");
librdf_model_add_statement(req->model, st);
librdf_free_statement(st);
st = quilt_st_create_uri(self, NS_RDF "type", NS_VOID "Dataset");
librdf_model_add_statement(req->model, st);
librdf_free_statement(st); */
if(!req->offset)
{
/* Generate a magic entry for 'any' */
entry = quilt_canon_create(req->canonical);
quilt_canon_reset_path(entry);
quilt_canon_reset_params(entry);
quilt_canon_add_path(entry, endpoint->path);
quilt_canon_set_fragment(entry, "all");
entrystr = quilt_canon_str(entry, QCO_SUBJECT);
dest = quilt_canon_create(req->canonical);
quilt_canon_reset_path(dest);
quilt_canon_reset_params(dest);
quilt_canon_add_path(dest, "everything");
quilt_canon_set_fragment(dest, NULL);
deststr = quilt_canon_str(dest, QCO_FRAGMENT);
st = quilt_st_create_uri(entrystr, NS_RDF "type", NS_ODRL "Group");
librdf_model_context_add_statement(req->model, graph, st);
librdf_free_statement(st);
st = quilt_st_create_literal(entrystr, NS_RDFS "label", "Everyone", "en-gb");
librdf_model_context_add_statement(req->model, graph, st);
librdf_free_statement(st);
st = quilt_st_create_literal(entrystr, NS_RDFS "comment", "Resources which are generally-accessible to the public", "en-gb");
librdf_model_context_add_statement(req->model, graph, st);
librdf_free_statement(st);
st = quilt_st_create_uri(entrystr, NS_RDFS "seeAlso", deststr);
librdf_model_context_add_statement(req->model, graph, st);
librdf_free_statement(st);
st = quilt_st_create_uri(self, NS_RDFS "seeAlso", entrystr);
librdf_model_context_add_statement(req->model, graph, st);
librdf_free_statement(st);
free(entrystr);
free(deststr);
quilt_canon_destroy(entry);
quilt_canon_destroy(dest);
}
if(patchwork->db)
{
if((r = patchwork_audiences_db(req, &query)) != 200)
{
return r;
}
}
if((r = patchwork_add_concrete(req)) != 200)
{
return r;
}
if((r = patchwork_query_meta(req, &query)) != 200)
{
return r;
}
free(self);
return 200;
}