001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/main/java/org/apache/http/protocol/ResponseConnControl.java $
003: * $Revision: 586026 $
004: * $Date: 2007-10-18 18:22:27 +0200 (Thu, 18 Oct 2007) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.protocol;
033:
034: import java.io.IOException;
035:
036: import org.apache.http.Header;
037: import org.apache.http.HttpEntity;
038: import org.apache.http.HttpException;
039: import org.apache.http.HttpRequest;
040: import org.apache.http.HttpResponse;
041: import org.apache.http.HttpResponseInterceptor;
042: import org.apache.http.HttpStatus;
043: import org.apache.http.HttpVersion;
044: import org.apache.http.ProtocolVersion;
045:
046: /**
047: * A response interceptor that suggests connection keep-alive to the client.
048: * For use on the server side.
049: *
050: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
051: *
052: * @version $Revision: 586026 $
053: *
054: * @since 4.0
055: */
056: public class ResponseConnControl implements HttpResponseInterceptor {
057:
058: public ResponseConnControl() {
059: super ();
060: }
061:
062: public void process(final HttpResponse response,
063: final HttpContext context) throws HttpException,
064: IOException {
065: if (response == null) {
066: throw new IllegalArgumentException(
067: "HTTP response may not be null");
068: }
069: if (context == null) {
070: throw new IllegalArgumentException(
071: "HTTP context may not be null");
072: }
073: // Always drop connection after certain type of responses
074: int status = response.getStatusLine().getStatusCode();
075: if (status == HttpStatus.SC_BAD_REQUEST
076: || status == HttpStatus.SC_REQUEST_TIMEOUT
077: || status == HttpStatus.SC_LENGTH_REQUIRED
078: || status == HttpStatus.SC_REQUEST_TOO_LONG
079: || status == HttpStatus.SC_REQUEST_URI_TOO_LONG
080: || status == HttpStatus.SC_SERVICE_UNAVAILABLE
081: || status == HttpStatus.SC_NOT_IMPLEMENTED) {
082: response.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE);
083: return;
084: }
085: // Always drop connection for HTTP/1.0 responses and below
086: // if the content body cannot be correctly delimited
087: HttpEntity entity = response.getEntity();
088: if (entity != null) {
089: ProtocolVersion ver = response.getStatusLine()
090: .getProtocolVersion();
091: if (entity.getContentLength() < 0
092: && (!entity.isChunked() || ver
093: .lessEquals(HttpVersion.HTTP_1_0))) {
094: response
095: .setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE);
096: return;
097: }
098: }
099: // Drop connection if requested by the client
100: HttpRequest request = (HttpRequest) context
101: .getAttribute(ExecutionContext.HTTP_REQUEST);
102: if (request != null) {
103: Header header = request.getFirstHeader(HTTP.CONN_DIRECTIVE);
104: if (header != null) {
105: response.setHeader(HTTP.CONN_DIRECTIVE, header
106: .getValue());
107: }
108: }
109: }
110:
111: }
|