Example #1
0
VirtualHost::VirtualHost() : m_disabled(false) {
  Hdf empty;
  initRuntimeOption(empty);
}
Example #2
0
void VirtualHost::init(const IniSetting::Map& ini, Hdf vh) {
  m_name = vh.getName();

  const char *prefix = Config::Get(ini, vh, "Prefix", "", false);
  const char *pattern = Config::Get(ini, vh, "Pattern", "", false);
  const char *pathTranslation = Config::Get(ini, vh, "PathTranslation", "",
                                            false);

  if (prefix) m_prefix = prefix;
  if (pattern) {
    m_pattern = format_pattern(pattern, false);
    if (!m_pattern.empty()) {
      m_pattern += "i"; // case-insensitive
    }
  }
  if (pathTranslation) {
    m_pathTranslation = pathTranslation;
    if (!m_pathTranslation.empty() &&
        m_pathTranslation[m_pathTranslation.length() - 1] != '/') {
      m_pathTranslation += '/';
    }
  }

  initRuntimeOption(ini, vh); // overwrites

  m_disabled = Config::GetBool(ini, vh, "Disabled", false, false);

  m_checkExistenceBeforeRewrite =
    Config::GetBool(ini, vh, "CheckExistenceBeforeRewrite", true, false);

  for (Hdf hdf = vh["RewriteRules"].firstChild(); hdf.exists();
       hdf = hdf.next()) {
    RewriteRule dummy;
    m_rewriteRules.push_back(dummy);
    RewriteRule &rule = m_rewriteRules.back();
    rule.pattern = format_pattern(Config::GetString(ini, hdf, "pattern", "",
                                                    false),
                                  true);
    rule.to = Config::GetString(ini, hdf, "to", "", false);
    rule.qsa = Config::GetBool(ini, hdf, "qsa", false, false);
    rule.redirect = Config::GetInt16(ini, hdf, "redirect", 0, false);
    rule.encode_backrefs = Config::GetBool(ini, hdf, "encode_backrefs", false,
                                           false);

    if (rule.pattern.empty() || rule.to.empty()) {
      throw std::runtime_error("Invalid rewrite rule: (empty pattern or to)");
    }

    for (Hdf chdf = hdf["conditions"].firstChild(); chdf.exists();
         chdf = chdf.next()) {
      RewriteCond dummy;
      rule.rewriteConds.push_back(dummy);
      RewriteCond &cond = rule.rewriteConds.back();
      cond.pattern = format_pattern(Config::GetString(ini, chdf, "pattern", "",
                                                      false),
                                    true);
      if (cond.pattern.empty()) {
        throw std::runtime_error("Invalid rewrite rule: (empty cond pattern)");
      }
      const char *type = Config::Get(ini, chdf, "type", "", false);
      if (type) {
        if (strcasecmp(type, "host") == 0) {
          cond.type = RewriteCond::Type::Host;
        } else if (strcasecmp(type, "request") == 0) {
          cond.type = RewriteCond::Type::Request;
        } else {
          throw std::runtime_error("Invalid rewrite rule: (invalid "
            "cond type)");
        }
      } else {
        cond.type = RewriteCond::Type::Request;
      }
      cond.negate = Config::GetBool(ini, chdf, "negate", false, false);
    }

  }

  if (vh["IpBlockMap"].firstChild().exists()) {
    m_ipBlocks = std::make_shared<IpBlockMap>(ini, vh["IpBlockMap"]);
  }

  for (Hdf hdf = vh["LogFilters"].firstChild(); hdf.exists();
       hdf = hdf.next()) {
    QueryStringFilter filter;
    filter.urlPattern = format_pattern(Config::GetString(ini, hdf, "url", "",
                                                         false),
                                       true);
    filter.replaceWith = Config::GetString(ini, hdf, "value", "", false);
    filter.replaceWith = "\\1=" + filter.replaceWith;

    std::string pattern = Config::GetString(ini, hdf, "pattern", "", false);
    std::vector<std::string> names;
    names = Config::GetVector(ini, hdf, "params", names, false);

    if (pattern.empty()) {
      for (unsigned int i = 0; i < names.size(); i++) {
        if (pattern.empty()) {
          pattern = "(?<=[&\?])(";
        } else {
          pattern += "|";
        }
        pattern += names[i];
      }
      if (!pattern.empty()) {
        pattern += ")=.*?(?=(&|$))";
        pattern = format_pattern(pattern, false);
      }
    } else if (!names.empty()) {
      throw std::runtime_error("Invalid log filter: (cannot specify "
        "both params and pattern)");
    }

    filter.namePattern = pattern;
    m_queryStringFilters.push_back(filter);
  }

  m_serverVars = Config::GetMap(ini, vh, "ServerVariables", m_serverVars,
                                false);
  m_serverName = Config::GetString(ini, vh, "ServerName", m_serverName, false);
}
Example #3
0
void VirtualHost::init(Hdf vh) {
  m_name = vh.getName();

  const char *prefix = vh["Prefix"].get("");
  const char *pattern = vh["Pattern"].get("");
  const char *pathTranslation = vh["PathTranslation"].get("");
  Hdf overwrite = vh["overwrite"];
  initRuntimeOption(overwrite);

  if (prefix) m_prefix = prefix;
  if (pattern) {
    m_pattern = Util::format_pattern(pattern, true);
    if (!m_pattern.empty()) {
      m_pattern += "i"; // case-insensitive
    }
  }
  if (pathTranslation) {
    m_pathTranslation = pathTranslation;
    if (!m_pathTranslation.empty() &&
        m_pathTranslation[m_pathTranslation.length() - 1] != '/') {
      m_pathTranslation += '/';
    }
  }
  m_disabled = vh["Disabled"].getBool(false);

  m_documentRoot = RuntimeOption::SourceRoot + m_pathTranslation;
  if (!m_documentRoot.empty() &&
      m_documentRoot[m_documentRoot.length() - 1] == '/') {
    m_documentRoot = m_documentRoot.substr(0, m_documentRoot.length() - 1);
  }

  Hdf rewriteRules = vh["RewriteRules"];
  for (Hdf hdf = rewriteRules.firstChild(); hdf.exists(); hdf = hdf.next()) {
    RewriteRule dummy;
    m_rewriteRules.push_back(dummy);
    RewriteRule &rule = m_rewriteRules.back();
    rule.pattern = Util::format_pattern(hdf["pattern"].getString(""), true);
    rule.to = hdf["to"].getString("");
    rule.qsa = hdf["qsa"].getBool(false);
    rule.redirect = hdf["redirect"].getInt16(0);

    if (rule.pattern.empty() || rule.to.empty()) {
      throw InvalidArgumentException("rewrite rule", "(empty pattern or to)");
    }
    Hdf rewriteConds = hdf["conditions"];
    for (Hdf chdf = rewriteConds.firstChild(); chdf.exists();
         chdf = chdf.next()) {
      RewriteCond dummy;
      rule.rewriteConds.push_back(dummy);
      RewriteCond &cond = rule.rewriteConds.back();
      cond.pattern = Util::format_pattern(chdf["pattern"].getString(""), true);
      if (cond.pattern.empty()) {
        throw InvalidArgumentException("rewrite rule", "(empty cond pattern)");
      }
      const char *type = chdf["type"].get();
      if (type) {
        if (strcasecmp(type, "host") == 0) {
          cond.type = RewriteCond::Host;
        } else if (strcasecmp(type, "request") == 0) {
          cond.type = RewriteCond::Request;
        } else {
          throw InvalidArgumentException("rewrite rule",
                                         "(invalid cond type)");
        }
      } else {
        cond.type = RewriteCond::Request;
      }
      cond.negate = chdf["negate"].getBool(false);
    }

  }

  if (vh["IpBlockMap"].firstChild().exists()) {
    Hdf ipblocks = vh["IpBlockMap"];
    m_ipBlocks = IpBlockMapPtr(new IpBlockMap(ipblocks));
  }

  Hdf logFilters = vh["LogFilters"];
  for (Hdf hdf = logFilters.firstChild(); hdf.exists(); hdf = hdf.next()) {
    QueryStringFilter filter;
    filter.urlPattern = Util::format_pattern(hdf["url"].getString(""), true);
    filter.replaceWith = hdf["value"].getString("");
    filter.replaceWith = "\\1=" + filter.replaceWith;

    string pattern = hdf["pattern"].getString("");
    vector<string> names;
    hdf["params"].get(names);

    if (pattern.empty()) {
      for (unsigned int i = 0; i < names.size(); i++) {
        if (pattern.empty()) {
          pattern = "(?<=[&\?])(";
        } else {
          pattern += "|";
        }
        pattern += names[i];
      }
      if (!pattern.empty()) {
        pattern += ")=.*?(?=(&|$))";
        pattern = Util::format_pattern(pattern, false);
      }
    } else if (!names.empty()) {
      throw InvalidArgumentException
        ("log filter", "(cannot specify both params and pattern)");
    }

    filter.namePattern = pattern;
    m_queryStringFilters.push_back(filter);
  }

  vh["ServerVariables"].get(m_serverVars);
  m_serverName = vh["ServerName"].getString();
}
Example #4
0
VirtualHost::VirtualHost() : m_disabled(false) {
  IniSetting::Map ini = IniSetting::Map::object;
  Hdf empty;
  initRuntimeOption(ini, empty);
}