Merge pull request #1032 from miguelzf/master
[mono.git] / mcs / class / System.Net.Http / System.Net.Http / HttpMethod.cs
1 //
2 // HttpMethod.cs
3 //
4 // Authors:
5 //      Marek Safar  <marek.safar@gmail.com>
6 //
7 // Copyright (C) 2011 Xamarin Inc (http://www.xamarin.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 namespace System.Net.Http
30 {
31         public class HttpMethod : IEquatable<HttpMethod>
32         {
33                 static readonly HttpMethod delete_method = new HttpMethod ("DELETE");
34                 static readonly HttpMethod get_method = new HttpMethod ("GET");
35                 static readonly HttpMethod head_method = new HttpMethod ("HEAD");
36                 static readonly HttpMethod options_method = new HttpMethod ("OPTIONS");
37                 static readonly HttpMethod post_method = new HttpMethod ("POST");
38                 static readonly HttpMethod put_method = new HttpMethod ("PUT");
39                 static readonly HttpMethod trace_method = new HttpMethod ("TRACE");
40
41                 readonly string method;
42
43                 public HttpMethod (string method)
44                 {
45                         if (string.IsNullOrEmpty (method))
46                                 throw new ArgumentException ("method");
47
48                         Headers.Parser.Token.Check (method);
49
50                         this.method = method;
51                 }
52
53                 public static HttpMethod Delete {
54                         get {
55                                 return delete_method;
56                         }
57                 }
58
59                 public static HttpMethod Get {
60                         get {
61                                 return get_method;
62                         }
63                 }
64
65                 public static HttpMethod Head {
66                         get {
67                                 return head_method;
68                         }
69                 }
70
71                 public string Method {
72                         get {
73                                 return method;
74                         }
75                 }
76
77                 public static HttpMethod Options {
78                         get {
79                                 return options_method;
80                         }
81                 }
82
83                 public static HttpMethod Post {
84                         get {
85                                 return post_method;
86                         }
87                 }
88
89                 public static HttpMethod Put {
90                         get {
91                                 return put_method;
92                         }
93                 }
94
95                 public static HttpMethod Trace {
96                         get {
97                                 return trace_method;
98                         }
99                 }
100
101                 public static bool operator == (HttpMethod left, HttpMethod right)
102                 {
103                         if ((object) left == null || (object) right == null)
104                                 return ReferenceEquals (left, right);
105
106                         return left.Equals (right);
107                 }
108
109                 public static bool operator != (HttpMethod left, HttpMethod right)
110                 {
111                         return !(left == right);
112                 }
113
114                 public bool Equals(HttpMethod other)
115                 {
116                         return string.Equals (method, other.method, StringComparison.OrdinalIgnoreCase);
117                 }
118
119                 public override bool Equals (object obj)
120                 {
121                         var other = obj as HttpMethod;
122                         return !ReferenceEquals (other, null) && Equals (other);
123                 }
124
125                 public override int GetHashCode ()
126                 {
127                         return method.GetHashCode ();
128                 }
129
130                 public override string ToString ()
131                 {
132                         return method;
133                 }
134         }
135 }