166 Commits
Author SHA1 Message Date
Martin Baulig 1256d6c8cf Use NetworkStream from CoreFX. (#16889)
* Use `NetworkStream` from CoreFX.

This brings the `NetworkStream` class and related tests from CoreFX.

* `SocketAsyncEventArgs`: partially replace the following properties
  with the CoreFX version: `Buffer`, `BufferList`, `Offset`, `SetBuffer ()`
  as well as the internal `CopyBufferFrom ()`.

* `SocketAsyncResult`: change `Buffer` from `byte[]` to `Memory<byte>`.

* Add internal `Socket` overloads:
   - `Receive (Memory<byte> buffer, int, int, SocketFlags, out SocketError)`
   - `ReceiveFrom (Memory<byte>, int, int, SocketFlags, ref EndPoint, out SocketError)`
   - `SendTo (Memory<byte>, int, int, SocketFlags, EndPoint)`

* Implement async task overloads from CoreFX (`Socket.Tasks.cs`) to `Socket`:
   - `int Read(Span<byte>)`
   - `Task<int> ReadAsync(byte[], int, int, CancellationToken)`
   - `ValueTask<int> ReadAsync(Memory<byte>, CancellationToken)`
   - `int ReadByte()`
   - `void Write(ReadOnlySpan<byte>)`
   - `Task WriteAsync(byte[], int, int, CancellationToken)`
   - `ValueTask WriteAsync(ReadOnlyMemory<byte>, CancellationToken)`
   - `void WriteByte(byte)`

* remove internal unused `PooledStream` and `ConnectionPool` APIs.

* bring new tests from CoreFX:
  - `System.Net.Sockets/tests/FunctionalTests/NetworkStreamTest.cs`
  - `System.Net.Sockets/tests/FunctionalTests/NetworkStreamTest.netcoreapp.cs`

* corlib: add `System.IO.FileSystem` and `System.Net.Sockets` resources and update SR.

* Update Socket.cs

* [csproj] Update project files

* Bump API snapshot submodule
2019-09-19 14:46:29 +02:00
Martin Baulig a2515a0869 [System]: MonoTlsStream is now IDisposable and cleans up on error. (#10254)
When the TLS Handshake fails in `MonoTlsStream.CreateStream()`, then we must dispose
the `SslStream` to avoid a memory leak.

In addition to this, `MonoTlsStream` now implements `IDisposable` and is disposes
in `WebConnection.CloseSocket()`.

Fixes #8689
2018-08-28 14:29:31 +02:00
Martin Baulig b97d0c43d1 [System]: WebConnection.Connect() needs to probe all IP addresses.
Fix a regression introduced in the new web stack - when connecting
to a DNS host name that resolves to multiple IP addresses, we need
to probe them all.

On iOS, "localhost" will resolve to both "::1" and "127.0.0.1".
2018-02-12 16:59:58 +01:00
monojenkins dd5b5ca52e Set NetworkStream.ReadTimeout in WebConnection.InitConnection(). (#6904)
Setting ReadTimeout in WebResponseStream.ctor throws System.Net.Sockets.SocketException : Invalid arguments on iOS.

Set it in WebConnection.InitConnection() immediately after establishing a connection, before we read/write anything.
2018-02-09 21:21:52 +01:00
Martin Baulig cd09ddbc55 New PR for the new web stack (#6125) 2017-12-14 10:06:47 +01:00
Martin Baulig 9b8bb0edbe Cosmetic; remove unnecessary cast and fix compiler warning. 2017-08-15 13:12:47 -04:00
Martin Baulig e5cdc000d7 [System]: Rename the internal class 'ChunkStream' into 'MonoChunkStream'. (#4803)
* [System]: Fully stub-out SslStream on platforms where it's not supported.

* Remove 'SECURITY_DEP' conditional from LocalCertificateSelectionCallback.

* Add the internal delegates and remove 'SECURITY_DEP' from SslStream.platformnotsupported.cs.

* In SslStream.cs, we now provide the full API when '!SECURITY_DEP' using the
PlatformNotSupportedException()-based implementation from SslStream.platformnotsupported.cs

* [System]: Rename the internal class 'ChunkStream' into 'MonoChunkStream'.

Our 'ChunkStream' is used by 'WebConnection', so it will stay after using
HttpListener from corefx.
2017-05-08 12:13:50 -04:00
Ludovic Henry 52b00f2922 [WebConnection] Fix race condition between Close and BeginWrite (#4693)
* [WebConnection] Make ReadDone and InitRead instance methods to avoid passing cnc around

* [WebConnection] Inline only call to InitConnection

* [WebConnection] Fix race condition between Close and BeginWrite
2017-04-14 14:51:42 -04:00
Marek Safar 092279ecd6 [System] Warnings cleanup 2016-10-12 10:19:18 +02:00
Marek Safar bb0a415101 [System] Set more error details on failed https proxy connect. Fixes #45108 2016-10-11 16:45:22 +02:00
Marek Safar 81fb20737f [System] Fill more WebException details when CONNECT fails. Fixes #19594 2016-10-01 00:11:23 +02:00
Marek Safar a26fda4e8c [System] Don't compress custom http headers in httplistener response. Fixes #34715 2016-09-13 16:11:43 +02:00
Marek Safar 870cbdc8b9 [System] Report an error instead of success when proxy authentication is required. Fixes #19594 2016-08-16 22:22:45 +02:00
Marcos Henrich 6f261e6bb6 [System] EndRead now throws WebException on abort.
When a requast is aborted on .NET a WebException is thrown and not a
ObjectDisposedException as we were doing.

Fixes #41477
2016-07-08 14:42:52 +01:00
Marek Safar ba81e38ecc [System] WebHeaderCollection from referencesource 2016-05-18 14:44:07 +02:00
Marek Safar c92f928ff8 [System] Authorization from referencesource 2016-05-16 12:13:52 +02:00
Martin Baulig e21ff252af [System]: Properly propagate exceptions in 'MonoTlsStream'.
(cherry picked from commit a7ab399d09)
2016-03-15 18:45:54 -04:00
Martin Baulig d7f52b74c9 [System]: In 'WebConnection.EndWrite()': always call EndWrite() on the underlying socket.
(cherry picked from commit dd9d66a7c2)
2016-03-15 18:44:58 -04:00
Rolf Bjarne Kvinge c010ebc09e [System] xamarin_start_wwan is not available for tvOS and watchOS. 2016-01-13 15:13:03 +01:00
Martin Baulig 3d172a4b34 Cosmetic; remove unused variable.
(cherry picked from commit a9afeddd80)
2015-11-04 15:11:31 -05:00
Martin Baulig 799361d19f Kill dead code. 2015-10-28 18:20:28 -04:00
Martin Baulig 35411c095d Remove a few more 'using System.Reflection's. 2015-10-23 16:05:19 -04:00
Martin Baulig 87ecbc9ce1 [System]: Use MonoTlsProvider API in WebConnection. 2015-10-21 14:45:26 -04:00
Martin Baulig 9af4b08290 Revert "[System]: WebConnection: improve chunked reads and async callbacks."
This reverts commit 9027f0ad28bf733ebb8ac9964c78c22853ca93c7.
2015-10-14 19:26:06 -04:00
Martin Baulig 25a206c6da [System/TLS]: Switch to the new ChainValidationHelper.
* Removed Mono.Security.Protocol.Tls.ValidationResult, use
  Mono.Security.Interface instead.  This is an API breaking
  change, but you can fix your application by simply adding
  the new using directive.

  The rationale behind this is to avoid a dependency on the
  Mono.Security.Protocol.Tls namespace when building and
  related classes when building the new TLS stack.

* Enable ChainValidationHelper in WebConnection, LegacySslStream
  and SslStream.

  The custom conditionals that were previously required for the
  mobile profiles are now gone as ChainValidationHelper already
  handles that internally.

* This is an internal API, the public front-end is
  Mono.Security.Interface.CertificateValidationHelper.

  CertificateValidationHelper can be used to access the system
  certificate validator on OS X and iOS; we do not compute the
  X509Chain on these platforms as it's not supported by the OS.

(cherry picked from commit 9713f35878dd90fe87423b5052b4f82ddc0b87f0)
2015-10-14 19:26:06 -04:00
Martin Baulig 77e1889d20 [System]: WebConnection: improve chunked reads and async callbacks.
Make sure we always complete the async operations on error and
also send callbacks outside the lock.

(cherry picked from commit 9027f0ad28bf733ebb8ac9964c78c22853ca93c7)
2015-10-14 15:18:27 -04:00
Marek Safar 613c2a9182 [system] Don't throw ObjectDisposedException on cancelled async WebConnection. Fixes #31507 2015-07-01 16:41:56 +02:00
Rolf Bjarne Kvinge 2b46111528 [corlib] A few XI-internal functions have been renamed; use the new names. 2015-05-26 14:08:54 +02:00
Alexander Köplinger 48e112d652 [Cleanup] Removed TARGET_JVM
It was once part of a special Java target profile, but this is no longer used today.
2014-08-08 03:28:34 +02:00
Martin Baulig e6ce33bc7d [Http]: Read version from "HTTP CONNECT" response.
When using "HTTP CONNECT" to create a tunnel to a proxy server,
check the proxy server's response for the HTTP Version and close
the connection if it's HTTP/1.0.
2014-06-05 21:31:31 +02:00
Martin Baulig 5428bdfb71 [Http]: Fix the loop in WebConnection.ReadHeaders().
We need to reset `gotStatus' each time we enter the outer loop or we
would attempt to interpret the status line as a header.
2014-06-05 21:31:31 +02:00
Marek Safar a6dccdaf6d Revert "Merge pull request #981 from methane/websocket"
This reverts commit 62cc262196, reversing
changes made to c1028cf912.
2014-05-23 17:24:45 +02:00
Marek Safar 62cc262196 Merge pull request #981 from methane/websocket
[websocket] Implement HttpListenerContext.AcceptWebSocketAsync()
2014-05-20 19:56:13 +02:00
Marek Habersack 34fb07d05f Include support for SSL/TLS right in System when building for the MONODROID profile
This patch builds upon commit 96a8500795 which placed parts of Mono.Security
responsible for dealing with SSL/TLS certificates for the purpose of certificate verification. The code enables
validation of the certifcates even if the user does not provide their own validation callback. The changes as related to
the original commit are few, basically consisting of enabling the same code used by MonoTouch in the MonoDroid context with
the only actual code change being the certificate validation bit mentioned above.

Since the change involves adding a subset of Mono.Security files to the monodroid version of System.dll, it is
also necessary to remove Mono.Android's dependence on Mono.Security.dll. The reason is that the code in AndroidPlatform
which looks up TLS support methods in Mono.Android.dll would use a type (X509CertificateCollection) that is now part of
System.dll but is *also* in Mono.Security.dll referenced by Mono.Android.dll and therefore the lookup would fail since
we'd have two distinct types from the reflection point of view.

Fixes https://bugzilla.xamarin.com/show_bug.cgi?id=18962
2014-04-25 21:39:53 +02:00
INADA Naoki 4239b4cb2e Reimplement StreamWebSocket. 2014-04-11 23:15:24 +09:00
Martin Baulig da6c88b49b [System] WebConnection and WebConnectionStream code cleanups.
* Merged WebConnection.EndWrite() and EndWrite2().

* WebConnectionStream.BeginWrite(): Use the same async-write
  callback for both chunked and non-chunked; remove the single-use
  small WriteRequestAsync() and inline it here to make the code
  more readable.

* WebConnectionStream.WriteRequestAsyncCB(): Now called for all
  async writes, call WebConnection.EndWrite() here.

The code is now more readable and easier to understand.  This is
the first part of a series of cleanups, which should eventually
lead towards full async and 100-continue.
2014-02-19 11:08:18 +01:00
Martin Baulig c6a70a9586 Rewrite and fix the web stack's connection reuse and idle logic.
* ServicePoint now uses an idle timer to close idle connections.

* WebConnectionGroup has been mostly rewritten to use a linked
  list of open connections and their idle times.

  This fixes both the problem that we were leaking sockets and
  that we were not reusing existing connections.

* ServicePoint now correctly keeps track of the number of open
  connections.

* ServicePointManager now uses the correct default idle time
  of 100 seconds (previously 15 minutes).

* WebConnection uses the new WebConnectionGroup's busy logic
  to correctly flag connections as busy / reusable.
2014-02-13 14:17:14 +01:00
Martin Baulig 861d495951 Fix WebConnectionStream.CheckComplete() logic for chunked encoding. 2014-02-13 13:22:19 +01:00
Sebastien Pouliot 613906e04b Further reduce the chance of an NRE in WebConnection, ref: 55f400b644 2013-11-05 16:52:19 -05:00
Jonathan Pryor 55f400b644 [System] Avoid an NRE when Close() and Write() are concurrent.
Fixes: https://bugzilla.xamarin.com/show_bug.cgi?id=15857

Scenario (for which I couldn't easily create a testcase, but can
sketch out): Imagine using HttpClient...in a multithreaded manner:

	// shared:
	static CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource ();

	// Thread 1
	var request = new HttpRequestMessage(HttpMethod.Post, "https://www.google.com/") {
		Content = new StringContent (new string('x', 1024)),
	};
	using (request)
	using (HttpClient httpClient = new HttpClient())
	using (HttpResponseMessage response = await httpClient.SendAsync(request,
			_cancellationTokenSource.Token)) {
		string responseContent = await response.Content.ReadAsStringAsync();
		Console.WriteLine ("Read {0} chars", responseContent.Length);
	}

	// Thread 2
	_cancellationTokenSource.Cancel ();

This isn't entirely farfetched; in the case of #15857, Thread 2 was
the Main thread (due to the user clicking a button), while Thread 1
was the ThreadPool (because of async/await).

The _occasional_ result: a TargetInvocationException exception:

	System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object
	  at (wrapper delegate-invoke) <Module>:invoke_bool__this___HttpsClientStream (Mono.Security.Protocol.Tls.HttpsClientStream)
	  at System.Reflection.MonoProperty.GetterAdapterFrame[HttpsClientStream,Boolean] (System.Reflection.Getter`2 getter, System.Object obj)
	  at System.Reflection.MonoProperty.GetValue (System.Object obj, System.Object[] index)
	  --- End of inner exception stack trace ---
	  at System.Reflection.MonoProperty.GetValue (System.Object obj, System.Object[] index)
	  at System.Net.WebConnection.Write (System.Net.HttpWebRequest request, System.Byte[] buffer, Int32 offset, Int32 size, System.String& err_msg)
	  at System.Net.WebConnectionStream.WriteHeaders ()
	  at System.Net.WebConnectionStream.SetHeaders (System.Byte[] buffer)
	  at System.Net.HttpWebRequest.SendRequestHeaders (Boolean propagate_error)
	--- End of stack trace from previous location where exception was thrown ---
	  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw ()
	  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[System.Net.Http.HttpResponseMessage].GetResult ()
	  at System.Net.Http.HttpClient+<SendAsyncWorker>c__async0.MoveNext ()
	--- End of stack trace from previous location where exception was thrown ---
	  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw ()
	  at System.Runtime.CompilerServices.TaskAwaiter`1[System.Net.Http.HttpResponseMessage].GetResult ()
	  at HttpClientAsyncCancellationRepro.Activity1+<AccessGoogleButton_Click>c__async0.MoveNext ()

Huh?

The reason why is because HttpClientHandler.SendAsync() registers a
Cancel handler with the CancellationTokenSource, and the registered
handler calls HttpWebRequest.Abort() (which eventually calls
WebConnection.Close()). This causes WebConnection.nstream to _change_
while WebConnection.Write() is executing, causing
WebConnection.nstream to go from non-null to null, which
WebConnection.Write() does not expect:

	# WebConnection.CreateStream; from 5 Threadpool worker
	# WebConnection.Write; from 5 Threadpool worker
	# WebConnection.Close; from 1  at    at System.Environment.get_StackTrace()
	   at System.Net.WebConnection.Close(Boolean sendNext)
	   at System.Net.WebConnection.Abort(System.Object sender, System.EventArgs args)
	   at System.Net.WebConnection+AbortHelper.Abort(System.Object sender, System.EventArgs args)
	   at System.Net.HttpWebRequest.Abort()
	   at System.Net.Http.HttpClientHandler+<SendAsync>c__async0.<>m__0(System.Object l)
	   at System.Threading.CancellationToken+<Register>c__AnonStorey0.<>m__0()
	   at System.Threading.CancellationTokenSource.Cancel(Boolean throwOnFirstException)
	   at System.Threading.CancellationTokenSource.Cancel()
	   at System.Threading.CancellationTokenSource.SafeLinkedCancel()
	   at System.Threading.CancellationTokenSource.Cancel(Boolean throwOnFirstException)
	   at System.Threading.CancellationTokenSource.Cancel()
	   at HttpClientAsyncCancellationRepro.Activity1.CancelButton_Click(System.Object sender, System.EventArgs e)
	   at Android.Views.View+IOnClickListenerImplementor.OnClick(Android.Views.View v)
	   at Android.Views.View+IOnClickListenerInvoker.n_OnClick_Landroid_view_View_(IntPtr jnienv, IntPtr native__this, IntPtr native_v)
	   at System.Object.e7b5b870-ea0f-434f-bc31-984f665a44ad(IntPtr , IntPtr , IntPtr )
	# WebConnection.Write: checking piTrustFailure; from 5 Threadpool worker

(Yay extra logging. Notice that WebConnection creates a stream from
the ThreadPool, starts WebConnection.Write(), then before it can se
piTrustFailure the instance is Close()d, which nulls out nstream.)

Since WebConnection.Write() attempts to access the (now null)
WebConnection.nstream field, it results in a NullReferenceException,
which is wrapped into a TargetInvocationException by
System.Reflection.

The fix? As usual, "don't do that."

In this case, WebConnection.Write() has already captured
WebConnection.nstream into the local variable `s` (which thus can't be
changed by another thread). Instead of using WebConnection.nstream,
reuse the `s` local variable, which cannot be null, thus avoiding the
NullReferenceException.

(This solution could fail if HttpsClientStream ever throws e.g.
ObjectDisposedException from HttpsClientStream.TrustFailure, but at
present HttpsClientStream.TrustFailure never throws, so this is fine.)
2013-11-01 16:56:53 -04:00
Jérémie Laval 96935b19e1 [System] Add crude (for now) socket stealing of an HttpWebRequest.
HttpWebRequest is a very tricky piece of code when it comes to its socket, this patch is more like a hack to get something working out of it but it's definitely not the best option.
2013-07-29 00:47:11 -04:00
Sebastien Pouliot abe99070fa Fix type lookup when SSL/TLS is not embedded into System.dll 2013-07-02 08:23:17 -04:00
Sebastien Pouliot 96a8500795 Allow the MONOTOUCH targetr to build its SSL/TLS support right inside System.dll and remove code duplication and non-required code paths. 2013-07-01 16:21:04 -04:00
Gonzalo Paniagua Javier 9b2e52a86a Merge pull request #631 from kebby/master
Fix race condition in System.Net.WebConnectionStream
2013-06-12 11:14:20 -07:00
Sebastien Pouliot f39dc2adcd [ios] Remove the need to use reflection to call the StartWWAN method (by moving it into libmonotouch) 2013-06-04 14:42:44 -04:00
Martin Baulig eda8f9416a [WebConnection]: Fix incorrect end-of-stream with null callback.
This fixes #12355/#12395/#5904.

When EndRead() is called with a null "inner" IAsyncResult, then
we should not consider this as an end-of-stream condition.

This happens sometimes when using SSL and chunked encoding.
2013-05-27 15:52:13 +02:00
Tammo 'kb' Hinrichs 907b13aa04 Fix race condition in WebConnectionStream
See https://bugzilla.xamarin.com/show_bug.cgi?id=1553
2013-05-08 16:34:17 +02:00
Marek Safar 0647a5d16b Remove duplicated files from system.dll (saves about 100kb) 2013-02-25 19:10:09 +01:00
Miguel de Icaza 14f0405443 Revert "[HttpWebRequest] Avoid using the threadpool for synchronous requests"
This reverts commit b190db44cb.

The reason is:

"The HttpWebRequests tests are not enough"

"A quick review of the code shows that the code is taking a lock, while
calling a function that retries connections (which in turn takes another
important lock --for the socket--) which might casue a stack overflow
if someone calls SetWriteStream"

"This also will make the opereation sync when calling Begin/End"

"Finally, this is a patch that improves the use of the threadpool, but
the threadpool still will end up exhausted"

Probably best to just increase the number of threads?
2013-02-15 11:11:58 -05:00
Miguel de Icaza 6e54963a8e Revert "[WebConnection] Use just one delegate to init the connection"
This reverts commit 4367f8c04a.

This is needed before I revert b190db44cb
which Gonzalo studied and determine shoudl be removed
2013-02-15 11:11:58 -05:00