+ std::list<Within>::const_iterator it = within_list.begin();
+ if (it != within_list.end())
+ embed_quoted_literal(content, vars, it->rule, false, skip_list);
+}
+
+void yf::HttpRewrite::Content::configure(
+ const xmlNode *ptr, std::map<std::string, RulePtr > &rules)
+{
+ for (; ptr; ptr = ptr->next)
+ {
+ if (ptr->type != XML_ELEMENT_NODE)
+ continue;
+ if (!strcmp((const char *) ptr->name, "within"))
+ {
+ static const char *names[7] =
+ { "header", "attr", "tag", "rule", "reqline", "type", 0 };
+ std::string values[6];
+ mp::xml::parse_attr(ptr, names, values);
+ Within w;
+ if (values[0].length() > 0)
+ w.header.assign(values[0], boost::regex_constants::icase);
+ if (values[1].length() > 0)
+ w.attr.assign(values[1], boost::regex_constants::icase);
+ if (values[2].length() > 0)
+ w.tag.assign(values[2], boost::regex_constants::icase);
+
+ std::vector<std::string> rulenames;
+ boost::split(rulenames, values[3], boost::is_any_of(","));
+ if (rulenames.size() == 0)
+ {
+ throw mp::filter::FilterException
+ ("Empty rule in '" + values[3] +
+ "' in http_rewrite filter");
+ }
+ else if (rulenames.size() == 1)
+ {
+ std::map<std::string,RulePtr>::const_iterator it =
+ rules.find(rulenames[0]);
+ if (it == rules.end())
+ throw mp::filter::FilterException
+ ("Reference to non-existing rule '" + rulenames[0] +
+ "' in http_rewrite filter");
+ w.rule = it->second;
+
+ }
+ else
+ {
+ RulePtr rule(new Rule);
+ size_t i;
+ for (i = 0; i < rulenames.size(); i++)
+ {
+ std::map<std::string,RulePtr>::const_iterator it =
+ rules.find(rulenames[i]);
+ if (it == rules.end())
+ throw mp::filter::FilterException
+ ("Reference to non-existing rule '" + rulenames[i] +
+ "' in http_rewrite filter");
+ RulePtr subRule = it->second;
+ std::list<Replace>::iterator rit =
+ subRule->replace_list.begin();
+ for (; rit != subRule->replace_list.end(); rit++)
+ rule->replace_list.push_back(*rit);
+ }
+ w.rule = rule;
+ }
+ w.reqline = values[4] == "1";
+ w.type = values[5];
+ if (w.type.empty() || w.type == "quoted-literal")
+ ;
+ else
+ throw mp::filter::FilterException
+ ("within type must be quoted-literal or none in "
+ " in http_rewrite filter");
+ within_list.push_back(w);
+ }
+ }
+}
+
+void yf::HttpRewrite::configure_phase(const xmlNode *ptr, Phase &phase)
+{
+ static const char *names[2] = { "verbose", 0 };
+ std::string values[1];
+ values[0] = "0";
+ mp::xml::parse_attr(ptr, names, values);
+
+ phase.m_verbose = atoi(values[0].c_str());
+
+ std::map<std::string, RulePtr > rules;
+ for (ptr = ptr->children; ptr; ptr = ptr->next)
+ {
+ if (ptr->type != XML_ELEMENT_NODE)
+ continue;
+ else if (!strcmp((const char *) ptr->name, "rule"))
+ {
+ static const char *names[2] = { "name", 0 };
+ std::string values[1];
+ values[0] = "default";
+ mp::xml::parse_attr(ptr, names, values);
+
+ RulePtr rule(new Rule);
+ for (xmlNode *p = ptr->children; p; p = p->next)
+ {
+ if (p->type != XML_ELEMENT_NODE)
+ continue;
+ if (!strcmp((const char *) p->name, "rewrite"))
+ {
+ Replace replace;
+ std::string from;
+ const struct _xmlAttr *attr;
+ for (attr = p->properties; attr; attr = attr->next)
+ {
+ if (!strcmp((const char *) attr->name, "from"))
+ from = mp::xml::get_text(attr->children);
+ else if (!strcmp((const char *) attr->name, "to"))
+ replace.recipe = mp::xml::get_text(attr->children);
+ else
+ throw mp::filter::FilterException
+ ("Bad attribute "
+ + std::string((const char *) attr->name)
+ + " in rewrite section of http_rewrite");
+ }
+ yaz_log(YLOG_LOG, "Found rewrite rule from '%s' to '%s'",
+ from.c_str(), replace.recipe.c_str());
+ if (!from.empty())
+ {
+ replace.parse_groups(from);
+ rule->replace_list.push_back(replace);
+ }
+ }
+ else
+ throw mp::filter::FilterException
+ ("Bad element "
+ + std::string((const char *) p->name)
+ + " in http_rewrite filter");
+ }
+ rules[values[0]] = rule;
+ }
+ else if (!strcmp((const char *) ptr->name, "content"))
+ {
+ static const char *names[3] =
+ { "type", "mime", 0 };
+ std::string values[2];
+ mp::xml::parse_attr(ptr, names, values);
+ if (values[0].empty())
+ {
+ throw mp::filter::FilterException
+ ("Missing attribute, type for for element "
+ + std::string((const char *) ptr->name)
+ + " in http_rewrite filter");
+ }
+ Content c;
+
+ c.type = values[0];
+ if (!values[1].empty())
+ c.content_re.assign(values[1], boost::regex::icase);
+ c.configure(ptr->children, rules);
+ phase.content_list.push_back(c);
+ }
+ else
+ {
+ throw mp::filter::FilterException
+ ("Bad element "
+ + std::string((const char *) ptr->name)
+ + " in http_rewrite filter");
+ }
+ }
+}
+
+void yf::HttpRewrite::configure(const xmlNode * ptr, bool test_only,
+ const char *path)
+{
+ for (ptr = ptr->children; ptr; ptr = ptr->next)
+ {
+ if (ptr->type != XML_ELEMENT_NODE)
+ continue;
+ else if (!strcmp((const char *) ptr->name, "request"))
+ {
+ configure_phase(ptr, *req_phase);
+ }
+ else if (!strcmp((const char *) ptr->name, "response"))
+ {
+ configure_phase(ptr, *res_phase);
+ }
+ else
+ {
+ throw mp::filter::FilterException
+ ("Bad element "
+ + std::string((const char *) ptr->name)
+ + " in http_rewrite1 filter");
+ }
+ }