Merge pull request #2721 from ludovic-henry/fix-mono_ms_ticks
[mono.git] / mcs / class / System.Net.Http / System.Net.Http / HttpResponseMessage.cs
1 //
2 // HttpResponseMessage.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 using System.Net.Http.Headers;
30 using System.Text;
31
32 namespace System.Net.Http
33 {
34         public class HttpResponseMessage : IDisposable
35         {
36                 HttpResponseHeaders headers;
37                 string reasonPhrase;
38                 HttpStatusCode statusCode;
39                 Version version;
40                 bool disposed;
41
42                 public HttpResponseMessage ()
43                         : this (HttpStatusCode.OK)
44                 {
45                 }
46
47                 public HttpResponseMessage (HttpStatusCode statusCode)
48                 {
49                         StatusCode = statusCode;
50                 }
51
52                 public HttpContent Content { get; set; }
53
54                 public HttpResponseHeaders Headers {
55                         get {
56                                 return headers ?? (headers = new HttpResponseHeaders ());
57                         }
58                 }
59
60                 public bool IsSuccessStatusCode {
61                         get {
62                                 // Successful codes are 2xx
63                                 return statusCode >= HttpStatusCode.OK && statusCode < HttpStatusCode.MultipleChoices;
64                         }
65                 }
66
67                 public string ReasonPhrase {
68                         get {
69                                 return reasonPhrase ?? HttpListenerResponseHelper.GetStatusDescription ((int) statusCode);
70                         }
71                         set {
72                                 reasonPhrase = value;
73                         }
74                 }
75
76                 public HttpRequestMessage RequestMessage { get; set; }
77
78                 public HttpStatusCode StatusCode {
79                         get {
80                                 return statusCode;
81                         }
82                         set {
83                                 if (value < 0)
84                                         throw new ArgumentOutOfRangeException ();
85
86                                 statusCode = value;
87                         }
88                 }
89
90                 public Version Version {
91                         get {
92                                 return version ?? HttpVersion.Version11;
93                         }
94                         set {
95                                 if (value == null)
96                                         throw new ArgumentNullException ("Version");
97
98                                 version = value;
99                         }
100                 }
101
102                 public void Dispose ()
103                 {
104                         Dispose (true);
105                 }
106
107                 protected virtual void Dispose (bool disposing)
108                 {
109                         if (disposing && !disposed) {
110                                 disposed = true;
111
112                                 if (Content != null)
113                                         Content.Dispose ();
114                         }
115                 }
116
117                 public HttpResponseMessage EnsureSuccessStatusCode ()
118                 {
119                         if (IsSuccessStatusCode)
120                                 return this;
121
122                         throw new HttpRequestException (string.Format ("{0} ({1})", (int) statusCode, ReasonPhrase));
123                 }
124                 
125                 public override string ToString ()
126                 {
127                         var sb = new StringBuilder ();
128                         sb.Append ("StatusCode: ").Append ((int)StatusCode);
129                         sb.Append (", ReasonPhrase: '").Append (ReasonPhrase ?? "<null>");
130                         sb.Append ("', Version: ").Append (Version);
131                         sb.Append (", Content: ").Append (Content != null ? Content.ToString () : "<null>");
132                         sb.Append (", Headers:\r\n{\r\n").Append (Headers);
133                         if (Content != null)
134                                 sb.Append (Content.Headers);
135                         
136                         sb.Append ("}");
137                         
138                         return sb.ToString ();
139                 }
140         }
141 }