Example #1
0
        /*++
            WriteHeaders

            This function writes header data to the network. Headers are special
            in that they don't have any non-header transforms applied to them,
            and are not subject to content-length constraints. We just write them
            through, and if we're done writing headers we tell the connection that.

            Input:

                httpWebRequest      - request whose headers we're about to write.
                writeDoneDelegate   - delegate we call to find out if we have a write stream.

            Returns:
                WebExceptionStatus.Pending      - we don't have a stream yet.
                WebExceptionStatus.SendFailure  - there was an error while writing to the wire.
                WebExceptionStatus.Success      - success.

        --*/
        internal virtual WebExceptionStatus WriteHeaders(HttpWebRequest httpWebRequest) {
            GlobalLog.Enter("ConnectStream#" + ValidationHelper.HashString(this) + "::WriteHeaders", "Connection#" + ValidationHelper.HashString(m_Connection) + ", " + httpWebRequest.WriteBuffer.Length.ToString());

            if (ErrorInStream) {
                GlobalLog.Leave("ConnectStream#" + ValidationHelper.HashString(this) + "::WriteHeaders", "ErrorInStream");
                return WebExceptionStatus.SendFailure;
            }

            Interlocked.Increment( ref m_CallNesting );
            GlobalLog.Print("Inc: " + m_CallNesting.ToString());

            try {
                //
                // no need to buffer here:
                // on resubmit, the headers, which may be changed, will be sent from
                // the HttpWebRequest object when calling into this method again.
                //
                m_Connection.Write(httpWebRequest.WriteBuffer, 0, httpWebRequest.WriteBuffer.Length);
            } catch {
                IOError();
                GlobalLog.Leave("ConnectStream#" + ValidationHelper.HashString(this) + "::WriteHeaders", "Exception");
                return WebExceptionStatus.SendFailure;
            }

            Interlocked.Decrement( ref m_CallNesting );
            GlobalLog.Print("Dec: " + m_CallNesting.ToString());

            bool completed = httpWebRequest.EndWriteHeaders();

            if (!completed) {
                // indicates that we're going pending
                GlobalLog.Leave("ConnectStream#" + ValidationHelper.HashString(this) + "::WriteHeaders", "Pending");
                return WebExceptionStatus.Pending;
            }

            if (BytesLeftToWrite == 0) {
                //
                // didn't go pending, no data to write. we're done.
                //
                CallDone();
            }

            GlobalLog.Leave("ConnectStream#" + ValidationHelper.HashString(this) + "::WriteHeaders", httpWebRequest.WriteBuffer.Length);
            return WebExceptionStatus.Success;
        }