X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Furl_recipe.cpp;h=3386d8f8165de687871a69677b165b9cd65bd80f;hb=99d83cbd75e1a9ff8e0275bd081afc514555fb4b;hp=80769281658454a8c3f88f14d421370aadad8895;hpb=e5329397729e8959a821c6956f75012372fc0a77;p=metaproxy-moved-to-github.git diff --git a/src/url_recipe.cpp b/src/url_recipe.cpp index 8076928..3386d8f 100644 --- a/src/url_recipe.cpp +++ b/src/url_recipe.cpp @@ -1,5 +1,5 @@ /* This file is part of Metaproxy. - Copyright (C) 2005-2011 Index Data + Copyright (C) Index Data Metaproxy is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free @@ -18,6 +18,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include "config.hpp" +#include #include #include @@ -38,17 +39,28 @@ std::string mp_xml::url_recipe_handle(xmlDoc *doc, std::string recipe) ptr1 = ptr1->children; size_t p0 = 0; - for (;;) + while (p0 < recipe.length()) { - size_t p1 = recipe.find_first_of("${", p0); - if (p1 == std::string::npos) + bool uri_encode = false; + if (p0 < recipe.length() -1 && recipe[p0] == '%' + && recipe[p0 + 1] == '{') { - result += recipe.substr(p0); - break; + p0++; + p0++; + uri_encode = true; + } + else if (p0 < recipe.length() -1 && recipe[p0] == '$' + && recipe[p0 + 1] == '{') + { + p0++; + p0++; + } + else + { + result += recipe[p0]; + p0++; + continue; } - result += recipe.substr(p0, p1 - p0); - p0 = p1+2; - int step = 0; // 0=variable, 1=pattern, 2=replacement, 3=mode std::string variable; std::string pattern; @@ -61,7 +73,7 @@ std::string mp_xml::url_recipe_handle(xmlDoc *doc, std::string recipe) int c_check = c; if (c_prev == '\\') c_check = 0; - + if (c_check == '}') step = 5; else if (step == 0) @@ -117,15 +129,32 @@ std::string mp_xml::url_recipe_handle(xmlDoc *doc, std::string recipe) break; } } - boost::regex::flag_type b_mode = boost::regex::perl; - if (mode.find_first_of('i') != std::string::npos) - b_mode |= boost::regex::icase; - boost::regex e(pattern, b_mode); - - boost::match_flag_type match_mode = boost::format_first_only; - if (mode.find_first_of('g') != std::string::npos) - match_mode = boost::format_all; - result += regex_replace(text, e, replacement, match_mode); + if (pattern.length() > 0) + { + boost::regex::flag_type b_mode = boost::regex::perl; + if (mode.find_first_of('i') != std::string::npos) + b_mode |= boost::regex::icase; + boost::regex e(pattern, b_mode); + boost::match_flag_type match_mode = boost::format_first_only; + if (mode.find_first_of('g') != std::string::npos) + match_mode = boost::format_all; + text = regex_replace(text, e, replacement, match_mode); + } + if (!uri_encode) + result += text; + else + { + char src[2]; + char dst[4]; + size_t i; + for (i = 0; i < text.length(); i++) + { + src[0] = text[i]; + src[1] = '\0'; + yaz_encode_uri_component(dst, src); + result += dst; + } + } } } return result;