Line data Source code
1 : /* 2 : This file is part of TALER 3 : Copyright (C) 2014-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 mhd.c 18 : * @brief MHD utility functions (used by the merchant backend) 19 : * @author Christian Grothoff 20 : */ 21 : #include "platform.h" 22 : #include "taler_util.h" 23 : 24 : 25 : /** 26 : * Find out if an MHD connection is using HTTPS (either 27 : * directly or via proxy). 28 : * 29 : * @param connection MHD connection 30 : * @returns #GNUNET_YES if the MHD connection is using https, 31 : * #GNUNET_NO if the MHD connection is using http, 32 : * #GNUNET_SYSERR if the connection type couldn't be determined 33 : */ 34 : enum GNUNET_GenericReturnValue 35 0 : TALER_mhd_is_https (struct MHD_Connection *connection) 36 : { 37 : const union MHD_ConnectionInfo *ci; 38 : const union MHD_DaemonInfo *di; 39 0 : const char *forwarded_proto = MHD_lookup_connection_value (connection, 40 : MHD_HEADER_KIND, 41 : "X-Forwarded-Proto"); 42 : 43 0 : if (NULL != forwarded_proto) 44 : { 45 0 : if (0 == strcasecmp (forwarded_proto, 46 : "https")) 47 0 : return GNUNET_YES; 48 0 : if (0 == strcasecmp (forwarded_proto, 49 : "http")) 50 0 : return GNUNET_NO; 51 0 : GNUNET_break (0); 52 0 : return GNUNET_SYSERR; 53 : } 54 : /* likely not reverse proxy, figure out if we are 55 : http by asking MHD */ 56 0 : ci = MHD_get_connection_info (connection, 57 : MHD_CONNECTION_INFO_DAEMON); 58 0 : if (NULL == ci) 59 : { 60 0 : GNUNET_break (0); 61 0 : return GNUNET_SYSERR; 62 : } 63 0 : di = MHD_get_daemon_info (ci->daemon, 64 : MHD_DAEMON_INFO_FLAGS); 65 0 : if (NULL == di) 66 : { 67 0 : GNUNET_break (0); 68 0 : return GNUNET_SYSERR; 69 : } 70 0 : if (0 != (di->flags & MHD_USE_TLS)) 71 0 : return GNUNET_YES; 72 0 : return GNUNET_NO; 73 : } 74 : 75 : 76 : /** 77 : * Make an absolute URL for a given MHD connection. 78 : * 79 : * @param connection the connection to get the URL for 80 : * @param path path of the url 81 : * @param ... NULL-terminated key-value pairs (char *) for query parameters, 82 : * the value will be url-encoded 83 : * @returns the URL, must be freed with #GNUNET_free 84 : */ 85 : char * 86 0 : TALER_url_absolute_mhd (struct MHD_Connection *connection, 87 : const char *path, 88 : ...) 89 : { 90 : /* By default we assume we're running under HTTPS */ 91 : const char *proto; 92 : const char *host; 93 : const char *forwarded_host; 94 : const char *prefix; 95 : va_list args; 96 : char *result; 97 : 98 0 : if (GNUNET_YES == TALER_mhd_is_https (connection)) 99 0 : proto = "https"; 100 : else 101 0 : proto = "http"; 102 : 103 0 : host = MHD_lookup_connection_value (connection, 104 : MHD_HEADER_KIND, 105 : "Host"); 106 0 : forwarded_host = MHD_lookup_connection_value (connection, 107 : MHD_HEADER_KIND, 108 : "X-Forwarded-Host"); 109 : 110 0 : prefix = MHD_lookup_connection_value (connection, 111 : MHD_HEADER_KIND, 112 : "X-Forwarded-Prefix"); 113 0 : if (NULL == prefix) 114 0 : prefix = ""; 115 : 116 0 : if (NULL != forwarded_host) 117 0 : host = forwarded_host; 118 : 119 0 : if (NULL == host) 120 : { 121 : /* Should never happen, at last the host header should be defined */ 122 0 : GNUNET_break (0); 123 0 : return NULL; 124 : } 125 : 126 0 : va_start (args, 127 : path); 128 0 : result = TALER_url_absolute_raw_va (proto, 129 : host, 130 : prefix, 131 : path, 132 : args); 133 0 : va_end (args); 134 0 : return result; 135 : }