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.http2.client.http;
20  
21  import java.io.IOException;
22  
23  import org.eclipse.jetty.client.HttpChannel;
24  import org.eclipse.jetty.client.HttpExchange;
25  import org.eclipse.jetty.client.HttpReceiver;
26  import org.eclipse.jetty.client.HttpResponse;
27  import org.eclipse.jetty.http.HttpField;
28  import org.eclipse.jetty.http.HttpFields;
29  import org.eclipse.jetty.http.MetaData;
30  import org.eclipse.jetty.http2.ErrorCode;
31  import org.eclipse.jetty.http2.api.Stream;
32  import org.eclipse.jetty.http2.frames.DataFrame;
33  import org.eclipse.jetty.http2.frames.HeadersFrame;
34  import org.eclipse.jetty.http2.frames.PushPromiseFrame;
35  import org.eclipse.jetty.http2.frames.ResetFrame;
36  import org.eclipse.jetty.util.Callback;
37  
38  public class HttpReceiverOverHTTP2 extends HttpReceiver implements Stream.Listener
39  {
40      public HttpReceiverOverHTTP2(HttpChannel channel)
41      {
42          super(channel);
43      }
44  
45      @Override
46      protected HttpChannelOverHTTP2 getHttpChannel()
47      {
48          return (HttpChannelOverHTTP2)super.getHttpChannel();
49      }
50  
51      @Override
52      public void onHeaders(Stream stream, HeadersFrame frame)
53      {
54          HttpExchange exchange = getHttpExchange();
55          if (exchange == null)
56              return;
57  
58          HttpResponse response = exchange.getResponse();
59          MetaData.Response metaData = (MetaData.Response)frame.getMetaData();
60          response.version(metaData.getVersion()).status(metaData.getStatus()).reason(metaData.getReason());
61  
62          if (responseBegin(exchange))
63          {
64              HttpFields headers = metaData.getFields();
65              for (HttpField header : headers)
66              {
67                  if (!responseHeader(exchange, header))
68                      return;
69              }
70  
71              if (responseHeaders(exchange))
72              {
73                  if (frame.isEndStream())
74                      responseSuccess(exchange);
75              }
76          }
77      }
78  
79      @Override
80      public Stream.Listener onPush(Stream stream, PushPromiseFrame frame)
81      {
82          // Not supported.
83          stream.reset(new ResetFrame(stream.getId(), ErrorCode.REFUSED_STREAM_ERROR.code), Callback.NOOP);
84          return null;
85      }
86  
87      @Override
88      public void onData(Stream stream, DataFrame frame, Callback callback)
89      {
90          HttpExchange exchange = getHttpExchange();
91          if (exchange == null)
92              return;
93  
94          if (responseContent(exchange, frame.getData(), callback))
95          {
96              if (frame.isEndStream())
97                  responseSuccess(exchange);
98          }
99      }
100 
101     @Override
102     public void onReset(Stream stream, ResetFrame frame)
103     {
104         HttpExchange exchange = getHttpExchange();
105         if (exchange == null)
106             return;
107 
108         ErrorCode error = ErrorCode.from(frame.getError());
109         String reason = error == null ? "reset" : error.name().toLowerCase();
110         exchange.getRequest().abort(new IOException(reason));
111     }
112 
113     @Override
114     public void onTimeout(Stream stream, Throwable failure)
115     {
116         responseFailure(failure);
117     }
118 }