13c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller/*
23c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * Copyright (C) 2014 Square, Inc.
33c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller *
43c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * Licensed under the Apache License, Version 2.0 (the "License");
53c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * you may not use this file except in compliance with the License.
63c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * You may obtain a copy of the License at
73c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller *
83c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller *      http://www.apache.org/licenses/LICENSE-2.0
93c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller *
103c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * Unless required by applicable law or agreed to in writing, software
113c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * distributed under the License is distributed on an "AS IS" BASIS,
123c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * See the License for the specific language governing permissions and
143c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * limitations under the License.
153c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller */
163c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerpackage com.squareup.okhttp.internal.spdy;
173c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
183c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport java.io.IOException;
193c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport java.util.List;
203c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport okio.BufferedSource;
213c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
223c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller/**
233c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * {@link com.squareup.okhttp.Protocol#HTTP_2 HTTP/2} only.
24e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller * Processes server-initiated HTTP requests on the client. Implementations must
25e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller * quickly dispatch callbacks to avoid creating a bottleneck.
263c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller *
27e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller * <p>While {@link #onReset} may occur at any time, the following callbacks are
28e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller * expected in order, correlated by stream ID.
29e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller * <ul>
30e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller *   <li>{@link #onRequest}</li>
31e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller *   <li>{@link #onHeaders} (unless canceled)</li>
32e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller *   <li>{@link #onData} (optional sequence of data frames)</li>
33e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller * </ul>
34e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller *
35e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller * <p>As a stream ID is scoped to a single HTTP/2 connection, implementations
36e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller * which target multiple connections should expect repetition of stream IDs.
373c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller *
383c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * <p>Return true to request cancellation of a pushed stream.  Note that this
393c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * does not guarantee future frames won't arrive on the stream ID.
403c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller */
413c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerpublic interface PushObserver {
423c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  /**
433c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller   * Describes the request that the server intends to push a response for.
443c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller   *
453c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller   * @param streamId server-initiated stream ID: an even number.
463c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller   * @param requestHeaders minimally includes {@code :method}, {@code :scheme},
473c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller   * {@code :authority}, and (@code :path}.
483c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller   */
493c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  boolean onRequest(int streamId, List<Header> requestHeaders);
503c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
513c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  /**
523c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller   * The response headers corresponding to a pushed request.  When {@code last}
533c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller   * is true, there are no data frames to follow.
543c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller   *
553c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller   * @param streamId server-initiated stream ID: an even number.
563c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller   * @param responseHeaders minimally includes {@code :status}.
573c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller   * @param last when true, there is no response data.
583c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller   */
593c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  boolean onHeaders(int streamId, List<Header> responseHeaders, boolean last);
603c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
613c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  /**
623c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller   * A chunk of response data corresponding to a pushed request.  This data
633c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller   * must either be read or skipped.
643c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller   *
653c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller   * @param streamId server-initiated stream ID: an even number.
663c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller   * @param source location of data corresponding with this stream ID.
673c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller   * @param byteCount number of bytes to read or skip from the source.
683c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller   * @param last when true, there are no data frames to follow.
693c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller   */
703c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  boolean onData(int streamId, BufferedSource source, int byteCount, boolean last)
713c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller      throws IOException;
723c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
73e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller  /** Indicates the reason why this stream was canceled. */
743c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  void onReset(int streamId, ErrorCode errorCode);
753c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
763c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  PushObserver CANCEL = new PushObserver() {
773c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
783c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller    @Override public boolean onRequest(int streamId, List<Header> requestHeaders) {
793c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller      return true;
803c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller    }
813c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
823c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller    @Override public boolean onHeaders(int streamId, List<Header> responseHeaders, boolean last) {
833c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller      return true;
843c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller    }
853c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
863c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller    @Override public boolean onData(int streamId, BufferedSource source, int byteCount,
873c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller        boolean last) throws IOException {
883c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller      source.skip(byteCount);
893c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller      return true;
903c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller    }
913c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
923c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller    @Override public void onReset(int streamId, ErrorCode errorCode) {
933c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller    }
943c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  };
953c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller}
96