aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPhil <phil.buschmann@tum.de>2018-01-22 17:38:45 +0100
committerPhil <phil.buschmann@tum.de>2018-01-22 17:38:45 +0100
commit8c785ca6cea5c84d84fb900a8f519a4d87a3fe8c (patch)
tree0ef829a4b12ba2e11b337d0136c7be8471d09e6e /src
parent963b0f5a783004823a14f65c697cd83dbfb60463 (diff)
parent4fce9ab87811196126dc64afa905cb72688728ea (diff)
downloadgnunet-8c785ca6cea5c84d84fb900a8f519a4d87a3fe8c.tar.gz
gnunet-8c785ca6cea5c84d84fb900a8f519a4d87a3fe8c.zip
Merge branch 'master' into identity_oidc
Diffstat (limited to 'src')
-rw-r--r--src/include/gnunet_json_lib.h10
-rw-r--r--src/json/json_helper.c48
-rw-r--r--src/json/test_json.c40
-rw-r--r--src/rest/gnunet-rest-server.c52
4 files changed, 150 insertions, 0 deletions
diff --git a/src/include/gnunet_json_lib.h b/src/include/gnunet_json_lib.h
index c12badcd9..49120982a 100644
--- a/src/include/gnunet_json_lib.h
+++ b/src/include/gnunet_json_lib.h
@@ -248,6 +248,16 @@ struct GNUNET_JSON_Specification
248GNUNET_JSON_spec_uint64 (const char *name, 248GNUNET_JSON_spec_uint64 (const char *name,
249 uint64_t *u64); 249 uint64_t *u64);
250 250
251/**
252 * Boolean (true mapped to GNUNET_YES, false mapped to GNUNET_NO).
253 *
254 * @param name name of the JSON field
255 * @param[out] boolean where to store the boolean found under @a name
256 */
257struct GNUNET_JSON_Specification
258GNUNET_JSON_spec_boolean (const char *name,
259 int *boolean);
260
251 261
252/* ************ GNUnet-specific parser specifications ******************* */ 262/* ************ GNUnet-specific parser specifications ******************* */
253 263
diff --git a/src/json/json_helper.c b/src/json/json_helper.c
index 194ec5c76..c7eca97ec 100644
--- a/src/json/json_helper.c
+++ b/src/json/json_helper.c
@@ -943,4 +943,52 @@ GNUNET_JSON_spec_rsa_signature (const char *name,
943} 943}
944 944
945 945
946/**
947 * Parse given JSON object to an int as a boolean.
948 *
949 * @param cls closure, NULL
950 * @param root the json object representing data
951 * @param[out] spec where to write the data
952 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
953 */
954static int
955parse_boolean (void *cls,
956 json_t *root,
957 struct GNUNET_JSON_Specification *spec)
958{
959 int *bp = spec->ptr;
960
961 if (! json_is_boolean (root))
962 {
963 GNUNET_break_op (0);
964 return GNUNET_SYSERR;
965 }
966 *bp = json_boolean_value (root) ? GNUNET_YES : GNUNET_NO;
967 return GNUNET_OK;
968}
969
970
971/**
972 * Boolean (true mapped to GNUNET_YES, false mapped to GNUNET_NO).
973 *
974 * @param name name of the JSON field
975 * @param[out] boolean where to store the boolean found under @a name
976 */
977struct GNUNET_JSON_Specification
978GNUNET_JSON_spec_boolean (const char *name,
979 int *boolean)
980{
981 struct GNUNET_JSON_Specification ret = {
982 .parser = &parse_boolean,
983 .cleaner = NULL,
984 .cls = NULL,
985 .field = name,
986 .ptr = boolean,
987 .ptr_size = sizeof (int),
988 .size_ptr = NULL
989 };
990 return ret;
991}
992
993
946/* end of json_helper.c */ 994/* end of json_helper.c */
diff --git a/src/json/test_json.c b/src/json/test_json.c
index 09a154678..adab817b9 100644
--- a/src/json/test_json.c
+++ b/src/json/test_json.c
@@ -195,6 +195,44 @@ test_rsa ()
195} 195}
196 196
197 197
198/**
199 * Test rsa conversions from/to JSON.
200 *
201 * @return 0 on success
202 */
203static int
204test_boolean ()
205{
206 int b1;
207 int b2;
208 json_t *json;
209 struct GNUNET_JSON_Specification pspec[] = {
210 GNUNET_JSON_spec_boolean ("b1", &b1),
211 GNUNET_JSON_spec_boolean ("b2", &b2),
212 GNUNET_JSON_spec_end()
213 };
214
215 json = json_object ();
216 json_object_set_new (json, "b1", json_true ());
217 json_object_set_new (json, "b2", json_false ());
218
219 GNUNET_assert (GNUNET_OK ==
220 GNUNET_JSON_parse (json, pspec,
221 NULL, NULL));
222
223 GNUNET_assert (GNUNET_YES == b1);
224 GNUNET_assert (GNUNET_NO == b2);
225
226 json_object_set_new (json, "b1", json_integer (42));
227
228 GNUNET_assert (GNUNET_OK !=
229 GNUNET_JSON_parse (json, pspec,
230 NULL, NULL));
231
232 return 0;
233}
234
235
198int 236int
199main(int argc, 237main(int argc,
200 const char *const argv[]) 238 const char *const argv[])
@@ -210,6 +248,8 @@ main(int argc,
210 return 1; 248 return 1;
211 if (0 != test_rsa ()) 249 if (0 != test_rsa ())
212 return 1; 250 return 1;
251 if (0 != test_boolean ())
252 return 1;
213 /* FIXME: test EdDSA signature conversion... */ 253 /* FIXME: test EdDSA signature conversion... */
214 return 0; 254 return 0;
215} 255}
diff --git a/src/rest/gnunet-rest-server.c b/src/rest/gnunet-rest-server.c
index c14b09819..a67b3203a 100644
--- a/src/rest/gnunet-rest-server.c
+++ b/src/rest/gnunet-rest-server.c
@@ -131,6 +131,8 @@ struct MhdConnectionHandle
131 131
132 struct GNUNET_REST_RequestHandle *data_handle; 132 struct GNUNET_REST_RequestHandle *data_handle;
133 133
134 struct MHD_PostProcessor *pp;
135
134 int status; 136 int status;
135 137
136 int state; 138 int state;
@@ -272,6 +274,40 @@ url_iterator (void *cls,
272 return MHD_YES; 274 return MHD_YES;
273} 275}
274 276
277static int
278post_data_iter (void *cls,
279 enum MHD_ValueKind kind,
280 const char *key,
281 const char *filename,
282 const char *content_type,
283 const char *transfer_encoding,
284 const char *data,
285 uint64_t off,
286 size_t size)
287{
288 struct GNUNET_REST_RequestHandle *handle = cls;
289 struct GNUNET_HashCode hkey;
290 char *val;
291
292 if (MHD_POSTDATA_KIND != kind)
293 return MHD_YES;
294
295 GNUNET_CRYPTO_hash (key, strlen (key), &hkey);
296 GNUNET_asprintf (&val, "%s", data);
297 if (GNUNET_OK !=
298 GNUNET_CONTAINER_multihashmap_put (handle->url_param_map,
299 &hkey,
300 val,
301 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
302 {
303 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
304 "Could not load add url param `%s'=%s\n",
305 key, data);
306 }
307 return MHD_YES;
308
309}
310
275/* ********************************* MHD response generation ******************* */ 311/* ********************************* MHD response generation ******************* */
276 312
277/** 313/**
@@ -343,6 +379,7 @@ create_response (void *cls,
343 MHD_HTTP_NOT_FOUND, 379 MHD_HTTP_NOT_FOUND,
344 failure_response); 380 failure_response);
345 } 381 }
382
346 return MHD_YES; 383 return MHD_YES;
347 } 384 }
348 if (GN_REST_STATE_INIT == con_handle->state) 385 if (GN_REST_STATE_INIT == con_handle->state)
@@ -361,10 +398,25 @@ create_response (void *cls,
361 MHD_GET_ARGUMENT_KIND, 398 MHD_GET_ARGUMENT_KIND,
362 &url_iterator, 399 &url_iterator,
363 rest_conndata_handle); 400 rest_conndata_handle);
401<<<<<<< HEAD
364 MHD_get_connection_values (con, 402 MHD_get_connection_values (con,
365 MHD_HEADER_KIND, 403 MHD_HEADER_KIND,
366 &header_iterator, 404 &header_iterator,
367 rest_conndata_handle); 405 rest_conndata_handle);
406=======
407 con_handle->pp = MHD_create_post_processor(con,
408 4000,
409 post_data_iter,
410 rest_conndata_handle);
411 if (*upload_data_size)
412 {
413 MHD_post_process(con_handle->pp, upload_data, *upload_data_size);
414 }
415 else
416 {
417 MHD_destroy_post_processor(con_handle->pp);
418 }
419>>>>>>> master
368 con_handle->state = GN_REST_STATE_PROCESSING; 420 con_handle->state = GN_REST_STATE_PROCESSING;
369 con_handle->plugin->process_request (rest_conndata_handle, 421 con_handle->plugin->process_request (rest_conndata_handle,
370 &plugin_callback, 422 &plugin_callback,