forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOwinRequestExtensions.cs
More file actions
85 lines (65 loc) · 2.03 KB
/
OwinRequestExtensions.cs
File metadata and controls
85 lines (65 loc) · 2.03 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
// 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.Collections.Generic;
using System.Diagnostics.Contracts;
using Microsoft.Owin;
namespace System.Web.Http.Owin
{
internal static class OwinRequestExtensions
{
private const string ContentLengthHeaderName = "Content-Length";
private const string DisableRequestBufferingKey = "server.DisableRequestBuffering";
public static void DisableBuffering(this IOwinRequest request)
{
if (request == null)
{
throw new ArgumentNullException("request");
}
IDictionary<string, object> environment = request.Environment;
if (environment == null)
{
return;
}
Action action;
if (!environment.TryGetValue(DisableRequestBufferingKey, out action))
{
return;
}
Contract.Assert(action != null);
action.Invoke();
}
public static int? GetContentLength(this IOwinRequest request)
{
Contract.Assert(request != null);
IHeaderDictionary headers = request.Headers;
if (headers == null)
{
return null;
}
string[] values;
if (!headers.TryGetValue(ContentLengthHeaderName, out values))
{
return null;
}
if (values == null || values.Length != 1)
{
return null;
}
string value = values[0];
if (value == null)
{
return null;
}
int parsed;
if (!Int32.TryParse(value, out parsed))
{
return null;
}
if (parsed < 0)
{
return null;
}
return parsed;
}
}
}