<commit_before>/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package de.uos.inf.did.abbozza.handler;

import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import de.uos.inf.did.abbozza.AbbozzaLocale;
import de.uos.inf.did.abbozza.AbbozzaLogger;
import de.uos.inf.did.abbozza.AbbozzaServer;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Vector;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;

/**
 *
 * @author michael
 */
public class JarDirHandler implements HttpHandler {

    private Vector<Object> entries;

    public JarDirHandler() {
        entries = new Vector<Object>();
    }

    public void addDir(String path, String name) {
        File file = new File(path);
        if (!file.exists()) {
            AbbozzaLogger.err(file.getAbsolutePath() + " not found");
            file = null;
        } else {
            AbbozzaLogger.out(name + " : " + file.getAbsolutePath());
            return;
        }
        entries.add(file);
    }
    
    
    public void addDir(File dir) {
        entries.add(dir);
    }

    
    public void addJar(String path, String name) {
        JarFile file;
        try {
            file = new JarFile(path);
            AbbozzaLogger.out(name + " : " + file.getName());
        } catch (IOException e) {
            AbbozzaLogger.err(name + " not found (" + path + ")");
            return;
        }        
        entries.add(file);
    }
    
    
    public void addJar(JarFile jar) {
        entries.add(jar);
    }

    public void clear() {
        entries.clear();
    }

    @Override
    public void handle(HttpExchange exchg) throws IOException {

        String path = exchg.getRequestURI().getPath();
        OutputStream os = exchg.getResponseBody();
        
        byte[] bytearray = getBytes(path);

        if (bytearray == null) {
            String result = "abbozza! : " + path + " nicht gefunden!";
            // System.out.println(result);

            exchg.sendResponseHeaders(400, result.length());
            os.write(result.getBytes());
            os.close();
            return;
        }

        Headers responseHeaders = exchg.getResponseHeaders();
        if (path.endsWith(".css")) {
            responseHeaders.set("Content-Type", "text/css");
        } else if (path.endsWith(".js")) {
            responseHeaders.set("Content-Type", "text/javascript");
        } else if (path.endsWith(".xml")) {
            responseHeaders.set("Content-Type", "text/xml");
        } else if (path.endsWith(".svg")) {
            responseHeaders.set("Content-Type", "image/svg+xml");            
        }

        // ok, we are ready to send the response.
        exchg.sendResponseHeaders(200, bytearray.length);
        os.write(bytearray, 0, bytearray.length);
        os.close();
    }

    public byte[] getBytes(String path) throws IOException {
        AbbozzaLogger.out("Reading " + path, AbbozzaLogger.INFO);
        byte[] bytearray = null;
        int tries = 0;

        while ((tries < 3) && (bytearray == null)) {

            Enumeration<Object> it = entries.elements();
            while (it.hasMoreElements() && (bytearray == null)) {
                Object entry = it.nextElement();
                if (entry instanceof JarFile) {
                    bytearray = getBytesFromJar((JarFile) entry, path);
                } else if (entry instanceof File) {
                    bytearray = getBytesFromDir((File) entry, path);
                }
            }

            if (bytearray == null) {
                tries++;
                AbbozzaServer.getInstance().findJarsAndDirs(this);
            }
        }

        if (bytearray == null) {
            AbbozzaLogger.out(AbbozzaLocale.entry("msg.not_found",path),AbbozzaLogger.ERROR);
        }
        return bytearray;
    }
    
    
    public byte[] getBytesFromDir(File webDir, String path) throws IOException {
        File file = new File(webDir + path);
        if (!file.exists()) {
            return null;
        }
        FileInputStream fis = new FileInputStream(file);

        byte[] bytearray = new byte[(int) file.length()];
        BufferedInputStream bis = new BufferedInputStream(fis);
        bis.read(bytearray, 0, bytearray.length);
        bis.close();

        return bytearray;
    }<commit_msg>* PAth during access to directory is checked<commit_after>/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package de.uos.inf.did.abbozza.handler;

import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import de.uos.inf.did.abbozza.AbbozzaLocale;
import de.uos.inf.did.abbozza.AbbozzaLogger;
import de.uos.inf.did.abbozza.AbbozzaServer;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Vector;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;

/**
 *
 * @author michael
 */
public class JarDirHandler implements HttpHandler {

    private Vector<Object> entries;

    public JarDirHandler() {
        entries = new Vector<Object>();
    }

    public void addDir(String path, String name) {
        File file = new File(path);
        if (!file.exists()) {
            AbbozzaLogger.err(file.getAbsolutePath() + " not found");
            file = null;
        } else {
            AbbozzaLogger.out(name + " : " + file.getAbsolutePath());
            return;
        }
        entries.add(file);
    }
    
    
    public void addDir(File dir) {
        entries.add(dir);
    }

    
    public void addJar(String path, String name) {
        JarFile file;
        try {
            file = new JarFile(path);
            AbbozzaLogger.out(name + " : " + file.getName());
        } catch (IOException e) {
            AbbozzaLogger.err(name + " not found (" + path + ")");
            return;
        }        
        entries.add(file);
    }
    
    
    public void addJar(JarFile jar) {
        entries.add(jar);
    }

    public void clear() {
        entries.clear();
    }

    @Override
    public void handle(HttpExchange exchg) throws IOException {

        String path = exchg.getRequestURI().getPath();
        OutputStream os = exchg.getResponseBody();
        
        byte[] bytearray = getBytes(path);

        if (bytearray == null) {
            String result = "abbozza! : " + path + " nicht gefunden!";
            // System.out.println(result);

            exchg.sendResponseHeaders(400, result.length());
            os.write(result.getBytes());
            os.close();
            return;
        }

        Headers responseHeaders = exchg.getResponseHeaders();
        if (path.endsWith(".css")) {
            responseHeaders.set("Content-Type", "text/css");
        } else if (path.endsWith(".js")) {
            responseHeaders.set("Content-Type", "text/javascript");
        } else if (path.endsWith(".xml")) {
            responseHeaders.set("Content-Type", "text/xml");
        } else if (path.endsWith(".svg")) {
            responseHeaders.set("Content-Type", "image/svg+xml");            
        }

        // ok, we are ready to send the response.
        exchg.sendResponseHeaders(200, bytearray.length);
        os.write(bytearray, 0, bytearray.length);
        os.close();
    }

    public byte[] getBytes(String path) throws IOException {
        AbbozzaLogger.out("Reading " + path, AbbozzaLogger.INFO);
        byte[] bytearray = null;
        int tries = 0;

        while ((tries < 3) && (bytearray == null)) {

            Enumeration<Object> it = entries.elements();
            while (it.hasMoreElements() && (bytearray == null)) {
                Object entry = it.nextElement();
                if (entry instanceof JarFile) {
                    bytearray = getBytesFromJar((JarFile) entry, path);
                } else if (entry instanceof File) {
                    bytearray = getBytesFromDir((File) entry, path);
                }
            }

            if (bytearray == null) {
                tries++;
                AbbozzaServer.getInstance().findJarsAndDirs(this);
            }
        }

        if (bytearray == null) {
            AbbozzaLogger.out(AbbozzaLocale.entry("msg.not_found",path),AbbozzaLogger.ERROR);
        }
        return bytearray;
    }
    
    
    public byte[] getBytesFromDir(File webDir, String path) throws IOException {
        File file = new File(webDir + path);
        if (!file.exists()) {
            return null;
        }
        
        // Check if the requested file is below the given directory
        if (!file.getCanonicalPath().startsWith(webDir.getCanonicalPath())) {
            return null;
        }

        FileInputStream fis = new FileInputStream(file);

        byte[] bytearray = new byte[(int) file.length()];
        BufferedInputStream bis = new BufferedInputStream(fis);
        bis.read(bytearray, 0, bytearray.length);
        bis.close();

        return bytearray;
    }
{'fraction_non_alphanumeric': 0.12189002717959439, 'fraction_numerical': 0.0020907380305247754, 'mean_word_length': 2.080167417900837, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '2555500', 'n_tokens_mistral': 2772, 'n_tokens_neox': 2686, 'n_words': 613}