aboutsummaryrefslogtreecommitdiff
path: root/src/mhd/mhd_spa.c
blob: 6bdd24465911ff2099eff5c6dcf5a1c434d5d751 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
  This file is part of TALER
  Copyright (C) 2020, 2023, 2024 Taler Systems SA

  TALER is free software; you can redistribute it and/or modify it under the
  terms of the GNU General Public License as published by the Free Software
  Foundation; either version 3, or (at your option) any later version.

  TALER is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of EXCHANGEABILITY or FITNESS FOR
  A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

  You should have received a copy of the GNU General Public License along with
  TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
*/
/**
 * @file mhd_spa.c
 * @brief logic to load single page apps
 * @author Christian Grothoff
 */
#include "platform.h"
#include <gnunet/gnunet_util_lib.h>
#include "taler_util.h"
#include "taler_mhd_lib.h"
#include <gnunet/gnunet_mhd_compat.h>


/**
 * Resource from the WebUi.
 */
struct WebuiFile
{
  /**
   * Kept in a DLL.
   */
  struct WebuiFile *next;

  /**
   * Kept in a DLL.
   */
  struct WebuiFile *prev;

  /**
   * Path this resource matches.
   */
  char *path;

  /**
   * SPA resource, compressed.
   */
  struct MHD_Response *zresponse;

  /**
   * SPA resource, vanilla.
   */
  struct MHD_Response *response;

};


/**
 * Resource from the WebUi.
 */
struct TALER_MHD_Spa
{
  /**
   * Resources of the WebUI, kept in a DLL.
   */
  struct WebuiFile *webui_head;

  /**
   * Resources of the WebUI, kept in a DLL.
   */
  struct WebuiFile *webui_tail;
};


MHD_RESULT
TALER_MHD_spa_handler (const struct TALER_MHD_Spa *spa,
                       struct MHD_Connection *connection,
                       const char *path)
{
  struct WebuiFile *w = NULL;

  if ( (NULL == path) ||
       (0 == strcmp (path,
                     "")) )
    path = "index.html";
  for (struct WebuiFile *pos = spa->webui_head;
       NULL != pos;
       pos = pos->next)
    if (0 == strcmp (path,
                     pos->path))
    {
      w = pos;
      break;
    }
  if (NULL == w)
    return TALER_MHD_reply_with_error (connection,
                                       MHD_HTTP_NOT_FOUND,
                                       TALER_EC_GENERIC_ENDPOINT_UNKNOWN,
                                       path);
  if ( (MHD_YES ==
        TALER_MHD_can_compress (connection)) &&
       (NULL != w->zresponse) )
    return MHD_queue_response (connection,
                               MHD_HTTP_OK,
                               w->zresponse);
  return MHD_queue_response (connection,
                             MHD_HTTP_OK,
                             w->response);
}


/**
 * Function called on each file to load for the WebUI.
 *
 * @param cls the `struct TALER_MHD_Spa *` to build
 * @param dn name of the file to load
 */
static enum GNUNET_GenericReturnValue
build_webui (void *cls,
             const char *dn)
{
  static const struct
  {
    const char *ext;
    const char *mime;
  } mime_map[] = {
    {
      .ext = "css",
      .mime = "text/css"
    },
    {
      .ext = "html",
      .mime = "text/html"
    },
    {
      .ext = "js",
      .mime = "text/javascript"
    },
    {
      .ext = "jpg",
      .mime = "image/jpeg"
    },
    {
      .ext = "jpeg",
      .mime = "image/jpeg"
    },
    {
      .ext = "png",
      .mime = "image/png"
    },
    {
      .ext = "svg",
      .mime = "image/svg+xml"
    },
    {
      .ext = NULL,
      .mime = NULL
    },
  };
  struct TALER_MHD_Spa *spa = cls;
  int fd;
  struct stat sb;
  struct MHD_Response *zresponse = NULL;
  struct MHD_Response *response;
  const char *ext;
  const char *mime;

  fd = open (dn,
             O_RDONLY);
  if (-1 == fd)
  {
    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
                              "open",
                              dn);
    return GNUNET_SYSERR;
  }
  if (0 !=
      fstat (fd,
             &sb))
  {
    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
                              "open",
                              dn);
    GNUNET_break (0 == close (fd));
    return GNUNET_SYSERR;
  }

  mime = NULL;
  ext = strrchr (dn, '.');
  if (NULL == ext)
  {
    GNUNET_break (0 == close (fd));
    return GNUNET_OK;
  }
  ext++;
  for (unsigned int i = 0; NULL != mime_map[i].ext; i++)
    if (0 == strcasecmp (ext,
                         mime_map[i].ext))
    {
      mime = mime_map[i].mime;
      break;
    }

  {
    void *in;
    ssize_t r;
    size_t csize;

    in = GNUNET_malloc_large (sb.st_size);
    if (NULL == in)
    {
      GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
                           "malloc");
      GNUNET_break (0 == close (fd));
      return GNUNET_SYSERR;
    }
    r = read (fd,
              in,
              sb.st_size);
    if ( (-1 == r) ||
         (sb.st_size != (size_t) r) )
    {
      GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
                                "read",
                                dn);
      GNUNET_free (in);
      GNUNET_break (0 == close (fd));
      return GNUNET_SYSERR;
    }
    csize = (size_t) r;
    if (MHD_YES ==
        TALER_MHD_body_compress (&in,
                                 &csize))
    {
      zresponse = MHD_create_response_from_buffer (csize,
                                                   in,
                                                   MHD_RESPMEM_MUST_FREE);
      if (NULL != zresponse)
      {
        if (MHD_NO ==
            MHD_add_response_header (zresponse,
                                     MHD_HTTP_HEADER_CONTENT_ENCODING,
                                     "deflate"))
        {
          GNUNET_break (0);
          MHD_destroy_response (zresponse);
          zresponse = NULL;
        }
        if (NULL != mime)
          GNUNET_break (MHD_YES ==
                        MHD_add_response_header (zresponse,
                                                 MHD_HTTP_HEADER_CONTENT_TYPE,
                                                 mime));
      }
    }
    else
    {
      GNUNET_free (in);
    }
  }

  response = MHD_create_response_from_fd (sb.st_size,
                                          fd);
  if (NULL == response)
  {
    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
                              "open",
                              dn);
    GNUNET_break (0 == close (fd));
    if (NULL != zresponse)
    {
      MHD_destroy_response (zresponse);
      zresponse = NULL;
    }
    return GNUNET_SYSERR;
  }
  if (NULL != mime)
    GNUNET_break (MHD_YES ==
                  MHD_add_response_header (response,
                                           MHD_HTTP_HEADER_CONTENT_TYPE,
                                           mime));

  {
    struct WebuiFile *w;
    const char *fn;

    fn = strrchr (dn, '/');
    GNUNET_assert (NULL != fn);
    w = GNUNET_new (struct WebuiFile);
    w->path = GNUNET_strdup (fn + 1);
    w->response = response;
    w->zresponse = zresponse;
    GNUNET_CONTAINER_DLL_insert (spa->webui_head,
                                 spa->webui_tail,
                                 w);
  }
  return GNUNET_OK;
}


struct TALER_MHD_Spa *
TALER_MHD_spa_load (const struct GNUNET_OS_ProjectData *pd,
                    const char *dir)
{
  struct TALER_MHD_Spa *spa;
  char *dn;

  {
    char *path;

    path = GNUNET_OS_installation_get_path (pd,
                                            GNUNET_OS_IPK_DATADIR);
    if (NULL == path)
    {
      GNUNET_break (0);
      return NULL;
    }
    GNUNET_asprintf (&dn,
                     "%s%s",
                     path,
                     dir);
    GNUNET_free (path);
  }
  spa = GNUNET_new (struct TALER_MHD_Spa);
  if (-1 ==
      GNUNET_DISK_directory_scan (dn,
                                  &build_webui,
                                  spa))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Failed to load WebUI from `%s'\n",
                dn);
    GNUNET_free (dn);
    TALER_MHD_spa_free (spa);
    return NULL;
  }
  GNUNET_free (dn);
  return spa;
}


void
TALER_MHD_spa_free (struct TALER_MHD_Spa *spa)
{
  struct WebuiFile *w;

  while (NULL != (w = spa->webui_head))
  {
    GNUNET_CONTAINER_DLL_remove (spa->webui_head,
                                 spa->webui_tail,
                                 w);
    if (NULL != w->response)
    {
      MHD_destroy_response (w->response);
      w->response = NULL;
    }
    if (NULL != w->zresponse)
    {
      MHD_destroy_response (w->zresponse);
      w->zresponse = NULL;
    }
    GNUNET_free (w->path);
    GNUNET_free (w);
  }
  GNUNET_free (spa);
}