Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
FtpWebRequest.cs
Go to the documentation of this file.
2using System.IO;
10
11namespace System.Net;
12
13public sealed class FtpWebRequest : WebRequest
14{
23
24 private object _syncObject;
25
27
28 private readonly Uri _uri;
29
31
32 private string _renameTo;
33
35
36 private bool _getResponseStarted;
37
39
40 private int _timeout = 100000;
41
42 private int _remainingTimeout;
43
44 private long _contentLength;
45
46 private long _contentOffset;
47
49
50 private bool _passive = true;
51
52 private bool _binary = true;
53
54 private string _connectionGroupName;
55
57
58 private bool _async;
59
60 private bool _aborted;
61
62 private bool _timedOut;
63
65
67
68 private readonly TimerThread.Callback _timerCallback;
69
70 private bool _enableSsl;
71
73
74 private Stream _stream;
75
77
78 private bool _onceFailed;
79
81
83
84 private int _readWriteTimeout = 300000;
85
87
89
91
92 private static readonly NetworkCredential s_defaultFtpNetworkCredential = new NetworkCredential("anonymous", "anonymous@", string.Empty);
93
95
97
99 {
100 get
101 {
103 }
104 set
105 {
106 }
107 }
108
109 public override string Method
110 {
111 get
112 {
113 return _methodInfo.Method;
114 }
115 set
116 {
117 if (string.IsNullOrEmpty(value))
118 {
120 }
121 if (InUse)
122 {
124 }
125 try
126 {
128 }
129 catch (ArgumentException)
130 {
132 }
133 }
134 }
135
136 public string? RenameTo
137 {
138 get
139 {
140 return _renameTo;
141 }
142 [param: DisallowNull]
143 set
144 {
145 if (InUse)
146 {
148 }
149 if (string.IsNullOrEmpty(value))
150 {
152 }
154 }
155 }
156
157 public override ICredentials? Credentials
158 {
159 get
160 {
161 return _authInfo;
162 }
163 [param: DisallowNull]
164 set
165 {
166 if (InUse)
167 {
169 }
170 if (value == null)
171 {
172 throw new ArgumentNullException("value");
173 }
175 {
177 }
179 }
180 }
181
182 public override Uri RequestUri => _uri;
183
184 public override int Timeout
185 {
186 get
187 {
188 return _timeout;
189 }
190 set
191 {
192 if (InUse)
193 {
195 }
196 if (value < 0 && value != -1)
197 {
199 }
200 if (_timeout != value)
201 {
202 _timeout = value;
203 _timerQueue = null;
204 }
205 }
206 }
207
209
211 {
212 get
213 {
214 return _readWriteTimeout;
215 }
216 set
217 {
219 {
221 }
222 if (value <= 0 && value != -1)
223 {
225 }
227 }
228 }
229
230 public long ContentOffset
231 {
232 get
233 {
234 return _contentOffset;
235 }
236 set
237 {
238 if (InUse)
239 {
241 }
242 if (value < 0)
243 {
244 throw new ArgumentOutOfRangeException("value");
245 }
247 }
248 }
249
250 public override long ContentLength
251 {
252 get
253 {
254 return _contentLength;
255 }
256 set
257 {
259 }
260 }
261
262 public override IWebProxy? Proxy
263 {
264 get
265 {
266 return null;
267 }
268 set
269 {
270 if (InUse)
271 {
273 }
274 }
275 }
276
277 public override string? ConnectionGroupName
278 {
279 get
280 {
282 }
283 set
284 {
285 if (InUse)
286 {
288 }
290 }
291 }
292
294
295 internal bool Aborted => _aborted;
296
298 {
299 get
300 {
301 if (_timerQueue == null)
302 {
304 }
305 return _timerQueue;
306 }
307 }
308
309 public bool KeepAlive
310 {
311 get
312 {
313 return true;
314 }
315 set
316 {
317 if (InUse)
318 {
320 }
321 }
322 }
323
325 {
326 get
327 {
328 return DefaultCachePolicy;
329 }
330 set
331 {
332 if (InUse)
333 {
335 }
336 }
337 }
338
339 public bool UseBinary
340 {
341 get
342 {
343 return _binary;
344 }
345 set
346 {
347 if (InUse)
348 {
350 }
351 _binary = value;
352 }
353 }
354
355 public bool UsePassive
356 {
357 get
358 {
359 return _passive;
360 }
361 set
362 {
363 if (InUse)
364 {
366 }
367 _passive = value;
368 }
369 }
370
372 {
373 get
374 {
375 return LazyInitializer.EnsureInitialized(ref _clientCertificates, ref _syncObject, () => new X509CertificateCollection());
376 }
377 set
378 {
379 if (value == null)
380 {
381 throw new ArgumentNullException("value");
382 }
384 }
385 }
386
387 public bool EnableSsl
388 {
389 get
390 {
391 return _enableSsl;
392 }
393 set
394 {
395 if (InUse)
396 {
398 }
400 }
401 }
402
403 public override WebHeaderCollection Headers
404 {
405 get
406 {
407 if (_ftpRequestHeaders == null)
408 {
410 }
411 return _ftpRequestHeaders;
412 }
413 set
414 {
416 }
417 }
418
419 public override string? ContentType
420 {
421 get
422 {
424 }
425 set
426 {
428 }
429 }
430
431 public override bool UseDefaultCredentials
432 {
433 get
434 {
436 }
437 set
438 {
440 }
441 }
442
443 public override bool PreAuthenticate
444 {
445 get
446 {
448 }
449 set
450 {
452 }
453 }
454
455 private bool InUse
456 {
457 get
458 {
460 {
461 return true;
462 }
463 return false;
464 }
465 }
466
467 internal FtpWebRequest(Uri uri)
468 {
469 if (System.Net.NetEventSource.Log.IsEnabled())
470 {
471 System.Net.NetEventSource.Info(this, uri, ".ctor");
472 }
473 if ((object)uri.Scheme != Uri.UriSchemeFtp)
474 {
475 throw new ArgumentOutOfRangeException("uri");
476 }
478 _syncObject = new object();
479 NetworkCredential networkCredential = null;
480 _uri = uri;
482 if (_uri.UserInfo != null && _uri.UserInfo.Length != 0)
483 {
484 string userInfo = _uri.UserInfo;
485 string userName = userInfo;
486 string password = "";
487 int num = userInfo.IndexOf(':');
488 if (num != -1)
489 {
490 userName = Uri.UnescapeDataString(userInfo.Substring(0, num));
491 num++;
492 password = Uri.UnescapeDataString(userInfo.Substring(num, userInfo.Length - num));
493 }
494 networkCredential = new NetworkCredential(userName, password);
495 }
496 if (networkCredential == null)
497 {
498 networkCredential = s_defaultFtpNetworkCredential;
499 }
500 _authInfo = networkCredential;
501 }
502
503 public override WebResponse GetResponse()
504 {
505 if (System.Net.NetEventSource.Log.IsEnabled())
506 {
507 System.Net.NetEventSource.Info(this, $"Method: {_methodInfo.Method}", "GetResponse");
508 }
509 try
510 {
511 CheckError();
512 if (_ftpWebResponse != null)
513 {
514 return _ftpWebResponse;
515 }
517 {
519 }
520 _getResponseStarted = true;
523 if (Timeout != -1)
524 {
525 _remainingTimeout = Timeout - (int)(DateTime.UtcNow - _startTime).TotalMilliseconds;
526 if (_remainingTimeout <= 0)
527 {
529 }
530 }
531 RequestStage requestStage = FinishRequestStage(RequestStage.RequestStarted);
532 if (requestStage >= RequestStage.RequestStarted)
533 {
534 if (requestStage < RequestStage.ReadReady)
535 {
536 lock (_syncObject)
537 {
538 if (_requestStage < RequestStage.ReadReady)
539 {
540 _readAsyncResult = new LazyAsyncResult(null, null, null);
541 }
542 }
543 if (_readAsyncResult != null)
544 {
546 }
547 CheckError();
548 }
549 }
550 else
551 {
552 SubmitRequest(isAsync: false);
554 {
556 }
557 else
558 {
560 }
561 CheckError();
563 }
564 }
565 catch (Exception ex)
566 {
567 if (System.Net.NetEventSource.Log.IsEnabled())
568 {
569 System.Net.NetEventSource.Error(this, ex, "GetResponse");
570 }
571 if (_exception == null)
572 {
573 if (System.Net.NetEventSource.Log.IsEnabled())
574 {
575 System.Net.NetEventSource.Error(this, ex, "GetResponse");
576 }
577 SetException(ex);
578 FinishRequestStage(RequestStage.CheckForError);
579 }
580 throw;
581 }
582 return _ftpWebResponse;
583 }
584
585 public override IAsyncResult BeginGetResponse(AsyncCallback? callback, object? state)
586 {
587 if (System.Net.NetEventSource.Log.IsEnabled())
588 {
589 System.Net.NetEventSource.Info(this, $"Method: {_methodInfo.Method}", "BeginGetResponse");
590 }
591 ContextAwareResult contextAwareResult;
592 try
593 {
594 if (_ftpWebResponse != null)
595 {
596 contextAwareResult = new ContextAwareResult(this, state, callback);
597 contextAwareResult.InvokeCallback(_ftpWebResponse);
598 return contextAwareResult;
599 }
601 {
603 }
604 _getResponseStarted = true;
605 CheckError();
606 RequestStage requestStage = FinishRequestStage(RequestStage.RequestStarted);
607 contextAwareResult = (ContextAwareResult)(_readAsyncResult = new ContextAwareResult(captureIdentity: true, forceCaptureContext: true, this, state, callback));
608 if (requestStage >= RequestStage.RequestStarted)
609 {
610 contextAwareResult.StartPostingAsyncOp();
611 contextAwareResult.FinishPostingAsyncOp();
612 if (requestStage >= RequestStage.ReadReady)
613 {
614 contextAwareResult = null;
615 }
616 else
617 {
618 lock (_syncObject)
619 {
620 if (_requestStage >= RequestStage.ReadReady)
621 {
622 contextAwareResult = null;
623 }
624 }
625 }
626 if (contextAwareResult == null)
627 {
628 contextAwareResult = (ContextAwareResult)_readAsyncResult;
629 if (!contextAwareResult.InternalPeekCompleted)
630 {
631 contextAwareResult.InvokeCallback();
632 }
633 }
634 }
635 else
636 {
637 lock (contextAwareResult.StartPostingAsyncOp())
638 {
639 SubmitRequest(isAsync: true);
640 contextAwareResult.FinishPostingAsyncOp();
641 }
642 FinishRequestStage(RequestStage.CheckForError);
643 }
644 }
645 catch (Exception message)
646 {
647 if (System.Net.NetEventSource.Log.IsEnabled())
648 {
649 System.Net.NetEventSource.Error(this, message, "BeginGetResponse");
650 }
651 throw;
652 }
653 return contextAwareResult;
654 }
655
657 {
658 try
659 {
660 if (asyncResult == null)
661 {
662 throw new ArgumentNullException("asyncResult");
663 }
664 if (!(asyncResult is LazyAsyncResult lazyAsyncResult))
665 {
666 throw new ArgumentException(System.SR.net_io_invalidasyncresult, "asyncResult");
667 }
668 if (lazyAsyncResult.EndCalled)
669 {
671 }
672 lazyAsyncResult.InternalWaitForCompletion();
673 lazyAsyncResult.EndCalled = true;
674 CheckError();
675 }
676 catch (Exception message)
677 {
678 if (System.Net.NetEventSource.Log.IsEnabled())
679 {
680 System.Net.NetEventSource.Error(this, message, "EndGetResponse");
681 }
682 throw;
683 }
684 return _ftpWebResponse;
685 }
686
687 public override Stream GetRequestStream()
688 {
689 if (System.Net.NetEventSource.Log.IsEnabled())
690 {
691 System.Net.NetEventSource.Info(this, $"Method: {_methodInfo.Method}", "GetRequestStream");
692 }
693 try
694 {
696 {
698 }
701 {
703 }
704 CheckError();
707 if (Timeout != -1)
708 {
709 _remainingTimeout = Timeout - (int)(DateTime.UtcNow - _startTime).TotalMilliseconds;
710 if (_remainingTimeout <= 0)
711 {
713 }
714 }
715 FinishRequestStage(RequestStage.RequestStarted);
716 SubmitRequest(isAsync: false);
718 CheckError();
720 {
721 _stream.WriteTimeout = ReadWriteTimeout;
722 _stream.ReadTimeout = ReadWriteTimeout;
723 }
724 }
725 catch (Exception message)
726 {
727 if (System.Net.NetEventSource.Log.IsEnabled())
728 {
729 System.Net.NetEventSource.Error(this, message, "GetRequestStream");
730 }
731 throw;
732 }
733 return _stream;
734 }
735
736 public override IAsyncResult BeginGetRequestStream(AsyncCallback? callback, object? state)
737 {
738 if (System.Net.NetEventSource.Log.IsEnabled())
739 {
740 System.Net.NetEventSource.Info(this, $"Method: {_methodInfo.Method}", "BeginGetRequestStream");
741 }
742 ContextAwareResult contextAwareResult = null;
743 try
744 {
746 {
748 }
751 {
753 }
754 CheckError();
755 FinishRequestStage(RequestStage.RequestStarted);
756 contextAwareResult = new ContextAwareResult(captureIdentity: true, forceCaptureContext: true, this, state, callback);
757 lock (contextAwareResult.StartPostingAsyncOp())
758 {
759 _writeAsyncResult = contextAwareResult;
760 SubmitRequest(isAsync: true);
761 contextAwareResult.FinishPostingAsyncOp();
762 FinishRequestStage(RequestStage.CheckForError);
763 return contextAwareResult;
764 }
765 }
766 catch (Exception message)
767 {
768 if (System.Net.NetEventSource.Log.IsEnabled())
769 {
770 System.Net.NetEventSource.Error(this, message, "BeginGetRequestStream");
771 }
772 throw;
773 }
774 }
775
777 {
778 Stream stream = null;
779 try
780 {
781 if (asyncResult == null)
782 {
783 throw new ArgumentNullException("asyncResult");
784 }
785 if (!(asyncResult is LazyAsyncResult lazyAsyncResult))
786 {
787 throw new ArgumentException(System.SR.net_io_invalidasyncresult, "asyncResult");
788 }
789 if (lazyAsyncResult.EndCalled)
790 {
792 }
793 lazyAsyncResult.InternalWaitForCompletion();
794 lazyAsyncResult.EndCalled = true;
795 CheckError();
796 stream = _stream;
797 lazyAsyncResult.EndCalled = true;
798 if (stream.CanTimeout)
799 {
800 stream.WriteTimeout = ReadWriteTimeout;
801 stream.ReadTimeout = ReadWriteTimeout;
802 }
803 }
804 catch (Exception message)
805 {
806 if (System.Net.NetEventSource.Log.IsEnabled())
807 {
808 System.Net.NetEventSource.Error(this, message, "EndGetRequestStream");
809 }
810 throw;
811 }
812 return stream;
813 }
814
815 private void SubmitRequest(bool isAsync)
816 {
817 try
818 {
819 _async = isAsync;
820 while (true)
821 {
822 FtpControlStream ftpControlStream = _connection;
823 if (ftpControlStream == null)
824 {
825 if (isAsync)
826 {
828 return;
829 }
830 ftpControlStream = (_connection = CreateConnection());
831 }
832 if (!isAsync && Timeout != -1)
833 {
834 _remainingTimeout = Timeout - (int)(DateTime.UtcNow - _startTime).TotalMilliseconds;
835 if (_remainingTimeout <= 0)
836 {
837 break;
838 }
839 }
840 if (System.Net.NetEventSource.Log.IsEnabled())
841 {
842 System.Net.NetEventSource.Info(this, "Request being submitted", "SubmitRequest");
843 }
845 try
846 {
848 return;
849 }
850 catch (Exception e)
851 {
852 if (AttemptedRecovery(e))
853 {
854 if (!isAsync && Timeout != -1)
855 {
856 _remainingTimeout = Timeout - (int)(DateTime.UtcNow - _startTime).TotalMilliseconds;
857 if (_remainingTimeout <= 0)
858 {
859 throw;
860 }
861 }
862 continue;
863 }
864 throw;
865 }
866 }
868 }
869 catch (WebException ex)
870 {
871 if (ex.InnerException is IOException { InnerException: SocketException { SocketErrorCode: SocketError.TimedOut } })
872 {
874 }
875 else
876 {
877 SetException(ex);
878 }
879 }
880 catch (Exception exception)
881 {
883 }
884 }
885
887 {
888 if (e is SocketException ex)
889 {
890 if (ex.SocketErrorCode == SocketError.HostNotFound)
891 {
893 }
895 }
896 return e;
897 }
898
899 private async void CreateConnectionAsync()
900 {
901 object obj;
902 try
903 {
904 TcpClient client = new TcpClient();
905 await client.ConnectAsync(_uri.Host, _uri.Port).ConfigureAwait(continueOnCapturedContext: false);
906 obj = new FtpControlStream(client);
907 }
908 catch (Exception e)
909 {
910 obj = TranslateConnectException(e);
911 }
912 AsyncRequestCallback(obj);
913 }
914
916 {
917 string host = _uri.Host;
918 int port = _uri.Port;
919 TcpClient tcpClient = new TcpClient();
920 try
921 {
922 tcpClient.Connect(host, port);
923 }
924 catch (Exception e)
925 {
926 throw TranslateConnectException(e);
927 }
928 return new FtpControlStream(tcpClient);
929 }
930
931 private Stream TimedSubmitRequestHelper(bool isAsync)
932 {
933 if (isAsync)
934 {
935 if (_requestCompleteAsyncResult == null)
936 {
937 _requestCompleteAsyncResult = new LazyAsyncResult(null, null, null);
938 }
939 return _connection.SubmitRequest(this, isAsync: true, readInitalResponseOnConnect: true);
940 }
941 Stream stream = null;
942 bool flag = false;
943 TimerThread.Timer timer = TimerQueue.CreateTimer(_timerCallback, null);
944 try
945 {
946 stream = _connection.SubmitRequest(this, isAsync: false, readInitalResponseOnConnect: true);
947 }
948 catch (Exception ex)
949 {
950 if ((!(ex is SocketException) && !(ex is ObjectDisposedException)) || !timer.HasExpired)
951 {
952 timer.Cancel();
953 throw;
954 }
955 flag = true;
956 }
957 if (flag || !timer.Cancel())
958 {
959 _timedOut = true;
961 }
962 if (stream != null)
963 {
964 lock (_syncObject)
965 {
966 if (_aborted)
967 {
968 ((ICloseEx)stream).CloseEx(CloseExState.Abort | CloseExState.Silent);
969 CheckError();
970 throw new System.Net.InternalException();
971 }
972 _stream = stream;
973 }
974 }
975 return stream;
976 }
977
978 private void TimerCallback(TimerThread.Timer timer, int timeNoticed, object context)
979 {
980 if (System.Net.NetEventSource.Log.IsEnabled())
981 {
982 System.Net.NetEventSource.Info(this, null, "TimerCallback");
983 }
984 FtpControlStream connection = _connection;
985 if (connection != null)
986 {
987 if (System.Net.NetEventSource.Log.IsEnabled())
988 {
989 System.Net.NetEventSource.Info(this, "aborting connection", "TimerCallback");
990 }
991 connection.AbortConnect();
992 }
993 }
994
996 {
997 if (e is OutOfMemoryException || _onceFailed || _aborted || _timedOut || _connection == null || !_connection.RecoverableFailure)
998 {
999 return false;
1000 }
1001 _onceFailed = true;
1002 lock (_syncObject)
1003 {
1004 if (_connection == null)
1005 {
1006 return false;
1007 }
1008 _connection.CloseSocket();
1009 if (System.Net.NetEventSource.Log.IsEnabled())
1010 {
1011 System.Net.NetEventSource.Info(this, $"Releasing connection: {_connection}", "AttemptedRecovery");
1012 }
1013 _connection = null;
1014 }
1015 return true;
1016 }
1017
1019 {
1020 if (System.Net.NetEventSource.Log.IsEnabled())
1021 {
1022 System.Net.NetEventSource.Info(this, null, "SetException");
1023 }
1025 {
1026 _exception = exception;
1027 throw exception;
1028 }
1029 FtpControlStream connection = _connection;
1030 if (_exception == null)
1031 {
1032 if (exception is WebException)
1033 {
1034 EnsureFtpWebResponse(exception);
1035 _exception = new WebException(exception.Message, null, ((WebException)exception).Status, _ftpWebResponse);
1036 }
1038 {
1039 _exception = exception;
1040 }
1041 else if (connection != null && connection.StatusCode != 0)
1042 {
1043 EnsureFtpWebResponse(exception);
1044 _exception = new WebException(System.SR.Format(System.SR.net_ftp_servererror, connection.StatusLine), exception, WebExceptionStatus.ProtocolError, _ftpWebResponse);
1045 }
1046 else
1047 {
1048 _exception = new WebException(exception.Message, exception);
1049 }
1050 if (connection != null && _ftpWebResponse != null)
1051 {
1052 _ftpWebResponse.UpdateStatus(connection.StatusCode, connection.StatusLine, connection.ExitMessage);
1053 }
1054 }
1055 }
1056
1057 private void CheckError()
1058 {
1059 if (_exception != null)
1060 {
1061 ExceptionDispatchInfo.Throw(_exception);
1062 }
1063 }
1064
1065 internal void RequestCallback(object obj)
1066 {
1067 if (_async)
1068 {
1069 AsyncRequestCallback(obj);
1070 }
1071 else
1072 {
1073 SyncRequestCallback(obj);
1074 }
1075 }
1076
1077 private void SyncRequestCallback(object obj)
1078 {
1079 RequestStage stage = RequestStage.CheckForError;
1080 try
1081 {
1082 bool flag = obj == null;
1083 Exception ex = obj as Exception;
1084 if (System.Net.NetEventSource.Log.IsEnabled())
1085 {
1086 System.Net.NetEventSource.Info(this, $"exp:{ex} completedRequest:{flag}", "SyncRequestCallback");
1087 }
1088 if (ex != null)
1089 {
1090 SetException(ex);
1091 return;
1092 }
1093 if (!flag)
1094 {
1095 throw new System.Net.InternalException();
1096 }
1097 FtpControlStream connection = _connection;
1098 if (connection != null)
1099 {
1100 EnsureFtpWebResponse(null);
1101 _ftpWebResponse.UpdateStatus(connection.StatusCode, connection.StatusLine, connection.ExitMessage);
1102 }
1103 stage = RequestStage.ReleaseConnection;
1104 }
1105 catch (Exception exception)
1106 {
1107 SetException(exception);
1108 }
1109 finally
1110 {
1111 FinishRequestStage(stage);
1112 CheckError();
1113 }
1114 }
1115
1116 private void AsyncRequestCallback(object obj)
1117 {
1118 RequestStage stage = RequestStage.CheckForError;
1119 try
1120 {
1121 FtpControlStream ftpControlStream = obj as FtpControlStream;
1122 FtpDataStream ftpDataStream = obj as FtpDataStream;
1123 Exception ex = obj as Exception;
1124 bool flag = obj == null;
1125 if (System.Net.NetEventSource.Log.IsEnabled())
1126 {
1127 System.Net.NetEventSource.Info(this, $"stream:{ftpDataStream} conn:{ftpControlStream} exp:{ex} completedRequest:{flag}", "AsyncRequestCallback");
1128 }
1129 while (true)
1130 {
1131 if (ex != null)
1132 {
1133 if (AttemptedRecovery(ex))
1134 {
1135 ftpControlStream = CreateConnection();
1136 if (ftpControlStream == null)
1137 {
1138 return;
1139 }
1140 ex = null;
1141 }
1142 if (ex != null)
1143 {
1144 SetException(ex);
1145 return;
1146 }
1147 }
1148 if (ftpControlStream == null)
1149 {
1150 break;
1151 }
1152 lock (_syncObject)
1153 {
1154 if (_aborted)
1155 {
1156 if (System.Net.NetEventSource.Log.IsEnabled())
1157 {
1158 System.Net.NetEventSource.Info(this, $"Releasing connect:{ftpControlStream}", "AsyncRequestCallback");
1159 }
1160 ftpControlStream.CloseSocket();
1161 return;
1162 }
1163 _connection = ftpControlStream;
1164 if (System.Net.NetEventSource.Log.IsEnabled())
1165 {
1166 System.Net.NetEventSource.Associate(this, _connection, "AsyncRequestCallback");
1167 }
1168 }
1169 try
1170 {
1171 ftpDataStream = (FtpDataStream)TimedSubmitRequestHelper(isAsync: true);
1172 return;
1173 }
1174 catch (Exception ex2)
1175 {
1176 ex = ex2;
1177 }
1178 }
1179 if (ftpDataStream != null)
1180 {
1181 lock (_syncObject)
1182 {
1183 if (_aborted)
1184 {
1185 ((ICloseEx)ftpDataStream).CloseEx(CloseExState.Abort | CloseExState.Silent);
1186 return;
1187 }
1188 _stream = ftpDataStream;
1189 }
1190 ftpDataStream.SetSocketTimeoutOption(Timeout);
1191 EnsureFtpWebResponse(null);
1192 stage = (ftpDataStream.CanRead ? RequestStage.ReadReady : RequestStage.WriteReady);
1193 }
1194 else
1195 {
1196 if (!flag)
1197 {
1198 throw new System.Net.InternalException();
1199 }
1200 ftpControlStream = _connection;
1201 if (ftpControlStream != null)
1202 {
1203 EnsureFtpWebResponse(null);
1204 _ftpWebResponse.UpdateStatus(ftpControlStream.StatusCode, ftpControlStream.StatusLine, ftpControlStream.ExitMessage);
1205 }
1206 stage = RequestStage.ReleaseConnection;
1207 }
1208 }
1209 catch (Exception exception)
1210 {
1211 SetException(exception);
1212 }
1213 finally
1214 {
1215 FinishRequestStage(stage);
1216 }
1217 }
1218
1220 {
1221 if (System.Net.NetEventSource.Log.IsEnabled())
1222 {
1223 System.Net.NetEventSource.Info(this, $"state:{stage}", "FinishRequestStage");
1224 }
1225 if (_exception != null)
1226 {
1227 stage = RequestStage.ReleaseConnection;
1228 }
1229 RequestStage requestStage;
1230 LazyAsyncResult writeAsyncResult;
1231 LazyAsyncResult readAsyncResult;
1232 FtpControlStream connection;
1233 lock (_syncObject)
1234 {
1235 requestStage = _requestStage;
1236 if (stage == RequestStage.CheckForError)
1237 {
1238 return requestStage;
1239 }
1240 if (requestStage == RequestStage.ReleaseConnection && stage == RequestStage.ReleaseConnection)
1241 {
1242 return RequestStage.ReleaseConnection;
1243 }
1244 if (stage > requestStage)
1245 {
1246 _requestStage = stage;
1247 }
1248 if (stage <= RequestStage.RequestStarted)
1249 {
1250 return requestStage;
1251 }
1252 writeAsyncResult = _writeAsyncResult;
1253 readAsyncResult = _readAsyncResult;
1254 connection = _connection;
1255 if (stage == RequestStage.ReleaseConnection)
1256 {
1257 if (_exception == null && !_aborted && requestStage != RequestStage.ReadReady && _methodInfo.IsDownload && !_ftpWebResponse.IsFromCache)
1258 {
1259 return requestStage;
1260 }
1261 _connection = null;
1262 }
1263 }
1264 try
1265 {
1266 if ((stage == RequestStage.ReleaseConnection || requestStage == RequestStage.ReleaseConnection) && connection != null)
1267 {
1268 try
1269 {
1270 if (_exception != null)
1271 {
1272 connection.Abort(_exception);
1273 }
1274 }
1275 finally
1276 {
1277 if (System.Net.NetEventSource.Log.IsEnabled())
1278 {
1279 System.Net.NetEventSource.Info(this, $"Releasing connection: {connection}", "FinishRequestStage");
1280 }
1281 connection.CloseSocket();
1282 if (_async && _requestCompleteAsyncResult != null)
1283 {
1284 _requestCompleteAsyncResult.InvokeCallback();
1285 }
1286 }
1287 }
1288 return requestStage;
1289 }
1290 finally
1291 {
1292 try
1293 {
1294 if (stage >= RequestStage.WriteReady)
1295 {
1296 if (_methodInfo.IsUpload && !_getRequestStreamStarted)
1297 {
1298 if (_stream != null)
1299 {
1300 _stream.Close();
1301 }
1302 }
1303 else if (writeAsyncResult != null && !writeAsyncResult.InternalPeekCompleted)
1304 {
1305 writeAsyncResult.InvokeCallback();
1306 }
1307 }
1308 }
1309 finally
1310 {
1311 if (stage >= RequestStage.ReadReady && readAsyncResult != null && !readAsyncResult.InternalPeekCompleted)
1312 {
1313 readAsyncResult.InvokeCallback();
1314 }
1315 }
1316 }
1317 }
1318
1319 public override void Abort()
1320 {
1321 if (_aborted)
1322 {
1323 return;
1324 }
1325 try
1326 {
1327 Stream stream;
1328 FtpControlStream connection;
1329 lock (_syncObject)
1330 {
1331 if (_requestStage >= RequestStage.ReleaseConnection)
1332 {
1333 return;
1334 }
1335 _aborted = true;
1336 stream = _stream;
1337 connection = _connection;
1339 }
1340 if (stream != null)
1341 {
1342 ((ICloseEx)stream).CloseEx(CloseExState.Abort | CloseExState.Silent);
1343 }
1345 }
1346 catch (Exception message)
1347 {
1348 if (System.Net.NetEventSource.Log.IsEnabled())
1349 {
1350 System.Net.NetEventSource.Error(this, message, "Abort");
1351 }
1352 throw;
1353 }
1354 }
1355
1357 {
1358 if (_ftpWebResponse == null || (_ftpWebResponse.GetResponseStream() is FtpWebResponse.EmptyStream && _stream != null))
1359 {
1360 lock (_syncObject)
1361 {
1362 if (_ftpWebResponse == null || (_ftpWebResponse.GetResponseStream() is FtpWebResponse.EmptyStream && _stream != null))
1363 {
1364 Stream stream = _stream;
1365 if (_methodInfo.IsUpload)
1366 {
1367 stream = null;
1368 }
1369 if (_stream != null && _stream.CanRead && _stream.CanTimeout)
1370 {
1371 _stream.ReadTimeout = ReadWriteTimeout;
1372 _stream.WriteTimeout = ReadWriteTimeout;
1373 }
1374 FtpControlStream connection = _connection;
1375 long num = connection?.ContentLength ?? (-1);
1376 if (stream == null && num < 0)
1377 {
1378 num = 0L;
1379 }
1380 if (_ftpWebResponse != null)
1381 {
1382 _ftpWebResponse.SetResponseStream(stream);
1383 }
1384 else if (connection != null)
1385 {
1386 _ftpWebResponse = new FtpWebResponse(stream, num, connection.ResponseUri, connection.StatusCode, connection.StatusLine, connection.LastModified, connection.BannerMessage, connection.WelcomeMessage, connection.ExitMessage);
1387 }
1388 else
1389 {
1390 _ftpWebResponse = new FtpWebResponse(stream, -1L, _uri, FtpStatusCode.Undefined, null, DateTime.Now, null, null, null);
1391 }
1392 }
1393 }
1394 }
1395 if (System.Net.NetEventSource.Log.IsEnabled())
1396 {
1397 System.Net.NetEventSource.Info(this, $"Returns {_ftpWebResponse} with stream {_ftpWebResponse._responseStream}", "EnsureFtpWebResponse");
1398 }
1399 }
1400
1401 internal void DataStreamClosed(CloseExState closeState)
1402 {
1403 if ((closeState & CloseExState.Abort) == 0)
1404 {
1405 if (!_async)
1406 {
1407 if (_connection != null)
1408 {
1409 _connection.CheckContinuePipeline();
1410 }
1411 }
1412 else
1413 {
1414 _requestCompleteAsyncResult.InternalWaitForCompletion();
1415 CheckError();
1416 }
1417 }
1418 else
1419 {
1421 }
1422 }
1423}
Exception? InnerException
Definition Exception.cs:104
virtual bool CanTimeout
Definition Stream.cs:498
virtual void Close()
Definition Stream.cs:644
Stream SubmitRequest(WebRequest request, bool isAsync, bool readInitalResponseOnConnect)
virtual void Abort(Exception e)
static NetworkCredential DefaultNetworkCredentials
static WebException RequestAbortedException
static NotSupportedException PropertyNotSupportedException
static WebException TimeoutException
void SetSocketTimeoutOption(int timeout)
static FtpMethodInfo GetMethodInfo(string method)
FtpControlStream CreateConnection()
async void CreateConnectionAsync()
readonly TimerThread.Callback _timerCallback
override IAsyncResult BeginGetResponse(AsyncCallback? callback, object? state)
override? RequestCachePolicy CachePolicy
bool AttemptedRecovery(Exception e)
LazyAsyncResult _readAsyncResult
static readonly TimerThread.Queue s_DefaultTimerQueue
FtpControlStream _connection
override bool UseDefaultCredentials
override WebResponse GetResponse()
void RequestCallback(object obj)
override bool PreAuthenticate
WebHeaderCollection _ftpRequestHeaders
TimerThread.Queue _timerQueue
FtpWebResponse _ftpWebResponse
void DataStreamClosed(CloseExState closeState)
override Stream EndGetRequestStream(IAsyncResult asyncResult)
override IAsyncResult BeginGetRequestStream(AsyncCallback? callback, object? state)
void SubmitRequest(bool isAsync)
void EnsureFtpWebResponse(Exception exception)
override? ICredentials Credentials
override? IWebProxy Proxy
override WebResponse EndGetResponse(IAsyncResult asyncResult)
RequestStage FinishRequestStage(RequestStage stage)
ContextAwareResult _writeAsyncResult
Stream TimedSubmitRequestHelper(bool isAsync)
override? string ContentType
void TimerCallback(TimerThread.Timer timer, int timeNoticed, object context)
Exception TranslateConnectException(Exception e)
void SetException(Exception exception)
override Stream GetRequestStream()
static readonly NetworkCredential s_defaultFtpNetworkCredential
X509CertificateCollection ClientCertificates
X509CertificateCollection _clientCertificates
static new? RequestCachePolicy DefaultCachePolicy
void AsyncRequestCallback(object obj)
override? string ConnectionGroupName
LazyAsyncResult _requestCompleteAsyncResult
void SyncRequestCallback(object obj)
void UpdateStatus(FtpStatusCode statusCode, string statusLine, string exitMessage)
override Stream GetResponseStream()
void SetResponseStream(Stream stream)
void InvokeCallback(object result)
static readonly System.Net.NetEventSource Log
static void Info(object thisOrContextObject, FormattableString formattableString=null, [CallerMemberName] string memberName=null)
static void Error(object thisOrContextObject, FormattableString formattableString, [CallerMemberName] string memberName=null)
static void Associate(object first, object second, [CallerMemberName] string memberName=null)
static ServicePoint FindServicePoint(Uri address)
Task ConnectAsync(IPAddress address, int port)
Definition TcpClient.cs:318
void Connect(string hostname, int port)
Definition TcpClient.cs:209
static Queue GetOrCreateQueue(int durationMilliseconds)
static ? RequestCachePolicy DefaultCachePolicy
Definition WebRequest.cs:69
virtual bool IsFromCache
static string net_ftp_invalid_renameto
Definition SR.cs:88
static string net_ftp_no_defaultcreds
Definition SR.cs:96
static string net_reqsubmitted
Definition SR.cs:16
static string net_io_invalidendcall
Definition SR.cs:22
static string net_ftp_invalid_method_name
Definition SR.cs:86
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string net_ftp_servererror
Definition SR.cs:82
static string net_webstatus_NameResolutionFailure
Definition SR.cs:108
static string net_io_timeout_use_gt_zero
Definition SR.cs:80
static string net_nouploadonget
Definition SR.cs:36
static string net_timeout
Definition SR.cs:66
static string net_io_timeout_use_ge_zero
Definition SR.cs:18
static string net_io_invalidasyncresult
Definition SR.cs:20
static string net_repcall
Definition SR.cs:38
static string net_webstatus_ConnectFailure
Definition SR.cs:110
static string net_ftp_unsupported_method
Definition SR.cs:102
Definition SR.cs:7
string UserInfo
Definition Uri.cs:577
static readonly string UriSchemeFtp
Definition Uri.cs:157
static string UnescapeDataString(string stringToUnescape)
Definition Uri.cs:4058
string Scheme
Definition Uri.cs:505
static DateTime Now
Definition DateTime.cs:103
static unsafe DateTime UtcNow
Definition DateTime.cs:142