public void run()

in java/com/google/gerrit/pgm/init/InitHttpd.java [60:177]


  public void run() throws IOException, InterruptedException {
    ui.header("HTTP Daemon");

    boolean anySsl = false;
    // If any listenUrls are present, validate whether it can be parsed as URL.
    String[] listenUrls = httpd.getList("listenUrl");
    for (String listenUrl : listenUrls) {
      if (listenUrl != null && !listenUrl.isEmpty()) {
        try {
          final URI u = toURI(listenUrl);
          if (u.getScheme().startsWith("https")) {
            anySsl = true;
          }
        } catch (URISyntaxException e) {
          System.err.println(
              String.format(
                  "warning: invalid httpd.listenUrl entry: '%s'. Gerrit may not be able to start.",
                  listenUrl));
        }
      }
    }

    if (listenUrls.length > 1) {
      // Because we will return early here, we will warn about steps otherwise being covered.
      if (!ui.isBatch()) {
        System.err.println(
            "Interactive configuration is not supported with multiple entries of "
                + "httpd.listenUrl.");
      }
      if (anySsl) {
        System.err.println(
            "Generating self-signed SSL certificates is not supported with multiple "
                + "entries of httpd.listenUrl.");
      }
      String canonicalWebUrlDefaultString = "";
      if (listenUrls[0] != null && !listenUrls[0].isEmpty()) {
        try {
          canonicalWebUrlDefaultString = new URI(listenUrls[0]).toString();
        } catch (URISyntaxException e) {
          // Should not happen, but log it anyway
          System.err.println(
              String.format("warning: invalid httpd.listenUrl entry: '%s'", listenUrls[0]));
        }
      }
      gerrit.string("Canonical URL", "canonicalWebUrl", canonicalWebUrlDefaultString);
      return;
    }

    boolean proxy = false;
    boolean ssl = false;
    String address = "*";
    int port = -1;
    String context = "/";

    if (listenUrls.length > 0 && listenUrls[0] != null && !listenUrls[0].isEmpty()) {
      try {
        final URI uri = toURI(listenUrls[0]);
        proxy = uri.getScheme().startsWith("proxy-");
        ssl = uri.getScheme().endsWith("https");
        address = isAnyAddress(new URI(listenUrls[0])) ? "*" : uri.getHost();
        port = uri.getPort();
        context = uri.getPath();
      } catch (URISyntaxException e) {
        System.err.println("warning: invalid httpd.listenUrl " + listenUrls[0]);
      }
    }

    proxy = ui.yesno(proxy, "Behind reverse proxy");

    if (proxy) {
      ssl = ui.yesno(ssl, "Proxy uses SSL (https://)");
      context = ui.readString(context, "Subdirectory on proxy server");
    } else {
      ssl = ui.yesno(ssl, "Use SSL (https://)");
      context = "/";
    }

    address = ui.readString(address, "Listen on address");

    if (port < 0) {
      if (proxy) {
        port = 8081;
      } else if (ssl) {
        port = 8443;
      } else {
        port = 8080;
      }
    }
    port = ui.readInt(port, "Listen on port");

    final StringBuilder urlbuf = new StringBuilder();
    urlbuf.append(proxy ? "proxy-" : "");
    urlbuf.append(ssl ? "https" : "http");
    urlbuf.append("://");
    urlbuf.append(address);
    if (0 <= port) {
      urlbuf.append(":");
      urlbuf.append(port);
    }
    urlbuf.append(context);

    URI uri;
    try {
      uri = toURI(urlbuf.toString());
      if (uri.getScheme().startsWith("proxy-")) {
        // If its a proxy URL, assume the reverse proxy is on our system
        // at the protocol standard ports (so omit the ports from the URL).
        //
        String s = uri.getScheme().substring("proxy-".length());
        uri = new URI(s + "://" + uri.getHost() + uri.getPath());
      }
    } catch (URISyntaxException e) {
      throw die("invalid httpd.listenUrl", e);
    }
    httpd.set("listenUrl", urlbuf.toString());
    gerrit.string("Canonical URL", "canonicalWebUrl", uri.toString());
    generateSslCertificate();
  }