Ein minimalistischer HTTP-Server
ulrich
2021-03-27 ee6a3e9057bff6e30e6deff43100bcf2bf9bfba5
Anpassungen am FielHandler, Stop-Ablauf verfeinert
3 files modified
40 ■■■■■ changed files
src/de/uhilger/minsrv/Server.java 2 ●●● patch | view | raw | blame | history
src/de/uhilger/minsrv/handler/FileHandler.java 4 ●●● patch | view | raw | blame | history
src/de/uhilger/minsrv/handler/StopServerHandler.java 34 ●●●●● patch | view | raw | blame | history
src/de/uhilger/minsrv/Server.java
@@ -85,7 +85,7 @@
    logger.info("Server starting on port " + port);
    HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
    server.createContext(ctx + "/av", new FileHandler(App.getInitParameter(App.IP_WWW_DATA)));
    server.createContext(ctx + STR_SLASH, new FileHandler(App.getInitParameter(App.IP_WWW_DATA)));
    server.createContext(ctx + "/server/stop", new StopServerHandler());
    server.setExecutor(Executors.newFixedThreadPool(20));
    server.start();
src/de/uhilger/minsrv/handler/FileHandler.java
@@ -57,6 +57,7 @@
  public static final String ACCEPT_RANGES_HEADER = "Accept-Ranges";
  public static final String LAST_MODIFIED_DATE_HEADER = "Last-Modified";
  public static final String CONTENT_TYPE = "Content-Type";
  public static final String CONTENT_LENGTH = "Content-Length";
  /* Statuscodes */
  public static final int SC_OK = 200;
@@ -110,7 +111,7 @@
      if (headers.containsKey(RANGE_HEADER)) {
        serveFileParts(e, new File(fileBase, fName));
      } else {
        if (fName.endsWith(Server.STR_SLASH)) {
        if (fName.length() < 1 || fName.endsWith(Server.STR_SLASH)) {
          fName += WELCOME_FILE;
        }
        serveFile(e, new File(fileBase, fName));
@@ -143,6 +144,7 @@
  protected void serveFile(HttpExchange e, File file) throws IOException {
    if (file.exists()) {
      setHeaders(e, file);
      e.getResponseHeaders().set(CONTENT_LENGTH, Long.toString(file.length()));
      e.sendResponseHeaders(SC_OK, file.length());
      if(HTTP_GET.equalsIgnoreCase(e.getRequestMethod())) {
        InputStream in = new FileInputStream(file);
src/de/uhilger/minsrv/handler/StopServerHandler.java
@@ -14,8 +14,7 @@
  You should have received a copy of the GNU Affero General Public License
  along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/
 */
package de.uhilger.minsrv.handler;
import com.sun.net.httpserver.HttpExchange;
@@ -23,28 +22,39 @@
import de.uhilger.minsrv.App;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Logger;
/**
 * Ein HTTP-Handler zum Stoppen der Anwendung
 *
 *
 * @author Ulrich Hilger
 */
public class StopServerHandler implements HttpHandler {
  @Override
  public void handle(HttpExchange exchange) throws IOException {
    Logger.getLogger(StopServerHandler.class.getName()).info(exchange.getRequestURI().toString());
  public void handle(HttpExchange e) throws IOException {
    Logger.getLogger(StopServerHandler.class.getName()).info(e.getRequestURI().toString());
    String response = "Server stopped";
    exchange.sendResponseHeaders(200, response.length());
    OutputStream os = exchange.getResponseBody();
    e.sendResponseHeaders(200, response.length());
    OutputStream os = e.getResponseBody();
    os.write(response.getBytes());
    os.flush();
    os.close();
    Logger.getLogger(StopServerHandler.class.getName()).info("stopping app.");
    App.stop();
    //exchange.getHttpContext().getServer().stop(5);
    Logger.getLogger(StopServerHandler.class.getName()).info("stopping server.");
    e.getHttpContext().getServer().stop(1);
    Timer timer = new Timer();
    timer.schedule(new AppStopper(), 2000);
  }
  class AppStopper extends TimerTask {
    @Override
    public void run() {
      Logger.getLogger(StopServerHandler.class.getName()).info("Mini-Server beendet.");
      App.stop();
    }
  }
}