#include "microhttpd.h"
#include <jansson.h>
-#define API_NAMESPACE "/identity"
-
-#define EGO_NAMESPACE "/identity/egos"
+#define GNUNET_REST_API_NS_IDENTITY "/identity"
#define ID_REST_STATE_INIT 0
struct EgoEntry *ego_tmp;
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Cleaning up\n");
- if (NULL != handle->json_root)
- json_decref (handle->json_root);
if (NULL != handle->name)
GNUNET_free (handle->name);
if (NULL != handle->timeout_task)
struct JsonApiObject *json_object;
struct JsonApiResource *json_resource;
json_t *key_str;
-
- if (GNUNET_NO == GNUNET_REST_namespace_match (handle->url, EGO_NAMESPACE))
+ if (GNUNET_NO == GNUNET_REST_namespace_match (handle->url, GNUNET_REST_API_NS_IDENTITY))
{
resp = GNUNET_REST_create_json_response (NULL);
handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST);
cleanup_handle (handle);
- GNUNET_break (0);
return;
}
- if ( (strlen (EGO_NAMESPACE) == strlen (handle->url) )) {
+ if ( (strlen (GNUNET_REST_API_NS_IDENTITY) == strlen (handle->url) )) {
GNUNET_CRYPTO_hash (URL_PARAM_SUBSYS, strlen (URL_PARAM_SUBSYS), &key);
if ( GNUNET_YES ==
GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
}
}
json_object = GNUNET_REST_jsonapi_object_new ();
- egoname = &handle->url[strlen (EGO_NAMESPACE)+1];
+ egoname = &handle->url[strlen (GNUNET_REST_API_NS_IDENTITY)+1];
- if (strlen (EGO_NAMESPACE) == strlen (handle->url))
+ if (strlen (GNUNET_REST_API_NS_IDENTITY) == strlen (handle->url))
{
egoname = NULL;
}
-
//Return all egos
for (ego_entry = handle->ego_head;
NULL != ego_entry;
GNUNET_free (keystring);
GNUNET_REST_jsonapi_object_resource_add (json_object, json_resource);
}
-
+ if (0 == GNUNET_REST_jsonapi_object_resource_count (json_object))
+ {
+ GNUNET_REST_jsonapi_object_delete (json_object);
+ GNUNET_SCHEDULER_add_now (&do_error, handle);
+ return;
+ }
GNUNET_REST_jsonapi_data_serialize (json_object, &result_str);
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str);
resp = GNUNET_REST_create_json_response (result_str);
char term_data[handle->data_size+1];
const char* egoname;
- if (strlen (API_NAMESPACE) != strlen (handle->url))
+ if (strlen (GNUNET_REST_API_NS_IDENTITY) != strlen (handle->url))
{
GNUNET_SCHEDULER_add_now (&do_error, handle);
return;
int ego_exists = GNUNET_NO;
json_t *subsys_json;
- if (strlen (API_NAMESPACE) > strlen (handle->url))
+ if (strlen (GNUNET_REST_API_NS_IDENTITY) > strlen (handle->url))
{
GNUNET_SCHEDULER_add_now (&do_error, handle);
return;
}
- egoname = &handle->url[strlen(EGO_NAMESPACE)+1];
+ egoname = &handle->url[strlen(GNUNET_REST_API_NS_IDENTITY)+1];
for (ego_entry = handle->ego_head;
NULL != ego_entry;
ego_entry = ego_entry->next)
struct RequestHandle *handle = cls;
int ego_exists = GNUNET_NO;
- if (strlen (API_NAMESPACE) >= strlen (handle->url))
+ if (strlen (GNUNET_REST_API_NS_IDENTITY) >= strlen (handle->url))
{
GNUNET_SCHEDULER_add_now (&do_error, handle);
return;
}
- egoname = &handle->url[strlen(API_NAMESPACE)+1];
+ egoname = &handle->url[strlen(GNUNET_REST_API_NS_IDENTITY)+1];
for (ego_entry = handle->ego_head;
NULL != ego_entry;
ego_entry = ego_entry->next)
init_cont (struct RequestHandle *handle)
{
static const struct GNUNET_REST_RestConnectionHandler handlers[] = {
- {MHD_HTTP_METHOD_GET, API_NAMESPACE, &ego_info_response},
- {MHD_HTTP_METHOD_POST, API_NAMESPACE, &ego_create_cont},
- {MHD_HTTP_METHOD_PUT, API_NAMESPACE, &subsys_set_cont},
- {MHD_HTTP_METHOD_DELETE, API_NAMESPACE, &ego_delete_cont},
+ {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_IDENTITY, &ego_info_response},
+ {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_IDENTITY, &ego_create_cont},
+ {MHD_HTTP_METHOD_PUT, GNUNET_REST_API_NS_IDENTITY, &subsys_set_cont},
+ {MHD_HTTP_METHOD_DELETE, GNUNET_REST_API_NS_IDENTITY, &ego_delete_cont},
GNUNET_REST_HANDLER_END
};
plugin.cfg = cfg;
api = GNUNET_new (struct GNUNET_REST_Plugin);
api->cls = &plugin;
- api->name = API_NAMESPACE;
+ api->name = GNUNET_REST_API_NS_IDENTITY;
api->process_request = &rest_identity_process_request;
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
_("Identity REST API initialized\n"));
return NULL;
res = GNUNET_new (struct JsonApiResource);
+ res->prev = NULL;
+ res->next = NULL;
res->res_obj = json_object ();
return;
res = GNUNET_new (struct JsonApiResource);
+ res->next = NULL;
+ res->prev = NULL;
res->res_obj = json_deep_copy (res_json);
GNUNET_REST_jsonapi_object_resource_add (obj, res);
}
GNUNET_REST_jsonapi_object_delete (struct JsonApiObject *resp)
{
struct JsonApiResource *res;
+ struct JsonApiResource *res_next;
for (res = resp->res_list_head;
- res != NULL;
- res = res->next)
+ res != NULL;)
+ {
+ GNUNET_CONTAINER_DLL_remove (resp->res_list_head,
+ resp->res_list_tail,
+ res);
+ res_next = res->next;
GNUNET_REST_jsonapi_resource_delete (res);
+ res = res_next;
+ }
GNUNET_free (resp);
}
}
json_object_set (root_json, GNUNET_REST_JSONAPI_KEY_DATA, res_arr);
}
- *result = json_dumps (root_json, JSON_COMPACT);
+ *result = json_dumps (root_json, JSON_INDENT(2));
return GNUNET_OK;
}
* @param data JSON result
* @retun MHD response
*/
- struct MHD_Response*
+struct MHD_Response*
GNUNET_REST_create_json_response (const char *data)
{
struct MHD_Response *resp;
size_t len;
if (NULL == data)
+ {
len = 0;
+ data = "";
+ }
else
len = strlen (data);
resp = MHD_create_response_from_buffer (len,
char *url;
count = 0;
-
while (NULL != handlers[count].method)
count++;
GNUNET_asprintf (&url, "%s", conn->url);
if (url[strlen (url)-1] == '/')
url[strlen (url)-1] = '\0';
-
for (i = 0; i < count; i++)
{
- if (0 != strcasecmp (conn->method, handlers[count].method))
+ if (0 != strcasecmp (conn->method, handlers[i].method))
break;
- if (strlen (url) < strlen (handlers[count].namespace))
+ if (strlen (url) < strlen (handlers[i].namespace))
break;
- if (GNUNET_NO == GNUNET_REST_namespace_match (url, handlers[count].namespace))
+ if (GNUNET_NO == GNUNET_REST_namespace_match (url, handlers[i].namespace))
break;
//Match
- handlers[count].proc (conn, (const char*)url, cls);
+ handlers[i].proc (conn, (const char*)url, cls);
GNUNET_free (url);
return GNUNET_YES;
}