teptris 0.3.21.0 → 0.3.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/ext/teptris_ext/engine/README.md +1 -1
- data/ext/teptris_ext/engine/src/include/teptris/schema.h +7 -0
- data/ext/teptris_ext/engine/src/include/teptris/version.h +2 -2
- data/ext/teptris_ext/engine/src/teptris/schema/formats.c +34 -0
- data/ext/teptris_ext/engine/src/teptris/schema/json_read.c +5 -2
- data/ext/teptris_ext/engine/src/teptris/schema/metaschemas.c +243 -0
- data/ext/teptris_ext/engine/src/teptris/schema/metaschemas.h +15 -0
- data/ext/teptris_ext/engine/src/teptris/schema/refs.c +243 -0
- data/ext/teptris_ext/engine/src/teptris/schema/refs.h +24 -0
- data/ext/teptris_ext/engine/src/teptris/schema/schema.c +903 -55
- data/ext/teptris_ext/engine/src/teptris/schema/schema_internal.h +92 -3
- data/ext/teptris_ext/engine/src/teptris/schema/validate.c +463 -48
- data/lib/teptris/version.rb +1 -1
- metadata +5 -1
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
/* The resource model: JSON Schema resources (a base URI + the jv
|
|
2
|
+
* subtree it names), relative-reference joining, and file-backed
|
|
3
|
+
* resource loading. A parser library never touches the network:
|
|
4
|
+
* http(s) targets are rejected by the caller; file-backed resources
|
|
5
|
+
* are size-capped and memoized per canonical path. */
|
|
6
|
+
#include <limits.h>
|
|
7
|
+
#include <stdio.h>
|
|
8
|
+
#include <stdlib.h>
|
|
9
|
+
#include <string.h>
|
|
10
|
+
|
|
11
|
+
#include "json_read.h"
|
|
12
|
+
#include "refs.h"
|
|
13
|
+
#include "schema_internal.h"
|
|
14
|
+
#include "teptris/memory/arena.h"
|
|
15
|
+
|
|
16
|
+
/* load (or memoize) the file-backed resource named by a base URI */
|
|
17
|
+
jv *sch_load_resource(sctx *c, const char *abs_uri) {
|
|
18
|
+
char path[TEPTRIS_URI_MAX];
|
|
19
|
+
if (!teptris_file_uri_to_path(abs_uri, path, sizeof path)) return NULL;
|
|
20
|
+
for (size_t i = 0; i < c->n_resources; i++) {
|
|
21
|
+
if (strcmp(c->resources[i].abs, abs_uri) == 0) {
|
|
22
|
+
return c->resources[i].root;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
if (c->n_resources >= 64) return NULL; /* the resource fan-out cap */
|
|
26
|
+
size_t len = 0;
|
|
27
|
+
char *text = malloc(TEPTRIS_RESOURCE_MAX_BYTES);
|
|
28
|
+
if (text == NULL) return NULL;
|
|
29
|
+
FILE *f = fopen(path, "rb");
|
|
30
|
+
if (f == NULL) {
|
|
31
|
+
free(text);
|
|
32
|
+
return NULL;
|
|
33
|
+
}
|
|
34
|
+
len = fread(text, 1, TEPTRIS_RESOURCE_MAX_BYTES, f);
|
|
35
|
+
fclose(f);
|
|
36
|
+
if (len == 0) {
|
|
37
|
+
free(text);
|
|
38
|
+
return NULL;
|
|
39
|
+
}
|
|
40
|
+
char jerr[160] = {0};
|
|
41
|
+
jv *root = json_read(c->a, text, len, jerr, sizeof jerr);
|
|
42
|
+
free(text);
|
|
43
|
+
if (root == NULL) return NULL;
|
|
44
|
+
|
|
45
|
+
if (c->n_resources == c->cap_resources) {
|
|
46
|
+
c->cap_resources = c->cap_resources == 0 ? 8 : c->cap_resources * 2;
|
|
47
|
+
void *nr = teptris_arena_alloc(c->a, c->cap_resources * sizeof(*c->resources));
|
|
48
|
+
if (c->n_resources > 0) {
|
|
49
|
+
memcpy(nr, c->resources, c->n_resources * sizeof(*c->resources));
|
|
50
|
+
}
|
|
51
|
+
c->resources = nr;
|
|
52
|
+
}
|
|
53
|
+
teptris_resource *res = &c->resources[c->n_resources++];
|
|
54
|
+
res->abs = teptris_arena_alloc(c->a, strlen(abs_uri) + 1);
|
|
55
|
+
strcpy((char *)res->abs, abs_uri);
|
|
56
|
+
res->root = root;
|
|
57
|
+
res->node = NULL;
|
|
58
|
+
res->recursive_anchor = false;
|
|
59
|
+
return root;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/* ---------------------------------------------------- uri joining -- */
|
|
63
|
+
|
|
64
|
+
/* the directory part of a base URI path ("file:///a/b/c.json" ->
|
|
65
|
+
* "file:///a/b/"); the base itself when there is no slash */
|
|
66
|
+
static void uri_dir(const char *base, char *out, size_t cap) {
|
|
67
|
+
if (base == NULL) {
|
|
68
|
+
out[0] = '\0';
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
const char *slash = strrchr(base, '/');
|
|
72
|
+
if (slash == NULL) {
|
|
73
|
+
snprintf(out, cap, "%s", base);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
size_t n = (size_t)(slash - base) + 1;
|
|
77
|
+
if (n >= cap) n = cap - 1;
|
|
78
|
+
memcpy(out, base, n);
|
|
79
|
+
out[n] = '\0';
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
static void remove_dot_segments(char *path) {
|
|
83
|
+
/* RFC 3986 5.2.4 over the whole string; paths here are simple
|
|
84
|
+
* enough that the loop form suffices */
|
|
85
|
+
char *w = path;
|
|
86
|
+
char *r = path;
|
|
87
|
+
while (*r != '\0') {
|
|
88
|
+
if (r[0] == '.' && r[1] == '/' ) {
|
|
89
|
+
r += 2;
|
|
90
|
+
} else if (r[0] == '.' && r[1] == '.' && r[2] == '/') {
|
|
91
|
+
r += 3;
|
|
92
|
+
/* pop the last segment of w */
|
|
93
|
+
char *slash = NULL;
|
|
94
|
+
for (char *p = w; *p != '\0'; p++) {
|
|
95
|
+
if (*p == '/') slash = p;
|
|
96
|
+
}
|
|
97
|
+
if (slash != NULL && slash > path) {
|
|
98
|
+
*slash = '\0';
|
|
99
|
+
} else if (slash == path) {
|
|
100
|
+
slash[1] = '\0';
|
|
101
|
+
} else {
|
|
102
|
+
w[0] = '\0';
|
|
103
|
+
}
|
|
104
|
+
} else {
|
|
105
|
+
while (*r != '\0' && *r != '/') *w++ = *r++;
|
|
106
|
+
if (*r == '/') *w++ = *r++;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
*w = '\0';
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/* join a relative reference against a base; scheme:// targets and
|
|
113
|
+
* absolute paths stand alone. NULL when the reference cannot be
|
|
114
|
+
* resolved (caller decides whether that is a rejection) */
|
|
115
|
+
bool teptris_uri_join(const char *base, const char *ref, char *out,
|
|
116
|
+
size_t cap) {
|
|
117
|
+
out[0] = '\0';
|
|
118
|
+
if (ref == NULL) return false;
|
|
119
|
+
if (strncmp(ref, "http://", 7) == 0 || strncmp(ref, "https://", 8) == 0) {
|
|
120
|
+
/* network targets never resolve here */
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
{
|
|
124
|
+
/* any other scheme ("urn:uuid:...", "mailto:...") is an
|
|
125
|
+
* absolute URI: it stands alone and may name an in-memory
|
|
126
|
+
* resource (see the resource table) */
|
|
127
|
+
const char *p = ref;
|
|
128
|
+
if ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z')) {
|
|
129
|
+
while ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') ||
|
|
130
|
+
(*p >= '0' && *p <= '9') || *p == '+' || *p == '-' ||
|
|
131
|
+
*p == '.') {
|
|
132
|
+
p++;
|
|
133
|
+
}
|
|
134
|
+
if (*p == ':' && p != ref) {
|
|
135
|
+
snprintf(out, cap, "%s", ref);
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
if (strncmp(ref, "file://", 7) == 0) {
|
|
141
|
+
snprintf(out, cap, "%s", ref);
|
|
142
|
+
return true;
|
|
143
|
+
}
|
|
144
|
+
if (ref[0] == '/') {
|
|
145
|
+
/* a network-path reference: no base directory applies, but an
|
|
146
|
+
* http(s) base still lends its scheme and authority */
|
|
147
|
+
if (strncmp(base, "http://", 7) == 0 ||
|
|
148
|
+
strncmp(base, "https://", 8) == 0) {
|
|
149
|
+
const char *auth = strstr(base, "://") + 3;
|
|
150
|
+
const char *end = strchr(auth, '/');
|
|
151
|
+
size_t n = end != NULL ? (size_t)(end - base) : strlen(base);
|
|
152
|
+
if (n >= cap || strlen(ref) + n + 1 >= cap) return false;
|
|
153
|
+
memcpy(out, base, n);
|
|
154
|
+
strcpy(out + n, ref);
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
/* absolute path: no base directory applies */
|
|
158
|
+
char tmp[TEPTRIS_URI_MAX];
|
|
159
|
+
snprintf(tmp, sizeof tmp, "%s", ref);
|
|
160
|
+
remove_dot_segments(tmp);
|
|
161
|
+
snprintf(out, cap, "file://%s", tmp);
|
|
162
|
+
return true;
|
|
163
|
+
}
|
|
164
|
+
if (base == NULL || base[0] == '\0') return false;
|
|
165
|
+
if (strncmp(base, "http://", 7) == 0 || strncmp(base, "https://", 8) == 0) {
|
|
166
|
+
/* merge in URI space (RFC 3986 5.3): suite documents name
|
|
167
|
+
* in-memory resources with http-space $ids; resolution merges
|
|
168
|
+
* against that space and only ever hits the resource table —
|
|
169
|
+
* nothing here is ever fetched from the network */
|
|
170
|
+
const char *auth = strstr(base, "://");
|
|
171
|
+
if (auth == NULL) return false;
|
|
172
|
+
/* merge uses the base's directory: up to the LAST slash */
|
|
173
|
+
const char *last_slash = strrchr(auth + 3, '/');
|
|
174
|
+
if (last_slash == NULL) return false;
|
|
175
|
+
size_t n = (size_t)(last_slash - base) + 1;
|
|
176
|
+
if (n >= cap) return false;
|
|
177
|
+
memcpy(out, base, n);
|
|
178
|
+
out[n] = '\0';
|
|
179
|
+
char path_part[TEPTRIS_URI_MAX];
|
|
180
|
+
const char *hash = strchr(ref, '#');
|
|
181
|
+
size_t plen = hash != NULL ? (size_t)(hash - ref) : strlen(ref);
|
|
182
|
+
if (plen >= sizeof path_part) return false;
|
|
183
|
+
memcpy(path_part, ref, plen);
|
|
184
|
+
path_part[plen] = '\0';
|
|
185
|
+
if (strlen(out) + plen + 1 > cap) return false;
|
|
186
|
+
strcat(out, path_part);
|
|
187
|
+
remove_dot_segments(out + n);
|
|
188
|
+
return true;
|
|
189
|
+
}
|
|
190
|
+
if (strncmp(base, "file://", 7) != 0) {
|
|
191
|
+
/* a non-file base (an http base that was rewritten away, a
|
|
192
|
+
* bare id) cannot host relative references without fs backing
|
|
193
|
+
* unless the base itself is a plain path */
|
|
194
|
+
snprintf(out, cap, "%s", ref);
|
|
195
|
+
return true; /* resolved against the CWD by the caller */
|
|
196
|
+
}
|
|
197
|
+
char dir[TEPTRIS_URI_MAX];
|
|
198
|
+
uri_dir(base, dir, sizeof dir);
|
|
199
|
+
size_t n = strlen(dir);
|
|
200
|
+
if (n >= cap) return false;
|
|
201
|
+
memcpy(out, dir, n + 1);
|
|
202
|
+
/* the reference may carry a fragment; split it now */
|
|
203
|
+
char path_part[TEPTRIS_URI_MAX];
|
|
204
|
+
const char *hash = strchr(ref, '#');
|
|
205
|
+
size_t plen = hash != NULL ? (size_t)(hash - ref) : strlen(ref);
|
|
206
|
+
if (plen >= sizeof path_part) return false;
|
|
207
|
+
memcpy(path_part, ref, plen);
|
|
208
|
+
path_part[plen] = '\0';
|
|
209
|
+
if (strlen(out) + strlen(path_part) + 1 > cap) return false;
|
|
210
|
+
strcat(out, path_part);
|
|
211
|
+
/* normalize the path part only (after file://) */
|
|
212
|
+
char *path_start = out + 7;
|
|
213
|
+
if (strncmp(out, "file://", 7) == 0) {
|
|
214
|
+
remove_dot_segments(path_start);
|
|
215
|
+
}
|
|
216
|
+
return true;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/* split "scheme://path#fragment" into the document part and the
|
|
220
|
+
* fragment part (fragment may be NULL/empty) */
|
|
221
|
+
void teptris_uri_split(const char *uri, char *doc, size_t doc_cap,
|
|
222
|
+
char *frag, size_t frag_cap) {
|
|
223
|
+
doc[0] = frag[0] = '\0';
|
|
224
|
+
const char *hash = strchr(uri, '#');
|
|
225
|
+
size_t dlen = hash != NULL ? (size_t)(hash - uri) : strlen(uri);
|
|
226
|
+
if (dlen >= doc_cap) dlen = doc_cap - 1;
|
|
227
|
+
memcpy(doc, uri, dlen);
|
|
228
|
+
doc[dlen] = '\0';
|
|
229
|
+
if (hash != NULL && hash[1] != '\0') {
|
|
230
|
+
snprintf(frag, frag_cap, "%s", hash + 1);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/* -------------------------------------------------- file resources -- */
|
|
235
|
+
|
|
236
|
+
bool teptris_file_uri_to_path(const char *uri, char *path, size_t cap) {
|
|
237
|
+
if (strncmp(uri, "file://", 7) == 0) {
|
|
238
|
+
snprintf(path, cap, "%s", uri + 7);
|
|
239
|
+
return true;
|
|
240
|
+
}
|
|
241
|
+
snprintf(path, cap, "%s", uri);
|
|
242
|
+
return true;
|
|
243
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/* The resource model: base URIs, relative-reference joining, and
|
|
2
|
+
* file-backed resource loading. Internal to the schema subsystem. */
|
|
3
|
+
#ifndef TEPTRIS_SCHEMA_REFS_H
|
|
4
|
+
#define TEPTRIS_SCHEMA_REFS_H
|
|
5
|
+
|
|
6
|
+
#include <stdbool.h>
|
|
7
|
+
#include <stddef.h>
|
|
8
|
+
|
|
9
|
+
#define TEPTRIS_URI_MAX 1024
|
|
10
|
+
#define TEPTRIS_RESOURCE_MAX_BYTES (1u << 20)
|
|
11
|
+
|
|
12
|
+
/* join a relative reference against a base URI; the caller owns the
|
|
13
|
+
* rejection policy for http(s) and unbacked bases */
|
|
14
|
+
bool teptris_uri_join(const char *base, const char *ref, char *out,
|
|
15
|
+
size_t cap);
|
|
16
|
+
|
|
17
|
+
/* split "doc#fragment" (fragment may come back empty) */
|
|
18
|
+
void teptris_uri_split(const char *uri, char *doc, size_t doc_cap,
|
|
19
|
+
char *frag, size_t frag_cap);
|
|
20
|
+
|
|
21
|
+
/* file:// URIs to plain paths (plain paths pass through) */
|
|
22
|
+
bool teptris_file_uri_to_path(const char *uri, char *path, size_t cap);
|
|
23
|
+
|
|
24
|
+
#endif
|