37 #include <curl/curl.h>
39 #include "GatewayUtils.h"
40 #include "GatewayResponseNames.h"
43 #include <BESCatalogUtils.h>
44 #include <BESCatalogList.h>
45 #include <BESCatalog.h>
47 #include <TheBESKeys.h>
49 #include <BESInternalError.h>
50 #include <BESDapError.h>
51 #include <BESSyntaxUserError.h>
58 using namespace gateway;
61 std::vector<string> GatewayUtils::WhiteList;
63 std::map<string, string> GatewayUtils::MimeList;
64 string GatewayUtils::ProxyProtocol;
65 string GatewayUtils::ProxyHost;
66 string GatewayUtils::ProxyUser;
67 string GatewayUtils::ProxyPassword;
68 string GatewayUtils::ProxyUserPW;
69 int GatewayUtils::ProxyPort = 0;
70 int GatewayUtils::ProxyAuthType = 0;
71 bool GatewayUtils::useInternalCache =
false;
73 string GatewayUtils::NoProxyRegex;
77 void GatewayUtils::Initialize()
83 string key = Gateway_WHITELIST;
85 if (!found || WhiteList.size() == 0) {
86 string err = (string)
"The parameter " + Gateway_WHITELIST +
" is not set or has no values in the gateway"
87 +
" configuration file";
95 std::string key = Gateway_MIMELIST;
96 std::vector<string> vals;
98 if (found && vals.size()) {
99 std::vector<string>::iterator i = vals.begin();
100 std::vector<string>::iterator e = vals.end();
101 for (; i != e; i++) {
102 size_t colon = (*i).find(
":");
103 if (colon == string::npos) {
104 string err = (string)
"Malformed " + Gateway_MIMELIST +
" " + (*i)
105 +
" specified in the gateway configuration";
108 string mod = (*i).substr(0, colon);
109 string mime = (*i).substr(colon + 1);
110 MimeList[mod] = mime;
115 key = Gateway_PROXYHOST;
117 if (found && !GatewayUtils::ProxyHost.empty()) {
121 key = Gateway_PROXYPORT;
124 if (found && !port.empty()) {
125 GatewayUtils::ProxyPort = atoi(port.c_str());
126 if (!GatewayUtils::ProxyPort) {
127 string err = (string)
"gateway proxy host specified," +
" but proxy port specified is invalid";
136 key = Gateway_PROXYPROTOCOL;
138 if (!found || GatewayUtils::ProxyProtocol.empty()) {
139 GatewayUtils::ProxyProtocol =
"http";
145 key = Gateway_PROXYUSER;
148 GatewayUtils::ProxyUser =
"";
154 key = Gateway_PROXYPASSWORD;
157 GatewayUtils::ProxyPassword =
"";
163 key = Gateway_PROXYUSERPW;
166 GatewayUtils::ProxyUserPW =
"";
172 key = Gateway_PROXYAUTHTYPE;
177 if (authType ==
"basic") {
178 GatewayUtils::ProxyAuthType = CURLAUTH_BASIC;
179 BESDEBUG(
"gateway",
"GatewayUtils::Initialize() - ProxyAuthType BASIC set." << endl);
181 else if (authType ==
"digest") {
182 GatewayUtils::ProxyAuthType = CURLAUTH_DIGEST;
183 BESDEBUG(
"gateway",
"GatewayUtils::Initialize() - ProxyAuthType DIGEST set." << endl);
186 else if (authType ==
"ntlm") {
187 GatewayUtils::ProxyAuthType = CURLAUTH_NTLM;
188 BESDEBUG(
"gateway",
"GatewayUtils::Initialize() - ProxyAuthType NTLM set." << endl);
191 GatewayUtils::ProxyAuthType = CURLAUTH_BASIC;
193 "GatewayUtils::Initialize() - User supplied an invalid value '"<< authType <<
"' for Gateway.ProxyAuthType. Falling back to BASIC authentication scheme." << endl);
198 GatewayUtils::ProxyAuthType = CURLAUTH_BASIC;
204 key = Gateway_USE_INTERNAL_CACHE;
208 if (use_cache ==
"true" || use_cache ==
"TRUE" || use_cache ==
"True" || use_cache ==
"yes"
209 || use_cache ==
"YES" || use_cache ==
"Yes")
210 GatewayUtils::useInternalCache =
true;
212 GatewayUtils::useInternalCache =
false;
216 GatewayUtils::useInternalCache =
false;
242 GatewayUtils::Get_tempfile_template(
char *file_template )
246 Regex directory(
"[-a-zA-Z0-9_\\]*");
248 string c = getenv(
"TEMP") ? getenv(
"TEMP") :
"";
249 if (!c.empty() && directory.match(c.c_str(), c.length()) && (access(c.c_str(), 6) == 0))
250 goto valid_temp_directory;
252 c = getenv(
"TMP") ? getenv(
"TMP") :
"";
253 if (!c.empty() && directory.match(c.c_str(), c.length()) && (access(c.c_str(), 6) == 0))
254 goto valid_temp_directory;
257 Regex directory(
"[-a-zA-Z0-9_/]*");
259 string c = getenv(
"TMPDIR") ? getenv(
"TMPDIR") :
"";
260 if (!c.empty() && directory.match(c.c_str(), c.length())
261 && (access(c.c_str(), W_OK | R_OK) == 0))
262 goto valid_temp_directory;
265 if (access(P_tmpdir, W_OK | R_OK) == 0) {
267 goto valid_temp_directory;
274 valid_temp_directory:
281 c.append(file_template);
283 char *temp =
new char[c.length() + 1];
284 strncpy(temp, c.c_str(), c.length());
285 temp[c.length()] =
'\0';
304 void GatewayUtils::Get_type_from_disposition(
const string &disp,
string &type)
310 size_t fnpos = disp.find(
"filename");
311 if (fnpos != string::npos) {
314 size_t pos = disp.find(
"#", fnpos);
315 if (pos == string::npos) pos = disp.find(
"=", fnpos);
317 if (pos != string::npos) {
323 size_t sp = disp.find(
" ", pos);
324 if (pos != string::npos) {
326 filename = disp.substr(pos + 1, sp - pos - 1);
330 filename = disp.substr(pos + 1);
334 if (filename[0] ==
'"') {
335 filename = filename.substr(1);
337 if (filename[filename.length() - 1] ==
'"') {
338 filename = filename.substr(0, filename.length() - 1);
351 BESCatalogUtils::match_citer i = utils->match_list_begin();
352 BESCatalogUtils::match_citer ie = utils->match_list_end();
354 for (; i != ie && !done; i++) {
355 BESCatalogUtils::handler_regex match = (*i);
358 " Comparing disp filename " << filename <<
" against expr " << match.regex << endl);
359 BESRegex reg_expr(match.regex.c_str());
360 if (reg_expr.
match(filename.c_str(), filename.length()) ==
static_cast<int>(filename.length())) {
361 type = match.handler;
368 string serr = (string)
"Unable to match data type, " +
"malformed Catalog TypeMatch parameter "
369 +
"in bes configuration file around " + match.regex +
": " + e.get_error_message();
370 throw BESDapError(serr,
false, e.get_error_code(), __FILE__, __LINE__);
378 void GatewayUtils::Get_type_from_content_type(
const string &ctype,
string &type)
380 BESDEBUG(
"gateway",
"GatewayUtils::Get_type_from_content_type() - BEGIN" << endl);
381 map<string, string>::iterator i = MimeList.begin();
382 map<string, string>::iterator e = MimeList.end();
384 for (; i != e && !done; i++) {
386 "GatewayUtils::Get_type_from_content_type() - Comparing content type '" << ctype <<
"' against mime list element '" << (*i).second <<
"'"<< endl);
388 "GatewayUtils::Get_type_from_content_type() - first: " << (*i).first <<
" second: " << (*i).second << endl);
390 if ((*i).second == ctype) {
392 BESDEBUG(
"gateway",
"GatewayUtils::Get_type_from_content_type() - MATCH" << endl);
398 BESDEBUG(
"gateway",
"GatewayUtils::Get_type_from_content_type() - END" << endl);
401 void GatewayUtils::Get_type_from_url(
const string &url,
string &type)
411 BESCatalogUtils::match_citer i = utils->match_list_begin();
412 BESCatalogUtils::match_citer ie = utils->match_list_end();
414 for (; i != ie && !done; i++) {
415 BESCatalogUtils::handler_regex match = (*i);
418 "GatewayUtils::Get_type_from_url() - Comparing url " << url <<
" against type match expr " << match.regex << endl);
419 BESRegex reg_expr(match.regex.c_str());
420 if (reg_expr.match(url.c_str(), url.length()) ==
static_cast<int>(url.length())) {
421 type = match.handler;
423 BESDEBUG(
"gateway",
"GatewayUtils::Get_type_from_url() - MATCH type: " << type << endl);
427 string serr = (string)
"Unable to match data type, " +
"malformed Catalog TypeMatch parameter "
428 +
"in bes configuration file around " + match.regex +
": " + e.get_error_message();
437 bool GatewayUtils::Is_Whitelisted(
const std::string &url){
438 bool whitelisted =
false;
439 std::vector<std::string>::const_iterator i = WhiteList.begin();
440 std::vector<std::string>::const_iterator e = WhiteList.end();
441 for (; i != e && !whitelisted; i++) {
442 if ((*i).length() <= url.length()) {
443 if (url.substr(0, (*i).length()) == (*i)) {