use of java.io.InputStream in project jetty.project by eclipse.
the class HttpServerTestBase method testBlockingWhileWritingResponseContent.
@Test
public void testBlockingWhileWritingResponseContent() throws Exception {
configureServer(new DataHandler());
long start = System.currentTimeMillis();
int total = 0;
try (Socket client = newSocket(_serverURI.getHost(), _serverURI.getPort())) {
OutputStream os = client.getOutputStream();
InputStream is = client.getInputStream();
os.write(("GET /data?writes=256&block=1024 HTTP/1.1\r\n" + "host: " + _serverURI.getHost() + ":" + _serverURI.getPort() + "\r\n" + "connection: close\r\n" + "content-type: unknown\r\n" + "\r\n").getBytes());
os.flush();
int len = 0;
byte[] buf = new byte[1024 * 32];
int sleeps = 0;
while (len >= 0) {
len = is.read(buf);
if (len > 0) {
total += len;
if ((total / 10240) > sleeps) {
Thread.sleep(200);
sleeps++;
}
}
}
assertTrue(total > (256 * 1024));
assertTrue(30000L > (System.currentTimeMillis() - start));
}
}
use of java.io.InputStream in project jetty.project by eclipse.
the class HttpServerTestBase method testBlockingWhileReadingRequestContent.
@Test
public void testBlockingWhileReadingRequestContent() throws Exception {
configureServer(new DataHandler());
long start = System.currentTimeMillis();
try (Socket client = newSocket(_serverURI.getHost(), _serverURI.getPort())) {
OutputStream os = client.getOutputStream();
InputStream is = client.getInputStream();
os.write(("GET /data?writes=1024&block=256 HTTP/1.1\r\n" + "host: " + _serverURI.getHost() + ":" + _serverURI.getPort() + "\r\n" + "connection: close\r\n" + "content-type: unknown\r\n" + "content-length: 30\r\n" + "\r\n").getBytes());
os.flush();
Thread.sleep(200);
os.write(("\r\n23456890").getBytes());
os.flush();
Thread.sleep(1000);
os.write(("abcdefghij").getBytes());
os.flush();
Thread.sleep(1000);
os.write(("0987654321\r\n").getBytes());
os.flush();
int total = 0;
int len = 0;
byte[] buf = new byte[1024 * 64];
int sleeps = 0;
while (len >= 0) {
len = is.read(buf);
if (len > 0) {
total += len;
if ((total / 10240) > sleeps) {
sleeps++;
Thread.sleep(100);
}
}
}
assertTrue(total > (1024 * 256));
assertTrue(30000L > (System.currentTimeMillis() - start));
}
}
use of java.io.InputStream in project jetty.project by eclipse.
the class HttpServerTestBase method testHead.
@Test
public void testHead() throws Exception {
configureServer(new EchoHandler(false));
try (Socket client = newSocket(_serverURI.getHost(), _serverURI.getPort())) {
OutputStream os = client.getOutputStream();
InputStream is = client.getInputStream();
os.write(("POST /R1 HTTP/1.1\015\012" + "Host: " + _serverURI.getHost() + ":" + _serverURI.getPort() + "\r\n" + "content-type: text/plain; charset=utf-8\r\n" + "content-length: 10\r\n" + "\015\012" + "123456789\n" + "HEAD /R2 HTTP/1.1\015\012" + "Host: " + _serverURI.getHost() + ":" + _serverURI.getPort() + "\015\012" + "content-type: text/plain; charset=utf-8\r\n" + "content-length: 10\r\n" + "\015\012" + "ABCDEFGHI\n" + "POST /R3 HTTP/1.1\015\012" + "Host: " + _serverURI.getHost() + ":" + _serverURI.getPort() + "\015\012" + "content-type: text/plain; charset=utf-8\r\n" + "content-length: 10\r\n" + "Connection: close\015\012" + "\015\012" + "abcdefghi\n").getBytes(StandardCharsets.ISO_8859_1));
String in = IO.toString(is);
Assert.assertThat(in, containsString("123456789"));
Assert.assertThat(in, not(containsString("ABCDEFGHI")));
Assert.assertThat(in, containsString("abcdefghi"));
}
}
use of java.io.InputStream in project jetty.project by eclipse.
the class HttpServerTestBase method testRecycledWriters.
@Test
public void testRecycledWriters() throws Exception {
configureServer(new EchoHandler());
try (Socket client = newSocket(_serverURI.getHost(), _serverURI.getPort())) {
OutputStream os = client.getOutputStream();
InputStream is = client.getInputStream();
os.write(("POST /echo?charset=utf-8 HTTP/1.1\r\n" + "host: " + _serverURI.getHost() + ":" + _serverURI.getPort() + "\r\n" + "content-type: text/plain; charset=utf-8\r\n" + "content-length: 10\r\n" + "\r\n").getBytes(StandardCharsets.ISO_8859_1));
os.write(("123456789\n").getBytes("utf-8"));
os.write(("POST /echo?charset=utf-8 HTTP/1.1\r\n" + "host: " + _serverURI.getHost() + ":" + _serverURI.getPort() + "\r\n" + "content-type: text/plain; charset=utf-8\r\n" + "content-length: 10\r\n" + "\r\n").getBytes(StandardCharsets.ISO_8859_1));
os.write(("abcdefghZ\n").getBytes("utf-8"));
String content = "Wibble";
byte[] contentB = content.getBytes("utf-8");
os.write(("POST /echo?charset=utf-16 HTTP/1.1\r\n" + "host: " + _serverURI.getHost() + ":" + _serverURI.getPort() + "\r\n" + "content-type: text/plain; charset=utf-8\r\n" + "content-length: " + contentB.length + "\r\n" + "connection: close\r\n" + "\r\n").getBytes(StandardCharsets.ISO_8859_1));
os.write(contentB);
os.flush();
ByteArrayOutputStream bout = new ByteArrayOutputStream();
IO.copy(is, bout);
byte[] b = bout.toByteArray();
//System.err.println("OUTPUT: "+new String(b));
int i = 0;
while (b[i] != 'Z') i++;
int state = 0;
while (state != 4) {
switch(b[i++]) {
case '\r':
if (state == 0 || state == 2)
state++;
continue;
case '\n':
if (state == 1 || state == 3)
state++;
continue;
default:
state = 0;
}
}
String in = new String(b, 0, i, StandardCharsets.UTF_8);
assertThat(in, containsString("123456789"));
assertThat(in, containsString("abcdefghZ"));
assertFalse(in.contains("Wibble"));
in = new String(b, i, b.length - i, StandardCharsets.UTF_16);
assertEquals("Wibble\n", in);
}
}
use of java.io.InputStream in project jetty.project by eclipse.
the class HttpServerTestBase method testCommittedError.
@Test
public void testCommittedError() throws Exception {
CommittedErrorHandler handler = new CommittedErrorHandler();
configureServer(handler);
try (Socket client = newSocket(_serverURI.getHost(), _serverURI.getPort());
StacklessLogging stackless = new StacklessLogging(HttpChannel.class)) {
((AbstractLogger) Log.getLogger(HttpChannel.class)).info("Expecting exception after commit then could not send 500....");
OutputStream os = client.getOutputStream();
InputStream is = client.getInputStream();
// Send a request
os.write(("GET / HTTP/1.1\r\n" + "Host: " + _serverURI.getHost() + ":" + _serverURI.getPort() + "\r\n" + "\r\n").getBytes());
os.flush();
client.setSoTimeout(2000);
String in = IO.toString(is);
// Closed by error!
assertEquals(-1, is.read());
assertThat(in, containsString("HTTP/1.1 200 OK"));
assertTrue(in.indexOf("Transfer-Encoding: chunked") > 0);
assertTrue(in.indexOf("Now is the time for all good men to come to the aid of the party") > 0);
assertThat(in, Matchers.not(Matchers.containsString("\r\n0\r\n")));
client.close();
Thread.sleep(200);
assertTrue(!handler._endp.isOpen());
}
}
Aggregations