Inheritance: InvalidOperationException, ISerializable
Beispiel #1
0
        // methods

        /// <include file='doc\filewebrequest.uex' path='docs/doc[@for="FileWebRequest.BeginGetRequestStream"]/*' />
        public override IAsyncResult BeginGetRequestStream(AsyncCallback callback, object state)
        {
            GlobalLog.Enter("FileWebRequest::BeginGetRequestStream");
            if (!CanGetRequestStream())
            {
                Exception e = new ProtocolViolationException(SR.GetString(SR.net_nouploadonget));
                GlobalLog.LeaveException("FileWebRequest::BeginGetRequestStream", e);
                throw e;
            }
            if (m_response != null)
            {
                Exception e = new InvalidOperationException(SR.GetString(SR.net_reqsubmitted));
                GlobalLog.LeaveException("FileWebRequest::BeginGetRequestStream", e);
                throw e;
            }
            lock (this) {
                if (m_writePending)
                {
                    Exception e = new InvalidOperationException(SR.GetString(SR.net_repcall));
                    GlobalLog.LeaveException("FileWebRequest::BeginGetRequestStream", e);
                    throw e;
                }
                m_writePending = true;
            }
            m_GetRequestStreamDelegate = new AsyncGetRequestStream(InternalGetRequestStream);
            GlobalLog.Leave("FileWebRequest::BeginGetRequestStream");
            return(m_GetRequestStreamDelegate.BeginInvoke(callback, state));
        }
Beispiel #2
0
        public override IAsyncResult BeginGetRequestStream(AsyncCallback callback, object state)
        {
            GlobalLog.Enter("FileWebRequest::BeginGetRequestStream");

            try {
                if (Aborted)
                {
                    throw ExceptionHelper.RequestAbortedException;
                }
                if (!CanGetRequestStream())
                {
                    Exception e = new ProtocolViolationException(SR.GetString(SR.net_nouploadonget));
                    GlobalLog.LeaveException("FileWebRequest::BeginGetRequestStream", e);
                    throw e;
                }
                if (m_response != null)
                {
                    Exception e = new InvalidOperationException(SR.GetString(SR.net_reqsubmitted));
                    GlobalLog.LeaveException("FileWebRequest::BeginGetRequestStream", e);
                    throw e;
                }
                lock (this) {
                    if (m_writePending)
                    {
                        Exception e = new InvalidOperationException(SR.GetString(SR.net_repcall));
                        GlobalLog.LeaveException("FileWebRequest::BeginGetRequestStream", e);
                        throw e;
                    }
                    m_writePending = true;
                }

                //we need to force the capture of the identity and context to make sure the
                //posted callback doesn't inavertently gain access to something it shouldn't.
                m_ReadAResult = new LazyAsyncResult(this, state, callback);
                ThreadPool.QueueUserWorkItem(s_GetRequestStreamCallback, m_ReadAResult);
            } catch (Exception exception) {
                if (Logging.On)
                {
                    Logging.Exception(Logging.Web, this, "BeginGetRequestStream", exception);
                }
                throw;
            } finally {
                GlobalLog.Leave("FileWebRequest::BeginGetRequestSteam");
            }

            string suri;

            if (FrameworkEventSource.Log.IsEnabled(EventLevel.Verbose, FrameworkEventSource.Keywords.NetClient))
            {
                suri = this.RequestUri.ToString();
            }
            else
            {
                suri = this.RequestUri.OriginalString;
            }
            FrameworkEventSource.Log.BeginGetRequestStream(this, suri);

            return(m_ReadAResult);
        }
Beispiel #3
0
		bool Redirect (WebAsyncResult result, HttpStatusCode code)
		{
			redirects++;
			Exception e = null;
			string uriString = null;

			switch (code) {
			case HttpStatusCode.Ambiguous: // 300
				e = new WebException ("Ambiguous redirect.");
				break;
			case HttpStatusCode.MovedPermanently: // 301
			case HttpStatusCode.Redirect: // 302
			case HttpStatusCode.TemporaryRedirect: // 307
				/* MS follows the redirect for POST too
				if (method != "GET" && method != "HEAD") // 10.3
					return false;
				*/

				contentLength = -1;
				bodyBufferLength = 0;
				bodyBuffer = null;
				if (code != HttpStatusCode.TemporaryRedirect)
					method = "GET";
				uriString = webResponse.Headers ["Location"];
				break;
			case HttpStatusCode.SeeOther: //303
				method = "GET";
				uriString = webResponse.Headers ["Location"];
				break;
			case HttpStatusCode.NotModified: // 304
				return false;
			case HttpStatusCode.UseProxy: // 305
				e = new NotImplementedException ("Proxy support not available.");
				break;
			case HttpStatusCode.Unused: // 306
			default:
				e = new ProtocolViolationException ("Invalid status code: " + (int) code);
				break;
			}

			if (e != null)
				throw e;

			if (uriString == null)
				throw new WebException ("No Location header found for " + (int) code,
							WebExceptionStatus.ProtocolError);

			Uri prev = actualUri;
			try {
				actualUri = new Uri (actualUri, uriString);
			} catch (Exception) {
				throw new WebException (String.Format ("Invalid URL ({0}) for {1}",
									uriString, (int) code),
									WebExceptionStatus.ProtocolError);
			}

			hostChanged = (actualUri.Scheme != prev.Scheme || Host != prev.Authority);
			return true;
		}
        public override IAsyncResult BeginGetRequestStream(AsyncCallback callback, object state)
        {
            GlobalLog.Enter("FileWebRequest::BeginGetRequestStream");
#if !MONO
            bool success = true;
#endif
            try {
                if (Aborted)
                {
                    throw ExceptionHelper.RequestAbortedException;
                }
                if (!CanGetRequestStream())
                {
                    Exception e = new ProtocolViolationException(SR.GetString(SR.net_nouploadonget));
                    GlobalLog.LeaveException("FileWebRequest::BeginGetRequestStream", e);
                    throw e;
                }
                if (m_response != null)
                {
                    Exception e = new InvalidOperationException(SR.GetString(SR.net_reqsubmitted));
                    GlobalLog.LeaveException("FileWebRequest::BeginGetRequestStream", e);
                    throw e;
                }
                lock (this) {
                    if (m_writePending)
                    {
                        Exception e = new InvalidOperationException(SR.GetString(SR.net_repcall));
                        GlobalLog.LeaveException("FileWebRequest::BeginGetRequestStream", e);
                        throw e;
                    }
                    m_writePending = true;
                }

                //we need to force the capture of the identity and context to make sure the
                //posted callback doesn't inavertently gain access to something it shouldn't.
                m_ReadAResult = new LazyAsyncResult(this, state, callback);
                ThreadPool.QueueUserWorkItem(s_GetRequestStreamCallback, m_ReadAResult);
            } catch (Exception exception) {
#if !MONO
                success = false;
#endif
                if (Logging.On)
                {
                    Logging.Exception(Logging.Web, this, "BeginGetRequestStream", exception);
                }
                throw;
            } finally {
#if !MONO
                if (FrameworkEventSource.Log.IsEnabled())
                {
                    LogBeginGetRequestStream(success, synchronous: false);
                }
#endif
                GlobalLog.Leave("FileWebRequest::BeginGetRequestSteam");
            }

            return(m_ReadAResult);
        }
Beispiel #5
0
        public override IAsyncResult BeginGetRequestStream(AsyncCallback callback, object state)
        {
            GlobalLog.Enter("FileWebRequest::BeginGetRequestStream");

            try {
                if (Aborted)
                {
                    throw ExceptionHelper.RequestAbortedException;
                }
                if (!CanGetRequestStream())
                {
                    Exception e = new ProtocolViolationException(SR.GetString(SR.net_nouploadonget));
                    GlobalLog.LeaveException("FileWebRequest::BeginGetRequestStream", e);
                    throw e;
                }
                if (m_response != null)
                {
                    Exception e = new InvalidOperationException(SR.GetString(SR.net_reqsubmitted));
                    GlobalLog.LeaveException("FileWebRequest::BeginGetRequestStream", e);
                    throw e;
                }
                lock (this) {
                    if (m_writePending)
                    {
                        Exception e = new InvalidOperationException(SR.GetString(SR.net_repcall));
                        GlobalLog.LeaveException("FileWebRequest::BeginGetRequestStream", e);
                        throw e;
                    }
                    m_writePending = true;
                }

                //we need to force the capture of the identity and context to make sure the
                //posted callback doesn't inavertently gain access to something it shouldn't.
                m_ReadAResult = new ContextAwareResult(true, true, true, this, state, callback);
                lock (m_ReadAResult.StartPostingAsyncOp())
                {
                    ThreadPool.UnsafeQueueUserWorkItem(s_GetRequestStreamCallback, m_ReadAResult);
                    m_ReadAResult.FinishPostingAsyncOp();
                }
            } catch (Exception exception) {
                if (Logging.On)
                {
                    Logging.Exception(Logging.Web, this, "BeginGetRequestStream", exception);
                }
                throw;
            } finally {
                GlobalLog.Leave("FileWebRequest::BeginGetRequestSteam");
            }
            return(m_ReadAResult);
        }
        void CheckWriteOverflow(long contentLength, long totalWritten, long size)
        {
            if (contentLength == -1)
            {
                return;
            }

            long avail = contentLength - totalWritten;

            if (size > avail)
            {
                KillBuffer();
                closed = true;
                var throwMe = new ProtocolViolationException(
                    "The number of bytes to be written is greater than " +
                    "the specified ContentLength.");
                Operation.CompleteRequestWritten(this, throwMe);
                throw throwMe;
            }
        }
 public override IAsyncResult BeginGetRequestStream(AsyncCallback callback, object state)
 {
     try
     {
         if (this.Aborted)
         {
             throw ExceptionHelper.RequestAbortedException;
         }
         if (!this.CanGetRequestStream())
         {
             Exception exception = new ProtocolViolationException(SR.GetString("net_nouploadonget"));
             throw exception;
         }
         if (this.m_response != null)
         {
             Exception exception2 = new InvalidOperationException(SR.GetString("net_reqsubmitted"));
             throw exception2;
         }
         lock (this)
         {
             if (this.m_writePending)
             {
                 Exception exception3 = new InvalidOperationException(SR.GetString("net_repcall"));
                 throw exception3;
             }
             this.m_writePending = true;
         }
         this.m_ReadAResult = new LazyAsyncResult(this, state, callback);
         ThreadPool.QueueUserWorkItem(s_GetRequestStreamCallback, this.m_ReadAResult);
     }
     catch (Exception exception4)
     {
         if (Logging.On)
         {
             Logging.Exception(Logging.Web, this, "BeginGetRequestStream", exception4);
         }
         throw;
     }
     return(this.m_ReadAResult);
 }
Beispiel #8
0
        public override IAsyncResult BeginGetRequestStream(AsyncCallback callback, object state)
        {
            GlobalLog.Enter("FileWebRequest::BeginGetRequestStream");

            try {
                if (Aborted)
                    throw ExceptionHelper.RequestAbortedException;
                if (!CanGetRequestStream()) {
                    Exception e = new ProtocolViolationException(SR.GetString(SR.net_nouploadonget));
                    GlobalLog.LeaveException("FileWebRequest::BeginGetRequestStream", e);
                    throw e;
                }
                if (m_response != null) {
                    Exception e = new InvalidOperationException(SR.GetString(SR.net_reqsubmitted));
                    GlobalLog.LeaveException("FileWebRequest::BeginGetRequestStream", e);
                    throw e;
                }
                lock(this) {
                    if (m_writePending) {
                        Exception e = new InvalidOperationException(SR.GetString(SR.net_repcall));
                        GlobalLog.LeaveException("FileWebRequest::BeginGetRequestStream", e);
                        throw e;
                    }
                    m_writePending = true;
                }
                                                    
                //we need to force the capture of the identity and context to make sure the
                //posted callback doesn't inavertently gain access to something it shouldn't.
                m_ReadAResult = new LazyAsyncResult(this, state, callback);
                ThreadPool.QueueUserWorkItem(s_GetRequestStreamCallback, m_ReadAResult);
            } catch (Exception exception) {
                if(Logging.On)Logging.Exception(Logging.Web, this, "BeginGetRequestStream", exception);
                throw;
            } finally {
                GlobalLog.Leave("FileWebRequest::BeginGetRequestSteam");
            }

            string suri;
            if (FrameworkEventSource.Log.IsEnabled(EventLevel.Verbose, FrameworkEventSource.Keywords.NetClient))
                suri = this.RequestUri.ToString();
            else
                suri = this.RequestUri.OriginalString;
            FrameworkEventSource.Log.BeginGetRequestStream(this, suri);

            return m_ReadAResult;
        }
Beispiel #9
0
		bool Redirect (WebAsyncResult result, HttpStatusCode code)
		{
			redirects++;
			Exception e = null;
			string uriString = null;

			switch (code) {
			case HttpStatusCode.Ambiguous: // 300
				e = new WebException ("Ambiguous redirect.");
				break;
			case HttpStatusCode.MovedPermanently: // 301
			case HttpStatusCode.Redirect: // 302
			case HttpStatusCode.TemporaryRedirect: // 307
				/* MS follows the redirect for POST too
				if (method != "GET" && method != "HEAD") // 10.3
					return false;
				*/

				contentLength = -1;
				bodyBufferLength = 0;
				bodyBuffer = null;
				if (code != HttpStatusCode.TemporaryRedirect)
					method = "GET";
				uriString = webResponse.Headers ["Location"];
				break;
			case HttpStatusCode.SeeOther: //303
				method = "GET";
				uriString = webResponse.Headers ["Location"];
				break;
			case HttpStatusCode.NotModified: // 304
				return false;
			case HttpStatusCode.UseProxy: // 305
				e = new NotImplementedException ("Proxy support not available.");
				break;
			case HttpStatusCode.Unused: // 306
			default:
				e = new ProtocolViolationException ("Invalid status code: " + (int) code);
				break;
			}

			if (e != null)
				throw e;

			if (uriString == null)
				throw new WebException ("No Location header found for " + (int) code,
							WebExceptionStatus.ProtocolError);

			Uri prev = actualUri;
			try {
				actualUri = new Uri (actualUri, uriString);
			} catch (Exception) {
				throw new WebException (String.Format ("Invalid URL ({0}) for {1}",
									uriString, (int) code),
									WebExceptionStatus.ProtocolError);
			}

			hostChanged = (actualUri.Scheme != prev.Scheme || Host != prev.Authority);
			return true;
		}
        public override IAsyncResult BeginGetRequestStream(AsyncCallback callback, object state)
        {
            GlobalLog.Enter("FileWebRequest::BeginGetRequestStream");

            try {
                if (Aborted)
                    throw ExceptionHelper.RequestAbortedException;
                if (!CanGetRequestStream()) {
                    Exception e = new ProtocolViolationException(SR.GetString(SR.net_nouploadonget));
                    GlobalLog.LeaveException("FileWebRequest::BeginGetRequestStream", e);
                    throw e;
                }
                if (m_response != null) {
                    Exception e = new InvalidOperationException(SR.GetString(SR.net_reqsubmitted));
                    GlobalLog.LeaveException("FileWebRequest::BeginGetRequestStream", e);
                    throw e;
                }
                lock(this) {
                    if (m_writePending) {
                        Exception e = new InvalidOperationException(SR.GetString(SR.net_repcall));
                        GlobalLog.LeaveException("FileWebRequest::BeginGetRequestStream", e);
                        throw e;
                    }
                    m_writePending = true;
                }
                                                    
                //we need to force the capture of the identity and context to make sure the
                //posted callback doesn't inavertently gain access to something it shouldn't.
                m_ReadAResult = new ContextAwareResult(true, true, true, this, state, callback);
                lock (m_ReadAResult.StartPostingAsyncOp())
                {
                    ThreadPool.UnsafeQueueUserWorkItem(s_GetRequestStreamCallback, m_ReadAResult);
                    m_ReadAResult.FinishPostingAsyncOp();
                }
            } catch (Exception exception) {
                if(Logging.On)Logging.Exception(Logging.Web, this, "BeginGetRequestStream", exception);
                throw;
            } finally {
                GlobalLog.Leave("FileWebRequest::BeginGetRequestSteam");
            }
            return m_ReadAResult;
        }
Beispiel #11
0
		bool Redirect (WebAsyncResult result, HttpStatusCode code, WebResponse response)
		{
			redirects++;
			Exception e = null;
			string uriString = null;
			switch (code) {
			case HttpStatusCode.Ambiguous: // 300
				e = new WebException ("Ambiguous redirect.");
				break;
			case HttpStatusCode.MovedPermanently: // 301
			case HttpStatusCode.Redirect: // 302
				if (method == "POST")
					RewriteRedirectToGet ();
				break;
			case HttpStatusCode.TemporaryRedirect: // 307
				break;
			case HttpStatusCode.SeeOther: //303
				RewriteRedirectToGet ();
				break;
			case HttpStatusCode.NotModified: // 304
				return false;
			case HttpStatusCode.UseProxy: // 305
				e = new NotImplementedException ("Proxy support not available.");
				break;
			case HttpStatusCode.Unused: // 306
			default:
				e = new ProtocolViolationException ("Invalid status code: " + (int) code);
				break;
			}

			if (method != "GET" && !InternalAllowBuffering)
				e = new WebException ("The request requires buffering data to succeed.", null, WebExceptionStatus.ProtocolError, webResponse);

			if (e != null)
				throw e;

			contentLength = -1;
			uriString = webResponse.Headers ["Location"];

			if (uriString == null)
				throw new WebException ("No Location header found for " + (int) code,
							WebExceptionStatus.ProtocolError);

			Uri prev = actualUri;
			try {
				actualUri = new Uri (actualUri, uriString);
			} catch (Exception) {
				throw new WebException (String.Format ("Invalid URL ({0}) for {1}",
									uriString, (int) code),
									WebExceptionStatus.ProtocolError);
			}

			hostChanged = (actualUri.Scheme != prev.Scheme || Host != prev.Authority);
			return true;
		}
 public override IAsyncResult BeginGetRequestStream(AsyncCallback callback, object state)
 {
     try
     {
         if (this.Aborted)
         {
             throw ExceptionHelper.RequestAbortedException;
         }
         if (!this.CanGetRequestStream())
         {
             Exception exception = new ProtocolViolationException(SR.GetString("net_nouploadonget"));
             throw exception;
         }
         if (this.m_response != null)
         {
             Exception exception2 = new InvalidOperationException(SR.GetString("net_reqsubmitted"));
             throw exception2;
         }
         lock (this)
         {
             if (this.m_writePending)
             {
                 Exception exception3 = new InvalidOperationException(SR.GetString("net_repcall"));
                 throw exception3;
             }
             this.m_writePending = true;
         }
         this.m_ReadAResult = new LazyAsyncResult(this, state, callback);
         ThreadPool.QueueUserWorkItem(s_GetRequestStreamCallback, this.m_ReadAResult);
     }
     catch (Exception exception4)
     {
         if (Logging.On)
         {
             Logging.Exception(Logging.Web, this, "BeginGetRequestStream", exception4);
         }
         throw;
     }
     return this.m_ReadAResult;
 }
		/// <summary>
		/// Callback method invoked when the given messages have failed to be sent
		/// because of an unexpected Bayeux server exception was thrown.
		/// </summary>
		/// <param name="info">Bayeux server error message.</param>
		/// <param name="ex">The exception that caused the failure.</param>
		/// <param name="messages">The messages being sent.</param>
		public virtual void OnProtocolError(string info, Exception ex, IMessage[] messages)
		{
			Exception pex = new ProtocolViolationException(info);
			if (ex != null)
			{
				FieldInfo f = pex.GetType().GetField("_innerException",
					BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.NonPublic);
				if (f != null) f.SetValue(pex, ex);
			}

			this.OnFailure(pex, messages);
		}
        public override void Write(byte[] buffer, int offset, int count)
        {
            #if DEBUG
            if (HttpTraceHelper.Api.TraceVerbose)
            {
                HttpTraceHelper.WriteLine("ListenerResponseStream#" + HttpTraceHelper.HashString(this) + "::Write() count:" + count.ToString());
            }
            #endif
            if (m_ContentLength != -1 && m_SentContentLength + count > m_ContentLength)
            {
                Exception exception = new ProtocolViolationException();
            #if DEBUG
                if (HttpTraceHelper.ExceptionThrown.TraceVerbose)
                {
                    HttpTraceHelper.WriteLine("ListenerResponseStream#" + HttpTraceHelper.HashString(this) + "::Write() throwing: " + exception.ToString());
                }
            #endif
                throw exception;
            }

            Socket checkSocket;
            if (!m_HttpListenerWebResponse.SentHeaders)
            {
                //
                // we didn't send the headers yet, do so now.
                //
                // we null out the Socket when we cleanup
                // make a local copy to avoid null reference exceptions
                checkSocket = m_HttpListenerWebResponse.Request.ConnectionState.ConnectionSocket;
                if (checkSocket != null)
                {
            #if DEBUG
                    if (HttpTraceHelper.Socket.TraceVerbose)
                    {
                        HttpTraceHelper.WriteLine("ListenerResponseStream#" + HttpTraceHelper.HashString(this) + "::GetResponseStream() calling Socket.Send() Length:" + m_HttpListenerWebResponse.HeadersBuffer.Length.ToString());
                    }
            #endif
                    checkSocket.Send(m_HttpListenerWebResponse.HeadersBuffer);
                    m_HttpListenerWebResponse.SentHeaders = true;
                }
            }

            int DataToWrite = count;

            if (m_WriteChunked)
            {
                string ChunkHeader = "0x" + Convert.ToString( count, 16 );
                DataToWrite += ChunkHeader.Length + 4;
                byte[] newBuffer = new byte[DataToWrite];

                for (int index=0; index<ChunkHeader.Length; index++)
                {
                    newBuffer[index] = (byte)ChunkHeader[index];
                }

                newBuffer[ChunkHeader.Length] = 0x0D;
                newBuffer[ChunkHeader.Length + 1] = 0x0A;

                Buffer.BlockCopy(buffer, offset, newBuffer, ChunkHeader.Length + 2, count);

                newBuffer[DataToWrite - 2] = 0x0D;
                newBuffer[DataToWrite - 1] = 0x0A;

                buffer = newBuffer;
                offset = 0;
            }
            #if DEBUG
            if (HttpTraceHelper.Socket.TraceVerbose)
            {
                HttpTraceHelper.WriteLine("ListenerResponseStream#" + HttpTraceHelper.HashString(this) + "::GetResponseStream() calling Socket.Send() Length:" + DataToWrite.ToString());
            }
            #endif
            // we null out the Socket when we cleanup
            // make a local copy to avoid null reference exceptions
            checkSocket = m_HttpListenerWebResponse.Request.ConnectionState.ConnectionSocket;
            if (checkSocket != null)
            {
                checkSocket.Send(buffer, offset, DataToWrite, SocketFlags.None);
            }

            if (m_ContentLength != -1)
            {
                //
                // keep track of the data transferred
                //
                m_SentContentLength -= count;
            }
        }
Beispiel #15
0
        bool Redirect(HttpStatusCode code)
        {
            _redirects++;
            Exception e = null;
            string uriString = null;
            switch (code)
            {
                case HttpStatusCode.Ambiguous: // 300
                    e = new WebException("Ambiguous redirect.");
                    break;
                case HttpStatusCode.MovedPermanently: // 301
                case HttpStatusCode.Redirect: // 302
                    if (_method == "POST")
                        _method = "GET";
                    break;
                case HttpStatusCode.TemporaryRedirect: // 307
                    break;
                case HttpStatusCode.SeeOther: //303
                    _method = "GET";
                    break;
                case HttpStatusCode.NotModified: // 304
                    return false;
                case HttpStatusCode.UseProxy: // 305
                    e = new NotImplementedException("Proxy support not available.");
                    break;
                case HttpStatusCode.Unused: // 306
                default:
                    e = new ProtocolViolationException("Invalid status code: " + (int)code);
                    break;
            }

            if (e != null)
                throw e;

            _contentLength = -1;
            //bodyBufferLength = 0;
            //bodyBuffer = null;
            uriString = _webResponse.Headers["Location"];

            if (uriString == null)
            {
                throw new WebException("No Location header found for " + (int)code,
                    WebExceptionStatus.ProtocolError);
            }

            var prev = Address;
            try
            {
                Address = new Uri(Address, uriString);
            }
            catch (Exception)
            {
                throw new WebException(String.Format("Invalid URL ({0}) for {1}",
                    uriString, (int)code),
                    WebExceptionStatus.ProtocolError);
            }

            _hostChanged = (Address.Scheme != prev.Scheme || Host != prev.Authority);

            return true;
        }