LCOV - code coverage report
Current view: top level - backend - taler-merchant-httpd_spa.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 4 71 5.6 %
Date: 2022-08-25 06:17:04 Functions: 1 3 33.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2020 Taler Systems SA
       4             : 
       5             :   TALER 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             :   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
      10             :   WARRANTY; without even the implied warranty of MERCHANTABILITY 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             :   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
      15             : */
      16             : /**
      17             :  * @file taler-merchant-httpd_spa.c
      18             :  * @brief logic to load the single page app (/)
      19             :  * @author Christian Grothoff
      20             :  */
      21             : #include "platform.h"
      22             : #include <gnunet/gnunet_util_lib.h>
      23             : #include <taler/taler_util.h>
      24             : #include <taler/taler_mhd_lib.h>
      25             : #include "taler-merchant-httpd_statics.h"
      26             : #include <gnunet/gnunet_mhd_compat.h>
      27             : 
      28             : 
      29             : /**
      30             :  * SPA, compressed.
      31             :  */
      32             : static struct MHD_Response *zspa;
      33             : 
      34             : /**
      35             :  * SPA, vanilla.
      36             :  */
      37             : static struct MHD_Response *spa;
      38             : 
      39             : 
      40             : MHD_RESULT
      41           0 : TMH_return_spa (const struct TMH_RequestHandler *rh,
      42             :                 struct MHD_Connection *connection,
      43             :                 struct TMH_HandlerContext *hc)
      44             : {
      45           0 :   if ( (MHD_YES ==
      46           0 :         TALER_MHD_can_compress (connection)) &&
      47           0 :        (NULL != zspa) )
      48           0 :     return MHD_queue_response (connection,
      49             :                                MHD_HTTP_OK,
      50             :                                zspa);
      51           0 :   return MHD_queue_response (connection,
      52             :                              MHD_HTTP_OK,
      53             :                              spa);
      54             : }
      55             : 
      56             : 
      57             : int
      58           0 : TMH_spa_init ()
      59             : {
      60             :   char *dn;
      61             :   int fd;
      62             :   struct stat sb;
      63             : 
      64             :   {
      65             :     char *path;
      66             : 
      67           0 :     path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
      68           0 :     if (NULL == path)
      69             :     {
      70           0 :       GNUNET_break (0);
      71           0 :       return GNUNET_SYSERR;
      72             :     }
      73           0 :     GNUNET_asprintf (&dn,
      74             :                      "%s/merchant/spa/spa.html",
      75             :                      path);
      76           0 :     GNUNET_free (path);
      77             :   }
      78             : 
      79             :   /* finally open template */
      80           0 :   fd = open (dn,
      81             :              O_RDONLY);
      82           0 :   if (-1 == fd)
      83             :   {
      84           0 :     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
      85             :                               "open",
      86             :                               dn);
      87           0 :     GNUNET_free (dn);
      88           0 :     return GNUNET_SYSERR;
      89             :   }
      90           0 :   if (0 !=
      91           0 :       fstat (fd,
      92             :              &sb))
      93             :   {
      94           0 :     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
      95             :                               "open",
      96             :                               dn);
      97           0 :     GNUNET_break (0 == close (fd));
      98           0 :     GNUNET_free (dn);
      99           0 :     return GNUNET_SYSERR;
     100             :   }
     101             : 
     102             :   {
     103             :     void *in;
     104             :     ssize_t r;
     105             :     size_t csize;
     106             : 
     107           0 :     in = GNUNET_malloc_large (sb.st_size);
     108           0 :     if (NULL == in)
     109             :     {
     110           0 :       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
     111             :                            "malloc");
     112           0 :       GNUNET_break (0 == close (fd));
     113           0 :       GNUNET_free (dn);
     114           0 :       return GNUNET_SYSERR;
     115             :     }
     116           0 :     r = read (fd,
     117             :               in,
     118           0 :               sb.st_size);
     119           0 :     if ( (-1 == r) ||
     120           0 :          (sb.st_size != (size_t) r) )
     121             :     {
     122           0 :       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
     123             :                                 "read",
     124             :                                 dn);
     125           0 :       GNUNET_free (in);
     126           0 :       GNUNET_break (0 == close (fd));
     127           0 :       GNUNET_free (dn);
     128           0 :       return GNUNET_SYSERR;
     129             :     }
     130           0 :     csize = (size_t) r;
     131           0 :     if (MHD_YES ==
     132           0 :         TALER_MHD_body_compress (&in,
     133             :                                  &csize))
     134             :     {
     135           0 :       zspa = MHD_create_response_from_buffer (csize,
     136             :                                               in,
     137             :                                               MHD_RESPMEM_MUST_FREE);
     138           0 :       if (NULL != zspa)
     139             :       {
     140           0 :         if (MHD_NO ==
     141           0 :             MHD_add_response_header (zspa,
     142             :                                      MHD_HTTP_HEADER_CONTENT_ENCODING,
     143             :                                      "deflate"))
     144             :         {
     145           0 :           GNUNET_break (0);
     146           0 :           MHD_destroy_response (zspa);
     147           0 :           zspa = NULL;
     148             :         }
     149           0 :         GNUNET_break (MHD_YES ==
     150             :                       MHD_add_response_header (zspa,
     151             :                                                MHD_HTTP_HEADER_CONTENT_TYPE,
     152             :                                                "text/html"));
     153             :       }
     154             :     }
     155             :     else
     156             :     {
     157           0 :       GNUNET_free (in);
     158             :     }
     159             :   }
     160             : 
     161           0 :   spa = MHD_create_response_from_fd (sb.st_size,
     162             :                                      fd);
     163           0 :   if (NULL == spa)
     164             :   {
     165           0 :     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
     166             :                               "open",
     167             :                               dn);
     168           0 :     GNUNET_break (0 == close (fd));
     169           0 :     GNUNET_free (dn);
     170           0 :     if (NULL != zspa)
     171             :     {
     172           0 :       MHD_destroy_response (zspa);
     173           0 :       zspa = NULL;
     174             :     }
     175           0 :     return GNUNET_SYSERR;
     176             :   }
     177           0 :   GNUNET_free (dn);
     178           0 :   GNUNET_break (MHD_YES ==
     179             :                 MHD_add_response_header (spa,
     180             :                                          MHD_HTTP_HEADER_CONTENT_TYPE,
     181             :                                          "text/html"));
     182           0 :   return GNUNET_OK;
     183             : }
     184             : 
     185             : 
     186             : /**
     187             :  * Nicely shut down.
     188             :  */
     189             : void __attribute__ ((destructor))
     190          14 : get_spa_fini ()
     191             : {
     192          14 :   if (NULL != spa)
     193             :   {
     194           0 :     MHD_destroy_response (spa);
     195           0 :     spa = NULL;
     196             :   }
     197          14 :   if (NULL != zspa)
     198             :   {
     199           0 :     MHD_destroy_response (zspa);
     200           0 :     zspa = NULL;
     201             :   }
     202          14 : }

Generated by: LCOV version 1.14