43#include "BESCatalogUtils.h"
44#include "BESCatalogList.h"
45#include "BESCatalog.h"
47#include "TheBESKeys.h"
48#include "BESInternalError.h"
49#include "BESNotFoundError.h"
50#include "BESSyntaxUserError.h"
56#include "ProxyConfig.h"
65#define prolog string("HttpUtils::").append(__func__).append("() - ")
78 if (found && vals.size()) {
82 size_t colon = (*i).find(
":");
83 if (colon == string::npos) {
84 string err = (string)
"Malformed " + HTTP_MIMELIST_KEY +
" " + (*i) +
85 " specified in the gateway configuration";
88 string mod = (*i).substr(0, colon);
89 string mime = (*i).substr(colon + 1);
90 mime_list[mod] = mime;
116 size_t fnpos = disp.find(
"filename");
117 if (fnpos != string::npos) {
120 size_t pos = disp.find(
"#", fnpos);
121 if (pos == string::npos) pos = disp.find(
"=", fnpos);
122 if (pos != string::npos) {
128 size_t sp = disp.find(
" ", pos);
129 if (pos != string::npos) {
131 filename = disp.substr(pos + 1, sp - pos - 1);
134 filename = disp.substr(pos + 1);
142 const BESCatalogUtils *utils = BESCatalogList::TheCatalogList()->default_catalog()->get_catalog_utils();
148void get_type_from_content_type(
const string &ctype,
string &type)
150 BESDEBUG(MODULE, prolog <<
"BEGIN content-type: " << ctype << endl);
151 map<string,string> mime_list;
156 for (; i != e && !done; i++) {
157 BESDEBUG(MODULE, prolog <<
"Comparing content type '" << ctype <<
"' against mime list element '" << (*i).second <<
"'" << endl);
158 BESDEBUG(MODULE, prolog <<
"first: " << (*i).first <<
" second: " << (*i).second << endl);
159 if ((*i).second == ctype) {
160 BESDEBUG(MODULE, prolog <<
"MATCH" << endl);
165 BESDEBUG(MODULE, prolog <<
"END" << endl);
168void get_type_from_url(
const string &
url,
string &type) {
169 const BESCatalogUtils *utils = BESCatalogList::TheCatalogList()->find_catalog(
"catalog")->
get_catalog_utils();
179 size_t max_redirects=0;
183 if (found && !value.empty()) {
184 std::istringstream(value) >> max_redirects;
187 max_redirects = HTTP_MAX_REDIRECTS_DEFAULT;
189 return max_redirects;
202void hexchar(
const unsigned char &c,
unsigned char &hex1,
unsigned char &hex2)
206 hex1 += hex1 <= 9 ?
'0' :
'a' - 10;
207 hex2 += hex2 <= 9 ?
'0' :
'a' - 10;
217 const char *str = s.c_str();
218 vector<char> v(s.size());
220 for (
size_t i = 0, l = s.size(); i < l; i++)
223 if ((c >=
'0' && c <=
'9') ||
224 (c >=
'a' && c <=
'z') ||
225 (c >=
'A' && c <=
'Z') ||
226 c ==
'-' || c ==
'_' || c ==
'.' || c ==
'!' || c ==
'~' ||
227 c ==
'*' || c ==
'\'' || c ==
'(' || c ==
')')
238 unsigned char d1, d2;
245 return {v.cbegin(), v.cend()};
265 void HttpUtils::decompose_url(
const string target_url, map<string,string> &url_info)
270 size_t query_index = target_url.find_first_of(
"?");
271 BESDEBUG(MODULE, prolog <<
"query_index: " << query_index << endl);
272 if(query_index != string::npos){
273 query_string = target_url.substr(query_index+1);
274 url_base = target_url.substr(0,query_index);
277 url_base = target_url;
279 url_info.insert( std::pair<string,string>(HTTP_TARGET_URL_KEY,target_url));
280 BESDEBUG(MODULE, prolog << HTTP_TARGET_URL_KEY <<
": " << target_url << endl);
281 url_info.insert( std::pair<string,string>(HTTP_URL_BASE_KEY,url_base));
282 BESDEBUG(MODULE, prolog << HTTP_URL_BASE_KEY <<
": " << url_base << endl);
283 url_info.insert( std::pair<string,string>(HTTP_QUERY_STRING_KEY,query_string));
284 BESDEBUG(MODULE, prolog << HTTP_QUERY_STRING_KEY <<
": " << query_string << endl);
285 if(!query_string.empty()){
286 vector<string> records;
287 string delimiters =
"&";
289 vector<string>::iterator i = records.begin();
290 for(; i!=records.end(); i++){
291 size_t index = i->find(
'=');
292 if(index != string::npos) {
293 string key = i->substr(0, index);
294 string value = i->substr(index+1);
295 BESDEBUG(MODULE, prolog <<
"key: " << key <<
" value: " << value << endl);
296 url_info.insert( std::pair<string,string>(key,value));
302 stringstream unix_time;
304 url_info.insert( std::pair<string,string>(HTTP_INGEST_TIME_KEY,unix_time.str()));
std::string get_handler_name(const std::string &item) const
Find the handler name that will process.
virtual BESCatalogUtils * get_catalog_utils() const
Get a pointer to the utilities, customized for this catalog.
error thrown if there is a user syntax error in the request or any other user error
static void tokenize(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters="/")
static void trim_if_surrounding_quotes(std::string &value)
Remove double quotes around a string This function will remove a leading and/or trailing double quote...
void get_value(const std::string &s, std::string &val, bool &found)
Retrieve the value of a given key, if set.
static TheBESKeys * TheKeys()
Access to the singleton.
void get_values(const std::string &s, std::vector< std::string > &vals, bool &found)
Retrieve the values of a given key, if set.
Parse a URL into the protocol, host, path and query parts.
utility class for the HTTP catalog module
size_t load_max_redirects_from_keys()
void get_type_from_disposition(const string &disp, string &type)
string url_encode(const string &s)
void load_mime_list_from_keys(map< string, string > &mime_list)
void hexchar(const unsigned char &c, unsigned char &hex1, unsigned char &hex2)