From 493305a0d4e9d7f9bdc35fabfd8027a487586e47 Mon Sep 17 00:00:00 2001 From: Martin Schanzenbach Date: Wed, 4 May 2016 09:44:35 +0000 Subject: - rework rest/jsonapi API; bugfixes --- src/gns/gnunet-gns-proxy.c | 4 +- src/gns/plugin_rest_gns.c | 30 ++++++++-- src/identity-provider/identity_token.c | 9 ++- .../plugin_rest_identity_provider.c | 29 +++++---- src/identity/plugin_rest_identity.c | 31 ++++++---- src/include/gnunet_jsonapi_lib.h | 37 ++++++++++++ src/include/gnunet_rest_lib.h | 16 +++-- src/include/gnunet_rest_plugin.h | 2 +- src/jsonapi/Makefile.am | 1 + src/jsonapi/jsonapi.c | 54 +++++++++++++++++ src/namestore/plugin_rest_namestore.c | 68 +++++++++++----------- src/rest/gnunet-rest-server.c | 8 +-- src/rest/rest.c | 6 +- 13 files changed, 218 insertions(+), 77 deletions(-) diff --git a/src/gns/gnunet-gns-proxy.c b/src/gns/gnunet-gns-proxy.c index 6793d6f5c..7ab8843bb 100644 --- a/src/gns/gnunet-gns-proxy.c +++ b/src/gns/gnunet-gns-proxy.c @@ -2522,7 +2522,7 @@ do_s5r_read (void *cls) s5r->sock, &do_s5r_read, s5r); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Processing %u bytes of socks data in state %d\n", + "Processing %zu bytes of socks data in state %d\n", s5r->rbuf_len, s5r->state); switch (s5r->state) @@ -2943,7 +2943,7 @@ run_cont () return; } GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Proxy listens on port %u\n", + "Proxy listens on port %lu\n", port); /* start MHD daemon for HTTP */ diff --git a/src/gns/plugin_rest_gns.c b/src/gns/plugin_rest_gns.c index 91a091f2e..718a9b29b 100644 --- a/src/gns/plugin_rest_gns.c +++ b/src/gns/plugin_rest_gns.c @@ -76,6 +76,11 @@ struct LookupHandle */ struct GNUNET_GNS_LookupRequest *lookup_request; + /** + * Handle to rest request + */ + struct GNUNET_REST_RequestHandle *rest_handle; + /** * Lookup an ego with the identity service. */ @@ -153,6 +158,11 @@ struct LookupHandle */ struct GNUNET_CRYPTO_EcdsaPrivateKey shorten_key; + /** + * HTTP response code + */ + int response_code; + }; @@ -218,7 +228,7 @@ do_error (void *cls) struct MHD_Response *resp; resp = GNUNET_REST_create_json_response (NULL); - handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST); + handle->proc (handle->proc_cls, resp, handle->response_code); cleanup_handle (handle); } @@ -505,7 +515,7 @@ parse_url (const char *url, struct LookupHandle *handle) } static void -get_gns_cont (struct RestConnectionDataHandle *conndata_handle, +get_gns_cont (struct GNUNET_REST_RequestHandle *conndata_handle, const char* url, void *cls) { @@ -621,7 +631,7 @@ get_gns_cont (struct RestConnectionDataHandle *conndata_handle, * @param handle the lookup handle */ static void -options_cont (struct RestConnectionDataHandle *con_handle, +options_cont (struct GNUNET_REST_RequestHandle *con_handle, const char* url, void *cls) { @@ -652,24 +662,32 @@ options_cont (struct RestConnectionDataHandle *con_handle, * @return GNUNET_OK if request accepted */ static void -rest_gns_process_request(struct RestConnectionDataHandle *conndata_handle, +rest_gns_process_request(struct GNUNET_REST_RequestHandle *conndata_handle, GNUNET_REST_ResultProcessor proc, void *proc_cls) { struct LookupHandle *handle = GNUNET_new (struct LookupHandle); + struct GNUNET_REST_RequestHandlerError err; handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL; handle->proc_cls = proc_cls; handle->proc = proc; + handle->rest_handle = conndata_handle; - static const struct GNUNET_REST_RestConnectionHandler handlers[] = { + static const struct GNUNET_REST_RequestHandler handlers[] = { {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_GNS, &get_gns_cont}, {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_GNS, &options_cont}, GNUNET_REST_HANDLER_END }; - if (GNUNET_NO == GNUNET_REST_handle_request (conndata_handle, handlers, handle)) + if (GNUNET_NO == GNUNET_JSONAPI_handle_request (conndata_handle, + handlers, + &err, + handle)) + { + handle->response_code = err.error_code; GNUNET_SCHEDULER_add_now (&do_error, handle); + } } diff --git a/src/identity-provider/identity_token.c b/src/identity-provider/identity_token.c index 3bed6962e..8a90138ec 100644 --- a/src/identity-provider/identity_token.c +++ b/src/identity-provider/identity_token.c @@ -98,7 +98,10 @@ decrypt_str_ecdhe (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key, &enc_key, &enc_iv, str_buf); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Decrypted bytes: %d Expected bytes: %d\n", str_size, cyphertext_len); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Decrypted bytes: %zd Expected bytes: %zd\n", + str_size, + cyphertext_len); if (-1 == str_size) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ECDH invalid\n"); @@ -172,14 +175,14 @@ encrypt_str_ecdhe (const char *plaintext, pub_key, &new_key_hash)); create_sym_key_from_ecdh(&new_key_hash, &skey, &iv); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Encrypting string %s\n (len=%d)", + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Encrypting string %s\n (len=%zd)", plaintext, strlen (plaintext)); enc_size = GNUNET_CRYPTO_symmetric_encrypt (plaintext, strlen (plaintext), &skey, &iv, *cyphertext); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Encrypted (len=%d)", enc_size); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Encrypted (len=%zd)", enc_size); return GNUNET_OK; } diff --git a/src/identity-provider/plugin_rest_identity_provider.c b/src/identity-provider/plugin_rest_identity_provider.c index 1f03713ea..1e1878cad 100644 --- a/src/identity-provider/plugin_rest_identity_provider.c +++ b/src/identity-provider/plugin_rest_identity_provider.c @@ -199,7 +199,7 @@ struct RequestHandle /** * Handle to the rest connection */ - struct RestConnectionDataHandle *conndata_handle; + struct GNUNET_REST_RequestHandle *conndata_handle; /** * The processing state @@ -271,6 +271,11 @@ struct RequestHandle */ char *emsg; + /** + * Reponse code + */ + int response_code; + /** * Response object */ @@ -337,7 +342,7 @@ do_error (void *cls) "{Error while processing request: %s}", handle->emsg); resp = GNUNET_REST_create_json_response (json_error); - handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST); + handle->proc (handle->proc_cls, resp, handle->response_code); cleanup_handle (handle); GNUNET_free (json_error); } @@ -434,7 +439,7 @@ token_creat_cont (void *cls, * @param cls the request handle */ static void -issue_token_cont (struct RestConnectionDataHandle *con, +issue_token_cont (struct GNUNET_REST_RequestHandle *con, const char *url, void *cls) { @@ -730,7 +735,7 @@ token_collect (void *cls, * @param cls the RequestHandle */ static void -list_token_cont (struct RestConnectionDataHandle *con_handle, +list_token_cont (struct GNUNET_REST_RequestHandle *con_handle, const char* url, void *cls) { @@ -919,7 +924,7 @@ exchange_token_ticket_cb (void *cls, * @param cls the RequestHandle */ static void -exchange_token_ticket_cont (struct RestConnectionDataHandle *con_handle, +exchange_token_ticket_cont (struct GNUNET_REST_RequestHandle *con_handle, const char* url, void *cls) { @@ -940,7 +945,7 @@ exchange_token_ticket_cont (struct RestConnectionDataHandle *con_handle, * @param cls the RequestHandle */ static void -options_cont (struct RestConnectionDataHandle *con_handle, +options_cont (struct GNUNET_REST_RequestHandle *con_handle, const char* url, void *cls) { @@ -965,7 +970,8 @@ options_cont (struct RestConnectionDataHandle *con_handle, static void init_cont (struct RequestHandle *handle) { - static const struct GNUNET_REST_RestConnectionHandler handlers[] = { + struct GNUNET_REST_RequestHandlerError err; + static const struct GNUNET_REST_RequestHandler handlers[] = { {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_IDENTITY_TOKEN_ISSUE, &issue_token_cont}, //{MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_IDENTITY_TOKEN_CHECK, &check_token_cont}, {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_IDENTITY_PROVIDER, &list_token_cont}, @@ -974,9 +980,12 @@ init_cont (struct RequestHandle *handle) GNUNET_REST_HANDLER_END }; - if (GNUNET_NO == GNUNET_REST_handle_request (handle->conndata_handle, handlers, handle)) + if (GNUNET_NO == GNUNET_REST_handle_request (handle->conndata_handle, + handlers, + &err, + handle)) { - handle->emsg = GNUNET_strdup ("Request unsupported"); + handle->response_code = err.error_code; GNUNET_SCHEDULER_add_now (&do_error, handle); } } @@ -1054,7 +1063,7 @@ list_ego (void *cls, * @return GNUNET_OK if request accepted */ static void -rest_identity_process_request(struct RestConnectionDataHandle *conndata_handle, +rest_identity_process_request(struct GNUNET_REST_RequestHandle *conndata_handle, GNUNET_REST_ResultProcessor proc, void *proc_cls) { diff --git a/src/identity/plugin_rest_identity.c b/src/identity/plugin_rest_identity.c index 10836f103..4a2429a9c 100644 --- a/src/identity/plugin_rest_identity.c +++ b/src/identity/plugin_rest_identity.c @@ -146,7 +146,12 @@ struct RequestHandle /** * Handle to the rest connection */ - struct RestConnectionDataHandle *conndata_handle; + struct GNUNET_REST_RequestHandle *conndata_handle; + + /** + * response code + */ + int response_code; /** * The processing state @@ -276,7 +281,7 @@ do_error (void *cls) resp = GNUNET_REST_create_json_response (json_error); handle->proc (handle->proc_cls, resp, - MHD_HTTP_BAD_REQUEST); + handle->response_code); cleanup_handle (handle); GNUNET_free (json_error); } @@ -348,7 +353,7 @@ get_ego_for_subsys (void *cls, * @param cls the request handle */ static void -ego_info_response (struct RestConnectionDataHandle *con, +ego_info_response (struct GNUNET_REST_RequestHandle *con, const char *url, void *cls) { @@ -477,7 +482,7 @@ do_finished (void *cls, const char *emsg) * @param cls request handle */ static void -ego_create_cont (struct RestConnectionDataHandle *con, +ego_create_cont (struct GNUNET_REST_RequestHandle *con, const char *url, void *cls) { @@ -566,7 +571,7 @@ ego_create_cont (struct RestConnectionDataHandle *con, * @param cls the RequestHandle */ static void -ego_edit_cont (struct RestConnectionDataHandle *con, +ego_edit_cont (struct GNUNET_REST_RequestHandle *con, const char *url, void *cls) { @@ -696,7 +701,7 @@ ego_edit_cont (struct RestConnectionDataHandle *con, } void -ego_delete_cont (struct RestConnectionDataHandle *con_handle, +ego_delete_cont (struct GNUNET_REST_RequestHandle *con_handle, const char* url, void *cls) { @@ -746,7 +751,7 @@ ego_delete_cont (struct RestConnectionDataHandle *con_handle, * @param cls the RequestHandle */ static void -options_cont (struct RestConnectionDataHandle *con_handle, +options_cont (struct GNUNET_REST_RequestHandle *con_handle, const char* url, void *cls) { @@ -771,7 +776,8 @@ options_cont (struct RestConnectionDataHandle *con_handle, static void init_cont (struct RequestHandle *handle) { - static const struct GNUNET_REST_RestConnectionHandler handlers[] = { + struct GNUNET_REST_RequestHandlerError err; + static const struct GNUNET_REST_RequestHandler handlers[] = { {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, &ego_edit_cont}, @@ -780,9 +786,12 @@ init_cont (struct RequestHandle *handle) GNUNET_REST_HANDLER_END }; - if (GNUNET_NO == GNUNET_REST_handle_request (handle->conndata_handle, handlers, handle)) + if (GNUNET_NO == GNUNET_JSONAPI_handle_request (handle->conndata_handle, + handlers, + &err, + handle)) { - handle->emsg = GNUNET_strdup ("Request unsupported"); + handle->response_code = err.error_code; GNUNET_SCHEDULER_add_now (&do_error, handle); } } @@ -860,7 +869,7 @@ list_ego (void *cls, * @return GNUNET_OK if request accepted */ static void -rest_identity_process_request(struct RestConnectionDataHandle *conndata_handle, +rest_identity_process_request(struct GNUNET_REST_RequestHandle *conndata_handle, GNUNET_REST_ResultProcessor proc, void *proc_cls) { diff --git a/src/include/gnunet_jsonapi_lib.h b/src/include/gnunet_jsonapi_lib.h index 1e540f6b6..d4556c174 100644 --- a/src/include/gnunet_jsonapi_lib.h +++ b/src/include/gnunet_jsonapi_lib.h @@ -22,6 +22,7 @@ #define GNUNET_JSONAPI_LIB_H #include "gnunet_util_lib.h" +#include "gnunet_rest_lib.h" #include "gnunet_json_lib.h" @@ -200,5 +201,41 @@ GNUNET_JSONAPI_data_serialize (const struct GNUNET_JSONAPI_Object *resp, */ json_t* GNUNET_JSONAPI_resource_get_id (const struct GNUNET_JSONAPI_Resource *resource); + + /* end of gnunet_jsonapi_lib.h */ + +/** + * Check rest request for validity + * + * @param req handle to the request + * @return GNUNET_OK if valid + */ +int +GNUNET_JSONAPI_check_request_acceptable (struct GNUNET_REST_RequestHandle *req); + +/** + * Check rest request for validity + * + * @param req handle to the request + * @return GNUNET_OK if valid + */ +int +GNUNET_JSONAPI_check_request_supported (struct GNUNET_REST_RequestHandle *req); + + +/** + * Handle jsonapi rest request. Checks request headers for jsonapi compliance + * + * @param req rest request handle + * @param handler rest request handlers + * @param cls closure + * @return GNUNET_OK if successful + */ +int +GNUNET_JSONAPI_handle_request (struct GNUNET_REST_RequestHandle *req, + const struct GNUNET_REST_RequestHandler *handlers, + struct GNUNET_REST_RequestHandlerError *err, + void *cls); + #endif diff --git a/src/include/gnunet_rest_lib.h b/src/include/gnunet_rest_lib.h index ea254cc9b..ecd825f3f 100644 --- a/src/include/gnunet_rest_lib.h +++ b/src/include/gnunet_rest_lib.h @@ -37,17 +37,22 @@ #define GNUNET_REST_HANDLER_END {NULL, NULL, NULL} -struct RestConnectionDataHandle +struct GNUNET_REST_RequestHandle { struct GNUNET_CONTAINER_MultiHashMap *url_param_map; const char *method; const char *url; const char *data; size_t data_size; +}; +struct GNUNET_REST_RequestHandlerError +{ + int error_code; + char* error_text; }; -struct GNUNET_REST_RestConnectionHandler +struct GNUNET_REST_RequestHandler { /** * Http method to handle @@ -62,7 +67,7 @@ struct GNUNET_REST_RestConnectionHandler /** * callback handler */ - void (*proc) (struct RestConnectionDataHandle *handle, + void (*proc) (struct GNUNET_REST_RequestHandle *handle, const char *url, void *cls); @@ -101,8 +106,9 @@ GNUNET_REST_create_json_response (const char *data); int -GNUNET_REST_handle_request (struct RestConnectionDataHandle *conn, - const struct GNUNET_REST_RestConnectionHandler *handlers, +GNUNET_REST_handle_request (struct GNUNET_REST_RequestHandle *conn, + const struct GNUNET_REST_RequestHandler *handlers, + struct GNUNET_REST_RequestHandlerError *err, void *cls); diff --git a/src/include/gnunet_rest_plugin.h b/src/include/gnunet_rest_plugin.h index 6685cdec2..ecd5f66f1 100644 --- a/src/include/gnunet_rest_plugin.h +++ b/src/include/gnunet_rest_plugin.h @@ -71,7 +71,7 @@ struct GNUNET_REST_Plugin * @param proc the callback for result * @param proc_cls closure for callback */ - void (*process_request) (struct RestConnectionDataHandle *handle, + void (*process_request) (struct GNUNET_REST_RequestHandle *handle, GNUNET_REST_ResultProcessor proc, void *proc_cls); diff --git a/src/jsonapi/Makefile.am b/src/jsonapi/Makefile.am index 8a702440d..bcd4172c6 100644 --- a/src/jsonapi/Makefile.am +++ b/src/jsonapi/Makefile.am @@ -17,6 +17,7 @@ libgnunetjsonapi_la_SOURCES = \ libgnunetjsonapi_la_LIBADD = \ $(top_builddir)/src/util/libgnunetutil.la \ $(top_builddir)/src/json/libgnunetjson.la \ + $(top_builddir)/src/rest/libgnunetrest.la \ -ljansson \ $(XLIB) diff --git a/src/jsonapi/jsonapi.c b/src/jsonapi/jsonapi.c index 6bd03e0ec..b648590e5 100644 --- a/src/jsonapi/jsonapi.c +++ b/src/jsonapi/jsonapi.c @@ -20,6 +20,7 @@ */ #include "platform.h" #include "gnunet_json_lib.h" +#include "gnunet_rest_lib.h" #define GNUNET_JSONAPI_KEY_DATA "data" @@ -476,3 +477,56 @@ GNUNET_JSON_spec_jsonapi (struct GNUNET_JSONAPI_Object **jsonapi_object) *jsonapi_object = NULL; return ret; } + +/** + * Check rest request for validity + * + * @param req handle to the request + * @return GNUNET_OK if valid + */ +int +GNUNET_JSONAPI_check_request_acceptable (struct GNUNET_REST_RequestHandle *req) +{ + //TODO + return GNUNET_OK; +} + +/** + * Check rest request for validity + * + * @param req handle to the request + * @return GNUNET_OK if valid + */ +int +GNUNET_JSONAPI_check_request_supported (struct GNUNET_REST_RequestHandle *req) +{ + //TODO + return GNUNET_OK; +} + +/** + * Handle jsonapi rest request. Checks request headers for jsonapi compliance + * + * @param req rest request handle + * @param handler rest request handlers + * @param cls closure + * @return GNUNET_OK if successful + */ +int +GNUNET_JSONAPI_handle_request (struct GNUNET_REST_RequestHandle *handle, + const struct GNUNET_REST_RequestHandler *handlers, + struct GNUNET_REST_RequestHandlerError *err, + void *cls) +{ + if (GNUNET_OK != GNUNET_JSONAPI_check_request_acceptable (handle)) + { + err->error_code = MHD_HTTP_NOT_ACCEPTABLE; + return GNUNET_SYSERR; + } + if (GNUNET_OK != GNUNET_JSONAPI_check_request_supported (handle)) + { + err->error_code = MHD_HTTP_UNSUPPORTED_MEDIA_TYPE; + return GNUNET_SYSERR; + } + return GNUNET_REST_handle_request (handle, handlers, err, cls); +} diff --git a/src/namestore/plugin_rest_namestore.c b/src/namestore/plugin_rest_namestore.c index c47a6a3c8..76a1126a5 100644 --- a/src/namestore/plugin_rest_namestore.c +++ b/src/namestore/plugin_rest_namestore.c @@ -111,7 +111,7 @@ struct RequestHandle /** * Rest connection */ - struct RestConnectionDataHandle *conndata_handle; + struct GNUNET_REST_RequestHandle *rest_handle; /** * Handle to GNS service. @@ -224,19 +224,14 @@ struct RequestHandle char *url; /** - * The data from the REST request + * Cfg */ - const char* data; + const struct GNUNET_CONFIGURATION_Handle *cfg; /** - * the length of the REST data + * HTTP response code */ - size_t data_size; - - /** - * Cfg - */ - const struct GNUNET_CONFIGURATION_Handle *cfg; + int response_code; }; @@ -364,7 +359,7 @@ do_error (void *cls) struct RequestHandle *handle = cls; struct MHD_Response *resp = GNUNET_REST_create_json_response (NULL); - handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST); + handle->proc (handle->proc_cls, resp, handle->response_code); cleanup_handle (handle); } @@ -596,7 +591,7 @@ del_cont (void *cls, } static void -namestore_delete_cont (struct RestConnectionDataHandle *con, +namestore_delete_cont (struct GNUNET_REST_RequestHandle *con, const char *url, void *cls) { @@ -718,7 +713,7 @@ json_to_gnsrecord (const json_t *records_json, } static void -namestore_create_cont (struct RestConnectionDataHandle *con, +namestore_create_cont (struct GNUNET_REST_RequestHandle *con, const char *url, void *cls) { @@ -728,7 +723,7 @@ namestore_create_cont (struct RestConnectionDataHandle *con, struct GNUNET_JSONAPI_Resource *json_res; json_t *name_json; json_t *records_json; - char term_data[handle->data_size+1]; + char term_data[handle->rest_handle->data_size+1]; if (strlen (GNUNET_REST_API_NS_NAMESTORE) != strlen (handle->url)) { @@ -737,13 +732,15 @@ namestore_create_cont (struct RestConnectionDataHandle *con, GNUNET_SCHEDULER_add_now (&do_error, handle); return; } - if (0 >= handle->data_size) + if (0 >= handle->rest_handle->data_size) { GNUNET_SCHEDULER_add_now (&do_error, handle); return; } - term_data[handle->data_size] = '\0'; - memcpy (term_data, handle->data, handle->data_size); + term_data[handle->rest_handle->data_size] = '\0'; + memcpy (term_data, + handle->rest_handle->data, + handle->rest_handle->data_size); GNUNET_assert (GNUNET_OK == GNUNET_JSONAPI_object_parse (term_data, &json_obj)); if (NULL == json_obj) @@ -853,7 +850,7 @@ namestore_zkey_response (void *cls, } static void -namestore_zkey_cont (struct RestConnectionDataHandle *con, +namestore_zkey_cont (struct GNUNET_REST_RequestHandle *con, const char *url, void *cls) { @@ -865,7 +862,7 @@ namestore_zkey_cont (struct RestConnectionDataHandle *con, strlen (GNUNET_REST_JSONAPI_NAMESTORE_ZKEY), &key); if ( GNUNET_NO == - GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map, + GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle->url_param_map, &key) ) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, @@ -873,7 +870,7 @@ namestore_zkey_cont (struct RestConnectionDataHandle *con, GNUNET_SCHEDULER_add_now (&do_error, handle); return; } - handle->zkey_str = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map, + handle->zkey_str = GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->url_param_map, &key); if (GNUNET_OK != GNUNET_CRYPTO_ecdsa_public_key_from_string (handle->zkey_str, @@ -893,7 +890,7 @@ namestore_zkey_cont (struct RestConnectionDataHandle *con, } static void -namestore_info_cont (struct RestConnectionDataHandle *con, +namestore_info_cont (struct GNUNET_REST_RequestHandle *con, const char *url, void *cls) { @@ -920,7 +917,7 @@ get_name_from_url (const char* url) * @param cls the RequestHandle */ static void -options_cont (struct RestConnectionDataHandle *con_handle, +options_cont (struct GNUNET_REST_RequestHandle *con_handle, const char* url, void *cls) { @@ -950,7 +947,8 @@ testservice_task (void *cls, int result) { struct RequestHandle *handle = cls; - static const struct GNUNET_REST_RestConnectionHandler handlers[] = { + struct GNUNET_REST_RequestHandlerError err; + static const struct GNUNET_REST_RequestHandler handlers[] = { {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_NAMESTORE_ZKEY, &namestore_zkey_cont}, //reverse {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_NAMESTORE, &namestore_info_cont}, //list {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_NAMESTORE, &namestore_create_cont}, //create @@ -976,8 +974,14 @@ testservice_task (void *cls, return; } - if (GNUNET_NO == GNUNET_REST_handle_request (handle->conndata_handle, handlers, handle)) + if (GNUNET_OK != GNUNET_JSONAPI_handle_request (handle->rest_handle, + handlers, + &err, + handle)) + { + handle->response_code = err.error_code; GNUNET_SCHEDULER_add_now (&do_error, (void*) handle); + } } @@ -1079,10 +1083,10 @@ testservice_id_task (void *cls, int result) strlen (GNUNET_REST_JSONAPI_NAMESTORE_EGO), &key); if ( GNUNET_YES == - GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map, + GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle->url_param_map, &key) ) { - ego = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map, + ego = GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->url_param_map, &key); } @@ -1091,10 +1095,10 @@ testservice_id_task (void *cls, int result) strlen (GNUNET_REST_JSONAPI_NAMESTORE_RECORD_TYPE), &key); if ( GNUNET_YES == - GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map, + GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle->url_param_map, &key) ) { - type = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map, + type = GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->url_param_map, &key); handle->type = GNUNET_GNSRECORD_typename_to_number (type); @@ -1134,7 +1138,7 @@ testservice_id_task (void *cls, int result) * @return GNUNET_OK if request accepted */ static void -rest_identity_process_request(struct RestConnectionDataHandle *conndata_handle, +rest_identity_process_request(struct GNUNET_REST_RequestHandle *rest_handle, GNUNET_REST_ResultProcessor proc, void *proc_cls) { @@ -1143,10 +1147,8 @@ rest_identity_process_request(struct RestConnectionDataHandle *conndata_handle, handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL; handle->proc_cls = proc_cls; handle->proc = proc; - handle->conndata_handle = conndata_handle; - handle->data = conndata_handle->data; - handle->data_size = conndata_handle->data_size; - GNUNET_asprintf (&handle->url, "%s", conndata_handle->url); + handle->rest_handle = rest_handle; + GNUNET_asprintf (&handle->url, "%s", rest_handle->url); if (handle->url[strlen (handle->url)-1] == '/') handle->url[strlen (handle->url)-1] = '\0'; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, diff --git a/src/rest/gnunet-rest-server.c b/src/rest/gnunet-rest-server.c index ffd65228a..92bef1094 100644 --- a/src/rest/gnunet-rest-server.c +++ b/src/rest/gnunet-rest-server.c @@ -129,7 +129,7 @@ struct MhdConnectionHandle struct GNUNET_REST_Plugin *plugin; - struct RestConnectionDataHandle *data_handle; + struct GNUNET_REST_RequestHandle *data_handle; int status; @@ -217,7 +217,7 @@ url_iterator (void *cls, const char *key, const char *value) { - struct RestConnectionDataHandle *handle = cls; + struct GNUNET_REST_RequestHandle *handle = cls; struct GNUNET_HashCode hkey; char *val; @@ -274,7 +274,7 @@ create_response (void *cls, char *plugin_name; struct GNUNET_HashCode key; struct MhdConnectionHandle *con_handle; - struct RestConnectionDataHandle *rest_conndata_handle; + struct GNUNET_REST_RequestHandle *rest_conndata_handle; con_handle = *con_cls; @@ -311,7 +311,7 @@ create_response (void *cls, } if (GN_REST_STATE_INIT == con_handle->state) { - rest_conndata_handle = GNUNET_new (struct RestConnectionDataHandle); + rest_conndata_handle = GNUNET_new (struct GNUNET_REST_RequestHandle); rest_conndata_handle->method = meth; rest_conndata_handle->url = url; rest_conndata_handle->data = upload_data; diff --git a/src/rest/rest.c b/src/rest/rest.c index a17955314..bf32c9849 100644 --- a/src/rest/rest.c +++ b/src/rest/rest.c @@ -75,8 +75,9 @@ GNUNET_REST_create_json_response (const char *data) } int -GNUNET_REST_handle_request (struct RestConnectionDataHandle *conn, - const struct GNUNET_REST_RestConnectionHandler *handlers, +GNUNET_REST_handle_request (struct GNUNET_REST_RequestHandle *conn, + const struct GNUNET_REST_RequestHandler *handlers, + struct GNUNET_REST_RequestHandlerError *err, void *cls) { int count; @@ -104,6 +105,7 @@ GNUNET_REST_handle_request (struct RestConnectionDataHandle *conn, return GNUNET_YES; } GNUNET_free (url); + err->error_code = MHD_HTTP_BAD_REQUEST; return GNUNET_NO; } -- cgit v1.2.3