-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.c
290 lines (276 loc) · 7.08 KB
/
file.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
/* Support for Anansi local disk caches
*
* Author: Mo McRoberts <[email protected]>
*
* Copyright (c) 2014-2016 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_anansi-plugin.h"
int
anansi_file_init(struct anansi_context_struct *context, URI_INFO *info)
{
char *p;
if(!info->path || !info->path[0])
{
twine_logf(LOG_CRIT, PLUGIN_NAME ": configured file: cache URI does not include a path\n");
return -1;
}
if(info->host && info->host[0])
{
twine_logf(LOG_WARNING, PLUGIN_NAME ": configured file: cache URI includes a hostname which will be ignored\n");
}
if(info->query && info->query[0])
{
twine_logf(LOG_WARNING, PLUGIN_NAME ": configured file: cache URI includes a query-string which will be ignored\n");
}
if(info->fragment && info->fragment[0])
{
twine_logf(LOG_WARNING, PLUGIN_NAME ": configured file: cache URI includes a fragment which will be ignored\n");
}
if(info->auth && info->auth[0])
{
twine_logf(LOG_WARNING, PLUGIN_NAME ": configured file: cache URI includes authentication details which will be ignored\n");
}
context->cachepath = (char *) calloc(1, strlen(info->path) + 3);
if(!context->cachepath)
{
twine_logf(LOG_CRIT, PLUGIN_NAME ": failed to allocate memory for cache path string '%s'\n", info->path);
return -1;
}
p = context->cachepath;
/* Ensure leading and trailing slashes */
if(info->path[0] != '/')
{
*p = '/';
p++;
}
strcpy(p, info->path);
p = strchr(p, 0);
if(p > context->cachepath)
{
p--;
if(*p != '/')
{
p++;
*p = '/';
p++;
*p = 0;
}
}
twine_logf(LOG_NOTICE, PLUGIN_NAME ": file cache path is '%s'\n", context->cachepath);
return 0;
}
/* Fetch an object from the Anansi cache when configured to use a disk cache */
TWINEGRAPH *
anansi_file_fetch(struct anansi_context_struct *context, const char *uristr, URI_INFO *info)
{
json_t *dict, *j, *headers;
const char *location, *type;
int r;
TWINEGRAPH *graph;
if(!info->path)
{
twine_logf(LOG_ERR, PLUGIN_NAME ": <%s> is not a valid Anansi cache resource URL\n", uristr);
return NULL;
}
dict = NULL;
r = anansi_file_ingest_info(context, info->path, &dict);
if(r || !dict || json_typeof(dict) != JSON_OBJECT)
{
twine_logf(LOG_ERR, PLUGIN_NAME ": failed to fetch cache information for <%s>\n", uristr);
if(dict)
{
json_decref(dict);
}
return NULL;
}
location = NULL;
type = NULL;
j = json_object_get(dict, "content_location");
if(j)
{
location = json_string_value(j);
}
if(!location)
{
twine_logf(LOG_ERR, PLUGIN_NAME ": object has no Content-Location\n");
json_decref(dict);
return NULL;
}
j = json_object_get(dict, "type");
if(j)
{
type = json_string_value(j);
}
if(!type)
{
twine_logf(LOG_ERR, PLUGIN_NAME ": object has no Content-Type\n");
json_decref(dict);
return NULL;
}
graph = twine_graph_create(context->twine, location);
if(!graph)
{
twine_logf(LOG_CRIT, PLUGIN_NAME ": failed to create new Twine graph object\n");
json_decref(dict);
return NULL;
}
r = anansi_file_ingest_payload(context, graph, info->path, type, location);
if(r)
{
twine_logf(LOG_ERR, PLUGIN_NAME ": failed to ingest payload for <%s>\n", uristr);
twine_graph_destroy(graph);
json_decref(dict);
return NULL;
}
headers = json_object_get(dict, "headers");
if(headers && json_typeof(headers) == JSON_OBJECT)
{
if(anansi_handler_process_headers(context, graph, headers, location))
{
twine_logf(LOG_ERR, PLUGIN_NAME ": failed to process headers\n");
twine_graph_destroy(graph);
json_decref(dict);
return NULL;
}
}
json_decref(dict);
return graph;
}
char *
anansi_file_path(struct anansi_context_struct *context, const char *path, const char *type)
{
size_t needed;
char *buf;
while(*path == '/')
{
path++;
}
/* base path + "/" + key[0..1] + "/" + key[2..3] + "/" + key[0..n] + "." + type + NUL */
needed = strlen(context->cachepath) + 1 + 2 + 1 + 2 + 1 + strlen(path) + 1 + (type ? strlen(type) : 0) + 1;
buf = (char *) calloc(1, needed);
if(!buf)
{
twine_logf(LOG_CRIT, PLUGIN_NAME ": failed to allocate %lu bytes for cache filename");
return NULL;
}
sprintf(buf, "%s%c%c/%c%c/%s%s%s", context->cachepath, path[0], path[1], path[2], path[3], path, (type && type[0] ? "." : ""), (type ? type : ""));
return buf;
}
char *
anansi_file_get(struct anansi_context_struct *context, const char *resource, const char *type, size_t *buflen)
{
char *path, *p, *buffer;
FILE *f;
size_t bufsize;
ssize_t r;
*buflen = 0;
path = anansi_file_path(context, resource, type);
if(!path)
{
return NULL;
}
buffer = NULL;
bufsize = 0;
*buflen = 0;
f = fopen(path, "rb");
if(!f)
{
twine_logf(LOG_ERR, PLUGIN_NAME ": failed to open %s for reading: %s\n", path, strerror(errno));
free(path);
return NULL;
}
twine_logf(LOG_DEBUG, PLUGIN_NAME ": reading '%s'\n", path);
while(!feof(f))
{
if(bufsize - *buflen < 1024)
{
p = (char *) realloc(buffer, bufsize + 1024);
if(!p)
{
twine_logf(LOG_CRIT, PLUGIN_NAME ": failed to reallocate buffer from %u bytes to %u bytes\n", (unsigned) bufsize, (unsigned) bufsize + 1024);
*buflen = 0;
fclose(f);
free(buffer);
free(path);
return NULL;
}
buffer = p;
bufsize += 1024;
}
r = fread(&(buffer[*buflen]), 1, 1023, f);
if(r < 0)
{
twine_logf(LOG_CRIT, PLUGIN_NAME ": error reading from '%s': %s\n", path, strerror(errno));
*buflen = 0;
fclose(f);
free(buffer);
free(path);
return NULL;
}
*buflen += r;
buffer[*buflen] = 0;
}
fclose(f);
free(path);
return buffer;
}
int
anansi_file_ingest_info(struct anansi_context_struct *context, const char *resource, json_t **dict)
{
char *buf;
size_t buflen;
int r;
json_error_t err;
r = 0;
buflen = 0;
buf = anansi_file_get(context, resource, CACHE_INFO_SUFFIX, &buflen);
if(!buf)
{
return -1;
}
*dict = json_loads(buf, 0, &err);
if(!*dict)
{
twine_logf(LOG_ERR, PLUGIN_NAME ": failed to parse '%s" CACHE_INFO_SUFFIX "': %s at (%d, %d)\n", resource, err.text, err.line, err.column);
r = -1;
}
else if(json_typeof(*dict) != JSON_OBJECT)
{
twine_logf(LOG_ERR, PLUGIN_NAME ": '%s" CACHE_INFO_SUFFIX "': not a JSON object\n", resource);
json_decref(*dict);
r = -1;
}
free(buf);
return r;
}
int
anansi_file_ingest_payload(struct anansi_context_struct *context, TWINEGRAPH *graph, const char *resource, const char *type, const char *location)
{
char *buf;
size_t buflen;
int r;
buflen = 0;
buf = anansi_file_get(context, resource, CACHE_PAYLOAD_SUFFIX, &buflen);
if(!buf)
{
return -1;
}
r = anansi_handler_process_payload(context, graph, buf, buflen, type, location);
free(buf);
return r;
}