Http.abort: leave responses that were read to the end alone
Their connection is back in the pool and may be carrying another request by then (in Plexamp Lite this cut read-ahead downloads short). From Plexamp Lite 1.14.1. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3fa210fc60
commit
bdf8de2767
1 file changed
+32
-16
+32
-16
@@ -451,19 +451,25 @@ public final class Http {
|
|||||||
// ------------------------------------------------------------------ body streams
|
// ------------------------------------------------------------------ body streams
|
||||||
|
|
||||||
private static final class FixedStream extends InputStream {
|
private static final class FixedStream extends InputStream {
|
||||||
private final Conn c; private long left; private final boolean keepAlive; private boolean closed;
|
private final Conn c; private long left; private final boolean keepAlive; private volatile boolean closed;
|
||||||
|
|
||||||
FixedStream(Conn c, long len, boolean keepAlive) {
|
FixedStream(Conn c, long len, boolean keepAlive) {
|
||||||
this.c = c; this.left = len; this.keepAlive = keepAlive;
|
this.c = c; this.left = len; this.keepAlive = keepAlive;
|
||||||
if (len == 0) finish(true);
|
if (len == 0) finish(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void finish(boolean complete) {
|
private synchronized void finish(boolean complete) {
|
||||||
if (closed) return;
|
if (closed) return;
|
||||||
closed = true;
|
closed = true;
|
||||||
if (complete && keepAlive) release(c); else c.close();
|
if (complete && keepAlive) release(c); else c.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
synchronized void abort() {
|
||||||
|
if (closed) return; // done: the connection may already be back in the pool, serving someone else
|
||||||
|
closed = true;
|
||||||
|
try { c.socket.close(); } catch (Exception ignored) {}
|
||||||
|
}
|
||||||
|
|
||||||
@Override public int read() throws IOException {
|
@Override public int read() throws IOException {
|
||||||
byte[] one = new byte[1];
|
byte[] one = new byte[1];
|
||||||
int n = read(one, 0, 1);
|
int n = read(one, 0, 1);
|
||||||
@@ -490,16 +496,22 @@ public final class Http {
|
|||||||
|
|
||||||
private static final class ChunkedStream extends InputStream {
|
private static final class ChunkedStream extends InputStream {
|
||||||
private final Conn c; private final boolean keepAlive;
|
private final Conn c; private final boolean keepAlive;
|
||||||
private long chunkLeft = 0; private boolean eof; private boolean closed;
|
private long chunkLeft = 0; private boolean eof; private volatile boolean closed;
|
||||||
|
|
||||||
ChunkedStream(Conn c, boolean keepAlive) { this.c = c; this.keepAlive = keepAlive; }
|
ChunkedStream(Conn c, boolean keepAlive) { this.c = c; this.keepAlive = keepAlive; }
|
||||||
|
|
||||||
private void finish(boolean complete) {
|
private synchronized void finish(boolean complete) {
|
||||||
if (closed) return;
|
if (closed) return;
|
||||||
closed = true;
|
closed = true;
|
||||||
if (complete && keepAlive) release(c); else c.close();
|
if (complete && keepAlive) release(c); else c.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
synchronized void abort() {
|
||||||
|
if (closed) return; // done: the connection may already be back in the pool, serving someone else
|
||||||
|
closed = true;
|
||||||
|
try { c.socket.close(); } catch (Exception ignored) {}
|
||||||
|
}
|
||||||
|
|
||||||
private boolean nextChunk() throws IOException {
|
private boolean nextChunk() throws IOException {
|
||||||
if (eof) return false;
|
if (eof) return false;
|
||||||
String line = readLine(c.in);
|
String line = readLine(c.in);
|
||||||
@@ -551,7 +563,7 @@ public final class Http {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static final class EofStream extends InputStream {
|
private static final class EofStream extends InputStream {
|
||||||
private final Conn c; private boolean closed;
|
private final Conn c; private volatile boolean closed;
|
||||||
EofStream(Conn c) { this.c = c; }
|
EofStream(Conn c) { this.c = c; }
|
||||||
|
|
||||||
@Override public int read() throws IOException {
|
@Override public int read() throws IOException {
|
||||||
@@ -568,21 +580,25 @@ public final class Http {
|
|||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public void close() {
|
@Override public synchronized void close() {
|
||||||
if (!closed) { closed = true; c.close(); }
|
if (!closed) { closed = true; c.close(); }
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/** Close one in-flight response's socket from another thread (used to abort streams). */
|
synchronized void abort() {
|
||||||
public static void abort(Response r) {
|
if (closed) return;
|
||||||
if (r == null) return;
|
closed = true;
|
||||||
InputStream b = r.body;
|
|
||||||
Conn c = null;
|
|
||||||
if (b instanceof FixedStream) c = ((FixedStream) b).c;
|
|
||||||
else if (b instanceof ChunkedStream) c = ((ChunkedStream) b).c;
|
|
||||||
else if (b instanceof EofStream) c = ((EofStream) b).c;
|
|
||||||
if (c != null) {
|
|
||||||
try { c.socket.close(); } catch (Exception ignored) {}
|
try { c.socket.close(); } catch (Exception ignored) {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Close one in-flight response's socket from another thread (used to abort streams). A response that has
|
||||||
|
* already been read to the end is left alone: its connection went back to the pool and may be carrying
|
||||||
|
* another request by now (closing it cut read-ahead downloads short: "Unexpected end of stream"). */
|
||||||
|
public static void abort(Response r) {
|
||||||
|
if (r == null) return;
|
||||||
|
InputStream b = r.body;
|
||||||
|
if (b instanceof FixedStream) ((FixedStream) b).abort();
|
||||||
|
else if (b instanceof ChunkedStream) ((ChunkedStream) b).abort();
|
||||||
|
else if (b instanceof EofStream) ((EofStream) b).abort();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user