challenger

OAuth 2.0-based authentication service that validates user can receive messages at a certain address
Log | Files | Refs | Submodules | README | LICENSE

challenger-httpd_spa.c (3841B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2020, 2023, 2025 Taler Systems SA
      4 
      5   Challenger is free software; you can redistribute it and/or modify it under the
      6   terms of the GNU General Public License as published by the Free Software
      7   Foundation; either version 3, or (at your option) any later version.
      8 
      9   Challenger is distributed in the hope that it will be useful, but WITHOUT ANY
     10   WARRANTY; without even the implied warranty of EXCHANGEABILITY or FITNESS FOR
     11   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     12 
     13   You should have received a copy of the GNU General Public License along with
     14   Challenger; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 */
     16 /**
     17  * @file challenger-httpd_spa.c
     18  * @brief logic to load the single page app (/)
     19  * @author Sebastian Marchano
     20  * @author Christian Grothoff
     21  */
     22 #include "platform.h"
     23 #include <gnunet/gnunet_util_lib.h>
     24 #include <taler/taler_util.h>
     25 #include <taler/taler_mhd_lib.h>
     26 #include <gnunet/gnunet_mhd_compat.h>
     27 #include "challenger_util.h"
     28 #include "challenger-httpd_spa.h"
     29 
     30 /**
     31  * Resources of the Challenger SPA.
     32  */
     33 static struct TALER_MHD_Spa *spa;
     34 
     35 
     36 enum MHD_Result
     37 CH_handler_spa (struct CH_HandlerContext *hc,
     38                 const char *upload_data,
     39                 size_t *upload_data_size)
     40 {
     41   const char *path = hc->path;
     42 
     43   if ( (NULL == path) ||
     44        (0 == strcmp (path,
     45                      "")) )
     46     path = "index.html";
     47   return TALER_MHD_spa_handler (spa,
     48                                 hc->connection,
     49                                 path);
     50 }
     51 
     52 
     53 enum GNUNET_GenericReturnValue
     54 CH_spa_init ()
     55 {
     56   spa = TALER_MHD_spa_load (CHALLENGER_project_data (),
     57                             "spa/");
     58   if (NULL == spa)
     59   {
     60     GNUNET_break (0);
     61     return GNUNET_SYSERR;
     62   }
     63   return GNUNET_OK;
     64 }
     65 
     66 
     67 /**
     68  * Nicely shut down.
     69  */
     70 void __attribute__ ((destructor))
     71 get_spa_fini (void);
     72 
     73 /* Declaration to avoid compiler warning */
     74 void __attribute__ ((destructor))
     75 get_spa_fini ()
     76 {
     77   if (NULL != spa)
     78   {
     79     TALER_MHD_spa_free (spa);
     80     spa = NULL;
     81   }
     82 }
     83 
     84 
     85 /**
     86  * Generates the response for "/", redirecting the
     87  * client to the "/webui/" from where we serve the SPA.
     88  *
     89  * @param rh request handler
     90  * @param connection MHD connection
     91  * @param hc handler context
     92  * @return MHD result code
     93  */
     94 enum MHD_Result
     95 CH_spa_redirect (struct CH_HandlerContext *hc,
     96                  const char *upload_data,
     97                  size_t *upload_data_size)
     98 {
     99   const char *text = "Redirecting to /webui/";
    100   struct MHD_Response *response;
    101   char *dst;
    102   const char *rparams;
    103 
    104   response = MHD_create_response_from_buffer (strlen (text),
    105                                               (void *) text,
    106                                               MHD_RESPMEM_PERSISTENT);
    107   if (NULL == response)
    108   {
    109     GNUNET_break (0);
    110     return MHD_NO;
    111   }
    112   TALER_MHD_add_global_headers (response,
    113                                 true);
    114   GNUNET_break (MHD_YES ==
    115                 MHD_add_response_header (response,
    116                                          MHD_HTTP_HEADER_CONTENT_TYPE,
    117                                          "text/plain"));
    118 
    119   rparams = strchr (hc->full_url, '?');
    120   if (NULL == rparams)
    121     dst = GNUNET_strdup ("/webui/");
    122   else
    123     GNUNET_asprintf (&dst,
    124                      "/webui/%s",
    125                      rparams);
    126 
    127   if (MHD_NO ==
    128       MHD_add_response_header (response,
    129                                MHD_HTTP_HEADER_LOCATION,
    130                                dst))
    131   {
    132     GNUNET_break (0);
    133     MHD_destroy_response (response);
    134     GNUNET_free (dst);
    135     return MHD_NO;
    136   }
    137 
    138   {
    139     enum MHD_Result ret;
    140 
    141     ret = MHD_queue_response (hc->connection,
    142                               MHD_HTTP_FOUND,
    143                               response);
    144     MHD_destroy_response (response);
    145     GNUNET_free (dst);
    146     return ret;
    147   }
    148 }