Example #1
0
		public static void Run (Func<SimpleAsyncResult, bool> func, SimpleAsyncCallback callback)
		{
			var result = new SimpleAsyncResult (callback);
			try {
				if (!func (result))
					result.SetCompleted (true);
			} catch (Exception ex) {
				result.SetCompleted (true, ex);
			}
		}
Example #2
0
		public static SimpleAsyncResult Run (SimpleAsyncFunc func, SimpleAsyncCallback callback)
		{
			var result = new SimpleAsyncResult (callback);
			try {
				if (!func (result))
					result.SetCompleted (true);
			} catch (Exception ex) {
				result.SetCompleted (true, ex);
			}
			return result;
		}
Example #3
0
        bool SetHeadersAsync(SimpleAsyncResult result, bool setInternalLength)
        {
            if (headersSent)
            {
                return(false);
            }

            string method         = request.Method;
            bool   no_writestream = (method == "GET" || method == "CONNECT" || method == "HEAD" ||
                                     method == "TRACE");
            bool webdav = (method == "PROPFIND" || method == "PROPPATCH" || method == "MKCOL" ||
                           method == "COPY" || method == "MOVE" || method == "LOCK" ||
                           method == "UNLOCK");

            if (setInternalLength && !no_writestream && writeBuffer != null)
            {
                request.InternalContentLength = writeBuffer.Length;
            }

            bool has_content = !no_writestream && (writeBuffer == null || request.ContentLength > -1);

            if (!(sendChunked || has_content || no_writestream || webdav))
            {
                return(false);
            }

            headersSent = true;
            headers     = request.GetRequestHeaders();

            var innerResult = cnc.BeginWrite(request, headers, 0, headers.Length, r => {
                try {
                    cnc.EndWrite(request, true, r);
                    if (!initRead)
                    {
                        initRead = true;
                        cnc.InitRead();
                    }
                    var cl = request.ContentLength;
                    if (!sendChunked && cl == 0)
                    {
                        requestWritten = true;
                    }
                    result.SetCompleted(false);
                } catch (WebException e) {
                    result.SetCompleted(false, e);
                } catch (Exception e) {
                    result.SetCompleted(false, new WebException("Error writing headers", WebExceptionStatus.SendFailure, WebExceptionInternalStatus.RequestFatal, e));
                }
            }, null);

            return(innerResult != null);
        }
Example #4
0
        public static void Run(Func <SimpleAsyncResult, bool> func, SimpleAsyncCallback callback)
        {
            var result = new SimpleAsyncResult(callback);

            try {
                if (!func(result))
                {
                    result.SetCompleted(true);
                }
            } catch (Exception ex) {
                result.SetCompleted(true, ex);
            }
        }
Example #5
0
        public static SimpleAsyncResult Run(SimpleAsyncFunc func, SimpleAsyncCallback callback)
        {
            var result = new SimpleAsyncResult(callback);

            try {
                if (!func(result))
                {
                    result.SetCompleted(true);
                }
            } catch (Exception ex) {
                result.SetCompleted(true, ex);
            }
            return(result);
        }
Example #6
0
		internal bool WriteRequestAsync (SimpleAsyncResult result)
		{
			if (requestWritten)
				return false;

			requestWritten = true;
			if (sendChunked || !allowBuffering || writeBuffer == null)
				return false;

			// Keep the call for a potential side-effect of GetBuffer
			var bytes = writeBuffer.GetBuffer ();
			var length = (int)writeBuffer.Length;
			if (request.ContentLength != -1 && request.ContentLength < length) {
				nextReadCalled = true;
				cnc.Close (true);
				throw new WebException ("Specified Content-Length is less than the number of bytes to write", null,
					WebExceptionStatus.ServerProtocolViolation, null);
			}

			SetHeadersAsync (true, inner => {
				if (inner.GotException) {
					result.SetCompleted (inner.CompletedSynchronouslyPeek, inner.Exception);
					return;
				}

				if (cnc.Data.StatusCode != 0 && cnc.Data.StatusCode != 100) {
					result.SetCompleted (inner.CompletedSynchronouslyPeek);
					return;
				}

				if (!initRead) {
					initRead = true;
					WebConnection.InitRead (cnc);
				}

				if (length == 0) {
					complete_request_written = true;
					result.SetCompleted (inner.CompletedSynchronouslyPeek);
					return;
				}

				cnc.BeginWrite (request, bytes, 0, length, r => {
					try {
						complete_request_written = cnc.EndWrite (request, false, r);
						result.SetCompleted (false);
					} catch (Exception exc) {
						result.SetCompleted (false, exc);
					}
				}, null);
			});

			return true;
		}
Example #7
0
		bool SetHeadersAsync (SimpleAsyncResult result, bool setInternalLength)
		{
			if (headersSent)
				return false;

			string method = request.Method;
			bool no_writestream = (method == "GET" || method == "CONNECT" || method == "HEAD" ||
			                      method == "TRACE");
			bool webdav = (method == "PROPFIND" || method == "PROPPATCH" || method == "MKCOL" ||
			              method == "COPY" || method == "MOVE" || method == "LOCK" ||
			              method == "UNLOCK");

			if (setInternalLength && !no_writestream && writeBuffer != null)
				request.InternalContentLength = writeBuffer.Length;

			bool has_content = !no_writestream && (writeBuffer == null || request.ContentLength > -1);
			if (!(sendChunked || has_content || no_writestream || webdav))
				return false;

			headersSent = true;
			headers = request.GetRequestHeaders ();

			var innerResult = cnc.BeginWrite (request, headers, 0, headers.Length, r => {
				try {
					cnc.EndWrite (request, true, r);
					if (!initRead) {
						initRead = true;
						WebConnection.InitRead (cnc);
					}
					var cl = request.ContentLength;
					if (!sendChunked && cl == 0)
						requestWritten = true;
					result.SetCompleted (false);
				} catch (WebException e) {
					result.SetCompleted (false, e);
				} catch (Exception e) {
					result.SetCompleted (false, new WebException ("Error writing headers", WebExceptionStatus.SendFailure, WebExceptionInternalStatus.RequestFatal, e));
				}
			}, null);

			return innerResult != null;
		}
		bool CheckIfForceWrite (SimpleAsyncResult result)
		{
			if (writeStream == null || writeStream.RequestWritten || !InternalAllowBuffering)
				return false;
			if (contentLength < 0 && writeStream.CanWrite == true && writeStream.WriteBufferLength < 0)
				return false;

			if (contentLength < 0 && writeStream.WriteBufferLength >= 0)
				InternalContentLength = writeStream.WriteBufferLength;

			// This will write the POST/PUT if the write stream already has the expected
			// amount of bytes in it (ContentLength) (bug #77753) or if the write stream
			// contains data and it has been closed already (xamarin bug #1512).

			if (writeStream.WriteBufferLength == contentLength || (contentLength == -1 && writeStream.CanWrite == false))
				return writeStream.WriteRequestAsync (result);

			return false;
		}
Example #9
0
        internal bool WriteRequestAsync(SimpleAsyncResult result)
        {
            if (requestWritten)
            {
                return(false);
            }

            requestWritten = true;
            if (sendChunked || !allowBuffering || writeBuffer == null)
            {
                return(false);
            }

            // Keep the call for a potential side-effect of GetBuffer
            var bytes  = writeBuffer.GetBuffer();
            var length = (int)writeBuffer.Length;

            if (request.ContentLength != -1 && request.ContentLength < length)
            {
                nextReadCalled = true;
                cnc.Close(true);
                throw new WebException("Specified Content-Length is less than the number of bytes to write", null,
                                       WebExceptionStatus.ServerProtocolViolation, null);
            }

            SetHeadersAsync(true, inner => {
                if (inner.GotException)
                {
                    result.SetCompleted(inner.CompletedSynchronouslyPeek, inner.Exception);
                    return;
                }

                if (cnc.Data.StatusCode != 0 && cnc.Data.StatusCode != 100)
                {
                    result.SetCompleted(inner.CompletedSynchronouslyPeek);
                    return;
                }

                if (!initRead)
                {
                    initRead = true;
                    cnc.InitRead();
                }

                if (length == 0)
                {
                    complete_request_written = true;
                    result.SetCompleted(inner.CompletedSynchronouslyPeek);
                    return;
                }

                cnc.BeginWrite(request, bytes, 0, length, r => {
                    try {
                        complete_request_written = cnc.EndWrite(request, false, r);
                        result.SetCompleted(false);
                    } catch (Exception exc) {
                        result.SetCompleted(false, exc);
                    }
                }, null);
            });

            return(true);
        }
Example #10
0
 internal void SetHeadersAsync(bool setInternalLength, SimpleAsyncCallback callback)
 {
     SimpleAsyncResult.Run(r => SetHeadersAsync(r, setInternalLength), callback);
 }
Example #11
0
		bool SetHeadersAsync (SimpleAsyncResult result, bool setInternalLength)
		{
			if (headersSent)
				return false;

			bool webdav = false;
			bool writestream = false;

			switch (request.Method) {
			case "PROPFIND":
			case "PROPPATCH":
			case "MKCOL":
			case "COPY":
			case "MOVE":
			case "LOCK":
			case "UNLOCK":
				webdav = true;
				break;
			case "POST":
			case "PUT":
				writestream = true;
				break;
			}

			if (setInternalLength && writestream && writeBuffer != null)
				request.InternalContentLength = writeBuffer.Length;

			if (!(sendChunked || request.ContentLength > -1 || !writestream || webdav))
				return false;

			headersSent = true;
			headers = request.GetRequestHeaders ();

			var innerResult = cnc.BeginWrite (request, headers, 0, headers.Length, r => {
				try {
					cnc.EndWrite (request, true, r);
					if (!initRead) {
						initRead = true;
						WebConnection.InitRead (cnc);
					}
					var cl = request.ContentLength;
					if (!sendChunked && cl == 0)
						requestWritten = true;
					result.SetCompleted (false);
				} catch (WebException e) {
					result.SetCompleted (false, e);
				} catch (Exception e) {
					result.SetCompleted (false, new WebException ("Error writing headers", e, WebExceptionStatus.SendFailure));
				}
			}, null);

			return innerResult != null;
		}
Example #12
0
        bool SetHeadersAsync(SimpleAsyncResult result, bool setInternalLength)
        {
            if (headersSent)
            {
                return(false);
            }

            bool webdav      = false;
            bool writestream = false;

            switch (request.Method)
            {
            case "PROPFIND":
            case "PROPPATCH":
            case "MKCOL":
            case "COPY":
            case "MOVE":
            case "LOCK":
            case "UNLOCK":
                webdav = true;
                break;

            case "POST":
            case "PUT":
                writestream = true;
                break;
            }

            if (setInternalLength && writestream && writeBuffer != null)
            {
                request.InternalContentLength = writeBuffer.Length;
            }

            if (!(sendChunked || request.ContentLength > -1 || !writestream || webdav))
            {
                return(false);
            }

            headersSent = true;
            headers     = request.GetRequestHeaders();

            var innerResult = cnc.BeginWrite(request, headers, 0, headers.Length, r => {
                try {
                    cnc.EndWrite(request, true, r);
                    if (!initRead)
                    {
                        initRead = true;
                        WebConnection.InitRead(cnc);
                    }
                    var cl = request.ContentLength;
                    if (!sendChunked && cl == 0)
                    {
                        requestWritten = true;
                    }
                    result.SetCompleted(false);
                } catch (WebException e) {
                    result.SetCompleted(false, e);
                } catch (Exception e) {
                    result.SetCompleted(false, new WebException("Error writing headers", e, WebExceptionStatus.SendFailure));
                }
            }, null);

            return(innerResult != null);
        }
Example #13
0
 internal SimpleAsyncResult SetHeadersAsync(bool setInternalLength, SimpleAsyncCallback callback)
 {
     return(SimpleAsyncResult.Run(r => SetHeadersAsync(r, setInternalLength), callback));
 }
        public override IAsyncResult BeginGetRequestStream(AsyncCallback callback, object state)
        {
            if (_requestStream != null) {
                throw new InvalidOperationException("A request stream has already been created.");
            }

            SimpleAsyncResult result = new SimpleAsyncResult() {
                AsyncState = state,
                CompletedSynchronously = true,
                IsCompleted = true
            };
            callback(result);
            return result;
        }
        public override IAsyncResult BeginGetResponse(AsyncCallback callback, object state)
        {
            if (HaveResponse) {
                throw new InvalidOperationException("A response object associated with this request has not been closed.");
            }
            if (_responseAsyncResult != null) {
                throw new InvalidOperationException("Already retrieving the response.");
            }

            _responseAsyncResult = new SimpleAsyncResult() {
                AsyncCallback = callback,
                AsyncState = state
            };

            _nativeRequest.Invoke("open", Method, _requestUri.OriginalString);
            foreach (var headerName in Headers.AllKeys) {
                _nativeRequest.Invoke("setRequestHeader", headerName, Headers[headerName]);
            }

            _nativeRequest.SetProperty("onreadystatechange",
                ScriptObjectUtility.ToScriptFunction((Action)ReadyStateChanged, args => HtmlPage.Window.CreateInstance("Array")));
            _nativeRequest.Invoke("send", GetBody());

            _responseAsyncResult.CompletedSynchronously = HaveResponse;
            _responseAsyncResult.IsCompleted = HaveResponse;

            return _responseAsyncResult;
        }