forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpExtensions.cs
More file actions
118 lines (102 loc) · 4.45 KB
/
HttpExtensions.cs
File metadata and controls
118 lines (102 loc) · 4.45 KB
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
110
111
112
113
114
115
116
117
118
using System;
using System.Net;
using System.Web;
using ServiceStack.Web;
namespace ServiceStack
{
public static class HttpExtensions
{
public static string ToAbsoluteUri(this IReturn requestDto, string httpMethod = null, string formatFallbackToPredefinedRoute = null)
{
var relativeUrl = requestDto.ToUrl(
httpMethod ?? HttpMethods.Get,
formatFallbackToPredefinedRoute ?? HostContext.Config.DefaultContentType.ToContentFormat());
return relativeUrl.ToAbsoluteUri();
}
public static string ToAbsoluteUri(this object requestDto, string httpMethod = null, string formatFallbackToPredefinedRoute = null)
{
var relativeUrl = requestDto.ToUrl(
httpMethod ?? HttpMethods.Get,
formatFallbackToPredefinedRoute ?? HostContext.Config.DefaultContentType.ToContentFormat());
return relativeUrl.ToAbsoluteUri();
}
public static string ToAbsoluteUri(this object requestDto, IRequest req, string httpMethod = null, string formatFallbackToPredefinedRoute = null)
{
var relativeUrl = requestDto.ToUrl(
httpMethod ?? HttpMethods.Get,
formatFallbackToPredefinedRoute ?? HostContext.Config.DefaultContentType.ToContentFormat());
return relativeUrl.ToAbsoluteUri(req);
}
public static string ToAbsoluteUri(this string relativeUrl, IRequest req = null)
{
if (req == null)
req = HostContext.TryGetCurrentRequest();
var absoluteUrl = HostContext.ResolveAbsoluteUrl("~/".CombineWith(relativeUrl), req);
return absoluteUrl;
}
/// <summary>
/// End a ServiceStack Request
/// </summary>
public static void EndRequest(this HttpResponseBase httpRes, bool skipHeaders = false)
{
if (!skipHeaders) httpRes.ApplyGlobalResponseHeaders();
httpRes.Close();
HostContext.CompleteRequest(null);
}
/// <summary>
/// End a ServiceStack Request
/// </summary>
public static void EndRequest(this IResponse httpRes, bool skipHeaders = false)
{
httpRes.EndHttpHandlerRequest(skipHeaders: skipHeaders);
}
/// <summary>
/// End a HttpHandler Request
/// </summary>
public static void EndHttpHandlerRequest(this HttpContextBase context, bool skipHeaders = false, bool skipClose = false, bool closeOutputStream = false, Action<HttpResponseBase> afterHeaders = null)
{
var httpRes = context.Response;
if (!skipHeaders) httpRes.ApplyGlobalResponseHeaders();
if (afterHeaders != null) afterHeaders(httpRes);
if (closeOutputStream) httpRes.CloseOutputStream();
else if (!skipClose) httpRes.Close();
HostContext.CompleteRequest(context.ToRequest());
//skipHeaders used when Apache+mod_mono doesn't like:
//response.OutputStream.Flush();
//response.Close();
}
/// <summary>
/// End a HttpHandler Request
/// </summary>
public static void EndHttpHandlerRequest(this IResponse httpRes, bool skipHeaders = false, bool skipClose = false, Action<IResponse> afterHeaders = null)
{
if (!skipHeaders) httpRes.ApplyGlobalResponseHeaders();
if (afterHeaders != null) afterHeaders(httpRes);
if (!skipClose && !httpRes.IsClosed) httpRes.Close();
HostContext.CompleteRequest(httpRes.Request);
}
/// <summary>
/// End an MQ Request
/// </summary>
public static void EndMqRequest(this IResponse httpRes, bool skipClose = false)
{
if (!skipClose && !httpRes.IsClosed) httpRes.Close();
HostContext.CompleteRequest(httpRes.Request);
}
/// <summary>
/// End a ServiceStack Request with no content
/// </summary>
public static void EndRequestWithNoContent(this IResponse httpRes)
{
if (HostContext.Config == null || HostContext.Config.Return204NoContentForEmptyResponse)
{
if (httpRes.StatusCode == (int)HttpStatusCode.OK)
{
httpRes.StatusCode = (int)HttpStatusCode.NoContent;
}
}
httpRes.SetContentLength(0);
httpRes.EndRequest();
}
}
}