Files
PowerToys/src/modules/launcher/PowerLauncher/Helper/DataWebRequestFactory.cs
Avneet Kaur 2c5b9b4d52 Audit culture bugs (#7707)
* Added comments and fixed CultureInfo /  StringComparison where appropriate

* Addressed comments

* Fixed comment
2020-10-30 16:43:09 -07:00

83 lines
2.2 KiB
C#

// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.IO;
using System.Net;
namespace PowerLauncher.Helper
{
public class DataWebRequestFactory : IWebRequestCreate
{
private class DataWebRequest : WebRequest
{
private readonly Uri _uri;
public DataWebRequest(Uri uri)
{
_uri = uri;
}
public override WebResponse GetResponse()
{
return new DataWebResponse(_uri);
}
}
private class DataWebResponse : WebResponse
{
private readonly string _contentType;
private readonly byte[] _data;
public DataWebResponse(Uri uri)
{
string uriString = uri.AbsoluteUri;
// Using Ordinal since this is internal and used with a symbol
int commaIndex = uriString.IndexOf(',', StringComparison.Ordinal);
var headers = uriString.Substring(0, commaIndex).Split(';');
_contentType = headers[0];
string dataString = uriString.Substring(commaIndex + 1);
_data = Convert.FromBase64String(dataString);
}
public override string ContentType
{
get
{
return _contentType;
}
set
{
throw new NotSupportedException();
}
}
public override long ContentLength
{
get
{
return _data.Length;
}
set
{
throw new NotSupportedException();
}
}
public override Stream GetResponseStream()
{
return new MemoryStream(_data);
}
}
public WebRequest Create(Uri uri)
{
return new DataWebRequest(uri);
}
}
}