| | |
| | | * @param e das Objekt mit Methoden zur Untersuchung der Anfrage sowie zum |
| | | * Anfertigen und Senden der Antwort |
| | | * @return Name der gewünschten Datei |
| | | * @throws IllegalArgumentException wenn der Dateiname ungueltige Zeichen |
| | | * enthaelt, z.B. ../ |
| | | */ |
| | | public String getFileName(HttpExchange e) { |
| | | public String getFileName(HttpExchange e) throws IllegalArgumentException { |
| | | String ctxPath = e.getHttpContext().getPath(); |
| | | String uriPath = e.getRequestURI().getPath(); |
| | | return uriPath.substring(ctxPath.length()); |
| | | return inspectFileName(uriPath.substring(ctxPath.length())); |
| | | } |
| | | |
| | | public String inspectFileName(String fileName) throws IllegalArgumentException { |
| | | if (fileName == null |
| | | || fileName.contains("..")) { |
| | | //|| fileName.contains("/") |
| | | //|| fileName.contains("\\")) { |
| | | throw new IllegalArgumentException("Invalid file name"); |
| | | } |
| | | return fileName; |
| | | } |
| | | |
| | | public String bodyLesen(HttpExchange exchange) throws IOException { |
| | |
| | | } |
| | | 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(HttpExchange exchange) { |
| | | if(exchange.getRequestMethod().equalsIgnoreCase("GET")) { |
| | |
| | | |
| | | public Map<String, String> getQueryMap(String query) { |
| | | Map<String, String> map = new HashMap<>(); |
| | | if(query instanceof String) { |
| | | String[] params = query.split(STR_AMP); |
| | | for (String param : params) { |
| | | String name = param.split(STR_EQUAL)[0]; |
| | | String value = param.split(STR_EQUAL)[1]; |
| | | map.put(name, value); |
| | | } |
| | | } else { |
| | | // map bleibt leer |
| | | try { |
| | | if(query instanceof String) { |
| | | String[] params = query.split(STR_AMP); |
| | | for (String param : params) { |
| | | String name = param.split(STR_EQUAL)[0]; |
| | | String value = param.split(STR_EQUAL)[1]; |
| | | map.put(name, value); |
| | | } |
| | | } else { |
| | | // map bleibt leer |
| | | } |
| | | } catch(Exception ex) { |
| | | |
| | | } finally { |
| | | return map; |
| | | } |
| | | return map; |
| | | } |
| | | |
| | | /*public String getRouteString(HttpExchange exchange) { |
| | | return exchange.getRequestURI().getPath() |
| | | .substring(exchange.getHttpContext().getPath().length()); |
| | | }*/ |
| | | |
| | | public String getRouteString(HttpExchange exchange) { |
| | | return getFileName(exchange); |
| | |
| | | |
| | | public List<String> getRouteList(String routeString) { |
| | | return Arrays.asList(routeString.split("/")); |
| | | } |
| | | |
| | | /* |
| | | 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; |
| | | } |
| | | */ |
| | | |
| | | } |
| | | } |