New tests.
[mono.git] / mcs / class / System.Web.Mvc2 / System.Web.Mvc / HttpRequestExtensions.cs
1 /* ****************************************************************************\r
2  *\r
3  * Copyright (c) Microsoft Corporation. All rights reserved.\r
4  *\r
5  * This software is subject to the Microsoft Public License (Ms-PL). \r
6  * A copy of the license can be found in the license.htm file included \r
7  * in this distribution.\r
8  *\r
9  * You must not remove this notice, or any other, from this software.\r
10  *\r
11  * ***************************************************************************/\r
12 \r
13 namespace System.Web.Mvc {\r
14     using System;\r
15 \r
16     public static class HttpRequestExtensions {\r
17         internal const string XHttpMethodOverrideKey = "X-HTTP-Method-Override";\r
18 \r
19         public static string GetHttpMethodOverride(this HttpRequestBase request) {\r
20             if (request == null) {\r
21                 throw new ArgumentNullException("request");\r
22             }\r
23 \r
24             string incomingVerb = request.HttpMethod;\r
25 \r
26             if (!String.Equals(incomingVerb, "POST", StringComparison.OrdinalIgnoreCase)) {\r
27                 return incomingVerb;\r
28             }\r
29 \r
30             string verbOverride = null;\r
31             string headerOverrideValue = request.Headers[XHttpMethodOverrideKey];\r
32             if (!String.IsNullOrEmpty(headerOverrideValue)) {\r
33                 verbOverride = headerOverrideValue;\r
34             }\r
35             else {\r
36                 string formOverrideValue = request.Form[XHttpMethodOverrideKey];\r
37                 if (!String.IsNullOrEmpty(formOverrideValue)) {\r
38                     verbOverride = formOverrideValue;\r
39                 }\r
40                 else {\r
41                     string queryStringOverrideValue = request.QueryString[XHttpMethodOverrideKey];\r
42                     if (!String.IsNullOrEmpty(queryStringOverrideValue)) {\r
43                         verbOverride = queryStringOverrideValue;\r
44                     }\r
45                 }\r
46             }\r
47             if (verbOverride != null) {\r
48                 if (!String.Equals(verbOverride, "GET", StringComparison.OrdinalIgnoreCase) &&\r
49                     !String.Equals(verbOverride, "POST", StringComparison.OrdinalIgnoreCase)) {\r
50                     incomingVerb = verbOverride;\r
51                 }\r
52             }\r
53             return incomingVerb;\r
54         }\r
55     }\r
56 }\r