Add missing property
[mono.git] / mcs / class / System / System.Net / HttpWebRequest.jvm.cs
1 \r
2 using System;\r
3 using System.Collections;\r
4 using System.Collections.Specialized;\r
5 using System.Configuration;\r
6 using System.Globalization;\r
7 using System.IO;\r
8 using System.Runtime;\r
9 using System.Runtime.CompilerServices;\r
10 using System.Runtime.Serialization;\r
11 using System.Security;\r
12 using System.Security.Cryptography.X509Certificates;\r
13 using System.Security.Permissions;\r
14 using System.Text;\r
15 using System.Threading;\r
16 using System.Net;\r
17 \r
18 namespace System.Net\r
19 {\r
20         [Serializable]\r
21         public class HttpWebRequest : WebRequest\r
22         {\r
23                 #region Fields\r
24 \r
25 \r
26                 private static readonly int DEFAULT_MAX_RESP_HEADER_LEN = 64;\r
27 \r
28                 private static int _defaultMaxResponseHeadersLength = DEFAULT_MAX_RESP_HEADER_LEN;\r
29 \r
30 \r
31                 private HttpProvider _provider;\r
32 \r
33                 #endregion\r
34 \r
35                 #region Constructors\r
36 \r
37                 internal HttpWebRequest(Uri uri)\r
38                 {\r
39                         _provider = HttpProvider.GetHttpProvider(uri);\r
40 //                      Console.WriteLine("uri to string: " + uri.ToString());\r
41                 }\r
42 \r
43 \r
44                 #endregion\r
45 \r
46                 #region Properties\r
47 \r
48 \r
49                 public string Accept\r
50                 {\r
51                         get{return Headers["Accept"];}\r
52                         set\r
53                         {\r
54                                 if(_provider.IsRequestStarted ())\r
55                                         throw new InvalidOperationException ("request started");\r
56                                 _provider.Headers.RemoveAndAdd ("Accept", value);\r
57                         }\r
58                 }\r
59 \r
60                 public Uri Address\r
61                 {\r
62                         get{return _provider.GetAddress();}\r
63                 }\r
64 \r
65                 public bool AllowAutoRedirect\r
66                 {\r
67                         get{return _provider.AllowAutoRedirect;}\r
68                         set{_provider.AllowAutoRedirect = value;}\r
69                 }\r
70 \r
71                 public bool AllowWriteStreamBuffering\r
72                 {\r
73                         get{return _provider.AllowWriteStreamBuffering;}\r
74                         set{_provider.AllowWriteStreamBuffering = value;}\r
75                 }\r
76 \r
77                 [MonoTODO] //documentation related\r
78                 public X509CertificateCollection ClientCertificates\r
79                 {\r
80                         [MonoTODO]\r
81                         get{return _provider.GetX509Certificates();}\r
82                         [MonoNotSupported("")]\r
83                         set { throw new NotImplementedException (); }\r
84                 }\r
85 \r
86                 public string Connection\r
87                 {\r
88                         get { return Headers["Connection"]; }\r
89                         set\r
90                         {\r
91                                 if(_provider.IsRequestStarted())\r
92                                         throw new InvalidOperationException ("request started");\r
93 \r
94                                 string val = value;\r
95                                 if (val != null)\r
96                                         val = val.Trim ().ToLower (CultureInfo.InvariantCulture);\r
97 \r
98                                 if (val == null || val.Length == 0)\r
99                                 {\r
100                                         Headers.RemoveInternal ("Connection");\r
101                                         return;\r
102                                 }\r
103 \r
104                                 if (val == "keep-alive" || val == "close")\r
105                                         throw new ArgumentException ("Keep-Alive and Close may not be set with this property");\r
106 \r
107 //                              if (this.KeepAlive && val.IndexOf ("keep-alive") == -1)\r
108 //                                      value = value + ", Keep-Alive";\r
109 \r
110                                 Headers.RemoveAndAdd ("Connection", value);\r
111                         }\r
112                 }\r
113 \r
114                 public override string ConnectionGroupName\r
115                 {\r
116                         get{return _provider.ConnectionGroupName;}\r
117                         set{_provider.ConnectionGroupName = value;}\r
118                 }\r
119 \r
120                 public override long ContentLength\r
121                 {\r
122                         get{return _provider.ContentLength;}\r
123                         set\r
124                         {\r
125                                 if(_provider.IsRequestStarted())\r
126                                         throw new InvalidOperationException("Connection already opened");\r
127                                 _provider.ContentLength = value;\r
128                         }\r
129                 }\r
130 \r
131                 public override string ContentType\r
132                 {\r
133                         get { return Headers["Content-Type"]; }\r
134                         set\r
135                         {\r
136                                 if (value == null || value.Trim().Length == 0)\r
137                                 {\r
138                                         Headers.RemoveInternal ("Content-Type");\r
139                                         return;\r
140                                 }\r
141                                 Headers.RemoveAndAdd ("Content-Type", value);\r
142                         }\r
143                 }\r
144                 [MonoTODO] //needed for automatic documentation tools,\r
145                         //since currently we don't support this feature\r
146                 public HttpContinueDelegate ContinueDelegate\r
147                 {\r
148                         [MonoTODO]\r
149                         get{return _provider.ContinueDelegate;}\r
150                         [MonoTODO]\r
151                         set{_provider.ContinueDelegate = value;}\r
152                 }\r
153 \r
154                 public CookieContainer CookieContainer\r
155                 {\r
156                         get{return _provider.CookieContainer;}\r
157                         set{_provider.CookieContainer = value;}\r
158                 }\r
159 \r
160                 public override ICredentials Credentials\r
161                 {\r
162                         get{return _provider.Credentials;}\r
163                         set{_provider.Credentials = value;}\r
164                 }\r
165 \r
166                 public static int DefaultMaximumResponseHeadersLength\r
167                 {\r
168                         get{return HttpProvider.DefaultMaxResponseHeadersLength;}\r
169                         set{HttpProvider.DefaultMaxResponseHeadersLength = value;}\r
170                 }\r
171 \r
172                 public string Expect\r
173                 {\r
174                         get{return Headers["Expect"];}\r
175                         set\r
176                         {\r
177                                 if(_provider.IsRequestStarted ())\r
178                                         throw new InvalidOperationException("Connection already opened");\r
179                                 string val = value;\r
180                                 if (val != null)\r
181                                         val = val.Trim ().ToLower (CultureInfo.InvariantCulture);\r
182 \r
183                                 if (val == null || val.Length == 0)\r
184                                 {\r
185                                         Headers.RemoveInternal ("Expect");\r
186                                         return;\r
187                                 }\r
188 \r
189                                 if (val == "100-continue")\r
190                                         throw new ArgumentException ("100-Continue cannot be set with this property.",\r
191                                                 "value");\r
192                                 Headers.RemoveAndAdd ("Expect", value);\r
193                         }\r
194                 }\r
195 \r
196                 public bool HaveResponse\r
197                 {\r
198                         get{return _provider.IsHaveResponse();}\r
199                 }\r
200 \r
201                 public override WebHeaderCollection Headers\r
202                 {\r
203                         get{return _provider.Headers;}\r
204                         set{_provider.Headers = value;}\r
205                 }\r
206 \r
207                 public DateTime IfModifiedSince\r
208                 {\r
209                         get\r
210                         {\r
211                                 string str = Headers["If-Modified-Since"];\r
212                                 if (str == null)\r
213                                         return DateTime.Now;\r
214                                 try\r
215                                 {\r
216                                         return MonoHttpDate.Parse (str);\r
217                                 }\r
218                                 catch (Exception)\r
219                                 {\r
220                                         return DateTime.Now;\r
221                                 }\r
222                         }\r
223                         set\r
224                         {\r
225                                 if(_provider.IsRequestStarted ())\r
226                                         throw new InvalidOperationException("Connection already started");\r
227                                 // rfc-1123 pattern\r
228                                 Headers.SetInternal ("If-Modified-Since",\r
229                                         value.ToUniversalTime ().ToString ("r", null));\r
230                                 // TODO: check last param when using different locale\r
231                         }\r
232                 }\r
233 \r
234                 public bool KeepAlive\r
235                 {\r
236                         get{return _provider.KeepAlive;}\r
237                         set{_provider.KeepAlive = value;}\r
238                 }\r
239 \r
240                 public int MaximumAutomaticRedirections\r
241                 {\r
242                         get{return _provider.MaxAutoRedirections;}\r
243                         set{_provider.MaxAutoRedirections = value;}\r
244                 }\r
245 \r
246                 [MonoTODO] //documentation\r
247                 public int MaximumResponseHeadersLength\r
248                 {\r
249                         [MonoTODO]\r
250                         get{return _provider.MaximumResponseHeadersLength;}\r
251                         [MonoTODO]\r
252                         set{_provider.MaximumResponseHeadersLength = value;}\r
253                 }\r
254 \r
255                 public string MediaType\r
256                 {\r
257                         get{return _provider.MediaType;}\r
258                         set{_provider.MediaType = value;}\r
259                 }\r
260 \r
261                 public override string Method\r
262                 {\r
263                         get{return _provider.MethodName;}\r
264                         set{_provider.MethodName = value;}\r
265                 }\r
266                 [MonoTODO] //for documentation related - limited.\r
267                 public bool Pipelined\r
268                 {\r
269                         [MonoTODO]\r
270                         get{return _provider.Pipelined;}\r
271                         [MonoTODO]\r
272                         set{_provider.Pipelined = value;}\r
273                 }\r
274 \r
275                 public override bool PreAuthenticate\r
276                 {\r
277                         get{return _provider.PreAuthenticate;}\r
278                         set{_provider.PreAuthenticate = value;}\r
279                 }\r
280 \r
281                 public Version ProtocolVersion\r
282                 {\r
283                         get{return _provider.ProtocolVersion;}\r
284                         set{_provider.ProtocolVersion = value;}\r
285                 }\r
286 \r
287                 public override IWebProxy Proxy\r
288                 {\r
289                         get{return _provider.Proxy;}\r
290                         set{_provider.Proxy = value;}\r
291                 }\r
292 \r
293                 public int ReadWriteTimeout\r
294                 {\r
295                         get{return _provider.ReadWriteTimeout;}\r
296                         set{_provider.ReadWriteTimeout = value;}\r
297                 }\r
298 \r
299                 public string Referer\r
300                 {\r
301                         get {return Headers["Referer"];}\r
302                         set\r
303                         {\r
304                                 if(_provider.IsRequestStarted ())\r
305                                         throw new InvalidOperationException("Connection already opened");\r
306                                 if (value == null || value.Trim().Length == 0)\r
307                                 {\r
308                                         Headers.RemoveInternal ("Referer");\r
309                                         return;\r
310                                 }\r
311                                 Headers.SetInternal ("Referer", value);\r
312                         }\r
313                 }\r
314                 internal Uri AuthUri\r
315                 {\r
316                         get { return RequestUri; }\r
317                 }\r
318                 public override Uri RequestUri\r
319                 {\r
320                         get{return _provider.GetOriginalAddress();}\r
321                 }\r
322 \r
323                 public bool SendChunked\r
324                 {\r
325                         get{return _provider.SendChunked;}\r
326                         set{_provider.SendChunked = value;}\r
327                 }\r
328 \r
329                 public ServicePoint ServicePoint\r
330                 {\r
331                         get{return _provider.ServicePoint;}\r
332                 }\r
333                 [MonoTODO] //once again - needed since our impl. still\r
334                         //doesn't support this feature we need document it..\r
335                 public override int Timeout\r
336                 {\r
337                         [MonoTODO]\r
338                         get{return _provider.Timeout;}\r
339                         [MonoTODO]\r
340                         set{_provider.Timeout = value;}\r
341                 }\r
342 \r
343 \r
344                 public string TransferEncoding\r
345                 {\r
346                         get { return Headers ["Transfer-Encoding"]; }\r
347                         set\r
348                         {\r
349                                 if(_provider.IsRequestStarted ())\r
350                                 {\r
351                                         throw new InvalidOperationException("Connection has been already opened");\r
352                                 }\r
353                                 string val = value;\r
354                                 if (val != null)\r
355                                         val = val.Trim ().ToLower (CultureInfo.InvariantCulture);\r
356 \r
357                                 if (val == null || val.Length == 0)\r
358                                 {\r
359                                         Headers.RemoveInternal ("Transfer-Encoding");\r
360                                         return;\r
361                                 }\r
362 \r
363                                 if (val == "chunked")\r
364                                         throw new ArgumentException ("Chunked encoding must be set with the SendChunked property");\r
365 \r
366                                 if (!this.SendChunked)\r
367                                         throw new InvalidOperationException ("SendChunked must be True");\r
368 \r
369                                 Headers.RemoveAndAdd ("Transfer-Encoding", value);\r
370                         }\r
371                 }\r
372 \r
373 \r
374                 public bool UnsafeAuthenticatedConnectionSharing\r
375                 {\r
376                         get { throw new NotImplementedException (); }\r
377                         set { throw new NotImplementedException (); }\r
378                 }\r
379 \r
380                 public string UserAgent\r
381                 {\r
382                         get { return Headers ["User-Agent"]; }\r
383                         set { Headers.SetInternal ("User-Agent", value); }\r
384                 }\r
385 \r
386 \r
387 \r
388 \r
389                 #endregion\r
390 \r
391                 #region Methods\r
392 \r
393                 //todo\r
394                 public override void Abort()\r
395                 {\r
396                         _provider.Abort();\r
397 //                      _connection.disconnect();\r
398 //                      _haveResponse = true;\r
399 //                      //aborted = true;\r
400 //                      if (_asyncWrite != null)\r
401 //                      {\r
402 //                              GHWebAsyncResult r = _asyncWrite;\r
403 //                              WebException wexc = new WebException ("Aborted.", WebExceptionStatus.RequestCanceled);\r
404 //                              r.SetCompleted (false, wexc);\r
405 //                              r.DoCallback ();\r
406 //                              _asyncWrite = null;\r
407 //                      }\r
408 //\r
409 //                      if (_asyncRead != null)\r
410 //                      {\r
411 //                              GHWebAsyncResult r = _asyncRead;\r
412 //                              WebException wexc = new WebException ("Aborted.", WebExceptionStatus.RequestCanceled);\r
413 //                              r.SetCompleted (false, wexc);\r
414 //                              r.DoCallback ();\r
415 //                              _asyncRead = null;\r
416 //                      }\r
417 //\r
418 ////                    if (abortHandler != null)\r
419 ////                    {\r
420 ////                            try\r
421 ////                            {\r
422 ////                                    abortHandler (this, EventArgs.Empty);\r
423 ////                            }\r
424 ////                            catch {}\r
425 ////                            abortHandler = null;\r
426 ////                    }\r
427 //\r
428 //                      if (_writeStream != null)\r
429 //                      {\r
430 //                              try\r
431 //                              {\r
432 //                                      _writeStream.Close ();\r
433 //                                      _writeStream = null;\r
434 //                              }\r
435 //                              catch {}\r
436 //                      }\r
437 //\r
438 //                      if (_response != null)\r
439 //                      {\r
440 //                              try\r
441 //                              {\r
442 //                                      _response.Close ();\r
443 //                                      _response = null;\r
444 //                              }\r
445 //                              catch {}\r
446 //                      }\r
447                 }\r
448 \r
449                 public void AddRange (int range)\r
450                 {\r
451                         AddRange ("bytes", range);\r
452                 }\r
453 \r
454                 public void AddRange (int from, int to)\r
455                 {\r
456                         AddRange ("bytes", from, to);\r
457                 }\r
458 \r
459                 public void AddRange (string rangeSpecifier, int range)\r
460                 {\r
461                         if (rangeSpecifier == null)\r
462                                 throw new ArgumentNullException ("rangeSpecifier");\r
463                         string value = Headers ["Range"];\r
464                         if (value == null || value.Length == 0)\r
465                                 value = rangeSpecifier + "=";\r
466                         else if (value.StartsWith (rangeSpecifier.ToLower () + "=", StringComparison.InvariantCultureIgnoreCase))\r
467                                 value += ",";\r
468                         else\r
469                                 throw new InvalidOperationException ("rangeSpecifier");\r
470                         Headers.RemoveAndAdd ("Range", value + range + "-");\r
471                 }\r
472 \r
473                 public void AddRange (string rangeSpecifier, int from, int to)\r
474                 {\r
475                         if (rangeSpecifier == null)\r
476                                 throw new ArgumentNullException ("rangeSpecifier");\r
477                         if (from < 0 || to < 0 || from > to)\r
478                                 throw new ArgumentOutOfRangeException ();\r
479                         string value = Headers ["Range"];\r
480                         if (value == null || value.Length == 0)\r
481                                 value = rangeSpecifier + "=";\r
482                         else if (value.StartsWith (rangeSpecifier.ToLower () + "=", StringComparison.InvariantCultureIgnoreCase))\r
483                                 value += ",";\r
484                         else\r
485                                 throw new InvalidOperationException ("rangeSpecifier");\r
486                         Headers.RemoveAndAdd ("Range", value + from + "-" + to);\r
487                 }\r
488 \r
489                 public override Stream GetRequestStream()\r
490                 {\r
491                         return _provider.GetRequestStream();\r
492 //                      lock(this)\r
493 //                      {\r
494 //                              Type t = Type.GetType("System.IO.ConsoleWriteStream", true);\r
495 //                              _connection.setDoOutput(true);\r
496 //\r
497 //\r
498 ////                            Console.WriteLine("Request is sent with following headers:");\r
499 ////                            java.util.Map map = _connection.getRequestProperties();\r
500 ////                            for(java.util.Iterator iter = map.keySet().iterator(); iter.hasNext();)\r
501 ////                            {\r
502 ////                                    string key = (string) iter.next();\r
503 ////                                    Console.WriteLine(key + ": " + map.get(key));\r
504 ////                            }\r
505 //\r
506 //                              foreach(string k in Headers)\r
507 //                              {\r
508 //                                      string val = Headers[k];\r
509 //                                      val = (val == null) ? "" : val;\r
510 //                                      _connection.setRequestProperty(k, val);\r
511 //                              }\r
512 //\r
513 //                              _writeStream = (Stream) Activator.CreateInstance(t, new object[]{_connection.getOutputStream()});\r
514 //                              _haveRequest = true;\r
515 //                              return _writeStream;\r
516 //                      }\r
517                 }\r
518 \r
519                 public override WebResponse GetResponse()\r
520                 {\r
521                         return _provider.GetResponse();\r
522                 }\r
523                 /*\r
524                 private void CommonChecks (bool putpost)\r
525                 {\r
526                         string method = _connection.getRequestMethod();\r
527 \r
528                         if (method == null)\r
529                                 throw new ProtocolViolationException ("Method is null.");\r
530 \r
531                         bool keepAlive = _headers["Keep-Alive"] == null;\r
532                         bool allowBuffering = true;\r
533                         bool sendChunked = true;\r
534                         long contentLength = _connection.getContentLength();\r
535 \r
536                         if (putpost && ((!keepAlive || (contentLength == -1 && !sendChunked)) && !allowBuffering))\r
537                                 throw new ProtocolViolationException ("Content-Length not set");\r
538 \r
539                         string transferEncoding = TransferEncoding;\r
540                         if (!sendChunked && transferEncoding != null && transferEncoding.Trim () != "")\r
541                                 throw new ProtocolViolationException ("SendChunked should be true.");\r
542                 }\r
543                 */\r
544 \r
545                 public override IAsyncResult BeginGetRequestStream(AsyncCallback callback, object state)\r
546                 {\r
547                         return _provider.BeginGetRequestStream(callback, state);\r
548                 }\r
549 \r
550                 public override Stream EndGetRequestStream(IAsyncResult asyncResult)\r
551                 {\r
552                         return _provider.EndGetRequestStream(asyncResult);\r
553                 }\r
554 \r
555                 public override IAsyncResult BeginGetResponse(AsyncCallback callback, object state)\r
556                 {\r
557                         //todo check input, http headers etc.\r
558 \r
559                         return  _provider.BeginGetResponse(callback, state);\r
560                 }\r
561 \r
562                 public override WebResponse EndGetResponse(IAsyncResult asyncResult)\r
563                 {\r
564                         return _provider.EndGetResponse(asyncResult);\r
565                 }\r
566 \r
567 \r
568 \r
569 \r
570                 #endregion\r
571 \r
572                 #region Inner Classes\r
573 \r
574 //              #region JavaHeaders class\r
575 //              [Serializable]\r
576 //                      internal sealed class JavaHeaders  : WebHeaderCollection\r
577 //              {\r
578 //                      private java.net.HttpURLConnection _connection;\r
579 //\r
580 //                      internal JavaHeaders(java.net.HttpURLConnection con)\r
581 //                      {\r
582 //                              _connection = con;\r
583 //                      }\r
584 //\r
585 //                      public string this[string key]\r
586 //                      {\r
587 //                              get\r
588 //                              {\r
589 //                                      return _connection.getHeaderField(key);\r
590 //                              }\r
591 //                              set\r
592 //                              {\r
593 //                                      _connection.addRequestProperty(key, value);\r
594 //                              }\r
595 //                      }\r
596 //              }\r
597 //              #endregion\r
598 \r
599 \r
600 \r
601 \r
602                 #endregion\r
603 #if NET_2_0\r
604                 public DecompressionMethods AutomaticDecompression\r
605                 {\r
606                         get {\r
607                                 throw new NotSupportedException ();\r
608                         }\r
609                         set {\r
610                                 throw new NotSupportedException ();\r
611                         }\r
612                 }\r
613 #endif\r
614 \r
615         }\r
616 }\r