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
ByteChannel immediately returning -1? #30
Comments
Hi, thanks for the report. Would it be possible to share a (ideally minimal) code that reproduces the problem? |
Yes, I will when I get a chance. |
Here is the reproduction code (I mean, it could always be my fault, but I don't see anything wrong with this code myself) import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.channels.ByteChannel;
import java.nio.channels.SocketChannel;
import java.security.SecureRandom;
import java.util.Collections;
import javax.net.ssl.SNIHostName;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLParameters;
import tlschannel.ClientTlsChannel;
import tlschannel.NeedsReadException;
public class Test {
public static void main(String[] args) {
// This works
runTestForURL("Google", "https://google.com/");
// This works
runTestForURL("WPT: Working Page", "https://wpt.live/");
// This does not work with TLS-Channel, but works with HttpURLConnection or an actual browser
runTestForURL("WPT: Failing Page", "https://wpt.live/css/css-color/color-001.html");
}
private static void runTestForURL(String testName, String url) {
System.out.println("Running test for " + testName + " (" + url.toString() + ")");
try {
HTTPSocket socket = openSocket(new URL(url));
socket.performTest();
} catch (Exception e) {
e.printStackTrace();
System.out.println("Test error'd");
}
System.out.println("-".repeat(20));
}
private static class HTTPSocket {
private final ByteChannel channel;
public HTTPSocket(ByteChannel channel) {
this.channel = channel;
}
public void sendRequest(String host, String path) throws IOException {
channel.write(ByteBuffer.wrap(
("GET " + (path.isEmpty()?"/":path) + " HTTP/1.1\r\n"
+ "Accept-Charset: utf-8\r\n"
+ "Cache-Control: no-cache\r\n"
+ "Accept: text/html, text/plain\r\n"
+ "Upgrade-Insecure-Requests: 1\r\n"
+ "Connection: keep-alive\r\n"
+ "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36 OPR/80.0.4170.63\r\n"
+ "Sec-Fetch-Dest: document\r\n"
+ "Sec-Fetch-Site: cross-site\r\n"
+ "Sec-Fetch-User: ?1\r\n"
+ "Host: " + host + "\r\n"
+ "Accept-Encoding: identity\r\n"
+ "Sec-Fetch-Mode: navigate\r\n\r\n").getBytes()));
}
public void performTest() throws IOException {
ByteBuffer buffer = ByteBuffer.allocate(128);
buffer.clear();
int rtn;
while (true) {
try {
rtn = channel.read(buffer);
break;
} catch (NeedsReadException e) {}
}
buffer.flip();
if (rtn == -1) {
System.out.println("\033[31mERR_IMMEDIATE_CLOSE\033[0m");
} else {
System.out.println("FIRST " + rtn + " BYTES:");
for (int i = 0; i < rtn; i++) {
System.out.print((char) buffer.get());
}
System.out.println("\n");
}
}
}
private static HTTPSocket openSocket(URL url) throws IOException {
SocketChannel sockChannel = SocketChannel.open();
sockChannel.connect(new InetSocketAddress(url.getHost(), 443));
sockChannel.finishConnect();
ByteChannel finalChannel = sockChannel;
try {
SSLContext sslContext = SSLContext.getInstance("TLSv1.3");
sslContext.init(null, null, new SecureRandom());
SSLEngine engine = sslContext.createSSLEngine();
SSLParameters params = engine.getSSLParameters();
params.setServerNames(Collections.singletonList(new SNIHostName(url.getHost())));
engine.setSSLParameters(params);
engine.setUseClientMode(true);
finalChannel = ClientTlsChannel
.newBuilder(sockChannel, engine)
.build();
} catch (Exception e) {
throw new IOException(e);
}
HTTPSocket socket = new HTTPSocket(finalChannel);
socket.sendRequest(url.getHost(), url.getPath());
// Things act weirdly for blocking sockets, so we are using non-blocking for this test
sockChannel.configureBlocking(false);
return socket;
}
} |
Thanks. I could reproduce it, and confirm that I the problem was already gone in master. So I just released 0.5.0. |
Thank you! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello.
I am encountering problems when using TLS-Channel for this page:
https://wpt.live/css/css-color/color-001.html
This is the only page that I am encountering problems with.
Whenever I try to read from the channel (after sending headers), the ByteChannel only gives -1.
This works fine when I use HTTP instead of HTTPS, and skip using tls-channel, so I suspect that it is a problem with tls-channel.
Edit: Right before it logs that it is returning -1, TLS Channel also logs that it receives 1206 bytes from the stream. However, I logged whether I actually received that from ByteChannel#read() in my code (I only call ByteChannel#read at one point in my code), and tls-channel never actually gives me those 1206 bytes.
The text was updated successfully, but these errors were encountered: