ab9e94b8e0aaacb29a3837649a68d308959c25c3
[src/app-framework-binder.git] / src / afb-hswitch.c
1 /*
2  * Copyright (C) 2015-2019 "IoT.bzh"
3  * Author "Fulup Ar Foll"
4  * Author José Bollo <jose.bollo@iot.bzh>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #define _GNU_SOURCE
20
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include <microhttpd.h>
25
26 #include "afb-context.h"
27 #include "afb-hreq.h"
28 #include "afb-apiset.h"
29 #include "afb-session.h"
30 #include "afb-websock.h"
31
32 int afb_hswitch_apis(struct afb_hreq *hreq, void *data)
33 {
34         const char *api, *verb, *i;
35         size_t lenapi, lenverb;
36         struct afb_apiset *apiset = data;
37
38         /* api is the first hierarchical item */
39         i = hreq->tail;
40         while (*i == '/')
41                 i++;
42         if (!*i)
43                 return 0; /* no API */
44         api = i;
45
46         /* search end of the api and get its length */
47         while (*++i && *i != '/');
48         lenapi = (size_t)(i - api);
49
50         /* search the verb */
51         while (*i == '/')
52                 i++;
53         if (!*i)
54                 return 0; /* no verb */
55         verb = i;
56
57         /* get the verb length */
58         while (*++i);
59         lenverb = (size_t)(i - verb);
60
61         /* found api + verb so process the call */
62         afb_hreq_call(hreq, apiset, api, lenapi, verb, lenverb);
63         return 1;
64 }
65
66 int afb_hswitch_one_page_api_redirect(struct afb_hreq *hreq, void *data)
67 {
68         size_t plen;
69         char *url;
70
71         if (hreq->lentail >= 2 && hreq->tail[1] == '#')
72                 return 0;
73         /*
74          * Here we have for example:
75          *    url  = "/pre/dir/page"   lenurl = 13
76          *    tail =     "/dir/page"   lentail = 9
77          *
78          * We will produce "/pre/#!dir/page"
79          *
80          * Let compute plen that include the / at end (for "/pre/")
81          */
82         plen = hreq->lenurl - hreq->lentail + 1;
83         url = alloca(hreq->lenurl + 3);
84         memcpy(url, hreq->url, plen);
85         url[plen++] = '#';
86         url[plen++] = '!';
87         memcpy(&url[plen], &hreq->tail[1], hreq->lentail);
88         afb_hreq_redirect_to(hreq, url, 1);
89         return 1;
90 }
91
92 int afb_hswitch_websocket_switch(struct afb_hreq *hreq, void *data)
93 {
94         struct afb_apiset *apiset = data;
95
96         if (hreq->lentail != 0)
97                 return 0;
98
99         if (afb_hreq_init_context(hreq) < 0) {
100                 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
101                 return 1;
102         }
103
104         return afb_websock_check_upgrade(hreq, apiset);
105 }
106
107