Add support for L4Re Virtio Sockets
[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 #if WITH_LIBMICROHTTPD
20
21 #define _GNU_SOURCE
22
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <microhttpd.h>
27
28 #include "afb-context.h"
29 #include "afb-hreq.h"
30 #include "afb-apiset.h"
31 #include "afb-session.h"
32 #include "afb-websock.h"
33
34 int afb_hswitch_apis(struct afb_hreq *hreq, void *data)
35 {
36         const char *api, *verb, *i;
37         size_t lenapi, lenverb;
38         struct afb_apiset *apiset = data;
39
40         /* api is the first hierarchical item */
41         i = hreq->tail;
42         while (*i == '/')
43                 i++;
44         if (!*i)
45                 return 0; /* no API */
46         api = i;
47
48         /* search end of the api and get its length */
49         while (*++i && *i != '/');
50         lenapi = (size_t)(i - api);
51
52         /* search the verb */
53         while (*i == '/')
54                 i++;
55         if (!*i)
56                 return 0; /* no verb */
57         verb = i;
58
59         /* get the verb length */
60         while (*++i);
61         lenverb = (size_t)(i - verb);
62
63         /* found api + verb so process the call */
64         afb_hreq_call(hreq, apiset, api, lenapi, verb, lenverb);
65         return 1;
66 }
67
68 int afb_hswitch_one_page_api_redirect(struct afb_hreq *hreq, void *data)
69 {
70         size_t plen;
71         char *url;
72
73         if (hreq->lentail >= 2 && hreq->tail[1] == '#')
74                 return 0;
75         /*
76          * Here we have for example:
77          *    url  = "/pre/dir/page"   lenurl = 13
78          *    tail =     "/dir/page"   lentail = 9
79          *
80          * We will produce "/pre/#!dir/page"
81          *
82          * Let compute plen that include the / at end (for "/pre/")
83          */
84         plen = hreq->lenurl - hreq->lentail + 1;
85         url = alloca(hreq->lenurl + 3);
86         memcpy(url, hreq->url, plen);
87         url[plen++] = '#';
88         url[plen++] = '!';
89         memcpy(&url[plen], &hreq->tail[1], hreq->lentail);
90         afb_hreq_redirect_to(hreq, url, 1);
91         return 1;
92 }
93
94 int afb_hswitch_websocket_switch(struct afb_hreq *hreq, void *data)
95 {
96         struct afb_apiset *apiset = data;
97
98         if (hreq->lentail != 0)
99                 return 0;
100
101         if (afb_hreq_init_context(hreq) < 0) {
102                 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
103                 return 1;
104         }
105
106         if (!hreq->xreq.context.validated) {
107                 afb_hreq_reply_error(hreq, MHD_HTTP_UNAUTHORIZED);
108                 return 1;
109         }
110
111         return afb_websock_check_upgrade(hreq, apiset);
112 }
113
114 #endif