refactored verbosity
[src/app-framework-binder.git] / src / http-svc.c
index 9fa6e84..b36d5f7 100644 (file)
  */
 
 #define _GNU_SOURCE
-#include <microhttpd.h>
+
+#include <stdio.h>
+#include <string.h>
 #include <assert.h>
 #include <poll.h>
+#include <fcntl.h>
 #include <sys/stat.h>
 
-#include "../include/local-def.h"
+#include <microhttpd.h>
+
+#include "local-def.h"
 #include "afb-method.h"
 #include "afb-hreq.h"
 #include "afb-websock.h"
 #include "afb-apis.h"
-#include "session.h"
 #include "afb-req-itf.h"
+#include "verbose.h"
 
 #define JSON_CONTENT  "application/json"
 #define FORM_CONTENT  MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA
 
+
 struct afb_hsrv_handler {
        struct afb_hsrv_handler *next;
        const char *prefix;
@@ -132,25 +138,6 @@ int afb_hsrv_add_handler(
        return 1;
 }
 
-static const char uuid_header[] = "x-afb-uuid";
-static const char uuid_arg[] = "uuid";
-static const char uuid_cookie[] = "uuid";
-
-static struct AFB_clientCtx *afb_hreq_context(struct afb_hreq *hreq)
-{
-       const char *uuid;
-
-       if (hreq->context == NULL) {
-               uuid = afb_hreq_get_header(hreq, uuid_header);
-               if (uuid == NULL)
-                       uuid = afb_hreq_get_argument(hreq, uuid_arg);
-               if (uuid == NULL)
-                       uuid = afb_hreq_get_cookie(hreq, uuid_cookie);
-               hreq->context = _ctxClientGet(uuid);
-       }
-       return hreq->context;
-}
-
 static int afb_hreq_websocket_switch(struct afb_hreq *hreq, void *data)
 {
        int later;
@@ -174,17 +161,19 @@ static int afb_hreq_rest_api(struct afb_hreq *hreq, void *data)
 {
        const char *api, *verb;
        size_t lenapi, lenverb;
+       struct AFB_clientCtx *context;
 
-       api = hreq->tail;
-       lenapi = strspn(api, "/");
-       verb = &hreq->tail[lenapi];
-       verb = &verb[strcspn(verb, "/")];
-       lenverb = strspn(verb, "/");
+       api = &hreq->tail[strspn(hreq->tail, "/")];
+       lenapi = strcspn(api, "/");
+       verb = &api[lenapi];
+       verb = &verb[strspn(verb, "/")];
+       lenverb = strcspn(verb, "/");
 
        if (!(*api && *verb && lenapi && lenverb))
                return 0;
 
-       return afb_apis_handle(afb_hreq_to_req(hreq), api, lenapi, verb, lenverb);
+       context = afb_hreq_context(hreq);
+       return afb_apis_handle(afb_hreq_to_req(hreq), context, api, lenapi, verb, lenverb);
 }
 
 static int handle_alias(struct afb_hreq *hreq, void *data)
@@ -371,18 +360,11 @@ internal_error:
 static void end_handler(void *cls, struct MHD_Connection *connection, void **recordreq,
                        enum MHD_RequestTerminationCode toe)
 {
-       AFB_session *session;
        struct afb_hreq *hreq;
 
-       session = cls;
        hreq = *recordreq;
 
-       if (hreq != NULL) {
-               if (hreq->postform != NULL)
-                       MHD_destroy_post_processor(hreq->postform);
-               afb_hreq_drop_data(hreq);
-               free(hreq);
-       }
+       afb_hreq_free(hreq);
 }
 
 static int new_client_handler(void *cls, const struct sockaddr *addr, socklen_t addrlen)
@@ -399,7 +381,7 @@ static int new_client_handler(void *cls, const struct sockaddr *addr, socklen_t
 static int init_lib_magic (AFB_session *session)
 {
        /* MAGIC_MIME tells magic to return a mime of the file, but you can specify different things */
-       if (verbose)
+       if (verbosity)
                printf("Loading mimetype default magic database\n");
 
        session->magic = magic_open(MAGIC_MIME_TYPE);
@@ -455,7 +437,7 @@ AFB_error httpdStart(AFB_session * session)
        init_lib_magic (session);
 #endif
 
-       if (verbose) {
+       if (verbosity) {
                printf("AFB:notice Waiting port=%d rootdir=%s\n", session->config->httpdPort, session->config->rootdir);
                printf("AFB:notice Browser URL= http:/*localhost:%d\n", session->config->httpdPort);
        }
@@ -491,10 +473,10 @@ AFB_error httpdLoop(AFB_session * session)
        pfd.fd = info->listen_fd;
        pfd.events = POLLIN;
 
-       if (verbose)
+       if (verbosity)
                fprintf(stderr, "AFB:notice entering httpd waiting loop\n");
        while (TRUE) {
-               if (verbose)
+               if (verbosity)
                        fprintf(stderr, "AFB:notice httpd alive [%d]\n", count++);
                poll(&pfd, 1, 15000);   /* 15 seconds (as above timeout when starting) */
                MHD_run(session->httpd);