private boolean checkSendHeaders(boolean end) { if (!headWritten) { if (headersEndHandler != null) { headersEndHandler.handle(null); } sanitizeHeaders(); if (Metrics.METRICS_ENABLED && metric != null) { conn.metrics().responseBegin(metric, this); } headWritten = true; headers.status(Integer.toString(status.code())); // Could be optimized for usual case ? stream.writeHeaders(headers, end); if (end) { ctx.flush(); } return true; } else { return false; } }
private static boolean validateHeadersSentState(Http2Stream stream, Http2Headers headers, boolean isServer, boolean endOfStream) { boolean isInformational = isServer && HttpStatusClass.valueOf(headers.status()) == INFORMATIONAL; if ((isInformational || !endOfStream) && stream.isHeadersSent() || stream.isTrailersSent()) { throw new IllegalStateException("Stream " + stream.id() + " sent too many headers EOS: " + endOfStream); } return isInformational; }
boolean validateHttpHeaders) throws Http2Exception { HttpResponseStatus status = parseStatus(http2Headers.status());
private void handleEndOfStream(final ChannelHandlerContext context, final Http2Stream stream, final Http2Headers headers, final ByteBuf data) { final PushNotificationPromise<ApnsPushNotification, PushNotificationResponse<ApnsPushNotification>> responsePromise = stream.getProperty(this.responsePromisePropertyKey); final ApnsPushNotification pushNotification = responsePromise.getPushNotification(); final HttpResponseStatus status = HttpResponseStatus.parseLine(headers.status()); if (HttpResponseStatus.OK.equals(status)) { responsePromise.trySuccess(new SimplePushNotificationResponse<>(responsePromise.getPushNotification(), true, getApnsIdFromHeaders(headers), null, null)); } else { if (data != null) { final ErrorResponse errorResponse = GSON.fromJson(data.toString(StandardCharsets.UTF_8), ErrorResponse.class); this.handleErrorResponse(context, stream.id(), headers, pushNotification, errorResponse); } else { log.warn("Gateway sent an end-of-stream HEADERS frame for an unsuccessful notification."); } } }
int id = stream == null ? 0 : stream.id(); final CharSequence status = headers.status();
@Override public void onPushPromiseRead(ChannelHandlerContext ctx, int streamId, int promisedStreamId, Http2Headers headers, int padding) throws Http2Exception { // A push promise should not be allowed to add headers to an existing stream Http2Stream promisedStream = connection.stream(promisedStreamId); if (headers.status() == null) { // A PUSH_PROMISE frame has no Http response status. // https://tools.ietf.org/html/rfc7540#section-8.2.1 // Server push is semantically equivalent to a server responding to a // request; however, in this case, that request is also sent by the // server, as a PUSH_PROMISE frame. headers.status(OK.codeAsText()); } FullHttpMessage msg = processHeadersBegin(ctx, promisedStream, headers, false, false, false); if (msg == null) { throw connectionError(PROTOCOL_ERROR, "Push Promise Frame received for pre-existing stream id %d", promisedStreamId); } msg.headers().setInt(HttpConversionUtil.ExtensionHeaderNames.STREAM_PROMISE_ID.text(), streamId); msg.headers().setShort(HttpConversionUtil.ExtensionHeaderNames.STREAM_WEIGHT.text(), Http2CodecUtil.DEFAULT_PRIORITY_WEIGHT); processHeadersEnd(ctx, promisedStream, msg, false); }
/** * Create a new object to contain the response data. * * @param streamId The stream associated with the response * @param http2Headers The initial set of HTTP/2 headers to create the response with * @param validateHttpHeaders <ul> * <li>{@code true} to validate HTTP headers in the http-codec</li> * <li>{@code false} not to validate HTTP headers in the http-codec</li> * </ul> * @return A new response object which represents headers for a chunked response * @throws Http2Exception see {@link #addHttp2ToHttpHeaders(int, Http2Headers, * HttpHeaders, HttpVersion, boolean, boolean)} */ public static HttpResponse toHttpResponse(final int streamId, final Http2Headers http2Headers, final boolean validateHttpHeaders) throws Http2Exception { final HttpResponseStatus status = parseStatus(http2Headers.status()); // HTTP/2 does not define a way to carry the version or reason phrase that is included in an // HTTP/1.1 status line. final HttpResponse msg = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status, validateHttpHeaders); try { addHttp2ToHttpHeaders(streamId, http2Headers, msg.headers(), msg.protocolVersion(), false, true); } catch (final Http2Exception e) { throw e; } catch (final Throwable t) { throw streamError(streamId, PROTOCOL_ERROR, t, "HTTP/2 to HTTP/1.x headers conversion error"); } return msg; }
String statusMessage; try { status = Integer.parseInt(headers.status().toString()); statusMessage = HttpResponseStatus.valueOf(status).reasonPhrase(); } catch (Exception e) {
out.status(response.status().codeAsText());
protected void handleErrorResponse(final ChannelHandlerContext context, final int streamId, final Http2Headers headers, final ApnsPushNotification pushNotification, final ErrorResponse errorResponse) { final PushNotificationPromise<ApnsPushNotification, PushNotificationResponse<ApnsPushNotification>> responsePromise = this.connection().stream(streamId).getProperty(this.responsePromisePropertyKey); final HttpResponseStatus status = HttpResponseStatus.parseLine(headers.status()); responsePromise.trySuccess(new SimplePushNotificationResponse<>(responsePromise.getPushNotification(), HttpResponseStatus.OK.equals(status), getApnsIdFromHeaders(headers), errorResponse.getReason(), errorResponse.getTimestamp())); }
private boolean checkSendHeaders(boolean end) { if (!headWritten) { if (headersEndHandler != null) { headersEndHandler.handle(null); } sanitizeHeaders(); if (Metrics.METRICS_ENABLED && metric != null) { conn.metrics().responseBegin(metric, this); } headWritten = true; headers.status(Integer.toString(status.code())); // Could be optimized for usual case ? stream.writeHeaders(headers, end); if (end) { ctx.flush(); } return true; } else { return false; } }
HttpStatusClass.valueOf(headers.status()) == INFORMATIONAL; if ((isInformational || !endOfStream) && stream.isHeadersReceived() || stream.isTrailersReceived()) { throw streamError(streamId, PROTOCOL_ERROR,
String statusMessage; try { status = Integer.parseInt(headers.status().toString()); statusMessage = HttpResponseStatus.valueOf(status).reasonPhrase(); } catch (Exception e) {
public HttpResponseStatus status() { try { return HttpConversionUtil.parseStatus(delegate.status()); } catch (Http2Exception e) { throw new RuntimeException(e); } }
/** * Throws a RuntimeException if the underlying status cannot be converted to an HttpResponseStatus */ @Override public HttpResponseStatus status() { try { return HttpConversionUtil.parseStatus(delegate.status()); } catch (Http2Exception e) { throw new RuntimeException(e); } }
public HttpResponseStatus getStatus() { return HttpResponseStatus.parseLine(headers.status()); }
private static boolean validateHeadersSentState(Http2Stream stream, Http2Headers headers, boolean isServer, boolean endOfStream) { boolean isInformational = isServer && HttpStatusClass.valueOf(headers.status()) == INFORMATIONAL; if ((isInformational || !endOfStream) && stream.isHeadersSent() || stream.isTrailersSent()) { throw new IllegalStateException("Stream " + stream.id() + " sent too many headers EOS: " + endOfStream); } return isInformational; }
private static boolean validateHeadersSentState(Http2Stream stream, Http2Headers headers, boolean isServer, boolean endOfStream) { boolean isInformational = isServer && HttpStatusClass.valueOf(headers.status()) == INFORMATIONAL; if ((isInformational || !endOfStream) && stream.isHeadersSent() || stream.isTrailersSent()) { throw new IllegalStateException("Stream " + stream.id() + " sent too many headers EOS: " + endOfStream); } return isInformational; }
protected void handleErrorResponse(final ChannelHandlerContext context, final int streamId, final Http2Headers headers, final ApnsPushNotification pushNotification, final ErrorResponse errorResponse) { final PushNotificationPromise<ApnsPushNotification, PushNotificationResponse<ApnsPushNotification>> responsePromise = this.connection().stream(streamId).getProperty(this.responsePromisePropertyKey); final HttpResponseStatus status = HttpResponseStatus.parseLine(headers.status()); responsePromise.trySuccess(new SimplePushNotificationResponse<>(responsePromise.getPushNotification(), HttpResponseStatus.OK.equals(status), getApnsIdFromHeaders(headers), errorResponse.getReason(), errorResponse.getTimestamp())); }
private void sendAPushPromise(ChannelHandlerContext ctx, int streamId, int pushPromiseStreamId, ByteBuf payload) throws Http2Exception { encoder().writePushPromise(ctx, streamId, pushPromiseStreamId, new DefaultHttp2Headers().status(OK.codeAsText()), 0, ctx.newPromise()); //Http2Stream stream = connection.local().reservePushStream(pushPromiseStreamId, connection.connectionStream()); Http2Headers headers = new DefaultHttp2Headers(); headers.status(OK.codeAsText()); encoder().writeHeaders(ctx, pushPromiseStreamId, headers, 0, false, ctx.newPromise()); encoder().writeData(ctx, pushPromiseStreamId, payload, 0, false, ctx.newPromise()); }