From 9d3717abd59e1672f5d8d7888ce613afdc7fb3c5 Mon Sep 17 00:00:00 2001 From: ulrich Date: Wed, 24 Jan 2024 13:00:11 +0000 Subject: [PATCH] HttpResponder.finish aufgeteilt --- src/de/uhilger/httpserver/base/HttpHelper.java | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 46 insertions(+), 0 deletions(-) diff --git a/src/de/uhilger/httpserver/base/HttpHelper.java b/src/de/uhilger/httpserver/base/HttpHelper.java index 56275b1..fed74ea 100644 --- a/src/de/uhilger/httpserver/base/HttpHelper.java +++ b/src/de/uhilger/httpserver/base/HttpHelper.java @@ -18,10 +18,14 @@ package de.uhilger.httpserver.base; import com.sun.net.httpserver.HttpExchange; +import de.uhilger.httpserver.base.handler.FileHandler; import java.io.BufferedReader; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.util.HashMap; +import java.util.Map; /** * Helfer fuer HTTP-Methoden @@ -37,6 +41,8 @@ public static final String HTTP_PUT = "PUT"; public static final String HTTP_DELETE = "DELETE"; + public static final String STR_AMP = "&"; + public static final String STR_EQUAL = "="; public static final String CONTENT_TYPE = "Content-Type"; @@ -68,5 +74,45 @@ return sb.toString(); } + public String getAttrStr(Map attributes, String key, String defaultValue) { + Object value = attributes.get(key); + if(value instanceof String) { + return value.toString(); + } else { + return defaultValue; + } + } + + public Map<String, String> getQueryMap(String query) { + String[] params = query.split(STR_AMP); + Map<String, String> map = new HashMap<String, String>(); + for (String param : params) { + String name = param.split(STR_EQUAL)[0]; + String value = param.split(STR_EQUAL)[1]; + map.put(name, value); + } + return map; + } + + public File tryWelcomeFiles(HttpExchange e, String fName) { + boolean notFound = true; + File file = null; + String fileBase = e.getHttpContext().getAttributes().get(FileHandler.ATTR_FILE_BASE).toString(); + Object welcomeFiles = e.getHttpContext().getAttributes().get(FileHandler.ATTR_WELCOME_FILES); + if(welcomeFiles instanceof String) { + String[] fileNames = welcomeFiles.toString().split(FileHandler.STR_COMMA); + int i = -1; + while(notFound && ++i < fileNames.length) { + file = new File(fileBase, fName + fileNames[i]); + if(file.exists()) { + notFound = false; + } + } + } + if(notFound) { + file = new File(fileBase, fName + FileHandler.WELCOME_FILE); + } + return file; + } } -- Gitblit v1.9.3