package com.mstar.android.samba;

import com.benq.platform.IFPBenQService;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Locale;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.TimeZone;
import okhttp3.HttpUrl;

/* JADX INFO: loaded from: classes.dex */
public class NanoHTTPD {
    public static final boolean DBG = true;
    public static final String HTTP_BADREQUEST = "400 Bad Request";
    public static final String HTTP_FORBIDDEN = "403 Forbidden";
    public static final String HTTP_INTERNALERROR = "500 Internal Server Error";
    public static final String HTTP_NOTFOUND = "404 Not Found";
    public static final String HTTP_NOTIMPLEMENTED = "501 Not Implemented";
    public static final String HTTP_NOTMODIFIED = "304 Not Modified";
    public static final String HTTP_OK = "200 OK";
    public static final String HTTP_PARTIALCONTENT = "206 Partial Content";
    public static final String HTTP_RANGE_NOT_SATISFIABLE = "416 Requested Range Not Satisfiable";
    public static final String HTTP_REDIRECT = "301 Moved Permanently";
    public static final String MIME_DEFAULT_BINARY = "application/octet-stream";
    public static final String MIME_HTML = "text/html";
    public static final String MIME_PLAINTEXT = "text/plain";
    public static final String MIME_XML = "text/xml";
    private static SimpleDateFormat gmtFrmt = null;
    private static int theBufferSize = 1048576;
    private String SmbFullPath;
    private String mName;
    private String mPassword;
    private final ServerSocket myServerSocket;
    private int myTcpPort;
    private Thread myThread;
    protected static PrintStream myOut = System.out;
    protected static PrintStream myErr = System.err;
    private static Hashtable theMimeTypes = new Hashtable();

    private class HTTPSession implements Runnable {
        private Socket mySocket;

        public HTTPSession(Socket socket) {
            System.out.println("HttpSession socket: " + socket + "ip = " + socket.getInetAddress());
            this.mySocket = socket;
            Thread thread = new Thread(this);
            thread.setDaemon(true);
            thread.start();
        }

        private void decodeHeader(BufferedReader bufferedReader, Properties properties, Properties properties2, Properties properties3) throws InterruptedException {
            String strDecodePercent;
            try {
                String line = bufferedReader.readLine();
                if (line == null) {
                    return;
                }
                StringTokenizer stringTokenizer = new StringTokenizer(line);
                if (!stringTokenizer.hasMoreTokens()) {
                    sendError("400 Bad Request", "BAD REQUEST: Syntax error. Usage: GET /example/file.html");
                }
                properties.put(IFPBenQService.Companion.Broadcast.EXTRA_METHOD, stringTokenizer.nextToken());
                if (!stringTokenizer.hasMoreTokens()) {
                    sendError("400 Bad Request", "BAD REQUEST: Missing URI. Usage: GET /example/file.html");
                }
                String strNextToken = stringTokenizer.nextToken();
                int iIndexOf = strNextToken.indexOf(63);
                if (iIndexOf >= 0) {
                    decodeParms(strNextToken.substring(iIndexOf + 1), properties2);
                    strDecodePercent = decodePercent(strNextToken.substring(0, iIndexOf));
                } else {
                    strDecodePercent = decodePercent(strNextToken);
                }
                if (stringTokenizer.hasMoreTokens()) {
                    String line2 = bufferedReader.readLine();
                    while (line2 != null && line2.trim().length() > 0) {
                        int iIndexOf2 = line2.indexOf(58);
                        if (iIndexOf2 >= 0) {
                            properties3.put(line2.substring(0, iIndexOf2).trim().toLowerCase(), line2.substring(iIndexOf2 + 1).trim());
                        }
                        line2 = bufferedReader.readLine();
                    }
                }
                properties.put("uri", strDecodePercent);
            } catch (IOException e2) {
                sendError("500 Internal Server Error", "SERVER INTERNAL ERROR: IOException: " + e2.getMessage());
            }
        }

        private void decodeMultipartData(String str, byte[] bArr, BufferedReader bufferedReader, Properties properties, Properties properties2) throws InterruptedException {
            int i2;
            String strSubstring;
            Properties properties3;
            try {
                Utils utils = new Utils();
                int[] boundaryPositions = utils.getBoundaryPositions(bArr, str.getBytes());
                String line = bufferedReader.readLine();
                int i3 = 1;
                while (line != null) {
                    if (line.indexOf(str) == -1) {
                        sendError("400 Bad Request", "BAD REQUEST: Content type is multipart/form-data but next chunk does not start with boundary. Usage: GET /example/file.html");
                    }
                    int i4 = i3 + 1;
                    Properties properties4 = new Properties();
                    String line2 = bufferedReader.readLine();
                    while (true) {
                        i2 = 0;
                        if (line2 == null || line2.trim().length() <= 0) {
                            break;
                        }
                        int iIndexOf = line2.indexOf(58);
                        if (iIndexOf != -1) {
                            properties4.put(line2.substring(0, iIndexOf).trim().toLowerCase(), line2.substring(iIndexOf + 1).trim());
                        }
                        line2 = bufferedReader.readLine();
                    }
                    if (line2 != null) {
                        String property = properties4.getProperty("content-disposition");
                        if (property == null) {
                            sendError("400 Bad Request", "BAD REQUEST: Content type is multipart/form-data but no content-disposition info found. Usage: GET /example/file.html");
                        }
                        StringTokenizer stringTokenizer = new StringTokenizer(property, ";");
                        Properties properties5 = new Properties();
                        while (stringTokenizer.hasMoreTokens()) {
                            String strTrim = stringTokenizer.nextToken().trim();
                            int iIndexOf2 = strTrim.indexOf(61);
                            if (iIndexOf2 != -1) {
                                properties5.put(strTrim.substring(i2, iIndexOf2).trim().toLowerCase(), strTrim.substring(iIndexOf2 + 1).trim());
                            }
                            i2 = 0;
                        }
                        String property2 = properties5.getProperty("name");
                        String strSubstring2 = property2.substring(1, property2.length() - 1);
                        String str2 = HttpUrl.FRAGMENT_ENCODE_SET;
                        if (properties4.getProperty("content-type") == null) {
                            while (line2 != null && line2.indexOf(str) == -1) {
                                line2 = bufferedReader.readLine();
                                if (line2 != null) {
                                    int iIndexOf3 = line2.indexOf(str);
                                    if (iIndexOf3 == -1) {
                                        str2 = str2 + line2;
                                    } else {
                                        str2 = str2 + line2.substring(0, iIndexOf3 - 2);
                                    }
                                }
                            }
                            properties3 = properties;
                            strSubstring = str2;
                        } else {
                            if (i4 > boundaryPositions.length) {
                                sendError("500 Internal Server Error", "Error processing request");
                            }
                            properties2.put(strSubstring2, utils.saveTmpFile(bArr, utils.stripMultipartHeaders(bArr, boundaryPositions[i3 - 1]), (boundaryPositions[i3] - r9) - 4, "test"));
                            String property3 = properties5.getProperty("filename");
                            strSubstring = property3.substring(1, property3.length() - 1);
                            do {
                                line2 = bufferedReader.readLine();
                                if (line2 == null) {
                                    break;
                                }
                            } while (line2.indexOf(str) == -1);
                            properties3 = properties;
                        }
                        properties3.put(strSubstring2, strSubstring);
                    }
                    i3 = i4;
                    line = line2;
                }
            } catch (IOException e2) {
                sendError("500 Internal Server Error", "SERVER INTERNAL ERROR: IOException: " + e2.getMessage());
            }
        }

        private void decodeParms(String str, Properties properties) throws InterruptedException {
            if (str == null) {
                return;
            }
            StringTokenizer stringTokenizer = new StringTokenizer(str, "&");
            while (stringTokenizer.hasMoreTokens()) {
                String strNextToken = stringTokenizer.nextToken();
                int iIndexOf = strNextToken.indexOf(61);
                if (iIndexOf >= 0) {
                    properties.put(decodePercent(strNextToken.substring(0, iIndexOf)).trim(), decodePercent(strNextToken.substring(iIndexOf + 1)));
                } else {
                    properties.put(decodePercent(strNextToken).trim(), HttpUrl.FRAGMENT_ENCODE_SET);
                }
            }
        }

        private String decodePercent(String str) throws InterruptedException {
            PrintStream printStream = System.out;
            printStream.println("before decodePercent str = " + str);
            try {
                String strDecode = URLDecoder.decode(str, "UTF-8");
                printStream.println("after decodePercent result = " + strDecode);
                return strDecode;
            } catch (Exception unused) {
                sendError("400 Bad Request", "BAD REQUEST: Bad percent-encoding.");
                return null;
            }
        }

        private void sendError(String str, String str2) throws InterruptedException {
            sendResponse(str, NanoHTTPD.MIME_PLAINTEXT, null, new ByteArrayInputStream(str2.getBytes()));
            throw new InterruptedException();
        }

        private void sendResponse(String str, String str2, Properties properties, InputStream inputStream) {
            System.out.println("Enter sendReponse!");
            try {
                try {
                    if (str == null) {
                        throw new Error("sendResponse(): Status can't be null.");
                    }
                    OutputStream outputStream = this.mySocket.getOutputStream();
                    PrintWriter printWriter = new PrintWriter(outputStream);
                    printWriter.print("HTTP/1.0 " + str + " \r\n");
                    if (str2 != null) {
                        printWriter.print("Content-Type: " + str2 + "\r\n");
                    }
                    if (properties == null || properties.getProperty("Date") == null) {
                        printWriter.print("Date: " + NanoHTTPD.gmtFrmt.format(new Date()) + "\r\n");
                    }
                    if (properties != null) {
                        Enumeration enumerationKeys = properties.keys();
                        while (enumerationKeys.hasMoreElements()) {
                            String str3 = (String) enumerationKeys.nextElement();
                            printWriter.print(str3 + ": " + properties.getProperty(str3) + "\r\n");
                        }
                    }
                    printWriter.print("\r\n");
                    printWriter.flush();
                    PrintStream printStream = System.out;
                    printStream.println("send data");
                    if (inputStream != null) {
                        int iGetAvailableLength = Utils.GetAvailableLength();
                        printStream.println("pending = " + iGetAvailableLength);
                        byte[] bArr = new byte[NanoHTTPD.theBufferSize];
                        while (iGetAvailableLength > 0) {
                            int i2 = inputStream.read(bArr, 0, iGetAvailableLength > NanoHTTPD.theBufferSize ? NanoHTTPD.theBufferSize : iGetAvailableLength);
                            if (i2 <= 0) {
                                break;
                            }
                            outputStream.write(bArr, 0, i2);
                            iGetAvailableLength -= i2;
                        }
                    }
                    outputStream.flush();
                    outputStream.close();
                    if (inputStream != null) {
                        inputStream.close();
                    }
                } catch (IOException unused) {
                    this.mySocket.close();
                }
            } catch (Throwable unused2) {
            }
        }

        /* JADX WARN: Removed duplicated region for block: B:50:0x0104 A[Catch: IOException -> 0x0032, all -> 0x01d4, TRY_LEAVE, TryCatch #3 {IOException -> 0x0032, all -> 0x01d4, blocks: (B:3:0x0008, B:6:0x0011, B:8:0x001e, B:11:0x002b, B:14:0x0035, B:17:0x0075, B:20:0x0081, B:22:0x0088, B:24:0x008f, B:29:0x009c, B:33:0x00a8, B:35:0x00b3, B:38:0x00c0, B:40:0x00d9, B:43:0x00e8, B:45:0x00f5, B:48:0x00fe, B:50:0x0104, B:53:0x010c, B:54:0x0111, B:56:0x0123, B:57:0x0128, B:65:0x0173, B:67:0x017b, B:69:0x0193, B:71:0x01a4, B:73:0x01b5, B:72:0x01aa, B:58:0x013e, B:60:0x0149, B:62:0x0151, B:63:0x016a), top: B:81:0x0008 }] */
        /* JADX WARN: Removed duplicated region for block: B:58:0x013e A[Catch: IOException -> 0x0032, all -> 0x01d4, TryCatch #3 {IOException -> 0x0032, all -> 0x01d4, blocks: (B:3:0x0008, B:6:0x0011, B:8:0x001e, B:11:0x002b, B:14:0x0035, B:17:0x0075, B:20:0x0081, B:22:0x0088, B:24:0x008f, B:29:0x009c, B:33:0x00a8, B:35:0x00b3, B:38:0x00c0, B:40:0x00d9, B:43:0x00e8, B:45:0x00f5, B:48:0x00fe, B:50:0x0104, B:53:0x010c, B:54:0x0111, B:56:0x0123, B:57:0x0128, B:65:0x0173, B:67:0x017b, B:69:0x0193, B:71:0x01a4, B:73:0x01b5, B:72:0x01aa, B:58:0x013e, B:60:0x0149, B:62:0x0151, B:63:0x016a), top: B:81:0x0008 }] */
        @Override // java.lang.Runnable
        /*
            Code decompiled incorrectly, please refer to instructions dump.
            To view partially-correct add '--show-bad-code' argument
        */
        public void run() throws java.lang.InterruptedException {
            /*
                Method dump skipped, instruction units count: 469
                To view this dump add '--comments-level debug' option
            */
            throw new UnsupportedOperationException("Method not decompiled: com.mstar.android.samba.NanoHTTPD.HTTPSession.run():void");
        }
    }

    static {
        StringTokenizer stringTokenizer = new StringTokenizer("css        text/css htm        text/html html        text/html xml        text/xml txt        text/plain asc        text/plain gif        image/gif jpg        image/jpeg jpeg        image/jpeg png        image/png mp3        audio/mpeg m3u        audio/mpeg-url mp4        video/mp4 ogv        video/ogg flv        video/x-flv mov        video/quicktime swf        application/x-shockwave-flash js        application/javascript pdf        application/pdf doc        application/msword ogg        application/x-ogg zip        application/octet-stream exe        application/octet-stream class        application/octet-stream ");
        while (stringTokenizer.hasMoreTokens()) {
            theMimeTypes.put(stringTokenizer.nextToken(), stringTokenizer.nextToken());
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("E, d MMM yyyy HH:mm:ss 'GMT'", Locale.US);
        gmtFrmt = simpleDateFormat;
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
    }

    public NanoHTTPD(int i2, File file) throws IOException {
        this.myTcpPort = i2;
        this.myServerSocket = new ServerSocket(this.myTcpPort);
        Thread thread = new Thread(new Runnable() { // from class: com.mstar.android.samba.NanoHTTPD.1
            @Override // java.lang.Runnable
            public void run() {
                while (true) {
                    try {
                        NanoHTTPD nanoHTTPD = NanoHTTPD.this;
                        nanoHTTPD.new HTTPSession(nanoHTTPD.myServerSocket.accept());
                    } catch (IOException unused) {
                        return;
                    }
                }
            }
        });
        this.myThread = thread;
        thread.setDaemon(true);
        this.myThread.start();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public Response serve(String str, String str2, Properties properties, Properties properties2, Properties properties3) {
        myOut.println(str2 + " '" + str + "' ");
        Enumeration<?> enumerationPropertyNames = properties.propertyNames();
        while (enumerationPropertyNames.hasMoreElements()) {
            String str3 = (String) enumerationPropertyNames.nextElement();
            myOut.println("  Head Request: '" + str3 + "' = '" + properties.getProperty(str3) + "'");
        }
        Enumeration<?> enumerationPropertyNames2 = properties2.propertyNames();
        while (enumerationPropertyNames2.hasMoreElements()) {
            String str4 = (String) enumerationPropertyNames2.nextElement();
            myOut.println("  PRM: '" + str4 + "' = '" + properties2.getProperty(str4) + "'");
        }
        Enumeration<?> enumerationPropertyNames3 = properties3.propertyNames();
        while (enumerationPropertyNames3.hasMoreElements()) {
            String str5 = (String) enumerationPropertyNames3.nextElement();
            myOut.println("  UPLOADED: '" + str5 + "' = '" + properties3.getProperty(str5) + "'");
        }
        return serveFile(str, properties, true);
    }

    public String GetSambaName() {
        return this.mPassword;
    }

    public String GetSambaPassword() {
        return this.mName;
    }

    public String GetSambaUrl() {
        return this.SmbFullPath;
    }

    public void SetSambaName(String str) {
        this.mName = str;
    }

    public void SetSambaPassword(String str) {
        this.mPassword = str;
    }

    public void SetSambaUrl(String str) {
        this.SmbFullPath = str;
    }

    /* JADX WARN: Removed duplicated region for block: B:101:0x03bf A[Catch: V -> 0x0392, IOException -> 0x0414, TryCatch #10 {V -> 0x0392, IOException -> 0x0414, blocks: (B:82:0x02ee, B:84:0x030f, B:85:0x0311, B:89:0x031e, B:91:0x0370, B:95:0x0399, B:99:0x03a3, B:101:0x03bf, B:102:0x03cb, B:104:0x03f3, B:105:0x040c), top: B:133:0x0299 }] */
    /* JADX WARN: Removed duplicated region for block: B:102:0x03cb A[Catch: V -> 0x0392, IOException -> 0x0414, TryCatch #10 {V -> 0x0392, IOException -> 0x0414, blocks: (B:82:0x02ee, B:84:0x030f, B:85:0x0311, B:89:0x031e, B:91:0x0370, B:95:0x0399, B:99:0x03a3, B:101:0x03bf, B:102:0x03cb, B:104:0x03f3, B:105:0x040c), top: B:133:0x0299 }] */
    /* JADX WARN: Removed duplicated region for block: B:99:0x03a3 A[Catch: V -> 0x0392, IOException -> 0x0414, TryCatch #10 {V -> 0x0392, IOException -> 0x0414, blocks: (B:82:0x02ee, B:84:0x030f, B:85:0x0311, B:89:0x031e, B:91:0x0370, B:95:0x0399, B:99:0x03a3, B:101:0x03bf, B:102:0x03cb, B:104:0x03f3, B:105:0x040c), top: B:133:0x0299 }] */
    /* JADX WARN: Unreachable blocks removed: 2, instructions: 3 */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public com.mstar.android.samba.Response serveFile(java.lang.String r29, java.util.Properties r30, boolean r31) {
        /*
            Method dump skipped, instruction units count: 1105
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: com.mstar.android.samba.NanoHTTPD.serveFile(java.lang.String, java.util.Properties, boolean):com.mstar.android.samba.Response");
    }

    public void stop() {
        try {
            System.out.println("HTTP stop");
            this.myServerSocket.close();
            this.myThread.join();
        } catch (IOException | InterruptedException unused) {
        }
    }
}
