InitRead() static private méthode

static private InitRead ( object state ) : void
state object
Résultat void
Exemple #1
0
        void WriteAsyncCB(IAsyncResult r)
        {
            WebAsyncResult result = (WebAsyncResult)r.AsyncState;

            result.InnerAsyncResult = null;

            try {
                cnc.EndWrite(request, true, r);
                result.SetCompleted(false, 0);
                if (!initRead)
                {
                    initRead = true;
                    cnc.InitRead();
                }
            } catch (Exception e) {
                KillBuffer();
                nextReadCalled = true;
                cnc.Close(true);
                if (e is System.Net.Sockets.SocketException)
                {
                    e = new IOException("Error writing request", e);
                }
                result.SetCompleted(false, e);
            }

            if (allowBuffering && !sendChunked && request.ContentLength > 0 && totalWritten == request.ContentLength)
            {
                complete_request_written = true;
            }

            result.DoCallback();
        }
Exemple #2
0
        public override void Close()
        {
            if (sendChunked)
            {
                if (disposed)
                {
                    return;
                }
                disposed = true;
                pending.WaitOne();
                byte [] chunk   = Encoding.ASCII.GetBytes("0\r\n\r\n");
                string  err_msg = null;
                cnc.Write(request, chunk, 0, chunk.Length, ref err_msg);
                return;
            }

            if (isRead)
            {
                if (!nextReadCalled)
                {
                    CheckComplete();
                    // If we have not read all the contents
                    if (!nextReadCalled)
                    {
                        nextReadCalled = true;
                        cnc.Close(true);
                    }
                }
                return;
            }
            else if (!allowBuffering)
            {
                complete_request_written = true;
                if (!initRead)
                {
                    initRead = true;
                    WebConnection.InitRead(cnc);
                }
                return;
            }

            if (disposed || requestWritten)
            {
                return;
            }

            long length = request.ContentLength;

            if (!sendChunked && length != -1 && totalWritten != length)
            {
                IOException io = new IOException("Cannot close the stream until all bytes are written");
                nextReadCalled = true;
                cnc.Close(true);
                throw new WebException("Request was cancelled.", io, WebExceptionStatus.RequestCanceled);
            }

            // Commented out the next line to fix xamarin bug #1512
            //WriteRequest ();
            disposed = true;
        }
        private void WriteRequestAsyncCB(IAsyncResult r)
        {
            WebAsyncResult webAsyncResult = (WebAsyncResult)r.AsyncState;

            try
            {
                this.cnc.EndWrite2(this.request, r);
                webAsyncResult.SetCompleted(false, 0);
                if (!this.initRead)
                {
                    this.initRead = true;
                    WebConnection.InitRead(this.cnc);
                }
            }
            catch (Exception ex)
            {
                this.KillBuffer();
                this.nextReadCalled = true;
                this.cnc.Close(true);
                if (ex is System.Net.Sockets.SocketException)
                {
                    ex = new IOException("Error writing request", ex);
                }
                webAsyncResult.SetCompleted(false, ex);
            }
            this.complete_request_written = true;
            webAsyncResult.DoCallback();
        }
        private void WriteRequestAsyncCB(IAsyncResult r)
        {
            WebAsyncResult webAsyncResult = (WebAsyncResult)r.AsyncState;

            try
            {
                cnc.EndWrite2(request, r);
                webAsyncResult.SetCompleted(synch: false, 0);
                if (!initRead)
                {
                    initRead = true;
                    WebConnection.InitRead(cnc);
                }
            }
            catch (Exception ex)
            {
                Exception ex2 = ex;
                KillBuffer();
                nextReadCalled = true;
                cnc.Close(sendNext: true);
                if (ex2 is SocketException)
                {
                    ex2 = new IOException("Error writing request", ex2);
                }
                webAsyncResult.SetCompleted(synch: false, ex2);
            }
            complete_request_written = true;
            webAsyncResult.DoCallback();
        }
Exemple #5
0
        internal void SetHeaders(byte [] buffer, int offset, int size)
        {
            if (headersSent)
            {
                return;
            }

            if (!allowBuffering || sendChunked)
            {
                headersSent = true;
                if (!cnc.Connected)
                {
                    throw new WebException("Not connected", null, WebExceptionStatus.SendFailure, null);
                }


                if (!cnc.Write(buffer, offset, size))
                {
                    throw new WebException("Error writing request.", null, WebExceptionStatus.SendFailure, null);
                }

                if (!initRead)
                {
                    initRead = true;
                    WebConnection.InitRead(cnc);
                }
            }
            else
            {
                headers = new byte [size];
                Buffer.BlockCopy(buffer, offset, headers, 0, size);
            }
        }
        internal void SetHeaders(byte[] buffer)
        {
            if (this.headersSent)
            {
                return;
            }
            this.headers = buffer;
            long   num    = this.request.ContentLength;
            string method = this.request.Method;
            bool   flag   = method == "GET" || method == "CONNECT" || method == "HEAD" || method == "TRACE" || method == "DELETE";

            if (this.sendChunked || num > -1L || flag)
            {
                this.WriteHeaders();
                if (!this.initRead)
                {
                    this.initRead = true;
                    WebConnection.InitRead(this.cnc);
                }
                if (!this.sendChunked && num == 0L)
                {
                    this.requestWritten = true;
                }
            }
        }
Exemple #7
0
        internal void SetHeaders(byte [] buffer)
        {
            if (headersSent)
            {
                return;
            }

            headers = buffer;
            long   cl             = request.ContentLength;
            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 (sendChunked || cl > -1 || no_writestream || webdav)
            {
                WriteHeaders();
                if (!initRead)
                {
                    initRead = true;
                    WebConnection.InitRead(cnc);
                }
                if (!sendChunked && cl == 0)
                {
                    requestWritten = true;
                }
            }
        }
Exemple #8
0
        void WriteRequestAsyncCB(IAsyncResult r)
        {
            WebAsyncResult result = (WebAsyncResult)r.AsyncState;

            try {
                cnc.EndWrite2(request, r);
                result.SetCompleted(false, 0);
                if (!initRead)
                {
                    initRead = true;
                    WebConnection.InitRead(cnc);
                }
            } catch (Exception e) {
                KillBuffer();
                nextReadCalled = true;
                cnc.Close(true);
                if (e is System.Net.Sockets.SocketException)
                {
                    e = new IOException("Error writing request", e);
                }
                result.SetCompleted(false, e);
            }
            complete_request_written = true;
            result.DoCallback();
        }
Exemple #9
0
        internal void SetHeaders(byte [] buffer)
        {
            if (headersSent)
            {
                return;
            }

            headers = buffer;
            long   cl             = request.ContentLength;
            string method         = request.Method;
            bool   no_writestream = (method == "GET" || method == "CONNECT" || method == "HEAD" ||
                                     method == "TRACE" || method == "DELETE");

            if (sendChunked || cl > -1 || no_writestream)
            {
                WriteHeaders();
                if (!initRead)
                {
                    initRead = true;
                    WebConnection.InitRead(cnc);
                }
                if (!sendChunked && cl == 0)
                {
                    requestWritten = true;
                }
            }
        }
Exemple #10
0
        void SetHeadersCB(IAsyncResult r)
        {
            WebAsyncResult result = (WebAsyncResult)r.AsyncState;

            result.InnerAsyncResult = null;
            try {
                cnc.EndWrite(request, true, r);
                result.SetCompleted(false, 0);
                if (!initRead)
                {
                    initRead = true;
                    WebConnection.InitRead(cnc);
                }
                long cl = request.ContentLength;
                if (!sendChunked && cl == 0)
                {
                    requestWritten = true;
                }
            } catch (WebException e) {
                result.SetCompleted(false, e);
            } catch (Exception e) {
                result.SetCompleted(false, new WebException("Error writing headers", e, WebExceptionStatus.SendFailure));
            }
            result.DoCallback();
        }
        internal void WriteRequest()
        {
            if (this.requestWritten)
            {
                return;
            }
            this.requestWritten = true;
            if (this.sendChunked)
            {
                return;
            }
            if (!this.allowBuffering || this.writeBuffer == null)
            {
                return;
            }
            byte[] buffer = this.writeBuffer.GetBuffer();
            int    num    = (int)this.writeBuffer.Length;

            if (this.request.ContentLength != -1L && this.request.ContentLength < (long)num)
            {
                this.nextReadCalled = true;
                this.cnc.Close(true);
                throw new WebException("Specified Content-Length is less than the number of bytes to write", null, WebExceptionStatus.ServerProtocolViolation, null);
            }
            if (!this.headersSent)
            {
                string method = this.request.Method;
                if (!(method == "GET") && !(method == "CONNECT") && !(method == "HEAD") && !(method == "TRACE") && !(method == "DELETE"))
                {
                    this.request.InternalContentLength = (long)num;
                }
                this.request.SendRequestHeaders(true);
            }
            this.WriteHeaders();
            if (this.cnc.Data.StatusCode != 0 && this.cnc.Data.StatusCode != 100)
            {
                return;
            }
            IAsyncResult result = null;

            if (num > 0)
            {
                result = this.cnc.BeginWrite(this.request, buffer, 0, num, null, null);
            }
            if (!this.initRead)
            {
                this.initRead = true;
                WebConnection.InitRead(this.cnc);
            }
            if (num > 0)
            {
                this.complete_request_written = this.cnc.EndWrite(this.request, result);
            }
            else
            {
                this.complete_request_written = true;
            }
        }
 public override void Close()
 {
     if (this.sendChunked)
     {
         if (this.disposed)
         {
             return;
         }
         this.disposed = true;
         this.pending.WaitOne();
         byte[] bytes = Encoding.ASCII.GetBytes("0\r\n\r\n");
         string text  = null;
         this.cnc.Write(this.request, bytes, 0, bytes.Length, ref text);
         return;
     }
     else
     {
         if (this.isRead)
         {
             if (!this.nextReadCalled)
             {
                 this.CheckComplete();
                 if (!this.nextReadCalled)
                 {
                     this.nextReadCalled = true;
                     this.cnc.Close(true);
                 }
             }
             return;
         }
         if (!this.allowBuffering)
         {
             this.complete_request_written = true;
             if (!this.initRead)
             {
                 this.initRead = true;
                 WebConnection.InitRead(this.cnc);
             }
             return;
         }
         if (this.disposed || this.requestWritten)
         {
             return;
         }
         long num = this.request.ContentLength;
         if (!this.sendChunked && num != -1L && this.totalWritten != num)
         {
             IOException innerException = new IOException("Cannot close the stream until all bytes are written");
             this.nextReadCalled = true;
             this.cnc.Close(true);
             throw new WebException("Request was cancelled.", innerException, WebExceptionStatus.RequestCanceled);
         }
         this.WriteRequest();
         this.disposed = true;
         return;
     }
 }
Exemple #13
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", e, WebExceptionStatus.SendFailure));
                }
            }, null);

            return(innerResult != null);
        }
Exemple #14
0
        public override void Close()
        {
            if (sendChunked)
            {
                pending.WaitOne();
                byte [] chunk = Encoding.ASCII.GetBytes("0\r\n\r\n");
                cnc.Write(chunk, 0, chunk.Length);
                return;
            }

            if (isRead)
            {
                if (!nextReadCalled)
                {
                    CheckComplete();
                    // If we have not read all the contents
                    if (!nextReadCalled)
                    {
                        nextReadCalled = true;
                        cnc.Close(true);
                    }
                }
                return;
            }
            else if (!allowBuffering)
            {
                complete_request_written = true;
                if (!initRead)
                {
                    initRead = true;
                    WebConnection.InitRead(cnc);
                }
                return;
            }

            if (disposed)
            {
                return;
            }

            long length = request.ContentLength;

            if (length != -1 && length > writeBuffer.Length)
            {
                throw new IOException("Cannot close the stream until all bytes are written");
            }

            WriteRequest();
            disposed = true;
        }
 public override void Close()
 {
     if (sendChunked)
     {
         if (!disposed)
         {
             disposed = true;
             pending.WaitOne();
             byte[] bytes   = Encoding.ASCII.GetBytes("0\r\n\r\n");
             string err_msg = null;
             cnc.Write(request, bytes, 0, bytes.Length, ref err_msg);
         }
     }
     else if (isRead)
     {
         if (!nextReadCalled)
         {
             CheckComplete();
             if (!nextReadCalled)
             {
                 nextReadCalled = true;
                 cnc.Close(sendNext: true);
             }
         }
     }
     else if (!allowBuffering)
     {
         complete_request_written = true;
         if (!initRead)
         {
             initRead = true;
             WebConnection.InitRead(cnc);
         }
     }
     else if (!disposed && !requestWritten)
     {
         long num = request.ContentLength;
         if (!sendChunked && num != -1 && totalWritten != num)
         {
             IOException innerException = new IOException("Cannot close the stream until all bytes are written");
             nextReadCalled = true;
             cnc.Close(sendNext: true);
             throw new WebException("Request was cancelled.", innerException, WebExceptionStatus.RequestCanceled);
         }
         WriteRequest();
         disposed = true;
     }
 }
        void WriteRequestAsyncCB(IAsyncResult ar)
        {
            var result      = (WebAsyncResult)ar;
            var innerResult = (WebAsyncResult)result.InnerAsyncResult;

            result.InnerAsyncResult = null;

            if (innerResult != null && innerResult.GotException)
            {
                result.SetCompleted(false, innerResult.Exception);
                result.DoCallback();
                return;
            }

            if (cnc.Data.StatusCode != 0 && cnc.Data.StatusCode != 100)
            {
                result.SetCompleted(false, 0);
                result.DoCallback();
                return;
            }

            byte[] bytes  = writeBuffer.GetBuffer();
            int    length = (int)writeBuffer.Length;

            if (length > 0)
            {
                result.InnerAsyncResult = cnc.BeginWrite(request, bytes, 0, length, WriteRequestAsyncCB2, result);
            }

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

            if (length == 0)
            {
                result.SetCompleted(false, 0);
                result.DoCallback();
                complete_request_written = true;
            }
        }
        internal void SetHeaders(byte[] buffer)
        {
            if (headersSent)
            {
                return;
            }
            headers = buffer;
            long   num    = request.ContentLength;
            string method = request.Method;
            int    num2;

            switch (method)
            {
            default:
                num2 = ((method == "DELETE") ? 1 : 0);
                break;

            case "GET":
            case "CONNECT":
            case "HEAD":
            case "TRACE":
                num2 = 1;
                break;
            }
            bool flag = (byte)num2 != 0;

            if (sendChunked || num > -1 || flag)
            {
                WriteHeaders();
                if (!initRead)
                {
                    initRead = true;
                    WebConnection.InitRead(cnc);
                }
                if (!sendChunked && num == 0L)
                {
                    requestWritten = true;
                }
            }
        }
Exemple #18
0
        internal void WriteRequest()
        {
            if (requestWritten)
            {
                return;
            }

            requestWritten = true;
            if (sendChunked)
            {
                return;
            }

            if (!allowBuffering || writeBuffer == null)
            {
                return;
            }

            byte [] bytes  = writeBuffer.GetBuffer();
            int     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);
            }

            if (!headersSent)
            {
                string method         = request.Method;
                bool   no_writestream = (method == "GET" || method == "CONNECT" || method == "HEAD" ||
                                         method == "TRACE");
                if (!no_writestream)
                {
                    request.InternalContentLength = length;
                }

                byte[]         requestHeaders = request.GetRequestHeaders();
                WebAsyncResult ar             = new WebAsyncResult(null, null);
                SetHeadersAsync(requestHeaders, ar);
                ar.AsyncWaitHandle.WaitOne();
                if (ar.Exception != null)
                {
                    throw ar.Exception;
                }
            }

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

            IAsyncResult result = null;

            if (length > 0)
            {
                result = cnc.BeginWrite(request, bytes, 0, length, null, null);
            }

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

            if (length > 0)
            {
                complete_request_written = cnc.EndWrite(request, false, result);
            }
            else
            {
                complete_request_written = true;
            }
        }
Exemple #19
0
        internal void WriteRequest()
        {
            if (requestWritten)
            {
                return;
            }

            if (sendChunked)
            {
                request.SendRequestHeaders();
                requestWritten = true;
                return;
            }

            if (!allowBuffering || writeBuffer == null)
            {
                return;
            }

            byte [] bytes  = writeBuffer.GetBuffer();
            int     length = (int)writeBuffer.Length;

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

            request.InternalContentLength = length;
            request.SendRequestHeaders();
            requestWritten = true;
            if (!cnc.Write(headers, 0, headers.Length))
            {
                throw new WebException("Error writing request.", null, WebExceptionStatus.SendFailure, null);
            }

            headersSent = true;
            if (cnc.Data.StatusCode != 0 && cnc.Data.StatusCode != 100)
            {
                return;
            }

            IAsyncResult result = null;

            if (length > 0)
            {
                result = cnc.BeginWrite(bytes, 0, length, null, null);
            }

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

            if (length > 0)
            {
                complete_request_written = cnc.EndWrite(result);
            }
            else
            {
                complete_request_written = true;
            }
        }
        private static void ReadDone(IAsyncResult result)
        {
            WebConnection     webConnection = (WebConnection)result.AsyncState;
            WebConnectionData data          = webConnection.Data;
            Stream            stream        = webConnection.nstream;

            if (stream == null)
            {
                webConnection.Close(true);
                return;
            }
            int num = -1;

            try
            {
                num = stream.EndRead(result);
            }
            catch (Exception e)
            {
                webConnection.HandleError(WebExceptionStatus.ReceiveFailure, e, "ReadDone1");
                return;
            }
            if (num == 0)
            {
                webConnection.HandleError(WebExceptionStatus.ReceiveFailure, null, "ReadDone2");
                return;
            }
            if (num < 0)
            {
                webConnection.HandleError(WebExceptionStatus.ServerProtocolViolation, null, "ReadDone3");
                return;
            }
            int num2 = -1;

            num += webConnection.position;
            if (webConnection.readState == ReadState.None)
            {
                Exception ex = null;
                try
                {
                    num2 = webConnection.GetResponse(webConnection.buffer, num);
                }
                catch (Exception ex2)
                {
                    ex = ex2;
                }
                if (ex != null)
                {
                    webConnection.HandleError(WebExceptionStatus.ServerProtocolViolation, ex, "ReadDone4");
                    return;
                }
            }
            if (webConnection.readState != ReadState.Content)
            {
                int    num3 = num * 2;
                int    num4 = (num3 >= webConnection.buffer.Length) ? num3 : webConnection.buffer.Length;
                byte[] dst  = new byte[num4];
                Buffer.BlockCopy(webConnection.buffer, 0, dst, 0, num);
                webConnection.buffer    = dst;
                webConnection.position  = num;
                webConnection.readState = ReadState.None;
                WebConnection.InitRead(webConnection);
                return;
            }
            webConnection.position = 0;
            WebConnectionStream webConnectionStream = new WebConnectionStream(webConnection);
            string text = data.Headers["Transfer-Encoding"];

            webConnection.chunkedRead = (text != null && text.ToLower().IndexOf("chunked") != -1);
            if (!webConnection.chunkedRead)
            {
                webConnectionStream.ReadBuffer       = webConnection.buffer;
                webConnectionStream.ReadBufferOffset = num2;
                webConnectionStream.ReadBufferSize   = num;
                webConnectionStream.CheckResponseInBuffer();
            }
            else if (webConnection.chunkStream == null)
            {
                try
                {
                    webConnection.chunkStream = new ChunkStream(webConnection.buffer, num2, num, data.Headers);
                }
                catch (Exception e2)
                {
                    webConnection.HandleError(WebExceptionStatus.ServerProtocolViolation, e2, "ReadDone5");
                    return;
                }
            }
            else
            {
                webConnection.chunkStream.ResetBuffer();
                try
                {
                    webConnection.chunkStream.Write(webConnection.buffer, num2, num);
                }
                catch (Exception e3)
                {
                    webConnection.HandleError(WebExceptionStatus.ServerProtocolViolation, e3, "ReadDone6");
                    return;
                }
            }
            data.stream = webConnectionStream;
            if (!WebConnection.ExpectContent(data.StatusCode) || data.request.Method == "HEAD")
            {
                webConnectionStream.ForceCompletion();
            }
            data.request.SetResponseData(data);
        }
Exemple #21
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);
        }
        internal void WriteRequest()
        {
            if (requestWritten)
            {
                return;
            }
            requestWritten = true;
            if (sendChunked || !allowBuffering || writeBuffer == null)
            {
                return;
            }
            byte[] buffer = writeBuffer.GetBuffer();
            int    num    = (int)writeBuffer.Length;

            if (request.ContentLength != -1 && request.ContentLength < num)
            {
                nextReadCalled = true;
                cnc.Close(sendNext: true);
                throw new WebException("Specified Content-Length is less than the number of bytes to write", null, WebExceptionStatus.ServerProtocolViolation, null);
            }
            if (!headersSent)
            {
                string method = request.Method;
                int    num2;
                switch (method)
                {
                default:
                    num2 = ((method == "DELETE") ? 1 : 0);
                    break;

                case "GET":
                case "CONNECT":
                case "HEAD":
                case "TRACE":
                    num2 = 1;
                    break;
                }
                if (num2 == 0)
                {
                    request.InternalContentLength = num;
                }
                request.SendRequestHeaders(propagate_error: true);
            }
            WriteHeaders();
            if (cnc.Data.StatusCode == 0 || cnc.Data.StatusCode == 100)
            {
                IAsyncResult result = null;
                if (num > 0)
                {
                    result = cnc.BeginWrite(request, buffer, 0, num, null, null);
                }
                if (!initRead)
                {
                    initRead = true;
                    WebConnection.InitRead(cnc);
                }
                if (num > 0)
                {
                    complete_request_written = cnc.EndWrite(request, result);
                }
                else
                {
                    complete_request_written = true;
                }
            }
        }
Exemple #23
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.CompletedSynchronously, inner.Exception);
                    return;
                }

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

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

                if (length == 0)
                {
                    complete_request_written = true;
                    result.SetCompleted(inner.CompletedSynchronously);
                    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);
        }