diff options
Diffstat (limited to 'src/namestore')
-rw-r--r-- | src/namestore/Makefile.am | 23 | ||||
-rw-r--r-- | src/namestore/plugin_rest_namestore.c | 1055 | ||||
-rwxr-xr-x | src/namestore/test_plugin_rest_namestore.sh | 208 |
3 files changed, 1 insertions, 1285 deletions
diff --git a/src/namestore/Makefile.am b/src/namestore/Makefile.am index 7f44c2a71..a349921d7 100644 --- a/src/namestore/Makefile.am +++ b/src/namestore/Makefile.am | |||
@@ -97,12 +97,6 @@ check_PROGRAMS = \ | |||
97 | $(FLAT_TESTS) | 97 | $(FLAT_TESTS) |
98 | endif | 98 | endif |
99 | 99 | ||
100 | if HAVE_MHD | ||
101 | if HAVE_JSON | ||
102 | REST_PLUGIN=libgnunet_plugin_rest_namestore.la | ||
103 | endif | ||
104 | endif | ||
105 | |||
106 | if ENABLE_TEST_RUN | 100 | if ENABLE_TEST_RUN |
107 | AM_TESTS_ENVIRONMENT=export GNUNET_PREFIX=$${GNUNET_PREFIX:-@libdir@};export PATH=$${GNUNET_PREFIX:-@prefix@}/bin:$$PATH;unset XDG_DATA_HOME;unset XDG_CONFIG_HOME; | 101 | AM_TESTS_ENVIRONMENT=export GNUNET_PREFIX=$${GNUNET_PREFIX:-@libdir@};export PATH=$${GNUNET_PREFIX:-@prefix@}/bin:$$PATH;unset XDG_DATA_HOME;unset XDG_CONFIG_HOME; |
108 | TESTS = \ | 102 | TESTS = \ |
@@ -186,8 +180,7 @@ gnunet_service_namestore_LDADD = \ | |||
186 | plugin_LTLIBRARIES = \ | 180 | plugin_LTLIBRARIES = \ |
187 | $(SQLITE_PLUGIN) \ | 181 | $(SQLITE_PLUGIN) \ |
188 | $(POSTGRES_PLUGIN) \ | 182 | $(POSTGRES_PLUGIN) \ |
189 | $(FLAT_PLUGIN) \ | 183 | $(FLAT_PLUGIN) |
190 | $(REST_PLUGIN) | ||
191 | 184 | ||
192 | 185 | ||
193 | 186 | ||
@@ -224,20 +217,6 @@ libgnunet_plugin_namestore_postgres_la_LIBADD = \ | |||
224 | libgnunet_plugin_namestore_postgres_la_LDFLAGS = \ | 217 | libgnunet_plugin_namestore_postgres_la_LDFLAGS = \ |
225 | $(GN_PLUGIN_LDFLAGS) $(POSTGRESQL_LDFLAGS) | 218 | $(GN_PLUGIN_LDFLAGS) $(POSTGRESQL_LDFLAGS) |
226 | 219 | ||
227 | libgnunet_plugin_rest_namestore_la_SOURCES = \ | ||
228 | plugin_rest_namestore.c | ||
229 | libgnunet_plugin_rest_namestore_la_LIBADD = \ | ||
230 | libgnunetnamestore.la \ | ||
231 | $(top_builddir)/src/rest/libgnunetrest.la \ | ||
232 | $(top_builddir)/src/identity/libgnunetidentity.la \ | ||
233 | $(top_builddir)/src/json/libgnunetjson.la \ | ||
234 | $(top_builddir)/src/gnsrecord/libgnunetgnsrecord.la \ | ||
235 | $(top_builddir)/src/util/libgnunetutil.la $(XLIBS) \ | ||
236 | $(LTLIBINTL) -ljansson -lmicrohttpd | ||
237 | libgnunet_plugin_rest_namestore_la_LDFLAGS = \ | ||
238 | $(GN_PLUGIN_LDFLAGS) | ||
239 | |||
240 | |||
241 | test_namestore_api_store_flat_SOURCES = \ | 220 | test_namestore_api_store_flat_SOURCES = \ |
242 | test_namestore_api_store.c | 221 | test_namestore_api_store.c |
243 | test_namestore_api_store_flat_LDADD = \ | 222 | test_namestore_api_store_flat_LDADD = \ |
diff --git a/src/namestore/plugin_rest_namestore.c b/src/namestore/plugin_rest_namestore.c deleted file mode 100644 index f14707cce..000000000 --- a/src/namestore/plugin_rest_namestore.c +++ /dev/null | |||
@@ -1,1055 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2012-2015 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | */ | ||
18 | /** | ||
19 | * @author Martin Schanzenbach | ||
20 | * @author Philippe Buschmann | ||
21 | * @file namestore/plugin_rest_namestore.c | ||
22 | * @brief GNUnet Namestore REST plugin | ||
23 | */ | ||
24 | |||
25 | #include "platform.h" | ||
26 | #include "gnunet_rest_plugin.h" | ||
27 | #include "gnunet_gns_service.h" | ||
28 | #include "gnunet_namestore_service.h" | ||
29 | #include "gnunet_identity_service.h" | ||
30 | #include "gnunet_rest_lib.h" | ||
31 | #include "gnunet_json_lib.h" | ||
32 | #include "microhttpd.h" | ||
33 | #include <jansson.h> | ||
34 | |||
35 | |||
36 | #define GNUNET_REST_API_NS_NAMESTORE "/namestore" | ||
37 | #define GNUNET_REST_SUBSYSTEM_NAMESTORE "namestore" | ||
38 | |||
39 | /** | ||
40 | * Parameter names | ||
41 | */ | ||
42 | #define GNUNET_REST_API_PARAM_PUBKEY "pubkey" | ||
43 | #define GNUNET_REST_API_PARAM_NAME "name" | ||
44 | |||
45 | /** | ||
46 | * Error messages | ||
47 | */ | ||
48 | #define GNUNET_REST_NAMESTORE_ERROR_UNKNOWN "Unknown Error" | ||
49 | |||
50 | #define GNUNET_REST_NAMESTORE_RD_COUNT 1 | ||
51 | |||
52 | /** | ||
53 | * State while collecting all egos | ||
54 | */ | ||
55 | #define ID_REST_STATE_INIT 0 | ||
56 | |||
57 | /** | ||
58 | * Done collecting egos | ||
59 | */ | ||
60 | #define ID_REST_STATE_POST_INIT 1 | ||
61 | /** | ||
62 | * The configuration handle | ||
63 | */ | ||
64 | const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
65 | |||
66 | /** | ||
67 | * HTTP methods allows for this plugin | ||
68 | */ | ||
69 | static char* allow_methods; | ||
70 | |||
71 | /** | ||
72 | * @brief struct returned by the initialization function of the plugin | ||
73 | */ | ||
74 | struct Plugin | ||
75 | { | ||
76 | const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
77 | }; | ||
78 | |||
79 | /** | ||
80 | * The default namestore ego | ||
81 | */ | ||
82 | struct EgoEntry | ||
83 | { | ||
84 | /** | ||
85 | * DLL | ||
86 | */ | ||
87 | struct EgoEntry *next; | ||
88 | |||
89 | /** | ||
90 | * DLL | ||
91 | */ | ||
92 | struct EgoEntry *prev; | ||
93 | |||
94 | /** | ||
95 | * Ego Identifier | ||
96 | */ | ||
97 | char *identifier; | ||
98 | |||
99 | /** | ||
100 | * Public key string | ||
101 | */ | ||
102 | char *keystring; | ||
103 | |||
104 | /** | ||
105 | * The Ego | ||
106 | */ | ||
107 | struct GNUNET_IDENTITY_Ego *ego; | ||
108 | }; | ||
109 | |||
110 | |||
111 | struct RequestHandle | ||
112 | { | ||
113 | /** | ||
114 | * Records to store | ||
115 | */ | ||
116 | char *label_name; | ||
117 | |||
118 | /** | ||
119 | * Records to store | ||
120 | */ | ||
121 | struct GNUNET_GNSRECORD_Data *rd; | ||
122 | |||
123 | /** | ||
124 | * NAMESTORE Operation | ||
125 | */ | ||
126 | struct GNUNET_NAMESTORE_QueueEntry *add_qe; | ||
127 | |||
128 | /** | ||
129 | * Response object | ||
130 | */ | ||
131 | json_t *resp_object; | ||
132 | |||
133 | /** | ||
134 | * The processing state | ||
135 | */ | ||
136 | int state; | ||
137 | |||
138 | /** | ||
139 | * Handle to NAMESTORE | ||
140 | */ | ||
141 | struct GNUNET_NAMESTORE_Handle *ns_handle; | ||
142 | |||
143 | /** | ||
144 | * Handle to NAMESTORE it | ||
145 | */ | ||
146 | struct GNUNET_NAMESTORE_ZoneIterator *list_it; | ||
147 | |||
148 | /** | ||
149 | * Private key for the zone | ||
150 | */ | ||
151 | const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_pkey; | ||
152 | |||
153 | /** | ||
154 | * IDENTITY Operation | ||
155 | */ | ||
156 | struct EgoEntry *ego_entry; | ||
157 | |||
158 | /** | ||
159 | * Ego list | ||
160 | */ | ||
161 | struct EgoEntry *ego_head; | ||
162 | |||
163 | /** | ||
164 | * Ego list | ||
165 | */ | ||
166 | struct EgoEntry *ego_tail; | ||
167 | |||
168 | /** | ||
169 | * IDENTITY Operation | ||
170 | */ | ||
171 | struct GNUNET_IDENTITY_Operation *op; | ||
172 | |||
173 | /** | ||
174 | * Handle to Identity service. | ||
175 | */ | ||
176 | struct GNUNET_IDENTITY_Handle *identity_handle; | ||
177 | |||
178 | /** | ||
179 | * Rest connection | ||
180 | */ | ||
181 | struct GNUNET_REST_RequestHandle *rest_handle; | ||
182 | |||
183 | /** | ||
184 | * Desired timeout for the lookup (default is no timeout). | ||
185 | */ | ||
186 | struct GNUNET_TIME_Relative timeout; | ||
187 | |||
188 | /** | ||
189 | * ID of a task associated with the resolution process. | ||
190 | */ | ||
191 | struct GNUNET_SCHEDULER_Task *timeout_task; | ||
192 | |||
193 | /** | ||
194 | * The plugin result processor | ||
195 | */ | ||
196 | GNUNET_REST_ResultProcessor proc; | ||
197 | |||
198 | /** | ||
199 | * The closure of the result processor | ||
200 | */ | ||
201 | void *proc_cls; | ||
202 | |||
203 | /** | ||
204 | * The url | ||
205 | */ | ||
206 | char *url; | ||
207 | |||
208 | /** | ||
209 | * Error response message | ||
210 | */ | ||
211 | char *emsg; | ||
212 | |||
213 | /** | ||
214 | * Reponse code | ||
215 | */ | ||
216 | int response_code; | ||
217 | |||
218 | }; | ||
219 | |||
220 | /** | ||
221 | * Cleanup lookup handle | ||
222 | * @param handle Handle to clean up | ||
223 | */ | ||
224 | static void | ||
225 | cleanup_handle (void *cls) | ||
226 | { | ||
227 | struct RequestHandle *handle = cls; | ||
228 | struct EgoEntry *ego_entry; | ||
229 | struct EgoEntry *ego_tmp; | ||
230 | |||
231 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
232 | "Cleaning up\n"); | ||
233 | if (NULL != handle->timeout_task) | ||
234 | { | ||
235 | GNUNET_SCHEDULER_cancel (handle->timeout_task); | ||
236 | handle->timeout_task = NULL; | ||
237 | } | ||
238 | if (NULL != handle->label_name) | ||
239 | GNUNET_free(handle->label_name); | ||
240 | if (NULL != handle->url) | ||
241 | GNUNET_free(handle->url); | ||
242 | if (NULL != handle->emsg) | ||
243 | GNUNET_free(handle->emsg); | ||
244 | if (NULL != handle->rd) | ||
245 | { | ||
246 | if (NULL != handle->rd->data) | ||
247 | GNUNET_free((void*)handle->rd->data); | ||
248 | GNUNET_free(handle->rd); | ||
249 | } | ||
250 | if (NULL != handle->timeout_task) | ||
251 | GNUNET_SCHEDULER_cancel(handle->timeout_task); | ||
252 | if (NULL != handle->list_it) | ||
253 | GNUNET_NAMESTORE_zone_iteration_stop(handle->list_it); | ||
254 | if (NULL != handle->add_qe) | ||
255 | GNUNET_NAMESTORE_cancel(handle->add_qe); | ||
256 | if (NULL != handle->identity_handle) | ||
257 | GNUNET_IDENTITY_disconnect(handle->identity_handle); | ||
258 | if (NULL != handle->ns_handle) | ||
259 | { | ||
260 | GNUNET_NAMESTORE_disconnect(handle->ns_handle); | ||
261 | } | ||
262 | |||
263 | for (ego_entry = handle->ego_head; | ||
264 | NULL != ego_entry;) | ||
265 | { | ||
266 | ego_tmp = ego_entry; | ||
267 | ego_entry = ego_entry->next; | ||
268 | GNUNET_free(ego_tmp->identifier); | ||
269 | GNUNET_free(ego_tmp->keystring); | ||
270 | GNUNET_free(ego_tmp); | ||
271 | } | ||
272 | |||
273 | if(NULL != handle->resp_object) | ||
274 | { | ||
275 | json_decref(handle->resp_object); | ||
276 | } | ||
277 | |||
278 | GNUNET_free (handle); | ||
279 | } | ||
280 | |||
281 | |||
282 | /** | ||
283 | * Task run on errors. Reports an error and cleans up everything. | ||
284 | * | ||
285 | * @param cls the `struct RequestHandle` | ||
286 | */ | ||
287 | static void | ||
288 | do_error (void *cls) | ||
289 | { | ||
290 | struct RequestHandle *handle = cls; | ||
291 | struct MHD_Response *resp; | ||
292 | json_t *json_error = json_object(); | ||
293 | char *response; | ||
294 | |||
295 | if (NULL == handle->emsg) | ||
296 | handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_ERROR_UNKNOWN); | ||
297 | |||
298 | json_object_set_new(json_error,"error", json_string(handle->emsg)); | ||
299 | |||
300 | if (0 == handle->response_code) | ||
301 | handle->response_code = MHD_HTTP_OK; | ||
302 | response = json_dumps (json_error, 0); | ||
303 | resp = GNUNET_REST_create_response (response); | ||
304 | handle->proc (handle->proc_cls, resp, handle->response_code); | ||
305 | json_decref(json_error); | ||
306 | GNUNET_free(response); | ||
307 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | ||
308 | } | ||
309 | |||
310 | |||
311 | /** | ||
312 | * Get EgoEntry from list with either a public key or a name | ||
313 | * If public key and name are not NULL, it returns the public key result first | ||
314 | * | ||
315 | * @param handle the RequestHandle | ||
316 | * @param pubkey the public key of an identity (only one can be NULL) | ||
317 | * @param name the name of an identity (only one can be NULL) | ||
318 | * @return EgoEntry or NULL if not found | ||
319 | */ | ||
320 | struct EgoEntry* | ||
321 | get_egoentry(struct RequestHandle *handle, char* pubkey, char *name) | ||
322 | { | ||
323 | struct EgoEntry *ego_entry; | ||
324 | if (NULL != pubkey) | ||
325 | { | ||
326 | for (ego_entry = handle->ego_head; | ||
327 | NULL != ego_entry; | ||
328 | ego_entry = ego_entry->next) | ||
329 | { | ||
330 | if (0 != strcasecmp (pubkey, ego_entry->keystring)) | ||
331 | continue; | ||
332 | return ego_entry; | ||
333 | } | ||
334 | } | ||
335 | if (NULL != name) | ||
336 | { | ||
337 | for (ego_entry = handle->ego_head; | ||
338 | NULL != ego_entry; | ||
339 | ego_entry = ego_entry->next) | ||
340 | { | ||
341 | if (0 != strcasecmp (name, ego_entry->identifier)) | ||
342 | continue; | ||
343 | return ego_entry; | ||
344 | } | ||
345 | } | ||
346 | return NULL; | ||
347 | } | ||
348 | |||
349 | |||
350 | /** | ||
351 | * Does internal server error when iteration failed. | ||
352 | */ | ||
353 | static void | ||
354 | namestore_iteration_error (void *cls) | ||
355 | { | ||
356 | struct RequestHandle *handle = cls; | ||
357 | struct MHD_Response *resp = GNUNET_REST_create_response (NULL); | ||
358 | handle->proc (handle->proc_cls, resp, MHD_HTTP_INTERNAL_SERVER_ERROR); | ||
359 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | ||
360 | } | ||
361 | |||
362 | |||
363 | static void | ||
364 | create_finished (void *cls, int32_t success, const char *emsg) | ||
365 | { | ||
366 | struct RequestHandle *handle = cls; | ||
367 | struct MHD_Response *resp; | ||
368 | |||
369 | handle->add_qe = NULL; | ||
370 | if (GNUNET_YES != success) | ||
371 | { | ||
372 | handle->emsg = GNUNET_strdup("Error storing records"); | ||
373 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
374 | return; | ||
375 | } | ||
376 | resp = GNUNET_REST_create_response (NULL); | ||
377 | handle->proc (handle->proc_cls, resp, MHD_HTTP_NO_CONTENT); | ||
378 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | ||
379 | } | ||
380 | |||
381 | |||
382 | static void | ||
383 | del_finished (void *cls, int32_t success, const char *emsg) | ||
384 | { | ||
385 | struct RequestHandle *handle = cls; | ||
386 | |||
387 | handle->add_qe = NULL; | ||
388 | if (GNUNET_NO == success) | ||
389 | { | ||
390 | handle->emsg = GNUNET_strdup("Deleting record failed. Record does not exist"); | ||
391 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
392 | return; | ||
393 | } | ||
394 | if (GNUNET_SYSERR == success) | ||
395 | { | ||
396 | handle->emsg = GNUNET_strdup("Deleting record failed"); | ||
397 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
398 | return; | ||
399 | } | ||
400 | handle->proc (handle->proc_cls, | ||
401 | GNUNET_REST_create_response (NULL), | ||
402 | MHD_HTTP_NO_CONTENT); | ||
403 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | ||
404 | } | ||
405 | |||
406 | |||
407 | /** | ||
408 | * Iteration over all results finished, build final | ||
409 | * response. | ||
410 | * | ||
411 | * @param cls the `struct RequestHandle` | ||
412 | */ | ||
413 | static void | ||
414 | namestore_list_finished (void *cls) | ||
415 | { | ||
416 | struct RequestHandle *handle = cls; | ||
417 | char *result_str; | ||
418 | struct MHD_Response *resp; | ||
419 | |||
420 | handle->list_it = NULL; | ||
421 | |||
422 | if (NULL == handle->resp_object) | ||
423 | { | ||
424 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
425 | return; | ||
426 | } | ||
427 | |||
428 | result_str = json_dumps (handle->resp_object, 0); | ||
429 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); | ||
430 | resp = GNUNET_REST_create_response (result_str); | ||
431 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); | ||
432 | GNUNET_free_non_null (result_str); | ||
433 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | ||
434 | } | ||
435 | |||
436 | |||
437 | /** | ||
438 | * Create a response with requested records | ||
439 | * | ||
440 | * @param handle the RequestHandle | ||
441 | */ | ||
442 | static void | ||
443 | namestore_list_iteration (void *cls, | ||
444 | const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key, | ||
445 | const char *rname, | ||
446 | unsigned int rd_len, | ||
447 | const struct GNUNET_GNSRECORD_Data *rd) | ||
448 | { | ||
449 | struct RequestHandle *handle = cls; | ||
450 | json_t *record_obj; | ||
451 | |||
452 | if (NULL == handle->resp_object) | ||
453 | handle->resp_object = json_array(); | ||
454 | |||
455 | /*if ( (NULL != handle->ego_entry->identifier) && | ||
456 | (0 != strcmp (handle->ego_entry->identifier, | ||
457 | rname)) ) | ||
458 | { | ||
459 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
460 | "%s does not match %s\n", rname, | ||
461 | handle->ego_entry->identifier); | ||
462 | GNUNET_NAMESTORE_zone_iterator_next (handle->list_it, 1); | ||
463 | return; | ||
464 | }*/ | ||
465 | |||
466 | for (unsigned int i = 0; i < rd_len; i++) | ||
467 | { | ||
468 | if ( (GNUNET_GNSRECORD_TYPE_NICK == rd[i].record_type) && | ||
469 | (0 != strcmp (rname, GNUNET_GNS_EMPTY_LABEL_AT)) ) | ||
470 | continue; | ||
471 | |||
472 | record_obj = GNUNET_JSON_from_gns_record(rname,rd); | ||
473 | |||
474 | if(NULL == record_obj) | ||
475 | continue; | ||
476 | |||
477 | json_array_append (handle->resp_object, record_obj); | ||
478 | json_decref (record_obj); | ||
479 | } | ||
480 | |||
481 | GNUNET_NAMESTORE_zone_iterator_next (handle->list_it, 1); | ||
482 | } | ||
483 | |||
484 | |||
485 | /** | ||
486 | * Handle namestore GET request | ||
487 | * | ||
488 | * @param con_handle the connection handle | ||
489 | * @param url the url | ||
490 | * @param cls the RequestHandle | ||
491 | */ | ||
492 | void | ||
493 | namestore_get (struct GNUNET_REST_RequestHandle *con_handle, | ||
494 | const char* url, | ||
495 | void *cls) | ||
496 | { | ||
497 | struct RequestHandle *handle = cls; | ||
498 | struct EgoEntry *ego_entry = NULL; | ||
499 | struct GNUNET_HashCode key; | ||
500 | char *pubkey = NULL; | ||
501 | char *name = NULL; | ||
502 | |||
503 | //change zone if pubkey or name specified | ||
504 | GNUNET_CRYPTO_hash (GNUNET_REST_API_PARAM_PUBKEY, | ||
505 | strlen (GNUNET_REST_API_PARAM_PUBKEY), | ||
506 | &key); | ||
507 | if ( GNUNET_YES | ||
508 | == GNUNET_CONTAINER_multihashmap_contains (con_handle->url_param_map, | ||
509 | &key)) | ||
510 | { | ||
511 | pubkey = GNUNET_CONTAINER_multihashmap_get (con_handle->url_param_map, | ||
512 | &key); | ||
513 | } | ||
514 | GNUNET_CRYPTO_hash (GNUNET_REST_API_PARAM_NAME, | ||
515 | strlen (GNUNET_REST_API_PARAM_NAME), | ||
516 | &key); | ||
517 | if ( GNUNET_YES | ||
518 | == GNUNET_CONTAINER_multihashmap_contains (con_handle->url_param_map, | ||
519 | &key)) | ||
520 | { | ||
521 | name = GNUNET_CONTAINER_multihashmap_get (con_handle->url_param_map, | ||
522 | &key); | ||
523 | } | ||
524 | |||
525 | ego_entry = get_egoentry(handle,pubkey,name); | ||
526 | if (NULL == ego_entry) | ||
527 | { | ||
528 | if (NULL != pubkey || NULL != name) | ||
529 | { | ||
530 | handle->emsg = GNUNET_strdup("Invalid identity"); | ||
531 | handle->response_code = MHD_HTTP_NOT_FOUND; | ||
532 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
533 | return; | ||
534 | } | ||
535 | } | ||
536 | if ( NULL != ego_entry ) | ||
537 | { | ||
538 | handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key(ego_entry->ego); | ||
539 | } | ||
540 | handle->list_it = GNUNET_NAMESTORE_zone_iteration_start (handle->ns_handle, | ||
541 | handle->zone_pkey, | ||
542 | &namestore_iteration_error, | ||
543 | handle, | ||
544 | &namestore_list_iteration, | ||
545 | handle, | ||
546 | &namestore_list_finished, | ||
547 | handle); | ||
548 | } | ||
549 | |||
550 | |||
551 | /** | ||
552 | * We're storing a new record; this requires | ||
553 | * that no record already exists | ||
554 | * | ||
555 | * @param cls closure, unused | ||
556 | * @param zone_key private key of the zone | ||
557 | * @param rec_name name that is being mapped (at most 255 characters long) | ||
558 | * @param rd_count number of entries in @a rd array | ||
559 | * @param rd array of records with data to store | ||
560 | */ | ||
561 | static void | ||
562 | create_new_record_cont (void *cls, | ||
563 | const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key, | ||
564 | const char *rec_name, | ||
565 | unsigned int rd_count, | ||
566 | const struct GNUNET_GNSRECORD_Data *rd) | ||
567 | { | ||
568 | struct RequestHandle *handle = cls; | ||
569 | |||
570 | handle->add_qe = NULL; | ||
571 | if (0 != strcmp (rec_name, handle->label_name)) | ||
572 | { | ||
573 | GNUNET_break (0); | ||
574 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
575 | return; | ||
576 | } | ||
577 | |||
578 | if (0 != rd_count) | ||
579 | { | ||
580 | handle->proc (handle->proc_cls, | ||
581 | GNUNET_REST_create_response (NULL), | ||
582 | MHD_HTTP_CONFLICT); | ||
583 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | ||
584 | return; | ||
585 | } | ||
586 | handle->add_qe = GNUNET_NAMESTORE_records_store (handle->ns_handle, | ||
587 | handle->zone_pkey, | ||
588 | handle->label_name, | ||
589 | GNUNET_REST_NAMESTORE_RD_COUNT, | ||
590 | handle->rd, | ||
591 | &create_finished, | ||
592 | handle); | ||
593 | } | ||
594 | |||
595 | /** | ||
596 | * Handle namestore POST request | ||
597 | * | ||
598 | * @param con_handle the connection handle | ||
599 | * @param url the url | ||
600 | * @param cls the RequestHandle | ||
601 | */ | ||
602 | void | ||
603 | namestore_add (struct GNUNET_REST_RequestHandle *con_handle, | ||
604 | const char* url, | ||
605 | void *cls) | ||
606 | { | ||
607 | struct RequestHandle *handle = cls; | ||
608 | struct GNUNET_GNSRECORD_Data *gns_record; | ||
609 | json_t *data_js; | ||
610 | json_t *name_json; | ||
611 | json_error_t err; | ||
612 | |||
613 | struct EgoEntry *ego_entry = NULL; | ||
614 | struct GNUNET_HashCode key; | ||
615 | char *pubkey = NULL; | ||
616 | char *name = NULL; | ||
617 | |||
618 | char term_data[handle->rest_handle->data_size + 1]; | ||
619 | struct GNUNET_JSON_Specification gnsspec[] = { | ||
620 | GNUNET_JSON_spec_gnsrecord_data(&gns_record), | ||
621 | GNUNET_JSON_spec_end () | ||
622 | }; | ||
623 | |||
624 | if (strlen (GNUNET_REST_API_NS_NAMESTORE) != strlen (handle->url)) | ||
625 | { | ||
626 | handle->emsg = GNUNET_strdup("Wrong URL"); | ||
627 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
628 | return; | ||
629 | } | ||
630 | if (0 >= handle->rest_handle->data_size) | ||
631 | { | ||
632 | handle->emsg = GNUNET_strdup("No data"); | ||
633 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
634 | return; | ||
635 | } | ||
636 | term_data[handle->rest_handle->data_size] = '\0'; | ||
637 | GNUNET_memcpy(term_data, handle->rest_handle->data, | ||
638 | handle->rest_handle->data_size); | ||
639 | data_js = json_loads (term_data, JSON_DECODE_ANY, &err); | ||
640 | if (GNUNET_OK != GNUNET_JSON_parse (data_js, gnsspec, NULL, NULL)) | ||
641 | { | ||
642 | handle->emsg = GNUNET_strdup("Invalid data"); | ||
643 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
644 | GNUNET_JSON_parse_free(gnsspec); | ||
645 | json_decref (data_js); | ||
646 | return; | ||
647 | } | ||
648 | handle->rd = gns_record; | ||
649 | |||
650 | name_json = json_object_get(data_js, "label"); | ||
651 | if (!json_is_string(name_json)) | ||
652 | { | ||
653 | handle->emsg = GNUNET_strdup("Missing name"); | ||
654 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
655 | json_decref (data_js); | ||
656 | return; | ||
657 | } | ||
658 | handle->label_name = GNUNET_strdup(json_string_value(name_json)); | ||
659 | if(NULL == handle->label_name) | ||
660 | { | ||
661 | handle->emsg = GNUNET_strdup("Missing name"); | ||
662 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
663 | json_decref (data_js); | ||
664 | return; | ||
665 | } | ||
666 | if (0 >= strlen(handle->label_name)) | ||
667 | { | ||
668 | handle->emsg = GNUNET_strdup("Missing name"); | ||
669 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
670 | json_decref (data_js); | ||
671 | return; | ||
672 | } | ||
673 | json_decref (data_js); | ||
674 | |||
675 | //change zone if pubkey or name specified | ||
676 | GNUNET_CRYPTO_hash (GNUNET_REST_API_PARAM_PUBKEY, | ||
677 | strlen (GNUNET_REST_API_PARAM_PUBKEY), | ||
678 | &key); | ||
679 | if ( GNUNET_YES | ||
680 | == GNUNET_CONTAINER_multihashmap_contains (con_handle->url_param_map, | ||
681 | &key)) | ||
682 | { | ||
683 | pubkey = GNUNET_CONTAINER_multihashmap_get (con_handle->url_param_map, | ||
684 | &key); | ||
685 | } | ||
686 | GNUNET_CRYPTO_hash (GNUNET_REST_API_PARAM_NAME, | ||
687 | strlen (GNUNET_REST_API_PARAM_NAME), | ||
688 | &key); | ||
689 | if ( GNUNET_YES | ||
690 | == GNUNET_CONTAINER_multihashmap_contains (con_handle->url_param_map, | ||
691 | &key)) | ||
692 | { | ||
693 | name = GNUNET_CONTAINER_multihashmap_get (con_handle->url_param_map, | ||
694 | &key); | ||
695 | } | ||
696 | |||
697 | ego_entry = get_egoentry(handle,pubkey,name); | ||
698 | if (NULL == ego_entry) | ||
699 | { | ||
700 | if (NULL != pubkey || NULL != name) | ||
701 | { | ||
702 | handle->emsg = GNUNET_strdup("Invalid identity"); | ||
703 | handle->response_code = MHD_HTTP_NOT_FOUND; | ||
704 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
705 | return; | ||
706 | } | ||
707 | } | ||
708 | if ( NULL != ego_entry ) | ||
709 | { | ||
710 | handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key(ego_entry->ego); | ||
711 | } | ||
712 | if (NULL == handle->zone_pkey) | ||
713 | { | ||
714 | handle->emsg = GNUNET_strdup("No default identity for namestore"); | ||
715 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
716 | return; | ||
717 | } | ||
718 | handle->add_qe = GNUNET_NAMESTORE_records_lookup (handle->ns_handle, | ||
719 | handle->zone_pkey, | ||
720 | handle->label_name, | ||
721 | &do_error, | ||
722 | handle, | ||
723 | &create_new_record_cont, | ||
724 | handle); | ||
725 | } | ||
726 | |||
727 | |||
728 | static void | ||
729 | del_cont (void *cls, | ||
730 | const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, | ||
731 | const char *label, | ||
732 | unsigned int rd_count, | ||
733 | const struct GNUNET_GNSRECORD_Data *rd) | ||
734 | { | ||
735 | struct RequestHandle *handle = cls; | ||
736 | |||
737 | handle->add_qe = NULL; | ||
738 | if (0 == rd_count) | ||
739 | { | ||
740 | handle->emsg = GNUNET_strdup("Record not found"); | ||
741 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
742 | return; | ||
743 | } | ||
744 | |||
745 | handle->add_qe = GNUNET_NAMESTORE_records_store (handle->ns_handle, | ||
746 | handle->zone_pkey, | ||
747 | handle->label_name, | ||
748 | 0, NULL, | ||
749 | &del_finished, | ||
750 | handle); | ||
751 | } | ||
752 | |||
753 | /** | ||
754 | * Handle namestore DELETE request | ||
755 | * | ||
756 | * @param con_handle the connection handle | ||
757 | * @param url the url | ||
758 | * @param cls the RequestHandle | ||
759 | */ | ||
760 | void | ||
761 | namestore_delete (struct GNUNET_REST_RequestHandle *con_handle, | ||
762 | const char* url, | ||
763 | void *cls) | ||
764 | { | ||
765 | struct RequestHandle *handle = cls; | ||
766 | struct GNUNET_HashCode key; | ||
767 | struct EgoEntry *ego_entry = NULL; | ||
768 | char *pubkey = NULL; | ||
769 | char *name = NULL; | ||
770 | |||
771 | //change zone if pubkey or name specified | ||
772 | GNUNET_CRYPTO_hash (GNUNET_REST_API_PARAM_PUBKEY, | ||
773 | strlen (GNUNET_REST_API_PARAM_PUBKEY), | ||
774 | &key); | ||
775 | if ( GNUNET_YES | ||
776 | == GNUNET_CONTAINER_multihashmap_contains (con_handle->url_param_map, | ||
777 | &key)) | ||
778 | { | ||
779 | pubkey = GNUNET_CONTAINER_multihashmap_get (con_handle->url_param_map, | ||
780 | &key); | ||
781 | } | ||
782 | GNUNET_CRYPTO_hash (GNUNET_REST_API_PARAM_NAME, | ||
783 | strlen (GNUNET_REST_API_PARAM_NAME), | ||
784 | &key); | ||
785 | if ( GNUNET_YES | ||
786 | == GNUNET_CONTAINER_multihashmap_contains (con_handle->url_param_map, | ||
787 | &key)) | ||
788 | { | ||
789 | name = GNUNET_CONTAINER_multihashmap_get (con_handle->url_param_map, | ||
790 | &key); | ||
791 | } | ||
792 | |||
793 | ego_entry = get_egoentry(handle,pubkey,name); | ||
794 | if (NULL == ego_entry) | ||
795 | { | ||
796 | if (NULL != pubkey || NULL != name) | ||
797 | { | ||
798 | handle->emsg = GNUNET_strdup("Invalid identity"); | ||
799 | handle->response_code = MHD_HTTP_NOT_FOUND; | ||
800 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
801 | return; | ||
802 | } | ||
803 | } | ||
804 | if ( NULL != ego_entry ) | ||
805 | { | ||
806 | handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key(ego_entry->ego); | ||
807 | } | ||
808 | |||
809 | GNUNET_CRYPTO_hash ("label", strlen ("label"), &key); | ||
810 | if ( GNUNET_NO | ||
811 | == GNUNET_CONTAINER_multihashmap_contains (con_handle->url_param_map, | ||
812 | &key)) | ||
813 | { | ||
814 | handle->emsg = GNUNET_strdup("Missing name"); | ||
815 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
816 | return; | ||
817 | } | ||
818 | handle->label_name = GNUNET_strdup( | ||
819 | GNUNET_CONTAINER_multihashmap_get (con_handle->url_param_map, &key)); | ||
820 | |||
821 | if (NULL == handle->zone_pkey) | ||
822 | { | ||
823 | handle->emsg = GNUNET_strdup("No default identity for namestore"); | ||
824 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
825 | return; | ||
826 | } | ||
827 | |||
828 | handle->add_qe = GNUNET_NAMESTORE_records_lookup (handle->ns_handle, | ||
829 | handle->zone_pkey, | ||
830 | handle->label_name, | ||
831 | &do_error, | ||
832 | handle, | ||
833 | &del_cont, | ||
834 | handle); | ||
835 | |||
836 | } | ||
837 | |||
838 | |||
839 | |||
840 | /** | ||
841 | * Respond to OPTIONS request | ||
842 | * | ||
843 | * @param con_handle the connection handle | ||
844 | * @param url the url | ||
845 | * @param cls the RequestHandle | ||
846 | */ | ||
847 | static void | ||
848 | options_cont (struct GNUNET_REST_RequestHandle *con_handle, | ||
849 | const char* url, | ||
850 | void *cls) | ||
851 | { | ||
852 | struct MHD_Response *resp; | ||
853 | struct RequestHandle *handle = cls; | ||
854 | |||
855 | //independent of path return all options | ||
856 | resp = GNUNET_REST_create_response (NULL); | ||
857 | MHD_add_response_header (resp, | ||
858 | "Access-Control-Allow-Methods", | ||
859 | allow_methods); | ||
860 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); | ||
861 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | ||
862 | return; | ||
863 | } | ||
864 | |||
865 | |||
866 | /** | ||
867 | * Handle rest request | ||
868 | * | ||
869 | * @param handle the request handle | ||
870 | */ | ||
871 | static void | ||
872 | init_cont (struct RequestHandle *handle) | ||
873 | { | ||
874 | struct GNUNET_REST_RequestHandlerError err; | ||
875 | static const struct GNUNET_REST_RequestHandler handlers[] = { | ||
876 | {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_NAMESTORE, &namestore_get}, | ||
877 | {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_NAMESTORE, &namestore_add}, | ||
878 | {MHD_HTTP_METHOD_DELETE, GNUNET_REST_API_NS_NAMESTORE, &namestore_delete}, | ||
879 | {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_NAMESTORE, &options_cont}, | ||
880 | GNUNET_REST_HANDLER_END | ||
881 | }; | ||
882 | |||
883 | if (GNUNET_NO == GNUNET_REST_handle_request (handle->rest_handle, | ||
884 | handlers, | ||
885 | &err, | ||
886 | handle)) | ||
887 | { | ||
888 | handle->response_code = err.error_code; | ||
889 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
890 | } | ||
891 | } | ||
892 | |||
893 | /** | ||
894 | * @param cls closure | ||
895 | * @param ego ego handle | ||
896 | * @param ctx context for application to store data for this ego | ||
897 | * (during the lifetime of this process, initially NULL) | ||
898 | * @param identifier identifier assigned by the user for this ego, | ||
899 | * NULL if the user just deleted the ego and it | ||
900 | * must thus no longer be used | ||
901 | */ | ||
902 | static void | ||
903 | default_ego_cb (void *cls, | ||
904 | struct GNUNET_IDENTITY_Ego *ego, | ||
905 | void **ctx, | ||
906 | const char *identifier) | ||
907 | { | ||
908 | struct RequestHandle *handle = cls; | ||
909 | handle->op = NULL; | ||
910 | |||
911 | if (ego != NULL) | ||
912 | { | ||
913 | handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key (ego); | ||
914 | } | ||
915 | } | ||
916 | |||
917 | |||
918 | /** | ||
919 | * Connect to identity callback | ||
920 | */ | ||
921 | static void | ||
922 | id_connect_cb (void *cls, | ||
923 | struct GNUNET_IDENTITY_Ego *ego, | ||
924 | void **ctx, | ||
925 | const char *name) | ||
926 | { | ||
927 | struct RequestHandle *handle = cls; | ||
928 | struct EgoEntry *ego_entry; | ||
929 | struct GNUNET_CRYPTO_EcdsaPublicKey pk; | ||
930 | |||
931 | if ((NULL == ego) && (NULL == handle->zone_pkey)) | ||
932 | { | ||
933 | handle->op = GNUNET_IDENTITY_get (handle->identity_handle, | ||
934 | GNUNET_REST_SUBSYSTEM_NAMESTORE, | ||
935 | &default_ego_cb, | ||
936 | handle); | ||
937 | } | ||
938 | if ((NULL == ego) && (ID_REST_STATE_INIT == handle->state)) | ||
939 | { | ||
940 | handle->state = ID_REST_STATE_POST_INIT; | ||
941 | init_cont (handle); | ||
942 | return; | ||
943 | } | ||
944 | if (ID_REST_STATE_INIT == handle->state) | ||
945 | { | ||
946 | ego_entry = GNUNET_new(struct EgoEntry); | ||
947 | GNUNET_IDENTITY_ego_get_public_key (ego, &pk); | ||
948 | ego_entry->keystring = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk); | ||
949 | ego_entry->ego = ego; | ||
950 | GNUNET_asprintf (&ego_entry->identifier, "%s", name); | ||
951 | GNUNET_CONTAINER_DLL_insert_tail(handle->ego_head, handle->ego_tail, | ||
952 | ego_entry); | ||
953 | } | ||
954 | |||
955 | } | ||
956 | |||
957 | |||
958 | /** | ||
959 | * Function processing the REST call | ||
960 | * | ||
961 | * @param method HTTP method | ||
962 | * @param url URL of the HTTP request | ||
963 | * @param data body of the HTTP request (optional) | ||
964 | * @param data_size length of the body | ||
965 | * @param proc callback function for the result | ||
966 | * @param proc_cls closure for callback function | ||
967 | * @return GNUNET_OK if request accepted | ||
968 | */ | ||
969 | static void | ||
970 | rest_process_request(struct GNUNET_REST_RequestHandle *rest_handle, | ||
971 | GNUNET_REST_ResultProcessor proc, | ||
972 | void *proc_cls) | ||
973 | { | ||
974 | struct RequestHandle *handle = GNUNET_new (struct RequestHandle); | ||
975 | |||
976 | handle->response_code = 0; | ||
977 | handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL; | ||
978 | handle->proc_cls = proc_cls; | ||
979 | handle->proc = proc; | ||
980 | handle->rest_handle = rest_handle; | ||
981 | handle->zone_pkey = NULL; | ||
982 | |||
983 | handle->url = GNUNET_strdup (rest_handle->url); | ||
984 | if (handle->url[strlen (handle->url)-1] == '/') | ||
985 | handle->url[strlen (handle->url)-1] = '\0'; | ||
986 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting...\n"); | ||
987 | |||
988 | handle->identity_handle = GNUNET_IDENTITY_connect (cfg, &id_connect_cb, handle); | ||
989 | handle->ns_handle = GNUNET_NAMESTORE_connect (cfg); | ||
990 | handle->timeout_task = | ||
991 | GNUNET_SCHEDULER_add_delayed (handle->timeout, | ||
992 | &do_error, | ||
993 | handle); | ||
994 | |||
995 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected\n"); | ||
996 | } | ||
997 | |||
998 | |||
999 | /** | ||
1000 | * Entry point for the plugin. | ||
1001 | * | ||
1002 | * @param cls Config info | ||
1003 | * @return NULL on error, otherwise the plugin context | ||
1004 | */ | ||
1005 | void * | ||
1006 | libgnunet_plugin_rest_namestore_init (void *cls) | ||
1007 | { | ||
1008 | static struct Plugin plugin; | ||
1009 | struct GNUNET_REST_Plugin *api; | ||
1010 | |||
1011 | cfg = cls; | ||
1012 | if (NULL != plugin.cfg) | ||
1013 | return NULL; /* can only initialize once! */ | ||
1014 | memset (&plugin, 0, sizeof (struct Plugin)); | ||
1015 | plugin.cfg = cfg; | ||
1016 | api = GNUNET_new (struct GNUNET_REST_Plugin); | ||
1017 | api->cls = &plugin; | ||
1018 | api->name = GNUNET_REST_API_NS_NAMESTORE; | ||
1019 | api->process_request = &rest_process_request; | ||
1020 | GNUNET_asprintf (&allow_methods, | ||
1021 | "%s, %s, %s, %s, %s", | ||
1022 | MHD_HTTP_METHOD_GET, | ||
1023 | MHD_HTTP_METHOD_POST, | ||
1024 | MHD_HTTP_METHOD_PUT, | ||
1025 | MHD_HTTP_METHOD_DELETE, | ||
1026 | MHD_HTTP_METHOD_OPTIONS); | ||
1027 | |||
1028 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1029 | _("Namestore REST API initialized\n")); | ||
1030 | return api; | ||
1031 | } | ||
1032 | |||
1033 | |||
1034 | /** | ||
1035 | * Exit point from the plugin. | ||
1036 | * | ||
1037 | * @param cls the plugin context (as returned by "init") | ||
1038 | * @return always NULL | ||
1039 | */ | ||
1040 | void * | ||
1041 | libgnunet_plugin_rest_namestore_done (void *cls) | ||
1042 | { | ||
1043 | struct GNUNET_REST_Plugin *api = cls; | ||
1044 | struct Plugin *plugin = api->cls; | ||
1045 | plugin->cfg = NULL; | ||
1046 | |||
1047 | GNUNET_free_non_null (allow_methods); | ||
1048 | GNUNET_free (api); | ||
1049 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1050 | "Namestore REST plugin is finished\n"); | ||
1051 | return NULL; | ||
1052 | } | ||
1053 | |||
1054 | /* end of plugin_rest_namestore.c */ | ||
1055 | |||
diff --git a/src/namestore/test_plugin_rest_namestore.sh b/src/namestore/test_plugin_rest_namestore.sh deleted file mode 100755 index de02dfafc..000000000 --- a/src/namestore/test_plugin_rest_namestore.sh +++ /dev/null | |||
@@ -1,208 +0,0 @@ | |||
1 | #!/usr/bin/bash | ||
2 | |||
3 | #First, start gnunet-arm and the rest-service. | ||
4 | #Exit 0 means success, exit 1 means failed test | ||
5 | |||
6 | namestore_link="http://localhost:7776/namestore" | ||
7 | wrong_link="http://localhost:7776/namestoreandmore" | ||
8 | |||
9 | |||
10 | curl_get () { | ||
11 | #$1 is link | ||
12 | #$2 is grep | ||
13 | cache="$(curl -v "$1" 2>&1 | grep "$2")" | ||
14 | #echo $cache | ||
15 | if [ "" == "$cache" ] | ||
16 | then | ||
17 | exit 1 | ||
18 | fi | ||
19 | } | ||
20 | |||
21 | curl_post () { | ||
22 | #$1 is link | ||
23 | #$2 is data | ||
24 | #$3 is grep | ||
25 | cache="$(curl -v -X "POST" "$1" --data "$2" 2>&1 | grep "$3")" | ||
26 | #echo $cache | ||
27 | if [ "" == "$cache" ] | ||
28 | then | ||
29 | exit 1 | ||
30 | fi | ||
31 | } | ||
32 | |||
33 | curl_delete () { | ||
34 | #$1 is link | ||
35 | #$2 is grep | ||
36 | cache="$(curl -v -X "DELETE" "$1" 2>&1 | grep "$2")" | ||
37 | #echo $cache | ||
38 | if [ "" == "$cache" ] | ||
39 | then | ||
40 | exit 1 | ||
41 | fi | ||
42 | } | ||
43 | |||
44 | # curl_put () { | ||
45 | # #$1 is link | ||
46 | # #$2 is data | ||
47 | # #$3 is grep | ||
48 | # cache="$(curl -v -X "PUT" "$1" --data "$2" 2>&1 | grep "$3")" | ||
49 | # #echo $cache | ||
50 | # if [ "" == "$cache" ] | ||
51 | # then | ||
52 | # exit 1 | ||
53 | # fi | ||
54 | # } | ||
55 | |||
56 | #Test subsystem default identity | ||
57 | |||
58 | #Test GET | ||
59 | gnunet-identity -D "test_plugin_rest_namestore" | ||
60 | gnunet-identity -C "test_plugin_rest_namestore" | ||
61 | test="$(gnunet-namestore -D -z "test_plugin_rest_namestore")" | ||
62 | name="test_plugin_rest_namestore" | ||
63 | public="$(gnunet-identity -d | grep "test_plugin_rest_namestore" | awk 'NR==1{print $3}')" | ||
64 | if [ "" == "$test" ] | ||
65 | then | ||
66 | #if no entries for test_plugin_rest_namestore | ||
67 | curl_get "${namestore_link}?name=$name" "error" | ||
68 | curl_get "${namestore_link}?name=" "error" | ||
69 | curl_get "${namestore_link}?name=$public" "error" | ||
70 | |||
71 | curl_get "${namestore_link}?pubkey=$public" "error" | ||
72 | curl_get "${namestore_link}?pubkey=$name" "error" | ||
73 | curl_get "${namestore_link}?pubkey=" "error" | ||
74 | else | ||
75 | #if entries exists (that should not be possible) | ||
76 | curl_get "${namestore_link}" "HTTP/1.1 200 OK" | ||
77 | curl_get "${namestore_link}?name=$name" "HTTP/1.1 200 OK" | ||
78 | curl_get "${namestore_link}?name=" "error" | ||
79 | curl_get "${namestore_link}?name=$public" "error" | ||
80 | |||
81 | curl_get "${namestore_link}?pubkey=$public" "HTTP/1.1 200 OK" | ||
82 | curl_get "${namestore_link}?pubkey=$name" "error" | ||
83 | curl_get "${namestore_link}?pubkey=" "error" | ||
84 | fi | ||
85 | gnunet-namestore -z $name -p -a -n "test_entry" -e "1d" -V "HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG" -t "PKEY" | ||
86 | curl_get "${namestore_link}" "HTTP/1.1 200 OK" | ||
87 | curl_get "${namestore_link}?name=$name" "HTTP/1.1 200 OK" | ||
88 | curl_get "${namestore_link}?name=" "error" | ||
89 | curl_get "${namestore_link}?name=$public" "error" | ||
90 | curl_get "${namestore_link}?pubkey=$public" "HTTP/1.1 200 OK" | ||
91 | curl_get "${namestore_link}?pubkey=$name" "error" | ||
92 | curl_get "${namestore_link}?pubkey=" "error" | ||
93 | gnunet-namestore -z $name -d -n "test_entry" | ||
94 | |||
95 | #Test POST with NAME | ||
96 | curl_post "${namestore_link}?name=$name" '{"value":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG", "type":"PKEY", "expiration_time":"1d","flag":0,"label":"test_entry"}' "HTTP/1.1 204 No Content" | ||
97 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
98 | #value | ||
99 | curl_post "${namestore_link}?name=$name" '{"value":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRGxxx", "type":"PKEY", "expiration_time":"1d","flag":0,"label":"test_entry"}' "error" | ||
100 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
101 | curl_post "${namestore_link}?name=$name" '{"value":"", "type":"PKEY", "expiration_time":"1d","flag":0,"label":"test_entry"}' "error" | ||
102 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
103 | curl_post "${namestore_link}?name=$name" '{"value_missing":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRGxxx", "type":"PKEY", "expiration_time":"1d","flag":0,"label":"test_entry"}' "error" | ||
104 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
105 | #time | ||
106 | curl_post "${namestore_link}?name=$name" '{"value":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG", "type":"PKEY", "expiration_time":"0d","flag":0,"label":"test_entry"}' "HTTP/1.1 204" | ||
107 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
108 | curl_post "${namestore_link}?name=$name" '{"value":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG", "type":"PKEY", "expiration_time":"10000d","flag":0,"label":"test_entry"}' "HTTP/1.1 204" | ||
109 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
110 | curl_post "${namestore_link}?name=$name" '{"value":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG", "type":"PKEY", "expiration_time":"now","flag":0,"label":"test_entry"}' "error" | ||
111 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
112 | curl_post "${namestore_link}?name=$name" '{"value":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG", "type":"PKEY", "expiration_time":"","flag":0,"label":"test_entry"}' "error" | ||
113 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
114 | curl_post "${namestore_link}?name=$name" '{"value":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG", "type":"PKEY", "expiration_time_missing":"1d","flag":0,"label":"test_entry"}' "error" | ||
115 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
116 | #flag | ||
117 | curl_post "${namestore_link}?name=$name" '{"value":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG", "type":"PKEY", "expiration_time":"1d","flag":0,"label":"test_entry"}' "HTTP/1.1 204 No Content" | ||
118 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
119 | curl_post "${namestore_link}?name=$name" '{"value":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG", "type":"PKEY", "expiration_time":"1d","flag":2,"label":"test_entry"}' "HTTP/1.1 204 No Content" | ||
120 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
121 | curl_post "${namestore_link}?name=$name" '{"value":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG", "type":"PKEY", "expiration_time":"1d","flag":8,"label":"test_entry"}' "HTTP/1.1 204 No Content" | ||
122 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
123 | curl_post "${namestore_link}?name=$name" '{"value":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG", "type":"PKEY", "expiration_time":"1d","flag":16,"label":"test_entry"}' "HTTP/1.1 204 No Content" | ||
124 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
125 | curl_post "${namestore_link}?name=$name" '{"value":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG", "type":"PKEY", "expiration_time":"1d","flag":-1,"label":"test_entry"}' "error" | ||
126 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
127 | curl_post "${namestore_link}?name=$name" '{"value":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG", "type":"PKEY", "expiration_time":"1d","flag":"Test","label":"test_entry"}' "error" | ||
128 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
129 | curl_post "${namestore_link}?name=$name" '{"value":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG", "type":"PKEY", "expiration_time":"1d","flag":,"label":"test_entry"}' "error" | ||
130 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
131 | curl_post "${namestore_link}?name=$name" '{"value":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG", "type":"PKEY", "expiration_time":"1d","flag_missing":0,"label":"test_entry"}' "error" | ||
132 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
133 | #label | ||
134 | curl_post "${namestore_link}?name=$name" '{"value":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG", "type":"PKEY", "expiration_time":"1d","flag":0,"label":"test_entry"}' "HTTP/1.1 204 No Content" | ||
135 | curl_post "${namestore_link}?name=$name" '{"value":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG", "type":"PKEY", "expiration_time":"1d","flag":0,"label":"test_entry"}' "HTTP/1.1 409" | ||
136 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
137 | curl_post "${namestore_link}?name=$name" '{"value":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG", "type":"PKEY", "expiration_time":"1d","flag":0,"label":""}' "error" | ||
138 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
139 | curl_post "${namestore_link}?name=$name" '{"value":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG", "type":"PKEY", "expiration_time":"1d","flag":0,"label_missing":"test_entry"}' "error" | ||
140 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
141 | |||
142 | #Test POST with PUBKEY | ||
143 | curl_post "${namestore_link}?pubkey=$public" '{"value":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG", "type":"PKEY", "expiration_time":"1d","flag":0,"label":"test_entry"}' "HTTP/1.1 204 No Content" | ||
144 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
145 | #value | ||
146 | curl_post "${namestore_link}?pubkey=$public" '{"value":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRGxxx", "type":"PKEY", "expiration_time":"1d","flag":0,"label":"test_entry"}' "error" | ||
147 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
148 | curl_post "${namestore_link}?pubkey=$public" '{"value":"", "type":"PKEY", "expiration_time":"1d","flag":0,"label":"test_entry"}' "error" | ||
149 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
150 | curl_post "${namestore_link}?pubkey=$public" '{"value_missing":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRGxxx", "type":"PKEY", "expiration_time":"1d","flag":0,"label":"test_entry"}' "error" | ||
151 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
152 | #time | ||
153 | curl_post "${namestore_link}?pubkey=$public" '{"value":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG", "type":"PKEY", "expiration_time":"0d","flag":0,"label":"test_entry"}' "HTTP/1.1 204" | ||
154 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
155 | curl_post "${namestore_link}?pubkey=$public" '{"value":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG", "type":"PKEY", "expiration_time":"10000d","flag":0,"label":"test_entry"}' "HTTP/1.1 204" | ||
156 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
157 | curl_post "${namestore_link}?pubkey=$public" '{"value":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG", "type":"PKEY", "expiration_time":"now","flag":0,"label":"test_entry"}' "error" | ||
158 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
159 | curl_post "${namestore_link}?pubkey=$public" '{"value":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG", "type":"PKEY", "expiration_time":"","flag":0,"label":"test_entry"}' "error" | ||
160 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
161 | curl_post "${namestore_link}?pubkey=$public" '{"value":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG", "type":"PKEY", "expiration_time_missing":"1d","flag":0,"label":"test_entry"}' "error" | ||
162 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
163 | #flag | ||
164 | curl_post "${namestore_link}?pubkey=$public" '{"value":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG", "type":"PKEY", "expiration_time":"1d","flag":0,"label":"test_entry"}' "HTTP/1.1 204 No Content" | ||
165 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
166 | curl_post "${namestore_link}?pubkey=$public" '{"value":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG", "type":"PKEY", "expiration_time":"1d","flag":2,"label":"test_entry"}' "HTTP/1.1 204 No Content" | ||
167 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
168 | curl_post "${namestore_link}?pubkey=$public" '{"value":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG", "type":"PKEY", "expiration_time":"1d","flag":8,"label":"test_entry"}' "HTTP/1.1 204 No Content" | ||
169 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
170 | curl_post "${namestore_link}?pubkey=$public" '{"value":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG", "type":"PKEY", "expiration_time":"1d","flag":16,"label":"test_entry"}' "HTTP/1.1 204 No Content" | ||
171 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
172 | curl_post "${namestore_link}?pubkey=$public" '{"value":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG", "type":"PKEY", "expiration_time":"1d","flag":-1,"label":"test_entry"}' "error" | ||
173 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
174 | curl_post "${namestore_link}?pubkey=$public" '{"value":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG", "type":"PKEY", "expiration_time":"1d","flag":"Test","label":"test_entry"}' "error" | ||
175 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
176 | curl_post "${namestore_link}?pubkey=$public" '{"value":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG", "type":"PKEY", "expiration_time":"1d","flag":,"label":"test_entry"}' "error" | ||
177 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
178 | curl_post "${namestore_link}?pubkey=$public" '{"value":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG", "type":"PKEY", "expiration_time":"1d","flag_missing":0,"label":"test_entry"}' "error" | ||
179 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
180 | #label | ||
181 | curl_post "${namestore_link}?pubkey=$public" '{"value":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG", "type":"PKEY", "expiration_time":"1d","flag":0,"label":"test_entry"}' "HTTP/1.1 204 No Content" | ||
182 | curl_post "${namestore_link}?pubkey=$public" '{"value":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG", "type":"PKEY", "expiration_time":"1d","flag":0,"label":"test_entry"}' "HTTP/1.1 409" | ||
183 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
184 | curl_post "${namestore_link}?pubkey=$public" '{"value":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG", "type":"PKEY", "expiration_time":"1d","flag":0,"label":""}' "error" | ||
185 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
186 | curl_post "${namestore_link}?pubkey=$public" '{"value":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG", "type":"PKEY", "expiration_time":"1d","flag":0,"label_missing":"test_entry"}' "error" | ||
187 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
188 | |||
189 | #wrong zone | ||
190 | curl_post "${namestore_link}?name=$public" '{"value":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG", "type":"PKEY", "expiration_time":"1d","flag":0,"label":"test_entry"}' "error" | ||
191 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
192 | curl_post "${namestore_link}?pubkey=$name" '{"value":"HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG", "type":"PKEY", "expiration_time":"1d","flag":0,"label":"test_entry"}' "error" | ||
193 | gnunet-namestore -z $name -d -n "test_entry" > /dev/null 2>&1 | ||
194 | |||
195 | #Test DELETE | ||
196 | gnunet-namestore -z $name -p -a -n "test_entry" -e "1d" -V "HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG" -t "PKEY" | ||
197 | curl_delete "${namestore_link}?label=test_entry&name=$name" "HTTP/1.1 204" | ||
198 | gnunet-namestore -z $name -p -a -n "test_entry" -e "1d" -V "HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG" -t "PKEY" | ||
199 | curl_delete "${namestore_link}?label=test_entry&pubkey=$public" "HTTP/1.1 204" | ||
200 | gnunet-namestore -z $name -p -a -n "test_entry" -e "1d" -V "HVX38H2CB7WJM0WCPWT9CFX6GASMYJVR65RN75SJSSKAYVYXHMRG" -t "PKEY" | ||
201 | curl_delete "${namestore_link}?label=test_entry&pubkey=$name" "HTTP/1.1 404" | ||
202 | |||
203 | |||
204 | #Test default identity | ||
205 | #not possible without defining | ||
206 | |||
207 | exit 0; | ||
208 | |||