metaproxy  1.21.0
Classes | Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
metaproxy_1::filter::HttpRewrite Class Reference

#include <filter_http_rewrite.hpp>

Inheritance diagram for metaproxy_1::filter::HttpRewrite:
Inheritance graph
Collaboration diagram for metaproxy_1::filter::HttpRewrite:
Collaboration graph

Classes

class  Content
 
class  Event
 
class  Phase
 
class  Replace
 
class  Rule
 
class  Within
 

Public Types

typedef boost::shared_ptr< RuleRulePtr
 

Public Member Functions

 HttpRewrite ()
 
 ~HttpRewrite ()
 
void process (metaproxy_1::Package &package) const
 
void configure (const xmlNode *ptr, bool test_only, const char *path)
 

Private Member Functions

void configure_phase (const xmlNode *ptr, Phase &phase)
 

Private Attributes

boost::scoped_ptr< Phasereq_phase
 
boost::scoped_ptr< Phaseres_phase
 

Detailed Description

Definition at line 30 of file filter_http_rewrite.hpp.

Member Typedef Documentation

◆ RulePtr

typedef boost::shared_ptr<Rule> metaproxy_1::filter::HttpRewrite::RulePtr

Definition at line 41 of file filter_http_rewrite.hpp.

Constructor & Destructor Documentation

◆ HttpRewrite()

mp::filter::HttpRewrite::HttpRewrite ( )

Definition at line 128 of file filter_http_rewrite.cpp.

128  :
129  req_phase(new Phase), res_phase(new Phase)
130 {
131 }
boost::scoped_ptr< Phase > res_phase
boost::scoped_ptr< Phase > req_phase

◆ ~HttpRewrite()

mp::filter::HttpRewrite::~HttpRewrite ( )

Definition at line 133 of file filter_http_rewrite.cpp.

134 {
135 }

Member Function Documentation

◆ configure()

void mp::filter::HttpRewrite::configure ( const xmlNode *  ptr,
bool  test_only,
const char *  path 
)

Definition at line 958 of file filter_http_rewrite.cpp.

960 {
961  for (ptr = ptr->children; ptr; ptr = ptr->next)
962  {
963  if (ptr->type != XML_ELEMENT_NODE)
964  continue;
965  else if (!strcmp((const char *) ptr->name, "request"))
966  {
967  configure_phase(ptr, *req_phase);
968  }
969  else if (!strcmp((const char *) ptr->name, "response"))
970  {
971  configure_phase(ptr, *res_phase);
972  }
973  else
974  {
975  throw mp::filter::FilterException
976  ("Bad element "
977  + std::string((const char *) ptr->name)
978  + " in http_rewrite filter");
979  }
980  }
981 }
void configure_phase(const xmlNode *ptr, Phase &phase)

References configure_phase(), req_phase, and res_phase.

Here is the call graph for this function:

◆ configure_phase()

void mp::filter::HttpRewrite::configure_phase ( const xmlNode *  ptr,
Phase phase 
)
private

Definition at line 868 of file filter_http_rewrite.cpp.

869 {
870  static const char *names[2] = { "verbose", 0 };
871  std::string values[1];
872  values[0] = "0";
873  mp::xml::parse_attr(ptr, names, values);
874 
875  phase.m_verbose = atoi(values[0].c_str());
876 
877  std::map<std::string, RulePtr > rules;
878  for (ptr = ptr->children; ptr; ptr = ptr->next)
879  {
880  if (ptr->type != XML_ELEMENT_NODE)
881  continue;
882  else if (!strcmp((const char *) ptr->name, "rule"))
883  {
884  static const char *names[2] = { "name", 0 };
885  std::string values[1];
886  values[0] = "default";
887  mp::xml::parse_attr(ptr, names, values);
888 
889  RulePtr rule(new Rule);
890  for (xmlNode *p = ptr->children; p; p = p->next)
891  {
892  if (p->type != XML_ELEMENT_NODE)
893  continue;
894  if (!strcmp((const char *) p->name, "rewrite"))
895  {
896  Replace replace;
897  std::string from;
898  const struct _xmlAttr *attr;
899  for (attr = p->properties; attr; attr = attr->next)
900  {
901  if (!strcmp((const char *) attr->name, "from"))
902  from = mp::xml::get_text(attr->children);
903  else if (!strcmp((const char *) attr->name, "to"))
904  replace.recipe = mp::xml::get_text(attr->children);
905  else
906  throw mp::filter::FilterException
907  ("Bad attribute "
908  + std::string((const char *) attr->name)
909  + " in rewrite section of http_rewrite");
910  }
911  yaz_log(YLOG_LOG, "Found rewrite rule from '%s' to '%s'",
912  from.c_str(), replace.recipe.c_str());
913  if (!from.empty())
914  {
915  replace.parse_groups(from);
916  rule->replace_list.push_back(replace);
917  }
918  }
919  else
920  throw mp::filter::FilterException
921  ("Bad element "
922  + std::string((const char *) p->name)
923  + " in http_rewrite filter");
924  }
925  rules[values[0]] = rule;
926  }
927  else if (!strcmp((const char *) ptr->name, "content"))
928  {
929  static const char *names[3] =
930  { "type", "mime", 0 };
931  std::string values[2];
932  mp::xml::parse_attr(ptr, names, values);
933  if (values[0].empty())
934  {
935  throw mp::filter::FilterException
936  ("Missing attribute, type for for element "
937  + std::string((const char *) ptr->name)
938  + " in http_rewrite filter");
939  }
940  Content c;
941 
942  c.type = values[0];
943  if (!values[1].empty())
944  c.content_re.assign(values[1], boost::regex::icase);
945  c.configure(ptr->children, rules);
946  phase.content_list.push_back(c);
947  }
948  else
949  {
950  throw mp::filter::FilterException
951  ("Bad element "
952  + std::string((const char *) ptr->name)
953  + " in http_rewrite filter");
954  }
955  }
956 }
boost::shared_ptr< Rule > RulePtr

References metaproxy_1::filter::HttpRewrite::Content::configure(), metaproxy_1::filter::HttpRewrite::Phase::content_list, metaproxy_1::filter::HttpRewrite::Content::content_re, metaproxy_1::filter::HttpRewrite::Phase::m_verbose, metaproxy_1::filter::HttpRewrite::Replace::parse_groups(), metaproxy_1::filter::HttpRewrite::Replace::recipe, and metaproxy_1::filter::HttpRewrite::Content::type.

Referenced by configure().

Here is the call graph for this function:

◆ process()

void mp::filter::HttpRewrite::process ( metaproxy_1::Package &  package) const

Definition at line 137 of file filter_http_rewrite.cpp.

138 {
139  yaz_log(YLOG_LOG, "HttpRewrite begins....");
140  Z_GDU *gdu = package.request().get();
141  //map of request/response vars
142  std::map<std::string, std::string> vars;
143  //we have an http req
144 
145  std::list<boost::regex> skip_list;
146 
147  if (gdu && gdu->which == Z_GDU_HTTP_Request)
148  {
149  Z_HTTP_Request *hreq = gdu->u.HTTP_Request;
150  mp::odr o;
151  std::string bind_addr = package.origin(). get_bind_address();
152  req_phase->rewrite_reqline(o, hreq, vars, bind_addr);
153  res_phase->read_skip_headers(hreq, skip_list, bind_addr);
154  yaz_log(YLOG_LOG, ">> Request headers");
155  req_phase->rewrite_headers(o, hreq->headers, vars);
156  req_phase->rewrite_body(o,
157  z_HTTP_header_lookup(hreq->headers,
158  "Content-Type"),
159  &hreq->content_buf, &hreq->content_len,
160  vars, skip_list);
161  package.request() = gdu;
162  }
163  package.move();
164  gdu = package.response().get();
165  if (gdu && gdu->which == Z_GDU_HTTP_Response)
166  {
167  Z_HTTP_Response *hres = gdu->u.HTTP_Response;
168  yaz_log(YLOG_LOG, "Response code %d", hres->code);
169  mp::odr o;
170  yaz_log(YLOG_LOG, "<< Respose headers");
171  res_phase->rewrite_headers(o, hres->headers, vars);
172  res_phase->rewrite_body(o,
173  z_HTTP_header_lookup(hres->headers,
174  "Content-Type"),
175  &hres->content_buf, &hres->content_len,
176  vars, skip_list);
177  package.response() = gdu;
178  }
179 }

Member Data Documentation

◆ req_phase

boost::scoped_ptr<Phase> metaproxy_1::filter::HttpRewrite::req_phase
private

Definition at line 36 of file filter_http_rewrite.hpp.

Referenced by configure().

◆ res_phase

boost::scoped_ptr<Phase> metaproxy_1::filter::HttpRewrite::res_phase
private

Definition at line 37 of file filter_http_rewrite.hpp.

Referenced by configure().


The documentation for this class was generated from the following files: