📄 trackerwebpageresponseimpl.java
字号:
// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space
// Source File Name: TrackerWebPageResponseImpl.java
package org.gudy.azureus2.pluginsimpl.local.tracker;
import com.aelitis.azureus.core.util.HTTPUtils;
import java.io.*;
import java.net.URL;
import java.util.*;
import java.util.zip.GZIPOutputStream;
import org.gudy.azureus2.core3.config.COConfigurationManager;
import org.gudy.azureus2.core3.torrent.*;
import org.gudy.azureus2.core3.tracker.host.TRHostTorrent;
import org.gudy.azureus2.core3.tracker.util.TRTrackerUtils;
import org.gudy.azureus2.core3.util.*;
import org.gudy.azureus2.plugins.tracker.TrackerTorrent;
import org.gudy.azureus2.plugins.tracker.web.TrackerWebPageRequest;
import org.gudy.azureus2.plugins.tracker.web.TrackerWebPageResponse;
// Referenced classes of package org.gudy.azureus2.pluginsimpl.local.tracker:
// TrackerTorrentImpl
public class TrackerWebPageResponseImpl
implements TrackerWebPageResponse
{
private static final String NL = "\r\n";
private OutputStream os;
private ByteArrayOutputStream baos;
private String content_type;
private int reply_status;
private Map header_map;
private TrackerWebPageRequest request;
private AsyncController async_control;
private boolean is_async;
protected TrackerWebPageResponseImpl(OutputStream _os, TrackerWebPageRequest _request, AsyncController _async_control)
{
baos = new ByteArrayOutputStream(2048);
content_type = "text/html";
reply_status = 200;
header_map = new LinkedHashMap();
os = _os;
request = _request;
async_control = _async_control;
String formatted_date_now = TimeFormatter.getHTTPDate(SystemTime.getCurrentTime());
setHeader("Last-Modified", formatted_date_now);
setHeader("Expires", formatted_date_now);
}
public void setLastModified(long time)
{
String formatted_date = TimeFormatter.getHTTPDate(time);
setHeader("Last-Modified", formatted_date);
}
public void setExpires(long time)
{
String formatted_date = TimeFormatter.getHTTPDate(time);
setHeader("Expires", formatted_date);
}
public void setContentType(String type)
{
content_type = type;
}
public void setReplyStatus(int status)
{
reply_status = status;
}
public void setHeader(String name, String value)
{
addHeader(name, value, true);
}
protected void addHeader(String name, String value, boolean replace)
{
Iterator it = header_map.keySet().iterator();
do
{
if (!it.hasNext())
break;
String key = (String)it.next();
if (key.equalsIgnoreCase(name))
if (replace)
it.remove();
else
return;
} while (true);
header_map.put(name, value);
}
public OutputStream getOutputStream()
{
return baos;
}
protected void complete()
throws IOException
{
if (is_async)
return;
byte reply_bytes[] = baos.toByteArray();
String status_string = "BAD";
if (reply_status == 200)
status_string = "OK";
else
if (reply_status == 204)
status_string = "No Content";
else
if (reply_status == 206)
status_string = "Partial Content";
else
if (reply_status == 401)
status_string = "Unauthorized";
else
if (reply_status == 404)
status_string = "Not Found";
else
if (reply_status == 501)
status_string = "Not Implemented";
String reply_header = (new StringBuilder()).append("HTTP/1.1 ").append(reply_status).append(" ").append(status_string).append("\r\n").toString();
addHeader("Server", "Azureus 4.2.0.0", false);
addHeader("Connection", "close", false);
addHeader("Content-Type", content_type, false);
for (Iterator it = header_map.keySet().iterator(); it.hasNext();)
{
String name = (String)it.next();
String value = (String)header_map.get(name);
reply_header = (new StringBuilder()).append(reply_header).append(name).append(": ").append(value).append("\r\n").toString();
}
reply_header = (new StringBuilder()).append(reply_header).append("Content-Length: ").append(reply_bytes.length).append("\r\n").append("\r\n").toString();
os.write(reply_header.getBytes());
os.flush();
os.write(reply_bytes);
os.flush();
}
public boolean useFile(String root_dir, String relative_url)
throws IOException
{
File canonical_file;
String file_type;
FileInputStream fis;
String target = (new StringBuilder()).append(root_dir).append(relative_url.replace('/', File.separatorChar)).toString();
canonical_file = (new File(target)).getCanonicalFile();
if (!canonical_file.toString().startsWith(root_dir))
return false;
if (canonical_file.isDirectory())
return false;
if (!canonical_file.canRead())
break MISSING_BLOCK_LABEL_163;
String str = canonical_file.toString().toLowerCase();
int pos = str.lastIndexOf(".");
if (pos == -1)
return false;
file_type = str.substring(pos + 1);
fis = null;
boolean flag;
fis = new FileInputStream(canonical_file);
useStream(file_type, fis);
flag = true;
if (fis != null)
fis.close();
return flag;
Exception exception;
exception;
if (fis != null)
fis.close();
throw exception;
return false;
}
public void useStream(String file_type, InputStream input_stream)
throws IOException
{
OutputStream os = getOutputStream();
String response_type = HTTPUtils.guessContentTypeFromFileType(file_type);
if (HTTPUtils.useCompressionForFileType(response_type))
{
Map headers = request.getHeaders();
String accept_encoding = (String)headers.get("accept-encoding");
if (HTTPUtils.canGZIP(accept_encoding))
{
os = new GZIPOutputStream(os);
header_map.put("Content-Encoding", "gzip");
}
}
setContentType(response_type);
byte buffer[] = new byte[4096];
do
{
int len = input_stream.read(buffer);
if (len <= 0)
break;
os.write(buffer, 0, len);
} while (true);
if (os instanceof GZIPOutputStream)
((GZIPOutputStream)os).finish();
}
public void writeTorrent(TrackerTorrent tracker_torrent)
throws IOException
{
try
{
TRHostTorrent host_torrent = ((TrackerTorrentImpl)tracker_torrent).getHostTorrent();
TOTorrent torrent = host_torrent.getTorrent();
TOTorrent torrent_to_send = TOTorrentFactory.deserialiseFromMap(torrent.serialiseToMap());
torrent_to_send.removeAdditionalProperties();
if (!TorrentUtils.isDecentralised(torrent_to_send))
{
URL url_sets[][] = TRTrackerUtils.getAnnounceURLs();
if (host_torrent.getStatus() != 3 && url_sets.length > 0 && COConfigurationManager.getBooleanParameter("Tracker Host Add Our Announce URLs"))
{
String protocol = torrent_to_send.getAnnounceURL().getProtocol();
int i = 0;
do
{
if (i >= url_sets.length)
break;
URL urls[] = url_sets[i];
if (urls[0].getProtocol().equalsIgnoreCase(protocol))
{
torrent_to_send.setAnnounceURL(urls[0]);
torrent_to_send.getAnnounceURLGroup().setAnnounceURLSets(new TOTorrentAnnounceURLSet[0]);
for (int j = 1; j < urls.length; j++)
TorrentUtils.announceGroupsInsertLast(torrent_to_send, new URL[] {
urls[j]
});
break;
}
i++;
} while (true);
}
}
baos.write(BEncoder.encode(torrent_to_send.serialiseToMap()));
setContentType("application/x-bittorrent");
}
catch (TOTorrentException e)
{
Debug.printStackTrace(e);
throw new IOException(e.toString());
}
}
public void setAsynchronous(boolean a)
throws IOException
{
if (async_control == null)
throw new IOException("Request is not non-blocking");
if (a)
{
is_async = true;
async_control.setAsyncStart();
} else
{
is_async = false;
complete();
async_control.setAsyncComplete();
}
}
public boolean getAsynchronous()
{
return is_async;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -