/// <summary> /// Download the requested bytes /// </summary> private HttpWebRequest CreateHttpWebRequest(int[,] byteRanges) { HttpWebRequest request; // Create the request object request = (HttpWebRequest)WpfWebRequestHelper.CreateRequest(_requestedUri); request.ProtocolVersion = HttpVersion.Version11; request.Method = "GET"; // Set the Proxy to Empty one; If we don't set this to empty one, it will try to find one for us // and ends up triggering JScript in another assembly. This will throw PolicyException since the JScript // dll doesn't have execution right. This is a bug in CLR; supposed to be fixed later // Future work: Need to keep consistent HTTP stack with the WININET one (e.g. authentication, proxy, cookies) // IWebProxy emptyProxy = GlobalProxySelection.GetEmptyWebProxy(); // request.Proxy = emptyProxy; request.Proxy = _proxy; request.Credentials = _credentials; request.CachePolicy = _cachePolicy; // Add byte ranges (to header) for (int i = 0; i < byteRanges.GetLength(0); ++i) { request.AddRange(byteRanges[i, Offset_Index], byteRanges[i, Offset_Index] + byteRanges[i, Length_Index] - 1); } return(request); }
private WebRequest GetRequest(bool allowPseudoRequest) { if (_webRequest == null) { // Don't even attempt to create if we know it will fail. This does not eliminate all failure cases // but most and is very common so let's save an expensive exception. // We still create a webRequest if possible even if we have a potential cacheEntry // because the caller may still specify BypassCache policy before calling GetResponse() that will force us to hit the server. if (!IsPreloadedPackage) { // Need inner request so we can get/set properties or create a real WebResponse. // Note: WebRequest.Create throws NotSupportedException for schemes that it does not recognize. // We need to be open-ended in our support of schemes, so we need to always try to create. // If WebRequest throws NotSupportedException then we catch and ignore if the WebRequest can be // satisfied from the cache. If the cache entry is missing then we simply re-throw because the request // cannot possibly succeed. try { _webRequest = WpfWebRequestHelper.CreateRequest(_innerUri); // special optimization for ftp - Passive mode won't return lengths on ISA servers FtpWebRequest ftpWebRequest = _webRequest as FtpWebRequest; if (ftpWebRequest != null) { ftpWebRequest.UsePassive = false; // default but allow override } } catch (NotSupportedException) { // If the inner Uri does not match any cache entry then we throw. if (!IsCachedPackage) { throw; } } } // Just return null if caller cannot accept a PseudoWebRequest if (_webRequest == null && allowPseudoRequest) { // We get here if the caller can accept a PseudoWebRequest (based on argument to the function) // and one of these two cases is true: // 1. We have a package from the PreloadedPackages collection // 2. If WebRequest.Create() failed and we have a cached package // In either case, we create a pseudo request to house property values. // In case 1, we know there will never be a cache bypass (we ignore cache policy for PreloadedPackages) // In case 2, the caller is using a schema that we cannot use for transport (not on of ftp, http, file, etc) // and we will silently accept and ignore their property modifications/queries. // Note that if they change the cache policy to BypassCache they will get an exception when they call // GetResponse(). If they leave cache policy intact, and call GetResponse() // they will get data from the cached package. _webRequest = new PseudoWebRequest(_uri, _innerUri, _partName, _cacheEntry); } } return(_webRequest); }
internal static WebRequest CreateWebRequest(Uri uri) { if (String.Compare(uri.Scheme, PackUriHelper.UriSchemePack, StringComparison.Ordinal) == 0) { return(((IWebRequestCreate)_factorySingleton).Create(uri)); } else { return(WpfWebRequestHelper.CreateRequest(uri)); } }
private HttpWebRequest CreateHttpWebRequest(int[,] byteRanges) { HttpWebRequest request; // Create the request object request = (HttpWebRequest)WpfWebRequestHelper.CreateRequest(_requestedUri); request.ProtocolVersion = HttpVersion.Version11; request.Method = "GET"; // Set the Proxy to Empty one; If we don't set this to empty one, it will try to find one for us // and ends up triggering JScript in another assembly. This will throw PolicyException since the JScript // dll doesn't have execution right. This is // Local assert to allow Proxy get/set under partial trust new WebPermission(PermissionState.Unrestricted).Assert(); // Blessed try { request.Proxy = _proxy; } finally { WebPermission.RevertAssert(); } request.Credentials = _credentials; request.CachePolicy = _cachePolicy; // Add byte ranges (to header) for (int i = 0; i < byteRanges.GetLength(0); ++i) { request.AddRange(byteRanges[i, Offset_Index], byteRanges[i, Offset_Index] + byteRanges[i, Length_Index] - 1); } return(request); }
/// <summary> /// TypeConverter method implementation. /// </summary> /// <param name="context">ITypeDescriptorContext</param> /// <param name="culture">current culture (see CLR specs)</param> /// <param name="value">value to convert from</param> /// <returns>value that is result of conversion</returns> public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { string text = ((string)value).Trim(); if (text != String.Empty) { if (text.LastIndexOf('.') == -1) { CursorType ct = (CursorType)Enum.Parse(typeof(CursorType), text); switch (ct) { case CursorType.Arrow: return(Cursors.Arrow); case CursorType.AppStarting: return(Cursors.AppStarting); case CursorType.Cross: return(Cursors.Cross); case CursorType.Help: return(Cursors.Help); case CursorType.IBeam: return(Cursors.IBeam); case CursorType.SizeAll: return(Cursors.SizeAll); case CursorType.SizeNESW: return(Cursors.SizeNESW); case CursorType.SizeNS: return(Cursors.SizeNS); case CursorType.SizeNWSE: return(Cursors.SizeNWSE); case CursorType.SizeWE: return(Cursors.SizeWE); case CursorType.UpArrow: return(Cursors.UpArrow); case CursorType.Wait: return(Cursors.Wait); case CursorType.Hand: return(Cursors.Hand); case CursorType.No: return(Cursors.No); case CursorType.None: return(Cursors.None); case CursorType.Pen: return(Cursors.Pen); case CursorType.ScrollNS: return(Cursors.ScrollNS); case CursorType.ScrollWE: return(Cursors.ScrollWE); case CursorType.ScrollAll: return(Cursors.ScrollAll); case CursorType.ScrollN: return(Cursors.ScrollN); case CursorType.ScrollS: return(Cursors.ScrollS); case CursorType.ScrollW: return(Cursors.ScrollW); case CursorType.ScrollE: return(Cursors.ScrollE); case CursorType.ScrollNW: return(Cursors.ScrollNW); case CursorType.ScrollNE: return(Cursors.ScrollNE); case CursorType.ScrollSW: return(Cursors.ScrollSW); case CursorType.ScrollSE: return(Cursors.ScrollSE); case CursorType.ArrowCD: return(Cursors.ArrowCD); } } else { if (text.EndsWith(".cur", StringComparison.OrdinalIgnoreCase) || text.EndsWith(".ani", StringComparison.OrdinalIgnoreCase)) { UriHolder uriHolder = TypeConverterHelper.GetUriFromUriContext(context, text); Uri finalUri = BindUriHelper.GetResolvedUri(uriHolder.BaseUri, uriHolder.OriginalUri); if (finalUri.IsAbsoluteUri && finalUri.IsFile) { return(new Cursor(finalUri.LocalPath)); } else { System.Net.WebRequest request = WpfWebRequestHelper.CreateRequest(finalUri); WpfWebRequestHelper.ConfigCachePolicy(request, false); return(new Cursor(WpfWebRequestHelper.GetResponseStream(request))); } } } } else { // An empty string means no cursor. return(null); } } throw GetConvertFromException(value); }
/// /// Begin a download /// internal static void BeginDownload( BitmapDecoder decoder, Uri uri, RequestCachePolicy uriCachePolicy, Stream stream ) { lock (_syncLock) { if (!_thread.IsAlive) { _thread.IsBackground = true; _thread.Start(); } } QueueEntry entry; // If there is already a download for this uri, just add the decoder to the list if (uri != null) { lock (_syncLock) { if (_uriTable[uri] != null) { entry = (QueueEntry)_uriTable[uri]; entry.decoders.Add(new WeakReference(decoder)); return; } } } entry = new QueueEntry(); entry.decoders = new List <WeakReference>(); lock (_syncLock) { entry.decoders.Add(new WeakReference(decoder)); } entry.inputUri = uri; entry.inputStream = stream; string cacheFolder = MS.Win32.WinInet.InternetCacheFolder.LocalPath; bool passed = false; new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Assert(); // BlessedAssert try { // Get the file path StringBuilder tmpFileName = new StringBuilder(NativeMethods.MAX_PATH); MS.Win32.UnsafeNativeMethods.GetTempFileName(cacheFolder, "WPF", 0, tmpFileName); try { string pathToUse = tmpFileName.ToString(); SafeFileHandle fileHandle = MS.Win32.UnsafeNativeMethods.CreateFile( pathToUse, NativeMethods.GENERIC_READ | NativeMethods.GENERIC_WRITE, /* dwDesiredAccess */ 0, /* dwShare */ null, /* lpSecurityAttributes */ NativeMethods.CREATE_ALWAYS, /* dwCreationDisposition */ NativeMethods.FILE_ATTRIBUTE_TEMPORARY | NativeMethods.FILE_FLAG_DELETE_ON_CLOSE, /* dwFlagsAndAttributes */ IntPtr.Zero /* hTemplateFile */ ); if (fileHandle.IsInvalid) { throw new Win32Exception(); } entry.outputStream = new FileStream(fileHandle, FileAccess.ReadWrite); entry.streamPath = pathToUse; passed = true; } catch (Exception e) { if (CriticalExceptions.IsCriticalException(e)) { throw; } } } finally { SecurityPermission.RevertAssert(); } if (!passed) { throw new IOException(SR.Get(SRID.Image_CannotCreateTempFile)); } entry.readBuffer = new byte[READ_SIZE]; entry.contentLength = -1; entry.contentType = string.Empty; entry.lastPercent = 0; // Add the entry to the table if we know the uri if (uri != null) { lock (_syncLock) { _uriTable[uri] = entry; } } if (stream == null) { bool fElevate = false; if (uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps) { SecurityHelper.BlockCrossDomainForHttpsApps(uri); // In this case we first check to see if the consumer has media permissions for // safe media (Site of Origin + Cross domain), if it // does we assert and run the code that requires the assert if (SecurityHelper.CallerHasMediaPermission(MediaPermissionAudio.NoAudio, MediaPermissionVideo.NoVideo, MediaPermissionImage.SafeImage)) { fElevate = true; } } // This is the case where we are accessing an http image from an http site and we have media permission if (fElevate) { (new WebPermission(NetworkAccess.Connect, BindUriHelper.UriToString(uri))).Assert(); // BlessedAssert } try { entry.webRequest = WpfWebRequestHelper.CreateRequest(uri); if (uriCachePolicy != null) { entry.webRequest.CachePolicy = uriCachePolicy; } } finally { if (fElevate) { WebPermission.RevertAssert(); } } entry.webRequest.BeginGetResponse(_responseCallback, entry); } else { _workQueue.Enqueue(entry); // Signal _waitEvent.Set(); } }
/// /// Begin a download /// internal static void BeginDownload( BitmapDecoder decoder, Uri uri, RequestCachePolicy uriCachePolicy, Stream stream ) { lock (_syncLock) { if (!_thread.IsAlive) { _thread.IsBackground = true; _thread.Start(); } } QueueEntry entry; // If there is already a download for this uri, just add the decoder to the list if (uri != null) { lock (_syncLock) { if (_uriTable[uri] != null) { entry = (QueueEntry)_uriTable[uri]; entry.decoders.Add(new WeakReference(decoder)); return; } } } entry = new QueueEntry(); entry.decoders = new List <WeakReference>(); lock (_syncLock) { entry.decoders.Add(new WeakReference(decoder)); } entry.inputUri = uri; entry.inputStream = stream; string cacheFolder = MS.Win32.WinInet.InternetCacheFolder.LocalPath; bool passed = false; new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Assert(); // BlessedAssert try { // Get the file path StringBuilder tmpFileName = new StringBuilder(NativeMethods.MAX_PATH); MS.Win32.UnsafeNativeMethods.GetTempFileName(cacheFolder, "WPF", 0, tmpFileName); try { string pathToUse = tmpFileName.ToString(); SafeFileHandle fileHandle = MS.Win32.UnsafeNativeMethods.CreateFile( pathToUse, NativeMethods.GENERIC_READ | NativeMethods.GENERIC_WRITE, /* dwDesiredAccess */ 0, /* dwShare */ null, /* lpSecurityAttributes */ NativeMethods.CREATE_ALWAYS, /* dwCreationDisposition */ NativeMethods.FILE_ATTRIBUTE_TEMPORARY | NativeMethods.FILE_FLAG_DELETE_ON_CLOSE, /* dwFlagsAndAttributes */ IntPtr.Zero /* hTemplateFile */ ); if (fileHandle.IsInvalid) { throw new Win32Exception(); } entry.outputStream = new FileStream(fileHandle, FileAccess.ReadWrite); entry.streamPath = pathToUse; passed = true; } catch (Exception e) { if (CriticalExceptions.IsCriticalException(e)) { throw; } } } finally { SecurityPermission.RevertAssert(); } if (!passed) { throw new IOException(SR.Get(SRID.Image_CannotCreateTempFile)); } entry.readBuffer = new byte[READ_SIZE]; entry.contentLength = -1; entry.contentType = string.Empty; entry.lastPercent = 0; // Add the entry to the table if we know the uri if (uri != null) { lock (_syncLock) { _uriTable[uri] = entry; } } if (stream == null) { entry.webRequest = WpfWebRequestHelper.CreateRequest(uri); if (uriCachePolicy != null) { entry.webRequest.CachePolicy = uriCachePolicy; } entry.webRequest.BeginGetResponse(_responseCallback, entry); } else { _workQueue.Enqueue(entry); // Signal _waitEvent.Set(); } }