aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/gns/plugin_rest_gns.c1
-rw-r--r--src/identity-provider/plugin_rest_identity_provider.c1
-rw-r--r--src/identity/plugin_rest_identity.c1
-rw-r--r--src/include/gnunet_jsonapi_lib.h119
-rw-r--r--src/jsonapi/Makefile.am16
-rw-r--r--src/jsonapi/jsonapi_document.c43
-rw-r--r--src/jsonapi/jsonapi_error.c36
-rw-r--r--src/jsonapi/jsonapi_resource.c2
-rw-r--r--src/jsonapi/test_jsonapi.c44
-rw-r--r--src/namestore/plugin_rest_namestore.c1
10 files changed, 187 insertions, 77 deletions
diff --git a/src/gns/plugin_rest_gns.c b/src/gns/plugin_rest_gns.c
index b2034c8ce..ef49b82a9 100644
--- a/src/gns/plugin_rest_gns.c
+++ b/src/gns/plugin_rest_gns.c
@@ -33,6 +33,7 @@
33#include <gnunet_gns_service.h> 33#include <gnunet_gns_service.h>
34#include <gnunet_rest_lib.h> 34#include <gnunet_rest_lib.h>
35#include <gnunet_jsonapi_lib.h> 35#include <gnunet_jsonapi_lib.h>
36#include <gnunet_jsonapi_util.h>
36#include <jansson.h> 37#include <jansson.h>
37 38
38#define GNUNET_REST_API_NS_GNS "/gns" 39#define GNUNET_REST_API_NS_GNS "/gns"
diff --git a/src/identity-provider/plugin_rest_identity_provider.c b/src/identity-provider/plugin_rest_identity_provider.c
index b039255b5..89c83639a 100644
--- a/src/identity-provider/plugin_rest_identity_provider.c
+++ b/src/identity-provider/plugin_rest_identity_provider.c
@@ -32,6 +32,7 @@
32#include "gnunet_namestore_service.h" 32#include "gnunet_namestore_service.h"
33#include "gnunet_rest_lib.h" 33#include "gnunet_rest_lib.h"
34#include "gnunet_jsonapi_lib.h" 34#include "gnunet_jsonapi_lib.h"
35#include "gnunet_jsonapi_util.h"
35#include "microhttpd.h" 36#include "microhttpd.h"
36#include <jansson.h> 37#include <jansson.h>
37#include <inttypes.h> 38#include <inttypes.h>
diff --git a/src/identity/plugin_rest_identity.c b/src/identity/plugin_rest_identity.c
index 50d13713a..f14c80f42 100644
--- a/src/identity/plugin_rest_identity.c
+++ b/src/identity/plugin_rest_identity.c
@@ -29,6 +29,7 @@
29#include "gnunet_identity_service.h" 29#include "gnunet_identity_service.h"
30#include "gnunet_rest_lib.h" 30#include "gnunet_rest_lib.h"
31#include "gnunet_jsonapi_lib.h" 31#include "gnunet_jsonapi_lib.h"
32#include "gnunet_jsonapi_util.h"
32#include "microhttpd.h" 33#include "microhttpd.h"
33#include <jansson.h> 34#include <jansson.h>
34#include "gnunet_signatures.h" 35#include "gnunet_signatures.h"
diff --git a/src/include/gnunet_jsonapi_lib.h b/src/include/gnunet_jsonapi_lib.h
index 5535cb2aa..ce41c33f1 100644
--- a/src/include/gnunet_jsonapi_lib.h
+++ b/src/include/gnunet_jsonapi_lib.h
@@ -22,7 +22,6 @@
22#define GNUNET_JSONAPI_LIB_H 22#define GNUNET_JSONAPI_LIB_H
23 23
24#include "gnunet_util_lib.h" 24#include "gnunet_util_lib.h"
25#include "gnunet_rest_lib.h"
26#include "gnunet_json_lib.h" 25#include "gnunet_json_lib.h"
27 26
28#define GNUNET_JSONAPI_KEY_DATA "data" 27#define GNUNET_JSONAPI_KEY_DATA "data"
@@ -101,6 +100,32 @@ void
101GNUNET_JSONAPI_relationship_delete (struct GNUNET_JSONAPI_Relationship *rel); 100GNUNET_JSONAPI_relationship_delete (struct GNUNET_JSONAPI_Relationship *rel);
102 101
103 102
103/****************** jsonapi Error API ********************/
104
105/**
106 * Create a JSON API error
107 *
108 * @param res the JSON error
109 */
110struct GNUNET_JSONAPI_Error*
111GNUNET_JSONAPI_error_new (const char *id,
112 const char *status,
113 const char *code,
114 const char *title,
115 const char *detail,
116 json_t *links,
117 json_t *source,
118 json_t *meta);
119
120/**
121 * Delete a JSON API error
122 *
123 * @param res the JSON error
124 */
125void
126GNUNET_JSONAPI_error_delete (struct GNUNET_JSONAPI_Error *error);
127
128
104/** 129/**
105 * Add a JSON API error to document 130 * Add a JSON API error to document
106 * 131 *
@@ -134,13 +159,7 @@ int
134GNUNET_JSONAPI_json_to_error (json_t *err_json, 159GNUNET_JSONAPI_json_to_error (json_t *err_json,
135 struct GNUNET_JSONAPI_Error **err); 160 struct GNUNET_JSONAPI_Error **err);
136 161
137/** 162/****************** jsonapi Resource API ********************/
138 * Delete a JSON API error
139 *
140 * @param res the JSON error
141 */
142void
143GNUNET_JSONAPI_error_delete (struct GNUNET_JSONAPI_Error *error);
144 163
145/** 164/**
146 * Create a JSON API resource 165 * Create a JSON API resource
@@ -154,6 +173,16 @@ GNUNET_JSONAPI_resource_new (const char *type, const char *id);
154 173
155 174
156/** 175/**
176 * Delete a JSON API resource
177 *
178 * @param res the JSON resource
179 * @param result Pointer where the resource should be stored
180 */
181void
182GNUNET_JSONAPI_resource_delete (struct GNUNET_JSONAPI_Resource *resource);
183
184
185/**
157 * String serialze jsonapi to json 186 * String serialze jsonapi to json
158 * 187 *
159 * @param data the JSON API resource 188 * @param data the JSON API resource
@@ -164,6 +193,7 @@ int
164GNUNET_JSONAPI_resource_to_json (const struct GNUNET_JSONAPI_Resource *res, 193GNUNET_JSONAPI_resource_to_json (const struct GNUNET_JSONAPI_Resource *res,
165 json_t **result); 194 json_t **result);
166 195
196
167/** 197/**
168 * Parse json to resource object 198 * Parse json to resource object
169 * 199 *
@@ -175,14 +205,6 @@ int
175GNUNET_JSONAPI_json_to_resource (json_t *res_json, 205GNUNET_JSONAPI_json_to_resource (json_t *res_json,
176 struct GNUNET_JSONAPI_Resource **res); 206 struct GNUNET_JSONAPI_Resource **res);
177 207
178/**
179 * Delete a JSON API resource
180 *
181 * @param res the JSON resource
182 * @param result Pointer where the resource should be stored
183 */
184void
185GNUNET_JSONAPI_resource_delete (struct GNUNET_JSONAPI_Resource *resource);
186 208
187/** 209/**
188 * Add a JSON API attribute 210 * Add a JSON API attribute
@@ -220,6 +242,15 @@ int
220GNUNET_JSONAPI_resource_check_id (const struct GNUNET_JSONAPI_Resource *resource, 242GNUNET_JSONAPI_resource_check_id (const struct GNUNET_JSONAPI_Resource *resource,
221 const char* id); 243 const char* id);
222 244
245/**
246 * Check a JSON API resource id
247 *
248 * @param res the JSON resource
249 * @return the resource id
250 */
251char*
252GNUNET_JSONAPI_resource_get_id (const struct GNUNET_JSONAPI_Resource *resource);
253
223 254
224/** 255/**
225 * Check a JSON API resource type 256 * Check a JSON API resource type
@@ -232,6 +263,7 @@ int
232GNUNET_JSONAPI_resource_check_type (const struct GNUNET_JSONAPI_Resource *resource, 263GNUNET_JSONAPI_resource_check_type (const struct GNUNET_JSONAPI_Resource *resource,
233 const char* type); 264 const char* type);
234 265
266/****************** jsonapi Document API ********************/
235 267
236/** 268/**
237 * Create a JSON API primary data 269 * Create a JSON API primary data
@@ -267,6 +299,17 @@ void
267GNUNET_JSONAPI_document_delete (struct GNUNET_JSONAPI_Document *resp); 299GNUNET_JSONAPI_document_delete (struct GNUNET_JSONAPI_Document *resp);
268 300
269/** 301/**
302 * String serialze jsonapi primary data
303 *
304 * @param data the JSON API primary data
305 * @param result where to store the result
306 * @return GNUNET_SYSERR on error else GNUNET_OK
307 */
308int
309GNUNET_JSONAPI_document_to_json (const struct GNUNET_JSONAPI_Document *doc,
310 json_t **root_json);
311
312/**
270 * Add a JSON API resource to primary data 313 * Add a JSON API resource to primary data
271 * 314 *
272 * @param data The JSON API data to add to 315 * @param data The JSON API data to add to
@@ -318,49 +361,5 @@ int
318GNUNET_JSONAPI_document_serialize (const struct GNUNET_JSONAPI_Document *resp, 361GNUNET_JSONAPI_document_serialize (const struct GNUNET_JSONAPI_Document *resp,
319 char **result); 362 char **result);
320 363
321/**
322 * Check a JSON API resource id
323 *
324 * @param res the JSON resource
325 * @return the resource id
326 */
327char*
328GNUNET_JSONAPI_resource_get_id (const struct GNUNET_JSONAPI_Resource *resource);
329
330
331/* end of gnunet_jsonapi_lib.h */ 364/* end of gnunet_jsonapi_lib.h */
332
333/**
334 * Check rest request for validity
335 *
336 * @param req handle to the request
337 * @return GNUNET_OK if valid
338 */
339int
340GNUNET_JSONAPI_check_request_acceptable (struct GNUNET_REST_RequestHandle *req);
341
342/**
343 * Check rest request for validity
344 *
345 * @param req handle to the request
346 * @return GNUNET_OK if valid
347 */
348int
349GNUNET_JSONAPI_check_request_supported (struct GNUNET_REST_RequestHandle *req);
350
351
352/**
353 * Handle jsonapi rest request. Checks request headers for jsonapi compliance
354 *
355 * @param req rest request handle
356 * @param handler rest request handlers
357 * @param cls closure
358 * @return GNUNET_OK if successful
359 */
360int
361GNUNET_JSONAPI_handle_request (struct GNUNET_REST_RequestHandle *req,
362 const struct GNUNET_REST_RequestHandler *handlers,
363 struct GNUNET_REST_RequestHandlerError *err,
364 void *cls);
365
366#endif 365#endif
diff --git a/src/jsonapi/Makefile.am b/src/jsonapi/Makefile.am
index 7e881acbd..22ae7dac5 100644
--- a/src/jsonapi/Makefile.am
+++ b/src/jsonapi/Makefile.am
@@ -7,13 +7,24 @@ if USE_COVERAGE
7endif 7endif
8 8
9lib_LTLIBRARIES = \ 9lib_LTLIBRARIES = \
10 libgnunetjsonapi.la 10 libgnunetjsonapi.la \
11 libgnunetjsonapiutils.la
12
13libgnunetjsonapiutils_la_LDFLAGS = \
14 -version-info 0:0:0 \
15 -no-undefined
16libgnunetjsonapiutils_la_SOURCES = \
17 jsonapi.c
18libgnunetjsonapiutils_la_LIBADD = \
19 $(top_builddir)/src/util/libgnunetutil.la \
20 $(top_builddir)/src/jsonapi/libgnunetjsonapi.la \
21 $(top_builddir)/src/rest/libgnunetrest.la \
22 $(XLIB)
11 23
12libgnunetjsonapi_la_LDFLAGS = \ 24libgnunetjsonapi_la_LDFLAGS = \
13 -version-info 0:0:0 \ 25 -version-info 0:0:0 \
14 -no-undefined 26 -no-undefined
15libgnunetjsonapi_la_SOURCES = \ 27libgnunetjsonapi_la_SOURCES = \
16 jsonapi.c \
17 jsonapi_document.c \ 28 jsonapi_document.c \
18 jsonapi_resource.c \ 29 jsonapi_resource.c \
19 jsonapi_error.c \ 30 jsonapi_error.c \
@@ -21,7 +32,6 @@ libgnunetjsonapi_la_SOURCES = \
21libgnunetjsonapi_la_LIBADD = \ 32libgnunetjsonapi_la_LIBADD = \
22 $(top_builddir)/src/util/libgnunetutil.la \ 33 $(top_builddir)/src/util/libgnunetutil.la \
23 $(top_builddir)/src/json/libgnunetjson.la \ 34 $(top_builddir)/src/json/libgnunetjson.la \
24 $(top_builddir)/src/rest/libgnunetrest.la \
25 -ljansson \ 35 -ljansson \
26 $(XLIB) 36 $(XLIB)
27 37
diff --git a/src/jsonapi/jsonapi_document.c b/src/jsonapi/jsonapi_document.c
index 4837ee2be..b99a7a4fe 100644
--- a/src/jsonapi/jsonapi_document.c
+++ b/src/jsonapi/jsonapi_document.c
@@ -286,19 +286,18 @@ GNUNET_JSONAPI_document_resource_remove (struct GNUNET_JSONAPI_Document *resp,
286 * @return GNUNET_SYSERR on error else GNUNET_OK 286 * @return GNUNET_SYSERR on error else GNUNET_OK
287 */ 287 */
288int 288int
289GNUNET_JSONAPI_document_serialize (const struct GNUNET_JSONAPI_Document *doc, 289GNUNET_JSONAPI_document_to_json (const struct GNUNET_JSONAPI_Document *doc,
290 char **result) 290 json_t **root_json)
291{ 291{
292 struct GNUNET_JSONAPI_Resource *res; 292 struct GNUNET_JSONAPI_Resource *res;
293 struct GNUNET_JSONAPI_Error *error; 293 struct GNUNET_JSONAPI_Error *error;
294 json_t *root_json;
295 json_t *res_json; 294 json_t *res_json;
296 json_t *res_json_tmp; 295 json_t *res_json_tmp;
297 296
298 if ((NULL == doc)) 297 if ((NULL == doc))
299 return GNUNET_SYSERR; 298 return GNUNET_SYSERR;
300 299
301 root_json = json_object (); 300 *root_json = json_object ();
302 301
303 //Check for errors first 302 //Check for errors first
304 if (doc->err_count != 0) 303 if (doc->err_count != 0)
@@ -313,7 +312,9 @@ GNUNET_JSONAPI_document_serialize (const struct GNUNET_JSONAPI_Document *doc,
313 &res_json_tmp)); 312 &res_json_tmp));
314 json_array_append (res_json, res_json_tmp); 313 json_array_append (res_json, res_json_tmp);
315 } 314 }
316 json_object_set (root_json, GNUNET_JSONAPI_KEY_ERRORS, res_json); 315 json_object_set_new (*root_json,
316 GNUNET_JSONAPI_KEY_ERRORS,
317 res_json);
317 } else { 318 } else {
318 switch (doc->res_count) 319 switch (doc->res_count)
319 { 320 {
@@ -338,14 +339,34 @@ GNUNET_JSONAPI_document_serialize (const struct GNUNET_JSONAPI_Document *doc,
338 } 339 }
339 break; 340 break;
340 } 341 }
341 json_object_set (root_json, GNUNET_JSONAPI_KEY_DATA, res_json); 342 json_object_set_new (*root_json,
343 GNUNET_JSONAPI_KEY_DATA,
344 res_json);
342 } 345 }
346 json_object_set (*root_json,
347 GNUNET_JSONAPI_KEY_META,
348 doc->meta);
349 return GNUNET_OK;
350}
351
352/**
353 * String serialze jsonapi primary data
354 *
355 * @param data the JSON API primary data
356 * @param result where to store the result
357 * @return GNUNET_SYSERR on error else GNUNET_OK
358 */
359int
360GNUNET_JSONAPI_document_serialize (const struct GNUNET_JSONAPI_Document *doc,
361 char **result)
362{
363 json_t *json_doc;
364 if (GNUNET_OK != GNUNET_JSONAPI_document_to_json (doc,
365 &json_doc))
366 return GNUNET_SYSERR;
343 367
344 //Add meta 368 *result = json_dumps (json_doc, JSON_INDENT(2));
345 json_object_set (root_json, GNUNET_JSONAPI_KEY_META, doc->meta); 369 json_decref (json_doc);
346 *result = json_dumps (root_json, JSON_INDENT(2));
347 json_decref (root_json);
348 json_decref (res_json);
349 return GNUNET_OK; 370 return GNUNET_OK;
350} 371}
351 372
diff --git a/src/jsonapi/jsonapi_error.c b/src/jsonapi/jsonapi_error.c
index d91f0a650..b7fc08d72 100644
--- a/src/jsonapi/jsonapi_error.c
+++ b/src/jsonapi/jsonapi_error.c
@@ -132,6 +132,42 @@ parse_jsonapierror (void *cls,
132} 132}
133 133
134/** 134/**
135 * Create a JSON API error
136 *
137 * @param res the JSON error
138 */
139struct GNUNET_JSONAPI_Error*
140GNUNET_JSONAPI_error_new (const char *id,
141 const char *status,
142 const char *code,
143 const char *title,
144 const char *detail,
145 json_t *links,
146 json_t *source,
147 json_t *meta)
148{
149 struct GNUNET_JSONAPI_Error *error;
150 error = GNUNET_new (struct GNUNET_JSONAPI_Error);
151
152 GNUNET_assert (NULL != id);
153 error->id = GNUNET_strdup (id);
154 GNUNET_assert (NULL != status);
155 error->status = GNUNET_strdup (status);
156 GNUNET_assert (NULL != code);
157 error->code = GNUNET_strdup (code);
158 GNUNET_assert (NULL != title);
159 error->title = GNUNET_strdup (title);
160 GNUNET_assert (NULL != detail);
161 error->detail = GNUNET_strdup (detail);
162 GNUNET_assert (NULL != links);
163 error->links = json_deep_copy (links);
164 GNUNET_assert (NULL != source);
165 error->source = json_deep_copy (source);
166 GNUNET_assert (NULL != meta);
167 error->meta = json_deep_copy (meta);
168 return error;
169}
170/**
135 * Delete a JSON API error 171 * Delete a JSON API error
136 * 172 *
137 * @param res the JSON error 173 * @param res the JSON error
diff --git a/src/jsonapi/jsonapi_resource.c b/src/jsonapi/jsonapi_resource.c
index 09217279a..d1d811482 100644
--- a/src/jsonapi/jsonapi_resource.c
+++ b/src/jsonapi/jsonapi_resource.c
@@ -145,7 +145,7 @@ GNUNET_JSONAPI_resource_add_attr (struct GNUNET_JSONAPI_Resource *resource,
145 return GNUNET_SYSERR; 145 return GNUNET_SYSERR;
146 if (NULL == resource->attr_obj) 146 if (NULL == resource->attr_obj)
147 resource->attr_obj = json_object (); 147 resource->attr_obj = json_object ();
148 json_object_set (resource->attr_obj, key, json); 148 json_object_set_new (resource->attr_obj, key, json);
149 return GNUNET_OK; 149 return GNUNET_OK;
150} 150}
151 151
diff --git a/src/jsonapi/test_jsonapi.c b/src/jsonapi/test_jsonapi.c
index 8b0b13566..b5f4d4cba 100644
--- a/src/jsonapi/test_jsonapi.c
+++ b/src/jsonapi/test_jsonapi.c
@@ -24,11 +24,47 @@
24#include "gnunet_jsonapi_lib.h" 24#include "gnunet_jsonapi_lib.h"
25#include "gnunet_json_lib.h" 25#include "gnunet_json_lib.h"
26 26
27#define TEST_JSONAPI_DOCUMENT "{\"data\":{\"id\":\"1\",\"type\":\"bar\",\"attributes\":{\"foo\":\"bar\"}}}"
28
29static int
30test_document ()
31{
32 struct GNUNET_JSONAPI_Document *obj;
33 struct GNUNET_JSONAPI_Resource *res;
34 json_t *doc_json;
35 json_t *data_js;
36 json_error_t err;
37
38 obj = GNUNET_JSONAPI_document_new ();
39 res = GNUNET_JSONAPI_resource_new ("bar",
40 "1");
41
42 GNUNET_assert (GNUNET_OK ==
43 GNUNET_JSONAPI_resource_add_attr (res,
44 "foo",
45 json_string ("bar")));
46
47 GNUNET_JSONAPI_document_resource_add (obj,
48 res);
49
50 GNUNET_assert (GNUNET_OK ==
51 GNUNET_JSONAPI_document_to_json (obj,
52 &doc_json));
53 data_js = json_loads (TEST_JSONAPI_DOCUMENT,
54 JSON_DECODE_ANY,
55 &err);
56 GNUNET_assert (NULL != data_js);
57 GNUNET_assert (0 != json_equal (data_js, doc_json));
58 GNUNET_JSONAPI_document_delete (obj);
59 json_decref (data_js);
60 json_decref (doc_json);
61 return 0;
62}
63
27static int 64static int
28test_serialize () 65test_serialize ()
29{ 66{
30 struct GNUNET_JSONAPI_Document *obj; 67 struct GNUNET_JSONAPI_Document *obj;
31 char* data = "{\"data\":{\"id\":\"1\",\"type\":\"bar\", \"attributes\":{\"foo\":\"bar\"}}}";
32 char* tmp_data; 68 char* tmp_data;
33 json_t* data_js; 69 json_t* data_js;
34 json_t* tmp_data_js; 70 json_t* tmp_data_js;
@@ -37,7 +73,9 @@ test_serialize ()
37 GNUNET_JSON_spec_jsonapi_document (&obj), 73 GNUNET_JSON_spec_jsonapi_document (&obj),
38 GNUNET_JSON_spec_end() 74 GNUNET_JSON_spec_end()
39 }; 75 };
40 data_js = json_loads (data, JSON_DECODE_ANY, &err); 76 data_js = json_loads (TEST_JSONAPI_DOCUMENT,
77 JSON_DECODE_ANY,
78 &err);
41 GNUNET_assert (NULL != data_js); 79 GNUNET_assert (NULL != data_js);
42 GNUNET_assert (GNUNET_OK == 80 GNUNET_assert (GNUNET_OK ==
43 GNUNET_JSON_parse (data_js, jsonapispec, 81 GNUNET_JSON_parse (data_js, jsonapispec,
@@ -98,6 +136,8 @@ main(int argc,
98 return 1; 136 return 1;
99 if (0 != test_serialize ()) 137 if (0 != test_serialize ())
100 return 1; 138 return 1;
139 if (0 != test_document ())
140 return 1;
101 return 0; 141 return 0;
102} 142}
103 143
diff --git a/src/namestore/plugin_rest_namestore.c b/src/namestore/plugin_rest_namestore.c
index c20ef8e8a..873a12b83 100644
--- a/src/namestore/plugin_rest_namestore.c
+++ b/src/namestore/plugin_rest_namestore.c
@@ -30,6 +30,7 @@
30#include "gnunet_identity_service.h" 30#include "gnunet_identity_service.h"
31#include "gnunet_rest_lib.h" 31#include "gnunet_rest_lib.h"
32#include "gnunet_jsonapi_lib.h" 32#include "gnunet_jsonapi_lib.h"
33#include "gnunet_jsonapi_util.h"
33#include "microhttpd.h" 34#include "microhttpd.h"
34#include <jansson.h> 35#include <jansson.h>
35 36