internal HttpListenerResponse() {
     if(Logging.On)Logging.PrintInfo(Logging.HttpListener, this, ".ctor", "");
     m_NativeResponse = new UnsafeNclNativeMethods.HttpApi.HTTP_RESPONSE();
     m_WebHeaders = new WebHeaderCollection(WebHeaderCollectionType.HttpListenerResponse);
     m_BoundaryType = BoundaryType.None;
     m_NativeResponse.StatusCode = (ushort)HttpStatusCode.OK;
     m_NativeResponse.Version.MajorVersion = 1;
     m_NativeResponse.Version.MinorVersion = 1;
     m_KeepAlive = true;
     m_ResponseState = ResponseState.Created;
 }
 private unsafe void SendError(ulong requestId, HttpStatusCode httpStatusCode, ArrayList challenges)
 {
     uint num2;
     byte[] buffer4;
     UnsafeNclNativeMethods.HttpApi.HTTP_RESPONSE pHttpResponse = new UnsafeNclNativeMethods.HttpApi.HTTP_RESPONSE {
         Version = new UnsafeNclNativeMethods.HttpApi.HTTP_VERSION()
     };
     pHttpResponse.Version.MajorVersion = 1;
     pHttpResponse.Version.MinorVersion = 1;
     pHttpResponse.StatusCode = (ushort) httpStatusCode;
     string statusDescription = HttpListenerResponse.GetStatusDescription((int) httpStatusCode);
     uint pBytesSent = 0;
     byte[] bytes = Encoding.Default.GetBytes(statusDescription);
     if (((buffer4 = bytes) == null) || (buffer4.Length == 0))
     {
         numRef = null;
         goto Label_006B;
     }
     fixed (byte* numRef = buffer4)
     {
         byte[] buffer5;
     Label_006B:
         pHttpResponse.pReason = (sbyte*) numRef;
         pHttpResponse.ReasonLength = (ushort) bytes.Length;
         byte[] buffer2 = Encoding.Default.GetBytes("0");
         if (((buffer5 = buffer2) == null) || (buffer5.Length == 0))
         {
             numRef2 = null;
             goto Label_00AF;
         }
         fixed (byte* numRef2 = buffer5)
         {
         Label_00AF:
             &pHttpResponse.Headers.KnownHeaders[11].pRawValue = (sbyte*) numRef2;
             &pHttpResponse.Headers.KnownHeaders[11].RawValueLength = (ushort) buffer2.Length;
             pHttpResponse.Headers.UnknownHeaderCount = (challenges == null) ? ((ushort) 0) : ((ushort) challenges.Count);
             GCHandle[] handleArray = null;
             UnsafeNclNativeMethods.HttpApi.HTTP_UNKNOWN_HEADER[] http_unknown_headerArray = null;
             GCHandle handle = new GCHandle();
             GCHandle handle2 = new GCHandle();
             if (pHttpResponse.Headers.UnknownHeaderCount > 0)
             {
                 handleArray = new GCHandle[pHttpResponse.Headers.UnknownHeaderCount];
                 http_unknown_headerArray = new UnsafeNclNativeMethods.HttpApi.HTTP_UNKNOWN_HEADER[pHttpResponse.Headers.UnknownHeaderCount];
             }
             try
             {
                 if (pHttpResponse.Headers.UnknownHeaderCount > 0)
                 {
                     handle = GCHandle.Alloc(http_unknown_headerArray, GCHandleType.Pinned);
                     pHttpResponse.Headers.pUnknownHeaders = (UnsafeNclNativeMethods.HttpApi.HTTP_UNKNOWN_HEADER*) Marshal.UnsafeAddrOfPinnedArrayElement(http_unknown_headerArray, 0);
                     handle2 = GCHandle.Alloc(s_WwwAuthenticateBytes, GCHandleType.Pinned);
                     sbyte* numPtr = (sbyte*) Marshal.UnsafeAddrOfPinnedArrayElement(s_WwwAuthenticateBytes, 0);
                     for (int i = 0; i < handleArray.Length; i++)
                     {
                         byte[] buffer3 = Encoding.Default.GetBytes((string) challenges[i]);
                         handleArray[i] = GCHandle.Alloc(buffer3, GCHandleType.Pinned);
                         http_unknown_headerArray[i].pName = numPtr;
                         http_unknown_headerArray[i].NameLength = (ushort) s_WwwAuthenticateBytes.Length;
                         http_unknown_headerArray[i].pRawValue = (sbyte*) Marshal.UnsafeAddrOfPinnedArrayElement(buffer3, 0);
                         http_unknown_headerArray[i].RawValueLength = (ushort) buffer3.Length;
                     }
                 }
                 num2 = UnsafeNclNativeMethods.HttpApi.HttpSendHttpResponse(this.m_RequestQueueHandle, requestId, 0, &pHttpResponse, null, &pBytesSent, SafeLocalFree.Zero, 0, null, null);
             }
             finally
             {
                 if (handle.IsAllocated)
                 {
                     handle.Free();
                 }
                 if (handle2.IsAllocated)
                 {
                     handle2.Free();
                 }
                 if (handleArray != null)
                 {
                     for (int j = 0; j < handleArray.Length; j++)
                     {
                         if (handleArray[j].IsAllocated)
                         {
                             handleArray[j].Free();
                         }
                     }
                 }
             }
         }
     }
     if (num2 != 0)
     {
         HttpListenerContext.CancelRequest(this.m_RequestQueueHandle, requestId);
     }
 }
 public void CopyFrom(HttpListenerResponse templateResponse) {
     if(Logging.On)Logging.PrintInfo(Logging.HttpListener, this, "CopyFrom", "templateResponse#"+ValidationHelper.HashString(templateResponse));
     m_NativeResponse = new UnsafeNclNativeMethods.HttpApi.HTTP_RESPONSE();
     m_ResponseState = ResponseState.Created;
     m_WebHeaders = templateResponse.m_WebHeaders;
     m_BoundaryType = templateResponse.m_BoundaryType;
     m_ContentLength = templateResponse.m_ContentLength;
     m_NativeResponse.StatusCode = templateResponse.m_NativeResponse.StatusCode;
     m_NativeResponse.Version.MajorVersion = templateResponse.m_NativeResponse.Version.MajorVersion;
     m_NativeResponse.Version.MinorVersion = templateResponse.m_NativeResponse.Version.MinorVersion;
     m_StatusDescription = templateResponse.m_StatusDescription;
     m_KeepAlive = templateResponse.m_KeepAlive;
 }
        private void SendError(ulong requestId, HttpStatusCode httpStatusCode, ArrayList challenges)
        {
            GlobalLog.Print("HttpListener#" + ValidationHelper.HashString(this) + "::SendInternalError() requestId:" + ValidationHelper.ToString(requestId));
            UnsafeNclNativeMethods.HttpApi.HTTP_RESPONSE httpResponse = new UnsafeNclNativeMethods.HttpApi.HTTP_RESPONSE();
            httpResponse.Version = new UnsafeNclNativeMethods.HttpApi.HTTP_VERSION();
            httpResponse.Version.MajorVersion = (ushort)1;
            httpResponse.Version.MinorVersion = (ushort)1;
            httpResponse.StatusCode = (ushort)httpStatusCode;
            string statusDescription = HttpStatusDescription.Get(httpStatusCode);
            uint DataWritten = 0;
            uint statusCode;
            byte[] byteReason = Encoding.Default.GetBytes(statusDescription);
            fixed (byte* pReason = byteReason)
            {
                httpResponse.pReason = (sbyte*)pReason;
                httpResponse.ReasonLength = (ushort)byteReason.Length;

                byte[] byteContentLength = Encoding.Default.GetBytes("0");
                fixed (byte* pContentLength = byteContentLength)
                {
                    (&httpResponse.Headers.KnownHeaders)[(int)HttpResponseHeader.ContentLength].pRawValue = (sbyte*)pContentLength;
                    (&httpResponse.Headers.KnownHeaders)[(int)HttpResponseHeader.ContentLength].RawValueLength = (ushort)byteContentLength.Length;

                    httpResponse.Headers.UnknownHeaderCount = checked((ushort) (challenges == null ? 0 : challenges.Count));
                    GCHandle[] challengeHandles = null;
                    UnsafeNclNativeMethods.HttpApi.HTTP_UNKNOWN_HEADER[] headersArray = null;
                    GCHandle headersArrayHandle = new GCHandle();
                    GCHandle wwwAuthenticateHandle = new GCHandle();
                    if (httpResponse.Headers.UnknownHeaderCount > 0)
                    {
                        challengeHandles = new GCHandle[httpResponse.Headers.UnknownHeaderCount];
                        headersArray = new UnsafeNclNativeMethods.HttpApi.HTTP_UNKNOWN_HEADER[httpResponse.Headers.UnknownHeaderCount];
                    }

                    try
                    {
                        if (httpResponse.Headers.UnknownHeaderCount > 0)
                        {
                            headersArrayHandle = GCHandle.Alloc(headersArray, GCHandleType.Pinned);
                            httpResponse.Headers.pUnknownHeaders = (UnsafeNclNativeMethods.HttpApi.HTTP_UNKNOWN_HEADER*) Marshal.UnsafeAddrOfPinnedArrayElement(headersArray, 0);
                            wwwAuthenticateHandle = GCHandle.Alloc(s_WwwAuthenticateBytes, GCHandleType.Pinned);
                            sbyte* wwwAuthenticate = (sbyte*) Marshal.UnsafeAddrOfPinnedArrayElement(s_WwwAuthenticateBytes, 0);

                            for (int i = 0; i < challengeHandles.Length; i++)
                            {
                                byte[] byteChallenge = Encoding.Default.GetBytes((string) challenges[i]);
                                challengeHandles[i] = GCHandle.Alloc(byteChallenge, GCHandleType.Pinned);
                                headersArray[i].pName = wwwAuthenticate;
                                headersArray[i].NameLength = (ushort) s_WwwAuthenticateBytes.Length;
                                headersArray[i].pRawValue = (sbyte*) Marshal.UnsafeAddrOfPinnedArrayElement(byteChallenge, 0);
                                headersArray[i].RawValueLength = checked((ushort) byteChallenge.Length);
                            }
                        }

                        GlobalLog.Print("HttpListener#" + ValidationHelper.HashString(this) + "::SendInternalError() calling UnsafeNclNativeMethods.HttpApi.HttpSendHtthttpResponse");
                        statusCode =
                            UnsafeNclNativeMethods.HttpApi.HttpSendHttpResponse(
                                m_RequestQueueHandle,
                                requestId,
                                0,
                                &httpResponse,
                                null,
                                &DataWritten,
                                SafeLocalFree.Zero,
                                0,
                                null,
                                null );
                    }
                    finally
                    {
                        if (headersArrayHandle.IsAllocated)
                        {
                            headersArrayHandle.Free();
                        }
                        if (wwwAuthenticateHandle.IsAllocated)
                        {
                            wwwAuthenticateHandle.Free();
                        }
                        if (challengeHandles != null)
                        {
                            for (int i = 0; i < challengeHandles.Length; i++)
                            {
                                if (challengeHandles[i].IsAllocated)
                                {
                                    challengeHandles[i].Free();
                                }
                            }
                        }
                    }
                }
            }
            GlobalLog.Print("HttpListener#" + ValidationHelper.HashString(this) + "::SendInternalError() call to UnsafeNclNativeMethods.HttpApi.HttpSendHttpResponse returned:" + statusCode);
            if (statusCode!=UnsafeNclNativeMethods.ErrorCodes.ERROR_SUCCESS) {
                // if we fail to send a 401 something's seriously wrong, abort the request
                GlobalLog.Print("HttpListener#" + ValidationHelper.HashString(this) + "::HandleAuthentication() SendUnauthorized() returned:" + statusCode);
                HttpListenerContext.CancelRequest(m_RequestQueueHandle, requestId);
            }
        }