1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
67
68
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
86 if (!close)
87 {
88 if (response.getVersion().compareTo(HttpVersion.HTTP_1_1) < 0)
89 {
90
91 close = !responseHeaders.contains(HttpHeader.CONNECTION, HttpHeaderValue.KEEP_ALIVE.asString());
92 }
93 else
94 {
95
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 }