Skip to content

Commit

Permalink
use host and port from request, storing it in the fetch context
Browse files Browse the repository at this point in the history
  • Loading branch information
ArneBab committed Aug 16, 2020
1 parent 1671eb8 commit 9a8cf09
Show file tree
Hide file tree
Showing 42 changed files with 253 additions and 175 deletions.
34 changes: 27 additions & 7 deletions src/freenet/client/FetchContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,24 +114,27 @@ public class FetchContext implements Cloneable, Serializable {
/** Number of attempts before we go into cooldown. Must be less than or equal to
* RequestScheduler.COOLDOWN_RETRIES. */
private int cooldownRetries;
/** Time period for which we go into cooldown. Must be NO LESS THAN
/** Time period for which we go into cooldown. Must be NO LESS THAN
* RequestScheduler.COOLDOWN_PERIOD, because ULPRs will ensure rapid success
* with that interval or less. */
private long cooldownTime;

/** Ignore USK DATEHINTs */
public boolean ignoreUSKDatehints;

public FetchContext(long curMaxLength,
/** host and port (authority) */
public String host;

public FetchContext(long curMaxLength,
long curMaxTempLength, int maxMetadataSize, int maxRecursionLevel, int maxArchiveRestarts, int maxArchiveLevels,
boolean dontEnterImplicitArchives,
int maxSplitfileBlockRetries, int maxNonSplitfileRetries, int maxUSKRetries,
boolean allowSplitfiles, boolean followRedirects, boolean localRequestOnly,
boolean filterData, int maxDataBlocksPerSegment, int maxCheckBlocksPerSegment,
BucketFactory bucketFactory,
ClientEventProducer producer,
boolean ignoreTooManyPathComponents, boolean canWriteClientCache, String charset, String overrideMIME) {
this.blocks = null;
boolean ignoreTooManyPathComponents, boolean canWriteClientCache, String charset, String overrideMIME,
String host) {
this.blocks = null;
this.maxOutputLength = curMaxLength;
if(maxOutputLength < 0) throw new IllegalArgumentException("Bad max output length");
this.maxTempLength = curMaxTempLength;
Expand Down Expand Up @@ -170,8 +173,9 @@ public FetchContext(long curMaxLength,
this.cooldownTime = RequestScheduler.COOLDOWN_PERIOD;
this.ignoreUSKDatehints = false; // FIXME
hasOwnEventProducer = true;
this.host = host;
}

/** Copy a FetchContext, creating a new EventProducer and not changing the blocks list.
* @param ctx The old FetchContext to copy.
* @param maskID Mask mode for the copy operation e.g. SPLITFILE_DEFAULT_BLOCK_MASK.
Expand Down Expand Up @@ -227,6 +231,7 @@ public FetchContext(FetchContext ctx, int maskID, boolean keepProducer, BlockSet
this.cooldownRetries = ctx.cooldownRetries;
this.cooldownTime = ctx.cooldownTime;
this.ignoreUSKDatehints = ctx.ignoreUSKDatehints;
this.host = ctx.host;

if(maskID == IDENTICAL_MASK || maskID == SPLITFILE_DEFAULT_MASK) {
// DEFAULT
Expand Down Expand Up @@ -333,8 +338,12 @@ public void writeTo(DataOutputStream dos) throws IOException {
dos.writeInt(cooldownRetries);
dos.writeLong(cooldownTime);
dos.writeBoolean(ignoreUSKDatehints);
if (host != null)
dos.writeUTF(host);
else
dos.writeUTF("");
}

/** Create from a saved form, e.g. for restarting a request from scratch. Will create its own
* SimpleEventProducer.
* @param dis
Expand Down Expand Up @@ -404,6 +413,11 @@ public FetchContext(DataInputStream dis) throws StorageFormatException, IOExcept
cooldownRetries = dis.readInt();
cooldownTime = dis.readLong();
ignoreUSKDatehints = dis.readBoolean();
s = dis.readUTF();
if(s.equals(""))
host = null;
else
host = s;
hasOwnEventProducer = true;
eventProducer = new SimpleEventProducer();
blocks = null;
Expand Down Expand Up @@ -444,6 +458,7 @@ public int hashCode() {
result = prime * result + ((prefetchHook == null) ? 0 : prefetchHook.hashCode());
result = prime * result + (returnZIPManifests ? 1231 : 1237);
result = prime * result + ((tagReplacer == null) ? 0 : tagReplacer.hashCode());
result = prime * result + ((host == null) ? 0 : host.hashCode());
return result;
}

Expand Down Expand Up @@ -539,6 +554,11 @@ public boolean equals(Object obj) {
return false;
} else if (!tagReplacer.equals(other.tagReplacer))
return false;
if (host == null) {
if (other.host != null)
return false;
} else if (!host.equals(other.host))
return false;
return true;
}

Expand Down
12 changes: 6 additions & 6 deletions src/freenet/client/HighLevelSimpleClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -352,21 +352,21 @@ public FetchContext getFetchContext(long overrideMaxSize) {
MAX_RECURSION, MAX_ARCHIVE_RESTARTS, MAX_ARCHIVE_LEVELS, DONT_ENTER_IMPLICIT_ARCHIVES,
SPLITFILE_BLOCK_RETRIES, NON_SPLITFILE_RETRIES, USK_RETRIES,
FETCH_SPLITFILES, FOLLOW_REDIRECTS, LOCAL_REQUESTS_ONLY,
FILTER_DATA, MAX_SPLITFILE_BLOCKS_PER_SEGMENT, MAX_SPLITFILE_CHECK_BLOCKS_PER_SEGMENT,
FILTER_DATA, MAX_SPLITFILE_BLOCKS_PER_SEGMENT, MAX_SPLITFILE_CHECK_BLOCKS_PER_SEGMENT,
bucketFactory, eventProducer,
false, CAN_WRITE_CLIENT_CACHE, null, null);
false, CAN_WRITE_CLIENT_CACHE, null, null, null);
}
public static FetchContext makeDefaultFetchContext(long maxLength, long maxTempLength,

public static FetchContext makeDefaultFetchContext(long maxLength, long maxTempLength,
BucketFactory bucketFactory, SimpleEventProducer eventProducer) {
return
new FetchContext(maxLength, maxTempLength, 1024*1024,
MAX_RECURSION, MAX_ARCHIVE_RESTARTS, MAX_ARCHIVE_LEVELS, DONT_ENTER_IMPLICIT_ARCHIVES,
SPLITFILE_BLOCK_RETRIES, NON_SPLITFILE_RETRIES, USK_RETRIES,
FETCH_SPLITFILES, FOLLOW_REDIRECTS, LOCAL_REQUESTS_ONLY,
FILTER_DATA, MAX_SPLITFILE_BLOCKS_PER_SEGMENT, MAX_SPLITFILE_CHECK_BLOCKS_PER_SEGMENT,
FILTER_DATA, MAX_SPLITFILE_BLOCKS_PER_SEGMENT, MAX_SPLITFILE_CHECK_BLOCKS_PER_SEGMENT,
bucketFactory, eventProducer,
false, CAN_WRITE_CLIENT_CACHE, null, null);
false, CAN_WRITE_CLIENT_CACHE, null, null, null);
}

@Override
Expand Down
8 changes: 5 additions & 3 deletions src/freenet/client/async/ClientGetWorkerThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
public class ClientGetWorkerThread extends Thread {

private InputStream input;
private String host;
final private URI uri;
final private HashResult[] hashes;
final private boolean filterData;
Expand Down Expand Up @@ -76,17 +77,18 @@ private static synchronized int counter() {
* @param prefetchHook Only needed if filterData is true.
* @param tagReplacer Used for web-pushing. Only needed if filterData is true.
* @param linkFilterExceptionProvider Provider for link filter exceptions
* @throws URISyntaxException
* @throws URISyntaxException
*/
public ClientGetWorkerThread(InputStream input, OutputStream output, FreenetURI uri,
String mimeType, HashResult[] hashes, boolean filterData, String charset,
String mimeType, String host, HashResult[] hashes, boolean filterData, String charset,
FoundURICallback prefetchHook, TagReplacerCallback tagReplacer, LinkFilterExceptionProvider linkFilterExceptionProvider) throws URISyntaxException {
super("ClientGetWorkerThread-"+counter());
this.input = input;
if(uri != null) this.uri = uri.toURI("/");
else this.uri = null;
if(mimeType != null && mimeType.compareTo("application/xhtml+xml") == 0) mimeType = "text/html";
this.mimeType = mimeType;
this.host = host;
this.hashes = hashes;
this.output = output;
this.filterData = filterData;
Expand All @@ -113,7 +115,7 @@ public void run() {
if(logMINOR) Logger.minor(this, "Running content filter... Prefetch hook: "+prefetchHook+" tagReplacer: "+tagReplacer);
if(mimeType == null || uri == null || input == null || output == null) throw new IOException("Insufficient arguements to worker thread");
// Send XHTML as HTML because we can't use web-pushing on XHTML.
FilterStatus filterStatus = ContentFilter.filter(input, output, mimeType, uri, prefetchHook, tagReplacer, charset, linkFilterExceptionProvider);
FilterStatus filterStatus = ContentFilter.filter(input, output, mimeType, uri, host, prefetchHook, tagReplacer, charset, linkFilterExceptionProvider);

String detectedMIMEType = filterStatus.mimeType.concat(filterStatus.charset == null ? "" : "; charset="+filterStatus.charset);
synchronized(this) {
Expand Down
8 changes: 4 additions & 4 deletions src/freenet/client/async/ClientGetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ public void onSuccess(StreamGenerator streamGenerator, ClientMetadata clientMeta

output = finalResult.getOutputStream();
if(ctx.overrideMIME != null) mimeType = ctx.overrideMIME;
worker = new ClientGetWorkerThread(new BufferedInputStream(dataInput), output, uri, mimeType, hashes, ctx.filterData, ctx.charset, ctx.prefetchHook, ctx.tagReplacer, context.linkFilterExceptionProvider);
worker = new ClientGetWorkerThread(new BufferedInputStream(dataInput), output, uri, mimeType, ctx.host, hashes, ctx.filterData, ctx.charset, ctx.prefetchHook, ctx.tagReplacer, context.linkFilterExceptionProvider);
worker.start();
try {
streamGenerator.writeTo(dataOutput, context);
Expand Down Expand Up @@ -903,9 +903,9 @@ public void onExpectedTopSize(long size, long compressed, int blocksReq, int blo
}

@Override
public void onSplitfileCompatibilityMode(final CompatibilityMode min,
final CompatibilityMode max, final byte[] customSplitfileKey,
final boolean dontCompress, final boolean bottomLayer, final boolean definitiveAnyway,
public void onSplitfileCompatibilityMode(final CompatibilityMode min,
final CompatibilityMode max, final byte[] customSplitfileKey,
final boolean dontCompress, final boolean bottomLayer, final boolean definitiveAnyway,
ClientContext context) {
context.getJobRunner(persistent()).queueNormalOrDrop(new PersistentJob() {

Expand Down
4 changes: 2 additions & 2 deletions src/freenet/client/async/SingleFileFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,7 @@ public void onSuccess(StreamGenerator streamGenerator, ClientMetadata clientMeta
pipeOut.connect(pipeIn);
DecompressorThreadManager decompressorManager = new DecompressorThreadManager(pipeIn, decompressors, maxLen);
pipeIn = decompressorManager.execute();
ClientGetWorkerThread worker = new ClientGetWorkerThread(new BufferedInputStream(pipeIn), output, null, null, null, false, null, null, null, context.linkFilterExceptionProvider);
ClientGetWorkerThread worker = new ClientGetWorkerThread(new BufferedInputStream(pipeIn), output, null, null, ctx.host,null, false, null, null, null, context.linkFilterExceptionProvider);
worker.start();
streamGenerator.writeTo(pipeOut, context);
decompressorManager.waitFinished();
Expand Down Expand Up @@ -1066,7 +1066,7 @@ public void onSuccess(StreamGenerator streamGenerator, ClientMetadata clientMeta
pipeIn.connect(pipeOut);
DecompressorThreadManager decompressorManager = new DecompressorThreadManager(pipeIn, decompressors, maxLen);
pipeIn = decompressorManager.execute();
ClientGetWorkerThread worker = new ClientGetWorkerThread(new BufferedInputStream(pipeIn), output, null, null, null, false, null, null, null, context.linkFilterExceptionProvider);
ClientGetWorkerThread worker = new ClientGetWorkerThread(new BufferedInputStream(pipeIn), output, null, null, ctx.host,null, false, null, null, null, context.linkFilterExceptionProvider);
worker.start();
streamGenerator.writeTo(pipeOut, context);
decompressorManager.waitFinished();
Expand Down
2 changes: 1 addition & 1 deletion src/freenet/client/async/USKFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public void onSuccess(StreamGenerator streamGenerator,
pipeOut.connect(pipeIn);
DecompressorThreadManager decompressorManager = new DecompressorThreadManager(pipeIn, decompressors, maxLen);
pipeIn = decompressorManager.execute();
ClientGetWorkerThread worker = new ClientGetWorkerThread(new BufferedInputStream(pipeIn), output, null, null, null, false, null, null, null, context.linkFilterExceptionProvider);
ClientGetWorkerThread worker = new ClientGetWorkerThread(new BufferedInputStream(pipeIn), output, null, null, ctx.host,null, false, null, null, null, context.linkFilterExceptionProvider);
worker.start();
streamGenerator.writeTo(pipeOut, context);
decompressorManager.waitFinished();
Expand Down
2 changes: 1 addition & 1 deletion src/freenet/client/async/USKRetriever.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void onSuccess(StreamGenerator streamGenerator, ClientMetadata clientMeta
pipeOut = new PipedOutputStream(pipeIn);
decompressorManager = new DecompressorThreadManager(pipeIn, decompressors, maxLen);
pipeIn = decompressorManager.execute();
ClientGetWorkerThread worker = new ClientGetWorkerThread(new BufferedInputStream(pipeIn), output, null, null, null, false, null, null, null, context.linkFilterExceptionProvider);
ClientGetWorkerThread worker = new ClientGetWorkerThread(new BufferedInputStream(pipeIn), output, null, null, ctx.host,null, false, null, null, null, context.linkFilterExceptionProvider);
worker.start();
streamGenerator.writeTo(pipeOut, context);
worker.waitFinished();
Expand Down
5 changes: 3 additions & 2 deletions src/freenet/client/filter/BMPFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ public int readShort(DataInputStream dis) throws IOException


@Override
public void readFilter(InputStream input, OutputStream output, String charset, HashMap<String, String> otherParams,
FilterCallback cb) throws DataFilterException, IOException {
public void readFilter(
InputStream input, OutputStream output, String charset, HashMap<String, String> otherParams,
String hostPort, FilterCallback cb) throws DataFilterException, IOException {
DataInputStream dis = new DataInputStream(input);
dis.mark(54);
byte[] StartWord = new byte[2];
Expand Down
5 changes: 3 additions & 2 deletions src/freenet/client/filter/CSSReadFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ public void shouldUpdate(){
}

@Override
public void readFilter(InputStream input, OutputStream output, String charset, HashMap<String, String> otherParams,
FilterCallback cb) throws DataFilterException, IOException {
public void readFilter(
InputStream input, OutputStream output, String charset, HashMap<String, String> otherParams,
String hostPort, FilterCallback cb) throws DataFilterException, IOException {
if (logDEBUG)
Logger.debug(
this,
Expand Down
2 changes: 1 addition & 1 deletion src/freenet/client/filter/CSSTokenizerFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4843,7 +4843,7 @@ public static void main(String arg[]) throws Throwable {
Logger.setupStdoutLogging(Logger.LogLevel.DEBUG, "");

ContentFilter.filter(inputStream, outputStream, "text/css",
new URI("http://127.0.0.1:8888/freenet:USK@ZupQjDFZSc3I4orBpl1iTEAPZKo2733RxCUbZ2Q7iH0,EO8Tuf8SP3lnDjQdAPdCM2ve2RaUEN8m-hod3tQ5oQE,AQACAAE/jFreesite/19/Style/"), null, null, null);
new URI("http://127.0.0.1:8888/freenet:USK@ZupQjDFZSc3I4orBpl1iTEAPZKo2733RxCUbZ2Q7iH0,EO8Tuf8SP3lnDjQdAPdCM2ve2RaUEN8m-hod3tQ5oQE,AQACAAE/jFreesite/19/Style/"), null, null, null, null);
} finally {
Closer.close(inputStream);
Closer.close(outputStream);
Expand Down
14 changes: 8 additions & 6 deletions src/freenet/client/filter/ContentDataFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,20 @@
* Data filter for a specific MIME type.
*/
public interface ContentDataFilter {

/** Filter data for reading. Objective is to ensure the data is safe if
* rendered by e.g. a web browser, and to guarantee that it is of the
* correct type. Filters should usually be implemented as "white list",
* that is, they should parse everything, and when encountering
* that is, they should parse everything, and when encountering
* anything they cannot parse, should delete it, or throw a DataFilterException.
* IMPORTANT Implementation note: The InputStream may be a PipedInputStream
* (or conceivably even a network stream). Implementations MUST NOT ASSUME
* IMPORTANT Implementation note: The InputStream may be a PipedInputStream
* (or conceivably even a network stream). Implementations MUST NOT ASSUME
* that input.available() == 0 => EOF!
* @param input Stream to read potentially unsafe data from.
* @param output Stream to write safe (but possibly incomplete) data to.
* @param charset Character set of the data if appropriate for this MIME type.
* @param otherParams Other type parameters if appropriate.
* @param hostPort
* @param cb Filter callback for modifying HTML tags. Irrelevant for most MIME types. In future we
* might need this for other types.
* @throws DataFilterException If the data cannot be filtered. Any data
Expand All @@ -34,6 +35,7 @@ public interface ContentDataFilter {
* if data is merely badly formatted - any such exceptions should be
* caught and converted to a DataFilterException.
*/
public void readFilter(InputStream input, OutputStream output, String charset, HashMap<String, String> otherParams,
FilterCallback cb) throws DataFilterException, IOException;
public void readFilter(
InputStream input, OutputStream output, String charset, HashMap<String, String> otherParams,
String hostPort, FilterCallback cb) throws DataFilterException, IOException;
}
33 changes: 27 additions & 6 deletions src/freenet/client/filter/ContentFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,12 @@ public static FilterMIMEType getMIMEType(String mimeType) {
* Stream to write filtered data to
* @param typeName
* MIME type for input data
* @param host
* HOST and PORT from the request
* @param maybeCharset
* MIME type of the referring document, as a hint, some types,
* such as CSS, will inherit it if no other data is available.
* @param linkFilterExceptionProvider
* @return
* @throws IOException
* If an internal error involving s occurred.
Expand All @@ -200,8 +203,16 @@ public static FilterMIMEType getMIMEType(String mimeType) {
* @throws IllegalStateException
* If data is invalid (e.g. corrupted file) and the filter have no way to recover.
*/
public static FilterStatus filter(InputStream input, OutputStream output, String typeName, URI baseURI, FoundURICallback cb, TagReplacerCallback trc , String maybeCharset) throws UnsafeContentTypeException, IOException {
return filter(input, output, typeName, baseURI, cb, trc, maybeCharset, null);
public static FilterStatus filter(
InputStream input,
OutputStream output,
String typeName,
URI baseURI,
String host,
FoundURICallback cb,
TagReplacerCallback trc,
String maybeCharset) throws UnsafeContentTypeException, IOException {
return filter(input, output, typeName, baseURI, host, cb, trc, maybeCharset, null);
}

/**
Expand All @@ -224,8 +235,17 @@ public static FilterStatus filter(InputStream input, OutputStream output, String
* @throws IllegalStateException
* If data is invalid (e.g. corrupted file) and the filter have no way to recover.
*/
public static FilterStatus filter(InputStream input, OutputStream output, String typeName, URI baseURI, FoundURICallback cb, TagReplacerCallback trc , String maybeCharset, LinkFilterExceptionProvider linkFilterExceptionProvider) throws UnsafeContentTypeException, IOException {
return filter(input, output, typeName, maybeCharset, new GenericReadFilterCallback(baseURI, cb,trc, linkFilterExceptionProvider));
public static FilterStatus filter(
InputStream input,
OutputStream output,
String typeName,
URI baseURI,
String host,
FoundURICallback cb,
TagReplacerCallback trc,
String maybeCharset,
LinkFilterExceptionProvider linkFilterExceptionProvider) throws UnsafeContentTypeException, IOException {
return filter(input, output, typeName, maybeCharset, host, new GenericReadFilterCallback(baseURI, cb, trc, linkFilterExceptionProvider));
}

/**
Expand All @@ -247,11 +267,12 @@ public static FilterStatus filter(InputStream input, OutputStream output, String
* @throws IllegalStateException
* If data is invalid (e.g. corrupted file) and the filter have no way to recover.
*/
public static FilterStatus filter(InputStream input, OutputStream output, String typeName, String maybeCharset, FilterCallback filterCallback) throws UnsafeContentTypeException, IOException {
public static FilterStatus filter(InputStream input, OutputStream output, String typeName, String maybeCharset, String hostPort, FilterCallback filterCallback) throws UnsafeContentTypeException, IOException {
if(logMINOR) Logger.minor(ContentFilter.class, "Filtering data of type"+typeName);
String type = typeName;
String options = "";
String charset = null;
// mimeType params
HashMap<String, String> otherParams = null;
input = new BufferedInputStream(input);

Expand Down Expand Up @@ -306,7 +327,7 @@ public static FilterStatus filter(InputStream input, OutputStream output, String
charset = detectCharset(charsetBuffer, offset, handler, maybeCharset);
}
try {
handler.readFilter.readFilter(input, output, charset, otherParams, filterCallback);
handler.readFilter.readFilter(input, output, charset, otherParams, hostPort, filterCallback);
}
catch(EOFException e) {
Logger.error(ContentFilter.class, "EOFException caught: "+e,e);
Expand Down
Loading

0 comments on commit 9a8cf09

Please sign in to comment.