Skip to content

Commit

Permalink
Reduced intermediate garbage in HeaderGroup and DefaultConnectionReus…
Browse files Browse the repository at this point in the history
…eStrategy

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpcore/branches/4.4.x@1673254 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
ok2c committed Apr 13, 2015
1 parent 8072018 commit 040af07
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/

package org.apache.hc5.core.http2.stream;

import org.apache.http.MessageHead;

public interface MessageEncoder<T extends MessageHead> {

void encode(T message);

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.apache.http.ProtocolVersion;
import org.apache.http.TokenIterator;
import org.apache.http.annotation.Immutable;
import org.apache.http.message.BasicHeaderIterator;
import org.apache.http.message.BasicTokenIterator;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;
Expand Down Expand Up @@ -107,9 +108,9 @@ public boolean keepAlive(final HttpResponse response,
// Check for the "Connection" header. If that is absent, check for
// the "Proxy-Connection" header. The latter is an unspecified and
// broken but unfortunately common extension of HTTP.
HeaderIterator hit = response.headerIterator(HTTP.CONN_DIRECTIVE);
if (!hit.hasNext()) {
hit = response.headerIterator("Proxy-Connection");
Header[] connHeaders = response.getHeaders(HTTP.CONN_DIRECTIVE);
if (connHeaders.length == 0) {
connHeaders = response.getHeaders("Proxy-Connection");
}

// Experimental usage of the "Connection" header in HTTP/1.0 is
Expand All @@ -135,9 +136,9 @@ public boolean keepAlive(final HttpResponse response,
// it takes precedence and indicates a non-persistent connection.
// If there is no "close" but a "keep-alive", we take the hint.

if (hit.hasNext()) {
if (connHeaders.length != 0) {
try {
final TokenIterator ti = createTokenIterator(hit);
final TokenIterator ti = new BasicTokenIterator(new BasicHeaderIterator(connHeaders, null));
boolean keepalive = false;
while (ti.hasNext()) {
final String token = ti.nextToken();
Expand All @@ -148,8 +149,7 @@ public boolean keepAlive(final HttpResponse response,
keepalive = true;
}
}
if (keepalive)
{
if (keepalive) {
return true;
// neither "close" nor "keep-alive", use default policy
}
Expand Down
10 changes: 7 additions & 3 deletions httpcore/src/main/java/org/apache/http/message/HeaderGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public class HeaderGroup implements Cloneable, Serializable {

private static final long serialVersionUID = 2608834160639271617L;

private final Header[] EMPTY = new Header[] {};

/** The list of headers for this group, in the order in which they were added */
private final List<Header> headers;

Expand Down Expand Up @@ -173,18 +175,20 @@ public Header getCondensedHeader(final String name) {
* @return an array of length &ge; 0
*/
public Header[] getHeaders(final String name) {
final List<Header> headersFound = new ArrayList<Header>();
List<Header> headersFound = null;
// HTTPCORE-361 : we don't use the for-each syntax, i.e.
// for (Header header : headers)
// as that creates an Iterator that needs to be garbage-collected
for (int i = 0; i < this.headers.size(); i++) {
final Header header = this.headers.get(i);
if (header.getName().equalsIgnoreCase(name)) {
if (headersFound == null) {
headersFound = new ArrayList<Header>();
}
headersFound.add(header);
}
}

return headersFound.toArray(new Header[headersFound.size()]);
return headersFound != null ? headersFound.toArray(new Header[headersFound.size()]) : EMPTY;
}

/**
Expand Down

0 comments on commit 040af07

Please sign in to comment.