jetty-9 javadoc and code cleanups

This commit is contained in:
Greg Wilkins 2012-03-08 15:18:17 +11:00
parent 8c25941e04
commit 03525bfe46
99 changed files with 1838 additions and 1877 deletions

View File

@ -29,7 +29,7 @@ import org.eclipse.jetty.http.HttpScheme;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.io.AbstractConnection;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.Buffers;
import org.eclipse.jetty.io.ByteArrayBuffer;
import org.eclipse.jetty.io.Connection;

View File

@ -18,7 +18,7 @@ import java.io.IOException;
import org.eclipse.jetty.http.AbstractGenerator;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.io.AsyncEndPoint;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.Buffers;
import org.eclipse.jetty.io.Connection;
import org.eclipse.jetty.io.EndPoint;

View File

@ -18,7 +18,7 @@ import java.io.InterruptedIOException;
import org.eclipse.jetty.http.AbstractGenerator;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.Buffers;
import org.eclipse.jetty.io.Connection;
import org.eclipse.jetty.io.EndPoint;

View File

@ -15,7 +15,7 @@ package org.eclipse.jetty.client;
import java.io.IOException;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.io.ByteBuffer;
/**
* An exchange that retains response status and response headers for later use.

View File

@ -21,8 +21,8 @@ import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.util.StringUtil;
/**

View File

@ -31,7 +31,7 @@ import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.http.PathMap;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.ByteArrayBuffer;
import org.eclipse.jetty.io.Connection;
import org.eclipse.jetty.io.EndPoint;

View File

@ -16,7 +16,7 @@ package org.eclipse.jetty.client;
import java.io.IOException;
import org.eclipse.jetty.io.ByteBuffer;
/**
*

View File

@ -16,7 +16,7 @@ package org.eclipse.jetty.client;
import java.io.IOException;
import org.eclipse.jetty.io.ByteBuffer;
public class HttpEventListenerWrapper implements HttpEventListener
{

View File

@ -25,7 +25,7 @@ import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.HttpScheme;
import org.eclipse.jetty.http.HttpURI;
import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.BufferCache.ByteBuffer;
import org.eclipse.jetty.io.ByteArrayBuffer;
import org.eclipse.jetty.io.Connection;

View File

@ -18,7 +18,7 @@ import java.io.IOException;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpScheme;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.io.ByteBuffer;
/**
* RedirectListener

View File

@ -23,7 +23,7 @@ import java.util.concurrent.ConcurrentHashMap;
import javax.net.ssl.SSLEngine;
import org.eclipse.jetty.io.AsyncEndPoint;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.ConnectedEndPoint;
import org.eclipse.jetty.io.Connection;
import org.eclipse.jetty.io.nio.AsyncConnection;

View File

@ -18,7 +18,7 @@ import java.io.IOException;
import org.eclipse.jetty.client.HttpExchange;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.ByteArrayBuffer;
import org.eclipse.jetty.util.B64Code;
import org.eclipse.jetty.util.StringUtil;

View File

@ -18,7 +18,7 @@ import java.io.IOException;
import org.eclipse.jetty.client.HttpExchange;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.ByteArrayBuffer;
import org.eclipse.jetty.util.B64Code;
import org.eclipse.jetty.util.StringUtil;

View File

@ -23,7 +23,7 @@ import org.eclipse.jetty.client.HttpEventListenerWrapper;
import org.eclipse.jetty.client.HttpExchange;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;

View File

@ -17,7 +17,7 @@ import java.io.IOException;
import org.eclipse.jetty.client.CachedExchange;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;

View File

@ -17,7 +17,7 @@ import java.io.IOException;
import org.eclipse.jetty.client.HttpExchange;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;

View File

@ -22,7 +22,7 @@ import org.eclipse.jetty.client.HttpExchange;
import org.eclipse.jetty.client.security.SecurityListener;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;

View File

@ -16,7 +16,7 @@ package org.eclipse.jetty.client.webdav;
import java.io.IOException;
import org.eclipse.jetty.client.HttpExchange;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;

View File

@ -21,7 +21,7 @@ import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.server.AbstractHttpConnection;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Request;

View File

@ -5,7 +5,7 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.io.ByteBuffer;
/* ------------------------------------------------------------ */

View File

@ -35,7 +35,7 @@ import org.eclipse.jetty.client.security.ProxyAuthorization;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.ByteArrayBuffer;
import org.eclipse.jetty.io.Connection;
import org.eclipse.jetty.io.EofException;

View File

@ -31,7 +31,7 @@ import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.client.security.Realm;
import org.eclipse.jetty.client.security.SimpleRealmResolver;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.EofException;
import org.eclipse.jetty.security.ConstraintMapping;
import org.eclipse.jetty.security.ConstraintSecurityHandler;

View File

@ -7,7 +7,7 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.util.thread.QueuedThreadPool;

View File

@ -13,7 +13,7 @@ import junit.framework.Assert;
import org.eclipse.jetty.http.HttpHeaderValue;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.bio.SocketConnector;

View File

@ -33,7 +33,7 @@ import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.HttpParser;
import org.eclipse.jetty.io.AsyncEndPoint;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.Buffers;
import org.eclipse.jetty.io.Connection;
import org.eclipse.jetty.io.EndPoint;

View File

@ -24,7 +24,7 @@ import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletRequest;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.Connection;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.server.Connector;

View File

@ -4,11 +4,11 @@
// 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
// 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.
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.http;
@ -25,37 +25,37 @@ import org.eclipse.jetty.util.Utf8StringBuffer;
public class EncodedHttpURI extends HttpURI
{
private final String _encoding;
public EncodedHttpURI(String encoding)
{
super();
_encoding = encoding;
}
@Override
public String getScheme()
{
if (_scheme==_authority)
return null;
int l=_authority-_scheme;
if (l==5 &&
_raw[_scheme]=='h' &&
_raw[_scheme+1]=='t' &&
_raw[_scheme+2]=='t' &&
_raw[_scheme+3]=='p' )
if (l==5 &&
_raw[_scheme]=='h' &&
_raw[_scheme+1]=='t' &&
_raw[_scheme+2]=='t' &&
_raw[_scheme+3]=='p' )
return HttpScheme.HTTP.toString();
if (l==6 &&
_raw[_scheme]=='h' &&
_raw[_scheme+1]=='t' &&
_raw[_scheme+2]=='t' &&
_raw[_scheme+3]=='p' &&
_raw[_scheme+4]=='s' )
if (l==6 &&
_raw[_scheme]=='h' &&
_raw[_scheme+1]=='t' &&
_raw[_scheme+2]=='t' &&
_raw[_scheme+3]=='p' &&
_raw[_scheme+4]=='s' )
return HttpScheme.HTTPS.toString();
return StringUtil.toString(_raw,_scheme,_authority-_scheme-1,_encoding);
}
@Override
public String getAuthority()
{
@ -63,7 +63,7 @@ public class EncodedHttpURI extends HttpURI
return null;
return StringUtil.toString(_raw,_authority,_path-_authority,_encoding);
}
@Override
public String getHost()
{
@ -71,7 +71,7 @@ public class EncodedHttpURI extends HttpURI
return null;
return StringUtil.toString(_raw,_host,_port-_host,_encoding);
}
@Override
public int getPort()
{
@ -79,7 +79,7 @@ public class EncodedHttpURI extends HttpURI
return -1;
return TypeUtil.parseInt(_raw, _port+1, _path-_port-1,10);
}
@Override
public String getPath()
{
@ -87,7 +87,7 @@ public class EncodedHttpURI extends HttpURI
return null;
return StringUtil.toString(_raw,_path,_param-_path,_encoding);
}
@Override
public String getDecodedPath()
{
@ -95,7 +95,7 @@ public class EncodedHttpURI extends HttpURI
return null;
return URIUtil.decodePath(_raw,_path,_param-_path);
}
@Override
public String getPathAndParam()
{
@ -103,7 +103,7 @@ public class EncodedHttpURI extends HttpURI
return null;
return StringUtil.toString(_raw,_path,_query-_path,_encoding);
}
@Override
public String getCompletePath()
{
@ -111,7 +111,7 @@ public class EncodedHttpURI extends HttpURI
return null;
return StringUtil.toString(_raw,_path,_end-_path,_encoding);
}
@Override
public String getParam()
{
@ -119,7 +119,7 @@ public class EncodedHttpURI extends HttpURI
return null;
return StringUtil.toString(_raw,_param+1,_query-_param-1,_encoding);
}
@Override
public String getQuery()
{
@ -127,13 +127,13 @@ public class EncodedHttpURI extends HttpURI
return null;
return StringUtil.toString(_raw,_query+1,_fragment-_query-1,_encoding);
}
@Override
public boolean hasQuery()
{
return (_fragment>_query);
}
@Override
public String getFragment()
{
@ -143,7 +143,7 @@ public class EncodedHttpURI extends HttpURI
}
@Override
public void decodeQueryTo(MultiMap parameters)
public void decodeQueryTo(MultiMap parameters)
{
if (_query==_fragment)
return;
@ -151,17 +151,17 @@ public class EncodedHttpURI extends HttpURI
}
@Override
public void decodeQueryTo(MultiMap parameters, String encoding)
throws UnsupportedEncodingException
{
public void decodeQueryTo(MultiMap parameters, String encoding)
throws UnsupportedEncodingException
{
if (_query==_fragment)
return;
if (encoding==null)
encoding=_encoding;
UrlEncoded.decodeTo(StringUtil.toString(_raw,_query+1,_fragment-_query-1,encoding),parameters,encoding);
}
}
@Override
public String toString()
{
@ -169,10 +169,10 @@ public class EncodedHttpURI extends HttpURI
_rawString= StringUtil.toString(_raw,_scheme,_end-_scheme,_encoding);
return _rawString;
}
public void writeTo(Utf8StringBuffer buf)
{
buf.getStringBuffer().append(toString());
}
}

View File

@ -4,11 +4,11 @@
// 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
// 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.
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.http;
@ -24,7 +24,7 @@ public interface HttpBuffers
* @return the requestBufferSize
*/
public int getRequestBufferSize();
/**
* @param requestBufferSize the requestBufferSize to set
*/
@ -97,5 +97,5 @@ public interface HttpBuffers
public void setMaxBuffers(int maxBuffers);
public int getMaxBuffers();
}

View File

@ -4,11 +4,11 @@
// 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
// 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.
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.http;
@ -29,32 +29,34 @@ public class HttpBuffersImpl extends AbstractLifeCycle implements HttpBuffers
private int _responseBufferSize=32*1024;
private int _responseHeaderSize=6*1024;
private int _maxBuffers=1024;
private Buffers.Type _requestBufferType=Buffers.Type.INDIRECT;
private Buffers.Type _requestHeaderType=Buffers.Type.INDIRECT;
private Buffers.Type _responseBufferType=Buffers.Type.INDIRECT;
private Buffers.Type _responseHeaderType=Buffers.Type.INDIRECT;
private Buffers _requestBuffers;
private Buffers _responseBuffers;
public HttpBuffersImpl()
{
super();
}
/**
* @return the requestBufferSize
*/
@Override
public int getRequestBufferSize()
{
return _requestBufferSize;
}
/**
* @param requestBufferSize the requestBufferSize to set
*/
@Override
public void setRequestBufferSize(int requestBufferSize)
{
_requestBufferSize = requestBufferSize;
@ -63,6 +65,7 @@ public class HttpBuffersImpl extends AbstractLifeCycle implements HttpBuffers
/**
* @return the requestHeaderSize
*/
@Override
public int getRequestHeaderSize()
{
return _requestHeaderSize;
@ -71,6 +74,7 @@ public class HttpBuffersImpl extends AbstractLifeCycle implements HttpBuffers
/**
* @param requestHeaderSize the requestHeaderSize to set
*/
@Override
public void setRequestHeaderSize(int requestHeaderSize)
{
_requestHeaderSize = requestHeaderSize;
@ -79,6 +83,7 @@ public class HttpBuffersImpl extends AbstractLifeCycle implements HttpBuffers
/**
* @return the responseBufferSize
*/
@Override
public int getResponseBufferSize()
{
return _responseBufferSize;
@ -87,6 +92,7 @@ public class HttpBuffersImpl extends AbstractLifeCycle implements HttpBuffers
/**
* @param responseBufferSize the responseBufferSize to set
*/
@Override
public void setResponseBufferSize(int responseBufferSize)
{
_responseBufferSize = responseBufferSize;
@ -95,6 +101,7 @@ public class HttpBuffersImpl extends AbstractLifeCycle implements HttpBuffers
/**
* @return the responseHeaderSize
*/
@Override
public int getResponseHeaderSize()
{
return _responseHeaderSize;
@ -103,6 +110,7 @@ public class HttpBuffersImpl extends AbstractLifeCycle implements HttpBuffers
/**
* @param responseHeaderSize the responseHeaderSize to set
*/
@Override
public void setResponseHeaderSize(int responseHeaderSize)
{
_responseHeaderSize = responseHeaderSize;
@ -111,6 +119,7 @@ public class HttpBuffersImpl extends AbstractLifeCycle implements HttpBuffers
/**
* @return the requestBufferType
*/
@Override
public Buffers.Type getRequestBufferType()
{
return _requestBufferType;
@ -127,6 +136,7 @@ public class HttpBuffersImpl extends AbstractLifeCycle implements HttpBuffers
/**
* @return the requestHeaderType
*/
@Override
public Buffers.Type getRequestHeaderType()
{
return _requestHeaderType;
@ -143,6 +153,7 @@ public class HttpBuffersImpl extends AbstractLifeCycle implements HttpBuffers
/**
* @return the responseBufferType
*/
@Override
public Buffers.Type getResponseBufferType()
{
return _responseBufferType;
@ -159,6 +170,7 @@ public class HttpBuffersImpl extends AbstractLifeCycle implements HttpBuffers
/**
* @return the responseHeaderType
*/
@Override
public Buffers.Type getResponseHeaderType()
{
return _responseHeaderType;
@ -175,6 +187,7 @@ public class HttpBuffersImpl extends AbstractLifeCycle implements HttpBuffers
/**
* @param requestBuffers the requestBuffers to set
*/
@Override
public void setRequestBuffers(Buffers requestBuffers)
{
_requestBuffers = requestBuffers;
@ -183,6 +196,7 @@ public class HttpBuffersImpl extends AbstractLifeCycle implements HttpBuffers
/**
* @param responseBuffers the responseBuffers to set
*/
@Override
public void setResponseBuffers(Buffers responseBuffers)
{
_responseBuffers = responseBuffers;
@ -190,42 +204,47 @@ public class HttpBuffersImpl extends AbstractLifeCycle implements HttpBuffers
@Override
protected void doStart()
throws Exception
{
throws Exception
{
_requestBuffers=BuffersFactory.newBuffers(_requestHeaderType,_requestHeaderSize,_requestBufferType,_requestBufferSize,_requestBufferType,getMaxBuffers());
_responseBuffers=BuffersFactory.newBuffers(_responseHeaderType,_responseHeaderSize,_responseBufferType,_responseBufferSize,_responseBufferType,getMaxBuffers());
super.doStart();
}
}
@Override
protected void doStop()
throws Exception
{
throws Exception
{
_requestBuffers=null;
_responseBuffers=null;
}
}
@Override
public Buffers getRequestBuffers()
{
return _requestBuffers;
}
@Override
public Buffers getResponseBuffers()
{
return _responseBuffers;
}
@Override
public void setMaxBuffers(int maxBuffers)
{
_maxBuffers = maxBuffers;
}
@Override
public int getMaxBuffers()
{
return _maxBuffers;
}
@Override
public String toString()
{
return _requestBuffers+"/"+_responseBuffers;

View File

@ -4,11 +4,11 @@
// 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
// 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.
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.http;
@ -34,7 +34,7 @@ public interface HttpContent
long getContentLength();
InputStream getInputStream() throws IOException;
void release();
/* ------------------------------------------------------------ */
/* ------------------------------------------------------------ */
/* ------------------------------------------------------------ */
@ -50,7 +50,7 @@ public interface HttpContent
_mimeType=mimeType;
_maxBuffer=-1;
}
public ResourceAsHttpContent(final Resource resource, final ByteBuffer mimeType, int maxBuffer)
{
_resource=resource;
@ -59,24 +59,28 @@ public interface HttpContent
}
/* ------------------------------------------------------------ */
@Override
public ByteBuffer getContentType()
{
return _mimeType;
}
/* ------------------------------------------------------------ */
@Override
public ByteBuffer getLastModified()
{
return null;
}
/* ------------------------------------------------------------ */
@Override
public ByteBuffer getDirectBuffer()
{
return null;
}
/* ------------------------------------------------------------ */
@Override
public ByteBuffer getIndirectBuffer()
{
try
@ -85,10 +89,10 @@ public interface HttpContent
return null;
int length=(int)_resource.length();
byte[] array = new byte[length];
int offset=0;
InputStream in=_resource.getInputStream();
do
{
int filled=in.read(array,offset,length);
@ -98,7 +102,7 @@ public interface HttpContent
offset+=filled;
}
while(length>0);
ByteBuffer buffer = ByteBuffer.wrap(array);
return buffer;
}
@ -109,24 +113,28 @@ public interface HttpContent
}
/* ------------------------------------------------------------ */
@Override
public long getContentLength()
{
return _resource.length();
}
/* ------------------------------------------------------------ */
@Override
public InputStream getInputStream() throws IOException
{
return _resource.getInputStream();
}
/* ------------------------------------------------------------ */
@Override
public Resource getResource()
{
return _resource;
}
/* ------------------------------------------------------------ */
@Override
public void release()
{
_resource.release();

View File

@ -4,25 +4,25 @@
// 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
// 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.
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.http;
public class HttpCookie
{
private final String _name;
private final String _value;
private final String _comment;
private final String _domain;
private final int _maxAge;
private final String _path;
private final boolean _secure;
private final int _version;
private final String _name;
private final String _value;
private final String _comment;
private final String _domain;
private final int _maxAge;
private final String _path;
private final boolean _secure;
private final int _version;
private final boolean _httpOnly;
/* ------------------------------------------------------------ */
@ -39,7 +39,7 @@ public class HttpCookie
_secure = false;
_version = 0;
}
/* ------------------------------------------------------------ */
public HttpCookie(String name, String value, String domain, String path)
{
@ -53,9 +53,9 @@ public class HttpCookie
_path = path;
_secure = false;
_version = 0;
}
/* ------------------------------------------------------------ */
public HttpCookie(String name, String value, int maxAge)
{
@ -70,7 +70,7 @@ public class HttpCookie
_secure = false;
_version = 0;
}
/* ------------------------------------------------------------ */
public HttpCookie(String name, String value, String domain, String path, int maxAge, boolean httpOnly, boolean secure)
{
@ -85,7 +85,7 @@ public class HttpCookie
_value = value;
_version = 0;
}
/* ------------------------------------------------------------ */
public HttpCookie(String name, String value, String domain, String path, int maxAge, boolean httpOnly, boolean secure, String comment, int version)
{
@ -100,7 +100,7 @@ public class HttpCookie
_value = value;
_version = version;
}
/* ------------------------------------------------------------ */
/** Get the name.
* @return the name
@ -109,7 +109,7 @@ public class HttpCookie
{
return _name;
}
/* ------------------------------------------------------------ */
/** Get the value.
* @return the value
@ -118,7 +118,7 @@ public class HttpCookie
{
return _value;
}
/* ------------------------------------------------------------ */
/** Get the comment.
* @return the comment
@ -127,7 +127,7 @@ public class HttpCookie
{
return _comment;
}
/* ------------------------------------------------------------ */
/** Get the domain.
* @return the domain
@ -136,7 +136,7 @@ public class HttpCookie
{
return _domain;
}
/* ------------------------------------------------------------ */
/** Get the maxAge.
* @return the maxAge
@ -145,7 +145,7 @@ public class HttpCookie
{
return _maxAge;
}
/* ------------------------------------------------------------ */
/** Get the path.
* @return the path
@ -154,7 +154,7 @@ public class HttpCookie
{
return _path;
}
/* ------------------------------------------------------------ */
/** Get the secure.
* @return the secure
@ -163,7 +163,7 @@ public class HttpCookie
{
return _secure;
}
/* ------------------------------------------------------------ */
/** Get the version.
* @return the version
@ -172,7 +172,7 @@ public class HttpCookie
{
return _version;
}
/* ------------------------------------------------------------ */
/** Get the isHttpOnly.
* @return the isHttpOnly
@ -181,6 +181,6 @@ public class HttpCookie
{
return _httpOnly;
}
}

View File

@ -4,11 +4,11 @@
// 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
// 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.
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.http;
@ -84,6 +84,6 @@ public class HttpException extends IOException
{
return ("HttpException("+_status+","+_reason+","+super.getCause()+")");
}
}

File diff suppressed because it is too large Load Diff

View File

@ -13,17 +13,8 @@
package org.eclipse.jetty.http;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
import javax.swing.text.View;
import org.eclipse.jetty.http.HttpGenerator.Action;
import org.eclipse.jetty.io.Buffers;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.io.EofException;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.log.Log;
@ -36,7 +27,7 @@ import org.eclipse.jetty.util.log.Logger;
*
*
*/
public class HttpGenerator
public class HttpGenerator
{
// Build cache of response lines for status
@ -94,7 +85,7 @@ public class HttpGenerator
private State _state = State.START;
private int _status = 0;
private final HttpFields _fields;
private HttpVersion _version = HttpVersion.HTTP_1_1;
private byte[] _reason;
@ -112,7 +103,7 @@ public class HttpGenerator
private ByteBuffer _date;
private boolean _sendServerVersion;
// common _content
@ -142,7 +133,7 @@ public class HttpGenerator
{
_fields=fields;
}
/* ------------------------------------------------------------------------------- */
public void reset()
{
@ -158,7 +149,7 @@ public class HttpGenerator
_date = null;
_method=null;
_needCRLF = false;
_uri=null;
_noContent=false;
@ -260,8 +251,8 @@ public class HttpGenerator
public boolean isPersistent()
{
return _persistent!=null
?_persistent.booleanValue()
:(isRequest()?true:_version.ordinal()>HttpVersion.HTTP_1_0.ordinal());
?_persistent.booleanValue()
:(isRequest()?true:_version.ordinal()>HttpVersion.HTTP_1_0.ordinal());
}
/* ------------------------------------------------------------ */
@ -309,7 +300,7 @@ public class HttpGenerator
_method=StringUtil.getBytes(method);
_uri=StringUtil.getUtf8Bytes(uri);
}
/* ------------------------------------------------------------ */
/**
*/
@ -321,7 +312,7 @@ public class HttpGenerator
_uri=StringUtil.getUtf8Bytes(uri);
setVersion(version);
}
/* ------------------------------------------------------------ */
/**
@ -330,7 +321,7 @@ public class HttpGenerator
*/
public void setResponse(int status, String reason)
{
if (_state != State.START)
if (_state != State.START)
throw new IllegalStateException("STATE!=START");
_method=null;
_status = status;
@ -359,13 +350,13 @@ public class HttpGenerator
return _contentLength>=0 && _contentPrepared>=_contentLength;
}
/* ------------------------------------------------------------ */
public long getContentWritten()
{
return _contentPrepared;
}
/* ------------------------------------------------------------ */
public boolean isRequest()
{
@ -377,7 +368,7 @@ public class HttpGenerator
{
return _method==null;
}
/* ------------------------------------------------------------ */
public Result generate(ByteBuffer header, ByteBuffer chunk, ByteBuffer buffer, ByteBuffer content, Action action)
@ -387,7 +378,7 @@ public class HttpGenerator
return result;
if (action==null)
action=Action.PREPARE;
// Do we have content to handle
if (BufferUtil.hasContent(content))
{
@ -426,10 +417,10 @@ public class HttpGenerator
return Result.NEED_BUFFER;
// Copy the content
_contentPrepared+=BufferUtil.put(content,buffer);
_contentPrepared+=BufferUtil.flipPutFlip(content,buffer);
// are we full?
if (BufferUtil.isAtCapacity(buffer))
if (BufferUtil.isFull(buffer))
{
if (isCommitted())
{
@ -448,7 +439,7 @@ public class HttpGenerator
}
}
}
// Handle the actions
if (result==Result.OK)
{
@ -609,15 +600,23 @@ public class HttpGenerator
if (BufferUtil.hasContent(buffer))
result=Result.FLUSH;
else
{
if (!_persistent)
result=Result.SHUTDOWN_OUT;
_state=State.END;
}
}
return result;
case END:
if (!_persistent)
result=Result.SHUTDOWN_OUT;
return Result.OK;
default:
throw new IllegalStateException();
}
}
}
finally
{
@ -632,7 +631,7 @@ public class HttpGenerator
// if we need CRLF add this to header
if (_needCRLF)
BufferUtil.putCRLF(chunk);
// Add the chunk size to the header
if (remaining>0)
{
@ -646,14 +645,14 @@ public class HttpGenerator
_needCRLF=false;
}
}
/* ------------------------------------------------------------ */
private void generateRequestLine(ByteBuffer header)
{
header.put(_method);
header.put((byte)' ');
header.put(_uri);
header.put(_uri);
switch(_version)
{
case HTTP_1_0:
@ -663,7 +662,7 @@ public class HttpGenerator
}
header.put(HttpTokens.CRLF);
}
/* ------------------------------------------------------------ */
private void generateResponseLine(ByteBuffer header)
{
@ -734,7 +733,7 @@ public class HttpGenerator
long length = field.getLongValue();
if (length>=0)
{
long prepared=_contentPrepared+BufferUtil.remaining(content);
long prepared=_contentPrepared+BufferUtil.length(content);
if (length < prepared || last && length != prepared)
{
LOG.warn("Incorrect ContentLength: "+length+"!="+prepared);
@ -744,7 +743,7 @@ public class HttpGenerator
}
else
{
// write the field to the header
// write the field to the header
header.put(HttpHeader.CONTENT_LENGTH.toBytesColonSpace());
BufferUtil.putDecLong(header,length);
BufferUtil.putCRLF(header);
@ -890,7 +889,7 @@ public class HttpGenerator
else if (last)
{
// we have seen all the _content there is
_contentLength = _contentPrepared+BufferUtil.remaining(content);
_contentLength = _contentPrepared+BufferUtil.length(content);
if (!content_length && (isResponse() || _contentLength>0 || content_type ) && !_noContent)
{
// known length but not actually set.
@ -1003,7 +1002,7 @@ public class HttpGenerator
return status._reason;
return null;
}
/* ------------------------------------------------------------------------------- */
@Override
public String toString()

View File

@ -4,18 +4,17 @@
// 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
// 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.
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.http;
import java.nio.ByteBuffer;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.StringMap;
import org.eclipse.jetty.util.StringUtil;
@ -103,7 +102,7 @@ public enum HttpHeader
SET_COOKIE2("Set-Cookie2"),
MIME_VERSION("MIME-Version"),
IDENTITY("identity"),
UNKNOWN("::UNKNOWN::");
@ -115,7 +114,7 @@ public enum HttpHeader
if (header!=UNKNOWN)
CACHE.put(header.toString(),header);
}
private final String _string;
private final byte[] _bytes;
private final byte[] _bytesColonSpace;
@ -141,7 +140,7 @@ public enum HttpHeader
{
return _bytes;
}
/* ------------------------------------------------------------ */
public byte[] toBytesColonSpace()
{
@ -149,11 +148,12 @@ public enum HttpHeader
}
/* ------------------------------------------------------------ */
@Override
public String toString()
{
return _string;
}
public static void main (String[] args)
{
for (HttpHeader h : HttpHeader.values())

View File

@ -4,11 +4,11 @@
// 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
// 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.
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.http;
@ -23,7 +23,7 @@ import org.eclipse.jetty.util.StringMap;
/**
*
*/
public enum HttpHeaderValue
public enum HttpHeaderValue
{
CLOSE("close"),
CHUNKED("chunked"),
@ -64,17 +64,18 @@ public enum HttpHeaderValue
}
/* ------------------------------------------------------------ */
@Override
public String toString()
{
return _string;
}
/* ------------------------------------------------------------ */
private static EnumSet<HttpHeader> __known =
private static EnumSet<HttpHeader> __known =
EnumSet.of(HttpHeader.CONNECTION,
HttpHeader.TRANSFER_ENCODING,
HttpHeader.CONTENT_ENCODING);
/* ------------------------------------------------------------ */
public static boolean hasKnownValues(HttpHeader header)
{

View File

@ -4,24 +4,23 @@
// 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
// 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.
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.http;
import java.nio.ByteBuffer;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.StringMap;
import org.eclipse.jetty.util.StringUtil;
/* ------------------------------------------------------------------------------- */
/**
/**
*/
public enum HttpMethod
{
@ -59,7 +58,7 @@ public enum HttpMethod
{
return _bytes;
}
/* ------------------------------------------------------------ */
public ByteBuffer toBuffer()
{

View File

@ -58,7 +58,7 @@ public class HttpParser
private final ResponseHandler _responseHandler;
private HttpHeader _header;
private HttpHeaderValue _value;
private int _responseStatus;
private int _responseStatus;
private boolean _persistent;
/* ------------------------------------------------------------------------------- */
@ -193,18 +193,18 @@ public class HttpParser
break;
}
}
/* ------------------------------------------------------------------------------- */
/**
* Parse until next Event.
* @return an indication of progress
* @return an indication of progress
*/
public boolean parseNext(ByteBuffer buffer) throws IOException
{
int start=-1;
State startState=null;
try
{
if (_state == State.END)
@ -221,9 +221,9 @@ public class HttpParser
byte ch;
int length=-1;
boolean at_next=false;
while (_state.ordinal()<State.END.ordinal() && buffer.hasRemaining() && !at_next)
{
{
ch=buffer.get();
if (_eol == HttpTokens.CARRIAGE_RETURN && ch == HttpTokens.LINE_FEED)
@ -307,7 +307,7 @@ public class HttpParser
else if (ch < HttpTokens.SPACE && ch>=0)
{
at_next|=_responseHandler.startResponse(_field0, _responseStatus, null);
_eol=ch;
_state=State.HEADER;
_field0=_field1=null;
@ -372,7 +372,7 @@ public class HttpParser
HttpVersion v=HttpVersion.CACHE.get(buffer,start,buffer.position()-start-1);
String version=v==null?BufferUtil.toString(buffer,start,buffer.position()-start-1,StringUtil.__ISO_8859_1_CHARSET):v.toString();
start=-1;
at_next|=_requestHandler.startRequest(_field0, _field1, version);
_eol=ch;
_persistent=HttpVersion.HTTP_1_1==v;
@ -387,7 +387,7 @@ public class HttpParser
{
String reason=BufferUtil.toString(buffer,start,buffer.position()-start-1,StringUtil.__ISO_8859_1_CHARSET);
start=-1;
at_next|=_responseHandler.startResponse(_field0, _responseStatus, reason);
_eol=ch;
_state=State.HEADER;
@ -492,7 +492,7 @@ public class HttpParser
{
_eol=ch;
_contentPosition=0;
// End of headers!
// work out the _content demarcation
if (_contentLength == HttpTokens.UNKNOWN_CONTENT)
@ -556,7 +556,7 @@ public class HttpParser
start=length=-1;
_state=State.HEADER;
break;
case HttpTokens.COLON:
_header=HttpHeader.CACHE.get(buffer,start,length);
_field0=_header==null?BufferUtil.toString(buffer,start,length,StringUtil.__ISO_8859_1_CHARSET):_header.toString();
@ -586,7 +586,7 @@ public class HttpParser
start=length=-1;
_state=State.HEADER;
break;
case HttpTokens.COLON:
_header=HttpHeader.CACHE.get(buffer,start,length);
_field0=_header==null?BufferUtil.toString(buffer,start,length,StringUtil.__ISO_8859_1_CHARSET):_header.toString();
@ -705,7 +705,7 @@ public class HttpParser
continue;
}
_eol=0;
switch (_state)
{
case EOF_CONTENT:
@ -820,14 +820,14 @@ public class HttpParser
{
_state=State.CHUNKED_CONTENT;
}
else
else
{
chunk=buffer.asReadOnlyBuffer();
if (chunk.remaining() > remaining)
chunk.limit(chunk.position()+remaining);
remaining=chunk.remaining();
_contentPosition += remaining;
_chunkPosition += remaining;
buffer.position(buffer.position()+remaining);
@ -837,7 +837,7 @@ public class HttpParser
}
case SEEKING_EOF:
{
{
buffer.clear().limit(0);
break;
}
@ -859,11 +859,11 @@ public class HttpParser
{
buffer.position(start);
_state=startState;
}
}
}
}
/* ------------------------------------------------------------------------------- */
public boolean onEOF() throws IOException
@ -935,7 +935,7 @@ public class HttpParser
/* ------------------------------------------------------------ */
/* ------------------------------------------------------------ */
/* Event Handler interface
* These methods return true if they want parsing to return to
* These methods return true if they want parsing to return to
* the caller.
*/
public interface EventHandler
@ -962,7 +962,7 @@ public class HttpParser
public abstract boolean startRequest(String method, String uri, String version)
throws IOException;
}
public interface ResponseHandler extends EventHandler
{
/**

View File

@ -4,11 +4,11 @@
// 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
// 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.
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.http;
@ -19,10 +19,10 @@ import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.StringMap;
/* ------------------------------------------------------------------------------- */
/**
/**
*/
public enum HttpScheme
{
{
HTTP("http"),
HTTPS("https"),
WS("ws"),
@ -53,8 +53,9 @@ public enum HttpScheme
}
/* ------------------------------------------------------------ */
@Override
public String toString()
{
return _string;
}
}
}

View File

@ -4,11 +4,11 @@
// 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
// 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.
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.http;
@ -33,5 +33,5 @@ public interface HttpTokens
public static final int EOF_CONTENT= -1;
public static final int NO_CONTENT= 0;
}

View File

@ -97,7 +97,7 @@ public class HttpURI
{
parse2(raw,offset,length);
}
public HttpURI(URI uri)
{
parse(uri.toASCIIString());
@ -125,7 +125,6 @@ public class HttpURI
int i=offset;
int e=offset+length;
int state=AUTH;
int m=offset;
_end=offset+length;
_scheme=offset;
_authority=offset;
@ -498,17 +497,17 @@ public class HttpURI
return null;
int l=_authority-_scheme;
if (l==5 &&
_raw[_scheme]=='h' &&
_raw[_scheme+1]=='t' &&
_raw[_scheme+2]=='t' &&
_raw[_scheme+3]=='p' )
_raw[_scheme]=='h' &&
_raw[_scheme+1]=='t' &&
_raw[_scheme+2]=='t' &&
_raw[_scheme+3]=='p' )
return HttpScheme.HTTP.toString();
if (l==6 &&
_raw[_scheme]=='h' &&
_raw[_scheme+1]=='t' &&
_raw[_scheme+2]=='t' &&
_raw[_scheme+3]=='p' &&
_raw[_scheme+4]=='s' )
_raw[_scheme]=='h' &&
_raw[_scheme+1]=='t' &&
_raw[_scheme+2]=='t' &&
_raw[_scheme+3]=='p' &&
_raw[_scheme+4]=='s' )
return HttpScheme.HTTPS.toString();
return toUtf8String(_scheme,_authority-_scheme-1);
@ -639,8 +638,8 @@ public class HttpURI
}
public void decodeQueryTo(MultiMap parameters, String encoding)
throws UnsupportedEncodingException
{
throws UnsupportedEncodingException
{
if (_query==_fragment)
return;
@ -648,7 +647,7 @@ public class HttpURI
UrlEncoded.decodeUtf8To(_raw,_query+1,_fragment-_query-1,parameters);
else
UrlEncoded.decodeTo(toUtf8String(_query+1,_fragment-_query-1),parameters,encoding);
}
}
public void clear()
{

View File

@ -4,18 +4,17 @@
// 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
// 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.
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.http;
import java.nio.ByteBuffer;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.StringMap;
import org.eclipse.jetty.util.StringUtil;
@ -26,7 +25,7 @@ public enum HttpVersion
HTTP_0_9("HTTP/0.9",9),
HTTP_1_0("HTTP/1.0",10),
HTTP_1_1("HTTP/1.1",11);
/* ------------------------------------------------------------ */
public final static StringMap<HttpVersion> CACHE= new StringMap<HttpVersion>(true);
static
@ -34,7 +33,7 @@ public enum HttpVersion
for (HttpVersion version : HttpVersion.values())
CACHE.put(version.toString(),version);
}
private final String _string;
private final byte[] _bytes;
private final ByteBuffer _buffer;
@ -60,7 +59,7 @@ public enum HttpVersion
{
return _buffer.asReadOnlyBuffer();
}
/* ------------------------------------------------------------ */
public int getVerion()
{
@ -68,6 +67,7 @@ public enum HttpVersion
}
/* ------------------------------------------------------------ */
@Override
public String toString()
{
return _string;

View File

@ -4,11 +4,11 @@
// 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
// 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.
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.http;
@ -28,7 +28,7 @@ import org.eclipse.jetty.util.log.Logger;
/* ------------------------------------------------------------ */
/**
/**
*
*/
public class MimeTypes
@ -71,6 +71,7 @@ public class MimeTypes
}
/* ------------------------------------------------------------ */
@Override
public String toString()
{
return _string;
@ -80,18 +81,18 @@ public class MimeTypes
/* ------------------------------------------------------------ */
private static final Logger LOG = Log.getLogger(MimeTypes.class);
private final static StringMap<MimeTypes.Type> CACHE= new StringMap<MimeTypes.Type>(true);
private final static StringMap<ByteBuffer> TYPES= new StringMap<ByteBuffer>(true);
private final static StringMap<ByteBuffer> TYPES= new StringMap<ByteBuffer>(true);
private final static Map<String,ByteBuffer> __dftMimeMap = new HashMap<String,ByteBuffer>();
private final static Map<String,String> __encodings = new HashMap<String,String>();
static
{
for (MimeTypes.Type type : MimeTypes.Type.values())
{
CACHE.put(type.toString(),type);
TYPES.put(type.toString(),type.toBuffer());
int charset=type.toString().indexOf(";charset=");
if (charset>0)
{
@ -99,7 +100,7 @@ public class MimeTypes
TYPES.put(type.toString().replace(";charset=","; charset="),type.toBuffer());
}
}
try
{
ResourceBundle mime = ResourceBundle.getBundle("org/eclipse/jetty/http/mime");
@ -133,13 +134,13 @@ public class MimeTypes
LOG.debug(e);
}
}
/* ------------------------------------------------------------ */
private final Map<String,ByteBuffer> _mimeMap=new HashMap<String,ByteBuffer>();
/* ------------------------------------------------------------ */
/** Constructor.
*/
@ -163,7 +164,7 @@ public class MimeTypes
if (mimeMap!=null)
{
for (String ext : mimeMap.keySet())
_mimeMap.put(StringUtil.asciiToLowerCase(ext),normalizeMimeType(mimeMap.get(ext)));
_mimeMap.put(StringUtil.asciiToLowerCase(ext),normalizeMimeType(mimeMap.get(ext)));
}
}
@ -199,8 +200,8 @@ public class MimeTypes
{
if (_mimeMap!=null)
type=_mimeMap.get("*");
if (type==null)
type=__dftMimeMap.get("*");
if (type==null)
type=__dftMimeMap.get("*");
}
return type;
@ -222,13 +223,13 @@ public class MimeTypes
MimeTypes.Type t =CACHE.get(type);
if (t!=null)
return t.toBuffer();
return BufferUtil.toBuffer(StringUtil.asciiToLowerCase(type));
}
/* ------------------------------------------------------------ */
public static String getCharsetFromContentType(ByteBuffer value)
{
{
int i=value.position();
int end=value.limit();
int state=0;
@ -237,14 +238,14 @@ public class MimeTypes
for (;i<end;i++)
{
byte b = value.get(i);
if (quote && state!=10)
{
if ('"'==b)
quote=false;
continue;
}
switch(state)
{
case 0:
@ -266,11 +267,11 @@ public class MimeTypes
case 7: if ('t'==b) state=8; else state=0;break;
case 8: if ('='==b) state=9; else if (' '!=b) state=0; break;
case 9:
if (' '==b)
case 9:
if (' '==b)
break;
if ('"'==b)
if ('"'==b)
{
quote=true;
start=i+1;
@ -280,17 +281,17 @@ public class MimeTypes
start=i;
state=10;
break;
case 10:
if (!quote && (';'==b || ' '==b )||
(quote && '"'==b ))
(quote && '"'==b ))
return StringUtil.normalizeCharset(value,start,i-start);
}
}
}
if (state==10)
return StringUtil.normalizeCharset(value,start,i-start);
return (String)__encodings.get(value);
return __encodings.get(value);
}
}

View File

@ -131,13 +131,13 @@ public class PathMap<O> extends HashMap<String,O>
{
String str = pathSpec.toString();
if ("".equals(str.trim()))
{
{
MappedEntry entry = new MappedEntry("",object);
entry.setMapped("");
_exactMap.put("", entry);
return super.put("", object);
}
StringTokenizer tok = new StringTokenizer(str,__pathSpecSeparators);
O old =null;
@ -175,7 +175,7 @@ public class PathMap<O> extends HashMap<String,O>
{
_default=entry;
_defaultSingletonList=
Collections.singletonList(_default);
Collections.singletonList(_default);
}
}
else
@ -216,15 +216,15 @@ public class PathMap<O> extends HashMap<String,O>
return null;
int l=path.length();
//special case
if (l == 1 && path.charAt(0)=='/')
{
entry = _exactMap.get("");
if (entry != null)
return (MappedEntry)entry;
return entry;
}
// try exact match
entry=_exactMap.get(path,0,l);
if (entry!=null)
@ -331,8 +331,8 @@ public class PathMap<O> extends HashMap<String,O>
*/
public boolean containsMatch(String path)
{
MappedEntry match = getMatch(path);
return match!=null && !match.equals(_default);
MappedEntry match = getMatch(path);
return match!=null && !match.equals(_default);
}
/* --------------------------------------------------------------- */
@ -380,18 +380,18 @@ public class PathMap<O> extends HashMap<String,O>
* @return true if match.
*/
public static boolean match(String pathSpec, String path)
throws IllegalArgumentException
{
throws IllegalArgumentException
{
return match(pathSpec, path, false);
}
}
/* --------------------------------------------------------------- */
/**
* @return true if match.
*/
public static boolean match(String pathSpec, String path, boolean noDefault)
throws IllegalArgumentException
{
throws IllegalArgumentException
{
char c = pathSpec.charAt(0);
if (c=='/')
{
@ -403,9 +403,9 @@ public class PathMap<O> extends HashMap<String,O>
}
else if (c=='*')
return path.regionMatches(path.length()-pathSpec.length()+1,
pathSpec,1,pathSpec.length()-1);
pathSpec,1,pathSpec.length()-1);
return false;
}
}
/* --------------------------------------------------------------- */
private static boolean isPathWildcardMatch(String pathSpec, String path)
@ -443,7 +443,7 @@ public class PathMap<O> extends HashMap<String,O>
else if (c=='*')
{
if (path.regionMatches(path.length()-(pathSpec.length()-1),
pathSpec,1,pathSpec.length()-1))
pathSpec,1,pathSpec.length()-1))
return path;
}
return null;
@ -457,7 +457,7 @@ public class PathMap<O> extends HashMap<String,O>
{
if ("".equals(pathSpec))
return path; //servlet 3 spec sec 12.2 will be '/'
char c = pathSpec.charAt(0);
if (c=='/')
@ -490,8 +490,8 @@ public class PathMap<O> extends HashMap<String,O>
* @return base plus path with pathspec removed
*/
public static String relativePath(String base,
String pathSpec,
String path )
String pathSpec,
String path )
{
String info=pathInfo(pathSpec,path);
if (info==null)
@ -527,16 +527,19 @@ public class PathMap<O> extends HashMap<String,O>
this.value=value;
}
@Override
public String getKey()
{
return key;
}
@Override
public O getValue()
{
return value;
}
@Override
public O setValue(O o)
{
throw new UnsupportedOperationException();

View File

@ -4,11 +4,11 @@
// 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
// 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.
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
@ -36,8 +36,8 @@ public class GzipResponseWrapper extends HttpServletResponseWrapper
{
public static final int DEFAULT_BUFFER_SIZE = 8192;
public static final int DEFAULT_MIN_GZIP_SIZE = 256;
private HttpServletRequest _request;
private final HttpServletRequest _request;
private Set<String> _mimeTypes;
private int _bufferSize=DEFAULT_BUFFER_SIZE;
private int _minGzipSize=DEFAULT_MIN_GZIP_SIZE;
@ -74,6 +74,7 @@ public class GzipResponseWrapper extends HttpServletResponseWrapper
/**
* @see javax.servlet.ServletResponseWrapper#setBufferSize(int)
*/
@Override
public void setBufferSize(int bufferSize)
{
_bufferSize = bufferSize;
@ -94,6 +95,7 @@ public class GzipResponseWrapper extends HttpServletResponseWrapper
/**
* @see javax.servlet.ServletResponseWrapper#setContentType(java.lang.String)
*/
@Override
public void setContentType(String ct)
{
super.setContentType(ct);
@ -105,9 +107,9 @@ public class GzipResponseWrapper extends HttpServletResponseWrapper
ct=ct.substring(0,colon);
}
if ((_gzStream==null || _gzStream._out==null) &&
(_mimeTypes==null && "application/gzip".equalsIgnoreCase(ct) ||
_mimeTypes!=null && (ct==null||!_mimeTypes.contains(StringUtil.asciiToLowerCase(ct)))))
if ((_gzStream==null || _gzStream._out==null) &&
(_mimeTypes==null && "application/gzip".equalsIgnoreCase(ct) ||
_mimeTypes!=null && (ct==null||!_mimeTypes.contains(StringUtil.asciiToLowerCase(ct)))))
{
noGzip();
}
@ -117,6 +119,7 @@ public class GzipResponseWrapper extends HttpServletResponseWrapper
/**
* @see javax.servlet.http.HttpServletResponseWrapper#setStatus(int, java.lang.String)
*/
@Override
public void setStatus(int sc, String sm)
{
super.setStatus(sc,sm);
@ -128,6 +131,7 @@ public class GzipResponseWrapper extends HttpServletResponseWrapper
/**
* @see javax.servlet.http.HttpServletResponseWrapper#setStatus(int)
*/
@Override
public void setStatus(int sc)
{
super.setStatus(sc);
@ -139,11 +143,12 @@ public class GzipResponseWrapper extends HttpServletResponseWrapper
/**
* @see javax.servlet.ServletResponseWrapper#setContentLength(int)
*/
@Override
public void setContentLength(int length)
{
setContentLength((long)length);
}
/* ------------------------------------------------------------ */
protected void setContentLength(long length)
{
@ -168,6 +173,7 @@ public class GzipResponseWrapper extends HttpServletResponseWrapper
/**
* @see javax.servlet.http.HttpServletResponseWrapper#addHeader(java.lang.String, java.lang.String)
*/
@Override
public void addHeader(String name, String value)
{
if ("content-length".equalsIgnoreCase(name))
@ -177,11 +183,11 @@ public class GzipResponseWrapper extends HttpServletResponseWrapper
_gzStream.setContentLength(_contentLength);
}
else if ("content-type".equalsIgnoreCase(name))
{
{
setContentType(value);
}
else if ("content-encoding".equalsIgnoreCase(name))
{
{
super.addHeader(name,value);
if (!isCommitted())
{
@ -196,6 +202,7 @@ public class GzipResponseWrapper extends HttpServletResponseWrapper
/**
* @see javax.servlet.http.HttpServletResponseWrapper#setHeader(java.lang.String, java.lang.String)
*/
@Override
public void setHeader(String name, String value)
{
if ("content-length".equalsIgnoreCase(name))
@ -203,11 +210,11 @@ public class GzipResponseWrapper extends HttpServletResponseWrapper
setContentLength(Long.parseLong(value));
}
else if ("content-type".equalsIgnoreCase(name))
{
{
setContentType(value);
}
else if ("content-encoding".equalsIgnoreCase(name))
{
{
super.setHeader(name,value);
if (!isCommitted())
{
@ -222,6 +229,7 @@ public class GzipResponseWrapper extends HttpServletResponseWrapper
/**
* @see javax.servlet.http.HttpServletResponseWrapper#setIntHeader(java.lang.String, int)
*/
@Override
public void setIntHeader(String name, int value)
{
if ("content-length".equalsIgnoreCase(name))
@ -238,6 +246,7 @@ public class GzipResponseWrapper extends HttpServletResponseWrapper
/**
* @see javax.servlet.ServletResponseWrapper#flushBuffer()
*/
@Override
public void flushBuffer() throws IOException
{
if (_writer!=null)
@ -252,6 +261,7 @@ public class GzipResponseWrapper extends HttpServletResponseWrapper
/**
* @see javax.servlet.ServletResponseWrapper#reset()
*/
@Override
public void reset()
{
super.reset();
@ -262,11 +272,12 @@ public class GzipResponseWrapper extends HttpServletResponseWrapper
_noGzip=false;
_contentLength=-1;
}
/* ------------------------------------------------------------ */
/**
* @see javax.servlet.ServletResponseWrapper#resetBuffer()
*/
@Override
public void resetBuffer()
{
super.resetBuffer();
@ -275,31 +286,34 @@ public class GzipResponseWrapper extends HttpServletResponseWrapper
_writer=null;
_gzStream=null;
}
/* ------------------------------------------------------------ */
/**
* @see javax.servlet.http.HttpServletResponseWrapper#sendError(int, java.lang.String)
*/
@Override
public void sendError(int sc, String msg) throws IOException
{
resetBuffer();
super.sendError(sc,msg);
}
/* ------------------------------------------------------------ */
/**
* @see javax.servlet.http.HttpServletResponseWrapper#sendError(int)
*/
@Override
public void sendError(int sc) throws IOException
{
resetBuffer();
super.sendError(sc);
}
/* ------------------------------------------------------------ */
/**
* @see javax.servlet.http.HttpServletResponseWrapper#sendRedirect(java.lang.String)
*/
@Override
public void sendRedirect(String location) throws IOException
{
resetBuffer();
@ -310,6 +324,7 @@ public class GzipResponseWrapper extends HttpServletResponseWrapper
/**
* @see javax.servlet.ServletResponseWrapper#getOutputStream()
*/
@Override
public ServletOutputStream getOutputStream() throws IOException
{
if (_gzStream==null)
@ -319,36 +334,37 @@ public class GzipResponseWrapper extends HttpServletResponseWrapper
setContentLength(_contentLength);
return getResponse().getOutputStream();
}
_gzStream=newGzipStream(_request,(HttpServletResponse)getResponse(),_contentLength,_bufferSize,_minGzipSize);
}
else if (_writer!=null)
throw new IllegalStateException("getWriter() called");
return _gzStream;
return _gzStream;
}
/* ------------------------------------------------------------ */
/**
* @see javax.servlet.ServletResponseWrapper#getWriter()
*/
@Override
public PrintWriter getWriter() throws IOException
{
if (_writer==null)
{
{
if (_gzStream!=null)
throw new IllegalStateException("getOutputStream() called");
if (getResponse().isCommitted() || _noGzip)
{
setContentLength(_contentLength);
return getResponse().getWriter();
}
_gzStream=newGzipStream(_request,(HttpServletResponse)getResponse(),_contentLength,_bufferSize,_minGzipSize);
_writer=newWriter(_gzStream,getCharacterEncoding());
}
return _writer;
return _writer;
}
/* ------------------------------------------------------------ */
@ -370,7 +386,7 @@ public class GzipResponseWrapper extends HttpServletResponseWrapper
}
}
}
/* ------------------------------------------------------------ */
/**
* Finish.
@ -384,7 +400,7 @@ public class GzipResponseWrapper extends HttpServletResponseWrapper
if (_gzStream!=null)
_gzStream.finish();
}
/* ------------------------------------------------------------ */
/**
* Allows derived implementations to replace GzipStream implementation.

View File

@ -4,11 +4,11 @@
// 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
// 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.
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
@ -97,11 +97,12 @@ public class GzipStream extends ServletOutputStream
_response.setHeader("Content-Length",Long.toString(_contentLength));
}
}
/* ------------------------------------------------------------ */
/**
* @see java.io.OutputStream#flush()
*/
@Override
public void flush() throws IOException
{
if (_out==null || _bOut!=null)
@ -111,7 +112,7 @@ public class GzipStream extends ServletOutputStream
else
doGzip();
}
_out.flush();
}
@ -119,12 +120,13 @@ public class GzipStream extends ServletOutputStream
/**
* @see java.io.OutputStream#close()
*/
@Override
public void close() throws IOException
{
if (_closed)
return;
if (_request.getAttribute("javax.servlet.include.request_uri")!=null)
if (_request.getAttribute("javax.servlet.include.request_uri")!=null)
flush();
else
{
@ -148,7 +150,7 @@ public class GzipStream extends ServletOutputStream
_out.close();
_closed=true;
}
}
}
/**
* Finish.
@ -166,21 +168,22 @@ public class GzipStream extends ServletOutputStream
else
doGzip();
}
if (_gzOut!=null && !_closed)
{
_closed=true;
_gzOut.close();
}
}
}
}
/* ------------------------------------------------------------ */
/**
* @see java.io.OutputStream#write(int)
*/
@Override
public void write(int b) throws IOException
{
{
checkOut(1);
_out.write(b);
}
@ -189,6 +192,7 @@ public class GzipStream extends ServletOutputStream
/**
* @see java.io.OutputStream#write(byte[])
*/
@Override
public void write(byte b[]) throws IOException
{
checkOut(b.length);
@ -199,12 +203,13 @@ public class GzipStream extends ServletOutputStream
/**
* @see java.io.OutputStream#write(byte[], int, int)
*/
@Override
public void write(byte b[], int off, int len) throws IOException
{
checkOut(len);
_out.write(b,off,len);
}
/**
* Sets the content encoding gzip.
*
@ -215,7 +220,7 @@ public class GzipStream extends ServletOutputStream
_response.setHeader("Content-Encoding", "gzip");
return _response.containsHeader("Content-Encoding");
}
/**
* Do gzip.
*
@ -223,11 +228,11 @@ public class GzipStream extends ServletOutputStream
*/
public void doGzip() throws IOException
{
if (_gzOut==null)
if (_gzOut==null)
{
if (_response.isCommitted())
throw new IllegalStateException();
if (setContentEncodingGzip())
{
_out=_gzOut=new GZIPOutputStream(_response.getOutputStream(),_bufferSize);
@ -238,11 +243,11 @@ public class GzipStream extends ServletOutputStream
_bOut=null;
}
}
else
else
doNotGzip();
}
}
/**
* Do not gzip.
*
@ -250,7 +255,7 @@ public class GzipStream extends ServletOutputStream
*/
public void doNotGzip() throws IOException
{
if (_gzOut!=null)
if (_gzOut!=null)
throw new IllegalStateException();
if (_out==null || _bOut!=null )
{
@ -262,20 +267,20 @@ public class GzipStream extends ServletOutputStream
if (_bOut!=null)
_out.write(_bOut.getBuf(),0,_bOut.getCount());
_bOut=null;
}
}
}
/**
* Check out.
*
* @param length the length
* @throws IOException Signals that an I/O exception has occurred.
*/
private void checkOut(int length) throws IOException
private void checkOut(int length) throws IOException
{
if (_closed)
if (_closed)
throw new IOException("CLOSED");
if (_out==null)
{
if (_response.isCommitted() || (_contentLength>=0 && _contentLength<_minGzipSize))

View File

@ -5,6 +5,7 @@ package org.eclipse.jetty.http.ssl;
/**
* @deprecated Use org.eclipse.jetty.util.ssl.SslContextFactory
*/
@Deprecated
public class SslContextFactory extends org.eclipse.jetty.util.ssl.SslContextFactory
{
public SslContextFactory()

View File

@ -62,7 +62,7 @@ public class HttpFieldsTest
assertEquals(e.nextElement(), "value0");
assertEquals(false, e.hasMoreElements());
}
@Test
public void testGet() throws Exception
{
@ -76,7 +76,7 @@ public class HttpFieldsTest
assertEquals("value1",header.getStringField("name1"));
assertEquals("value1",header.getStringField("Name1"));
}
@Test
public void testCRLF() throws Exception
{
@ -110,13 +110,13 @@ public class HttpFieldsTest
header.putTo(buffer);
buffer.flip();
String out = BufferUtil.toString(buffer);
Assert.assertThat(out,JUnitMatchers.containsString(HttpHeader.CONNECTION+": "+HttpHeaderValue.KEEP_ALIVE));
Assert.assertThat(out,JUnitMatchers.containsString(HttpHeader.TRANSFER_ENCODING+": "+HttpHeaderValue.CHUNKED));
Assert.assertThat(out,JUnitMatchers.containsString(HttpHeader.CONTENT_ENCODING+": "+HttpHeaderValue.GZIP));
}
@Test
@ -257,8 +257,8 @@ public class HttpFieldsTest
assertEquals("everything=value;Path=path;Domain=domain;Expires=Thu, 01-Jan-1970 00:00:00 GMT;Secure;HttpOnly",e.nextElement());
assertFalse(e.hasMoreElements());
assertEquals("Thu, 01 Jan 1970 00:00:00 GMT",fields.getStringField("Expires"));
fields.clear();
fields.addSetCookie("ev erything","va lue","do main","pa th",1,"co mment",true,true,2);
String setCookie=fields.getStringField("Set-Cookie");
@ -273,7 +273,7 @@ public class HttpFieldsTest
fields.addSetCookie("name","v a l u e",null,null,-1,null,false,false,0);
setCookie=fields.getStringField("Set-Cookie");
assertEquals(17,setCookie.indexOf("Version=1"));
fields.clear();
fields.addSetCookie("json","{\"services\":[\"cwa\", \"aa\"]}",null,null,-1,null,false,false,-1);
assertEquals("json=\"{\\\"services\\\":[\\\"cwa\\\", \\\"aa\\\"]}\"",fields.getStringField("Set-Cookie"));
@ -291,12 +291,12 @@ public class HttpFieldsTest
e=fields.getValues("Set-Cookie");
assertEquals("name=more;Domain=domain",e.nextElement());
assertEquals("foo=bob;Domain=domain",e.nextElement());
fields=new HttpFields(0);
fields.addSetCookie("name","value==",null,null,-1,null,false,false,0);
setCookie=fields.getStringField("Set-Cookie");
assertEquals("name=value==",setCookie);
}
private Set<String> enum2set(Enumeration<String> e)
@ -359,10 +359,10 @@ public class HttpFieldsTest
fields.putDateField("Dminus",-1);
assertEquals("Wed, 31 Dec 1969 23:59:59 GMT",fields.getStringField("Dminus"));
fields.putDateField("Dminus",-1000);
assertEquals("Wed, 31 Dec 1969 23:59:59 GMT",fields.getStringField("Dminus"));
fields.putDateField("Dancient",Long.MIN_VALUE);
assertEquals("Sun, 02 Dec 55 16:47:04 GMT",fields.getStringField("Dancient"));
}

View File

@ -19,19 +19,11 @@ import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.matchers.JUnitMatchers.containsString;
import java.io.IOException;
import java.nio.ByteBuffer;
import javax.swing.text.View;
import org.eclipse.jetty.http.HttpGenerator.Action;
import org.eclipse.jetty.io.ByteArrayEndPoint;
import org.eclipse.jetty.io.SimpleBuffers;
import org.eclipse.jetty.util.BufferUtil;
import org.hamcrest.CoreMatchers;
import org.junit.Assert;
import org.junit.Test;
import org.junit.matchers.JUnitMatchers;
public class HttpGeneratorClientTest
{
@ -50,30 +42,31 @@ public class HttpGeneratorClientTest
fields.add("User-Agent","test");
gen.setRequest(HttpMethod.GET,"/index.html",HttpVersion.HTTP_1_1);
HttpGenerator.Result
HttpGenerator.Result
result=gen.generate(null,null,null,null,Action.COMPLETE);
assertEquals(HttpGenerator.State.COMMITTING_COMPLETING,gen.getState());
assertEquals(HttpGenerator.Result.NEED_HEADER,result);
result=gen.generate(header,null,null,null,null);
assertEquals(HttpGenerator.Result.FLUSH,result);
String head = BufferUtil.toString(header);
BufferUtil.clear(header);
result=gen.generate(null,null,null,null,null);
assertEquals(HttpGenerator.Result.OK,result);
assertEquals(HttpGenerator.State.END,gen.getState());
assertEquals(0,gen.getContentWritten());
assertEquals(0,gen.getContentWritten());
assertThat(head,containsString("GET /index.html HTTP/1.1"));
assertThat(head,not(containsString("Content-Length")));
assertThat(head,not(containsString("Content-Length")));
}
@Test
public void testRequestWithSmallContent() throws Exception
{
String body="";
ByteBuffer header=BufferUtil.allocate(4096);
ByteBuffer buffer=BufferUtil.allocate(8096);
ByteBuffer content=BufferUtil.toBuffer("Hello World");
@ -86,18 +79,18 @@ public class HttpGeneratorClientTest
fields.add("Host","something");
fields.add("User-Agent","test");
HttpGenerator.Result
HttpGenerator.Result
result=gen.generate(null,null,null,content,null);
assertEquals(HttpGenerator.Result.NEED_BUFFER,result);
assertEquals(HttpGenerator.State.START,gen.getState());
result=gen.generate(null,null,buffer,content,null);
assertEquals(HttpGenerator.Result.OK,result);
assertEquals(HttpGenerator.State.START,gen.getState());
assertEquals("Hello World",BufferUtil.toString(buffer));
assertTrue(BufferUtil.isEmpty(content));
result=gen.generate(null,null,buffer,content1,null);
assertEquals(HttpGenerator.Result.OK,result);
assertEquals(HttpGenerator.State.START,gen.getState());
@ -107,33 +100,34 @@ public class HttpGeneratorClientTest
result=gen.generate(null,null,buffer,null,Action.COMPLETE);
assertEquals(HttpGenerator.Result.NEED_HEADER,result);
assertEquals(HttpGenerator.State.COMMITTING_COMPLETING,gen.getState());
result=gen.generate(header,null,buffer,null,null);
assertEquals(HttpGenerator.Result.FLUSH,result);
assertEquals(HttpGenerator.State.COMPLETING,gen.getState());
String head = BufferUtil.toString(header);
BufferUtil.clear(header);
String body = BufferUtil.toString(buffer);
body += BufferUtil.toString(buffer);
BufferUtil.clear(buffer);
result=gen.generate(null,null,buffer,null,null);
assertEquals(HttpGenerator.Result.OK,result);
assertEquals(HttpGenerator.State.END,gen.getState());
assertThat(head,containsString("POST /index.html HTTP/1.1"));
assertThat(head,containsString("Host: something"));
assertThat(head,containsString("Content-Length: 58"));
assertTrue(head.endsWith("\r\n\r\n"));
assertEquals("Hello World. The quick brown fox jumped over the lazy dog.",body);
assertEquals(58,gen.getContentWritten());
assertEquals(58,gen.getContentWritten());
}
@Test
public void testRequestWithChunkedContent() throws Exception
{
String body="";
ByteBuffer header=BufferUtil.allocate(4096);
ByteBuffer buffer=BufferUtil.allocate(16);
ByteBuffer content0=BufferUtil.toBuffer("Hello World! ");
@ -146,18 +140,18 @@ public class HttpGeneratorClientTest
fields.add("Host","something");
fields.add("User-Agent","test");
HttpGenerator.Result
HttpGenerator.Result
result=gen.generate(null,null,null,content0,null);
assertEquals(HttpGenerator.Result.NEED_BUFFER,result);
assertEquals(HttpGenerator.State.START,gen.getState());
result=gen.generate(null,null,buffer,content0,null);
assertEquals(HttpGenerator.Result.OK,result);
assertEquals(HttpGenerator.State.START,gen.getState());
assertEquals("Hello World! ",BufferUtil.toString(buffer));
assertEquals(0,content0.remaining());
result=gen.generate(null,null,buffer,content1,null);
assertEquals(HttpGenerator.Result.NEED_HEADER,result);
assertEquals(HttpGenerator.State.COMMITTING,gen.getState());
@ -173,7 +167,7 @@ public class HttpGeneratorClientTest
String head = BufferUtil.toString(header);
BufferUtil.clear(header);
String body = BufferUtil.toString(buffer);
body+=BufferUtil.toString(buffer);
BufferUtil.clear(buffer);
result=gen.generate(null,null,buffer,content1,null);
@ -187,7 +181,7 @@ public class HttpGeneratorClientTest
assertEquals("\r\n10\r\n",BufferUtil.toString(chunk));
assertEquals(" quick brown fox",BufferUtil.toString(buffer));
assertEquals(27,content1.remaining());
body += BufferUtil.toString(chunk)+BufferUtil.toString(buffer);
body+=BufferUtil.toString(chunk)+BufferUtil.toString(buffer);
BufferUtil.clear(chunk);
BufferUtil.clear(buffer);
@ -197,7 +191,7 @@ public class HttpGeneratorClientTest
assertEquals("\r\n10\r\n",BufferUtil.toString(chunk));
assertEquals(" jumped over the",BufferUtil.toString(buffer));
assertEquals(11,content1.remaining());
body += BufferUtil.toString(chunk)+BufferUtil.toString(buffer);
body+=BufferUtil.toString(chunk)+BufferUtil.toString(buffer);
BufferUtil.clear(chunk);
BufferUtil.clear(buffer);
@ -207,13 +201,13 @@ public class HttpGeneratorClientTest
assertEquals("",BufferUtil.toString(chunk));
assertEquals(" lazy dog. ",BufferUtil.toString(buffer));
assertEquals(0,content1.remaining());
result=gen.generate(null,chunk,buffer,null,Action.COMPLETE);
assertEquals(HttpGenerator.Result.FLUSH,result);
assertEquals(HttpGenerator.State.COMPLETING,gen.getState());
assertEquals("\r\nB\r\n",BufferUtil.toString(chunk));
assertEquals(" lazy dog. ",BufferUtil.toString(buffer));
body += BufferUtil.toString(chunk)+BufferUtil.toString(buffer);
body+=BufferUtil.toString(chunk)+BufferUtil.toString(buffer);
BufferUtil.clear(chunk);
BufferUtil.clear(buffer);
@ -222,15 +216,15 @@ public class HttpGeneratorClientTest
assertEquals(HttpGenerator.State.END,gen.getState());
assertEquals("\r\n0\r\n\r\n",BufferUtil.toString(chunk));
assertEquals(0,buffer.remaining());
body += BufferUtil.toString(chunk);
BufferUtil.toString(chunk);
BufferUtil.clear(chunk);
result=gen.generate(null,chunk,buffer,null,null);
assertEquals(HttpGenerator.Result.OK,result);
assertEquals(HttpGenerator.State.END,gen.getState());
assertEquals(59,gen.getContentWritten());
assertEquals(59,gen.getContentWritten());
// System.err.println(head+body);
assertThat(head,containsString("POST /index.html HTTP/1.1"));
@ -238,11 +232,13 @@ public class HttpGeneratorClientTest
assertThat(head,not(containsString("Content-Length")));
assertThat(head,containsString("Transfer-Encoding: chunked"));
assertTrue(head.endsWith("\r\n\r\n10\r\n"));
assertThat(body,containsString("dog"));
}
@Test
public void testRequestWithLargeChunkedContent() throws Exception
{
{
String body="";
ByteBuffer header=BufferUtil.allocate(4096);
ByteBuffer content0=BufferUtil.toBuffer("Hello Cruel World! ");
ByteBuffer content1=BufferUtil.toBuffer("The quick brown fox jumped over the lazy dog. ");
@ -255,26 +251,26 @@ public class HttpGeneratorClientTest
fields.add("Host","something");
fields.add("User-Agent","test");
HttpGenerator.Result
HttpGenerator.Result
result=gen.generate(null,null,null,content0,null);
assertEquals(HttpGenerator.Result.NEED_HEADER,result);
assertEquals(HttpGenerator.State.COMMITTING,gen.getState());
result=gen.generate(header,null,null,content0,null);
assertEquals(HttpGenerator.Result.FLUSH_CONTENT,result);
assertEquals(HttpGenerator.State.COMMITTED,gen.getState());
assertTrue(gen.isChunking());
String head = BufferUtil.toString(header);
BufferUtil.clear(header);
String body = BufferUtil.toString(content0);
body+=BufferUtil.toString(content0);
BufferUtil.clear(content0);
result=gen.generate(header,null,null,content0,null);
assertEquals(HttpGenerator.Result.OK,result);
assertEquals(HttpGenerator.State.COMMITTED,gen.getState());
result=gen.generate(null,null,null,content1,null);
assertEquals(HttpGenerator.Result.NEED_CHUNK,result);
assertEquals(HttpGenerator.State.COMMITTED,gen.getState());
@ -284,22 +280,22 @@ public class HttpGeneratorClientTest
assertEquals(HttpGenerator.Result.FLUSH_CONTENT,result);
assertEquals(HttpGenerator.State.COMMITTED,gen.getState());
assertEquals("\r\n2E\r\n",BufferUtil.toString(chunk));
body += BufferUtil.toString(chunk)+BufferUtil.toString(content1);
body+=BufferUtil.toString(chunk)+BufferUtil.toString(content1);
BufferUtil.clear(content1);
result=gen.generate(null,chunk,null,null,Action.COMPLETE);
assertEquals(HttpGenerator.Result.FLUSH,result);
assertEquals(HttpGenerator.State.END,gen.getState());
assertEquals("\r\n0\r\n\r\n",BufferUtil.toString(chunk));
body += BufferUtil.toString(chunk);
BufferUtil.toString(chunk);
result=gen.generate(null,chunk,null,null,null);
assertEquals(HttpGenerator.Result.OK,result);
assertEquals(HttpGenerator.State.END,gen.getState());
assertEquals(65,gen.getContentWritten());
assertEquals(65,gen.getContentWritten());
// System.err.println(head+body);
assertThat(head,containsString("POST /index.html HTTP/1.1"));
@ -307,12 +303,14 @@ public class HttpGeneratorClientTest
assertThat(head,not(containsString("Content-Length")));
assertThat(head,containsString("Transfer-Encoding: chunked"));
assertTrue(head.endsWith("\r\n\r\n13\r\n"));
assertThat(body,containsString("dog"));
}
@Test
public void testRequestWithKnownContent() throws Exception
{
String body="";
ByteBuffer header=BufferUtil.allocate(4096);
ByteBuffer buffer=BufferUtil.allocate(16);
ByteBuffer content0=BufferUtil.toBuffer("Hello World! ");
@ -326,19 +324,19 @@ public class HttpGeneratorClientTest
fields.add("User-Agent","test");
fields.add("Content-Length","59");
gen.setContentLength(59);
HttpGenerator.Result
HttpGenerator.Result
result=gen.generate(null,null,null,content0,null);
assertEquals(HttpGenerator.Result.NEED_BUFFER,result);
assertEquals(HttpGenerator.State.START,gen.getState());
result=gen.generate(null,null,buffer,content0,null);
assertEquals(HttpGenerator.Result.OK,result);
assertEquals(HttpGenerator.State.START,gen.getState());
assertEquals("Hello World! ",BufferUtil.toString(buffer));
assertEquals(0,content0.remaining());
result=gen.generate(null,null,buffer,content1,null);
assertEquals(HttpGenerator.Result.NEED_HEADER,result);
assertEquals(HttpGenerator.State.COMMITTING,gen.getState());
@ -354,7 +352,7 @@ public class HttpGeneratorClientTest
String head = BufferUtil.toString(header);
BufferUtil.clear(header);
String body = BufferUtil.toString(buffer);
body+=BufferUtil.toString(buffer);
BufferUtil.clear(buffer);
result=gen.generate(null,null,buffer,content1,null);
@ -362,7 +360,7 @@ public class HttpGeneratorClientTest
assertEquals(HttpGenerator.State.COMMITTED,gen.getState());
assertEquals(" quick brown fox",BufferUtil.toString(buffer));
assertEquals(27,content1.remaining());
body += BufferUtil.toString(buffer);
body+=BufferUtil.toString(buffer);
BufferUtil.clear(buffer);
result=gen.generate(null,null,buffer,content1,null);
@ -370,7 +368,7 @@ public class HttpGeneratorClientTest
assertEquals(HttpGenerator.State.COMMITTED,gen.getState());
assertEquals(" jumped over the",BufferUtil.toString(buffer));
assertEquals(11,content1.remaining());
body += BufferUtil.toString(buffer);
body+=BufferUtil.toString(buffer);
BufferUtil.clear(buffer);
result=gen.generate(null,null,buffer,content1,null);
@ -378,21 +376,21 @@ public class HttpGeneratorClientTest
assertEquals(HttpGenerator.State.COMMITTED,gen.getState());
assertEquals(" lazy dog. ",BufferUtil.toString(buffer));
assertEquals(0,content1.remaining());
result=gen.generate(null,null,buffer,null,Action.COMPLETE);
assertEquals(HttpGenerator.Result.FLUSH,result);
assertEquals(HttpGenerator.State.COMPLETING,gen.getState());
assertEquals(" lazy dog. ",BufferUtil.toString(buffer));
body += BufferUtil.toString(buffer);
body+=BufferUtil.toString(buffer);
BufferUtil.clear(buffer);
result=gen.generate(null,null,buffer,null,null);
assertEquals(HttpGenerator.Result.OK,result);
assertEquals(HttpGenerator.State.END,gen.getState());
assertEquals(0,buffer.remaining());
assertEquals(59,gen.getContentWritten());
assertEquals(59,gen.getContentWritten());
// System.err.println(head+body);
assertThat(head,containsString("POST /index.html HTTP/1.1"));
@ -400,11 +398,13 @@ public class HttpGeneratorClientTest
assertThat(head,containsString("Content-Length: 59"));
assertThat(head,not(containsString("chunked")));
assertTrue(head.endsWith("\r\n\r\n"));
assertThat(body,containsString("dog"));
}
@Test
public void testRequestWithKnownLargeContent() throws Exception
{
String body="";
ByteBuffer header=BufferUtil.allocate(4096);
ByteBuffer content0=BufferUtil.toBuffer("Hello World! ");
ByteBuffer content1=BufferUtil.toBuffer("The quick brown fox jumped over the lazy dog. ");
@ -418,9 +418,9 @@ public class HttpGeneratorClientTest
fields.add("User-Agent","test");
fields.add("Content-Length","59");
gen.setContentLength(59);
HttpGenerator.Result
HttpGenerator.Result
result=gen.generate(null,null,null,content0,null);
assertEquals(HttpGenerator.Result.NEED_HEADER,result);
assertEquals(HttpGenerator.State.COMMITTING,gen.getState());
@ -432,25 +432,25 @@ public class HttpGeneratorClientTest
String head = BufferUtil.toString(header);
BufferUtil.clear(header);
String body = BufferUtil.toString(content0);
body+=BufferUtil.toString(content0);
BufferUtil.clear(content0);
result=gen.generate(header,null,null,null,null);
assertEquals(HttpGenerator.Result.OK,result);
assertEquals(HttpGenerator.State.COMMITTED,gen.getState());
result=gen.generate(null,null,null,content1,null);
assertEquals(HttpGenerator.Result.FLUSH_CONTENT,result);
assertEquals(HttpGenerator.State.COMMITTED,gen.getState());
body += BufferUtil.toString(content1);
body+=BufferUtil.toString(content1);
BufferUtil.clear(content1);
result=gen.generate(null,null,null,null,Action.COMPLETE);
assertEquals(HttpGenerator.Result.OK,result);
assertEquals(HttpGenerator.State.END,gen.getState());
assertEquals(59,gen.getContentWritten());
assertEquals(59,gen.getContentWritten());
// System.err.println(head+body);
assertThat(head,containsString("POST /index.html HTTP/1.1"));
@ -458,6 +458,7 @@ public class HttpGeneratorClientTest
assertThat(head,containsString("Content-Length: 59"));
assertThat(head,not(containsString("chunked")));
assertTrue(head.endsWith("\r\n\r\n"));
assertThat(body,containsString("dog"));
}
}

View File

@ -27,15 +27,9 @@ import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import javax.swing.text.View;
import org.eclipse.jetty.http.HttpGenerator.Action;
import org.eclipse.jetty.io.ByteArrayEndPoint;
import org.eclipse.jetty.io.SimpleBuffers;
import org.eclipse.jetty.util.BufferUtil;
import org.hamcrest.CoreMatchers;
import org.junit.Test;
import org.junit.matchers.JUnitMatchers;
/**
*
@ -48,7 +42,7 @@ public class HttpGeneratorTest
public final static String CONTENT="The quick brown fox jumped over the lazy dog.\nNow is the time for all good men to come to the aid of the party\nThe moon is blue to a fish in love.\n";
public final static String[] connect={null,"keep-alive","close","TE, close"};
@Test
public void testResponseNoContent() throws Exception
@ -60,31 +54,32 @@ public class HttpGeneratorTest
gen.setVersion(HttpVersion.HTTP_1_1);
gen.setResponse(200,null);
fields.add("Last-Modified",HttpFields.__01Jan1970);
HttpGenerator.Result
HttpGenerator.Result
result=gen.generate(null,null,null,null,Action.COMPLETE);
assertEquals(HttpGenerator.State.COMMITTING_COMPLETING,gen.getState());
assertEquals(HttpGenerator.Result.NEED_HEADER,result);
result=gen.generate(header,null,null,null,null);
assertEquals(HttpGenerator.Result.FLUSH,result);
String head = BufferUtil.toString(header);
BufferUtil.clear(header);
result=gen.generate(null,null,null,null,null);
assertEquals(HttpGenerator.Result.OK,result);
assertEquals(HttpGenerator.State.END,gen.getState());
assertEquals(0,gen.getContentWritten());
assertEquals(0,gen.getContentWritten());
assertThat(head,containsString("HTTP/1.1 200 OK"));
assertThat(head,containsString("Last-Modified: Thu, 01 Jan 1970 00?00?00 GMT"));
assertThat(head,containsString("Content-Length: 0"));
}
@Test
public void testResponseWithSmallContent() throws Exception
{
String body="";
ByteBuffer header=BufferUtil.allocate(4096);
ByteBuffer buffer=BufferUtil.allocate(8096);
ByteBuffer content=BufferUtil.toBuffer("Hello World");
@ -96,18 +91,18 @@ public class HttpGeneratorTest
gen.setResponse(200,null);
fields.add("Last-Modified",HttpFields.__01Jan1970);
HttpGenerator.Result
HttpGenerator.Result
result=gen.generate(null,null,null,content,null);
assertEquals(HttpGenerator.Result.NEED_BUFFER,result);
assertEquals(HttpGenerator.State.START,gen.getState());
result=gen.generate(null,null,buffer,content,null);
assertEquals(HttpGenerator.Result.OK,result);
assertEquals(HttpGenerator.State.START,gen.getState());
assertEquals("Hello World",BufferUtil.toString(buffer));
assertTrue(BufferUtil.isEmpty(content));
result=gen.generate(null,null,buffer,content1,null);
assertEquals(HttpGenerator.Result.OK,result);
assertEquals(HttpGenerator.State.START,gen.getState());
@ -117,33 +112,34 @@ public class HttpGeneratorTest
result=gen.generate(null,null,buffer,null,Action.COMPLETE);
assertEquals(HttpGenerator.Result.NEED_HEADER,result);
assertEquals(HttpGenerator.State.COMMITTING_COMPLETING,gen.getState());
result=gen.generate(header,null,buffer,null,null);
assertEquals(HttpGenerator.Result.FLUSH,result);
assertEquals(HttpGenerator.State.COMPLETING,gen.getState());
String head = BufferUtil.toString(header);
BufferUtil.clear(header);
String body = BufferUtil.toString(buffer);
body+=BufferUtil.toString(buffer);
BufferUtil.clear(buffer);
result=gen.generate(null,null,buffer,null,null);
assertEquals(HttpGenerator.Result.OK,result);
assertEquals(HttpGenerator.State.END,gen.getState());
assertThat(head,containsString("HTTP/1.1 200 OK"));
assertThat(head,containsString("Last-Modified: Thu, 01 Jan 1970 00?00?00 GMT"));
assertThat(head,containsString("Content-Length: 58"));
assertTrue(head.endsWith("\r\n\r\n"));
assertEquals("Hello World. The quick brown fox jumped over the lazy dog.",body);
assertEquals(58,gen.getContentWritten());
assertEquals(58,gen.getContentWritten());
}
@Test
public void testResponseWithChunkedContent() throws Exception
{
String body="";
ByteBuffer header=BufferUtil.allocate(4096);
ByteBuffer buffer=BufferUtil.allocate(16);
ByteBuffer content0=BufferUtil.toBuffer("Hello World! ");
@ -155,18 +151,18 @@ public class HttpGeneratorTest
gen.setResponse(200,null);
fields.add("Last-Modified",HttpFields.__01Jan1970);
HttpGenerator.Result
HttpGenerator.Result
result=gen.generate(null,null,null,content0,null);
assertEquals(HttpGenerator.Result.NEED_BUFFER,result);
assertEquals(HttpGenerator.State.START,gen.getState());
result=gen.generate(null,null,buffer,content0,null);
assertEquals(HttpGenerator.Result.OK,result);
assertEquals(HttpGenerator.State.START,gen.getState());
assertEquals("Hello World! ",BufferUtil.toString(buffer));
assertEquals(0,content0.remaining());
result=gen.generate(null,null,buffer,content1,null);
assertEquals(HttpGenerator.Result.NEED_HEADER,result);
assertEquals(HttpGenerator.State.COMMITTING,gen.getState());
@ -182,7 +178,7 @@ public class HttpGeneratorTest
String head = BufferUtil.toString(header);
BufferUtil.clear(header);
String body = BufferUtil.toString(buffer);
body+=BufferUtil.toString(buffer);
BufferUtil.clear(buffer);
result=gen.generate(null,null,buffer,content1,null);
@ -196,7 +192,7 @@ public class HttpGeneratorTest
assertEquals("\r\n10\r\n",BufferUtil.toString(chunk));
assertEquals(" quick brown fox",BufferUtil.toString(buffer));
assertEquals(27,content1.remaining());
body += BufferUtil.toString(chunk)+BufferUtil.toString(buffer);
body+=BufferUtil.toString(chunk)+BufferUtil.toString(buffer);
BufferUtil.clear(chunk);
BufferUtil.clear(buffer);
@ -206,7 +202,7 @@ public class HttpGeneratorTest
assertEquals("\r\n10\r\n",BufferUtil.toString(chunk));
assertEquals(" jumped over the",BufferUtil.toString(buffer));
assertEquals(11,content1.remaining());
body += BufferUtil.toString(chunk)+BufferUtil.toString(buffer);
body+=BufferUtil.toString(chunk)+BufferUtil.toString(buffer);
BufferUtil.clear(chunk);
BufferUtil.clear(buffer);
@ -216,13 +212,13 @@ public class HttpGeneratorTest
assertEquals("",BufferUtil.toString(chunk));
assertEquals(" lazy dog. ",BufferUtil.toString(buffer));
assertEquals(0,content1.remaining());
result=gen.generate(null,chunk,buffer,null,Action.COMPLETE);
assertEquals(HttpGenerator.Result.FLUSH,result);
assertEquals(HttpGenerator.State.COMPLETING,gen.getState());
assertEquals("\r\nB\r\n",BufferUtil.toString(chunk));
assertEquals(" lazy dog. ",BufferUtil.toString(buffer));
body += BufferUtil.toString(chunk)+BufferUtil.toString(buffer);
body+=BufferUtil.toString(chunk)+BufferUtil.toString(buffer);
BufferUtil.clear(chunk);
BufferUtil.clear(buffer);
@ -231,17 +227,17 @@ public class HttpGeneratorTest
assertEquals(HttpGenerator.State.END,gen.getState());
assertEquals("\r\n0\r\n\r\n",BufferUtil.toString(chunk));
assertEquals(0,buffer.remaining());
body += BufferUtil.toString(chunk);
body+=BufferUtil.toString(chunk);
BufferUtil.clear(chunk);
result=gen.generate(null,chunk,buffer,null,null);
assertEquals(HttpGenerator.Result.OK,result);
assertEquals(HttpGenerator.State.END,gen.getState());
assertEquals(59,gen.getContentWritten());
assertEquals(59,gen.getContentWritten());
// System.err.println(head+body);
assertThat(head,containsString("HTTP/1.1 200 OK"));
assertThat(head,containsString("Last-Modified: Thu, 01 Jan 1970 00?00?00 GMT"));
assertThat(head,not(containsString("Content-Length")));
@ -252,6 +248,7 @@ public class HttpGeneratorTest
@Test
public void testResponseWithLargeChunkedContent() throws Exception
{
String body="";
ByteBuffer header=BufferUtil.allocate(4096);
ByteBuffer content0=BufferUtil.toBuffer("Hello Cruel World! ");
ByteBuffer content1=BufferUtil.toBuffer("The quick brown fox jumped over the lazy dog. ");
@ -263,26 +260,26 @@ public class HttpGeneratorTest
gen.setResponse(200,null);
fields.add("Last-Modified",HttpFields.__01Jan1970);
HttpGenerator.Result
HttpGenerator.Result
result=gen.generate(null,null,null,content0,null);
assertEquals(HttpGenerator.Result.NEED_HEADER,result);
assertEquals(HttpGenerator.State.COMMITTING,gen.getState());
result=gen.generate(header,null,null,content0,null);
assertEquals(HttpGenerator.Result.FLUSH_CONTENT,result);
assertEquals(HttpGenerator.State.COMMITTED,gen.getState());
assertTrue(gen.isChunking());
String head = BufferUtil.toString(header);
BufferUtil.clear(header);
String body = BufferUtil.toString(content0);
body+=BufferUtil.toString(content0);
BufferUtil.clear(content0);
result=gen.generate(header,null,null,content0,null);
assertEquals(HttpGenerator.Result.OK,result);
assertEquals(HttpGenerator.State.COMMITTED,gen.getState());
result=gen.generate(null,null,null,content1,null);
assertEquals(HttpGenerator.Result.NEED_CHUNK,result);
assertEquals(HttpGenerator.State.COMMITTED,gen.getState());
@ -292,22 +289,22 @@ public class HttpGeneratorTest
assertEquals(HttpGenerator.Result.FLUSH_CONTENT,result);
assertEquals(HttpGenerator.State.COMMITTED,gen.getState());
assertEquals("\r\n2E\r\n",BufferUtil.toString(chunk));
body += BufferUtil.toString(chunk)+BufferUtil.toString(content1);
body+=BufferUtil.toString(chunk)+BufferUtil.toString(content1);
BufferUtil.clear(content1);
result=gen.generate(null,chunk,null,null,Action.COMPLETE);
assertEquals(HttpGenerator.Result.FLUSH,result);
assertEquals(HttpGenerator.State.END,gen.getState());
assertEquals("\r\n0\r\n\r\n",BufferUtil.toString(chunk));
body += BufferUtil.toString(chunk);
body+=BufferUtil.toString(chunk);
result=gen.generate(null,chunk,null,null,null);
assertEquals(HttpGenerator.Result.OK,result);
assertEquals(HttpGenerator.State.END,gen.getState());
assertEquals(65,gen.getContentWritten());
assertEquals(65,gen.getContentWritten());
// System.err.println(head+body);
assertThat(head,containsString("HTTP/1.1 200 OK"));
@ -321,6 +318,7 @@ public class HttpGeneratorTest
@Test
public void testResponseWithKnownContent() throws Exception
{
String body="";
ByteBuffer header=BufferUtil.allocate(4096);
ByteBuffer buffer=BufferUtil.allocate(16);
ByteBuffer content0=BufferUtil.toBuffer("Hello World! ");
@ -333,19 +331,19 @@ public class HttpGeneratorTest
fields.add("Last-Modified",HttpFields.__01Jan1970);
fields.add("Content-Length","59");
gen.setContentLength(59);
HttpGenerator.Result
HttpGenerator.Result
result=gen.generate(null,null,null,content0,null);
assertEquals(HttpGenerator.Result.NEED_BUFFER,result);
assertEquals(HttpGenerator.State.START,gen.getState());
result=gen.generate(null,null,buffer,content0,null);
assertEquals(HttpGenerator.Result.OK,result);
assertEquals(HttpGenerator.State.START,gen.getState());
assertEquals("Hello World! ",BufferUtil.toString(buffer));
assertEquals(0,content0.remaining());
result=gen.generate(null,null,buffer,content1,null);
assertEquals(HttpGenerator.Result.NEED_HEADER,result);
assertEquals(HttpGenerator.State.COMMITTING,gen.getState());
@ -361,7 +359,7 @@ public class HttpGeneratorTest
String head = BufferUtil.toString(header);
BufferUtil.clear(header);
String body = BufferUtil.toString(buffer);
body+=BufferUtil.toString(buffer);
BufferUtil.clear(buffer);
result=gen.generate(null,null,buffer,content1,null);
@ -369,7 +367,7 @@ public class HttpGeneratorTest
assertEquals(HttpGenerator.State.COMMITTED,gen.getState());
assertEquals(" quick brown fox",BufferUtil.toString(buffer));
assertEquals(27,content1.remaining());
body += BufferUtil.toString(buffer);
body+=BufferUtil.toString(buffer);
BufferUtil.clear(buffer);
result=gen.generate(null,null,buffer,content1,null);
@ -377,7 +375,7 @@ public class HttpGeneratorTest
assertEquals(HttpGenerator.State.COMMITTED,gen.getState());
assertEquals(" jumped over the",BufferUtil.toString(buffer));
assertEquals(11,content1.remaining());
body += BufferUtil.toString(buffer);
body+=BufferUtil.toString(buffer);
BufferUtil.clear(buffer);
result=gen.generate(null,null,buffer,content1,null);
@ -385,21 +383,21 @@ public class HttpGeneratorTest
assertEquals(HttpGenerator.State.COMMITTED,gen.getState());
assertEquals(" lazy dog. ",BufferUtil.toString(buffer));
assertEquals(0,content1.remaining());
result=gen.generate(null,null,buffer,null,Action.COMPLETE);
assertEquals(HttpGenerator.Result.FLUSH,result);
assertEquals(HttpGenerator.State.COMPLETING,gen.getState());
assertEquals(" lazy dog. ",BufferUtil.toString(buffer));
body += BufferUtil.toString(buffer);
body+=BufferUtil.toString(buffer);
BufferUtil.clear(buffer);
result=gen.generate(null,null,buffer,null,null);
assertEquals(HttpGenerator.Result.OK,result);
assertEquals(HttpGenerator.State.END,gen.getState());
assertEquals(0,buffer.remaining());
assertEquals(59,gen.getContentWritten());
assertEquals(59,gen.getContentWritten());
// System.err.println(head+body);
assertThat(head,containsString("HTTP/1.1 200 OK"));
@ -412,6 +410,7 @@ public class HttpGeneratorTest
@Test
public void testResponseWithKnownLargeContent() throws Exception
{
String body="";
ByteBuffer header=BufferUtil.allocate(4096);
ByteBuffer content0=BufferUtil.toBuffer("Hello World! ");
ByteBuffer content1=BufferUtil.toBuffer("The quick brown fox jumped over the lazy dog. ");
@ -424,9 +423,9 @@ public class HttpGeneratorTest
fields.add("Last-Modified",HttpFields.__01Jan1970);
fields.add("Content-Length","59");
gen.setContentLength(59);
HttpGenerator.Result
HttpGenerator.Result
result=gen.generate(null,null,null,content0,null);
assertEquals(HttpGenerator.Result.NEED_HEADER,result);
assertEquals(HttpGenerator.State.COMMITTING,gen.getState());
@ -438,25 +437,25 @@ public class HttpGeneratorTest
String head = BufferUtil.toString(header);
BufferUtil.clear(header);
String body = BufferUtil.toString(content0);
body+=BufferUtil.toString(content0);
BufferUtil.clear(content0);
result=gen.generate(header,null,null,null,null);
assertEquals(HttpGenerator.Result.OK,result);
assertEquals(HttpGenerator.State.COMMITTED,gen.getState());
result=gen.generate(null,null,null,content1,null);
assertEquals(HttpGenerator.Result.FLUSH_CONTENT,result);
assertEquals(HttpGenerator.State.COMMITTED,gen.getState());
body += BufferUtil.toString(content1);
body+=BufferUtil.toString(content1);
BufferUtil.clear(content1);
result=gen.generate(null,null,null,null,Action.COMPLETE);
assertEquals(HttpGenerator.Result.OK,result);
assertEquals(HttpGenerator.State.END,gen.getState());
assertEquals(59,gen.getContentWritten());
assertEquals(59,gen.getContentWritten());
// System.err.println(head+body);
assertThat(head,containsString("HTTP/1.1 200 OK"));
@ -465,10 +464,10 @@ public class HttpGeneratorTest
assertThat(head,not(containsString("chunked")));
assertTrue(head.endsWith("\r\n\r\n"));
}
@Test
public void testHTTP() throws Exception
{
@ -477,7 +476,7 @@ public class HttpGeneratorTest
Handler handler = new Handler();
HttpParser parser=null;
// For HTTP version
for (int v=9;v<=11;v++)
{
@ -497,7 +496,7 @@ public class HttpGeneratorTest
fields.clear();
String response=tr[r].build(v,gen,"OK\r\nTest",connect[c],null,chunks, fields);
// System.err.println("===\n"+t+"\n"+response+(gen.isPersistent()?"...\n":"---\n"));
if (v==9)
@ -510,7 +509,7 @@ public class HttpGeneratorTest
parser=new HttpParser(handler);
parser.setHeadResponse(tr[r]._head);
try
{
parser.parseNext(BufferUtil.toBuffer(response));
@ -524,7 +523,7 @@ public class HttpGeneratorTest
if (tr[r]._body!=null)
assertEquals(t,tr[r]._body, this._content);
if (v==10)
assertTrue(t,gen.isPersistent() || tr[r]._contentLength>=0|| c==2 || c==0);
else
@ -537,7 +536,7 @@ public class HttpGeneratorTest
assertTrue(t,tr[r]._body==null);
else
assertThat(t,tr[r]._contentLength,either(equalTo(_content.length())).or(equalTo(-1)));
}
}
}
@ -547,9 +546,9 @@ public class HttpGeneratorTest
private static final String[] headers= { "Content-Type","Content-Length","Connection","Transfer-Encoding","Other"};
private static class TR
{
private int _code;
private String _body;
private boolean _head;
private final int _code;
private final String _body;
private final boolean _head;
String _contentType;
int _contentLength;
String _connection;
@ -574,7 +573,7 @@ public class HttpGeneratorTest
gen.setVersion(HttpVersion.fromVersion(version));
gen.setResponse(_code,reason);
gen.setHead(_head);
if (_contentType!=null)
fields.put("Content-Type",_contentType);
if (_contentLength>=0)
@ -588,15 +587,15 @@ public class HttpGeneratorTest
fields.put("Transfer-Encoding",_te);
if (_other!=null)
fields.put("Other",_other);
ByteBuffer content=_body==null?null:BufferUtil.toBuffer(_body);
if (content!=null)
content.limit(0);
ByteBuffer header=null;
ByteBuffer chunk=null;
ByteBuffer buffer=null;
while(!gen.isComplete())
{
// if we have unwritten content
@ -615,7 +614,7 @@ public class HttpGeneratorTest
// Generate
Action action=BufferUtil.hasContent(content)?null:Action.COMPLETE;
/* System.err.printf("generate(%s,%s,%s,%s,%s)@%s%n",
BufferUtil.toSummaryString(header),
BufferUtil.toSummaryString(chunk),
@ -630,13 +629,13 @@ public class HttpGeneratorTest
BufferUtil.toSummaryString(buffer),
BufferUtil.toSummaryString(content),
action,gen.getState());*/
switch(result)
{
case NEED_HEADER:
header=BufferUtil.allocate(2048);
break;
case NEED_BUFFER:
buffer=BufferUtil.allocate(8192);
break;
@ -645,7 +644,7 @@ public class HttpGeneratorTest
header=null;
chunk=BufferUtil.allocate(HttpGenerator.CHUNK_SIZE);
break;
case FLUSH:
if (BufferUtil.hasContent(header))
{
@ -681,10 +680,10 @@ public class HttpGeneratorTest
content.position(content.limit());
}
break;
case OK:
case SHUTDOWN_OUT:
// TODO
// TODO
}
}
return response;
@ -698,23 +697,23 @@ public class HttpGeneratorTest
}
private final TR[] tr =
{
/* 0 */ new TR(200,null,-1,null,false),
/* 1 */ new TR(200,null,-1,CONTENT,false),
/* 2 */ new TR(200,null,CONTENT.length(),null,true),
/* 3 */ new TR(200,null,CONTENT.length(),CONTENT,false),
/* 4 */ new TR(200,"text/html",-1,null,true),
/* 5 */ new TR(200,"text/html",-1,CONTENT,false),
/* 6 */ new TR(200,"text/html",CONTENT.length(),null,true),
/* 7 */ new TR(200,"text/html",CONTENT.length(),CONTENT,false),
};
{
/* 0 */ new TR(200,null,-1,null,false),
/* 1 */ new TR(200,null,-1,CONTENT,false),
/* 2 */ new TR(200,null,CONTENT.length(),null,true),
/* 3 */ new TR(200,null,CONTENT.length(),CONTENT,false),
/* 4 */ new TR(200,"text/html",-1,null,true),
/* 5 */ new TR(200,"text/html",-1,CONTENT,false),
/* 6 */ new TR(200,"text/html",CONTENT.length(),null,true),
/* 7 */ new TR(200,"text/html",CONTENT.length(),CONTENT,false),
};
private String _content;
private String _version;
private int _status;
private String _reason;
private List<String> _hdr=new ArrayList<>();
private List<String> _val=new ArrayList<>();
private final List<String> _hdr=new ArrayList<>();
private final List<String> _val=new ArrayList<>();
private class Handler implements HttpParser.ResponseHandler
{
@ -742,7 +741,7 @@ public class HttpGeneratorTest
_content= null;
return false;
}
@Override
public boolean content(ByteBuffer ref) throws IOException
{
@ -765,6 +764,6 @@ public class HttpGeneratorTest
return true;
}
}
}

View File

@ -118,19 +118,19 @@ public class HttpParserTest
{
ByteBuffer buffer= BufferUtil.toBuffer(
"GET / HTTP/1.0\015\012" +
"Host: localhost\015\012" +
"Header1: value1\015\012" +
"Header2 : value 2a \015\012" +
" value 2b \015\012" +
"Header3: \015\012" +
"Header4 \015\012" +
" value4\015\012" +
"Server5: notServer\015\012" +
"\015\012");
"Host: localhost\015\012" +
"Header1: value1\015\012" +
"Header2 : value 2a \015\012" +
" value 2b \015\012" +
"Header3: \015\012" +
"Header4 \015\012" +
" value4\015\012" +
"Server5: notServer\015\012" +
"\015\012");
Handler handler = new Handler();
HttpParser parser= new HttpParser((HttpParser.RequestHandler)handler);
parser.parseAll(buffer);
assertEquals("GET", f0);
assertEquals("/", f1);
assertEquals("HTTP/1.0", f2);
@ -148,25 +148,25 @@ public class HttpParserTest
assertEquals("notServer", val[5]);
assertEquals(5, h);
}
@Test
public void testSplitHeaderParse() throws Exception
{
ByteBuffer buffer= BufferUtil.toBuffer(
"XXXXGET / HTTP/1.0\015\012" +
"Host: localhost\015\012" +
"Header1: value1\015\012" +
"Header2 : value 2a \015\012" +
" value 2b \015\012" +
"Header3: \015\012" +
"Header4 \015\012" +
" value4\015\012" +
"Server5: notServer\015\012" +
"\015\012ZZZZ");
"XXXXGET / HTTP/1.0\015\012" +
"Host: localhost\015\012" +
"Header1: value1\015\012" +
"Header2 : value 2a \015\012" +
" value 2b \015\012" +
"Header3: \015\012" +
"Header4 \015\012" +
" value4\015\012" +
"Server5: notServer\015\012" +
"\015\012ZZZZ");
buffer.position(2);
buffer.limit(buffer.capacity()-2);
buffer=buffer.slice();
for (int i=0;i<buffer.capacity()-4;i++)
{
Handler handler = new Handler();
@ -174,13 +174,13 @@ public class HttpParserTest
buffer.position(2);
buffer.limit(2+i);
if (!parser.parseNext(buffer))
{
buffer.limit(buffer.capacity()-2);
parser.parseNext(buffer);
}
assertEquals("GET", f0);
assertEquals("/", f1);
assertEquals("HTTP/1.0", f2);
@ -200,24 +200,24 @@ public class HttpParserTest
}
}
@Test
public void testChunkParse() throws Exception
{
ByteBuffer buffer= BufferUtil.toBuffer(
"GET /chunk HTTP/1.0\015\012"
+ "Header1: value1\015\012"
+ "Transfer-Encoding: chunked\015\012"
+ "\015\012"
+ "a;\015\012"
+ "0123456789\015\012"
+ "1a\015\012"
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ\015\012"
+ "0\015\012");
"GET /chunk HTTP/1.0\015\012"
+ "Header1: value1\015\012"
+ "Transfer-Encoding: chunked\015\012"
+ "\015\012"
+ "a;\015\012"
+ "0123456789\015\012"
+ "1a\015\012"
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ\015\012"
+ "0\015\012");
Handler handler = new Handler();
HttpParser parser= new HttpParser((HttpParser.RequestHandler)handler);
parser.parseAll(buffer);
assertEquals("GET", f0);
assertEquals("/chunk", f1);
assertEquals("HTTP/1.0", f2);
@ -232,26 +232,26 @@ public class HttpParserTest
{
ByteBuffer buffer= BufferUtil.toBuffer(
"GET /mp HTTP/1.0\015\012"
+ "Connection: Keep-Alive\015\012"
+ "Header1: value1\015\012"
+ "Transfer-Encoding: chunked\015\012"
+ "\015\012"
+ "a;\015\012"
+ "0123456789\015\012"
+ "1a\015\012"
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ\015\012"
+ "0\015\012"
+ "POST /foo HTTP/1.0\015\012"
+ "Connection: Keep-Alive\015\012"
+ "Header2: value2\015\012"
+ "Content-Length: 0\015\012"
+ "\015\012"
+ "PUT /doodle HTTP/1.0\015\012"
+ "Connection: close\015\012"
+ "Header3: value3\015\012"
+ "Content-Length: 10\015\012"
+ "\015\012"
+ "0123456789\015\012");
+ "Connection: Keep-Alive\015\012"
+ "Header1: value1\015\012"
+ "Transfer-Encoding: chunked\015\012"
+ "\015\012"
+ "a;\015\012"
+ "0123456789\015\012"
+ "1a\015\012"
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ\015\012"
+ "0\015\012"
+ "POST /foo HTTP/1.0\015\012"
+ "Connection: Keep-Alive\015\012"
+ "Header2: value2\015\012"
+ "Content-Length: 0\015\012"
+ "\015\012"
+ "PUT /doodle HTTP/1.0\015\012"
+ "Connection: close\015\012"
+ "Header3: value3\015\012"
+ "Content-Length: 10\015\012"
+ "\015\012"
+ "0123456789\015\012");
Handler handler = new Handler();
@ -264,7 +264,7 @@ public class HttpParserTest
assertEquals("Header1", hdr[1]);
assertEquals("value1", val[1]);
assertEquals("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", _content);
parser.reset();
parser.parseNext(buffer);
assertEquals("POST", f0);
@ -291,11 +291,11 @@ public class HttpParserTest
public void testResponseParse0() throws Exception
{
ByteBuffer buffer= BufferUtil.toBuffer(
"HTTP/1.1 200 Correct\015\012"
+ "Content-Length: 10\015\012"
+ "Content-Type: text/plain\015\012"
+ "\015\012"
+ "0123456789\015\012");
"HTTP/1.1 200 Correct\015\012"
+ "Content-Length: 10\015\012"
+ "Content-Type: text/plain\015\012"
+ "\015\012"
+ "0123456789\015\012");
Handler handler = new Handler();
HttpParser parser= new HttpParser((HttpParser.ResponseHandler)handler);
@ -312,9 +312,9 @@ public class HttpParserTest
public void testResponseParse1() throws Exception
{
ByteBuffer buffer= BufferUtil.toBuffer(
"HTTP/1.1 304 Not-Modified\015\012"
+ "Connection: close\015\012"
+ "\015\012");
"HTTP/1.1 304 Not-Modified\015\012"
+ "Connection: close\015\012"
+ "\015\012");
Handler handler = new Handler();
HttpParser parser= new HttpParser((HttpParser.ResponseHandler)handler);
@ -330,14 +330,14 @@ public class HttpParserTest
public void testResponseParse2() throws Exception
{
ByteBuffer buffer= BufferUtil.toBuffer(
"HTTP/1.1 204 No-Content\015\012"
+ "Header: value\015\012"
+ "\015\012"
+ "HTTP/1.1 200 Correct\015\012"
+ "Content-Length: 10\015\012"
+ "Content-Type: text/plain\015\012"
+ "\015\012"
+ "0123456789\015\012");
"HTTP/1.1 204 No-Content\015\012"
+ "Header: value\015\012"
+ "\015\012"
+ "HTTP/1.1 200 Correct\015\012"
+ "Content-Length: 10\015\012"
+ "Content-Type: text/plain\015\012"
+ "\015\012"
+ "0123456789\015\012");
Handler handler = new Handler();
HttpParser parser= new HttpParser((HttpParser.ResponseHandler)handler);
@ -348,7 +348,7 @@ public class HttpParserTest
assertTrue(headerCompleted);
assertTrue(messageCompleted);
parser.setPersistent(true);
parser.reset();
parser.parseNext(buffer);
@ -366,11 +366,11 @@ public class HttpParserTest
public void testResponseParse3() throws Exception
{
ByteBuffer buffer= BufferUtil.toBuffer(
"HTTP/1.1 200\015\012"
+ "Content-Length: 10\015\012"
+ "Content-Type: text/plain\015\012"
+ "\015\012"
+ "0123456789\015\012");
"HTTP/1.1 200\015\012"
+ "Content-Length: 10\015\012"
+ "Content-Type: text/plain\015\012"
+ "\015\012"
+ "0123456789\015\012");
Handler handler = new Handler();
HttpParser parser= new HttpParser((HttpParser.ResponseHandler)handler);
@ -382,16 +382,16 @@ public class HttpParserTest
assertTrue(headerCompleted);
assertTrue(messageCompleted);
}
@Test
public void testResponseParse4() throws Exception
{
ByteBuffer buffer= BufferUtil.toBuffer(
"HTTP/1.1 200 \015\012"
+ "Content-Length: 10\015\012"
+ "Content-Type: text/plain\015\012"
+ "\015\012"
+ "0123456789\015\012");
"HTTP/1.1 200 \015\012"
+ "Content-Length: 10\015\012"
+ "Content-Type: text/plain\015\012"
+ "\015\012"
+ "0123456789\015\012");
Handler handler = new Handler();
HttpParser parser= new HttpParser((HttpParser.ResponseHandler)handler);
@ -403,15 +403,15 @@ public class HttpParserTest
assertTrue(headerCompleted);
assertTrue(messageCompleted);
}
@Test
public void testResponse304WithContentLength() throws Exception
{
ByteBuffer buffer= BufferUtil.toBuffer(
"HTTP/1.1 304 found\015\012"
+ "Content-Length: 10\015\012"
+ "\015\012");
"HTTP/1.1 304 found\015\012"
+ "Content-Length: 10\015\012"
+ "\015\012");
Handler handler = new Handler();
HttpParser parser= new HttpParser((HttpParser.ResponseHandler)handler);
parser.parseNext(buffer);
@ -422,22 +422,22 @@ public class HttpParserTest
assertTrue(headerCompleted);
assertTrue(messageCompleted);
}
@Test
public void testSeekEOF() throws Exception
{
ByteBuffer buffer= BufferUtil.toBuffer(
"HTTP/1.1 200 OK\015\012"
+ "Content-Length: 0\015\012"
+ "Connection: close\015\012"
+ "\015\012"
+ "\015\012" // extra CRLF ignored
+ "HTTP/1.1 400 OK\015\012"); // extra data causes close
"HTTP/1.1 200 OK\015\012"
+ "Content-Length: 0\015\012"
+ "Connection: close\015\012"
+ "\015\012"
+ "\015\012" // extra CRLF ignored
+ "HTTP/1.1 400 OK\015\012"); // extra data causes close
Handler handler = new Handler();
HttpParser parser= new HttpParser((HttpParser.ResponseHandler)handler);
parser.parseNext(buffer);
assertEquals("HTTP/1.1", f0);
assertEquals("200", f1);
@ -445,10 +445,10 @@ public class HttpParserTest
assertEquals(null,_content);
assertTrue(headerCompleted);
assertTrue(messageCompleted);
}
private String _content;
private String f0;
private String f1;
@ -465,6 +465,7 @@ public class HttpParserTest
private HttpFields fields;
private boolean request;
@Override
public boolean content(ByteBuffer ref)
{
if (_content==null)
@ -475,6 +476,7 @@ public class HttpParserTest
return false;
}
@Override
public boolean startRequest(String tok0, String tok1, String tok2)
{
//System.err.println("request "+tok0+" "+tok1+" "+tok2);
@ -492,6 +494,7 @@ public class HttpParserTest
return false;
}
@Override
public boolean parsedHeader(String name, String value)
{
//System.err.println("header "+name+": "+value);
@ -500,6 +503,7 @@ public class HttpParserTest
return false;
}
@Override
public boolean headerComplete()
{
//System.err.println("headerComplete");
@ -517,6 +521,7 @@ public class HttpParserTest
return false;
}
@Override
public boolean messageComplete(long contentLength)
{
//System.err.println("messageComplete");
@ -524,6 +529,7 @@ public class HttpParserTest
return true;
}
@Override
public boolean startResponse(String version, int status, String reason)
{
request=false;

View File

@ -4,11 +4,11 @@
// 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
// 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.
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
@ -24,20 +24,20 @@ import org.junit.Test;
/* ------------------------------------------------------------ */
public class HttpURITest
{
public static final String __input = "http://example.com:8080/path/to/context?parameter=%22value%22#fragment";
public static final String __input = "http://example.com:8080/path/to/context?parameter=%22value%22#fragment";
public static final String __scheme = "http";
public static final String __host = "example.com";
public static final int __port = 8080;
public static final String __path = "/path/to/context";
public static final String __query = "parameter=%22value%22";
public static final String __fragment = "fragment";
/* ------------------------------------------------------------ */
@Test
public void testFromString() throws Exception
{
HttpURI uri = new HttpURI(__input);
assertEquals(__scheme, uri.getScheme());
assertEquals(__host,uri.getHost());
assertEquals(__port,uri.getPort());
@ -45,13 +45,13 @@ public class HttpURITest
assertEquals(__query,uri.getQuery());
assertEquals(__fragment,uri.getFragment());
}
/* ------------------------------------------------------------ */
@Test
public void testFromURI() throws Exception
{
HttpURI uri = new HttpURI(new URI(__input));
assertEquals(__scheme, uri.getScheme());
assertEquals(__host,uri.getHost());
assertEquals(__port,uri.getPort());

View File

@ -39,23 +39,23 @@ public class PathMapTest extends TestCase
p.put("", "10");
String[][] tests = {
{ "/abs/path", "1"},
{ "/abs/path/xxx", "8"},
{ "/abs/pith", "8"},
{ "/abs/path/longer", "2"},
{ "/abs/path/", "8"},
{ "/abs/path/xxx", "8"},
{ "/animal/bird/eagle/bald", "3"},
{ "/animal/fish/shark/grey", "4"},
{ "/animal/insect/bug", "5"},
{ "/animal", "5"},
{ "/animal/", "5"},
{ "/animal/x", "5"},
{ "/animal/*", "5"},
{ "/suffix/path.tar.gz", "6"},
{ "/suffix/path.gz", "7"},
{ "/animal/path.gz", "5"},
{ "/Other/path", "8"},};
{ "/abs/path", "1"},
{ "/abs/path/xxx", "8"},
{ "/abs/pith", "8"},
{ "/abs/path/longer", "2"},
{ "/abs/path/", "8"},
{ "/abs/path/xxx", "8"},
{ "/animal/bird/eagle/bald", "3"},
{ "/animal/fish/shark/grey", "4"},
{ "/animal/insect/bug", "5"},
{ "/animal", "5"},
{ "/animal/", "5"},
{ "/animal/x", "5"},
{ "/animal/*", "5"},
{ "/suffix/path.tar.gz", "6"},
{ "/suffix/path.gz", "7"},
{ "/animal/path.gz", "5"},
{ "/Other/path", "8"},};
for (String[] test : tests)
{
@ -65,7 +65,7 @@ public class PathMapTest extends TestCase
assertEquals("Get absolute path", "1", p.get("/abs/path"));
assertEquals("Match absolute path", "/abs/path", p.getMatch("/abs/path").getKey());
assertEquals("all matches", "[/animal/bird/*=3, /animal/*=5, *.tar.gz=6, *.gz=7, /=8]",
p.getMatches("/animal/bird/path.tar.gz").toString());
p.getMatches("/animal/bird/path.tar.gz").toString());
assertEquals("Dir matches", "[/animal/fish/*=4, /animal/*=5, /=8]", p.getMatches("/animal/fish/").toString());
assertEquals("Dir matches", "[/animal/fish/*=4, /animal/*=5, /=8]", p.getMatches("/animal/fish").toString());
assertEquals("Dir matches", "[/=8]", p.getMatches("/").toString());

View File

@ -180,7 +180,7 @@ public class ByteArrayEndPoint implements ConnectedEndPoint
if (_closed)
throw new IOException("CLOSED");
if (_in!=null)
return BufferUtil.put(_in,buffer);
return BufferUtil.flipPutFlip(_in,buffer);
return 0;
}

View File

@ -294,7 +294,7 @@ public class SslConnection extends AbstractConnection implements AsyncConnection
if (BufferUtil.hasContent(_unwrapBuf))
{
// transfer from temp buffer to fill buffer
BufferUtil.put(_unwrapBuf,toFill);
BufferUtil.flipPutFlip(_unwrapBuf,toFill);
return true;
}
else
@ -305,7 +305,7 @@ public class SslConnection extends AbstractConnection implements AsyncConnection
else if (BufferUtil.hasContent(_unwrapBuf))
{
// transfer from temp buffer to fill buffer
BufferUtil.put(_unwrapBuf,toFill);
BufferUtil.flipPutFlip(_unwrapBuf,toFill);
return true;
}
@ -326,7 +326,7 @@ public class SslConnection extends AbstractConnection implements AsyncConnection
try
{
// Read any available data
if (!BufferUtil.isAtCapacity(_inbound) && (filled=_endp.fill(_inbound))>0)
if (!BufferUtil.isFull(_inbound) && (filled=_endp.fill(_inbound))>0)
progress = true;
else
_inbound.compact().flip();
@ -358,11 +358,11 @@ public class SslConnection extends AbstractConnection implements AsyncConnection
case NOT_HANDSHAKING:
{
// Try unwrapping some application data
if (!BufferUtil.isAtCapacity(toFill) && BufferUtil.hasContent(_inbound) && unwrap(toFill))
if (!BufferUtil.isFull(toFill) && BufferUtil.hasContent(_inbound) && unwrap(toFill))
progress=true;
// Try wrapping some application data
if (BufferUtil.hasContent(toFlush) && !BufferUtil.isAtCapacity(_outbound) && wrap(toFlush))
if (BufferUtil.hasContent(toFlush) && !BufferUtil.isFull(_outbound) && wrap(toFlush))
progress=true;
}
break;

View File

@ -11,7 +11,6 @@ import org.junit.Test;
public class BufferUtilTest
{
@Test
public void testPut() throws Exception
{
@ -19,75 +18,17 @@ public class BufferUtilTest
ByteBuffer from=BufferUtil.toBuffer("12345");
BufferUtil.clear(to);
assertEquals(5,BufferUtil.put(from,to));
assertEquals(5,BufferUtil.flipPutFlip(from,to));
assertTrue(BufferUtil.isEmpty(from));
assertEquals("12345",BufferUtil.toString(to));
from=BufferUtil.toBuffer("XX67890ZZ");
from.position(2);
assertEquals(5,BufferUtil.put(from,to));
assertEquals(5,BufferUtil.flipPutFlip(from,to));
assertEquals(2,from.remaining());
assertEquals("1234567890",BufferUtil.toString(to));
}
@Test
public void testPutUnderMax() throws Exception
{
ByteBuffer to = BufferUtil.allocate(10);
ByteBuffer from=BufferUtil.toBuffer("12345");
BufferUtil.clear(to);
assertEquals(5,BufferUtil.put(from,to,10));
assertTrue(BufferUtil.isEmpty(from));
assertEquals("12345",BufferUtil.toString(to));
from=BufferUtil.toBuffer("XX67890ZZ");
from.position(2);
assertEquals(5,BufferUtil.put(from,to,10));
assertEquals(2,from.remaining());
assertEquals("1234567890",BufferUtil.toString(to));
}
@Test
public void testPutAtMax() throws Exception
{
ByteBuffer to = BufferUtil.allocate(10);
ByteBuffer from=BufferUtil.toBuffer("12345");
BufferUtil.clear(to);
assertEquals(5,BufferUtil.put(from,to,5));
assertTrue(BufferUtil.isEmpty(from));
assertEquals("12345",BufferUtil.toString(to));
from=BufferUtil.toBuffer("XX67890ZZ");
from.position(2);
assertEquals(5,BufferUtil.put(from,to,5));
assertEquals(2,from.remaining());
assertEquals("1234567890",BufferUtil.toString(to));
}
@Test
public void testPutOverMax() throws Exception
{
ByteBuffer to = BufferUtil.allocate(10);
ByteBuffer from=BufferUtil.toBuffer("12345");
BufferUtil.clear(to);
assertEquals(4,BufferUtil.put(from,to,4));
assertEquals(1,from.remaining());
assertEquals("1234",BufferUtil.toString(to));
from=BufferUtil.toBuffer("XX567890ZZ");
from.position(2);
assertEquals(4,BufferUtil.put(from,to,4));
assertEquals(4,from.remaining());
assertEquals("12345678",BufferUtil.toString(to));
}
@Test
@ -97,73 +38,16 @@ public class BufferUtilTest
ByteBuffer from=BufferUtil.toBuffer("12345");
BufferUtil.clear(to);
assertEquals(5,BufferUtil.put(from,to));
assertEquals(5,BufferUtil.flipPutFlip(from,to));
assertTrue(BufferUtil.isEmpty(from));
assertEquals("12345",BufferUtil.toString(to));
from=BufferUtil.toBuffer("XX67890ZZ");
from.position(2);
assertEquals(5,BufferUtil.put(from,to));
assertEquals(5,BufferUtil.flipPutFlip(from,to));
assertEquals(2,from.remaining());
assertEquals("1234567890",BufferUtil.toString(to));
}
@Test
public void testPutUnderMaxDirect() throws Exception
{
ByteBuffer to = BufferUtil.allocateDirect(10);
ByteBuffer from=BufferUtil.toBuffer("12345");
BufferUtil.clear(to);
assertEquals(5,BufferUtil.put(from,to,10));
assertTrue(BufferUtil.isEmpty(from));
assertEquals("12345",BufferUtil.toString(to));
from=BufferUtil.toBuffer("XX67890ZZ");
from.position(2);
assertEquals(5,BufferUtil.put(from,to,10));
assertEquals(2,from.remaining());
assertEquals("1234567890",BufferUtil.toString(to));
}
@Test
public void testPutAtMaxDirect() throws Exception
{
ByteBuffer to = BufferUtil.allocateDirect(10);
ByteBuffer from=BufferUtil.toBuffer("12345");
BufferUtil.clear(to);
assertEquals(5,BufferUtil.put(from,to,5));
assertTrue(BufferUtil.isEmpty(from));
assertEquals("12345",BufferUtil.toString(to));
from=BufferUtil.toBuffer("XX67890ZZ");
from.position(2);
assertEquals(5,BufferUtil.put(from,to,5));
assertEquals(2,from.remaining());
assertEquals("1234567890",BufferUtil.toString(to));
}
@Test
public void testPutOverMaxDirect() throws Exception
{
ByteBuffer to = BufferUtil.allocateDirect(10);
ByteBuffer from=BufferUtil.toBuffer("12345");
BufferUtil.clear(to);
assertEquals(4,BufferUtil.put(from,to,4));
assertEquals(1,from.remaining());
assertEquals("1234",BufferUtil.toString(to));
from=BufferUtil.toBuffer("XX567890ZZ");
from.position(2);
assertEquals(4,BufferUtil.put(from,to,4));
assertEquals(4,from.remaining());
assertEquals("12345678",BufferUtil.toString(to));
}
}

View File

@ -125,7 +125,7 @@ public class SelectChannelEndPointTest
{
progress=false;
_in.compact().flip();
if (!BufferUtil.isAtCapacity(_in) && _endp.fill(_in)>0)
if (!BufferUtil.isFull(_in) && _endp.fill(_in)>0)
{
progress=true;
}
@ -134,11 +134,11 @@ public class SelectChannelEndPointTest
while (_blockAt>0 && _in.remaining()>0 && _in.remaining()<_blockAt)
{
_endp.blockReadable(10000);
if (!BufferUtil.isAtCapacity(_in) && _endp.fill(_in)>0)
if (!BufferUtil.isFull(_in) && _endp.fill(_in)>0)
progress=true;
}
if (BufferUtil.hasContent(_in) && BufferUtil.put(_in,_out)>0)
if (BufferUtil.hasContent(_in) && BufferUtil.flipPutFlip(_in,_out)>0)
progress=true;
if (BufferUtil.hasContent(_out) && _endp.flush(_out)>0)

View File

@ -39,7 +39,7 @@ import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.http.Parser;
import org.eclipse.jetty.io.AbstractConnection;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.BufferCache.ByteBuffer;
import org.eclipse.jetty.io.Buffers;
import org.eclipse.jetty.io.Connection;

View File

@ -18,7 +18,7 @@ import java.io.IOException;
import javax.servlet.ServletInputStream;
import org.eclipse.jetty.http.HttpParser;
import org.eclipse.jetty.io.ByteBuffer;
public class HttpInput extends ServletInputStream
{

View File

@ -20,7 +20,7 @@ import javax.servlet.ServletOutputStream;
import org.eclipse.jetty.http.AbstractGenerator;
import org.eclipse.jetty.http.Generator;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.ByteArrayBuffer;
import org.eclipse.jetty.io.EofException;
import org.eclipse.jetty.util.ByteArrayOutputStream2;

View File

@ -64,11 +64,11 @@ import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.http.HttpURI;
import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.io.nio.DirectNIOBuffer;
import org.eclipse.jetty.io.nio.IndirectNIOBuffer;
import org.eclipse.jetty.io.nio.NIOBuffer;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.ContextHandler.Context;

View File

@ -27,11 +27,11 @@ import java.util.concurrent.atomic.AtomicReference;
import org.eclipse.jetty.http.HttpContent;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.ByteArrayBuffer;
import org.eclipse.jetty.io.View;
import org.eclipse.jetty.io.nio.DirectNIOBuffer;
import org.eclipse.jetty.io.nio.IndirectNIOBuffer;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.resource.Resource;

View File

@ -22,7 +22,7 @@ import java.util.HashSet;
import java.util.Set;
import org.eclipse.jetty.http.HttpException;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.ConnectedEndPoint;
import org.eclipse.jetty.io.Connection;
import org.eclipse.jetty.io.EndPoint;

View File

@ -18,12 +18,12 @@ import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.HttpParser;
import org.eclipse.jetty.io.AsyncEndPoint;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.ConnectedEndPoint;
import org.eclipse.jetty.io.Connection;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.io.nio.AsyncConnection;
import org.eclipse.jetty.io.nio.IndirectNIOBuffer;
import org.eclipse.jetty.io.nio.SelectChannelEndPoint;
import org.eclipse.jetty.io.nio.SelectorManager;
import org.eclipse.jetty.server.AbstractHttpConnection;

View File

@ -57,7 +57,7 @@ import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.HttpException;
import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.server.AbstractHttpConnection;
import org.eclipse.jetty.server.Dispatcher;
import org.eclipse.jetty.server.Handler;

View File

@ -26,7 +26,7 @@ import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.ByteArrayBuffer;
import org.eclipse.jetty.io.WriterOutputStream;
import org.eclipse.jetty.server.AbstractHttpConnection;

View File

@ -23,7 +23,7 @@ import java.nio.channels.SocketChannel;
import java.util.Set;
import org.eclipse.jetty.http.HttpException;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.ConnectedEndPoint;
import org.eclipse.jetty.io.Connection;
import org.eclipse.jetty.io.EndPoint;

View File

@ -8,7 +8,7 @@ import java.io.IOException;
import org.eclipse.jetty.http.AbstractGenerator;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpGenerator;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.Buffers;
import org.eclipse.jetty.io.ByteArrayBuffer;
import org.eclipse.jetty.io.ByteArrayEndPoint;

View File

@ -30,7 +30,7 @@ import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.NetworkTrafficListener;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.server.nio.NetworkTrafficSelectChannelConnector;

View File

@ -18,7 +18,7 @@ import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.nio.SelectChannelConnector;

View File

@ -21,7 +21,7 @@ import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.io.nio.IndirectNIOBuffer;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;

View File

@ -13,7 +13,9 @@
package org.eclipse.jetty.util;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ByteChannel;
import java.nio.charset.Charset;
import javax.swing.text.Position;
@ -23,6 +25,30 @@ import javax.swing.text.Position;
/**
* Buffer utility methods.
*
* These utility methods facilitate the usage of NIO {@link ByteBuffer}'s in a more flexible way.
* The standard {@link ByteBuffer#flip()} assumes that once flipped to flush a buffer,
* that it will be completely emptied before being cleared ready to be filled again.
* The {@link #flipToFill(ByteBuffer)} and {@link #flipToFlush(ByteBuffer, int)} methods provided here
* do not assume that the buffer is empty and will preserve content when flipped.
* <p>
* ByteBuffers can be considered in one of two modes: Flush mode where valid content is contained between
* position and limit which is consumed by advancing the position; and Fill mode where empty space is between
* the position and limit, which is filled by advancing the position. In fill mode, there may be valid data
* in the buffer before the position and the start of this data is given by the return value of {@link #flipToFill(ByteBuffer)}
* <p>
* A typical pattern for using the buffers in this style is:
* <pre>
* ByteBuffer buf = BufferUtil.allocate(4096);
*
* while(true)
* {
* int pos=BufferUtil.flipToFill(buf);
* if (inChannel.read(buf)<0)
* break;
* BufferUtil.flipToFlush(buf,pos);
* outChannel.write(buf);
* }
* </pre>
*
*/
public class BufferUtil
@ -35,7 +61,7 @@ public class BufferUtil
/* ------------------------------------------------------------ */
/** Allocate ByteBuffer in output mode.
/** Allocate ByteBuffer in flush mode.
* The position and limit will both be zero, indicating that the buffer is
* empty and must be flipped before any data is put to it.
* @param capacity
@ -49,7 +75,7 @@ public class BufferUtil
}
/* ------------------------------------------------------------ */
/** Allocate ByteBuffer in output mode.
/** Allocate ByteBuffer in flush mode.
* The position and limit will both be zero, indicating that the buffer is
* empty and must be flipped before any data is put to it.
* @param capacity
@ -63,8 +89,11 @@ public class BufferUtil
}
/* ------------------------------------------------------------ */
/** Clear the buffer to be empty in flush mode.
* The position and limit are set to 0;
* @param buffer The buffer to clear.
*/
public static void clear(ByteBuffer buffer)
{
buffer.position(0);
@ -72,6 +101,10 @@ public class BufferUtil
}
/* ------------------------------------------------------------ */
/** Clear the buffer to be empty in fill mode.
* The position is set to 0 and the limit is set to the capacity.
* @param buffer The buffer to clear.
*/
public static void clearToFill(ByteBuffer buffer)
{
buffer.position(0);
@ -79,6 +112,16 @@ public class BufferUtil
}
/* ------------------------------------------------------------ */
/** Flip the buffer to fill mode.
* The position is set to the first unused position in the buffer
* (the old limit) and the limit is set to the capacity.
* If the buffer is empty, then this call is effectively {@link #clearToFill(ByteBuffer)}.
* If there is no unused space to fill, a {@link ByteBuffer#compact()} is done to attempt
* to create space.
* @param buffer The buffer to flip
* @return The position of the valid data before the flipped position. This value should be
* passed to a subsequent call to {@link #flipToFlush(ByteBuffer, int)}
*/
public static int flipToFill(ByteBuffer buffer)
{
int position=buffer.position();
@ -89,13 +132,30 @@ public class BufferUtil
buffer.limit(buffer.capacity());
return 0;
}
int capacity=buffer.capacity();
if (limit==capacity)
{
buffer.compact();
buffer.position(buffer.limit());
buffer.limit(buffer.capacity());
return 0;
}
buffer.position(limit);
buffer.limit(buffer.capacity());
buffer.limit(capacity);
return position;
}
/* ------------------------------------------------------------ */
/** Flip the buffer to Flush mode.
* The limit is set to the first unused byte(the old position) amd
* the position is set to the passed position.
* @param buffer the buffer to be flipped
* @param position The position of valid data to flip to. This should
* be the return value of the previous call to {@link #flipToFill(ByteBuffer)}
*/
public static void flipToFlush(ByteBuffer buffer,int position)
{
buffer.limit(buffer.position());
@ -104,6 +164,10 @@ public class BufferUtil
/* ------------------------------------------------------------ */
/** Convert a ByteBuffer to a byte array.
* @param buffer The buffer to convert in flush mode. The buffer is not altered.
* @return An array of bytes duplicated from the buffer.
*/
public static byte[] toArray(ByteBuffer buffer)
{
byte[] to = new byte[buffer.remaining()];
@ -118,151 +182,130 @@ public class BufferUtil
}
/* ------------------------------------------------------------ */
/** Check for an empty or null buffer.
* @param buf the buffer to check
* @return true if the buffer is null or empty.
*/
public static boolean isEmpty(ByteBuffer buf)
{
return buf==null || buf.remaining()==0;
}
/* ------------------------------------------------------------ */
/** Check for a non null and non empty buffer.
* @param buf the buffer to check
* @return true if the buffer is not null and not empty.
*/
public static boolean hasContent(ByteBuffer buf)
{
return buf!=null && buf.remaining()>0;
}
/* ------------------------------------------------------------ */
public static boolean isAtCapacity(ByteBuffer buf)
/** Check for a non null and full buffer.
* @param buf the buffer to check
* @return true if the buffer is not null and the limit equals the capacity.
*/
public static boolean isFull(ByteBuffer buf)
{
return buf!=null && buf.limit()==buf.capacity();
}
/* ------------------------------------------------------------ */
public static long remaining(ByteBuffer buffer)
/** Get remaining from null checked buffer
* @param buffer The buffer to get the remaining from, in flush mode.
* @return 0 if the buffer is null, else the bytes remaining in the buffer.
*/
public static long length(ByteBuffer buffer)
{
return buffer==null?0:buffer.remaining();
}
/* ------------------------------------------------------------ */
/**
* Put data from one buffer into another, avoiding over/under flows
* @param from Buffer to take bytes from
* @param to Buffer to put bytes to. The buffer is flipped before and after the put.
* @return number of bytes moved
*/
public static int put(ByteBuffer from, ByteBuffer to, long maxBytes)
{
return put(from,to,maxBytes>=Integer.MAX_VALUE?Integer.MAX_VALUE:(int)maxBytes);
}
/* ------------------------------------------------------------ */
/**
* Put data from one buffer into another, avoiding over/under flows
* @param from Buffer to take bytes from
* @param to Buffer to put bytes to. The buffer is flipped before and after the put.
* @return number of bytes moved
*/
public static int put(ByteBuffer from, ByteBuffer to, int maxBytes)
{
int put;
int pos=flipToFill(to);
try
{
maxBytes=Math.min(maxBytes,to.remaining());
int remaining=from.remaining();
if (remaining>0)
{
if (remaining<=maxBytes)
{
to.put(from);
put=remaining;
}
else if (from.hasArray())
{
put=maxBytes;
to.put(from.array(),from.arrayOffset()+from.position(),put);
from.position(from.position()+put);
}
else
{
put=maxBytes;
ByteBuffer slice=from.slice();
slice.limit(put);
to.put(slice);
from.position(from.position()+put);
}
}
else
put=0;
}
finally
{
flipToFlush(to,pos);
}
return put;
}
/* ------------------------------------------------------------ */
/**
* Put data from one buffer into another, avoiding over/under flows
* @param from Buffer to take bytes from
* @param to Buffer to put bytes to. The buffer is flipped before and after the put.
* @param from Buffer to take bytes from in flush mode
* @param to Buffer to put bytes to in fill mode.
* @return number of bytes moved
*/
public static int put(ByteBuffer from, ByteBuffer to)
{
int put;
int remaining=from.remaining();
if (remaining>0)
{
if (remaining<=to.remaining())
{
to.put(from);
put=remaining;
}
else if (from.hasArray())
{
put=to.remaining();
to.put(from.array(),from.arrayOffset()+from.position(),put);
from.position(from.position()+put);
}
else
{
put=to.remaining();
ByteBuffer slice=from.slice();
slice.limit(put);
to.put(slice);
from.position(from.position()+put);
}
}
else
put=0;
return put;
}
/* ------------------------------------------------------------ */
/**
* Put data from one buffer into another, avoiding over/under flows
* @param from Buffer to take bytes from in flush mode
* @param to Buffer to put bytes to in flush mode. The buffer is flipped before and after the put.
* @return number of bytes moved
*/
public static int flipPutFlip(ByteBuffer from, ByteBuffer to)
{
int pos= flipToFill(to);
try
{
int remaining=from.remaining();
if (remaining>0)
{
if (remaining<=to.remaining())
{
to.put(from);
put=remaining;
}
else if (from.hasArray())
{
put=to.remaining();
to.put(from.array(),from.arrayOffset()+from.position(),put);
from.position(from.position()+put);
}
else
{
put=to.remaining();
ByteBuffer slice=from.slice();
slice.limit(put);
to.put(slice);
from.position(from.position()+put);
}
}
else
put=0;
return put(from,to);
}
finally
{
flipToFlush(to,pos);
}
return put;
}
/* ------------------------------------------------------------ */
/** Convert the buffer to an ISO-8859-1 String
* @param buffer The buffer to convert in flush mode. The buffer is unchanged
* @return The buffer as a string.
*/
public static String toString(ByteBuffer buffer)
{
return toString(buffer,StringUtil.__ISO_8859_1_CHARSET);
}
/* ------------------------------------------------------------ */
/** Convert the buffer to an UTF-8 String
* @param buffer The buffer to convert in flush mode. The buffer is unchanged
* @return The buffer as a string.
*/
public static String toUTF8String(ByteBuffer buffer)
{
return toString(buffer,StringUtil.__UTF8_CHARSET);
}
/* ------------------------------------------------------------ */
/** Convert the buffer to an ISO-8859-1 String
* @param buffer The buffer to convert in flush mode. The buffer is unchanged
* @param charset The {@link Charset} to use to convert the bytes
* @return The buffer as a string.
*/
public static String toString(ByteBuffer buffer, Charset charset)
{
if (buffer == null)
@ -278,6 +321,11 @@ public class BufferUtil
}
/* ------------------------------------------------------------ */
/** Convert a partial buffer to an ISO-8859-1 String
* @param buffer The buffer to convert in flush mode. The buffer is unchanged
* @param charset The {@link Charset} to use to convert the bytes
* @return The buffer as a string.
*/
public static String toString(ByteBuffer buffer, int position, int length, Charset charset)
{
if (buffer == null)
@ -300,7 +348,7 @@ public class BufferUtil
* Convert buffer to an integer. Parses up to the first non-numeric character. If no number is found an IllegalArgumentException is thrown
*
* @param buffer
* A buffer containing an integer. The position is not changed.
* A buffer containing an integer in flush mode. The position is not changed.
* @return an int
*/
public static int toInt(ByteBuffer buffer)
@ -341,7 +389,7 @@ public class BufferUtil
* Convert buffer to an long. Parses up to the first non-numeric character. If no number is found an IllegalArgumentException is thrown
*
* @param buffer
* A buffer containing an integer. The position is not changed.
* A buffer containing an integer in flush mode. The position is not changed.
* @return an int
*/
public static long toLong(ByteBuffer buffer)
@ -423,45 +471,7 @@ public class BufferUtil
}
}
}
/* ------------------------------------------------------------ */
/**
* Add hex integer BEFORE current getIndex.
*
* @param buffer
* @param n
*/
public static void prependHexInt(ByteBuffer buffer, int n)
{
if (n == 0)
{
int gi = buffer.position();
buffer.put(--gi,(byte)'0');
buffer.position(gi);
}
else
{
boolean minus = false;
if (n < 0)
{
minus = true;
n = -n;
}
int gi = buffer.position();
while (n > 0)
{
int d = 0xf & n;
n = n >> 4;
buffer.put(--gi,DIGIT[d]);
}
if (minus)
buffer.put(--gi,(byte)'-');
buffer.position(gi);
}
}
/* ------------------------------------------------------------ */
public static void putDecInt(ByteBuffer buffer, int n)
{

View File

@ -19,7 +19,7 @@ import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.util.QuotedStringTokenizer;
import org.eclipse.jetty.websocket.WebSocketParser.FrameHandler;

View File

@ -21,7 +21,7 @@ import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.ByteArrayBuffer;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;

View File

@ -28,7 +28,7 @@
package org.eclipse.jetty.websocket;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.Buffers;
import org.eclipse.jetty.io.Buffers.Type;
import org.eclipse.jetty.io.BuffersFactory;

View File

@ -31,7 +31,7 @@ import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpParser;
import org.eclipse.jetty.io.AbstractConnection;
import org.eclipse.jetty.io.AsyncEndPoint;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.Buffers;
import org.eclipse.jetty.io.ByteArrayBuffer;
import org.eclipse.jetty.io.ConnectedEndPoint;

View File

@ -18,7 +18,7 @@ package org.eclipse.jetty.websocket;
import java.util.List;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.nio.AsyncConnection;
public interface WebSocketConnection extends AsyncConnection

View File

@ -36,11 +36,11 @@ import java.util.List;
import org.eclipse.jetty.io.AbstractConnection;
import org.eclipse.jetty.io.AsyncEndPoint;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.ByteArrayBuffer;
import org.eclipse.jetty.io.Connection;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.io.nio.IndirectNIOBuffer;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;

View File

@ -36,7 +36,7 @@ import java.util.List;
import org.eclipse.jetty.io.AbstractConnection;
import org.eclipse.jetty.io.AsyncEndPoint;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.ByteArrayBuffer;
import org.eclipse.jetty.io.Connection;
import org.eclipse.jetty.io.EndPoint;

View File

@ -36,7 +36,7 @@ import java.util.List;
import org.eclipse.jetty.io.AbstractConnection;
import org.eclipse.jetty.io.AsyncEndPoint;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.ByteArrayBuffer;
import org.eclipse.jetty.io.Connection;
import org.eclipse.jetty.io.EndPoint;

View File

@ -36,7 +36,7 @@ import java.util.List;
import org.eclipse.jetty.io.AbstractConnection;
import org.eclipse.jetty.io.AsyncEndPoint;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.ByteArrayBuffer;
import org.eclipse.jetty.io.Connection;
import org.eclipse.jetty.io.EndPoint;

View File

@ -31,7 +31,7 @@ package org.eclipse.jetty.websocket;
import java.io.IOException;
import java.math.BigInteger;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.io.EofException;

View File

@ -30,7 +30,7 @@ package org.eclipse.jetty.websocket;
import java.io.IOException;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.io.EofException;

View File

@ -30,7 +30,7 @@ package org.eclipse.jetty.websocket;
import java.io.IOException;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.io.EofException;

View File

@ -30,7 +30,7 @@ package org.eclipse.jetty.websocket;
import java.io.IOException;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.io.EofException;

View File

@ -28,7 +28,7 @@
package org.eclipse.jetty.websocket;
import org.eclipse.jetty.io.ByteBuffer;

View File

@ -30,7 +30,7 @@ package org.eclipse.jetty.websocket;
import java.io.IOException;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.Buffers;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.util.TypeUtil;

View File

@ -30,7 +30,7 @@ package org.eclipse.jetty.websocket;
import java.io.IOException;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.Buffers;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.util.log.Log;

View File

@ -30,7 +30,7 @@ package org.eclipse.jetty.websocket;
import java.io.IOException;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.Buffers;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.util.log.Log;

View File

@ -30,7 +30,7 @@ package org.eclipse.jetty.websocket;
import java.io.IOException;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.Buffers;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.util.log.Log;

View File

@ -33,7 +33,7 @@ import javax.servlet.http.HttpServletRequest;
import junit.framework.Assert;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.bio.SocketEndPoint;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;

View File

@ -29,7 +29,7 @@ import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletRequest;
import junit.framework.Assert;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.bio.SocketEndPoint;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;

View File

@ -28,7 +28,7 @@ import java.util.concurrent.atomic.AtomicReference;
import javax.servlet.http.HttpServletRequest;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.ByteArrayEndPoint;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;

View File

@ -32,7 +32,7 @@ import java.util.zip.Inflater;
import javax.servlet.http.HttpServletRequest;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.ByteArrayEndPoint;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;

View File

@ -34,7 +34,7 @@ import java.util.zip.Inflater;
import javax.servlet.http.HttpServletRequest;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.ByteArrayEndPoint;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;

View File

@ -22,7 +22,7 @@ import java.util.ArrayList;
import java.util.List;
import org.eclipse.jetty.http.HttpHeaderValue;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.BufferCache.ByteBuffer;
import org.eclipse.jetty.io.ByteArrayBuffer;
import org.eclipse.jetty.io.ByteArrayEndPoint;

View File

@ -21,7 +21,7 @@ import java.util.ArrayList;
import java.util.List;
import org.eclipse.jetty.http.HttpHeaderValue;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.BufferCache.ByteBuffer;
import org.eclipse.jetty.io.ByteArrayBuffer;
import org.eclipse.jetty.io.ByteArrayEndPoint;

View File

@ -21,7 +21,7 @@ import java.util.ArrayList;
import java.util.List;
import org.eclipse.jetty.http.HttpHeaderValue;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.BufferCache.ByteBuffer;
import org.eclipse.jetty.io.ByteArrayBuffer;
import org.eclipse.jetty.io.ByteArrayEndPoint;

View File

@ -19,7 +19,7 @@ import java.util.ArrayList;
import java.util.List;
import org.eclipse.jetty.http.HttpHeaderValue;
import org.eclipse.jetty.io.ByteBuffer;
import org.eclipse.jetty.io.BufferCache.ByteBuffer;
import org.eclipse.jetty.io.ByteArrayBuffer;
import org.eclipse.jetty.io.ByteArrayEndPoint;