Welcome to mirror list, hosted at ThFree Co, Russian Federation.

HttpWebRequest.jvm.cs « System.Net « System « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a28092192752151057f31dfd942ff1862a494641 (plain)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Globalization;
using System.IO;
using System.Runtime;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Security;
using System.Security.Cryptography.X509Certificates;
using System.Security.Permissions;
using System.Text;
using System.Threading;
using System.Net;

namespace System.Net
{
	[Serializable]
	public class HttpWebRequest : WebRequest
	{
		#region Fields


		private static readonly int DEFAULT_MAX_RESP_HEADER_LEN = 64;

		private static int _defaultMaxResponseHeadersLength = DEFAULT_MAX_RESP_HEADER_LEN;


		private HttpProvider _provider;

		#endregion

		#region Constructors

		internal HttpWebRequest(Uri uri)
		{
			_provider = HttpProvider.GetHttpProvider(uri);
//			Console.WriteLine("uri to string: " + uri.ToString());
		}


		#endregion

		#region Properties


		public string Accept
		{
			get{return Headers["Accept"];}
			set
			{
				if(_provider.IsRequestStarted ())
					throw new InvalidOperationException ("request started");
				_provider.Headers.RemoveAndAdd ("Accept", value);
			}
		}

		public Uri Address
		{
			get{return _provider.GetAddress();}
		}

		public bool AllowAutoRedirect
		{
			get{return _provider.AllowAutoRedirect;}
			set{_provider.AllowAutoRedirect = value;}
		}

		public bool AllowWriteStreamBuffering
		{
			get{return _provider.AllowWriteStreamBuffering;}
			set{_provider.AllowWriteStreamBuffering = value;}
		}

		[MonoTODO] //documentation related
		public X509CertificateCollection ClientCertificates
		{
			[MonoTODO]
			get{return _provider.GetX509Certificates();}
			[MonoNotSupported("")]
			set { throw new NotImplementedException (); }
		}

		public string Connection
		{
			get { return Headers["Connection"]; }
			set
			{
				if(_provider.IsRequestStarted())
					throw new InvalidOperationException ("request started");

				string val = value;
				if (val != null)
					val = val.Trim ().ToLower (CultureInfo.InvariantCulture);

				if (val == null || val.Length == 0)
				{
					Headers.RemoveInternal ("Connection");
					return;
				}

				if (val == "keep-alive" || val == "close")
					throw new ArgumentException ("Keep-Alive and Close may not be set with this property");

//				if (this.KeepAlive && val.IndexOf ("keep-alive") == -1)
//					value = value + ", Keep-Alive";

				Headers.RemoveAndAdd ("Connection", value);
			}
		}

		public override string ConnectionGroupName
		{
			get{return _provider.ConnectionGroupName;}
			set{_provider.ConnectionGroupName = value;}
		}

		public override long ContentLength
		{
			get{return _provider.ContentLength;}
			set
			{
				if(_provider.IsRequestStarted())
					throw new InvalidOperationException("Connection already opened");
				_provider.ContentLength = value;
			}
		}

		public override string ContentType
		{
			get { return Headers["Content-Type"]; }
			set
			{
				if (value == null || value.Trim().Length == 0)
				{
					Headers.RemoveInternal ("Content-Type");
					return;
				}
				Headers.RemoveAndAdd ("Content-Type", value);
			}
		}
		[MonoTODO] //needed for automatic documentation tools,
			//since currently we don't support this feature
		public HttpContinueDelegate ContinueDelegate
		{
			[MonoTODO]
			get{return _provider.ContinueDelegate;}
			[MonoTODO]
			set{_provider.ContinueDelegate = value;}
		}

		public CookieContainer CookieContainer
		{
			get{return _provider.CookieContainer;}
			set{_provider.CookieContainer = value;}
		}

		public override ICredentials Credentials
		{
			get{return _provider.Credentials;}
			set{_provider.Credentials = value;}
		}

		public static int DefaultMaximumResponseHeadersLength
		{
			get{return HttpProvider.DefaultMaxResponseHeadersLength;}
			set{HttpProvider.DefaultMaxResponseHeadersLength = value;}
		}

		public string Expect
		{
			get{return Headers["Expect"];}
			set
			{
				if(_provider.IsRequestStarted ())
					throw new InvalidOperationException("Connection already opened");
				string val = value;
				if (val != null)
					val = val.Trim ().ToLower (CultureInfo.InvariantCulture);

				if (val == null || val.Length == 0)
				{
					Headers.RemoveInternal ("Expect");
					return;
				}

				if (val == "100-continue")
					throw new ArgumentException ("100-Continue cannot be set with this property.",
						"value");
				Headers.RemoveAndAdd ("Expect", value);
			}
		}

		public bool HaveResponse
		{
			get{return _provider.IsHaveResponse();}
		}

		public override WebHeaderCollection Headers
		{
			get{return _provider.Headers;}
			set{_provider.Headers = value;}
		}

		public DateTime IfModifiedSince
		{
			get
			{
				string str = Headers["If-Modified-Since"];
				if (str == null)
					return DateTime.Now;
				try
				{
					return MonoHttpDate.Parse (str);
				}
				catch (Exception)
				{
					return DateTime.Now;
				}
			}
			set
			{
				if(_provider.IsRequestStarted ())
					throw new InvalidOperationException("Connection already started");
				// rfc-1123 pattern
				Headers.SetInternal ("If-Modified-Since",
					value.ToUniversalTime ().ToString ("r", null));
				// TODO: check last param when using different locale
			}
		}

		public bool KeepAlive
		{
			get{return _provider.KeepAlive;}
			set{_provider.KeepAlive = value;}
		}

		public int MaximumAutomaticRedirections
		{
			get{return _provider.MaxAutoRedirections;}
			set{_provider.MaxAutoRedirections = value;}
		}

		[MonoTODO] //documentation
		public int MaximumResponseHeadersLength
		{
			[MonoTODO]
			get{return _provider.MaximumResponseHeadersLength;}
			[MonoTODO]
			set{_provider.MaximumResponseHeadersLength = value;}
		}

		public string MediaType
		{
			get{return _provider.MediaType;}
			set{_provider.MediaType = value;}
		}

		public override string Method
		{
			get{return _provider.MethodName;}
			set{_provider.MethodName = value;}
		}
		[MonoTODO] //for documentation related - limited.
		public bool Pipelined
		{
			[MonoTODO]
			get{return _provider.Pipelined;}
			[MonoTODO]
			set{_provider.Pipelined = value;}
		}

		public override bool PreAuthenticate
		{
			get{return _provider.PreAuthenticate;}
			set{_provider.PreAuthenticate = value;}
		}

		public Version ProtocolVersion
		{
			get{return _provider.ProtocolVersion;}
			set{_provider.ProtocolVersion = value;}
		}

		public override IWebProxy Proxy
		{
			get{return _provider.Proxy;}
			set{_provider.Proxy = value;}
		}

		public int ReadWriteTimeout
		{
			get{return _provider.ReadWriteTimeout;}
			set{_provider.ReadWriteTimeout = value;}
		}

		public string Referer
		{
			get {return Headers["Referer"];}
			set
			{
				if(_provider.IsRequestStarted ())
					throw new InvalidOperationException("Connection already opened");
				if (value == null || value.Trim().Length == 0)
				{
					Headers.RemoveInternal ("Referer");
					return;
				}
				Headers.SetInternal ("Referer", value);
			}
		}
		internal Uri AuthUri
		{
			get { return RequestUri; }
		}
		public override Uri RequestUri
		{
			get{return _provider.GetOriginalAddress();}
		}

		public bool SendChunked
		{
			get{return _provider.SendChunked;}
			set{_provider.SendChunked = value;}
		}

		public ServicePoint ServicePoint
		{
			get{return _provider.ServicePoint;}
		}
		[MonoTODO] //once again - needed since our impl. still
			//doesn't support this feature we need document it..
		public override int Timeout
		{
			[MonoTODO]
			get{return _provider.Timeout;}
			[MonoTODO]
			set{_provider.Timeout = value;}
		}


		public string TransferEncoding
		{
			get { return Headers ["Transfer-Encoding"]; }
			set
			{
				if(_provider.IsRequestStarted ())
				{
					throw new InvalidOperationException("Connection has been already opened");
				}
				string val = value;
				if (val != null)
					val = val.Trim ().ToLower (CultureInfo.InvariantCulture);

				if (val == null || val.Length == 0)
				{
					Headers.RemoveInternal ("Transfer-Encoding");
					return;
				}

				if (val == "chunked")
					throw new ArgumentException ("Chunked encoding must be set with the SendChunked property");

				if (!this.SendChunked)
					throw new InvalidOperationException ("SendChunked must be True");

				Headers.RemoveAndAdd ("Transfer-Encoding", value);
			}
		}


		public bool UnsafeAuthenticatedConnectionSharing
		{
			get { throw new NotImplementedException (); }
			set { throw new NotImplementedException (); }
		}

		public string UserAgent
		{
			get { return Headers ["User-Agent"]; }
			set { Headers.SetInternal ("User-Agent", value); }
		}




		#endregion

		#region Methods

		//todo
		public override void Abort()
		{
			_provider.Abort();
//			_connection.disconnect();
//			_haveResponse = true;
//			//aborted = true;
//			if (_asyncWrite != null)
//			{
//				GHWebAsyncResult r = _asyncWrite;
//				WebException wexc = new WebException ("Aborted.", WebExceptionStatus.RequestCanceled);
//				r.SetCompleted (false, wexc);
//				r.DoCallback ();
//				_asyncWrite = null;
//			}
//
//			if (_asyncRead != null)
//			{
//				GHWebAsyncResult r = _asyncRead;
//				WebException wexc = new WebException ("Aborted.", WebExceptionStatus.RequestCanceled);
//				r.SetCompleted (false, wexc);
//				r.DoCallback ();
//				_asyncRead = null;
//			}
//
////			if (abortHandler != null)
////			{
////				try
////				{
////					abortHandler (this, EventArgs.Empty);
////				}
////				catch {}
////				abortHandler = null;
////			}
//
//			if (_writeStream != null)
//			{
//				try
//				{
//					_writeStream.Close ();
//					_writeStream = null;
//				}
//				catch {}
//			}
//
//			if (_response != null)
//			{
//				try
//				{
//					_response.Close ();
//					_response = null;
//				}
//				catch {}
//			}
		}

		public void AddRange (int range)
		{
			AddRange ("bytes", range);
		}

		public void AddRange (int from, int to)
		{
			AddRange ("bytes", from, to);
		}

		public void AddRange (string rangeSpecifier, int range)
		{
			if (rangeSpecifier == null)
				throw new ArgumentNullException ("rangeSpecifier");
			string value = Headers ["Range"];
			if (value == null || value.Length == 0)
				value = rangeSpecifier + "=";
			else if (value.StartsWith (rangeSpecifier.ToLower () + "=", StringComparison.InvariantCultureIgnoreCase))
				value += ",";
			else
				throw new InvalidOperationException ("rangeSpecifier");
			Headers.RemoveAndAdd ("Range", value + range + "-");
		}

		public void AddRange (string rangeSpecifier, int from, int to)
		{
			if (rangeSpecifier == null)
				throw new ArgumentNullException ("rangeSpecifier");
			if (from < 0 || to < 0 || from > to)
				throw new ArgumentOutOfRangeException ();
			string value = Headers ["Range"];
			if (value == null || value.Length == 0)
				value = rangeSpecifier + "=";
			else if (value.StartsWith (rangeSpecifier.ToLower () + "=", StringComparison.InvariantCultureIgnoreCase))
				value += ",";
			else
				throw new InvalidOperationException ("rangeSpecifier");
			Headers.RemoveAndAdd ("Range", value + from + "-" + to);
		}

		public override Stream GetRequestStream()
		{
			return _provider.GetRequestStream();
//			lock(this)
//			{
//				Type t = Type.GetType("System.IO.ConsoleWriteStream", true);
//				_connection.setDoOutput(true);
//
//
////				Console.WriteLine("Request is sent with following headers:");
////				java.util.Map map = _connection.getRequestProperties();
////				for(java.util.Iterator iter = map.keySet().iterator(); iter.hasNext();)
////				{
////					string key = (string) iter.next();
////					Console.WriteLine(key + ": " + map.get(key));
////				}
//
//				foreach(string k in Headers)
//				{
//					string val = Headers[k];
//					val = (val == null) ? "" : val;
//					_connection.setRequestProperty(k, val);
//				}
//
//				_writeStream = (Stream) Activator.CreateInstance(t, new object[]{_connection.getOutputStream()});
//				_haveRequest = true;
//				return _writeStream;
//			}
		}

		public override WebResponse GetResponse()
		{
			return _provider.GetResponse();
		}
		/*
		private void CommonChecks (bool putpost)
		{
			string method = _connection.getRequestMethod();

			if (method == null)
				throw new ProtocolViolationException ("Method is null.");

			bool keepAlive = _headers["Keep-Alive"] == null;
			bool allowBuffering = true;
			bool sendChunked = true;
			long contentLength = _connection.getContentLength();

			if (putpost && ((!keepAlive || (contentLength == -1 && !sendChunked)) && !allowBuffering))
				throw new ProtocolViolationException ("Content-Length not set");

			string transferEncoding = TransferEncoding;
			if (!sendChunked && transferEncoding != null && transferEncoding.Trim () != "")
				throw new ProtocolViolationException ("SendChunked should be true.");
		}
		*/

		public override IAsyncResult BeginGetRequestStream(AsyncCallback callback, object state)
		{
			return _provider.BeginGetRequestStream(callback, state);
		}

		public override Stream EndGetRequestStream(IAsyncResult asyncResult)
		{
			return _provider.EndGetRequestStream(asyncResult);
		}

		public override IAsyncResult BeginGetResponse(AsyncCallback callback, object state)
		{
			//todo check input, http headers etc.

			return	_provider.BeginGetResponse(callback, state);
		}

		public override WebResponse EndGetResponse(IAsyncResult asyncResult)
		{
			return _provider.EndGetResponse(asyncResult);
		}




		#endregion

		#region Inner Classes

//		#region JavaHeaders class
//		[Serializable]
//			internal sealed class JavaHeaders  : WebHeaderCollection
//		{
//			private java.net.HttpURLConnection _connection;
//
//			internal JavaHeaders(java.net.HttpURLConnection con)
//			{
//				_connection = con;
//			}
//
//			public string this[string key]
//			{
//				get
//				{
//					return _connection.getHeaderField(key);
//				}
//				set
//				{
//					_connection.addRequestProperty(key, value);
//				}
//			}
//		}
//		#endregion




		#endregion
                public DecompressionMethods AutomaticDecompression
                {
                        get {
                                throw new NotSupportedException ();
                        }
                        set {
                                throw new NotSupportedException ();
                        }
                }

	}
}