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;
20  
21  import java.net.HttpCookie;
22  import java.net.URI;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.eclipse.jetty.client.api.Authentication;
27  import org.eclipse.jetty.client.api.Connection;
28  import org.eclipse.jetty.client.api.ContentProvider;
29  import org.eclipse.jetty.client.api.Request;
30  import org.eclipse.jetty.client.api.Response;
31  import org.eclipse.jetty.http.HttpField;
32  import org.eclipse.jetty.http.HttpFields;
33  import org.eclipse.jetty.http.HttpHeader;
34  import org.eclipse.jetty.http.HttpHeaderValue;
35  import org.eclipse.jetty.http.HttpMethod;
36  import org.eclipse.jetty.http.HttpVersion;
37  
38  public abstract class HttpConnection implements Connection
39  {
40      private static final HttpField CHUNKED_FIELD = new HttpField(HttpHeader.TRANSFER_ENCODING, HttpHeaderValue.CHUNKED);
41  
42      private final HttpDestination destination;
43  
44      protected HttpConnection(HttpDestination destination)
45      {
46          this.destination = destination;
47      }
48  
49      public HttpClient getHttpClient()
50      {
51          return destination.getHttpClient();
52      }
53  
54      public HttpDestination getHttpDestination()
55      {
56          return destination;
57      }
58  
59      @Override
60      public void send(Request request, Response.CompleteListener listener)
61      {
62          ArrayList<Response.ResponseListener> listeners = new ArrayList<>(2);
63          if (request.getTimeout() > 0)
64          {
65              TimeoutCompleteListener timeoutListener = new TimeoutCompleteListener(request);
66              timeoutListener.schedule(getHttpClient().getScheduler());
67              listeners.add(timeoutListener);
68          }
69          if (listener != null)
70              listeners.add(listener);
71  
72          HttpExchange exchange = new HttpExchange(getHttpDestination(), (HttpRequest)request, listeners);
73  
74          send(exchange);
75      }
76  
77      protected abstract void send(HttpExchange exchange);
78  
79      protected void normalizeRequest(Request request)
80      {
81          String method = request.getMethod();
82          HttpVersion version = request.getVersion();
83          HttpFields headers = request.getHeaders();
84          ContentProvider content = request.getContent();
85          ProxyConfiguration.Proxy proxy = destination.getProxy();
86  
87          // Make sure the path is there
88          String path = request.getPath();
89          if (path.trim().length() == 0)
90          {
91              path = "/";
92              request.path(path);
93          }
94          if (proxy != null && !HttpMethod.CONNECT.is(method))
95          {
96              path = request.getURI().toString();
97              request.path(path);
98          }
99  
100         // If we are HTTP 1.1, add the Host header
101         if (version.getVersion() > 10)
102         {
103             if (!headers.containsKey(HttpHeader.HOST.asString()))
104                 headers.put(getHttpDestination().getHostField());
105         }
106 
107         if (request.getAgent() == null)
108             headers.put(getHttpClient().getUserAgentField());
109 
110         // Add content headers
111         if (content != null)
112         {
113             long contentLength = content.getLength();
114             if (contentLength >= 0)
115             {
116                 if (!headers.containsKey(HttpHeader.CONTENT_LENGTH.asString()))
117                     headers.put(HttpHeader.CONTENT_LENGTH, String.valueOf(contentLength));
118             }
119             else
120             {
121                 if (!headers.containsKey(HttpHeader.TRANSFER_ENCODING.asString()))
122                     headers.put(CHUNKED_FIELD);
123             }
124         }
125 
126         // Cookies
127         List<HttpCookie> cookies = getHttpClient().getCookieStore().get(request.getURI());
128         StringBuilder cookieString = null;
129         for (int i = 0; i < cookies.size(); ++i)
130         {
131             if (cookieString == null)
132                 cookieString = new StringBuilder();
133             if (i > 0)
134                 cookieString.append("; ");
135             HttpCookie cookie = cookies.get(i);
136             cookieString.append(cookie.getName()).append("=").append(cookie.getValue());
137         }
138         if (cookieString != null)
139             request.header(HttpHeader.COOKIE.asString(), cookieString.toString());
140 
141         // Authorization
142         URI authenticationURI = proxy != null ? proxy.getURI() : request.getURI();
143         if (authenticationURI != null)
144         {
145             Authentication.Result authnResult = getHttpClient().getAuthenticationStore().findAuthenticationResult(authenticationURI);
146             if (authnResult != null)
147                 authnResult.apply(request);
148         }
149     }
150 
151     @Override
152     public String toString()
153     {
154         return String.format("%s@%h", getClass().getSimpleName(), this);
155     }
156 }