fix unexpected ending spaces
[src/app-framework-binder.git] / src / afb-hswitch.c
1 /*
2  * Copyright (C) 2015, 2016 "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/afb-req-itf.h>
27 #include "afb-context.h"
28 #include "afb-hreq.h"
29 #include "afb-apis.h"
30 #include "session.h"
31 #include "afb-websock.h"
32
33 int afb_hswitch_apis(struct afb_hreq *hreq, void *data)
34 {
35         const char *api, *verb;
36         size_t lenapi, lenverb;
37
38         api = &hreq->tail[strspn(hreq->tail, "/")];
39         lenapi = strcspn(api, "/");
40         verb = &api[lenapi];
41         verb = &verb[strspn(verb, "/")];
42         lenverb = strcspn(verb, "/");
43
44         if (!(*api && *verb && lenapi && lenverb))
45                 return 0;
46
47         if (afb_hreq_init_context(hreq) < 0)
48                 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
49         else
50                 afb_apis_call(afb_hreq_to_req(hreq), &hreq->context, api, lenapi, verb, lenverb);
51         return 1;
52 }
53
54 int afb_hswitch_one_page_api_redirect(struct afb_hreq *hreq, void *data)
55 {
56         size_t plen;
57         char *url;
58
59         if (hreq->lentail >= 2 && hreq->tail[1] == '#')
60                 return 0;
61         /*
62          * Here we have for example:
63          *    url  = "/pre/dir/page"   lenurl = 13
64          *    tail =     "/dir/page"   lentail = 9
65          *
66          * We will produce "/pre/#!dir/page"
67          *
68          * Let compute plen that include the / at end (for "/pre/")
69          */
70         plen = hreq->lenurl - hreq->lentail + 1;
71         url = alloca(hreq->lenurl + 3);
72         memcpy(url, hreq->url, plen);
73         url[plen++] = '#';
74         url[plen++] = '!';
75         memcpy(&url[plen], &hreq->tail[1], hreq->lentail);
76         return afb_hreq_redirect_to(hreq, url, 1);
77 }
78
79 int afb_hswitch_websocket_switch(struct afb_hreq *hreq, void *data)
80 {
81         if (hreq->lentail != 0)
82                 return 0;
83
84         if (afb_hreq_init_context(hreq) < 0) {
85                 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
86                 return 1;
87         }
88
89         if (!hreq->context.validated) {
90                 afb_hreq_reply_error(hreq, MHD_HTTP_UNAUTHORIZED);
91                 return 1;
92         }
93
94         return afb_websock_check_upgrade(hreq);
95 }
96
97