public static MessageBody For( HttpVersion httpVersion, FrameRequestHeaders headers, Frame context) { // see also http://tools.ietf.org/html/rfc2616#section-4.4 var keepAlive = httpVersion != HttpVersion.Http10; var connection = headers.HeaderConnection; var upgrade = false; if (connection.Count > 0) { var connectionOptions = FrameHeaders.ParseConnection(connection); upgrade = (connectionOptions & ConnectionOptions.Upgrade) == ConnectionOptions.Upgrade; keepAlive = (connectionOptions & ConnectionOptions.KeepAlive) == ConnectionOptions.KeepAlive; } var transferEncoding = headers.HeaderTransferEncoding; if (transferEncoding.Count > 0) { var transferCoding = FrameHeaders.GetFinalTransferCoding(headers.HeaderTransferEncoding); // https://tools.ietf.org/html/rfc7230#section-3.3.3 // If a Transfer-Encoding header field // is present in a request and the chunked transfer coding is not // the final encoding, the message body length cannot be determined // reliably; the server MUST respond with the 400 (Bad Request) // status code and then close the connection. if (transferCoding != TransferCoding.Chunked) { context.ThrowRequestRejected(RequestRejectionReason.FinalTransferCodingNotChunked, transferEncoding.ToString()); } if (upgrade) { context.ThrowRequestRejected(RequestRejectionReason.UpgradeRequestCannotHavePayload); } return(new ForChunkedEncoding(keepAlive, context)); } if (headers.ContentLength.HasValue) { var contentLength = headers.ContentLength.Value; if (contentLength == 0) { return(keepAlive ? _zeroContentLengthKeepAlive : _zeroContentLengthClose); } else if (upgrade) { context.ThrowRequestRejected(RequestRejectionReason.UpgradeRequestCannotHavePayload); } return(new ForContentLength(keepAlive, contentLength, context)); } // Avoid slowing down most common case if (!object.ReferenceEquals(context.Method, HttpMethods.Get)) { // If we got here, request contains no Content-Length or Transfer-Encoding header. // Reject with 411 Length Required. if (HttpMethods.IsPost(context.Method) || HttpMethods.IsPut(context.Method)) { var requestRejectionReason = httpVersion == HttpVersion.Http11 ? RequestRejectionReason.LengthRequired : RequestRejectionReason.LengthRequiredHttp10; context.ThrowRequestRejected(requestRejectionReason, context.Method); } } if (upgrade) { return(new ForUpgrade(context)); } return(keepAlive ? _zeroContentLengthKeepAlive : _zeroContentLengthClose); }