metaproxy  1.21.0
Public Member Functions | Public Attributes | List of all members
metaproxy_1::filter::HttpRewrite::Phase Class Reference
Collaboration diagram for metaproxy_1::filter::HttpRewrite::Phase:
Collaboration graph

Public Member Functions

 Phase ()
 
void read_skip_headers (Z_HTTP_Request *hreq, std::list< boost::regex > &skip_list, std::string bind_addr)
 
void rewrite_reqline (mp::odr &o, Z_HTTP_Request *hreq, std::map< std::string, std::string > &vars, std::string bind_addr) const
 
void rewrite_headers (mp::odr &o, Z_HTTP_Header *headers, std::map< std::string, std::string > &vars) const
 
void rewrite_body (mp::odr &o, const char *content_type, char **content_buf, int *content_len, std::map< std::string, std::string > &vars, std::list< boost::regex > &skip_list) const
 

Public Attributes

int m_verbose
 
std::list< Contentcontent_list
 

Detailed Description

Definition at line 87 of file filter_http_rewrite.cpp.

Constructor & Destructor Documentation

◆ Phase()

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

Definition at line 759 of file filter_http_rewrite.cpp.

Member Function Documentation

◆ read_skip_headers()

void mp::filter::HttpRewrite::Phase::read_skip_headers ( Z_HTTP_Request *  hreq,
std::list< boost::regex > &  skip_list,
std::string  bind_addr 
)

Definition at line 182 of file filter_http_rewrite.cpp.

185 {
186  std::string url(hreq->path);
187  if ( url.substr(0,7) != "http://" && url.substr(0,8) != "https://")
188  { // path was relative, as it often is
189  // make absolute, so we can match the page regex against it
190  const char *host = z_HTTP_header_lookup(hreq->headers, "Host");
191  std::string proto;
192  if (bind_addr.find("ssl:") == 0) {
193  proto = "https";
194  } else {
195  proto = "http";
196  }
197  if (host)
198  url = proto + "://" + std::string(host) + hreq->path ;
199  }
200 
201  while ( const char *hv = z_HTTP_header_remove( &(hreq->headers),
202  "X-Metaproxy-SkipLink") )
203  {
204  yaz_log(YLOG_LOG,"Found SkipLink '%s'", hv );
205  const char *p = strchr(hv,' ');
206  if (!p)
207  continue; // should not happen
208  std::string page(hv,p);
209  std::string link(p+1);
210  boost::regex pagere(page);
211  if ( boost::regex_search(url, pagere) )
212  {
213  yaz_log(YLOG_LOG,"SkipLink '%s' matches URL %s",
214  page.c_str(), url.c_str() );
215  boost::regex linkre(link);
216  skip_list.push_back(linkre);
217  }
218  else
219  {
220  yaz_log(YLOG_LOG,"SkipLink ignored, '%s' does not match '%s'",
221  url.c_str(), page.c_str() );
222  }
223  }
224 }

◆ rewrite_body()

void mp::filter::HttpRewrite::Phase::rewrite_body ( mp::odr &  o,
const char *  content_type,
char **  content_buf,
int *  content_len,
std::map< std::string, std::string > &  vars,
std::list< boost::regex > &  skip_list 
) const

Definition at line 313 of file filter_http_rewrite.cpp.

320 {
321  if (*content_len == 0)
322  return;
323  if (!content_type) {
324  yaz_log(YLOG_LOG, "rewrite_body: null content_type, can not rewrite");
325  return;
326  }
327  std::list<Content>::const_iterator cit = content_list.begin();
328  for (; cit != content_list.end(); cit++)
329  {
330  yaz_log(YLOG_LOG, "rewrite_body: content_type=%s type=%s",
331  content_type, cit->type.c_str());
332  if (cit->type != "headers"
333  && regex_match(content_type, cit->content_re))
334  break;
335  }
336  if (cit == content_list.end()) {
337  yaz_log(YLOG_LOG,"rewrite_body: No content rule matched %s, not rewriting",
338  content_type );
339  return;
340  }
341 
342  int i;
343  for (i = 0; i < *content_len; i++)
344  if ((*content_buf)[i] == 0) {
345  yaz_log(YLOG_LOG,"rewrite_body: Looks like binary stuff, not rewriting");
346  return; // binary content. skip
347  }
348 
349  std::string content(*content_buf, *content_len);
350  cit->parse(m_verbose, content, vars, skip_list);
351  *content_buf = odr_strdup(o, content.c_str());
352  *content_len = strlen(*content_buf);
353 }

◆ rewrite_headers()

void mp::filter::HttpRewrite::Phase::rewrite_headers ( mp::odr &  o,
Z_HTTP_Header *  headers,
std::map< std::string, std::string > &  vars 
) const

Definition at line 281 of file filter_http_rewrite.cpp.

284 {
285  std::list<Content>::const_iterator cit = content_list.begin();
286  for (; cit != content_list.end(); cit++)
287  if (cit->type == "headers")
288  break;
289 
290  if (cit == content_list.end())
291  return;
292 
293  for (Z_HTTP_Header *header = headers; header; header = header->next)
294  {
295  std::list<Within>::const_iterator it = cit->within_list.begin();
296  for (; it != cit->within_list.end(); it++)
297  {
298  if (!it->header.empty() &&
299  regex_match(header->name, it->header))
300  {
301  // Match and replace only the header value
302  std::string hval(header->value);
303  std::list<boost::regex> dummy_skip_list; // no skips here!
304  if (it->exec(vars, hval, true, dummy_skip_list))
305  {
306  header->value = odr_strdup(o, hval.c_str());
307  }
308  }
309  }
310  }
311 }

◆ rewrite_reqline()

void mp::filter::HttpRewrite::Phase::rewrite_reqline ( mp::odr &  o,
Z_HTTP_Request *  hreq,
std::map< std::string, std::string > &  vars,
std::string  bind_addr 
) const

Definition at line 227 of file filter_http_rewrite.cpp.

231 {
232  std::string proto;
233  if (bind_addr.find("ssl:") == 0) {
234  proto = "https";
235  } else {
236  proto = "http";
237  }
238  yaz_log(YLOG_LOG,"rewrite_reqline: p='%s' ba='%s'",
239  hreq->path, proto.c_str() );
240  std::string path;
241  if ((strstr(hreq->path, "http://") == hreq->path) ||
242  (strstr(hreq->path, "https://") == hreq->path) )
243  {
244  yaz_log(YLOG_LOG, "Path in the method line is absolute, "
245  "possibly a proxy request"); // the usual case with cf_proxy
246  path = hreq->path;
247  }
248  else
249  {
250  const char *host = z_HTTP_header_lookup(hreq->headers, "Host");
251  if (!host)
252  return;
253 
254  path = proto + "://";
255  path += host;
256  path += hreq->path;
257  }
258 
259  std::list<Content>::const_iterator cit = content_list.begin();
260  for (; cit != content_list.end(); cit++)
261  if (cit->type == "headers")
262  break;
263 
264  if (cit == content_list.end())
265  return;
266 
267  std::list<Within>::const_iterator it = cit->within_list.begin();
268  for (; it != cit->within_list.end(); it++)
269  if (it->reqline)
270  {
271  yaz_log(YLOG_LOG, "Proxy request URL is %s", path.c_str());
272  std::list<boost::regex> dummy_skip_list; // no skips here!
273  if (it->exec(vars, path, true, dummy_skip_list))
274  {
275  yaz_log(YLOG_LOG, "Rewritten request URL is %s", path.c_str());
276  hreq->path = odr_strdup(o, path.c_str());
277  }
278  }
279 }

Member Data Documentation

◆ content_list

std::list<Content> metaproxy_1::filter::HttpRewrite::Phase::content_list

◆ m_verbose

int metaproxy_1::filter::HttpRewrite::Phase::m_verbose

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