1 /* 2 * Copyright (C) 2014 Square, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.squareup.okhttp.internal.spdy; 17 18 import java.io.IOException; 19 import java.util.List; 20 import okio.BufferedSource; 21 22 /** 23 * {@link com.squareup.okhttp.Protocol#HTTP_2 HTTP/2} only. 24 * Processes server-initiated HTTP requests on the client. 25 * 26 * <p>Use the stream ID to correlate response headers and data. 27 * 28 * <p>Return true to request cancellation of a pushed stream. Note that this 29 * does not guarantee future frames won't arrive on the stream ID. 30 */ 31 public interface PushObserver { 32 /** 33 * Describes the request that the server intends to push a response for. 34 * 35 * @param streamId server-initiated stream ID: an even number. 36 * @param requestHeaders minimally includes {@code :method}, {@code :scheme}, 37 * {@code :authority}, and (@code :path}. 38 */ 39 boolean onRequest(int streamId, List<Header> requestHeaders); 40 41 /** 42 * The response headers corresponding to a pushed request. When {@code last} 43 * is true, there are no data frames to follow. 44 * 45 * @param streamId server-initiated stream ID: an even number. 46 * @param responseHeaders minimally includes {@code :status}. 47 * @param last when true, there is no response data. 48 */ 49 boolean onHeaders(int streamId, List<Header> responseHeaders, boolean last); 50 51 /** 52 * A chunk of response data corresponding to a pushed request. This data 53 * must either be read or skipped. 54 * 55 * @param streamId server-initiated stream ID: an even number. 56 * @param source location of data corresponding with this stream ID. 57 * @param byteCount number of bytes to read or skip from the source. 58 * @param last when true, there are no data frames to follow. 59 */ 60 boolean onData(int streamId, BufferedSource source, int byteCount, boolean last) 61 throws IOException; 62 63 /** Indicates the reason why this stream was cancelled. */ 64 void onReset(int streamId, ErrorCode errorCode); 65 66 PushObserver CANCEL = new PushObserver() { 67 68 @Override public boolean onRequest(int streamId, List<Header> requestHeaders) { 69 return true; 70 } 71 72 @Override public boolean onHeaders(int streamId, List<Header> responseHeaders, boolean last) { 73 return true; 74 } 75 76 @Override public boolean onData(int streamId, BufferedSource source, int byteCount, 77 boolean last) throws IOException { 78 source.skip(byteCount); 79 return true; 80 } 81 82 @Override public void onReset(int streamId, ErrorCode errorCode) { 83 } 84 }; 85 } 86