搜书大师 v23.2版本的 MD5 值为:5ee433f6456b6278bd6e80a5d44df55e
以下内容为反编译后的 jb1.java 源代码,内容仅作参考
package com.lygame.aaa;
import com.flyersoft.WB.DownloadTaskBrowser;
import com.qq.e.ads.nativ.express2.MediaEventListener;
import com.qq.e.ads.nativ.express2.NativeExpressAD2CallbackExt;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.security.KeyStore;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.TimeZone;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPOutputStream;
import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.TrustManagerFactory;
import org.jsoup.helper.HttpConnection;
public abstract class jb1 {
public static final String MIME_HTML = "text/html";
public static final String MIME_PLAINTEXT = "text/plain";
protected static Map<String, String> MIME_TYPES = null;
private static final String QUERY_STRING_PARAMETER = "NanoHttpd.QUERY_STRING";
public static final int SOCKET_READ_TIMEOUT = 5000;
protected b asyncRunner;
private final String hostname;
private final int myPort;
private volatile ServerSocket myServerSocket;
private Thread myThread;
private s serverSocketFactory;
private v tempFileManagerFactory;
private static final String CONTENT_DISPOSITION_REGEX = "([ |\t]*Content-Disposition[ |\t]*:)(.*)";
private static final Pattern CONTENT_DISPOSITION_PATTERN = Pattern.compile(CONTENT_DISPOSITION_REGEX, 2);
private static final String CONTENT_TYPE_REGEX = "([ |\t]*content-type[ |\t]*:)(.*)";
private static final Pattern CONTENT_TYPE_PATTERN = Pattern.compile(CONTENT_TYPE_REGEX, 2);
private static final String CONTENT_DISPOSITION_ATTRIBUTE_REGEX = "[ |\t]*([a-zA-Z]*)[ |\t]*=[ |\t]*['|\"]([^\"^']*)['|\"]";
private static final Pattern CONTENT_DISPOSITION_ATTRIBUTE_PATTERN = Pattern.compile(CONTENT_DISPOSITION_ATTRIBUTE_REGEX);
private static final Logger LOG = Logger.getLogger(jb1.class.getName());
public interface b {
void closeAll();
void closed(c cVar);
void exec(c cVar);
}
public class c implements Runnable {
private final InputStream a;
private final Socket b;
public c(InputStream inputStream, Socket socket) {
this.a = inputStream;
this.b = socket;
}
public void a() {
jb1.safeClose(this.a);
jb1.safeClose(this.b);
}
@Override
public void run() {
OutputStream outputStream = null;
try {
try {
outputStream = this.b.getOutputStream();
l lVar = new l(jb1.this.tempFileManagerFactory.create(), this.a, outputStream, this.b.getInetAddress());
while (!this.b.isClosed()) {
lVar.execute();
}
} catch (Exception e) {
if ((!(e instanceof SocketException) || !"NanoHttpd Shutdown".equals(e.getMessage())) && !(e instanceof SocketTimeoutException)) {
jb1.LOG.log(Level.SEVERE, "Communication with the client broken, or an bug in the handler code", (Throwable) e);
}
}
} finally {
jb1.safeClose(outputStream);
jb1.safeClose(this.a);
jb1.safeClose(this.b);
jb1.this.asyncRunner.closed(this);
}
}
}
public static class d {
private static final String a = "US-ASCII";
private static final String b = "multipart/form-data";
private static final String c = "[ |\t]*([^/^ ^;^,]+/[^ ^;^,]+)";
private static final Pattern d = Pattern.compile(c, 2);
private static final String e = "[ |\t]*(charset)[ |\t]*=[ |\t]*['|\"]?([^\"^'^;^,]*)['|\"]?";
private static final Pattern f = Pattern.compile(e, 2);
private static final String g = "[ |\t]*(boundary)[ |\t]*=[ |\t]*['|\"]?([^\"^'^;^,]*)['|\"]?";
private static final Pattern h = Pattern.compile(g, 2);
private final String i;
private final String j;
private final String k;
private final String l;
public d(String str) {
this.i = str;
if (str != null) {
this.j = d(str, d, "", 1);
this.k = d(str, f, null, 2);
} else {
this.j = "";
this.k = "UTF-8";
}
if ("multipart/form-data".equalsIgnoreCase(this.j)) {
this.l = d(str, h, null, 2);
} else {
this.l = null;
}
}
private String d(String str, Pattern pattern, String str2, int i) {
Matcher matcher = pattern.matcher(str);
return matcher.find() ? matcher.group(i) : str2;
}
public String a() {
return this.l;
}
public String b() {
return this.j;
}
public String c() {
return this.i;
}
public String e() {
String str = this.k;
return str == null ? "US-ASCII" : str;
}
public boolean f() {
return "multipart/form-data".equalsIgnoreCase(this.j);
}
public d g() {
if (this.k != null) {
return this;
}
return new d(this.i + "; charset=UTF-8");
}
}
public static class e {
private final String a;
private final String b;
private final String c;
public e(String str, String str2) {
this(str, str2, 30);
}
public static String b(int i) {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
simpleDateFormat.setTimeZone(TimeZone.getTimeZone(tx2.a));
calendar.add(5, i);
return simpleDateFormat.format(calendar.getTime());
}
public String a() {
return String.format("%s=%s; expires=%s", this.a, this.b, this.c);
}
public e(String str, String str2, int i) {
this.a = str;
this.b = str2;
this.c = b(i);
}
public e(String str, String str2, String str3) {
this.a = str;
this.b = str2;
this.c = str3;
}
}
public class f implements Iterable<String> {
private final HashMap<String, String> a = new HashMap<>();
private final ArrayList<e> b = new ArrayList<>();
public f(Map<String, String> map) {
String str = map.get("cookie");
if (str != null) {
for (String str2 : str.split(x1.b)) {
String[] split = str2.trim().split("=");
if (split.length == 2) {
this.a.put(split[0], split[1]);
}
}
}
}
public void a(String str) {
d(str, "-delete-", -30);
}
public String b(String str) {
return this.a.get(str);
}
public void c(e eVar) {
this.b.add(eVar);
}
public void d(String str, String str2, int i) {
this.b.add(new e(str, str2, e.b(i)));
}
public void e(o oVar) {
Iterator<e> it = this.b.iterator();
while (it.hasNext()) {
oVar.b("Set-Cookie", it.next().a());
}
}
@Override
public Iterator<String> iterator() {
return this.a.keySet().iterator();
}
}
public static class g implements b {
private long a;
private final List<c> b = Collections.synchronizedList(new ArrayList());
public List<c> a() {
return this.b;
}
@Override
public void closeAll() {
Iterator it = new ArrayList(this.b).iterator();
while (it.hasNext()) {
((c) it.next()).a();
}
}
@Override
public void closed(c cVar) {
this.b.remove(cVar);
}
@Override
public void exec(c cVar) {
this.a++;
Thread thread = new Thread(cVar);
thread.setDaemon(true);
thread.setName("NanoHttpd Request Processor (#" + this.a + ")");
this.b.add(cVar);
thread.start();
}
}
public static class h implements s {
@Override
public ServerSocket create() throws IOException {
return new ServerSocket();
}
}
public static class i implements t {
private final File a;
private final OutputStream b;
public i(File file) throws IOException {
File createTempFile = File.createTempFile("NanoHTTPD-", "", file);
this.a = createTempFile;
this.b = new FileOutputStream(createTempFile);
}
@Override
public void delete() throws Exception {
jb1.safeClose(this.b);
if (this.a.delete()) {
return;
}
throw new Exception("could not delete temporary file: " + this.a.getAbsolutePath());
}
@Override
public String getName() {
return this.a.getAbsolutePath();
}
@Override
public OutputStream open() throws Exception {
return this.b;
}
}
public static class j implements u {
private final File a;
private final List<t> b;
public j() {
File file = new File(System.getProperty("java.io.tmpdir"));
this.a = file;
if (!file.exists()) {
file.mkdirs();
}
this.b = new ArrayList();
}
@Override
public void clear() {
Iterator<t> it = this.b.iterator();
while (it.hasNext()) {
try {
it.next().delete();
} catch (Exception e) {
jb1.LOG.log(Level.WARNING, "could not delete file ", (Throwable) e);
}
}
this.b.clear();
}
@Override
public t createTempFile(String str) throws Exception {
i iVar = new i(this.a);
this.b.add(iVar);
return iVar;
}
}
private class k implements v {
private k() {
}
@Override
public u create() {
return new j();
}
}
public interface m {
void execute() throws IOException;
f getCookies();
Map<String, String> getHeaders();
InputStream getInputStream();
n getMethod();
Map<String, List<String>> getParameters();
@Deprecated
Map<String, String> getParms();
String getQueryParameterString();
String getRemoteHostName();
String getRemoteIpAddress();
String getUri();
void parseBody(Map<String, String> map) throws IOException, p;
}
public enum n {
GET,
PUT,
POST,
DELETE,
HEAD,
OPTIONS,
TRACE,
CONNECT,
PATCH,
PROPFIND,
PROPPATCH,
MKCOL,
MOVE,
COPY,
LOCK,
UNLOCK;
static n lookup(String str) {
if (str == null) {
return null;
}
try {
return valueOf(str);
} catch (IllegalArgumentException unused) {
return null;
}
}
}
public static class o implements Closeable {
private c a;
private boolean a0;
private String b;
private boolean b0;
private InputStream c;
private boolean c0;
private long d;
private final Map<String, String> e = new a();
private final Map<String, String> f = new HashMap();
private n g;
class a extends HashMap<String, String> {
a() {
}
@Override
public String put(String str, String str2) {
o.this.f.put(str == null ? str : str.toLowerCase(), str2);
return (String) super.put((a) str, str2);
}
}
public static class b extends FilterOutputStream {
public b(OutputStream outputStream) {
super(outputStream);
}
public void b() throws IOException {
((FilterOutputStream) this).out.write("0\r\n\r\n".getBytes());
}
@Override
public void write(int i) throws IOException {
write(new byte[]{(byte) i}, 0, 1);
}
@Override
public void write(byte[] bArr) throws IOException {
write(bArr, 0, bArr.length);
}
@Override
public void write(byte[] bArr, int i, int i2) throws IOException {
if (i2 == 0) {
return;
}
((FilterOutputStream) this).out.write(String.format("%x\r\n", Integer.valueOf(i2)).getBytes());
((FilterOutputStream) this).out.write(bArr, i, i2);
((FilterOutputStream) this).out.write("\r\n".getBytes());
}
}
public interface c {
String getDescription();
int getRequestStatus();
}
public enum d implements c {
SWITCH_PROTOCOL(101, "Switching Protocols"),
OK(200, "OK"),
CREATED(201, "Created"),
ACCEPTED(202, "Accepted"),
NO_CONTENT(204, "No Content"),
PARTIAL_CONTENT(206, "Partial Content"),
MULTI_STATUS(MediaEventListener.EVENT_VIDEO_ERROR, "Multi-Status"),
REDIRECT(NativeExpressAD2CallbackExt.EVENT_VIDEO_PAGE_OPEN, "Moved Permanently"),
FOUND(NativeExpressAD2CallbackExt.EVENT_VIDEO_PAGE_CLOSE, "Found"),
REDIRECT_SEE_OTHER(NativeExpressAD2CallbackExt.EVENT_LEFT_APPLICATION, "See Other"),
NOT_MODIFIED(NativeExpressAD2CallbackExt.EVENT_OPEN_OVERLAY, "Not Modified"),
TEMPORARY_REDIRECT(307, "Temporary Redirect"),
BAD_REQUEST(400, "Bad Request"),
UNAUTHORIZED(401, "Unauthorized"),
FORBIDDEN(403, "Forbidden"),
NOT_FOUND(y91.i, "Not Found"),
METHOD_NOT_ALLOWED(405, "Method Not Allowed"),
NOT_ACCEPTABLE(406, "Not Acceptable"),
REQUEST_TIMEOUT(408, "Request Timeout"),
CONFLICT(409, "Conflict"),
GONE(410, "Gone"),
LENGTH_REQUIRED(411, "Length Required"),
PRECONDITION_FAILED(412, "Precondition Failed"),
PAYLOAD_TOO_LARGE(413, "Payload Too Large"),
UNSUPPORTED_MEDIA_TYPE(415, "Unsupported Media Type"),
RANGE_NOT_SATISFIABLE(416, "Requested Range Not Satisfiable"),
EXPECTATION_FAILED(417, "Expectation Failed"),
TOO_MANY_REQUESTS(429, "Too Many Requests"),
INTERNAL_ERROR(DownloadTaskBrowser.MIN_JS_DEALY, "Internal Server Error"),
NOT_IMPLEMENTED(501, "Not Implemented"),
SERVICE_UNAVAILABLE(503, "Service Unavailable"),
UNSUPPORTED_HTTP_VERSION(505, "HTTP Version Not Supported");
private final String description;
private final int requestStatus;
d(int i, String str) {
this.requestStatus = i;
this.description = str;
}
public static d lookup(int i) {
for (d dVar : values()) {
if (dVar.getRequestStatus() == i) {
return dVar;
}
}
return null;
}
@Override
public String getDescription() {
return "" + this.requestStatus + " " + this.description;
}
@Override
public int getRequestStatus() {
return this.requestStatus;
}
}
public o(c cVar, String str, InputStream inputStream, long j) {
this.a = cVar;
this.b = str;
if (inputStream == null) {
this.c = new ByteArrayInputStream(new byte[0]);
this.d = 0L;
} else {
this.c = inputStream;
this.d = j;
}
this.a0 = this.d < 0;
this.c0 = true;
}
private void u(OutputStream outputStream, long j) throws IOException {
byte[] bArr = new byte[(int) 16384];
boolean z = j == -1;
while (true) {
if (j <= 0 && !z) {
return;
}
int read = this.c.read(bArr, 0, (int) (z ? 16384L : Math.min(j, 16384L)));
if (read <= 0) {
return;
}
outputStream.write(bArr, 0, read);
if (!z) {
j -= read;
}
}
}
private void v(OutputStream outputStream, long j) throws IOException {
if (this.b0) {
GZIPOutputStream gZIPOutputStream = new GZIPOutputStream(outputStream);
u(gZIPOutputStream, -1L);
gZIPOutputStream.finish();
return;
}
u(outputStream, j);
}
private void w(OutputStream outputStream, long j) throws IOException {
if (this.g != n.HEAD && this.a0) {
b bVar = new b(outputStream);
v(bVar, -1L);
bVar.b();
return;
}
v(outputStream, j);
}
public void A(boolean z) {
this.b0 = z;
}
public void B(boolean z) {
this.c0 = z;
}
public void C(String str) {
this.b = str;
}
public void D(n nVar) {
this.g = nVar;
}
public void E(c cVar) {
this.a = cVar;
}
public void b(String str, String str2) {
this.e.put(str, str2);
}
public void c(boolean z) {
if (z) {
this.e.put(kb1.HEADER_CONNECTION, "close");
} else {
this.e.remove(kb1.HEADER_CONNECTION);
}
}
@Override
public void close() throws IOException {
InputStream inputStream = this.c;
if (inputStream != null) {
inputStream.close();
}
}
public InputStream d() {
return this.c;
}
public String e(String str) {
return this.f.get(str.toLowerCase());
}
public String g() {
return this.b;
}
public n n() {
return this.g;
}
public c o() {
return this.a;
}
public boolean r() {
return "close".equals(e(kb1.HEADER_CONNECTION));
}
protected void s(PrintWriter printWriter, String str, String str2) {
printWriter.append((CharSequence) str).append(": ").append((CharSequence) str2).append("\r\n");
}
public void t(OutputStream outputStream) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("E, d MMM yyyy HH:mm:ss 'GMT'", Locale.US);
simpleDateFormat.setTimeZone(TimeZone.getTimeZone(tx2.a));
try {
if (this.a != null) {
PrintWriter printWriter = new PrintWriter((Writer) new BufferedWriter(new OutputStreamWriter(outputStream, new d(this.b).e())), false);
printWriter.append("HTTP/1.1 ").append((CharSequence) this.a.getDescription()).append(" \r\n");
String str = this.b;
if (str != null) {
s(printWriter, HttpConnection.CONTENT_TYPE, str);
}
if (e(f03.j) == null) {
s(printWriter, "Date", simpleDateFormat.format(new Date()));
}
for (Map.Entry<String, String> entry : this.e.entrySet()) {
s(printWriter, entry.getKey(), entry.getValue());
}
if (e(kb1.HEADER_CONNECTION) == null) {
s(printWriter, "Connection", this.c0 ? "keep-alive" : "close");
}
if (e("content-length") != null) {
this.b0 = false;
}
if (this.b0) {
s(printWriter, HttpConnection.CONTENT_ENCODING, "gzip");
y(true);
}
long j = this.c != null ? this.d : 0L;
if (this.g != n.HEAD && this.a0) {
s(printWriter, "Transfer-Encoding", "chunked");
} else if (!this.b0) {
j = x(printWriter, j);
}
printWriter.append("\r\n");
printWriter.flush();
w(outputStream, j);
outputStream.flush();
jb1.safeClose(this.c);
return;
}
throw new Error("sendResponse(): Status can't be null.");
} catch (IOException e) {
jb1.LOG.log(Level.SEVERE, "Could not send response to the client", (Throwable) e);
}
}
protected long x(PrintWriter printWriter, long j) {
String e = e("content-length");
if (e != null) {
try {
j = Long.parseLong(e);
} catch (NumberFormatException unused) {
jb1.LOG.severe("content-length was no number " + e);
}
}
printWriter.print("Content-Length: " + j + "\r\n");
return j;
}
public void y(boolean z) {
this.a0 = z;
}
public void z(InputStream inputStream) {
this.c = inputStream;
}
}
public static class q implements s {
private SSLServerSocketFactory a;
private String[] b;
public q(SSLServerSocketFactory sSLServerSocketFactory, String[] strArr) {
this.a = sSLServerSocketFactory;
this.b = strArr;
}
@Override
public ServerSocket create() throws IOException {
SSLServerSocket sSLServerSocket = (SSLServerSocket) this.a.createServerSocket();
String[] strArr = this.b;
if (strArr != null) {
sSLServerSocket.setEnabledProtocols(strArr);
} else {
sSLServerSocket.setEnabledProtocols(sSLServerSocket.getSupportedProtocols());
}
sSLServerSocket.setUseClientMode(false);
sSLServerSocket.setWantClientAuth(false);
sSLServerSocket.setNeedClientAuth(false);
return sSLServerSocket;
}
}
public class r implements Runnable {
private final int a;
private IOException b;
private boolean c = false;
public r(int i) {
this.a = i;
}
@Override
public void run() {
try {
jb1.this.myServerSocket.bind(jb1.this.hostname != null ? new InetSocketAddress(jb1.this.hostname, jb1.this.myPort) : new InetSocketAddress(jb1.this.myPort));
this.c = true;
do {
try {
Socket accept = jb1.this.myServerSocket.accept();
int i = this.a;
if (i > 0) {
accept.setSoTimeout(i);
}
InputStream inputStream = accept.getInputStream();
jb1 jb1Var = jb1.this;
jb1Var.asyncRunner.exec(jb1Var.createClientHandler(accept, inputStream));
} catch (IOException e) {
jb1.LOG.log(Level.FINE, "Communication with the client broken", (Throwable) e);
}
} while (!jb1.this.myServerSocket.isClosed());
} catch (IOException e2) {
this.b = e2;
}
}
}
public interface s {
ServerSocket create() throws IOException;
}
public interface t {
void delete() throws Exception;
String getName();
OutputStream open() throws Exception;
}
public interface u {
void clear();
t createTempFile(String str) throws Exception;
}
public interface v {
u create();
}
public jb1(int i2) {
this(null, i2);
}
protected static Map<String, List<String>> decodeParameters(Map<String, String> map) {
return decodeParameters(map.get(QUERY_STRING_PARAMETER));
}
protected static String decodePercent(String str) {
try {
return URLDecoder.decode(str, "UTF8");
} catch (UnsupportedEncodingException e2) {
LOG.log(Level.WARNING, "Encoding not supported, ignored", (Throwable) e2);
return null;
}
}
public static String getMimeTypeForFile(String str) {
int lastIndexOf = str.lastIndexOf(46);
String str2 = lastIndexOf >= 0 ? mimeTypes().get(str.substring(lastIndexOf + 1).toLowerCase()) : null;
return str2 == null ? "application/octet-stream" : str2;
}
private static void loadMimeTypes(Map<String, String> map, String str) {
try {
Enumeration<URL> resources = jb1.class.getClassLoader().getResources(str);
while (resources.hasMoreElements()) {
URL nextElement = resources.nextElement();
Properties properties = new Properties();
InputStream inputStream = null;
try {
try {
inputStream = nextElement.openStream();
properties.load(inputStream);
} catch (IOException e2) {
LOG.log(Level.SEVERE, "could not load mimetypes from " + nextElement, (Throwable) e2);
}
safeClose(inputStream);
map.putAll(properties);
} catch (Throwable th) {
safeClose(inputStream);
throw th;
}
}
} catch (IOException unused) {
LOG.log(Level.INFO, "no mime types available at " + str);
}
}
public static SSLServerSocketFactory makeSSLSocketFactory(KeyStore keyStore, KeyManager[] keyManagerArr) throws IOException {
try {
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(keyStore);
SSLContext sSLContext = SSLContext.getInstance("TLS");
sSLContext.init(keyManagerArr, trustManagerFactory.getTrustManagers(), null);
return sSLContext.getServerSocketFactory();
} catch (Exception e2) {
throw new IOException(e2.getMessage());
}
}
public static Map<String, String> mimeTypes() {
if (MIME_TYPES == null) {
HashMap hashMap = new HashMap();
MIME_TYPES = hashMap;
loadMimeTypes(hashMap, "META-INF/nanohttpd/default-mimetypes.properties");
loadMimeTypes(MIME_TYPES, "META-INF/nanohttpd/mimetypes.properties");
if (MIME_TYPES.isEmpty()) {
LOG.log(Level.WARNING, "no mime types found in the classpath! please provide mimetypes.properties");
}
}
return MIME_TYPES;
}
public static o newChunkedResponse(o.c cVar, String str, InputStream inputStream) {
return new o(cVar, str, inputStream, -1L);
}
public static o newFixedLengthResponse(o.c cVar, String str, InputStream inputStream, long j2) {
return new o(cVar, str, inputStream, j2);
}
public static final void safeClose(Object obj) {
if (obj != null) {
try {
if (obj instanceof Closeable) {
((Closeable) obj).close();
} else if (obj instanceof Socket) {
((Socket) obj).close();
} else {
if (obj instanceof ServerSocket) {
((ServerSocket) obj).close();
return;
}
throw new IllegalArgumentException("Unknown object to close");
}
} catch (IOException e2) {
LOG.log(Level.SEVERE, "Could not close", (Throwable) e2);
}
}
}
public synchronized void closeAllConnections() {
stop();
}
protected c createClientHandler(Socket socket, InputStream inputStream) {
return new c(inputStream, socket);
}
protected r createServerRunnable(int i2) {
return new r(i2);
}
public String getHostname() {
return this.hostname;
}
public final int getListeningPort() {
if (this.myServerSocket == null) {
return -1;
}
return this.myServerSocket.getLocalPort();
}
public s getServerSocketFactory() {
return this.serverSocketFactory;
}
public v getTempFileManagerFactory() {
return this.tempFileManagerFactory;
}
public final boolean isAlive() {
return wasStarted() && !this.myServerSocket.isClosed() && this.myThread.isAlive();
}
public void makeSecure(SSLServerSocketFactory sSLServerSocketFactory, String[] strArr) {
this.serverSocketFactory = new q(sSLServerSocketFactory, strArr);
}
public o serve(m mVar) {
HashMap hashMap = new HashMap();
n method = mVar.getMethod();
if (n.PUT.equals(method) || n.POST.equals(method)) {
try {
mVar.parseBody(hashMap);
} catch (p e2) {
return newFixedLengthResponse(e2.getStatus(), "text/plain", e2.getMessage());
} catch (IOException e3) {
return newFixedLengthResponse(o.d.INTERNAL_ERROR, "text/plain", "SERVER INTERNAL ERROR: IOException: " + e3.getMessage());
}
}
Map<String, String> parms = mVar.getParms();
parms.put(QUERY_STRING_PARAMETER, mVar.getQueryParameterString());
return serve(mVar.getUri(), method, mVar.getHeaders(), parms, hashMap);
}
public void setAsyncRunner(b bVar) {
this.asyncRunner = bVar;
}
public void setServerSocketFactory(s sVar) {
this.serverSocketFactory = sVar;
}
public void setTempFileManagerFactory(v vVar) {
this.tempFileManagerFactory = vVar;
}
public void start() throws IOException {
start(5000);
}
public void stop() {
try {
safeClose(this.myServerSocket);
this.asyncRunner.closeAll();
Thread thread = this.myThread;
if (thread != null) {
thread.join();
}
} catch (Exception e2) {
LOG.log(Level.SEVERE, "Could not stop all connections", (Throwable) e2);
}
}
protected boolean useGzipWhenAccepted(o oVar) {
return oVar.g() != null && (oVar.g().toLowerCase().contains("text/") || oVar.g().toLowerCase().contains("/json"));
}
public final boolean wasStarted() {
return (this.myServerSocket == null || this.myThread == null) ? false : true;
}
public static final class p extends Exception {
private static final long serialVersionUID = 6569838532917408380L;
private final o.d status;
public p(o.d dVar, String str) {
super(str);
this.status = dVar;
}
public o.d getStatus() {
return this.status;
}
public p(o.d dVar, String str, Exception exc) {
super(str, exc);
this.status = dVar;
}
}
public jb1(String str, int i2) {
this.serverSocketFactory = new h();
this.hostname = str;
this.myPort = i2;
setTempFileManagerFactory(new k());
setAsyncRunner(new g());
}
protected static Map<String, List<String>> decodeParameters(String str) {
HashMap hashMap = new HashMap();
if (str != null) {
StringTokenizer stringTokenizer = new StringTokenizer(str, h1.b);
while (stringTokenizer.hasMoreTokens()) {
String nextToken = stringTokenizer.nextToken();
int indexOf = nextToken.indexOf(61);
String trim = (indexOf >= 0 ? decodePercent(nextToken.substring(0, indexOf)) : decodePercent(nextToken)).trim();
if (!hashMap.containsKey(trim)) {
hashMap.put(trim, new ArrayList());
}
String decodePercent = indexOf >= 0 ? decodePercent(nextToken.substring(indexOf + 1)) : null;
if (decodePercent != null) {
((List) hashMap.get(trim)).add(decodePercent);
}
}
}
return hashMap;
}
public static o newFixedLengthResponse(o.c cVar, String str, String str2) {
byte[] bArr;
d dVar = new d(str);
if (str2 == null) {
return newFixedLengthResponse(cVar, str, new ByteArrayInputStream(new byte[0]), 0L);
}
try {
if (!Charset.forName(dVar.e()).newEncoder().canEncode(str2)) {
dVar = dVar.g();
}
bArr = str2.getBytes(dVar.e());
} catch (UnsupportedEncodingException e2) {
LOG.log(Level.SEVERE, "encoding problem, responding nothing", (Throwable) e2);
bArr = new byte[0];
}
return newFixedLengthResponse(cVar, dVar.c(), new ByteArrayInputStream(bArr), bArr.length);
}
public void start(int i2) throws IOException {
start(i2, true);
}
public void start(int i2, boolean z) throws IOException {
this.myServerSocket = getServerSocketFactory().create();
this.myServerSocket.setReuseAddress(true);
r createServerRunnable = createServerRunnable(i2);
Thread thread = new Thread(createServerRunnable);
this.myThread = thread;
thread.setDaemon(z);
this.myThread.setName("NanoHttpd Main Listener");
this.myThread.start();
while (!createServerRunnable.c && createServerRunnable.b == null) {
try {
Thread.sleep(10L);
} catch (Throwable unused) {
}
}
if (createServerRunnable.b != null) {
throw createServerRunnable.b;
}
}
protected class l implements m {
private static final int a = 512;
private static final int b = 1024;
public static final int c = 8192;
public static final int d = 1024;
private final u e;
private final OutputStream f;
private final BufferedInputStream g;
private int h;
private int i;
private String j;
private n k;
private Map<String, List<String>> l;
private Map<String, String> m;
private f n;
private String o;
private String p;
private String q;
private String r;
public l(u uVar, InputStream inputStream, OutputStream outputStream) {
this.e = uVar;
this.g = new BufferedInputStream(inputStream, 8192);
this.f = outputStream;
}
private void a(BufferedReader bufferedReader, Map<String, String> map, Map<String, List<String>> map2, Map<String, String> map3) throws p {
String decodePercent;
try {
String readLine = bufferedReader.readLine();
if (readLine == null) {
return;
}
StringTokenizer stringTokenizer = new StringTokenizer(readLine);
if (stringTokenizer.hasMoreTokens()) {
map.put("method", stringTokenizer.nextToken());
if (stringTokenizer.hasMoreTokens()) {
String nextToken = stringTokenizer.nextToken();
int indexOf = nextToken.indexOf(63);
if (indexOf >= 0) {
c(nextToken.substring(indexOf + 1), map2);
decodePercent = jb1.decodePercent(nextToken.substring(0, indexOf));
} else {
decodePercent = jb1.decodePercent(nextToken);
}
if (stringTokenizer.hasMoreTokens()) {
this.r = stringTokenizer.nextToken();
} else {
this.r = "HTTP/1.1";
jb1.LOG.log(Level.FINE, "no protocol version specified, strange. Assuming HTTP/1.1.");
}
String readLine2 = bufferedReader.readLine();
while (readLine2 != null && !readLine2.trim().isEmpty()) {
int indexOf2 = readLine2.indexOf(58);
if (indexOf2 >= 0) {
map3.put(readLine2.substring(0, indexOf2).trim().toLowerCase(Locale.US), readLine2.substring(indexOf2 + 1).trim());
}
readLine2 = bufferedReader.readLine();
}
map.put("uri", decodePercent);
return;
}
throw new p(o.d.BAD_REQUEST, "BAD REQUEST: Missing URI. Usage: GET /example/file.html");
}
throw new p(o.d.BAD_REQUEST, "BAD REQUEST: Syntax error. Usage: GET /example/file.html");
} catch (IOException e) {
throw new p(o.d.INTERNAL_ERROR, "SERVER INTERNAL ERROR: IOException: " + e.getMessage(), e);
}
}
private void b(d dVar, ByteBuffer byteBuffer, Map<String, List<String>> map, Map<String, String> map2) throws p {
String str;
try {
int[] f = f(byteBuffer, dVar.a().getBytes());
int i = 2;
if (f.length >= 2) {
int i2 = 1024;
byte[] bArr = new byte[1024];
int i3 = 0;
int i4 = 0;
int i5 = 0;
while (true) {
int i6 = 1;
if (i4 >= f.length - 1) {
return;
}
byteBuffer.position(f[i4]);
int remaining = byteBuffer.remaining() < i2 ? byteBuffer.remaining() : 1024;
byteBuffer.get(bArr, i3, remaining);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(bArr, i3, remaining), Charset.forName(dVar.e())), remaining);
String readLine = bufferedReader.readLine();
if (readLine == null || !readLine.contains(dVar.a())) {
break;
}
String readLine2 = bufferedReader.readLine();
String str2 = null;
String str3 = null;
String str4 = null;
int i7 = 2;
while (readLine2 != null && readLine2.trim().length() > 0) {
Matcher matcher = jb1.CONTENT_DISPOSITION_PATTERN.matcher(readLine2);
if (matcher.matches()) {
Matcher matcher2 = jb1.CONTENT_DISPOSITION_ATTRIBUTE_PATTERN.matcher(matcher.group(i));
while (matcher2.find()) {
String group = matcher2.group(i6);
if ("name".equalsIgnoreCase(group)) {
str = matcher2.group(2);
} else {
if ("filename".equalsIgnoreCase(group)) {
String group2 = matcher2.group(2);
if (!group2.isEmpty()) {
if (i5 > 0) {
str = str2 + String.valueOf(i5);
str3 = group2;
i5++;
} else {
i5++;
}
}
str3 = group2;
}
i6 = 1;
}
str2 = str;
i6 = 1;
}
}
Matcher matcher3 = jb1.CONTENT_TYPE_PATTERN.matcher(readLine2);
if (matcher3.matches()) {
str4 = matcher3.group(2).trim();
}
readLine2 = bufferedReader.readLine();
i7++;
i = 2;
i6 = 1;
}
int i8 = 0;
while (true) {
int i9 = i7 - 1;
if (i7 <= 0) {
break;
}
i8 = i(bArr, i8);
i7 = i9;
}
if (i8 < remaining - 4) {
int i10 = f[i4] + i8;
i4++;
int i11 = f[i4] - 4;
byteBuffer.position(i10);
List<String> list = map.get(str2);
if (list == null) {
list = new ArrayList<>();
map.put(str2, list);
}
if (str4 == null) {
byte[] bArr2 = new byte[i11 - i10];
byteBuffer.get(bArr2);
list.add(new String(bArr2, dVar.e()));
} else {
String h = h(byteBuffer, i10, i11 - i10, str3);
if (!map2.containsKey(str2)) {
map2.put(str2, h);
} else {
int i12 = 2;
while (true) {
if (!map2.containsKey(str2 + i12)) {
break;
} else {
i12++;
}
}
map2.put(str2 + i12, h);
}
list.add(str3);
}
i2 = 1024;
i = 2;
i3 = 0;
} else {
throw new p(o.d.INTERNAL_ERROR, "Multipart header size exceeds MAX_HEADER_SIZE.");
}
}
throw new p(o.d.BAD_REQUEST, "BAD REQUEST: Content type is multipart/form-data but chunk does not start with boundary.");
}
throw new p(o.d.BAD_REQUEST, "BAD REQUEST: Content type is multipart/form-data but contains less than two boundary strings.");
} catch (p e) {
throw e;
} catch (Exception e2) {
throw new p(o.d.INTERNAL_ERROR, e2.toString());
}
}
private void c(String str, Map<String, List<String>> map) {
String trim;
String str2;
if (str == null) {
this.o = "";
return;
}
this.o = str;
StringTokenizer stringTokenizer = new StringTokenizer(str, h1.b);
while (stringTokenizer.hasMoreTokens()) {
String nextToken = stringTokenizer.nextToken();
int indexOf = nextToken.indexOf(61);
if (indexOf >= 0) {
trim = jb1.decodePercent(nextToken.substring(0, indexOf)).trim();
str2 = jb1.decodePercent(nextToken.substring(indexOf + 1));
} else {
trim = jb1.decodePercent(nextToken).trim();
str2 = "";
}
List<String> list = map.get(trim);
if (list == null) {
list = new ArrayList<>();
map.put(trim, list);
}
list.add(str2);
}
}
private int d(byte[] bArr, int i) {
int i2;
int i3 = 0;
while (true) {
int i4 = i3 + 1;
if (i4 >= i) {
return 0;
}
if (bArr[i3] == 13 && bArr[i4] == 10 && (i2 = i3 + 3) < i && bArr[i3 + 2] == 13 && bArr[i2] == 10) {
return i3 + 4;
}
if (bArr[i3] == 10 && bArr[i4] == 10) {
return i3 + 2;
}
i3 = i4;
}
}
private int[] f(ByteBuffer byteBuffer, byte[] bArr) {
int[] iArr = new int[0];
if (byteBuffer.remaining() < bArr.length) {
return iArr;
}
int length = bArr.length + 4096;
byte[] bArr2 = new byte[length];
int remaining = byteBuffer.remaining() < length ? byteBuffer.remaining() : length;
byteBuffer.get(bArr2, 0, remaining);
int length2 = remaining - bArr.length;
int i = 0;
do {
for (int i2 = 0; i2 < length2; i2++) {
for (int i3 = 0; i3 < bArr.length && bArr2[i2 + i3] == bArr[i3]; i3++) {
if (i3 == bArr.length - 1) {
int[] iArr2 = new int[iArr.length + 1];
System.arraycopy(iArr, 0, iArr2, 0, iArr.length);
iArr2[iArr.length] = i + i2;
iArr = iArr2;
}
}
}
i += length2;
System.arraycopy(bArr2, length - bArr.length, bArr2, 0, bArr.length);
length2 = length - bArr.length;
if (byteBuffer.remaining() < length2) {
length2 = byteBuffer.remaining();
}
byteBuffer.get(bArr2, bArr.length, length2);
} while (length2 > 0);
return iArr;
}
private RandomAccessFile g() {
try {
return new RandomAccessFile(this.e.createTempFile(null).getName(), "rw");
} catch (Exception e) {
throw new Error(e);
}
}
private String h(ByteBuffer byteBuffer, int i, int i2, String str) {
t createTempFile;
ByteBuffer duplicate;
FileOutputStream fileOutputStream;
if (i2 <= 0) {
return "";
}
FileOutputStream fileOutputStream2 = null;
try {
try {
createTempFile = this.e.createTempFile(str);
duplicate = byteBuffer.duplicate();
fileOutputStream = new FileOutputStream(createTempFile.getName());
} catch (Exception e) {
e = e;
}
} catch (Throwable th) {
th = th;
}
try {
FileChannel channel = fileOutputStream.getChannel();
duplicate.position(i).limit(i + i2);
channel.write(duplicate.slice());
String name = createTempFile.getName();
jb1.safeClose(fileOutputStream);
return name;
} catch (Exception e2) {
e = e2;
fileOutputStream2 = fileOutputStream;
throw new Error(e);
} catch (Throwable th2) {
th = th2;
fileOutputStream2 = fileOutputStream;
jb1.safeClose(fileOutputStream2);
throw th;
}
}
private int i(byte[] bArr, int i) {
while (bArr[i] != 10) {
i++;
}
return i + 1;
}
public long e() {
if (this.m.containsKey("content-length")) {
return Long.parseLong(this.m.get("content-length"));
}
if (this.h < this.i) {
return r1 - r0;
}
return 0L;
}
@Override
public void execute() throws IOException {
byte[] bArr;
boolean z;
o oVar = null;
try {
try {
try {
try {
try {
bArr = new byte[8192];
z = false;
this.h = 0;
this.i = 0;
this.g.mark(8192);
} catch (p e) {
jb1.newFixedLengthResponse(e.getStatus(), "text/plain", e.getMessage()).t(this.f);
jb1.safeClose(this.f);
}
} catch (IOException e2) {
jb1.newFixedLengthResponse(o.d.INTERNAL_ERROR, "text/plain", "SERVER INTERNAL ERROR: IOException: " + e2.getMessage()).t(this.f);
jb1.safeClose(this.f);
}
} catch (SocketException e3) {
throw e3;
}
} catch (SocketTimeoutException e4) {
throw e4;
} catch (SSLException e5) {
jb1.newFixedLengthResponse(o.d.INTERNAL_ERROR, "text/plain", "SSL PROTOCOL FAILURE: " + e5.getMessage()).t(this.f);
jb1.safeClose(this.f);
}
try {
int read = this.g.read(bArr, 0, 8192);
if (read == -1) {
jb1.safeClose(this.g);
jb1.safeClose(this.f);
throw new SocketException("NanoHttpd Shutdown");
}
while (read > 0) {
int i = this.i + read;
this.i = i;
int d2 = d(bArr, i);
this.h = d2;
if (d2 > 0) {
break;
}
BufferedInputStream bufferedInputStream = this.g;
int i2 = this.i;
read = bufferedInputStream.read(bArr, i2, 8192 - i2);
}
if (this.h < this.i) {
this.g.reset();
this.g.skip(this.h);
}
this.l = new HashMap();
Map<String, String> map = this.m;
if (map == null) {
this.m = new HashMap();
} else {
map.clear();
}
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(bArr, 0, this.i)));
HashMap hashMap = new HashMap();
a(bufferedReader, hashMap, this.l, this.m);
String str = this.p;
if (str != null) {
this.m.put("remote-addr", str);
this.m.put("http-client-ip", this.p);
}
n lookup = n.lookup(hashMap.get("method"));
this.k = lookup;
if (lookup != null) {
this.j = hashMap.get("uri");
this.n = new f(this.m);
String str2 = this.m.get(kb1.HEADER_CONNECTION);
boolean z2 = "HTTP/1.1".equals(this.r) && (str2 == null || !str2.matches("(?i).*close.*"));
oVar = jb1.this.serve(this);
if (oVar != null) {
String str3 = this.m.get("accept-encoding");
this.n.e(oVar);
oVar.D(this.k);
if (jb1.this.useGzipWhenAccepted(oVar) && str3 != null && str3.contains("gzip")) {
z = true;
}
oVar.A(z);
oVar.B(z2);
oVar.t(this.f);
if (!z2 || oVar.r()) {
throw new SocketException("NanoHttpd Shutdown");
}
return;
}
throw new p(o.d.INTERNAL_ERROR, "SERVER INTERNAL ERROR: Serve() returned a null response.");
}
throw new p(o.d.BAD_REQUEST, "BAD REQUEST: Syntax error. HTTP verb " + hashMap.get("method") + " unhandled.");
} catch (SSLException e6) {
throw e6;
} catch (IOException unused) {
jb1.safeClose(this.g);
jb1.safeClose(this.f);
throw new SocketException("NanoHttpd Shutdown");
}
} finally {
jb1.safeClose(null);
this.e.clear();
}
}
@Override
public f getCookies() {
return this.n;
}
@Override
public final Map<String, String> getHeaders() {
return this.m;
}
@Override
public final InputStream getInputStream() {
return this.g;
}
@Override
public final n getMethod() {
return this.k;
}
@Override
public final Map<String, List<String>> getParameters() {
return this.l;
}
@Override
@Deprecated
public final Map<String, String> getParms() {
HashMap hashMap = new HashMap();
for (String str : this.l.keySet()) {
hashMap.put(str, this.l.get(str).get(0));
}
return hashMap;
}
@Override
public String getQueryParameterString() {
return this.o;
}
@Override
public String getRemoteHostName() {
return this.q;
}
@Override
public String getRemoteIpAddress() {
return this.p;
}
@Override
public final String getUri() {
return this.j;
}
@Override
public void parseBody(Map<String, String> map) throws IOException, p {
long e;
RandomAccessFile g;
ByteArrayOutputStream byteArrayOutputStream;
DataOutput dataOutput;
ByteBuffer map2;
RandomAccessFile randomAccessFile = null;
try {
e = e();
if (e < 1024) {
byteArrayOutputStream = new ByteArrayOutputStream();
dataOutput = new DataOutputStream(byteArrayOutputStream);
g = null;
} else {
g = g();
byteArrayOutputStream = null;
dataOutput = g;
}
} catch (Throwable th) {
th = th;
}
try {
byte[] bArr = new byte[512];
while (this.i >= 0 && e > 0) {
int read = this.g.read(bArr, 0, (int) Math.min(e, 512L));
this.i = read;
e -= read;
if (read > 0) {
dataOutput.write(bArr, 0, read);
}
}
if (byteArrayOutputStream != null) {
map2 = ByteBuffer.wrap(byteArrayOutputStream.toByteArray(), 0, byteArrayOutputStream.size());
} else {
map2 = g.getChannel().map(FileChannel.MapMode.READ_ONLY, 0L, g.length());
g.seek(0L);
}
if (n.POST.equals(this.k)) {
d dVar = new d(this.m.get(z0.d));
if (dVar.f()) {
if (dVar.a() != null) {
b(dVar, map2, this.l, map);
} else {
throw new p(o.d.BAD_REQUEST, "BAD REQUEST: Content type is multipart/form-data but boundary missing. Usage: GET /example/file.html");
}
} else {
byte[] bArr2 = new byte[map2.remaining()];
map2.get(bArr2);
String trim = new String(bArr2, dVar.e()).trim();
if (HttpConnection.FORM_URL_ENCODED.equalsIgnoreCase(dVar.b())) {
c(trim, this.l);
} else if (trim.length() != 0) {
map.put("postData", trim);
}
}
} else if (n.PUT.equals(this.k)) {
map.put(rv.d, h(map2, 0, map2.limit(), null));
}
jb1.safeClose(g);
} catch (Throwable th2) {
th = th2;
randomAccessFile = g;
jb1.safeClose(randomAccessFile);
throw th;
}
}
public l(u uVar, InputStream inputStream, OutputStream outputStream, InetAddress inetAddress) {
this.e = uVar;
this.g = new BufferedInputStream(inputStream, 8192);
this.f = outputStream;
this.p = (inetAddress.isLoopbackAddress() || inetAddress.isAnyLocalAddress()) ? "127.0.0.1" : inetAddress.getHostAddress().toString();
this.q = (inetAddress.isLoopbackAddress() || inetAddress.isAnyLocalAddress()) ? f03.o : inetAddress.getHostName().toString();
this.m = new HashMap();
}
}
public static SSLServerSocketFactory makeSSLSocketFactory(KeyStore keyStore, KeyManagerFactory keyManagerFactory) throws IOException {
try {
return makeSSLSocketFactory(keyStore, keyManagerFactory.getKeyManagers());
} catch (Exception e2) {
throw new IOException(e2.getMessage());
}
}
public static SSLServerSocketFactory makeSSLSocketFactory(String str, char[] cArr) throws IOException {
try {
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
InputStream resourceAsStream = jb1.class.getResourceAsStream(str);
if (resourceAsStream != null) {
keyStore.load(resourceAsStream, cArr);
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keyStore, cArr);
return makeSSLSocketFactory(keyStore, keyManagerFactory);
}
throw new IOException("Unable to load keystore from classpath: " + str);
} catch (Exception e2) {
throw new IOException(e2.getMessage());
}
}
public static o newFixedLengthResponse(String str) {
return newFixedLengthResponse(o.d.OK, MIME_HTML, str);
}
@Deprecated
public o serve(String str, n nVar, Map<String, String> map, Map<String, String> map2, Map<String, String> map3) {
return newFixedLengthResponse(o.d.NOT_FOUND, "text/plain", "Not Found");
}
}