|
|
@ -387,13 +387,11 @@ public class httpdProxyHandler extends httpdAbstractHandler implements httpdHand
|
|
|
|
// in two of these cases we trigger a scheduler to handle newly arrived files:
|
|
|
|
// in two of these cases we trigger a scheduler to handle newly arrived files:
|
|
|
|
// case 1 and case 3
|
|
|
|
// case 1 and case 3
|
|
|
|
plasmaHTCache.Entry hpc;
|
|
|
|
plasmaHTCache.Entry hpc;
|
|
|
|
if (cacheExists) {
|
|
|
|
if ((cacheExists) &&
|
|
|
|
// we respond on the request by using the cache
|
|
|
|
((hpc = cacheManager.newEntry(requestDate, 0, url, requestHeader, "200 OK",
|
|
|
|
|
|
|
|
cachedResponseHeader, null,
|
|
|
|
hpc = cacheManager.newEntry(requestDate, 0, url, requestHeader, "200 OK", cachedResponseHeader, null, switchboard.defaultProxyProfile);
|
|
|
|
switchboard.defaultProxyProfile)).shallUseCache())) {
|
|
|
|
|
|
|
|
// we respond on the request by using the cache, the cache is fresh
|
|
|
|
if (hpc.shallUseCache()) {
|
|
|
|
|
|
|
|
// the cache is fresh
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
// replace date field in old header by actual date, this is according to RFC
|
|
|
|
// replace date field in old header by actual date, this is according to RFC
|
|
|
@ -443,12 +441,17 @@ public class httpdProxyHandler extends httpdAbstractHandler implements httpdHand
|
|
|
|
// we do nothing here
|
|
|
|
// we do nothing here
|
|
|
|
respondError(respond, "111 socket error: " + e.getMessage(), 1, url.toString());
|
|
|
|
respondError(respond, "111 socket error: " + e.getMessage(), 1, url.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
respond.flush();
|
|
|
|
// the cache is (supposed to be) stale
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// the cache does either not exist or is (supposed to be) stale
|
|
|
|
|
|
|
|
long sizeBeforeDelete = -1;
|
|
|
|
|
|
|
|
if (cacheExists) {
|
|
|
|
// delete the cache
|
|
|
|
// delete the cache
|
|
|
|
long sizeBeforeDelete = cacheFile.length();
|
|
|
|
sizeBeforeDelete = cacheFile.length();
|
|
|
|
cacheFile.delete();
|
|
|
|
cacheFile.delete();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// take a new file from the server
|
|
|
|
// take a new file from the server
|
|
|
|
httpc remote = null;
|
|
|
|
httpc remote = null;
|
|
|
@ -470,12 +473,14 @@ public class httpdProxyHandler extends httpdAbstractHandler implements httpdHand
|
|
|
|
// reserver cache entry
|
|
|
|
// reserver cache entry
|
|
|
|
hpc = cacheManager.newEntry(requestDate, 0, url, requestHeader, res.status, res.responseHeader, null, switchboard.defaultProxyProfile);
|
|
|
|
hpc = cacheManager.newEntry(requestDate, 0, url, requestHeader, res.status, res.responseHeader, null, switchboard.defaultProxyProfile);
|
|
|
|
|
|
|
|
|
|
|
|
// make a scraper and transformer
|
|
|
|
// handle file types
|
|
|
|
if (((ext == null) || (!(switchboard.extensionBlack.contains(ext)))) &&
|
|
|
|
if (((ext == null) || (!(switchboard.extensionBlack.contains(ext)))) &&
|
|
|
|
(httpd.isTextMime(res.responseHeader.mime(), switchboard.mimeWhite))) {
|
|
|
|
(httpd.isTextMime(res.responseHeader.mime(), switchboard.mimeWhite))) {
|
|
|
|
if (transformer.isIdentityTransformer()) {
|
|
|
|
if (transformer.isIdentityTransformer()) {
|
|
|
|
hfos = hpc.getContentOutputStream();
|
|
|
|
// no transformation, only passthrough
|
|
|
|
|
|
|
|
hfos = respond;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// make a scraper and transformer
|
|
|
|
scraper = new htmlFilterContentScraper(url);
|
|
|
|
scraper = new htmlFilterContentScraper(url);
|
|
|
|
hfos = new htmlFilterOutputStream(respond, scraper, transformer, (ext.length() == 0));
|
|
|
|
hfos = new htmlFilterOutputStream(respond, scraper, transformer, (ext.length() == 0));
|
|
|
|
if (((htmlFilterOutputStream) hfos).binarySuspect()) {
|
|
|
|
if (((htmlFilterOutputStream) hfos).binarySuspect()) {
|
|
|
@ -501,42 +506,41 @@ public class httpdProxyHandler extends httpdAbstractHandler implements httpdHand
|
|
|
|
if ((storeError = hpc.shallStoreCache()) == null) {
|
|
|
|
if ((storeError = hpc.shallStoreCache()) == null) {
|
|
|
|
// we write a new cache entry
|
|
|
|
// we write a new cache entry
|
|
|
|
if ((contentLength > 0) && // known
|
|
|
|
if ((contentLength > 0) && // known
|
|
|
|
(contentLength < 1048576)) // 1 MB
|
|
|
|
(contentLength < 1048576)) {// 1 MB
|
|
|
|
{
|
|
|
|
// ok, we don't write actually into a file, only to RAM, and schedule writing the file.
|
|
|
|
byte[] cacheArray;
|
|
|
|
byte[] cacheArray;
|
|
|
|
if (transformer.isIdentityTransformer()) {
|
|
|
|
|
|
|
|
res.writeContentX(hfos, respond);
|
|
|
|
|
|
|
|
cacheArray = hpc.getContentBytes();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
cacheArray = res.writeContent(hfos);
|
|
|
|
cacheArray = res.writeContent(hfos);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hfos instanceof htmlFilterOutputStream) ((htmlFilterOutputStream) hfos).finalize();
|
|
|
|
if (hfos instanceof htmlFilterOutputStream) ((htmlFilterOutputStream) hfos).finalize();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (sizeBeforeDelete == -1) {
|
|
|
|
|
|
|
|
// totally fresh file
|
|
|
|
|
|
|
|
hpc.status = plasmaHTCache.CACHE_FILL; // it's an insert
|
|
|
|
|
|
|
|
cacheManager.stackProcess(hpc, cacheArray);
|
|
|
|
|
|
|
|
} else if (sizeBeforeDelete == cacheArray.length) {
|
|
|
|
// before we came here we deleted a cache entry
|
|
|
|
// before we came here we deleted a cache entry
|
|
|
|
if (sizeBeforeDelete == cacheArray.length) {
|
|
|
|
|
|
|
|
cacheArray = null;
|
|
|
|
cacheArray = null;
|
|
|
|
hpc.status = plasmaHTCache.CACHE_STALE_RELOAD_BAD;
|
|
|
|
hpc.status = plasmaHTCache.CACHE_STALE_RELOAD_BAD;
|
|
|
|
cacheManager.stackProcess(hpc); // unnecessary update
|
|
|
|
cacheManager.stackProcess(hpc); // unnecessary update
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// before we came here we deleted a cache entry
|
|
|
|
hpc.status = plasmaHTCache.CACHE_STALE_RELOAD_GOOD;
|
|
|
|
hpc.status = plasmaHTCache.CACHE_STALE_RELOAD_GOOD;
|
|
|
|
cacheManager.stackProcess(hpc, cacheArray); // necessary update, write response header to cache
|
|
|
|
cacheManager.stackProcess(hpc, cacheArray); // necessary update, write response header to cache
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
// the file is too big to cache it in the ram, write to file
|
|
|
|
// the file is too big to cache it in the ram, write to file right here
|
|
|
|
cacheFile.getParentFile().mkdirs();
|
|
|
|
cacheFile.getParentFile().mkdirs();
|
|
|
|
if (transformer.isIdentityTransformer()) {
|
|
|
|
|
|
|
|
res.writeContent(respond, cacheFile);
|
|
|
|
|
|
|
|
if (contentLength < 10485760) { // 10 mb
|
|
|
|
|
|
|
|
serverFileUtils.copy(cacheFile, hfos);
|
|
|
|
|
|
|
|
} // else hfos is empty and that means: no work afterwards with it
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
res.writeContent(hfos, cacheFile);
|
|
|
|
res.writeContent(hfos, cacheFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hfos instanceof htmlFilterOutputStream) ((htmlFilterOutputStream) hfos).finalize();
|
|
|
|
if (hfos instanceof htmlFilterOutputStream) ((htmlFilterOutputStream) hfos).finalize();
|
|
|
|
|
|
|
|
if (sizeBeforeDelete == -1) {
|
|
|
|
|
|
|
|
// totally fresh file
|
|
|
|
|
|
|
|
hpc.status = plasmaHTCache.CACHE_FILL; // it's an insert
|
|
|
|
|
|
|
|
cacheManager.stackProcess(hpc);
|
|
|
|
|
|
|
|
} else if (sizeBeforeDelete == cacheFile.length()) {
|
|
|
|
// before we came here we deleted a cache entry
|
|
|
|
// before we came here we deleted a cache entry
|
|
|
|
if (sizeBeforeDelete == cacheFile.length()) {
|
|
|
|
|
|
|
|
hpc.status = plasmaHTCache.CACHE_STALE_RELOAD_BAD;
|
|
|
|
hpc.status = plasmaHTCache.CACHE_STALE_RELOAD_BAD;
|
|
|
|
cacheManager.stackProcess(hpc); // unnecessary update
|
|
|
|
cacheManager.stackProcess(hpc); // unnecessary update
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// before we came here we deleted a cache entry
|
|
|
|
hpc.status = plasmaHTCache.CACHE_STALE_RELOAD_GOOD;
|
|
|
|
hpc.status = plasmaHTCache.CACHE_STALE_RELOAD_GOOD;
|
|
|
|
cacheManager.stackProcess(hpc); // necessary update, write response header to cache
|
|
|
|
cacheManager.stackProcess(hpc); // necessary update, write response header to cache
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -546,10 +550,16 @@ public class httpdProxyHandler extends httpdAbstractHandler implements httpdHand
|
|
|
|
log.logDebug(cacheFile.toString() + " not cached: " + storeError);
|
|
|
|
log.logDebug(cacheFile.toString() + " not cached: " + storeError);
|
|
|
|
res.writeContent(hfos, null);
|
|
|
|
res.writeContent(hfos, null);
|
|
|
|
if (hfos instanceof htmlFilterOutputStream) ((htmlFilterOutputStream) hfos).finalize();
|
|
|
|
if (hfos instanceof htmlFilterOutputStream) ((htmlFilterOutputStream) hfos).finalize();
|
|
|
|
|
|
|
|
if (sizeBeforeDelete == -1) {
|
|
|
|
|
|
|
|
// no old file and no load. just data passing
|
|
|
|
|
|
|
|
hpc.status = plasmaHTCache.CACHE_PASSING;
|
|
|
|
|
|
|
|
cacheManager.stackProcess(hpc);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// before we came here we deleted a cache entry
|
|
|
|
// before we came here we deleted a cache entry
|
|
|
|
hpc.status = plasmaHTCache.CACHE_STALE_NO_RELOAD;
|
|
|
|
hpc.status = plasmaHTCache.CACHE_STALE_NO_RELOAD;
|
|
|
|
cacheManager.stackProcess(hpc);
|
|
|
|
cacheManager.stackProcess(hpc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
} catch (SocketException e) {
|
|
|
|
} catch (SocketException e) {
|
|
|
|
// this may happen if the client suddenly closes its connection
|
|
|
|
// this may happen if the client suddenly closes its connection
|
|
|
|
// maybe the user has stopped loading
|
|
|
|
// maybe the user has stopped loading
|
|
|
@ -557,6 +567,7 @@ public class httpdProxyHandler extends httpdAbstractHandler implements httpdHand
|
|
|
|
// but we clean the cache also, since it may be only partial
|
|
|
|
// but we clean the cache also, since it may be only partial
|
|
|
|
// and most possible corrupted
|
|
|
|
// and most possible corrupted
|
|
|
|
if (cacheFile.exists()) cacheFile.delete();
|
|
|
|
if (cacheFile.exists()) cacheFile.delete();
|
|
|
|
|
|
|
|
respondHeader(respond,"404 client unexpectedly closed connection", new httpHeader(null));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
remote.close();
|
|
|
|
remote.close();
|
|
|
|
} catch (Exception e) {
|
|
|
|
} catch (Exception e) {
|
|
|
@ -576,138 +587,6 @@ public class httpdProxyHandler extends httpdAbstractHandler implements httpdHand
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception ee) {}
|
|
|
|
} catch (Exception ee) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// we take a new file from the net and respond with that
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
// open the connection
|
|
|
|
|
|
|
|
//httpc remote = newhttpc(host, port, timeout);
|
|
|
|
|
|
|
|
httpc remote;
|
|
|
|
|
|
|
|
if (yAddress == null) {
|
|
|
|
|
|
|
|
remote = newhttpc(host, port, timeout);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
remote = newhttpc(yAddress, timeout);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//System.out.println("HEADER: CLIENT TO PROXY = " + requestHeader.toString()); // DEBUG
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// send request
|
|
|
|
|
|
|
|
httpc.response res = remote.GET(remotePath, requestHeader);
|
|
|
|
|
|
|
|
long contentLength = res.responseHeader.contentLength();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// reserve cache entry
|
|
|
|
|
|
|
|
hpc = cacheManager.newEntry(requestDate, 0, url, requestHeader, res.status, res.responseHeader, null, switchboard.defaultProxyProfile);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// make a scraper and transformer
|
|
|
|
|
|
|
|
if (((ext == null) || (!(switchboard.extensionBlack.contains(ext)))) &&
|
|
|
|
|
|
|
|
(httpd.isTextMime(res.responseHeader.mime(), switchboard.mimeWhite))) {
|
|
|
|
|
|
|
|
if (transformer.isIdentityTransformer()) {
|
|
|
|
|
|
|
|
hfos = hpc.getContentOutputStream();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
scraper = new htmlFilterContentScraper(url);
|
|
|
|
|
|
|
|
hfos = new htmlFilterOutputStream(respond, scraper, transformer, (ext.length() == 0));
|
|
|
|
|
|
|
|
if (((htmlFilterOutputStream) hfos).binarySuspect()) {
|
|
|
|
|
|
|
|
scraper = null; // forget it, may be rubbish
|
|
|
|
|
|
|
|
log.logDebug("Content of " + url + " is probably binary. deleted scraper.");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
hpc.scraper = scraper;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
log.logDebug("Resource " + url + " has wrong extension (" + ext + ") or wrong mime-type (" + res.responseHeader.mime() + "). not scraped");
|
|
|
|
|
|
|
|
scraper = null;
|
|
|
|
|
|
|
|
hfos = respond;
|
|
|
|
|
|
|
|
hpc.scraper = scraper;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// handle incoming cookies
|
|
|
|
|
|
|
|
handleIncomingCookies(res.responseHeader, host, ip);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// request has been placed and result has been returned. work off response
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
//System.out.println("HEADER: SERVER TO PROXY = [" + res.status + "] " + ((httpHeader) res.responseHeader).toString()); // DEBUG
|
|
|
|
|
|
|
|
respondHeader(respond, res.status, res.responseHeader);
|
|
|
|
|
|
|
|
String storeError;
|
|
|
|
|
|
|
|
if ((storeError = hpc.shallStoreCache()) == null) {
|
|
|
|
|
|
|
|
// we write a new cache entry
|
|
|
|
|
|
|
|
if ((contentLength > 0) && (contentLength < 1048576)) {
|
|
|
|
|
|
|
|
// write to buffer
|
|
|
|
|
|
|
|
byte[] cacheArray;
|
|
|
|
|
|
|
|
if (transformer.isIdentityTransformer()) {
|
|
|
|
|
|
|
|
res.writeContentX(hfos, respond);
|
|
|
|
|
|
|
|
cacheArray = hpc.getContentBytes();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
cacheArray = res.writeContent(hfos);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hfos instanceof htmlFilterOutputStream) ((htmlFilterOutputStream) hfos).finalize();
|
|
|
|
|
|
|
|
// enQueue new entry with response header and file as byte[]
|
|
|
|
|
|
|
|
hpc.status = plasmaHTCache.CACHE_FILL;
|
|
|
|
|
|
|
|
cacheManager.stackProcess(hpc, cacheArray);
|
|
|
|
|
|
|
|
} else try {
|
|
|
|
|
|
|
|
// write to file system directly
|
|
|
|
|
|
|
|
cacheFile.getParentFile().mkdirs();
|
|
|
|
|
|
|
|
if (transformer.isIdentityTransformer()) {
|
|
|
|
|
|
|
|
res.writeContent(respond, cacheFile);
|
|
|
|
|
|
|
|
if (contentLength < 10485760) { // 10 mb
|
|
|
|
|
|
|
|
serverFileUtils.copy(cacheFile, hfos);
|
|
|
|
|
|
|
|
} // else hfos is empty and that means: no work afterwards with it
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
res.writeContent(hfos, cacheFile);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hfos instanceof htmlFilterOutputStream) ((htmlFilterOutputStream) hfos).finalize();
|
|
|
|
|
|
|
|
// enQueue new entry with response header
|
|
|
|
|
|
|
|
hpc.status = plasmaHTCache.CACHE_FILL;
|
|
|
|
|
|
|
|
cacheManager.stackProcess(hpc);
|
|
|
|
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
|
|
|
|
// this may happen if there are no write rights whatsoever
|
|
|
|
|
|
|
|
// (do nothing)
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
Exception occurred:
|
|
|
|
|
|
|
|
java.io.FileNotFoundException:
|
|
|
|
|
|
|
|
/opt/yacy_pre_v0.314_20041219/DATA/HTCACHE/www.spiegel.de/fotostrecke/0,5538,PB64-SUQ9NDYwNyZucj0z,00.html
|
|
|
|
|
|
|
|
(Permission denied)
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// no caching
|
|
|
|
|
|
|
|
//System.out.println("DEBUG: " + res.status + " " + cacheFile.toString()); // debug
|
|
|
|
|
|
|
|
log.logDebug(cacheFile.toString() + " not cached: " + storeError);
|
|
|
|
|
|
|
|
res.writeContent(hfos, null);
|
|
|
|
|
|
|
|
if (hfos instanceof htmlFilterOutputStream) ((htmlFilterOutputStream) hfos).finalize();
|
|
|
|
|
|
|
|
// no old file and no load. just data passing
|
|
|
|
|
|
|
|
hpc.status = plasmaHTCache.CACHE_PASSING;
|
|
|
|
|
|
|
|
cacheManager.stackProcess(hpc);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (SocketException e) {
|
|
|
|
|
|
|
|
// this may happen if the client suddenly closes its connection
|
|
|
|
|
|
|
|
// maybe the user has stopped loading
|
|
|
|
|
|
|
|
// in that case, we are not responsible and just forget it
|
|
|
|
|
|
|
|
// but we clean the cache also, since it may be only partial
|
|
|
|
|
|
|
|
// and most possible corrupted
|
|
|
|
|
|
|
|
if (cacheFile.exists()) cacheFile.delete();
|
|
|
|
|
|
|
|
respondHeader(respond,"404 client unexpectedly closed connection", new httpHeader(null));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
remote.close();
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
// this may happen if the targeted host does not exist or anything with the
|
|
|
|
|
|
|
|
// remote server was wrong.
|
|
|
|
|
|
|
|
// in any case, sending a 404 is appropriate
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
if ((e.toString().indexOf("unknown host")) > 0) {
|
|
|
|
|
|
|
|
respondHeader(respond,"404 unknown host", new httpHeader(null));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
respondHeader(respond,"404 resource not available (generic exception: " + e.toString() + ")", new httpHeader(null));
|
|
|
|
|
|
|
|
//respond.write(("Exception occurred:\r\n").getBytes());
|
|
|
|
|
|
|
|
//respond.write((e.toString() + "\r\n").getBytes());
|
|
|
|
|
|
|
|
//respond.write(("[TRACE: ").getBytes());
|
|
|
|
|
|
|
|
//e.printStackTrace(new PrintStream(respond));
|
|
|
|
|
|
|
|
//respond.write(("]\r\n").getBytes());
|
|
|
|
|
|
|
|
/* http://www.geocrawler.com/archives/3/201/1999/8/50/2505805/
|
|
|
|
|
|
|
|
> java.net.ConnectException: Connection refused
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception ee) {}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
respond.flush();
|
|
|
|
respond.flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|