368787 always set token view to new header buffers in httpparser

This commit is contained in:
Greg Wilkins 2012-01-17 15:24:52 +11:00
parent 81c48518b6
commit 50fe23882a
4 changed files with 85 additions and 150 deletions

View File

@ -63,8 +63,8 @@ public class HttpParser implements Parser
private Buffer _body; // Buffer for large content
private Buffer _buffer; // The current buffer in use (either _header or _content)
private CachedBuffer _cached;
private View.CaseInsensitive _tok0; // Saved token: header name, request method or response version
private View.CaseInsensitive _tok1; // Saved token: header value, request URI or response code
private final View.CaseInsensitive _tok0; // Saved token: header name, request method or response version
private final View.CaseInsensitive _tok1; // Saved token: header value, request URI or response code
private String _multiLineValue;
private int _responseStatus; // If >0 then we are parsing a response
private boolean _forceContentBuffer;
@ -93,13 +93,8 @@ public class HttpParser implements Parser
_buffer=buffer;
_handler=handler;
if (buffer != null)
{
_tok0=new View.CaseInsensitive(buffer);
_tok1=new View.CaseInsensitive(buffer);
_tok0.setPutIndex(_tok0.getIndex());
_tok1.setPutIndex(_tok1.getIndex());
}
_tok0=new View.CaseInsensitive(_header);
_tok1=new View.CaseInsensitive(_header);
}
/* ------------------------------------------------------------------------------- */
@ -114,6 +109,8 @@ public class HttpParser implements Parser
_buffers=buffers;
_endp=endp;
_handler=handler;
_tok0=new View.CaseInsensitive();
_tok1=new View.CaseInsensitive();
}
/* ------------------------------------------------------------------------------- */
@ -256,17 +253,7 @@ public class HttpParser implements Parser
return 0;
if (_buffer==null)
{
if (_header == null)
{
_header=_buffers.getHeader();
}
_buffer=_header;
_tok0=new View.CaseInsensitive(_header);
_tok1=new View.CaseInsensitive(_header);
_tok0.setPutIndex(_tok0.getIndex());
_tok1.setPutIndex(_tok1.getIndex());
}
_buffer=getHeaderBuffer();
if (_state == STATE_CONTENT && _contentPosition == _contentLength)
@ -1013,11 +1000,7 @@ public class HttpParser implements Parser
{
// Do we have a buffer?
if (_buffer==null)
{
_buffer=_header=getHeaderBuffer();
_tok0=new View.CaseInsensitive(_buffer);
_tok1=new View.CaseInsensitive(_buffer);
}
_buffer=getHeaderBuffer();
// Is there unconsumed content in body buffer
if (_state>STATE_END && _buffer==_header && _header!=null && !_header.hasContent() && _body!=null && _body.hasContent())
@ -1086,9 +1069,7 @@ public class HttpParser implements Parser
// This is probably a pipelined header of the next request, so we need to
// copy it to the header buffer.
if (_header==null)
{
_header=_buffers.getHeader();
}
getHeaderBuffer();
else
{
_header.setMarkIndex(-1);
@ -1165,6 +1146,8 @@ public class HttpParser implements Parser
if (_header == null)
{
_header=_buffers.getHeader();
_tok0.update(_header);
_tok1.update(_header);
}
return _header;
}

View File

@ -448,8 +448,8 @@ public class HttpConnectionTest
PrintWriter writer = response.getWriter();
writer.write("<html><h1>FOO</h1></html>");
writer.flush();
writer.close();
throw new RuntimeException("SHOULD NOT GET HERE");
if (!writer.checkError())
throw new RuntimeException("SHOULD NOT GET HERE");
}
catch(Exception e)
{

View File

@ -31,16 +31,21 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import junit.framework.Assert;
import org.eclipse.jetty.http.HttpParser;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.StdErrLog;
import org.junit.Test;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertThat;
/**
*
@ -492,7 +497,7 @@ public abstract class HttpServerTestBase extends HttpServerTestFixture
}
finally
{
System.err.println("Got "+total+" of "+(512*1024));
//System.err.println("Got "+total+" of "+(512*1024));
client.close();
}
}
@ -739,7 +744,7 @@ public abstract class HttpServerTestBase extends HttpServerTestFixture
IO.copy(is,bout);
byte[] b=bout.toByteArray();
System.err.println("OUTPUT: "+new String(b));
//System.err.println("OUTPUT: "+new String(b));
int i=0;
while (b[i]!='Z')
i++;
@ -1205,4 +1210,69 @@ public abstract class HttpServerTestBase extends HttpServerTestFixture
@Test
public void testUnreadInput () throws Exception
{
configureServer(new NoopHandler());
final int REQS=5;
String content="This is a loooooooooooooooooooooooooooooooooo"+
"ooooooooooooooooooooooooooooooooooooooooooooo"+
"ooooooooooooooooooooooooooooooooooooooooooooo"+
"ooooooooooooooooooooooooooooooooooooooooooooo"+
"ooooooooooooooooooooooooooooooooooooooooooooo"+
"ooooooooooooooooooooooooooooooooooooooooooooo"+
"ooooooooooooooooooooooooooooooooooooooooooooo"+
"ooooooooooooooooooooooooooooooooooooooooooooo"+
"ooooooooooooooooooooooooooooooooooooooooooooo"+
"oooooooooooonnnnnnnnnnnnnnnnggggggggg content"+
new String(new char[65*1024]);
final byte[] bytes = content.getBytes();
Socket client=newSocket(HOST,_connector.getLocalPort());
final OutputStream out=client.getOutputStream();
new Thread()
{
public void run()
{
try
{
for (int i=0; i<REQS; i++)
{
out.write("GET / HTTP/1.1\r\nHost: localhost\r\n".getBytes(StringUtil.__ISO_8859_1));
out.write(("Content-Length: "+bytes.length+"\r\n" + "\r\n").getBytes(StringUtil.__ISO_8859_1));
out.write(bytes,0,bytes.length);
}
out.write("GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n".getBytes(StringUtil.__ISO_8859_1));
out.flush();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}.start();
String resps = readResponse(client);
int offset=0;
for (int i=0;i<(REQS+1);i++)
{
int ok=resps.indexOf("HTTP/1.1 200 OK",offset);
assertThat("resp"+i,ok,greaterThanOrEqualTo(offset));
offset=ok+15;
}
}
public class NoopHandler extends AbstractHandler
{
public void handle(String target, Request baseRequest,
HttpServletRequest request, HttpServletResponse response) throws IOException,
ServletException
{
//don't read the input, just send something back
((Request)request).setHandled(true);
response.setStatus(200);
}
}
}

View File

@ -1,118 +0,0 @@
// ========================================================================
// Copyright (c) 2008-2009 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.server;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.bio.SocketConnector;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class UnreadInputTest
{
public static final String __OK_RESPONSE = "HTTP/1.1 200 OK\r\nContent-Length: 0\r\nServer: Jetty(7.0.x)\r\n\r\n";
protected Server _server = new Server();
protected SocketConnector _connector;
protected int _port;
protected Socket _socket;
protected OutputStream _outputStream;
protected InputStream _inputStream;
@Before
public void init() throws Exception
{
//server side
_connector = new SocketConnector();
_server.setConnectors(new Connector[]{ _connector });
_server.setHandler(new NoopHandler());
_server.start();
_port = _connector.getLocalPort();
//client side
_socket = new Socket((String)null, _port);
_outputStream = _socket.getOutputStream();
_inputStream = _socket.getInputStream();
}
@After
public void destroy() throws Exception
{
_socket.close();
_server.stop();
_server.join();
}
@Test
public void testUnreadInput () throws Exception
{
for (int i=0; i<2; i++)
{
String content = "This is a loooooooooooooooooooooooooooooooooo"+
"ooooooooooooooooooooooooooooooooooooooooooooo"+
"ooooooooooooooooooooooooooooooooooooooooooooo"+
"ooooooooooooooooooooooooooooooooooooooooooooo"+
"ooooooooooooooooooooooooooooooooooooooooooooo"+
"ooooooooooooooooooooooooooooooooooooooooooooo"+
"ooooooooooooooooooooooooooooooooooooooooooooo"+
"ooooooooooooooooooooooooooooooooooooooooooooo"+
"ooooooooooooooooooooooooooooooooooooooooooooo"+
"ooooooooooooooooooooooonnnnnnnnnnnnnnnnggggggggg content";
byte[] bytes = content.getBytes();
_outputStream.write("GET / HTTP/1.1\r\nHost: localhost\r\n".getBytes());
Thread.currentThread();
Thread.sleep(500L);
String str = "Content-Length: "+bytes.length+"\r\n" + "\r\n";
_outputStream.write(str.getBytes());
Thread.currentThread();
Thread.sleep(500L);
//write some bytes of the content
_outputStream.write(bytes, 0, (bytes.length/2));
Thread.currentThread();
Thread.sleep(1000L);
//write the rest
_outputStream.write(bytes, bytes.length/2, (bytes.length - bytes.length/2));
}
byte[] inbuf = new byte[__OK_RESPONSE.getBytes().length*2];
int x = _inputStream.read(inbuf);
System.err.println(new String(inbuf, 0, x));
}
public class NoopHandler extends AbstractHandler
{
public void handle(String target, Request baseRequest,
HttpServletRequest request, HttpServletResponse response) throws IOException,
ServletException
{
//don't read the input, just send something back
((Request)request).setHandled(true);
response.setStatus(200);
}
}
}