From 23cb28e856fb61155ccdd577ec5ff9011c6c4676 Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Fri, 29 Jul 2016 12:34:12 +1000 Subject: [PATCH 1/2] improved debug logging of request/response --- .../eclipse/jetty/embedded/LikeJettyXml.java | 4 ++- .../org/eclipse/jetty/server/HttpChannel.java | 12 ++++++- .../java/org/eclipse/jetty/server/Server.java | 32 ++++++++----------- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/examples/embedded/src/main/java/org/eclipse/jetty/embedded/LikeJettyXml.java b/examples/embedded/src/main/java/org/eclipse/jetty/embedded/LikeJettyXml.java index 3dfa4d96a2c..07c4c2f1062 100644 --- a/examples/embedded/src/main/java/org/eclipse/jetty/embedded/LikeJettyXml.java +++ b/examples/embedded/src/main/java/org/eclipse/jetty/embedded/LikeJettyXml.java @@ -34,6 +34,7 @@ import org.eclipse.jetty.security.HashLoginService; import org.eclipse.jetty.server.ConnectorStatistics; import org.eclipse.jetty.server.DebugListener; import org.eclipse.jetty.server.Handler; +import org.eclipse.jetty.server.HttpChannel; import org.eclipse.jetty.server.HttpConfiguration; import org.eclipse.jetty.server.HttpConnectionFactory; import org.eclipse.jetty.server.LowResourceMonitor; @@ -47,6 +48,7 @@ import org.eclipse.jetty.server.handler.DefaultHandler; import org.eclipse.jetty.server.handler.HandlerCollection; import org.eclipse.jetty.server.handler.RequestLogHandler; import org.eclipse.jetty.server.handler.StatisticsHandler; +import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.ssl.SslContextFactory; import org.eclipse.jetty.util.thread.QueuedThreadPool; import org.eclipse.jetty.util.thread.ScheduledExecutorScheduler; @@ -166,7 +168,7 @@ public class LikeJettyXml // === jetty-deploy.xml === DeploymentManager deployer = new DeploymentManager(); - DebugListener debug = new DebugListener(System.out,true,true,true); + DebugListener debug = new DebugListener(System.err,true,true,true); server.addBean(debug); deployer.addLifeCycleBinding(new DebugListenerBinding(debug)); deployer.setContexts(contexts); diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannel.java b/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannel.java index 2b7ee496c3f..d687f8454bc 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannel.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannel.java @@ -597,6 +597,11 @@ public class HttpChannel implements Runnable, HttpOutput.Interceptor fields.put(_connector.getServer().getDateField()); _request.setMetaData(request); + + if (LOG.isDebugEnabled()) + LOG.debug("REQUEST for {} on {}{}{} {} {}{}{}",request.getURIString(),this,System.lineSeparator(), + request.getMethod(),request.getURIString(),request.getVersion(),System.lineSeparator(), + request.getFields()); } public boolean onContent(HttpInput.Content content) @@ -616,6 +621,9 @@ public class HttpChannel implements Runnable, HttpOutput.Interceptor public void onCompleted() { + if (LOG.isDebugEnabled()) + LOG.debug("COMPLETE for {} written={}",getRequest().getRequestURI(),getBytesWritten()); + if (_requestLog!=null ) _requestLog.log(_request, _response); @@ -733,7 +741,9 @@ public class HttpChannel implements Runnable, HttpOutput.Interceptor { _committedMetaData=info; if (LOG.isDebugEnabled()) - LOG.debug("Commit {} to {}",info,this); + LOG.debug("COMMIT for {} on {}{}{} {} {}{}{}",getRequest().getRequestURI(),this,System.lineSeparator(), + info.getStatus(),info.getReason(),info.getVersion(),System.lineSeparator(), + info.getFields()); } public boolean isCommitted() diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/Server.java b/jetty-server/src/main/java/org/eclipse/jetty/server/Server.java index af420f1fe59..42f4963dede 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/Server.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/Server.java @@ -502,15 +502,14 @@ public class Server extends HandlerWrapper implements Attributes * or after the entire request has been received (for short requests of known length), or * on the dispatch of an async request. */ - public void handle(HttpChannel connection) throws IOException, ServletException + public void handle(HttpChannel channel) throws IOException, ServletException { - final String target=connection.getRequest().getPathInfo(); - final Request request=connection.getRequest(); - final Response response=connection.getResponse(); + final String target=channel.getRequest().getPathInfo(); + final Request request=channel.getRequest(); + final Response response=channel.getResponse(); if (LOG.isDebugEnabled()) - LOG.debug("{} on {}{}{} {} {}{}{}", request.getDispatcherType(), connection, System.lineSeparator(), - request.getMethod(), target, request.getProtocol(), System.lineSeparator(), request.getHttpFields()); + LOG.debug("{} {} {} on {}", request.getDispatcherType(), request.getMethod(), target, channel); if (HttpMethod.OPTIONS.is(request.getMethod()) || "*".equals(target)) { @@ -524,8 +523,7 @@ public class Server extends HandlerWrapper implements Attributes handle(target, request, request, response); if (LOG.isDebugEnabled()) - LOG.debug("RESPONSE for {} h={}{}{} {}{}{}", target, request.isHandled(), System.lineSeparator(), - response.getStatus(), response.getReason(), System.lineSeparator(), response.getHttpFields()); + LOG.debug("handled={} async={} committed={} on {}", request.isHandled(),request.isAsyncStarted(),response.isCommitted(),channel); } /* ------------------------------------------------------------ */ @@ -541,12 +539,12 @@ public class Server extends HandlerWrapper implements Attributes * or after the entire request has been received (for short requests of known length), or * on the dispatch of an async request. */ - public void handleAsync(HttpChannel connection) throws IOException, ServletException + public void handleAsync(HttpChannel channel) throws IOException, ServletException { - final HttpChannelState state = connection.getRequest().getHttpChannelState(); + final HttpChannelState state = channel.getRequest().getHttpChannelState(); final AsyncContextEvent event = state.getAsyncContextEvent(); - final Request baseRequest=connection.getRequest(); + final Request baseRequest=channel.getRequest(); final String path=event.getPath(); if (path!=null) @@ -566,14 +564,10 @@ public class Server extends HandlerWrapper implements Attributes final HttpServletResponse response=(HttpServletResponse)event.getSuppliedResponse(); if (LOG.isDebugEnabled()) - { - LOG.debug(request.getDispatcherType()+" "+request.getMethod()+" "+target+" on "+connection); - handle(target, baseRequest, request, response); - LOG.debug("RESPONSE "+target+" "+connection.getResponse().getStatus()); - } - else - handle(target, baseRequest, request, response); - + LOG.debug("{} {} {} on {}", request.getDispatcherType(), request.getMethod(), target, channel); + handle(target, baseRequest, request, response); + if (LOG.isDebugEnabled()) + LOG.debug("handledAsync={} async={} committed={} on {}", channel.getRequest().isHandled(),request.isAsyncStarted(),response.isCommitted(),channel); } /* ------------------------------------------------------------ */ From e0a1a1988f7a242eedf235e55529fbaa9aafeea5 Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Fri, 29 Jul 2016 16:45:09 +1000 Subject: [PATCH 2/2] Response.reset does not clear session cookie #789 --- .../eclipse/jetty/embedded/LikeJettyXml.java | 3 -- .../org/eclipse/jetty/http/HttpFields.java | 7 ++++ .../org/eclipse/jetty/server/Request.java | 4 +- .../org/eclipse/jetty/server/Response.java | 42 ++++++++++++------- 4 files changed, 36 insertions(+), 20 deletions(-) diff --git a/examples/embedded/src/main/java/org/eclipse/jetty/embedded/LikeJettyXml.java b/examples/embedded/src/main/java/org/eclipse/jetty/embedded/LikeJettyXml.java index 07c4c2f1062..328765b559d 100644 --- a/examples/embedded/src/main/java/org/eclipse/jetty/embedded/LikeJettyXml.java +++ b/examples/embedded/src/main/java/org/eclipse/jetty/embedded/LikeJettyXml.java @@ -28,13 +28,11 @@ import org.eclipse.jetty.deploy.bindings.DebugListenerBinding; import org.eclipse.jetty.deploy.providers.WebAppProvider; import org.eclipse.jetty.http.HttpVersion; import org.eclipse.jetty.jmx.MBeanContainer; -import org.eclipse.jetty.rewrite.handler.CompactPathRule; import org.eclipse.jetty.rewrite.handler.RewriteHandler; import org.eclipse.jetty.security.HashLoginService; import org.eclipse.jetty.server.ConnectorStatistics; import org.eclipse.jetty.server.DebugListener; import org.eclipse.jetty.server.Handler; -import org.eclipse.jetty.server.HttpChannel; import org.eclipse.jetty.server.HttpConfiguration; import org.eclipse.jetty.server.HttpConnectionFactory; import org.eclipse.jetty.server.LowResourceMonitor; @@ -48,7 +46,6 @@ import org.eclipse.jetty.server.handler.DefaultHandler; import org.eclipse.jetty.server.handler.HandlerCollection; import org.eclipse.jetty.server.handler.RequestLogHandler; import org.eclipse.jetty.server.handler.StatisticsHandler; -import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.ssl.SslContextFactory; import org.eclipse.jetty.util.thread.QueuedThreadPool; import org.eclipse.jetty.util.thread.ScheduledExecutorScheduler; diff --git a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpFields.java b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpFields.java index 283cfc5f45c..67294d615ca 100644 --- a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpFields.java +++ b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpFields.java @@ -30,6 +30,8 @@ import java.util.Map; import java.util.NoSuchElementException; import java.util.Set; import java.util.StringTokenizer; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; import org.eclipse.jetty.util.ArrayTernaryTrie; import org.eclipse.jetty.util.QuotedStringTokenizer; @@ -96,6 +98,11 @@ public class HttpFields implements Iterable { return new Itr(); } + + public Stream stream() + { + return StreamSupport.stream(Arrays.spliterator(_fields,0,_size),false); + } /** * Get Collection of header names. diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/Request.java b/jetty-server/src/main/java/org/eclipse/jetty/server/Request.java index db54e19b2f1..b8762ebfcf5 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/Request.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/Request.java @@ -1469,9 +1469,7 @@ public class Request implements HttpServletRequest } /* ------------------------------------------------------------ */ - /* - * Add @override when 3.1 api is available - */ + @Override public String changeSessionId() { HttpSession session = getSession(false); diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/Response.java b/jetty-server/src/main/java/org/eclipse/jetty/server/Response.java index fb0e1530ac9..48b296eba4f 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/Response.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/Response.java @@ -27,8 +27,10 @@ import java.util.Collections; import java.util.EnumSet; import java.util.Enumeration; import java.util.Iterator; +import java.util.List; import java.util.Locale; import java.util.concurrent.atomic.AtomicInteger; +import java.util.stream.Collectors; import javax.servlet.RequestDispatcher; import javax.servlet.ServletOutputStream; @@ -1180,14 +1182,24 @@ public class Response implements HttpServletResponse @Override public void reset() { + reset(false); + } + + public void reset(boolean preserveCookies) + { resetForForward(); _status = 200; _reason = null; _contentLength = -1; + + List cookies = preserveCookies + ?_fields.stream() + .filter(f->f.getHeader()==HttpHeader.SET_COOKIE) + .collect(Collectors.toList()):null; + _fields.clear(); - String connection = _channel.getRequest().getHeader(HttpHeader.CONNECTION.asString()); - + String connection = _channel.getRequest().getHeader(HttpHeader.CONNECTION.asString()); if (connection != null) { for (String value: StringUtil.csvSplit(null,connection,0,connection.length())) @@ -1214,21 +1226,23 @@ public class Response implements HttpServletResponse } } } - } - public void reset(boolean preserveCookies) - { - if (!preserveCookies) - reset(); + if (preserveCookies) + cookies.forEach(f->_fields.add(f)); else { - ArrayList cookieValues = new ArrayList(5); - Enumeration vals = _fields.getValues(HttpHeader.SET_COOKIE.asString()); - while (vals.hasMoreElements()) - cookieValues.add(vals.nextElement()); - reset(); - for (String v:cookieValues) - _fields.add(HttpHeader.SET_COOKIE, v); + Request request = getHttpChannel().getRequest(); + HttpSession session = request.getSession(false); + if (session!=null && session.isNew()) + { + SessionManager sm = request.getSessionManager(); + if (sm!=null) + { + HttpCookie c=sm.getSessionCookie(session,request.getContextPath(),request.isSecure()); + if (c!=null) + addCookie(c); + } + } } }