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: 39 69 56.5 %
Date: 2021-08-30 06:54:17 Functions: 3 3 100.0 %
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           7 : TMH_return_spa (const struct TMH_RequestHandler *rh,
      42             :                    struct MHD_Connection *connection,
      43             :                    struct TMH_HandlerContext *hc)
      44             : {
      45           7 :   if ( (MHD_YES ==
      46           7 :         TALER_MHD_can_compress (connection)) &&
      47           0 :        (NULL != zspa) )
      48           0 :     return MHD_queue_response (connection,
      49             :                                MHD_HTTP_OK,
      50             :                                zspa);
      51           7 :   return MHD_queue_response (connection,
      52             :                              MHD_HTTP_OK,
      53             :                              spa);
      54             : }
      55             : 
      56             : 
      57             : int
      58          16 : TMH_spa_init ()
      59             : {
      60             :   char *dn;
      61             :   int fd;
      62             :   struct stat sb;
      63             : 
      64             :   {
      65             :     char *path;
      66             : 
      67          16 :     path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
      68          16 :     if (NULL == path)
      69             :     {
      70           0 :       GNUNET_break (0);
      71           0 :       return GNUNET_SYSERR;
      72             :     }
      73          16 :     GNUNET_asprintf (&dn,
      74             :                      "%s/merchant/spa/spa.html",
      75             :                      path);
      76          16 :     GNUNET_free (path);
      77             :   }
      78             : 
      79             :   /* finally open template */
      80          16 :   fd = open (dn,
      81             :              O_RDONLY);
      82          16 :   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          16 :   if (0 !=
      91          16 :       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          16 :     in = GNUNET_malloc_large (sb.st_size);
     108          16 :     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          16 :     r = read (fd,
     117             :               in,
     118          16 :               sb.st_size);
     119          16 :     if ( (-1 == r) ||
     120          16 :          (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          16 :     csize = (size_t) r;
     131          16 :     if (MHD_YES ==
     132          16 :         TALER_MHD_body_compress (&in,
     133             :                                  &csize))
     134             :     {
     135          16 :       zspa = MHD_create_response_from_buffer (csize,
     136             :                                               in,
     137             :                                               MHD_RESPMEM_MUST_FREE);
     138          16 :       if (NULL != zspa)
     139             :       {
     140          16 :         if (MHD_NO ==
     141          16 :             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          16 :         GNUNET_break (MHD_YES ==
     150             :                       MHD_add_response_header (zspa,
     151             :                                                MHD_HTTP_HEADER_CONTENT_TYPE,
     152             :                                                "text/html"));
     153             :       }
     154             :     }
     155             :   }
     156             : 
     157          16 :   spa = MHD_create_response_from_fd (sb.st_size,
     158             :                                      fd);
     159          16 :   if (NULL == spa)
     160             :   {
     161           0 :     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
     162             :                               "open",
     163             :                               dn);
     164           0 :     GNUNET_break (0 == close (fd));
     165           0 :     GNUNET_free (dn);
     166           0 :     if (NULL != zspa)
     167             :     {
     168           0 :       MHD_destroy_response (zspa);
     169           0 :       zspa = NULL;
     170             :     }
     171           0 :     return GNUNET_SYSERR;
     172             :   }
     173          16 :   GNUNET_break (MHD_YES ==
     174             :                 MHD_add_response_header (spa,
     175             :                                          MHD_HTTP_HEADER_CONTENT_TYPE,
     176             :                                          "text/html"));
     177          16 :   return GNUNET_OK;
     178             : }
     179             : 
     180             : 
     181             : /**
     182             :  * Nicely shut down.
     183             :  */
     184             : void __attribute__ ((destructor))
     185          21 : get_spa_fini ()
     186             : {
     187          21 :   if (NULL != spa)
     188             :   {
     189          16 :     MHD_destroy_response (spa);
     190          16 :     spa = NULL;
     191             :   }
     192          21 :   if (NULL != zspa)
     193             :   {
     194          16 :     MHD_destroy_response (zspa);
     195          16 :     zspa = NULL;
     196             :   }
     197          21 : }

Generated by: LCOV version 1.14