paivana-httpd_helper.c (3515B)
1 /* 2 This file is part of GNUnet. 3 Copyright (C) 2026 Taler Systems SA 4 5 Paivana is free software; you can redistribute it and/or 6 modify it under the terms of the GNU General Public License 7 as published by the Free Software Foundation; either version 8 3, or (at your option) any later version. 9 10 Paivana is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty 12 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 13 the GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public 16 License along with Paivana; see the file COPYING. If not, 17 write to the Free Software Foundation, Inc., 51 Franklin 18 Street, Fifth Floor, Boston, MA 02110-1301, USA. 19 */ 20 21 /** 22 * @author Christian Grothoff 23 * @file paivana-httpd_helper.c 24 * @brief helper functions 25 */ 26 #include "paivana-httpd.h" 27 #include "paivana-httpd_helper.h" 28 #include <taler/taler_mhd_lib.h> 29 30 bool 31 PAIVANA_HTTPD_get_client_address (struct MHD_Connection *connection, 32 void **ca, 33 size_t *ca_len) 34 { 35 const union MHD_ConnectionInfo *ci; 36 const struct sockaddr *sa; 37 socklen_t sa_len; 38 39 // FIXME: also support getting client address from HTTP 40 // headers instead (in case of reverse proxy). 41 ci = MHD_get_connection_info (connection, 42 MHD_CONNECTION_INFO_CLIENT_ADDRESS); 43 GNUNET_assert (NULL != ci); 44 sa = ci->client_addr; 45 switch (sa->sa_family) 46 { 47 case AF_INET: 48 sa_len = sizeof (struct sockaddr_in); 49 break; 50 case AF_INET6: 51 sa_len = sizeof (struct sockaddr_in6); 52 break; 53 default: 54 GNUNET_break (0); 55 *ca = NULL; 56 *ca_len = 0; 57 return false; 58 } 59 ca = GNUNET_memdup (sa, 60 sa_len); 61 *ca_len = sa_len; 62 return true; 63 } 64 65 66 bool 67 PAIVANA_HTTPD_get_base_url (struct MHD_Connection *connection, 68 struct GNUNET_Buffer *buf) 69 { 70 const char *host; 71 const char *forwarded_host; 72 const char *forwarded_port; 73 74 GNUNET_buffer_clear (buf); 75 if (NULL != PH_base_url) 76 { 77 GNUNET_buffer_write_str (buf, 78 PH_base_url); 79 return true; 80 } 81 if (GNUNET_YES == 82 TALER_mhd_is_https (connection)) 83 GNUNET_buffer_write_str (buf, 84 "https://"); 85 else 86 GNUNET_buffer_write_str (buf, 87 "http://"); 88 host = MHD_lookup_connection_value (connection, 89 MHD_HEADER_KIND, 90 MHD_HTTP_HEADER_HOST); 91 forwarded_host = MHD_lookup_connection_value (connection, 92 MHD_HEADER_KIND, 93 "X-Forwarded-Host"); 94 if (NULL != forwarded_host) 95 { 96 GNUNET_buffer_write_str (buf, 97 forwarded_host); 98 } 99 else 100 { 101 if (NULL == host) 102 { 103 GNUNET_break (0); 104 return false; 105 } 106 GNUNET_buffer_write_str (buf, 107 host); 108 } 109 forwarded_port = MHD_lookup_connection_value (connection, 110 MHD_HEADER_KIND, 111 "X-Forwarded-Port"); 112 if (NULL != forwarded_port) 113 { 114 GNUNET_buffer_write_str (buf, 115 ":"); 116 GNUNET_buffer_write_str (buf, 117 forwarded_port); 118 } 119 return true; 120 }