Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Shutdown hijacked stdin #1567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
Loading
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.dockerjava.httpclient5;

import org.apache.hc.client5.http.io.ManagedHttpClientConnection;
import org.apache.hc.core5.http.ClassicHttpRequest;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.ConnectionReuseStrategy;
Expand Down Expand Up @@ -80,6 +81,9 @@ private ClassicHttpResponse executeHijacked(
fakeRequest.setHeader(HttpHeaders.CONTENT_LENGTH, Long.MAX_VALUE);
fakeRequest.setEntity(new HijackedEntity(hijackedInput));
conn.sendRequestEntity(fakeRequest);
if (conn instanceof ManagedHttpClientConnection) {
((ManagedHttpClientConnection) conn).getSocket().shutdownOutput();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,32 @@ public OutputStream getOutputStream() {
return this.outputStream;
}

@Override
public void shutdownInput() throws IOException {
try (Handle handle = this.fileDescriptor.acquire()) {
if (!handle.isClosed()) {
try {
shutdown(handle.intValue(), SHUT_RD);
} catch (LastErrorException ex) {
throw new IOException(ex);
}
}
}
}

@Override
public void shutdownOutput() throws IOException {
try (Handle handle = this.fileDescriptor.acquire()) {
if (!handle.isClosed()) {
try {
shutdown(handle.intValue(), SHUT_WR);
} catch (LastErrorException ex) {
throw new IOException(ex);
}
}
}
}

@Override
public void close() throws IOException {
super.close();
Expand All @@ -123,6 +149,8 @@ public void close() throws IOException {

private native int write(int fd, ByteBuffer buffer, int count) throws LastErrorException;

private native int shutdown(int fd, int how) throws LastErrorException;

private native int close(int fd) throws LastErrorException;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -281,6 +282,57 @@ public void onComplete() {
}
}

/**
* The process in Docker container should receive EOF when reading its
* stdin after the corresponding stream is closed on docker-java side in
* case when the container has been created with StdinOnce flag set to
* true.
*/
@Test
public void closeStdinWithStdinOnce() throws Exception {
Assume.assumeTrue("supports stdin attach", getFactoryType().supportsStdinAttach());
Assume.assumeFalse("not fixed for OkHttp", getFactoryType().equals(FactoryType.OKHTTP));

DockerClient dockerClient = dockerRule.getClient();

CreateContainerResponse container = dockerClient.createContainerCmd(DEFAULT_IMAGE)
.withStdinOpen(true)
.withStdInOnce(true)
.withCmd("sh", "-c", "read -r line; echo Done")
.exec();

CountDownLatch done = new CountDownLatch(1);
PipedOutputStream stdin = new PipedOutputStream();

try (
ResultCallback.Adapter<Frame> resultCallback = dockerClient.attachContainerCmd(container.getId())
.withStdIn(new PipedInputStream(stdin))
.withStdOut(true)
.withFollowStream(true)
.exec(new ResultCallback.Adapter<Frame>() {
@Override
public void onNext(Frame frame) {
if (frame.toString().contains("Done")) {
done.countDown();
}
}
})
) {
resultCallback.awaitStarted(5, SECONDS);
dockerClient.startContainerCmd(container.getId()).exec();

stdin.write("Hello".getBytes(StandardCharsets.UTF_8));
stdin.close();

assertTrue("Should detect closed stdin", done.await(5, SECONDS));

assertTrue(
"The script should finish quickly after stdin is closed on docker-java side",
resultCallback.awaitCompletion(5, SECONDS)
);
}
}

public static class AttachContainerTestCallback extends ResultCallback.Adapter<Frame> {
private final StringBuffer log = new StringBuffer();

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.