wip
[src/app-framework-binder.git] / src / afb-hreq.c
index 54c097e..462f7ad 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016, 2017, 2018 "IoT.bzh"
+ * Copyright (C) 2016-2019 "IoT.bzh"
  * Author: José Bollo <jose.bollo@iot.bzh>
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -20,6 +20,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include <ctype.h>
 #include <assert.h>
 #include <errno.h>
 #include <fcntl.h>
@@ -27,6 +28,9 @@
 
 #include <microhttpd.h>
 #include <json-c/json.h>
+#if !defined(JSON_C_TO_STRING_NOSLASHESCAPE)
+#define JSON_C_TO_STRING_NOSLASHESCAPE 0
+#endif
 
 #if defined(USE_MAGIC_MIME_TYPE)
 #include <magic.h>
@@ -732,6 +736,24 @@ const char *afb_hreq_get_header(struct afb_hreq *hreq, const char *name)
        return MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, name);
 }
 
+const char *afb_hreq_get_authorization_bearer(struct afb_hreq *hreq)
+{
+       static const char bearer[] = "Bearer";
+       const char *value = afb_hreq_get_header(hreq, MHD_HTTP_HEADER_AUTHORIZATION);
+       if (value) {
+               if (strncasecmp(value, bearer, sizeof bearer - 1) == 0) {
+                       value += sizeof bearer - 1;
+                       if (isblank(*value++)) {
+                               while (isblank(*value))
+                                       value++;
+                               if (*value)
+                                       return value;
+                       }
+               }
+       }
+       return NULL;
+}
+
 int afb_hreq_post_add(struct afb_hreq *hreq, const char *key, const char *data, size_t size)
 {
        void *p;
@@ -897,7 +919,7 @@ static struct json_object *req_json(struct afb_xreq *xreq)
 
 static ssize_t send_json_cb(json_object *obj, uint64_t pos, char *buf, size_t max)
 {
-       ssize_t len = stpncpy(buf, json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN)+pos, max) - buf;
+       ssize_t len = stpncpy(buf, json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN|JSON_C_TO_STRING_NOSLASHESCAPE)+pos, max) - buf;
        return len ? : (ssize_t)MHD_CONTENT_READER_END_OF_STREAM;
 }
 
@@ -918,7 +940,7 @@ static void req_reply(struct afb_xreq *xreq, struct json_object *object, const c
        if (reqid != NULL && json_object_object_get_ex(reply, "request", &sub))
                json_object_object_add(sub, "reqid", json_object_new_string(reqid));
 
-       response = MHD_create_response_from_callback((uint64_t)strlen(json_object_to_json_string_ext(reply, JSON_C_TO_STRING_PLAIN)), SIZE_RESPONSE_BUFFER, (void*)send_json_cb, reply, (void*)json_object_put);
+       response = MHD_create_response_from_callback((uint64_t)strlen(json_object_to_json_string_ext(reply, JSON_C_TO_STRING_PLAIN|JSON_C_TO_STRING_NOSLASHESCAPE)), SIZE_RESPONSE_BUFFER, (void*)send_json_cb, reply, (void*)json_object_put);
        afb_hreq_reply(hreq, MHD_HTTP_OK, response, NULL);
 }
 
@@ -945,19 +967,27 @@ int afb_hreq_init_context(struct afb_hreq *hreq)
        if (hreq->xreq.context.session != NULL)
                return 0;
 
+       /* get the uuid of the session */
        uuid = afb_hreq_get_header(hreq, long_key_for_uuid);
-       if (uuid == NULL)
+       if (uuid == NULL) {
                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, long_key_for_token);
-       if (token == NULL)
-               token = afb_hreq_get_argument(hreq, long_key_for_token);
-       if (token == NULL)
-               token = afb_hreq_get_argument(hreq, short_key_for_token);
+               if (uuid == NULL) {
+                       uuid = afb_hreq_get_cookie(hreq, cookie_name);
+                       if (uuid == NULL)
+                               uuid = afb_hreq_get_argument(hreq, short_key_for_uuid);
+               }
+       }
+
+       /* get the authorisation token */
+       token = afb_hreq_get_authorization_bearer(hreq);
+       if (token == NULL) {
+               token = afb_hreq_get_header(hreq, long_key_for_token);
+               if (token == NULL) {
+                       token = afb_hreq_get_argument(hreq, long_key_for_token);
+                       if (token == NULL)
+                               token = afb_hreq_get_argument(hreq, short_key_for_token);
+               }
+       }
 
        return afb_context_connect(&hreq->xreq.context, uuid, token);
 }