public static AppDelegate Middleware(AppDelegate app) { return (call, callback) => app( call, (result, error) => { if (error != null || IsStatusWithNoNoEntityBody(result.Status) || result.Headers.ContainsKey("Content-Length") || result.Headers.ContainsKey("Transfer-Encoding")) { callback(result, error); } else { var buffer = new DataBuffer(); result.Body.Invoke( buffer.Add, ex => { buffer.End(ex); result.Headers.SetHeader("Content-Length", buffer.GetCount().ToString()); result.Body = buffer.Body; callback(result, null); }, call.CallDisposed); } }); }
public static AppDelegate Middleware(AppDelegate app) { return (env, result, fault) => app( env, (status, headers, body) => { if (IsStatusWithNoNoEntityBody(status) || headers.ContainsKey("Content-Length") || headers.ContainsKey("Transfer-Encoding")) { result(status, headers, body); } else { var token = CancellationToken.None; object obj; if (env.TryGetValue(typeof(CancellationToken).FullName, out obj) && obj is CancellationToken) token = (CancellationToken)obj; var buffer = new DataBuffer(); body( buffer.Add, ex => { buffer.End(ex); headers["Content-Length"] = new[] { buffer.GetCount().ToString() }; result(status, headers, buffer.Body); }, token); } }, fault); }