ASF Bugzilla – Attachment 34090 Details for
Bug 59926
ServerCookie memery leak
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
avoid serverCookie memery leak
cookies.patch (text/plain), 7.94 KB, created by
gehui
on 2016-08-02 11:22:51 UTC
(
hide
)
Description:
avoid serverCookie memery leak
Filename:
MIME Type:
Creator:
gehui
Created:
2016-08-02 11:22:51 UTC
Size:
7.94 KB
patch
obsolete
>Index: java/org/apache/catalina/connector/mbeans-descriptors.xml >=================================================================== >--- java/org/apache/catalina/connector/mbeans-descriptors.xml (revision 1754708) >+++ java/org/apache/catalina/connector/mbeans-descriptors.xml (working copy) >@@ -94,6 +94,10 @@ > <attribute name="maxHeaderCount" > description="The maximum number of headers that are allowed by the container. 100 by default. A value of less than 0 means no limit." > type="int"/> >+ >+ <attribute name="maxCookieCount" >+ description="The maximum number of cookies that are allowed by the container. -1 by default. A value of less than 0 means no limit." >+ type="int"/> > > <attribute name="maxKeepAliveRequests" > description="Maximum number of Keep-Alive requests to honor per connection" >Index: java/org/apache/coyote/AbstractProtocol.java >=================================================================== >--- java/org/apache/coyote/AbstractProtocol.java (revision 1754708) >+++ java/org/apache/coyote/AbstractProtocol.java (working copy) >@@ -296,6 +296,13 @@ > endpoint.setMaxHeaderCount(maxHeaderCount); > } > >+ public int getMaxCookieCount() { >+ return endpoint.getMaxCookieCount(); >+ } >+ public void setMaxCookieCount(int maxCookieCount) { >+ endpoint.setMaxCookieCount(maxCookieCount); >+ } >+ > public long getConnectionCount() { > return endpoint.getConnectionCount(); > } >Index: java/org/apache/coyote/http11/AbstractHttp11Protocol.java >=================================================================== >--- java/org/apache/coyote/http11/AbstractHttp11Protocol.java (revision 1754708) >+++ java/org/apache/coyote/http11/AbstractHttp11Protocol.java (working copy) >@@ -648,6 +648,7 @@ > processor.setMaxSavePostSize(getMaxSavePostSize()); > processor.setServer(getServer()); > processor.setServerRemoveAppProvidedValues(getServerRemoveAppProvidedValues()); >+ processor.setAttainableMaxCookieCount(getMaxHttpHeaderSize()); > return processor; > } > >Index: java/org/apache/coyote/http11/Http11Processor.java >=================================================================== >--- java/org/apache/coyote/http11/Http11Processor.java (revision 1754708) >+++ java/org/apache/coyote/http11/Http11Processor.java (working copy) >@@ -225,6 +225,11 @@ > private final Map<String,UpgradeProtocol> httpUpgradeProtocols; > > >+ private int attainableMaxCookieCount = -1; >+ >+ public void setAttainableMaxCookieCount(int maxHttpHeaderSize) { >+ attainableMaxCookieCount = (maxHttpHeaderSize - "Cookie:".length()) / 3; >+ } > public Http11Processor(int maxHttpHeaderSize, AbstractEndpoint<?> endpoint,int maxTrailerSize, > Set<String> allowedTrailerHeaders, int maxExtensionSize, int maxSwallowSize, > Map<String,UpgradeProtocol> httpUpgradeProtocols) { >@@ -1014,6 +1019,8 @@ > readComplete = false; > break; > } >+ request.getCookies().setLimit(endpoint.getMaxCookieCount()); >+ request.getCookies().setAttainableMaxCookieCount(attainableMaxCookieCount); > if (!disableUploadTimeout) { > socketWrapper.setReadTimeout(connectionUploadTimeout); > } >Index: java/org/apache/tomcat/util/http/LocalStrings.properties >=================================================================== >--- java/org/apache/tomcat/util/http/LocalStrings.properties (revision 1754708) >+++ java/org/apache/tomcat/util/http/LocalStrings.properties (working copy) >@@ -30,6 +30,7 @@ > cookies.fallToDebug=\n Note: further occurrences of Cookie errors will be logged at DEBUG level. > > headers.maxCountFail=More than the maximum allowed number of headers ([{0}]) were detected. >+cookies.maxCountFail=More than the maximum allowed number of cookies ([{0}]) were detected. > > rfc6265CookieProcessor.invalidCharInValue=An invalid character [{0}] was present in the Cookie value > rfc6265CookieProcessor.invalidDomain=An invalid domain [{0}] was specified for this cookie >Index: java/org/apache/tomcat/util/http/ServerCookies.java >=================================================================== >--- java/org/apache/tomcat/util/http/ServerCookies.java (revision 1754708) >+++ java/org/apache/tomcat/util/http/ServerCookies.java (working copy) >@@ -16,17 +16,47 @@ > */ > package org.apache.tomcat.util.http; > >+import org.apache.tomcat.util.res.StringManager; >+ > /** > * This class is not thread-safe. > */ > public class ServerCookies { > >+ static final StringManager sm = StringManager.getManager("org.apache.tomcat.util.http"); >+ > private ServerCookie[] serverCookies; > > private int cookieCount = 0; > >+ private int attainableMaxCookieCount = -1; > >+ private int initialSize; >+ >+ public void setAttainableMaxCookieCount(int attainableMaxCookieCount) { >+ this.attainableMaxCookieCount = attainableMaxCookieCount; >+ } >+ >+ /** >+ * The limit on the number of cookie fields. >+ */ >+ private int limit = -1; >+ >+ /** >+ * Set limit on the number of cookie fields. >+ */ >+ public void setLimit(int limit) { >+ this.limit = limit; >+ if (limit > 0 && serverCookies.length > limit && cookieCount < limit) { >+ // shrink cookie list array >+ ServerCookie scookiesTmp[] = new ServerCookie[limit]; >+ System.arraycopy(serverCookies, 0, scookiesTmp, 0, cookieCount); >+ serverCookies = scookiesTmp; >+ } >+ } >+ > public ServerCookies(int initialSize) { >+ this.initialSize = initialSize; > serverCookies = new ServerCookie[initialSize]; > } > >@@ -38,8 +68,19 @@ > * @return the new cookie > */ > public ServerCookie addCookie() { >+ if (limit > -1 && cookieCount >= limit) { >+ throw new IllegalStateException(sm.getString("cookies.maxCountFail", Integer.valueOf(limit))); >+ } >+ > if (cookieCount >= serverCookies.length) { >- ServerCookie scookiesTmp[] = new ServerCookie[2*cookieCount]; >+ int arrayLength = 2 * cookieCount; >+ if (attainableMaxCookieCount > 0 && arrayLength > attainableMaxCookieCount) { >+ arrayLength = attainableMaxCookieCount; >+ } >+ if (limit > 0 && arrayLength > limit) { >+ arrayLength = limit; >+ } >+ ServerCookie scookiesTmp[] = new ServerCookie[arrayLength]; > System.arraycopy(serverCookies, 0, scookiesTmp, 0, cookieCount); > serverCookies = scookiesTmp; > } >@@ -65,9 +106,17 @@ > > > public void recycle() { >- for (int i = 0; i < cookieCount; i++) { >- serverCookies[i].recycle(); >+ if (cookieCount > initialSize) { >+ ServerCookie scookiesTmp[] = new ServerCookie[initialSize]; >+ System.arraycopy(serverCookies, 0, scookiesTmp, 0, initialSize); >+ serverCookies = scookiesTmp; > } >+ >+ for (int i = 0; i < initialSize; i++) { >+ if (serverCookies[i] != null) { >+ serverCookies[i].recycle(); >+ } >+ } > cookieCount = 0; > } > } >Index: java/org/apache/tomcat/util/net/AbstractEndpoint.java >=================================================================== >--- java/org/apache/tomcat/util/net/AbstractEndpoint.java (revision 1754708) >+++ java/org/apache/tomcat/util/net/AbstractEndpoint.java (working copy) >@@ -533,6 +533,18 @@ > } > > /** >+ * The maximum number of cookies in a request that are allowed. >+ * -1 by default. A value of less than 0 means no limit. >+ */ >+ private int maxCookieCount = -1; >+ public int getMaxCookieCount() { >+ return maxCookieCount; >+ } >+ public void setMaxCookieCount(int maxCookieCount) { >+ this.maxCookieCount = maxCookieCount; >+ } >+ >+ /** > * Name of the thread pool, which will be used for naming child threads. > */ > private String name = "TP";
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 59926
: 34090