Ejemplo n.º 1
0
 internal WinHttpRequestStream(SafeWinHttpHandle requestHandle, bool chunkedMode)
 {
     bool ignore = false;
     requestHandle.DangerousAddRef(ref ignore);
     _requestHandle = requestHandle;
     _chunkedMode = chunkedMode;
 }
Ejemplo n.º 2
0
        internal WinHttpRequestStream(SafeWinHttpHandle requestHandle, bool chunkedMode)
        {
            bool ignore = false;

            requestHandle.DangerousAddRef(ref ignore);
            _requestHandle = requestHandle;
            _chunkedMode   = chunkedMode;
        }
 internal WinHttpResponseStream(
     SafeWinHttpHandle sessionHandle,
     SafeWinHttpHandle connectHandle,
     SafeWinHttpHandle requestHandle)
 {
     // While we only use the requestHandle to do actual reads of the response body,
     // we need to keep the parent handles (connection, session) alive as well.
     bool ignore = false;
     sessionHandle.DangerousAddRef(ref ignore);
     connectHandle.DangerousAddRef(ref ignore);
     requestHandle.DangerousAddRef(ref ignore);
     _sessionHandle = sessionHandle;
     _connectHandle = connectHandle;
     _requestHandle = requestHandle;
 }
Ejemplo n.º 4
0
        internal WinHttpResponseStream(
            SafeWinHttpHandle sessionHandle,
            SafeWinHttpHandle connectHandle,
            SafeWinHttpHandle requestHandle)
        {
            // While we only use the requestHandle to do actual reads of the response body,
            // we need to keep the parent handles (connection, session) alive as well.
            bool ignore = false;

            sessionHandle.DangerousAddRef(ref ignore);
            connectHandle.DangerousAddRef(ref ignore);
            requestHandle.DangerousAddRef(ref ignore);
            _sessionHandle = sessionHandle;
            _connectHandle = connectHandle;
            _requestHandle = requestHandle;
        }
Ejemplo n.º 5
0
        private void EnsureSessionHandleExists(RequestState state)
        {
            bool ignore = false;

            if (_sessionHandle == null)
            {
                lock (_lockObject)
                {
                    if (_sessionHandle == null)
                    {
                        uint accessType;

                        // If a custom proxy is specified and it is really the system web proxy
                        // (initial WebRequest.DefaultWebProxy) then we need to update the settings
                        // since that object is only a sentinel.
                        if (state.WindowsProxyUsePolicy == WindowsProxyUsePolicy.UseCustomProxy)
                        {
                            Debug.Assert(state.Proxy != null);
                            try
                            {
                                state.Proxy.GetProxy(state.RequestMessage.RequestUri);
                            }
                            catch (PlatformNotSupportedException)
                            {
                                // This is the system web proxy.
                                state.WindowsProxyUsePolicy = WindowsProxyUsePolicy.UseWinInetProxy;
                                state.Proxy = null;
                            }
                        }

                        if (state.WindowsProxyUsePolicy == WindowsProxyUsePolicy.DoNotUseProxy ||
                            state.WindowsProxyUsePolicy == WindowsProxyUsePolicy.UseCustomProxy)
                        {
                            // Either no proxy at all or a custom IWebProxy proxy is specified.
                            // For a custom IWebProxy, we'll need to calculate and set the proxy
                            // on a per request handle basis using the request Uri.  For now,
                            // we set the session handle to have no proxy.
                            accessType = Interop.WinHttp.WINHTTP_ACCESS_TYPE_NO_PROXY;
                        }
                        else if (state.WindowsProxyUsePolicy == WindowsProxyUsePolicy.UseWinHttpProxy)
                        {
                            // Use WinHTTP per-machine proxy settings which are set using the "netsh winhttp" command.
                            accessType = Interop.WinHttp.WINHTTP_ACCESS_TYPE_DEFAULT_PROXY;
                        }
                        else
                        {
                            // Use WinInet per-user proxy settings.
                            accessType = Interop.WinHttp.WINHTTP_ACCESS_TYPE_AUTOMATIC_PROXY;
                        }

                        _sessionHandle = Interop.WinHttp.WinHttpOpen(
                            IntPtr.Zero,
                            accessType,
                            Interop.WinHttp.WINHTTP_NO_PROXY_NAME,
                            Interop.WinHttp.WINHTTP_NO_PROXY_BYPASS,
                            0);
                        if (!_sessionHandle.IsInvalid)
                        {
                            _sessionHandle.DangerousAddRef(ref ignore);

                            return;
                        }

                        int lastError = Marshal.GetLastWin32Error();
                        if (lastError != Interop.WinHttp.ERROR_INVALID_PARAMETER)
                        {
                            throw new HttpRequestException(
                                SR.net_http_client_execution_error,
                                WinHttpException.CreateExceptionUsingError(lastError));
                        }

                        // We must be running on a platform earlier than Win8.1/Win2K12R2 which doesn't support
                        // WINHTTP_ACCESS_TYPE_AUTOMATIC_PROXY.  So, we'll need to read the Wininet style proxy
                        // settings ourself using our WinInetProxyHelper object.
                        _proxyHelper = new WinInetProxyHelper();
                        _sessionHandle = Interop.WinHttp.WinHttpOpen(
                            IntPtr.Zero,
                            _proxyHelper.ManualSettingsOnly ? Interop.WinHttp.WINHTTP_ACCESS_TYPE_NAMED_PROXY : Interop.WinHttp.WINHTTP_ACCESS_TYPE_NO_PROXY,
                            _proxyHelper.ManualSettingsOnly ? _proxyHelper.Proxy : Interop.WinHttp.WINHTTP_NO_PROXY_NAME,
                            _proxyHelper.ManualSettingsOnly ? _proxyHelper.ProxyBypass : Interop.WinHttp.WINHTTP_NO_PROXY_BYPASS,
                            0);
                        if (_sessionHandle.IsInvalid)
                        {
                            throw new HttpRequestException(
                                SR.net_http_client_execution_error,
                                WinHttpException.CreateExceptionUsingLastError());
                        }

                        _sessionHandle.DangerousAddRef(ref ignore);
                    }
                }
            }
        }