Add support for L4Re Virtio Sockets
[src/app-framework-binder.git] / src / afb-hreq.c
index fa16565..49c1692 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");
@@ -15,6 +15,8 @@
  * limitations under the License.
  */
 
+#if WITH_LIBMICROHTTPD
+
 #define _GNU_SOURCE
 
 #include <stdlib.h>
 
 #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)
+#if USE_MAGIC_MIME_TYPE
 #include <magic.h>
 #endif
 
@@ -36,6 +41,7 @@
 #include "afb-msg-json.h"
 #include "afb-context.h"
 #include "afb-hreq.h"
+#include "afb-hsrv.h"
 #include "afb-session.h"
 #include "afb-cred.h"
 #include "verbose.h"
@@ -168,10 +174,9 @@ static void afb_hreq_reply_v(struct afb_hreq *hreq, unsigned status, struct MHD_
 
        hreq->replied = 1;
        if (hreq->suspended != 0) {
-               extern void run_micro_httpd(struct afb_hsrv *hsrv);
                MHD_resume_connection (hreq->connection);
                hreq->suspended = 0;
-               run_micro_httpd(hreq->hsrv);
+               afb_hsrv_run(hreq->hsrv);
        }
 }
 
@@ -215,7 +220,7 @@ void afb_hreq_reply_free(struct afb_hreq *hreq, unsigned status, size_t size, ch
        va_end(args);
 }
 
-#if defined(USE_MAGIC_MIME_TYPE)
+#if USE_MAGIC_MIME_TYPE
 
 #if !defined(MAGIC_DB)
 #define MAGIC_DB "/usr/share/misc/magic.mgc"
@@ -259,7 +264,7 @@ static const char *mimetype_fd_name(int fd, const char *filename)
 {
        const char *result = NULL;
 
-#if defined(INFER_EXTENSION)
+#if INFER_EXTENSION
        /*
         * Set some well-known extensions
         * Note that it is mandatory for example for css files in order to provide
@@ -304,7 +309,7 @@ static const char *mimetype_fd_name(int fd, const char *filename)
                }
        }
 #endif
-#if defined(USE_MAGIC_MIME_TYPE)
+#if USE_MAGIC_MIME_TYPE
        if (result == NULL)
                result = magic_mimetype_fd(fd);
 #endif
@@ -897,7 +902,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 +923,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);
 }
 
@@ -998,3 +1003,4 @@ struct afb_hreq *afb_hreq_create()
        return hreq;
 }
 
+#endif