Commit Graph
138 Commits
Author SHA1 Message Date
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
Andres G. Aragoneses 4367f8c04a [WebConnection] Use just one delegate to init the connection
As suggested by Marek Safar, using WaitCallback without the threadpool
is just fine, so revert back to using one delegate.
2013-02-08 14:36:50 +00:00
Andres G. Aragoneses b190db44cb [HttpWebRequest] Avoid using the threadpool for synchronous requests
The code path for synchronous requests was the same as the one for
asynchronous ones: the threadpool. It's actually not needed in this
case because the thread is going to wait anyway, so there's no need
to use the threadpool.

This helps in scenarios in which SOA is heavily used:
- The process is hosted under xsp/mono-fastcgi/... which use the threadpool
too.
- If the requests generate other requests to other REST services, the
usage of the threadpool in these cases may interfere with the usage of
the threadpool by xsp/mono-fastcgi/...
- The thread that is serving a request shouldn't request another thread
for a blocking operation, preventing the use of that thread by other
possible incoming requests.

This commit is contributed under the terms of MIT/X11 licence.

(It doesn't cause any breakage in the System test suite.)
2013-02-07 21:25:34 +00:00
Chris Toshok 72d19698c3 Up the default max persistent connections to 10 for monotouch, and warn when we use more. 2012-11-07 13:20:19 -05:00
Martin Baulig b049dfb1a4 [6329][Web]: Fixed abort race condition in WebConnection.ReadDone().
ReadDone() now passes the 'WebConnectionData' instance to GetResponse()
and 'readState' has been moved into 'WebConnectionData'.

Abort() can now reset 'Data' while ReadDone() still uses the old
instance, but learns about the abort request from 'data.ReadState'.
2012-10-27 00:56:00 +02:00
Martin Baulig c399f1024b [#7258][Web]: Check 'Connection: close' header.
When receiving a 407 in CreateTunnel(), check whether the server
asks to close the connection.

Thanks again to Christian Gan for this patch.
2012-10-21 18:56:13 +02:00
Martin Baulig dda6a22fb4 [#7258][Web]: Support multiple proxy auth methods.
* AuthenticationManager.EnsureModules(): Swap the order of the
  modules, putting the strongest ones first.

* WebConnection.CreateTunnel(): Pass the proxy's address to
  AuthenticationManager to support CredentialsCache.  Only attempt
  one single authentication method.

I've done some extensive testing with different squid configurations
(digest only, ntlm only, digest and ntlm, ntlm and digest) and the
.NET runtime only attempts one single authentication method.

If you provide credentials like this:

	var proxy = new WebProxy (proxyUrl, false);
	proxy.Credentials = new NetworkCredential ("user", "pass", "domain");

then these must match the server's first authentication method.

You can also use a CredentialsCache like this:

	var ntlm_cred = new NetworkCredential ("u1", "p1", "d1");
	var digest_cred = new NetworkCredential ("u2", "u3", "d2");

	var cc = new CredentialCache ();
	cc.Add (proxy_uri, "NTLM", ntlm_cred);
	cc.Add (proxy_uri, "Digest", digest_cred);

	var proxy = new WebProxy (proxy_uri, false);
	proxy.Credentials = cc;
2012-10-19 03:56:16 +02:00
Christian Gan b5a6d9024c [#7258][Web]: Implement NTLM proxy authentication.
This is mostly Christian Gan's patch for the bug report, I only
did a few minor tweaks to it.  Thanks a lot for helping me out
with this.  Martin.
2012-10-18 23:45:02 +02:00
Martin Baulig 3118e82bfa [#7258][Web]: Fix proxy authentication for secure connections.
In WebConnection.CreateTunnel():

* Support multi "Proxy-Authentication" headers from the server.

* Pass a fake HttpWebRequest with method "CONNECT" to
  AuthenticationManager.Authenticate(); the HTTP method is part
  of the digest response.

* Add "Proxy-Connection: Keep-Alive".
2012-10-18 00:45:37 +02:00
Martin Baulig 9a8ed89b84 [#7200]: WebRequest returns result in another thread.
This is a partial revertion of master commit #9069d07, which was
backported into mono-2-10 in commit #bfa23f8.

I only reverted the change in line 716, keeping the rest of the
commit in place.
2012-09-27 02:24:20 +02:00
Sebastien Pouliot 57fb542729 Wrap potential exceptions (like #6425 NRE) inside an (expected) WebException 2012-08-10 16:53:51 -04:00
Gonzalo Paniagua Javier c26c9cf9bd GetResponse() returns 0 when it need more data, not -1
GetResponse() returns 0 to signal that it needs more data to complete
	while -1 signals an error processing the data.
2012-07-16 13:51:17 -04:00
Gonzalo Paniagua Javier 69dad66f00 Attempt to fix #1553
This should prevent a popular exception to happen again.
2012-06-28 15:48:57 -04:00
Marek Safar 9069d07837 Don't double dispose web connection 2012-06-22 12:29:11 +01:00
Gonzalo Paniagua Javier 271524d471 Aborting a pending request closes the socket now
Aborting an HttpWebRequest whose request stream is not closed yet will
	now close the underlying socket properly.
	Fixes bug #5710.
2012-06-18 22:50:57 -04:00
Rolf Bjarne Kvinge 1a237e871d Ensure we get the correct StartWWAN overload. 2012-04-04 16:02:45 +02:00
Gonzalo Paniagua Javier 864d354b59 If we read an entire chunk, return it in the next read operation
When waiting for the chunk trailer, we can return the data read
	so far.
2012-03-13 11:57:51 -04:00
Justin Clark-Casey 159271bfbe If WebConnection.GetResponse() returns -1 in ReadDone() then also treat this as a ServerProtocolViolation rather than failing with an exception later on.
We occasionally see this happen in the OpenSimulator project when web requests are made (the project makes a lot of these).
I'm reasoning that a -1 from GetResponse() also indicates that the response is badly formatted (wrong number of parts or not properly terminated) as well as catching an Exception.
Making this change removes the issues that we see.
2012-03-01 01:06:08 +00:00
symform ccfe7d6e74 Fixed a race in System.Net.WebConnection that was causing it to throw NullReferenceException on Close/Dispose. 2012-01-03 17:22:45 -08:00
Jackson Harper 01767e670e We can accidently swallow out of FD errors here since this function is called from a threadpool thread. 2011-06-28 23:22:44 -04:00
Gonzalo Paniagua Javier 3eb2f54bb1 [HttpWebRequest] Ignore Transfer-Encoding when there's no content.
If the transfer encoding was set to chunked, we tried to read even when
	the response might known to have no content. This led to timeouts.

	Fixes bug #686371.
2011-04-09 15:18:00 -04:00
Gonzalo Paniagua Javier 891af303aa [Socket] Implemented TCP keep alive support
Implemented SetTcpKeepAlive for ServicePoint*.
2011-01-13 18:28:16 -05:00
Gonzalo Paniagua Javier 604d5faa0b More StringComparison.Ordinal
Use StringComparison.Ordinal when calling IndexOf to find the index of
	an ASCII string.
2010-10-31 17:23:18 -04:00
Gonzalo Paniagua Javier f832b99535 Avoid sending data in the challenge phase of NTLM
If there's a POST/PUT request that gets a NTLM 401, we save the data
	and don't send it again until after the challenge phase. Ie, instead of
	sending all the data 3 times, we only send it at most twice.
2010-09-01 15:52:09 -04:00
Geoff Norton 42bb87da50 Ensure that we only reflect into monotouch if the type exists.
There is a possible crash using this profile if the monotouch.dll
isn't loaded into the process space.  We will null guard the
type and only call start_wwan if its present.
2010-08-20 10:54:07 -04:00
Gonzalo Paniagua Javier eb2a5490f2 Adapt to new exception in threadpool behavior.
Since an exception in the threadpool can kill the application,
	silently ignore it when we know it is not harmful.
2010-08-11 13:15:39 -04:00
Sebastien Pouliot 9c8cb87fe3 In .:
2010-07-21  Sebastien Pouliot  <[email protected]>

	* Makefile: Ensure SECURITY_DEP gets defined in our moonlight
	raw profile

In System.Net:
2010-07-21  Sebastien Pouliot  <[email protected]>

	* ServicePointManager.cs: Add special validation for Moonlight
	* WebConnection.cs: Change a define to NET_2_1

In System.Net.Security:
2010-07-21  Sebastien Pouliot  <[email protected]>

	* LocalCertificateSelectionCallback.cs:
	* SslStream.cs:
		Don't use alias in moonlight build (not needed and breaks
		compilation otherwise)


svn path=/trunk/mcs/; revision=160675
2010-07-21 13:36:35 +00:00
Miguel de Icaza bd17ba1532 2010-03-20 Miguel de Icaza <[email protected]>
* WebConnection.cs: Drop pre-2.0 and pre-1.1 support, and remove
	warnings. 

svn path=/trunk/mcs/; revision=153946
2010-03-20 22:53:39 +00:00
Gonzalo Paniagua Javier 5c20ab97e6 2010-03-01 Gonzalo Paniagua Javier <[email protected]>
* System.Net/WebConnection.cs: attach a validation callback to the new
	event in Mono.Security.
	* System.Net/ServicePointManager.cs: added a new ValidationHelper
	class that takes care of building the certificate chain and perform
	all the check that were done in Mono.Security before. This way the 2.0
	server certificate validation callback can get all the data it needs.
	* System.Net.Security/SslStream.cs: don't do anything if a chain
	element has no error.

	Fixes bug #584050.

svn path=/trunk/mcs/; revision=152654
2010-03-01 09:01:14 +00:00