<commit_before>package org.basex.api;

import static org.basex.api.HTTPText.*;
import static org.basex.core.Text.*;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.URL;

import org.basex.BaseXServer;
import org.basex.api.rest.RESTServlet;
import org.basex.api.webdav.WebDAVServlet;
import org.basex.core.BaseXException;
import org.basex.core.Context;
import org.basex.core.MainProp;
import org.basex.core.Prop;
import org.basex.io.IOFile;
import org.basex.util.Args;
import org.basex.util.Performance;
import org.basex.util.Token;
import org.basex.util.Util;
import org.mortbay.jetty.Server;

/**
 * This is the main class for the starting the database HTTP services.
 *
 * @author BaseX Team 2005-11, BSD License
 * @author <NAME>
 */
public final class BaseXHTTP {
  /** Activate WebDAV. */
  private boolean webdav = true;
  /** Activate REST. */
  private boolean rest = true;

  /** Database server. */
  private BaseXServer server;
  /** Start as daemon. */
  private boolean service;
  /** Stopped flag. */
  private boolean stopped;
  /** HTTP server. */
  protected Server jetty;
  /** Quiet flag. */
  private boolean quiet;

  /**
   * Main method, launching the HTTP services.
   * Command-line arguments are listed with the {@code -h} argument.
   * @param args command-line arguments
   */
  public static void main(final String[] args) {
    try {
      new BaseXHTTP(args);
    } catch(final Exception ex) {
      Util.errln(ex);
      System.exit(1);
    }
  }

  /**
   * Constructor.
   * @param args command-line arguments
   * @throws Exception exception
   */
  public BaseXHTTP(final String... args) throws Exception {
    parseArguments(args);

    final Context context = HTTPSession.context();
    final MainProp mprop = context.mprop;

    final int port = mprop.num(MainProp.SERVERPORT);
    final int eport = mprop.num(MainProp.EVENTPORT);
    final int hport = mprop.num(MainProp.HTTPPORT);
    final String host = mprop.get(MainProp.HOST);

    if(service) {
      start(hport, args);
      Util.outln(HTTP + ' ' + SERVERSTART);
      Performance.sleep(1000);
      return;
    }

    if(stopped) {
      BaseXServer.stop(port, eport);
      return;
    }

    // set password if only user was specified
    if(System.getProperty(DBUSER) != null) {
      while(System.getProperty(DBPASS) == null) {
        Util.out(SERVERPW + COLS);
        System.setProperty(DBPASS, Util.password());
      }
    }

    if(HTTPSession.local() || !Token.eq(host, LOCALHOST, LOCALIP)) {
      Util.outln(CONSOLE + HTTP + ' ' + SERVERSTART, SERVERMODE);
    } else {
      server = new BaseXServer(context, quiet ? "-z" : "");
      Util.outln(HTTP + ' ' + SERVERSTART);
    }


    System.setProperty("STOP.PORT", "8985");
    System.setProperty("STOP.KEY", "8985");
    jetty = new Server(hport);

    final org.mortbay.jetty.servlet.Context jcontext =
        new org.mortbay.jetty.servlet.Context(jetty, "/",
            org.mortbay.jetty.servlet.Context.SESSIONS);

    if(rest) jcontext.addServlet(RESTServlet.class, "/rest/*");
    if(webdav) jcontext.addServlet(WebDAVServlet.class, "/webdav/*");

    jetty.start();

    final ServerSocket socket = new ServerSocket();
    socket.setReuseAddress(true);
    socket.bind(new InetSocketAddress(hport + 1));
  }

  /**
   * Stops the server.
   * @throws Exception exception
   */
  public void stop() throws Exception {
    if(jetty != null) jetty.stop();
    if(server != null) server.quit();
  }

  /**
   * Parses the command-line arguments, specified by the user.
   * @param args command-line arguments
   * @throws IOException I/O exception
   */
  protected void parseArguments(final String[] args) throws IOException {
    final Args arg = new Args(args, this, HTTPINFO, Util.info(CONSOLE, HTTP));
    final Context ctx = HTTPSession.context();<commit_msg>[FIX] HTTP Server: binding to port+1 removed<commit_after>package org.basex.api;

import static org.basex.api.HTTPText.*;
import static org.basex.core.Text.*;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import org.basex.BaseXServer;
import org.basex.api.rest.RESTServlet;
import org.basex.api.webdav.WebDAVServlet;
import org.basex.core.BaseXException;
import org.basex.core.Context;
import org.basex.core.MainProp;
import org.basex.core.Prop;
import org.basex.io.IOFile;
import org.basex.util.Args;
import org.basex.util.Performance;
import org.basex.util.Token;
import org.basex.util.Util;
import org.mortbay.jetty.Server;

/**
 * This is the main class for the starting the database HTTP services.
 *
 * @author BaseX Team 2005-11, BSD License
 * @author <NAME>
 */
public final class BaseXHTTP {
  /** Activate WebDAV. */
  private boolean webdav = true;
  /** Activate REST. */
  private boolean rest = true;

  /** Database server. */
  private BaseXServer server;
  /** Start as daemon. */
  private boolean service;
  /** Stopped flag. */
  private boolean stopped;
  /** HTTP server. */
  protected Server jetty;
  /** Quiet flag. */
  private boolean quiet;

  /**
   * Main method, launching the HTTP services.
   * Command-line arguments are listed with the {@code -h} argument.
   * @param args command-line arguments
   */
  public static void main(final String[] args) {
    try {
      new BaseXHTTP(args);
    } catch(final Exception ex) {
      Util.errln(ex);
      System.exit(1);
    }
  }

  /**
   * Constructor.
   * @param args command-line arguments
   * @throws Exception exception
   */
  public BaseXHTTP(final String... args) throws Exception {
    parseArguments(args);

    final Context context = HTTPSession.context();
    final MainProp mprop = context.mprop;

    final int port = mprop.num(MainProp.SERVERPORT);
    final int eport = mprop.num(MainProp.EVENTPORT);
    final int hport = mprop.num(MainProp.HTTPPORT);
    final String host = mprop.get(MainProp.HOST);

    if(service) {
      start(hport, args);
      Util.outln(HTTP + ' ' + SERVERSTART);
      Performance.sleep(1000);
      return;
    }

    if(stopped) {
      BaseXServer.stop(port, eport);
      return;
    }

    // set password if only user was specified
    if(System.getProperty(DBUSER) != null) {
      while(System.getProperty(DBPASS) == null) {
        Util.out(SERVERPW + COLS);
        System.setProperty(DBPASS, Util.password());
      }
    }

    if(HTTPSession.local() || !Token.eq(host, LOCALHOST, LOCALIP)) {
      Util.outln(CONSOLE + HTTP + ' ' + SERVERSTART, SERVERMODE);
    } else {
      server = new BaseXServer(context, quiet ? "-z" : "");
      Util.outln(HTTP + ' ' + SERVERSTART);
    }

    jetty = new Server(hport);

    final org.mortbay.jetty.servlet.Context jcontext =
        new org.mortbay.jetty.servlet.Context(jetty, "/",
            org.mortbay.jetty.servlet.Context.SESSIONS);

    if(rest) jcontext.addServlet(RESTServlet.class, "/rest/*");
    if(webdav) jcontext.addServlet(WebDAVServlet.class, "/webdav/*");

    jetty.start();
  }

  /**
   * Stops the server.
   * @throws Exception exception
   */
  public void stop() throws Exception {
    if(jetty != null) jetty.stop();
    if(server != null) server.quit();
  }

  /**
   * Parses the command-line arguments, specified by the user.
   * @param args command-line arguments
   * @throws IOException I/O exception
   */
  protected void parseArguments(final String[] args) throws IOException {
    final Args arg = new Args(args, this, HTTPINFO, Util.info(CONSOLE, HTTP));
    final Context ctx = HTTPSession.context();
{'fraction_non_alphanumeric': 0.13040646169880146, 'fraction_numerical': 0.004168837936425221, 'mean_word_length': 3.1274193548387097, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5314081', 'n_tokens_mistral': 2502, 'n_tokens_neox': 2433, 'n_words': 596}