View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.client.http;
20  
21  import org.eclipse.jetty.client.HttpChannel;
22  import org.eclipse.jetty.client.HttpExchange;
23  import org.eclipse.jetty.client.api.Response;
24  import org.eclipse.jetty.client.api.Result;
25  import org.eclipse.jetty.http.HttpFields;
26  import org.eclipse.jetty.http.HttpHeader;
27  import org.eclipse.jetty.http.HttpHeaderValue;
28  import org.eclipse.jetty.http.HttpVersion;
29  
30  public class HttpChannelOverHTTP extends HttpChannel
31  {
32      private final HttpConnectionOverHTTP connection;
33      private final HttpSenderOverHTTP sender;
34      private final HttpReceiverOverHTTP receiver;
35  
36      public HttpChannelOverHTTP(HttpConnectionOverHTTP connection)
37      {
38          super(connection.getHttpDestination());
39          this.connection = connection;
40          this.sender = new HttpSenderOverHTTP(this);
41          this.receiver = new HttpReceiverOverHTTP(this);
42      }
43  
44      public HttpConnectionOverHTTP getHttpConnection()
45      {
46          return connection;
47      }
48  
49      @Override
50      public void send()
51      {
52          HttpExchange exchange = getHttpExchange();
53          if (exchange != null)
54              sender.send(exchange);
55      }
56  
57      @Override
58      public void proceed(HttpExchange exchange, Throwable failure)
59      {
60          sender.proceed(exchange, failure);
61      }
62  
63      @Override
64      public boolean abort(Throwable cause)
65      {
66          // We want the return value to be that of the response
67          // because if the response has already successfully
68          // arrived then we failed to abort the exchange
69          sender.abort(cause);
70          return receiver.abort(cause);
71      }
72  
73      public void receive()
74      {
75          receiver.receive();
76      }
77  
78      @Override
79      public void exchangeTerminated(Result result)
80      {
81          super.exchangeTerminated(result);
82          Response response = result.getResponse();
83          HttpFields responseHeaders = response.getHeaders();
84          boolean close = result.isFailed() || receiver.isShutdown();
85          // Only check HTTP headers if there are no failures.
86          if (!close)
87          {
88              if (response.getVersion().compareTo(HttpVersion.HTTP_1_1) < 0)
89              {
90                  // HTTP 1.0 must close the connection unless it has an explicit keep alive.
91                  close = !responseHeaders.contains(HttpHeader.CONNECTION, HttpHeaderValue.KEEP_ALIVE.asString());
92              }
93              else
94              {
95                  // HTTP 1.1 or greater closes only if it has an explicit close.
96                  close = responseHeaders.contains(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.asString());
97              }
98          }
99          if (close)
100             connection.close();
101         else
102             connection.release();
103     }
104 
105     @Override
106     public String toString()
107     {
108         return String.format("%s@%x", getClass().getSimpleName(), hashCode());
109     }
110 }