minor cleanups

Signed-off-by: Greg Wilkins <gregw@webtide.com>
This commit is contained in:
Greg Wilkins 2019-05-22 14:02:38 +02:00
parent 4ac511ba13
commit 70311fe987
3 changed files with 44 additions and 42 deletions

View File

@ -116,7 +116,7 @@ public class HttpFields implements Iterable<HttpField>
public Stream<HttpField> stream()
{
return Arrays.stream(_fields);
return Arrays.stream(_fields).filter(f->f!=null);
}
/**
@ -175,6 +175,22 @@ public class HttpFields implements Iterable<HttpField>
return null;
}
public List<HttpField> getFields(HttpHeader header)
{
List<HttpField> fields = null;
for (int i=0;i<_size;i++)
{
HttpField f=_fields[i];
if (f.getHeader()==header)
{
if (fields==null)
fields = new ArrayList<>();
fields.add(f);
}
}
return fields==null?Collections.emptyList():fields;
}
public boolean contains(HttpField field)
{
for (int i=_size;i-->0;)

View File

@ -41,7 +41,6 @@ import java.util.Locale;
import java.util.Map;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import javax.servlet.AsyncContext;
import javax.servlet.AsyncListener;
import javax.servlet.DispatcherType;
@ -465,8 +464,8 @@ public class Request implements HttpServletRequest
int contentLength = getContentLength();
if (contentLength != 0 && _inputState == __NONE)
{
contentType = HttpFields.valueParameters(contentType, null);
if (MimeTypes.Type.FORM_ENCODED.is(contentType) &&
String baseType = HttpFields.valueParameters(contentType, null);
if (MimeTypes.Type.FORM_ENCODED.is(baseType) &&
_channel.getHttpConfiguration().isFormEncodedMethod(getMethod()))
{
if (_metaData!=null)
@ -477,7 +476,7 @@ public class Request implements HttpServletRequest
}
extractFormParameters(_contentParameters);
}
else if (MimeTypes.Type.MULTIPART_FORM_DATA.is(contentType) &&
else if (MimeTypes.Type.MULTIPART_FORM_DATA.is(baseType) &&
getAttribute(__MULTIPART_CONFIG_ELEMENT) != null &&
_multiParts == null)
{
@ -485,7 +484,7 @@ public class Request implements HttpServletRequest
{
if (_metaData!=null && getHttpFields().contains(HttpHeader.CONTENT_ENCODING))
throw new BadMessageException(HttpStatus.NOT_IMPLEMENTED_501,"Unsupported Content-Encoding");
getParts(_contentParameters);
getParts(contentType, _contentParameters);
}
catch (IOException | ServletException e)
{
@ -2324,13 +2323,13 @@ public class Request implements HttpServletRequest
@Override
public Collection<Part> getParts() throws IOException, ServletException
{
if (getContentType() == null ||
!MimeTypes.Type.MULTIPART_FORM_DATA.is(HttpFields.valueParameters(getContentType(),null)))
String contentType = getContentType();
if (contentType == null || !MimeTypes.Type.MULTIPART_FORM_DATA.is(HttpFields.valueParameters(contentType,null)))
throw new ServletException("Content-Type != multipart/form-data");
return getParts(null);
return getParts(contentType, null);
}
private Collection<Part> getParts(MultiMap<String> params) throws IOException, ServletException
private Collection<Part> getParts(String contentType, MultiMap<String> params) throws IOException, ServletException
{
if (_multiParts == null)
_multiParts = (MultiParts)getAttribute(__MULTIPARTS);
@ -2342,7 +2341,7 @@ public class Request implements HttpServletRequest
throw new IllegalStateException("No multipart config for servlet");
_multiParts = newMultiParts(getInputStream(),
getContentType(), config,
contentType, config,
(_context != null?(File)_context.getAttribute("javax.servlet.context.tempdir"):null));
setAttribute(__MULTIPARTS, _multiParts);
@ -2417,12 +2416,12 @@ public class Request implements HttpServletRequest
switch(compliance)
{
case RFC7578:
return new MultiParts.MultiPartsHttpParser(getInputStream(), getContentType(), config,
return new MultiParts.MultiPartsHttpParser(getInputStream(), contentType, config,
(_context != null?(File)_context.getAttribute("javax.servlet.context.tempdir"):null), this);
case LEGACY:
default:
return new MultiParts.MultiPartsUtilParser(getInputStream(), getContentType(), config,
return new MultiParts.MultiPartsUtilParser(getInputStream(), contentType, config,
(_context != null?(File)_context.getAttribute("javax.servlet.context.tempdir"):null), this);
}

View File

@ -24,14 +24,11 @@ import java.nio.channels.IllegalSelectorException;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
@ -1348,37 +1345,27 @@ public class Response implements HttpServletResponse
_reason = null;
_contentLength = -1;
List<HttpField> cookies = preserveCookies
?_fields.stream()
.filter(f->f.getHeader()==HttpHeader.SET_COOKIE)
.collect(Collectors.toList()):null;
List<HttpField> cookies = preserveCookies ?_fields.getFields(HttpHeader.SET_COOKIE):null;
_fields.clear();
String connection = _channel.getRequest().getHeader(HttpHeader.CONNECTION.asString());
if (connection != null)
for (String value: _channel.getRequest().getHttpFields().getCSV(HttpHeader.CONNECTION,false))
{
for (String value: StringUtil.csvSplit(null,connection,0,connection.length()))
HttpHeaderValue cb = HttpHeaderValue.CACHE.get(value);
if (cb != null)
{
HttpHeaderValue cb = HttpHeaderValue.CACHE.get(value);
if (cb != null)
switch (cb)
{
switch (cb)
{
case CLOSE:
_fields.put(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.toString());
break;
case KEEP_ALIVE:
if (HttpVersion.HTTP_1_0.is(_channel.getRequest().getProtocol()))
_fields.put(HttpHeader.CONNECTION, HttpHeaderValue.KEEP_ALIVE.toString());
break;
case TE:
_fields.put(HttpHeader.CONNECTION, HttpHeaderValue.TE.toString());
break;
default:
}
case CLOSE:
_fields.put(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.toString());
break;
case KEEP_ALIVE:
if (HttpVersion.HTTP_1_0.is(_channel.getRequest().getProtocol()))
_fields.put(HttpHeader.CONNECTION, HttpHeaderValue.KEEP_ALIVE.toString());
break;
case TE:
_fields.put(HttpHeader.CONNECTION, HttpHeaderValue.TE.toString());
break;
default:
}
}
}