From 4e2a31c15ceb72db4eb10950a815210a68ab661d Mon Sep 17 00:00:00 2001
From: ulrich
Date: Sun, 02 Jan 2022 11:35:19 +0000
Subject: [PATCH] Code aufgeraeumt und Kommentare ergaenzt

---
 src/de/uhilger/httpserver/base/HttpHelper.java |   42 ++++++++++++++++++++++++++++++++++--------
 1 files changed, 34 insertions(+), 8 deletions(-)

diff --git a/src/de/uhilger/httpserver/base/HttpHelper.java b/src/de/uhilger/httpserver/base/HttpHelper.java
index 565cdd4..fed74ea 100644
--- a/src/de/uhilger/httpserver/base/HttpHelper.java
+++ b/src/de/uhilger/httpserver/base/HttpHelper.java
@@ -24,6 +24,8 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.util.HashMap;
+import java.util.Map;
 
 /**
  * Helfer fuer HTTP-Methoden
@@ -39,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";
 
@@ -70,17 +74,39 @@
     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) {
-    String fileBase = e.getHttpContext().getAttributes().get(FileHandler.ATTR_FILE_BASE).toString();
-    String welcomeFiles = e.getHttpContext().getAttributes().get(FileHandler.ATTR_WELCOME_FILES).toString();
-    String[] fileNames = welcomeFiles.split(FileHandler.STR_COMMA);
     boolean notFound = true;
-    int i = -1;
     File file = null;
-    while(notFound && ++i < fileNames.length) {
-      file = new File(fileBase, fName + fileNames[i]);
-      if(file.exists()) {
-        notFound = false;
+    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) {

--
Gitblit v1.9.3