Esempio n. 1
0
 public HttpStreamChannelAdapter(ChannelHandlerContext ctx, FullHttpRequest req) {
   this.ctx = ctx;
   this.encoding = HttpRequestWrapper.extractCharacterEncodingOrDefault(req.headers());
 }
  /**
   * Processes the provided {@link ApacheSection} (i.e., .htaccess) for instructions and options
   *
   * @param handler The {@link HttpHandler}
   * @return Will return false if the current request should halt, i.e., the request was already
   *     handled by the directives
   */
  public boolean handleDirectives(ApacheSection apache, HttpHandler handler)
      throws ApacheDirectiveException {
    boolean def = true;

    for (ApacheDirective kv : apache.directives()) {
      String key = kv.getKey();
      String[] args = kv.getArguments();

      HttpRequestWrapper request = handler.getRequest();
      // HttpResponseWrapper response = handler.getResponse();
      WebInterpreter fi = handler.getInterpreter();
      Site site = handler.getSite();

      switch (key) {
          /** Section Types */
        case "IfDefine":
          kv.isSection();
          kv.hasArguments(1, "<Startup Argument>");
          if (AppLoader.options().has(args[0]) || AppLoader.options().hasArgument(args[0]))
            if (!handleDirectives((ApacheSection) kv, handler)) def = false;
          break;
        case "IfModule": // TODO Implement detection of common Apache modules the server can
                         // currently imitate, e.g., Rewrite
          kv.isSection();
          kv.hasArguments(1, "<plugin>");
          if (PluginManager.instance().getPluginByNameWithoutException(args[0]) != null
              || PluginManager.instance().getPluginByClassnameWithoutException(args[0]) != null)
            if (!handleDirectives((ApacheSection) kv, handler)) def = false;
          break;
        case "IfVersion":
          kv.isSection();
          kv.hasArguments(2, "<operator> <version>");
          if (StringFunc.compareVersions(Versioning.getVersionNumber(), args[1], args[0]))
            if (!handleDirectives((ApacheSection) kv, handler)) def = false;
          break;
        case "Directory":
          kv.isSection();
          kv.hasArguments(1, "<directory>");
          if (fi.hasFile()) {
            String rel =
                fi.getFilePath()
                    .substring(
                        site.getSubdomain(request.getSubdomain())
                            .directory()
                            .getAbsolutePath()
                            .length());
            if (rel.startsWith("/")) rel = rel.substring(1);

            if (args[0].startsWith("/")
                    && (fi.getFilePath().startsWith(args[0]) || ("/" + rel).startsWith(args[0]))
                || rel.startsWith(args[0]))
              if (!handleDirectives((ApacheSection) kv, handler)) def = false;
          }
          break;
          /** Individual Key/Values */
        case "ErrorDocument":
          ErrorDocument doc = ErrorDocument.parseArgs(args);
          errorDocuments.put(doc.getHttpCode(), doc);
          break;
        case "AllowOverride":
          for (String a : args)
            if (a.equalsIgnoreCase("All")) for (Override o : overrides) o.allow();
            else if (a.equalsIgnoreCase("None")) for (Override o : overrides) o.deny();
            else {
              Override o = override(a.contains("=") ? a.substring(0, a.indexOf("=")) : a);
              if (o == null)
                throw new ApacheDirectiveException(
                    "The 'AllowOverride' directive does not reconize the option '" + a + "'", kv);
              if ((o == overrides.overrideNonfatal || o == overrides.overrideOptions)
                  && a.contains("=")) o.setParams(a.substring(a.indexOf("=") + 1));
              o.allow();
            }
          break;
        case "Location":
          kv.isSection();
          kv.hasArguments(1, "<url>");
          if (request.getUri().startsWith(args[0]))
            if (!handleDirectives((ApacheSection) kv, handler)) def = false;
          break;
        case "LocationMatch":
          kv.isSection();
          kv.hasArguments(1, "<url regex>");
          if (request.getUri().matches(args[0]))
            if (!handleDirectives((ApacheSection) kv, handler)) def = false;
          break;
        case "Options":
          if (overrides.overrideOptions.allowed())
            for (String a : args) {
              if ((a.startsWith("+") || a.startsWith("-") ? a.substring(1) : a)
                  .equalsIgnoreCase("All")) {
                for (Option o : options)
                  if (o != options.optionMultiViews)
                    if (a.startsWith("-")) o.disable();
                    else o.enable();
              } else {
                Option o = option(a.startsWith("+") || a.startsWith("-") ? a.substring(1) : a);
                if (o == null)
                  throw new ApacheDirectiveException(
                      "The 'Options' directive does not reconize the option '" + a + "'", kv);
                if (a.startsWith("-")) o.disable();
                else o.enable();
              }
            }
          else
            throw new ApacheDirectiveException(
                "The directive 'Option' has been forbidden here", kv);
          break;
        case "VirtualHost":
          throw new ApacheDirectiveException(
              "You can not define a new site using the VirtualHost directive here, site configs are located within the webroot.");
        case "Proxy":
        case "ProxyMatch":
        case "ProxyPass":
          throw new ApacheDirectiveException(
              "The module mod_proxy is exclusive to the Apache Web Server. We currently have no implementation, nor interest, to support said directives.");
        default:
          throw new ApacheDirectiveException(
              "Currently the Apache directive '" + key + "' is not implemented", kv);
      }

      // ErrorDocument 403 http://www.yahoo.com/
      // Order deny,allow
      // Deny from all
      // Allow from 208.113.134.190

    }

    return def;
  }
Esempio n. 3
0
  /**
   * Writes the XML response for this header.
   *
   * <pre class="xml">{@code
   * <header>
   *   <!-- Deprecated in Berlioz 1.0 -->
   *   <group>[service group name]</group>
   *   <service>[service name]</service>
   *   <path-info>[berlioz path]</path-info>
   *   <context-path>[servlet context path]</context-path>
   *   <!-- Deprecated in Berlioz 1.0 -->
   *   <host>[server host]</host>
   *   <port>[server port]</port>
   *   <url>[url (up to query)]</url>
   *   <query-string>[query string]</query-string>
   *   <!-- End deprecated in Berlioz 1.0 -->
   *   <location scheme="[http|https]"
   *               host="[hostname]"
   *               port="[post]"
   *               path="[path]"
   *               base="[base]"
   *              query="[query]">[full url]</location>
   *   <path context="[servlet context path]"
   *          prefix="[prefix if berlioz mapped to a prefix]"
   *            info="[berlioz path]"
   *       extension="[prefix if berlioz mapped to an extension]"/>
   *   <http-parameters>
   *     <parameter name="[name-A]">[value-A]</parameter>
   *     <parameter name="[name-B]">[value-B1]</parameter>
   *     <parameter name="[name-B]">[value-B2]</parameter>
   *     <parameter name="[name-C]">[value-C]</parameter>
   *     <parameter name="[name-D]">[value-D]</parameter>
   *     <!-- ... -->
   *   </http-parameters>
   *   <uri-parameters>
   *     <parameter name="[name-X]">[value-X]</parameter>
   *     <parameter name="[name-Y]">[value-Y]</parameter>
   *     <!-- ... -->
   *   </uri-parameters>
   *   <berlioz version="[version]" mode="[mode]"/>
   * </header>
   * }</pre>
   *
   * @see XMLWritable#toXML(org.pageseeder.xmlwriter.XMLWriter)
   * @param xml The XML Writer to use.
   * @throws IOException If thrown by the underlying XML Writer.
   */
  @Override
  public void toXML(XMLWriter xml) throws IOException {
    HttpServletRequest req = this._core.request();

    boolean compatibility =
        GlobalSettings.has(BerliozOption.XML_HEADER_COMPATIBILITY)
            && !"1.0".equals(GlobalSettings.get(BerliozOption.XML_HEADER_VERSION));

    // start serialising
    xml.openElement("header", true);
    if (compatibility) {
      xml.writeComment("Elements below will be deprecated in Berlioz 1.0");
      xml.element("group", this._group);
      xml.element("service", this._service);
      xml.writeComment("Use 'path' instead");
      xml.element("path-info", HttpRequestWrapper.getBerliozPath(req));
      xml.element("context-path", req.getContextPath());
      xml.writeComment("Use 'location' instead");
      xml.element("scheme", req.getScheme());
      xml.element("host", req.getServerName());
      xml.element("port", Integer.toString(req.getServerPort()));
      xml.element("url", req.getRequestURL().toString());
      xml.element("query-string", req.getQueryString());
      xml.writeComment("End deprecated elements");
    }

    // New location info
    Location location = this._core.location();
    if (location != null) {
      location.toXML(xml);
      PathInfo path = location.info();
      path.toXML(xml);
    }

    // Write the http parameters
    xml.openElement("http-parameters", true);
    Enumeration<?> names = req.getParameterNames();
    while (names.hasMoreElements()) {
      String paramName = (String) names.nextElement();
      String[] values = req.getParameterValues(paramName);
      for (String value : values) {
        xml.openElement("parameter", false);
        xml.attribute("name", paramName);
        xml.writeText(value);
        xml.closeElement();
      }
    }
    xml.closeElement(); // close http-parameters

    // Write the URI parameters
    if (this._results != null) {
      Set<String> unames = this._results.names();
      xml.openElement("uri-parameters", !unames.isEmpty());
      for (String name : unames) {
        Object value = this._results.get(name);
        xml.openElement("parameter", false);
        xml.attribute("name", name);
        xml.writeText(value != null ? value.toString() : "");
        xml.closeElement();
      }
      xml.closeElement();
    }

    // Include Berlioz version and mode
    xml.openElement("berlioz");
    xml.attribute("version", GlobalSettings.getVersion());
    xml.attribute("mode", GlobalSettings.getMode());
    xml.closeElement();

    xml.closeElement(); // close header
  }