more simplification
[src/app-framework-binder.git] / src / afb-hreq.c
index 3f95c53..e45b250 100644 (file)
 #include <sys/stat.h>
 
 #include <microhttpd.h>
+#include <json.h>
 
 #if defined(USE_MAGIC_MIME_TYPE)
 #include <magic.h>
 #endif
 
-#include "local-def.h"
 #include "afb-method.h"
+#include "afb-websock.h"
+#include "afb-apis.h"
 #include "afb-req-itf.h"
 #include "afb-hreq.h"
 #include "session.h"
@@ -408,6 +410,68 @@ int afb_hreq_redirect_to(struct afb_hreq *hreq, const char *url)
        return 1;
 }
 
+int afb_hreq_one_page_api_redirect(
+               struct afb_hreq *hreq,
+               void *data)
+{
+       size_t plen;
+       char *url;
+
+       if (hreq->lentail >= 2 && hreq->tail[1] == '#')
+               return 0;
+       /*
+        * Here we have for example:
+        *    url  = "/pre/dir/page"   lenurl = 13
+        *    tail =     "/dir/page"   lentail = 9
+        *
+        * We will produce "/pre/#!dir/page"
+        *
+        * Let compute plen that include the / at end (for "/pre/")
+        */
+       plen = hreq->lenurl - hreq->lentail + 1;
+       url = alloca(hreq->lenurl + 3);
+       memcpy(url, hreq->url, plen);
+       url[plen++] = '#';
+       url[plen++] = '!';
+       memcpy(&url[plen], &hreq->tail[1], hreq->lentail);
+       return afb_hreq_redirect_to(hreq, url);
+}
+
+int afb_hreq_websocket_switch(struct afb_hreq *hreq, void *data)
+{
+       int later;
+
+       afb_hreq_context(hreq);
+       if (hreq->lentail != 0 || !afb_websock_check(hreq, &later))
+               return 0;
+
+       if (!later) {
+               struct afb_websock *ws = afb_websock_create(hreq);
+               if (ws != NULL)
+                       hreq->upgrade = 1;
+       }
+       return 1;
+}
+
+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[strspn(hreq->tail, "/")];
+       lenapi = strcspn(api, "/");
+       verb = &api[lenapi];
+       verb = &verb[strspn(verb, "/")];
+       lenverb = strcspn(verb, "/");
+
+       if (!(*api && *verb && lenapi && lenverb))
+               return 0;
+
+       context = afb_hreq_context(hreq);
+       return afb_apis_handle(afb_hreq_to_req(hreq), context, api, lenapi, verb, lenverb);
+}
+
 const char *afb_hreq_get_cookie(struct afb_hreq *hreq, const char *name)
 {
        return MHD_lookup_connection_value(hreq->connection, MHD_COOKIE_KIND, name);