This repository has been archived by the owner on Nov 20, 2018. It is now read-only.
/
HttpResponse.cs
109 lines (92 loc) · 4.64 KB
/
HttpResponse.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.IO;
using System.Threading.Tasks;
namespace Microsoft.AspNetCore.Http
{
/// <summary>
/// Represents the outgoing side of an individual HTTP request.
/// </summary>
public abstract class HttpResponse
{
private static readonly Func<object, Task> _callbackDelegate = callback => ((Func<Task>)callback)();
private static readonly Func<object, Task> _disposeDelegate = disposable =>
{
((IDisposable)disposable).Dispose();
return Task.CompletedTask;
};
/// <summary>
/// Gets the <see cref="HttpContext"/> for this response.
/// </summary>
public abstract HttpContext HttpContext { get; }
/// <summary>
/// Gets or sets the HTTP response code.
/// </summary>
public abstract int StatusCode { get; set; }
/// <summary>
/// Gets the response headers.
/// </summary>
public abstract IHeaderDictionary Headers { get; }
/// <summary>
/// Gets or sets the response body <see cref="Stream"/>.
/// </summary>
public abstract Stream Body { get; set; }
/// <summary>
/// Gets or sets the value for the <c>Content-Length</c> response header.
/// </summary>
public abstract long? ContentLength { get; set; }
/// <summary>
/// Gets or sets the value for the <c>Content-Type</c> response header.
/// </summary>
public abstract string ContentType { get; set; }
/// <summary>
/// Gets an object that can be used to manage cookies for this response.
/// </summary>
public abstract IResponseCookies Cookies { get; }
/// <summary>
/// Gets a value indicating whether response headers have been sent to the client.
/// </summary>
public abstract bool HasStarted { get; }
/// <summary>
/// Adds a delegate to be invoked just before response headers will be sent to the client.
/// </summary>
/// <param name="callback">The delegate to execute.</param>
/// <param name="state">A state object to capture and pass back to the delegate.</param>
public abstract void OnStarting(Func<object, Task> callback, object state);
/// <summary>
/// Adds a delegate to be invoked just before response headers will be sent to the client.
/// </summary>
/// <param name="callback">The delegate to execute.</param>
public virtual void OnStarting(Func<Task> callback) => OnStarting(_callbackDelegate, callback);
/// <summary>
/// Adds a delegate to be invoked after the response has finished being sent to the client.
/// </summary>
/// <param name="callback">The delegate to invoke.</param>
/// <param name="state">A state object to capture and pass back to the delegate.</param>
public abstract void OnCompleted(Func<object, Task> callback, object state);
/// <summary>
/// Registers an object for disposal by the host once the request has finished processing.
/// </summary>
/// <param name="disposable">The object to be disposed.</param>
public virtual void RegisterForDispose(IDisposable disposable) => OnCompleted(_disposeDelegate, disposable);
/// <summary>
/// Adds a delegate to be invoked after the response has finished being sent to the client.
/// </summary>
/// <param name="callback">The delegate to invoke.</param>
public virtual void OnCompleted(Func<Task> callback) => OnCompleted(_callbackDelegate, callback);
/// <summary>
/// Returns a temporary redirect response (HTTP 302) to the client.
/// </summary>
/// <param name="location">The URL to redirect the client to. This must be properly encoded for use in http headers
/// where only ASCII characters are allowed.</param>
public virtual void Redirect(string location) => Redirect(location, permanent: false);
/// <summary>
/// Returns a redirect response (HTTP 301 or HTTP 302) to the client.
/// </summary>
/// <param name="location">The URL to redirect the client to. This must be properly encoded for use in http headers
/// where only ASCII characters are allowed.</param>
/// <param name="permanent"><c>True</c> if the redirect is permanent (301), otherwise <c>false</c> (302).</param>
public abstract void Redirect(string location, bool permanent);
}
}