<commit_before>/*
 * EpubServer.java
 * SDKLauncher-Android
 *
 * Created by <NAME> (Mantano) on 2013-09-03.
 */
//  Copyright (c) 2014 Readium Foundation and/or its licensees. All rights reserved.
//  Redistribution and use in source and binary forms, with or without modification, 
//  are permitted provided that the following conditions are met:
//  1. Redistributions of source code must retain the above copyright notice, this 
//  list of conditions and the following disclaimer.
//  2. Redistributions in binary form must reproduce the above copyright notice, 
//  this list of conditions and the following disclaimer in the documentation and/or 
//  other materials provided with the distribution.
//  3. Neither the name of the organization nor the names of its contributors may be 
//  used to endorse or promote products derived from this software without specific 
//  prior written permission.
//
//  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
//  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
//  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
//  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
//  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
//  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
//  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
//  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
//  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
//  OF THE POSSIBILITY OF SUCH DAMAGE

package org.readium.sdk.android.launcher.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.readium.sdk.android.ManifestItem;
import org.readium.sdk.android.Package;
import org.readium.sdk.android.PackageResource;

import android.util.Log;
import fi.iki.elonen.NanoHTTPD;

/**
 * This small web server will serve media files such as audio and video.
 */
public class EpubServer extends NanoHTTPD {

    private static final String TAG = "EpubServer";
    public static final String HTTP_HOST = "127.0.0.1";
    public static final int HTTP_PORT = 8080;
    /**
     * Hashtable mapping (String)FILENAME_EXTENSION -> (String)MIME_TYPE
     */
    private static final Map<String, String> MIME_TYPES;

    private final Package mPackage;
    private final boolean quiet;
    
    static {
        Map<String, String> tmpMap = new HashMap<String, String>();
        tmpMap.put("html", "application/xhtml+xml"); // FORCE
        tmpMap.put("xhtml", "application/xhtml+xml"); // FORCE
        tmpMap.put("xml", "application/xml"); // FORCE
        tmpMap.put("htm", "text/html");
        tmpMap.put("css", "text/css");
        tmpMap.put("java", "text/x-java-source, text/java");
        tmpMap.put("txt", "text/plain");
        tmpMap.put("asc", "text/plain");
        tmpMap.put("gif", "image/gif");
        tmpMap.put("jpg", "image/jpeg");
        tmpMap.put("jpeg", "image/jpeg");
        tmpMap.put("png", "image/png");
        tmpMap.put("mp3", "audio/mpeg");
        tmpMap.put("m3u", "audio/mpeg-url");
        tmpMap.put("mp4", "video/mp4");
        tmpMap.put("ogv", "video/ogg");
        tmpMap.put("flv", "video/x-flv");
        tmpMap.put("mov", "video/quicktime");
        tmpMap.put("swf", "application/x-shockwave-flash");
        tmpMap.put("js", "application/javascript");
        tmpMap.put("pdf", "application/pdf");
        tmpMap.put("doc", "application/msword");
        tmpMap.put("ogg", "application/x-ogg");
        tmpMap.put("zip", "application/octet-stream");
        tmpMap.put("exe", "application/octet-stream");
        tmpMap.put("class", "application/octet-stream");
        tmpMap.put("webm", "video/webm");
        MIME_TYPES = Collections.unmodifiableMap(tmpMap);
    }

    public EpubServer(String host, int port, Package pckg, boolean quiet) {
        super(host, port);
        this.mPackage = pckg;
        this.quiet = quiet;
    }

    Package getPackage() {
        return mPackage;
    }
    
    public void startServer() {
        try {
            start();
        } catch (IOException e) {
            Log.e(TAG, ""+e.getMessage());
        }
    }

    /**
     * Serves file from homeDir and its' subdirectories (only). Uses only URI,
     * ignores all headers and HTTP parameters.
     */
    Response serveFile(String uri, Map<String, String> header, Package pckg) {
        synchronized (this) {

        Response res = null;

        final int contentLength = pckg.getArchiveInfoSize(uri);
        if (contentLength == 0) {
            res = new Response(Response.Status.NOT_FOUND,
                    NanoHTTPD.MIME_PLAINTEXT, "Error 404, file not found.");
        }

        if (res == null) {
            // Get MIME type from file name extension, if possible
            String mime = null;
            int dot = uri.lastIndexOf('.');
            if (dot >= 0) {<commit_msg>minor: synchronise on object, not class this<commit_after>/*
 * EpubServer.java
 * SDKLauncher-Android
 *
 * Created by <NAME> (Mantano) on 2013-09-03.
 */
//  Copyright (c) 2014 Readium Foundation and/or its licensees. All rights reserved.
//  Redistribution and use in source and binary forms, with or without modification, 
//  are permitted provided that the following conditions are met:
//  1. Redistributions of source code must retain the above copyright notice, this 
//  list of conditions and the following disclaimer.
//  2. Redistributions in binary form must reproduce the above copyright notice, 
//  this list of conditions and the following disclaimer in the documentation and/or 
//  other materials provided with the distribution.
//  3. Neither the name of the organization nor the names of its contributors may be 
//  used to endorse or promote products derived from this software without specific 
//  prior written permission.
//
//  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
//  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
//  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
//  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
//  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
//  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
//  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
//  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
//  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
//  OF THE POSSIBILITY OF SUCH DAMAGE

package org.readium.sdk.android.launcher.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.readium.sdk.android.ManifestItem;
import org.readium.sdk.android.Package;
import org.readium.sdk.android.PackageResource;

import android.util.Log;
import fi.iki.elonen.NanoHTTPD;

/**
 * This small web server will serve media files such as audio and video.
 */
public class EpubServer extends NanoHTTPD {

    private static final String TAG = "EpubServer";
    public static final String HTTP_HOST = "127.0.0.1";
    public static final int HTTP_PORT = 8080;
    /**
     * Hashtable mapping (String)FILENAME_EXTENSION -> (String)MIME_TYPE
     */
    private static final Map<String, String> MIME_TYPES;

    private final Package mPackage;
    private final boolean quiet;
    
    static {
        Map<String, String> tmpMap = new HashMap<String, String>();
        tmpMap.put("html", "application/xhtml+xml"); // FORCE
        tmpMap.put("xhtml", "application/xhtml+xml"); // FORCE
        tmpMap.put("xml", "application/xml"); // FORCE
        tmpMap.put("htm", "text/html");
        tmpMap.put("css", "text/css");
        tmpMap.put("java", "text/x-java-source, text/java");
        tmpMap.put("txt", "text/plain");
        tmpMap.put("asc", "text/plain");
        tmpMap.put("gif", "image/gif");
        tmpMap.put("jpg", "image/jpeg");
        tmpMap.put("jpeg", "image/jpeg");
        tmpMap.put("png", "image/png");
        tmpMap.put("mp3", "audio/mpeg");
        tmpMap.put("m3u", "audio/mpeg-url");
        tmpMap.put("mp4", "video/mp4");
        tmpMap.put("ogv", "video/ogg");
        tmpMap.put("flv", "video/x-flv");
        tmpMap.put("mov", "video/quicktime");
        tmpMap.put("swf", "application/x-shockwave-flash");
        tmpMap.put("js", "application/javascript");
        tmpMap.put("pdf", "application/pdf");
        tmpMap.put("doc", "application/msword");
        tmpMap.put("ogg", "application/x-ogg");
        tmpMap.put("zip", "application/octet-stream");
        tmpMap.put("exe", "application/octet-stream");
        tmpMap.put("class", "application/octet-stream");
        tmpMap.put("webm", "video/webm");
        MIME_TYPES = Collections.unmodifiableMap(tmpMap);
    }

    public EpubServer(String host, int port, Package pckg, boolean quiet) {
        super(host, port);
        this.mPackage = pckg;
        this.quiet = quiet;
    }

    Package getPackage() {
        return mPackage;
    }
    
    public void startServer() {
        try {
            start();
        } catch (IOException e) {
            Log.e(TAG, ""+e.getMessage());
        }
    }

    private final Object criticalSectionSynchronizedLock = new Object();

    /**
     * Serves file from homeDir and its' subdirectories (only). Uses only URI,
     * ignores all headers and HTTP parameters.
     */
    Response serveFile(String uri, Map<String, String> header, Package pckg) {
        synchronized (criticalSectionSynchronizedLock) {

        Response res = null;

        final int contentLength = pckg.getArchiveInfoSize(uri);
        if (contentLength == 0) {
            res = new Response(Response.Status.NOT_FOUND,
                    NanoHTTPD.MIME_PLAINTEXT, "Error 404, file not found.");
        }

        if (res == null) {
            // Get MIME type from file name extension, if possible
            String mime = null;
            int dot = uri.lastIndexOf('.');
            if (dot >= 0) {
{'fraction_non_alphanumeric': 0.11795741473525533, 'fraction_numerical': 0.006406632749199171, 'mean_word_length': 2.8982739625413148, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6348751', 'n_tokens_mistral': 3209, 'n_tokens_neox': 3049, 'n_words': 972}