View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 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.HttpReceiver;
24  import org.eclipse.jetty.client.HttpSender;
25  import org.eclipse.jetty.client.api.Response;
26  import org.eclipse.jetty.client.api.Result;
27  import org.eclipse.jetty.http.HttpFields;
28  import org.eclipse.jetty.http.HttpHeader;
29  import org.eclipse.jetty.http.HttpHeaderValue;
30  import org.eclipse.jetty.http.HttpVersion;
31  
32  public class HttpChannelOverHTTP extends HttpChannel
33  {
34      private final HttpConnectionOverHTTP connection;
35      private final HttpSenderOverHTTP sender;
36      private final HttpReceiverOverHTTP receiver;
37  
38      public HttpChannelOverHTTP(HttpConnectionOverHTTP connection)
39      {
40          super(connection.getHttpDestination());
41          this.connection = connection;
42          this.sender = newHttpSender();
43          this.receiver = newHttpReceiver();
44      }
45  
46      protected HttpSenderOverHTTP newHttpSender()
47      {
48          return new HttpSenderOverHTTP(this);
49      }
50  
51      protected HttpReceiverOverHTTP newHttpReceiver()
52      {
53          return new HttpReceiverOverHTTP(this);
54      }
55  
56      @Override
57      protected HttpSender getHttpSender()
58      {
59          return sender;
60      }
61  
62      @Override
63      protected HttpReceiver getHttpReceiver()
64      {
65          return receiver;
66      }
67  
68      public HttpConnectionOverHTTP getHttpConnection()
69      {
70          return connection;
71      }
72  
73      @Override
74      public void send()
75      {
76          HttpExchange exchange = getHttpExchange();
77          if (exchange != null)
78              sender.send(exchange);
79      }
80  
81      @Override
82      public void release()
83      {
84          connection.release();
85      }
86  
87      public void receive()
88      {
89          receiver.receive();
90      }
91  
92      @Override
93      public void exchangeTerminated(HttpExchange exchange, Result result)
94      {
95          super.exchangeTerminated(exchange, result);
96  
97          Response response = result.getResponse();
98          HttpFields responseHeaders = response.getHeaders();
99          boolean close = result.isFailed() || receiver.isShutdown();
100 
101         if (!close)
102         {
103             if (response.getVersion().compareTo(HttpVersion.HTTP_1_1) < 0)
104             {
105                 // HTTP 1.0 must close the connection unless it has an explicit keep alive.
106                 close = !responseHeaders.contains(HttpHeader.CONNECTION, HttpHeaderValue.KEEP_ALIVE.asString());
107             }
108             else
109             {
110                 // HTTP 1.1 or greater closes only if it has an explicit close.
111                 close = responseHeaders.contains(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.asString());
112             }
113         }
114 
115         if (close)
116             connection.close();
117         else
118             release();
119     }
120 
121     @Override
122     public String toString()
123     {
124         return String.format("%s[send=%s,recv=%s]",
125                 super.toString(),
126                 sender,
127                 receiver);
128     }
129 }