use of java.io.Reader in project hive by apache.
the class TestReusableStringReader method testSkip.
@Test
public void testSkip() throws IOException {
Reader reader = new ReusableStringReader();
((ReusableStringReader) reader).set(fox);
// skip entire the data:
long skipped = reader.skip(fox.length() + 1);
assertEquals(fox.length(), skipped);
assertEquals(-1, reader.read());
// reset the data
((ReusableStringReader) reader).set(fox);
char[] cc = new char[6];
int read;
read = reader.read(cc);
assertEquals(6, read);
assertEquals("Quick ", new String(cc));
// skip some piece of data:
skipped = reader.skip(30);
assertEquals(30, skipped);
read = reader.read(cc);
assertEquals(4, read);
assertEquals("dog.", new String(cc, 0, read));
// skip when already at EOF:
skipped = reader.skip(300);
assertEquals(0, skipped);
assertEquals(-1, reader.read());
reader.close();
}
use of java.io.Reader in project tomcat by apache.
the class TestUpgrade method doTestCheckClosed.
private void doTestCheckClosed(Class<? extends HttpUpgradeHandler> upgradeHandlerClass) throws Exception {
UpgradeConnection conn = doUpgrade(upgradeHandlerClass);
Reader r = conn.getReader();
int c = r.read();
Assert.assertEquals(-1, c);
}
use of java.io.Reader in project tomcat by apache.
the class TestHttp11Processor method testRequestBodySwallowing.
/*
* Tests what happens if a request is completed during a dispatch but the
* request body has not been fully read.
*/
@Test
public void testRequestBodySwallowing() throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
DispatchingServlet servlet = new DispatchingServlet();
Wrapper w = Tomcat.addServlet(ctx, "Test", servlet);
w.setAsyncSupported(true);
ctx.addServletMappingDecoded("/test", "Test");
tomcat.start();
// Hand-craft the client so we have complete control over the timing
SocketAddress addr = new InetSocketAddress("localhost", getPort());
Socket socket = new Socket();
socket.setSoTimeout(300000);
socket.connect(addr, 300000);
OutputStream os = socket.getOutputStream();
Writer writer = new OutputStreamWriter(os, "ISO-8859-1");
InputStream is = socket.getInputStream();
Reader r = new InputStreamReader(is, "ISO-8859-1");
BufferedReader reader = new BufferedReader(r);
// Write the headers
writer.write("POST /test HTTP/1.1\r\n");
writer.write("Host: localhost:8080\r\n");
writer.write("Transfer-Encoding: chunked\r\n");
writer.write("\r\n");
writer.flush();
validateResponse(reader);
// Write the request body
writer.write("2\r\n");
writer.write("AB\r\n");
writer.write("0\r\n");
writer.write("\r\n");
writer.flush();
// Write the 2nd request
writer.write("POST /test HTTP/1.1\r\n");
writer.write("Host: localhost:8080\r\n");
writer.write("Transfer-Encoding: chunked\r\n");
writer.write("\r\n");
writer.flush();
// Read the 2nd response
validateResponse(reader);
// Write the 2nd request body
writer.write("2\r\n");
writer.write("AB\r\n");
writer.write("0\r\n");
writer.write("\r\n");
writer.flush();
// Done
socket.close();
}
use of java.io.Reader in project tomcat by apache.
the class TestSsl method testRenegotiateWorks.
@Test
public void testRenegotiateWorks() throws Exception {
Tomcat tomcat = getTomcatInstance();
Assume.assumeTrue("SSL renegotiation has to be supported for this test", TesterSupport.isClientRenegotiationSupported(getTomcatInstance()));
Context root = tomcat.addContext("", TEMP_DIR);
Wrapper w = Tomcat.addServlet(root, "tester", new TesterServlet());
w.setAsyncSupported(true);
root.addServletMappingDecoded("/", "tester");
TesterSupport.initSsl(tomcat);
tomcat.start();
SSLContext sslCtx = SSLContext.getInstance("TLS");
sslCtx.init(null, TesterSupport.getTrustManagers(), null);
SSLSocketFactory socketFactory = sslCtx.getSocketFactory();
SSLSocket socket = (SSLSocket) socketFactory.createSocket("localhost", getPort());
OutputStream os = socket.getOutputStream();
InputStream is = socket.getInputStream();
Reader r = new InputStreamReader(is);
doRequest(os, r);
TesterHandshakeListener listener = new TesterHandshakeListener();
socket.addHandshakeCompletedListener(listener);
socket.startHandshake();
// One request should be sufficient
int requestCount = 0;
int listenerComplete = 0;
try {
while (requestCount < 10) {
requestCount++;
doRequest(os, r);
if (listener.isComplete() && listenerComplete == 0) {
listenerComplete = requestCount;
}
}
} catch (AssertionError | IOException e) {
String message = "Failed on request number " + requestCount + " after startHandshake(). " + e.getMessage();
log.error(message, e);
Assert.fail(message);
}
Assert.assertTrue(listener.isComplete());
System.out.println("Renegotiation completed after " + listenerComplete + " requests");
}
use of java.io.Reader in project tomcat by apache.
the class SSIFilter method doFilter.
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// cast once
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
// indicate that we're in SSI processing
req.setAttribute(Globals.SSI_FLAG_ATTR, "true");
// setup to capture output
ByteArrayServletOutputStream basos = new ByteArrayServletOutputStream();
ResponseIncludeWrapper responseIncludeWrapper = new ResponseIncludeWrapper(getServletContext(), req, res, basos);
// process remainder of filter chain
chain.doFilter(req, responseIncludeWrapper);
// we can't assume the chain flushed its output
responseIncludeWrapper.flushOutputStreamOrWriter();
byte[] bytes = basos.toByteArray();
// get content type
String contentType = responseIncludeWrapper.getContentType();
// is this an allowed type for SSI processing?
if (contentTypeRegEx.matcher(contentType).matches()) {
String encoding = res.getCharacterEncoding();
// set up SSI processing
SSIExternalResolver ssiExternalResolver = new SSIServletExternalResolver(getServletContext(), req, res, isVirtualWebappRelative, debug, encoding);
SSIProcessor ssiProcessor = new SSIProcessor(ssiExternalResolver, debug, allowExec);
// prepare readers/writers
Reader reader = new InputStreamReader(new ByteArrayInputStream(bytes), encoding);
ByteArrayOutputStream ssiout = new ByteArrayOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(ssiout, encoding));
// do SSI processing
long lastModified = ssiProcessor.process(reader, responseIncludeWrapper.getLastModified(), writer);
// set output bytes
writer.flush();
bytes = ssiout.toByteArray();
// override headers
if (expires != null) {
res.setDateHeader("expires", (new java.util.Date()).getTime() + expires.longValue() * 1000);
}
if (lastModified > 0) {
res.setDateHeader("last-modified", lastModified);
}
res.setContentLength(bytes.length);
Matcher shtmlMatcher = shtmlRegEx.matcher(responseIncludeWrapper.getContentType());
if (shtmlMatcher.matches()) {
// Convert shtml mime type to ordinary html mime type but preserve
// encoding, if any.
String enc = shtmlMatcher.group(1);
res.setContentType("text/html" + ((enc != null) ? enc : ""));
}
}
// write output
OutputStream out = null;
try {
out = res.getOutputStream();
} catch (IllegalStateException e) {
// Ignore, will try to use a writer
}
if (out == null) {
res.getWriter().write(new String(bytes));
} else {
out.write(bytes);
}
}
Aggregations