X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fafb-hreq.c;h=4239670de8149a5b74ea09fb35972539096a277f;hb=9d59db317f2ac51c3eb058fe740bd39b3021fbcd;hp=0f2f0ffb809f71507a0760032548d21e6c7fc5a1;hpb=c7e9786d408f13d8f8f43c6b68da916bbb1ed5f3;p=src%2Fapp-framework-binder.git diff --git a/src/afb-hreq.c b/src/afb-hreq.c index 0f2f0ffb..4239670d 100644 --- a/src/afb-hreq.c +++ b/src/afb-hreq.c @@ -33,7 +33,7 @@ #endif #include "afb-method.h" -#include "afb-req-itf.h" +#include #include "afb-msg-json.h" #include "afb-context.h" #include "afb-hreq.h" @@ -44,13 +44,14 @@ static char empty_string[] = ""; -static const char uuid_header[] = "x-afb-uuid"; -static const char uuid_arg[] = "uuid"; -static const char uuid_cookie[] = "uuid"; +static const char long_key_for_uuid[] = "x-afb-uuid"; +static const char short_key_for_uuid[] = "uuid"; -static const char token_header[] = "x-afb-token"; -static const char token_arg[] = "token"; -static const char token_cookie[] = "token"; +static const char long_key_for_token[] = "x-afb-token"; +static const char short_key_for_token[] = "token"; + +static const char long_key_for_reqid[] = "x-afb-reqid"; +static const char short_key_for_reqid[] = "reqid"; static char *cookie_name = NULL; static char *cookie_setter = NULL; @@ -84,7 +85,9 @@ static const struct afb_req_itf afb_hreq_itf = { .context_get = (void*)afb_context_get, .context_set = (void*)afb_context_set, .addref = (void*)afb_hreq_addref, - .unref = (void*)afb_hreq_unref + .unref = (void*)afb_hreq_unref, + .session_close = (void*)afb_context_close, + .session_set_LOA = (void*)afb_context_change_loa }; static struct hreq_data *get_data(struct afb_hreq *hreq, const char *key, int create) @@ -471,6 +474,7 @@ int afb_hreq_reply_file(struct afb_hreq *hreq, int dirfd, const char *filename) int afb_hreq_redirect_to(struct afb_hreq *hreq, const char *url) { + /* TODO: append the query part! */ afb_hreq_reply_static(hreq, MHD_HTTP_MOVED_PERMANENTLY, 0, NULL, MHD_HTTP_HEADER_LOCATION, url, NULL); DEBUG("redirect from [%s] to [%s]", hreq->url, url); @@ -607,6 +611,7 @@ struct afb_req afb_hreq_to_req(struct afb_hreq *hreq) static struct afb_arg req_get(struct afb_hreq *hreq, const char *name) { + const char *value; struct hreq_data *hdat = get_data(hreq, name, 0); if (hdat) return (struct afb_arg){ @@ -614,10 +619,11 @@ static struct afb_arg req_get(struct afb_hreq *hreq, const char *name) .value = hdat->value, .path = hdat->path }; - + + value = MHD_lookup_connection_value(hreq->connection, MHD_GET_ARGUMENT_KIND, name); return (struct afb_arg){ - .name = name, - .value = MHD_lookup_connection_value(hreq->connection, MHD_GET_ARGUMENT_KIND, name), + .name = value == NULL ? NULL : name, + .value = value, .path = NULL }; } @@ -677,14 +683,21 @@ static ssize_t send_json_cb(json_object *obj, uint64_t pos, char *buf, size_t ma static void req_reply(struct afb_hreq *hreq, unsigned retcode, const char *status, const char *info, json_object *resp) { - struct json_object *reply; - const char *token, *uuid; + struct json_object *reply, *request; + const char *token, *uuid, *reqid; struct MHD_Response *response; token = afb_context_sent_token(&hreq->context); uuid = afb_context_sent_uuid(&hreq->context); reply = afb_msg_json_reply(status, info, resp, token, uuid); + + reqid = afb_hreq_get_argument(hreq, long_key_for_reqid); + if (reqid == NULL) + reqid = afb_hreq_get_argument(hreq, short_key_for_reqid); + if (reqid != NULL && json_object_object_get_ex(reply, "request", &request)) + json_object_object_add (request, short_key_for_reqid, json_object_new_string(reqid)); + response = MHD_create_response_from_callback((uint64_t)strlen(json_object_to_json_string(reply)), SIZE_RESPONSE_BUFFER, (void*)send_json_cb, reply, (void*)json_object_put); afb_hreq_reply(hreq, retcode, response, NULL); } @@ -707,17 +720,19 @@ int afb_hreq_init_context(struct afb_hreq *hreq) if (hreq->context.session != NULL) return 0; - uuid = afb_hreq_get_header(hreq, uuid_header); + uuid = afb_hreq_get_header(hreq, long_key_for_uuid); if (uuid == NULL) - uuid = afb_hreq_get_argument(hreq, uuid_arg); + uuid = afb_hreq_get_argument(hreq, long_key_for_uuid); if (uuid == NULL) uuid = afb_hreq_get_cookie(hreq, cookie_name); + if (uuid == NULL) + uuid = afb_hreq_get_argument(hreq, short_key_for_uuid); - token = afb_hreq_get_header(hreq, token_header); + token = afb_hreq_get_header(hreq, long_key_for_token); if (token == NULL) - token = afb_hreq_get_argument(hreq, token_arg); + token = afb_hreq_get_argument(hreq, long_key_for_token); if (token == NULL) - token = afb_hreq_get_cookie(hreq, token_cookie); + token = afb_hreq_get_argument(hreq, short_key_for_token); return afb_context_connect(&hreq->context, uuid, token); } @@ -732,7 +747,7 @@ int afb_hreq_init_cookie(int port, const char *path, int maxage) cookie_setter = NULL; path = path ? : "/"; - rc = asprintf(&cookie_name, "x-afb-uuid-%d", port); + rc = asprintf(&cookie_name, "%s-%d", long_key_for_uuid, port); if (rc < 0) return 0; rc = asprintf(&cookie_setter, "%s=%%s; Path=%s; Max-Age=%d; HttpOnly",