private Task <long?> DownloadUsingAzureBlobsAsync( OperationContext context, ContentHash contentHash, string path, FileMode fileMode) { return(AsyncHttpRetryHelper <long?> .InvokeAsync( async() => { StreamWithRange?httpStream = null; Uri?uri = null; try { httpStream = await GetStreamInternalAsync(context, contentHash, 0, null).ConfigureAwait(false); if (httpStream == null) { return null; } try { var success = DownloadUriCache.Instance.TryGetDownloadUri(contentHash, out var preauthUri); uri = success ? preauthUri.NotNullUri : new Uri("http://empty.com"); Directory.CreateDirectory(Directory.GetParent(path) !.FullName); // TODO: Investigate using ManagedParallelBlobDownloader instead (bug 1365340) await ParallelHttpDownload.Download( _parallelSegmentDownloadConfig, uri, httpStream.Value, destinationPath: path, mode: fileMode, cancellationToken: context.Token, logSegmentStart: (destinationPath, offset, endOffset) => { }, logSegmentStop: (destinationPath, offset, endOffset) => { }, logSegmentFailed: (destinationPath, offset, endOffset, message) => Tracer.Debug(context, $"Download {destinationPath} [{offset}, {endOffset}) failed. (message: {message})"), segmentFactory: async(offset, length, token) => { var offsetStream = await GetStreamInternalAsync( context, contentHash, offset, (int?)_parallelSegmentDownloadConfig.SegmentSizeInBytes).ConfigureAwait(false); return offsetStream.Value; }).ConfigureAwait(false); } catch (Exception e) when(fileMode == FileMode.CreateNew && !IsErrorFileExists(e)) { try { // Need to delete here so that a partial download doesn't run afoul of FileReplacementMode.FailIfExists upon retry // Don't do this if the error itself was that the file already existed File.Delete(path); } catch (Exception ex) { Tracer.Warning(context, $"Error deleting file at {path}: {ex}"); } TraceException(context, contentHash, uri, e); throw; } return httpStream.Value.Range.WholeLength; } catch (StorageException storageEx) when(storageEx.InnerException is WebException webEx) { if (((HttpWebResponse?)webEx.Response)?.StatusCode == HttpStatusCode.NotFound) { return null; } TraceException(context, contentHash, uri, storageEx); throw; } finally { httpStream?.Dispose(); } }, maxRetries : 5, tracer : new AppTraceSourceContextAdapter(context, "BlobReadOnlyContentSession", SourceLevels.All), canRetryDelegate : exception => { // HACK HACK: This is an additional layer of retries specifically to catch the SSL exceptions seen in DM. // Newer versions of Artifact packages have this retry automatically, but the packages deployed with the M119.0 release do not. // Once the next release is completed, these retries can be removed. if (exception is HttpRequestException && exception.InnerException is WebException) { return true; } while (exception != null) { if (exception is TimeoutException) { return true; } exception = exception.InnerException; } return false; }, cancellationToken : context.Token, continueOnCapturedContext : false, context : context.TracingContext.Id.ToString())); }
public WebProxy(Uri?Address, bool BypassOnLocal) : this(Address, BypassOnLocal, null, null) { }
public override bool TryMarkEndpointUnavailableForPartitionKeyRange( DocumentServiceRequest request) { if (request == null) { throw new ArgumentNullException(nameof(request)); } // Only do partition level failover if it is a write operation. // Write operation will throw a write forbidden if it is not the primary // region. if (request.IsReadOnlyRequest) { return(false); } if (request.RequestContext == null) { return(false); } if (!this.CanUsePartitionLevelFailoverLocations(request)) { return(false); } PartitionKeyRange?partitionKeyRange = request.RequestContext.ResolvedPartitionKeyRange; if (partitionKeyRange == null) { return(false); } Uri?failedLocation = request.RequestContext.LocationEndpointToRoute; if (failedLocation == null) { return(false); } PartitionKeyRangeFailoverInfo partionFailover = this.PartitionKeyRangeToLocation.Value.GetOrAdd( partitionKeyRange, (_) => new PartitionKeyRangeFailoverInfo(failedLocation)); // Will return true if it was able to update to a new region if (partionFailover.TryMoveNextLocation( locations: this.globalEndpointManager.ReadEndpoints, failedLocation: failedLocation)) { DefaultTrace.TraceInformation("Partition level override added to new location. PartitionKeyRange: {0}, failedLocation: {1}, new location: {2}", partitionKeyRange, failedLocation, partionFailover.Current); return(true); } // All the locations have been tried. Remove the override information DefaultTrace.TraceInformation("Partition level override removed. PartitionKeyRange: {0}, failedLocation: {1}", partitionKeyRange, failedLocation); this.PartitionKeyRangeToLocation.Value.TryRemove(partitionKeyRange, out PartitionKeyRangeFailoverInfo _); return(false); }
// // Resolves into either baseUri or relativeUri according to conditions OR if not possible it uses newUriString // to return combined URI strings from both Uris // otherwise if e != null on output the operation has failed // internal static Uri?ResolveHelper(Uri baseUri, Uri?relativeUri, ref string?newUriString, ref bool userEscaped, out UriFormatException?e) { Debug.Assert(!baseUri.IsNotAbsoluteUri && !baseUri.UserDrivenParsing, "Uri::ResolveHelper()|baseUri is not Absolute or is controlled by User Parser."); e = null; string relativeStr = string.Empty; if ((object?)relativeUri != null) { if (relativeUri.IsAbsoluteUri) { return(relativeUri); } relativeStr = relativeUri.OriginalString; userEscaped = relativeUri.UserEscaped; } else { relativeStr = string.Empty; } // Here we can assert that passed "relativeUri" is indeed a relative one if (relativeStr.Length > 0 && (UriHelper.IsLWS(relativeStr[0]) || UriHelper.IsLWS(relativeStr[relativeStr.Length - 1]))) { relativeStr = relativeStr.Trim(UriHelper.s_WSchars); } if (relativeStr.Length == 0) { newUriString = baseUri.GetParts(UriComponents.AbsoluteUri, baseUri.UserEscaped ? UriFormat.UriEscaped : UriFormat.SafeUnescaped); return(null); } // Check for a simple fragment in relative part if (relativeStr[0] == '#' && !baseUri.IsImplicitFile && baseUri.Syntax !.InFact(UriSyntaxFlags.MayHaveFragment)) { newUriString = baseUri.GetParts(UriComponents.AbsoluteUri & ~UriComponents.Fragment, UriFormat.UriEscaped) + relativeStr; return(null); } // Check for a simple query in relative part if (relativeStr[0] == '?' && !baseUri.IsImplicitFile && baseUri.Syntax !.InFact(UriSyntaxFlags.MayHaveQuery)) { newUriString = baseUri.GetParts(UriComponents.AbsoluteUri & ~UriComponents.Query & ~UriComponents.Fragment, UriFormat.UriEscaped) + relativeStr; return(null); } // Check on the DOS path in the relative Uri (a special case) if (relativeStr.Length >= 3 && (relativeStr[1] == ':' || relativeStr[1] == '|') && UriHelper.IsAsciiLetter(relativeStr[0]) && (relativeStr[2] == '\\' || relativeStr[2] == '/')) { if (baseUri.IsImplicitFile) { // It could have file:/// prepended to the result but we want to keep it as *Implicit* File Uri newUriString = relativeStr; return(null); } else if (baseUri.Syntax !.InFact(UriSyntaxFlags.AllowDOSPath)) { // The scheme is not changed just the path gets replaced string prefix; if (baseUri.InFact(Flags.AuthorityFound)) { prefix = baseUri.Syntax.InFact(UriSyntaxFlags.PathIsRooted) ? ":///" : "://"; } else { prefix = baseUri.Syntax.InFact(UriSyntaxFlags.PathIsRooted) ? ":/" : ":"; } newUriString = baseUri.Scheme + prefix + relativeStr; return(null); } // If we are here then input like "http://host/path/" + "C:\x" will produce the result http://host/path/c:/x } ParsingError err = GetCombinedString(baseUri, relativeStr, userEscaped, ref newUriString); if (err != ParsingError.None) { e = GetException(err); return(null); } if ((object?)newUriString == (object)baseUri._string) { return(baseUri); } return(null); }
protected override Image ReadObjectContents(ref Utf8JsonReader reader, JsonSerializerOptions options) { var approved = false; var back = false; string? comment = null; int? edit = null; var front = false; string? id = null; Uri? image = null; Thumbnails? thumbnails = null; var types = CoverArtType.None; List <string>?unknownTypes = null; Dictionary <string, object?>?rest = null; while (reader.TokenType == JsonTokenType.PropertyName) { var prop = reader.GetPropertyName(); try { reader.Read(); switch (prop) { case "approved": approved = reader.GetBoolean(); break; case "back": back = reader.GetBoolean(); break; case "comment": comment = reader.GetString(); break; case "edit": edit = reader.GetInt32(); break; case "front": front = reader.GetBoolean(); break; case "image": image = reader.GetUri(); break; case "id": if (reader.TokenType == JsonTokenType.String) { id = reader.GetString(); } else if (reader.TryGetUInt64(out var numeric)) { id = numeric.ToString(CultureInfo.InvariantCulture); } else { throw new JsonException($"A CoverArt Archive ID is expected to be expressed either as a number or a string, but a '{reader.TokenType}' token was found instead."); } break; case "thumbnails": thumbnails = reader.GetObject(ThumbnailsReader.Instance, options); break; case "types": { foreach (var type in reader.ReadList <string>(options) ?? Enumerable.Empty <string>()) { ImageReader.AddCoverArtType(type, ref types, ref unknownTypes); } break; } default: rest ??= new Dictionary <string, object?>(); rest[prop] = reader.GetOptionalObject(options); break; } } catch (Exception e) { throw new JsonException($"Failed to deserialize the '{prop}' property.", e); } reader.Read(); } if (!edit.HasValue) { throw new JsonException("Required property 'edit' missing or null."); } if (id == null) { throw new JsonException("Required property 'id' missing or null."); } if (thumbnails == null) { throw new JsonException("Required property 'thumbnails' missing or null."); } return(new Image(edit.Value, id, thumbnails, types) { Approved = approved, Back = back, Comment = comment, Front = front, Location = image, UnknownTypes = unknownTypes, UnhandledProperties = rest }); }
/// <summary> /// This method is used to create a valid pack Uri /// </summary> /// <param name="packageUri">This is the uri that points to the entire package. /// This parameter should be an absolute Uri. This parameter cannot be null or empty </param> /// <param name="partUri">This is the uri that points to the part within the package /// This parameter should be a relative Uri. /// This parameter can be null in which case we will create a valid pack uri /// that references the entire package</param> /// <returns>A Uri with the "pack://" scheme</returns> /// <exception cref="ArgumentNullException">If packageUri parameter is null</exception> /// <exception cref="ArgumentException">If packageUri parameter is not an absolute Uri</exception> /// <exception cref="ArgumentException">If partUri parameter does not conform to the valid partUri syntax</exception> public static Uri Create(Uri packageUri, Uri?partUri) { return(Create(packageUri, partUri, null)); }
/// <summary> /// httpApi的请求消息 /// </summary> /// <param name="requestUri">请求uri</param> public HttpApiRequestMessage(Uri?requestUri) : this(requestUri, useDefaultUserAgent : false) { }
public override async Task ExecuteAsync(OutputContext output, ApplicationBuilder application, ServiceBuilder service) { var bindings = service.Outputs.OfType <ComputedBindings>().FirstOrDefault(); if (bindings is null) { return; } foreach (var binding in bindings.Bindings) { if (binding is SecretInputBinding secretInputBinding) { if (!Secrets.Add(secretInputBinding.Name)) { output.WriteDebugLine($"Already validated secret '{secretInputBinding.Name}'."); continue; } output.WriteDebugLine($"Validating secret '{secretInputBinding.Name}'."); var config = KubernetesClientConfiguration.BuildDefaultConfig(); // Workaround for https://github.com/kubernetes-client/csharp/issues/372 var store = await KubernetesClientConfiguration.LoadKubeConfigAsync(); var context = store.Contexts.Where(c => c.Name == config.CurrentContext).FirstOrDefault(); // Use namespace of application, or current context, or 'default' config.Namespace = application.Namespace; config.Namespace ??= context?.ContextDetails?.Namespace ?? "default"; var kubernetes = new Kubernetes(config); try { var result = await kubernetes.ReadNamespacedSecretWithHttpMessagesAsync(secretInputBinding.Name, config.Namespace); output.WriteInfoLine($"Found existing secret '{secretInputBinding.Name}'."); continue; } catch (HttpOperationException ex) when(ex.Response.StatusCode == HttpStatusCode.NotFound) { // The kubernetes client uses exceptions for 404s. } catch (Exception ex) { output.WriteDebugLine("Failed to query secret."); output.WriteDebugLine(ex.ToString()); throw new CommandException("Unable connect to kubernetes.", ex); } if (Force) { output.WriteDebugLine("Skipping because force was specified."); continue; } if (!Interactive && secretInputBinding is SecretConnectionStringInputBinding) { throw new CommandException( $"The secret '{secretInputBinding.Name}' used for service '{secretInputBinding.Service.Name}' is missing from the deployment environment. " + $"Rerun the command with --interactive to specify the value interactively, or with --force to skip validation. Alternatively " + $"use the following command to manually create the secret." + System.Environment.NewLine + $"kubectl create secret generic {secretInputBinding.Name} --namespace {config.Namespace} --from-literal=connectionstring=<value>"); } if (!Interactive && secretInputBinding is SecretUrlInputBinding) { throw new CommandException( $"The secret '{secretInputBinding.Name}' used for service '{secretInputBinding.Service.Name}' is missing from the deployment environment. " + $"Rerun the command with --interactive to specify the value interactively, or with --force to skip validation. Alternatively " + $"use the following command to manually create the secret." + System.Environment.NewLine + $"kubectl create secret generic {secretInputBinding.Name} --namespace {config.Namespace} --from-literal=protocol=<value> --from-literal=host=<value> --from-literal=port=<value>"); } V1Secret secret; if (secretInputBinding is SecretConnectionStringInputBinding) { // If we get here then we should create the secret. var text = output.Prompt($"Enter the connection string to use for service '{secretInputBinding.Service.Name}'", allowEmpty: true); if (string.IsNullOrWhiteSpace(text)) { output.WriteAlwaysLine($"Skipping creation of secret for '{secretInputBinding.Service.Name}'. This may prevent creation of pods until secrets are created."); output.WriteAlwaysLine($"Manually create a secret with:"); output.WriteAlwaysLine($"kubectl create secret generic {secretInputBinding.Name} --namespace {config.Namespace} --from-literal=connectionstring=<value>"); continue; } secret = new V1Secret(type: "Opaque", stringData: new Dictionary <string, string>() { { "connectionstring", text }, }); } else if (secretInputBinding is SecretUrlInputBinding) { // If we get here then we should create the secret. string text; Uri? uri = null; while (true) { text = output.Prompt($"Enter the URI to use for service '{secretInputBinding.Service.Name}'", allowEmpty: true); if (string.IsNullOrEmpty(text)) { break; // skip } else if (Uri.TryCreate(text, UriKind.Absolute, out uri)) { break; // success } output.WriteAlwaysLine($"Invalid URI: '{text}'"); } if (string.IsNullOrWhiteSpace(text)) { output.WriteAlwaysLine($"Skipping creation of secret for '{secretInputBinding.Service.Name}'. This may prevent creation of pods until secrets are created."); output.WriteAlwaysLine($"Manually create a secret with:"); output.WriteAlwaysLine($"kubectl create secret generic {secretInputBinding.Name} --namespace {config.Namespace} --from-literal=protocol=<value> --from-literal=host=<value> --from-literal=port=<value>"); continue; } secret = new V1Secret(type: "Opaque", stringData: new Dictionary <string, string>() { { "protocol", uri !.Scheme },
public static Task <T?> GetFromMessagePackLz4AAsync <T>(this HttpClient client, Uri?requestUri, CancellationToken cancellationToken = default) { if (client == null) { throw new ArgumentNullException(nameof(client)); } if (requestUri == null) { throw new ArgumentNullException(nameof(requestUri)); } var resultTask = client.GetMessagePackLz4AAsync(requestUri, cancellationToken); return(GetFromMessagePackAsyncCore <T>(resultTask, CustomFormatterLz4A, cancellationToken)); }
/// <summary> /// Initializes a new instance of the <see cref="JValue"/> class with the given value. /// </summary> /// <param name="value">The value.</param> public JValue(Uri? value) : this(value, (value != null) ? JTokenType.Uri : JTokenType.Null) { }
internal static int Compare(JTokenType valueType, object? objA, object? objB) { if (objA == objB) { return 0; } if (objB == null) { return 1; } if (objA == null) { return -1; } switch (valueType) { case JTokenType.Integer: { #if HAVE_BIG_INTEGER if (objA is BigInteger integerA) { return CompareBigInteger(integerA, objB); } if (objB is BigInteger integerB) { return -CompareBigInteger(integerB, objA); } #endif if (objA is ulong || objB is ulong || objA is decimal || objB is decimal) { return Convert.ToDecimal(objA, CultureInfo.InvariantCulture).CompareTo(Convert.ToDecimal(objB, CultureInfo.InvariantCulture)); } else if (objA is float || objB is float || objA is double || objB is double) { return CompareFloat(objA, objB); } else { return Convert.ToInt64(objA, CultureInfo.InvariantCulture).CompareTo(Convert.ToInt64(objB, CultureInfo.InvariantCulture)); } } case JTokenType.Float: { #if HAVE_BIG_INTEGER if (objA is BigInteger integerA) { return CompareBigInteger(integerA, objB); } if (objB is BigInteger integerB) { return -CompareBigInteger(integerB, objA); } #endif if (objA is ulong || objB is ulong || objA is decimal || objB is decimal) { return Convert.ToDecimal(objA, CultureInfo.InvariantCulture).CompareTo(Convert.ToDecimal(objB, CultureInfo.InvariantCulture)); } return CompareFloat(objA, objB); } case JTokenType.Comment: case JTokenType.String: case JTokenType.Raw: string s1 = Convert.ToString(objA, CultureInfo.InvariantCulture); string s2 = Convert.ToString(objB, CultureInfo.InvariantCulture); return string.CompareOrdinal(s1, s2); case JTokenType.Boolean: bool b1 = Convert.ToBoolean(objA, CultureInfo.InvariantCulture); bool b2 = Convert.ToBoolean(objB, CultureInfo.InvariantCulture); return b1.CompareTo(b2); case JTokenType.Date: #if HAVE_DATE_TIME_OFFSET if (objA is DateTime dateA) { #else DateTime dateA = (DateTime)objA; #endif DateTime dateB; #if HAVE_DATE_TIME_OFFSET if (objB is DateTimeOffset offsetB) { dateB = offsetB.DateTime; } else #endif { dateB = Convert.ToDateTime(objB, CultureInfo.InvariantCulture); } return dateA.CompareTo(dateB); #if HAVE_DATE_TIME_OFFSET } else { DateTimeOffset offsetA = (DateTimeOffset)objA; if (!(objB is DateTimeOffset offsetB)) { offsetB = new DateTimeOffset(Convert.ToDateTime(objB, CultureInfo.InvariantCulture)); } return offsetA.CompareTo(offsetB); } #endif case JTokenType.Bytes: if (!(objB is byte[] bytesB)) { throw new ArgumentException("Object must be of type byte[]."); } byte[]? bytesA = objA as byte[]; MiscellaneousUtils.Assert(bytesA != null); return MiscellaneousUtils.ByteArrayCompare(bytesA!, bytesB); case JTokenType.Guid: if (!(objB is Guid)) { throw new ArgumentException("Object must be of type Guid."); } Guid guid1 = (Guid)objA; Guid guid2 = (Guid)objB; return guid1.CompareTo(guid2); case JTokenType.Uri: Uri? uri2 = objB as Uri; if (uri2 == null) { throw new ArgumentException("Object must be of type Uri."); } Uri uri1 = (Uri)objA; return Comparer<string>.Default.Compare(uri1.ToString(), uri2.ToString()); case JTokenType.TimeSpan: if (!(objB is TimeSpan)) { throw new ArgumentException("Object must be of type TimeSpan."); } TimeSpan ts1 = (TimeSpan)objA; TimeSpan ts2 = (TimeSpan)objB; return ts1.CompareTo(ts2); default: throw MiscellaneousUtils.CreateArgumentOutOfRangeException(nameof(valueType), valueType, "Unexpected value type: {0}".FormatWith(CultureInfo.InvariantCulture, valueType)); } }
public static void SendingHealthProbeToEndpointOfDestination(ILogger logger, Uri?endpointUri, string destinationId, string clusterId) { _sendingHealthProbeToEndpointOfDestination(logger, endpointUri, destinationId, clusterId, null); }
private static BitmapDecoder?GetDecoder(BitmapSource image, out GifFile?gifFile) { gifFile = null; BitmapDecoder?decoder = null; Stream? stream = null; Uri? uri = null; var createOptions = BitmapCreateOptions.None; var bmp = image as BitmapImage; if (bmp != null) { createOptions = bmp.CreateOptions; if (bmp.StreamSource != null) { stream = bmp.StreamSource; } else if (bmp.UriSource != null) { uri = bmp.UriSource; if (bmp.BaseUri != null && !uri.IsAbsoluteUri) { uri = new Uri(bmp.BaseUri, uri); } } } else { var frame = image as BitmapFrame; if (frame != null) { decoder = frame.Decoder; Uri.TryCreate(frame.BaseUri, frame.ToString(), out uri); } } if (decoder == null) { if (stream != null) { stream.Position = 0; decoder = BitmapDecoder.Create(stream, createOptions, BitmapCacheOption.OnLoad); } else if (uri != null && uri.IsAbsoluteUri) { decoder = BitmapDecoder.Create(uri, createOptions, BitmapCacheOption.OnLoad); } } if (decoder is GifBitmapDecoder && !CanReadNativeMetadata(decoder)) { if (stream != null) { stream.Position = 0; gifFile = GifFile.ReadGifFile(stream, true); } else if (uri != null) { gifFile = DecodeGifFile(uri); } } return(decoder); }
private void TraceException(OperationContext context, ContentHash hash, Uri?azureBlobUri, Exception e, [CallerMemberName] string?operation = null) { string errorMessage = $"{operation} failed. ContentHash=[{hash.ToShortString()}], BaseAddress=[{BlobStoreHttpClient.BaseAddress}], BlobUri=[{getBlobUri(azureBlobUri)}]"; // Explicitly trace all the failures here to simplify errors analysis. Tracer.Debug(context, $"{errorMessage}. Error=[{e}]");
/// <summary> /// Ensure that "dataSource" is cached as an XPathDocument and return a navigator over the document. /// </summary> private XPathNavigator?ConstructDocument(object dataSource, string?uriRelative, Uri?uriResolved) { Debug.Assert(dataSource != null, "GetType() below assumes dataSource is not null"); Stream?stream = dataSource as Stream; if (stream != null) { // Create document from stream XmlReader reader = _readerSettings.CreateReader(stream, uriResolved?.ToString()); try { // Create WhitespaceRuleReader if whitespace should be stripped return(new XPathDocument(WhitespaceRuleReader.CreateReader(reader, _wsRules), XmlSpace.Preserve).CreateNavigator()); } finally { // Always close reader that was opened here reader.Close(); } } else if (dataSource is XmlReader reader) { // Create document from reader // Create WhitespaceRuleReader if whitespace should be stripped return(new XPathDocument(WhitespaceRuleReader.CreateReader(reader, _wsRules), XmlSpace.Preserve).CreateNavigator()); } else if (dataSource is IXPathNavigable xPathNavigable) { if (_wsRules != null) { throw new XslTransformException(SR.XmlIl_CantStripNav, string.Empty); } return(xPathNavigable.CreateNavigator()); } Debug.Assert(uriRelative != null, "Relative URI should not be null"); throw new XslTransformException(SR.XmlIl_CantResolveEntity, uriRelative, dataSource.GetType().ToString()); }
public static Task <T?> GetFromMessagePackAsync <T>(this HttpClient client, Uri?requestUri, CancellationToken cancellationToken = default) => client.GetFromMessagePackAsync <T>(requestUri, CustomFormatter, cancellationToken);
//This method validates the packUri and returns its two components if they are valid- //1. Package Uri //2. Part Uri internal static void ValidateAndGetPackUriComponents(Uri packUri, out Uri packageUri, out Uri?partUri) { //Validate if its not null and is an absolute Uri, has pack:// Scheme. packUri = ValidatePackUri(packUri); packageUri = GetPackageUriComponent(packUri); partUri = GetPartUriComponent(packUri); }
public static async Task <HttpResponseMessage?> GetMessagePackLz4AAsync(this HttpClient client, Uri?requestUri, CancellationToken cancellationToken = default) { if (client == null) { throw new ArgumentNullException(nameof(client)); } if (requestUri == null) { throw new ArgumentNullException(nameof(requestUri)); } return(await client.GetMessagePackAsync(requestUri, MessagePackMediaTypeLz4A, cancellationToken)); }
/// <inheritdoc cref="WithUrl(ISetupActivity{SendHttpRequest}, Func{ActivityExecutionContext, ValueTask{Uri?}})"/> public static ISetupActivity <Http.SendHttpRequest> WithUrl(this ISetupActivity <Http.SendHttpRequest> activity, Uri?url) => activity.Set(x => x.Url, url);
private static async Task <HttpResponseMessage?> GetMessagePackAsync(this HttpClient client, Uri?requestUri, string mediaType, CancellationToken cancellationToken = default) { var request = new HttpRequestMessage(HttpMethod.Get, requestUri); request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(mediaType)); return(await client.SendAsync(request, cancellationToken)); }
/// <summary> /// Used register any subschemas during validation. Enables look-forward compatibility with `$ref` keywords. /// </summary> /// <param name="baseUri">The current base URI</param> /// <param name="localRegistry"></param> public void RegisterSubschemas(Uri?baseUri, JsonSchemaRegistry localRegistry) { }
public static Task <HttpResponseMessage> PutAsMessagePackAsync <T>(this HttpClient client, Uri?requestUri, T value, MessagePackSerializerOptions options, CancellationToken cancellationToken = default) { if (client == null) { throw new ArgumentNullException(nameof(client)); } if (requestUri == null) { throw new ArgumentNullException(nameof(requestUri)); } var request = new HttpRequestMessage(HttpMethod.Put, requestUri); return(client.SendAsMessagePackAsyncCore(request, value, options, MessagePackMediaType, cancellationToken)); }
protected HttpClientService(HttpClientServiceSettings?settings, Uri?defaultBaseUri) : this(settings, new HttpClientServiceDefaults { BaseUri = defaultBaseUri }) { }
public static Task <T?> PutReadAsMessagePackAsync <T>(this HttpClient client, Uri?requestUri, T value, CancellationToken cancellationToken = default) => client.PutReadAsMessagePackAsync(requestUri, value, CustomFormatter, cancellationToken);
public WebProxy(Uri?Address) : this(Address, false, null, null) { }
public static Task <T?> PutReadAsMessagePackLz4AAsync <T>(this HttpClient client, Uri?requestUri, T value, CancellationToken cancellationToken = default) { if (client == null) { throw new ArgumentNullException(nameof(client)); } if (requestUri == null) { throw new ArgumentNullException(nameof(requestUri)); } var request = new HttpRequestMessage(HttpMethod.Put, requestUri); return(client.SendReadAsMessagePackAsyncCore(request, value, CustomFormatterLz4A, MessagePackMediaType, cancellationToken)); }
public WebProxy(Uri?Address, bool BypassOnLocal, string[]?BypassList) : this(Address, BypassOnLocal, BypassList, null) { }
private static Func <string, CancellationToken, bool, ValueTask <byte[]?> >?CreateDownloadBytesFunc() { try { // Use reflection to access System.Net.Http: // Since System.Net.Http.dll explicitly depends on System.Security.Cryptography.X509Certificates.dll, // the latter can't in turn have an explicit dependency on the former. // Get the relevant types needed. Type?socketsHttpHandlerType = Type.GetType("System.Net.Http.SocketsHttpHandler, System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", throwOnError: false); Type?httpMessageHandlerType = Type.GetType("System.Net.Http.HttpMessageHandler, System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", throwOnError: false); Type?httpClientType = Type.GetType("System.Net.Http.HttpClient, System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", throwOnError: false); Type?httpRequestMessageType = Type.GetType("System.Net.Http.HttpRequestMessage, System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", throwOnError: false); Type?httpResponseMessageType = Type.GetType("System.Net.Http.HttpResponseMessage, System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", throwOnError: false); Type?httpResponseHeadersType = Type.GetType("System.Net.Http.Headers.HttpResponseHeaders, System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", throwOnError: false); Type?httpContentType = Type.GetType("System.Net.Http.HttpContent, System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", throwOnError: false); if (socketsHttpHandlerType == null || httpMessageHandlerType == null || httpClientType == null || httpRequestMessageType == null || httpResponseMessageType == null || httpResponseHeadersType == null || httpContentType == null) { Debug.Fail("Unable to load required type."); return(null); } Type taskOfHttpResponseMessageType = typeof(Task <>).MakeGenericType(httpResponseMessageType); // Get the methods on those types. ConstructorInfo?socketsHttpHandlerCtor = socketsHttpHandlerType.GetConstructor(Type.EmptyTypes); PropertyInfo? pooledConnectionIdleTimeoutProp = socketsHttpHandlerType.GetProperty("PooledConnectionIdleTimeout"); PropertyInfo? allowAutoRedirectProp = socketsHttpHandlerType.GetProperty("AllowAutoRedirect"); ConstructorInfo?httpClientCtor = httpClientType.GetConstructor(new Type[] { httpMessageHandlerType }); PropertyInfo? requestUriProp = httpRequestMessageType.GetProperty("RequestUri"); ConstructorInfo?httpRequestMessageCtor = httpRequestMessageType.GetConstructor(Type.EmptyTypes); MethodInfo? sendMethod = httpClientType.GetMethod("Send", new Type[] { httpRequestMessageType, typeof(CancellationToken) }); MethodInfo? sendAsyncMethod = httpClientType.GetMethod("SendAsync", new Type[] { httpRequestMessageType, typeof(CancellationToken) }); PropertyInfo? responseContentProp = httpResponseMessageType.GetProperty("Content"); PropertyInfo? responseStatusCodeProp = httpResponseMessageType.GetProperty("StatusCode"); PropertyInfo? responseHeadersProp = httpResponseMessageType.GetProperty("Headers"); PropertyInfo? responseHeadersLocationProp = httpResponseHeadersType.GetProperty("Location"); MethodInfo? readAsStreamMethod = httpContentType.GetMethod("ReadAsStream", Type.EmptyTypes); PropertyInfo? taskOfHttpResponseMessageResultProp = taskOfHttpResponseMessageType.GetProperty("Result"); if (socketsHttpHandlerCtor == null || pooledConnectionIdleTimeoutProp == null || allowAutoRedirectProp == null || httpClientCtor == null || requestUriProp == null || httpRequestMessageCtor == null || sendMethod == null || sendAsyncMethod == null || responseContentProp == null || responseStatusCodeProp == null || responseHeadersProp == null || responseHeadersLocationProp == null || readAsStreamMethod == null || taskOfHttpResponseMessageResultProp == null) { Debug.Fail("Unable to load required members."); return(null); } // Only keep idle connections around briefly, as a compromise between resource leakage and port exhaustion. const int PooledConnectionIdleTimeoutSeconds = 15; const int MaxRedirections = 10; // Equivalent of: // var socketsHttpHandler = new SocketsHttpHandler() { // PooledConnectionIdleTimeout = TimeSpan.FromSeconds(PooledConnectionIdleTimeoutSeconds), // AllowAutoRedirect = false // }; // var httpClient = new HttpClient(socketsHttpHandler); // Note: using a ConstructorInfo instead of Activator.CreateInstance, so the ILLinker can see the usage through the lambda method. object?socketsHttpHandler = socketsHttpHandlerCtor.Invoke(null); pooledConnectionIdleTimeoutProp.SetValue(socketsHttpHandler, TimeSpan.FromSeconds(PooledConnectionIdleTimeoutSeconds)); allowAutoRedirectProp.SetValue(socketsHttpHandler, false); object?httpClient = httpClientCtor.Invoke(new object?[] { socketsHttpHandler }); return(async(string uriString, CancellationToken cancellationToken, bool async) => { Uri uri = new Uri(uriString); if (!IsAllowedScheme(uri.Scheme)) { return null; } // Equivalent of: // HttpRequestMessage requestMessage = new HttpRequestMessage() { RequestUri = new Uri(uri) }; // HttpResponseMessage responseMessage = httpClient.Send(requestMessage, cancellationToken); // Note: using a ConstructorInfo instead of Activator.CreateInstance, so the ILLinker can see the usage through the lambda method. object requestMessage = httpRequestMessageCtor.Invoke(null); requestUriProp.SetValue(requestMessage, uri); object responseMessage; if (async) { Task sendTask = (Task)sendAsyncMethod.Invoke(httpClient, new object[] { requestMessage, cancellationToken }) !; await sendTask.ConfigureAwait(false); responseMessage = taskOfHttpResponseMessageResultProp.GetValue(sendTask) !; } else { responseMessage = sendMethod.Invoke(httpClient, new object[] { requestMessage, cancellationToken }) !; } int redirections = 0; Uri?redirectUri; bool hasRedirect; while (true) { int statusCode = (int)responseStatusCodeProp.GetValue(responseMessage) !; object responseHeaders = responseHeadersProp.GetValue(responseMessage) !; Uri?location = (Uri?)responseHeadersLocationProp.GetValue(responseHeaders); redirectUri = GetUriForRedirect((Uri)requestUriProp.GetValue(requestMessage) !, statusCode, location, out hasRedirect); if (redirectUri == null) { break; } ((IDisposable)responseMessage).Dispose(); redirections++; if (redirections > MaxRedirections) { ReportRedirectsExceeded(); return null; } ReportRedirected(redirectUri); // Equivalent of: // requestMessage = new HttpRequestMessage() { RequestUri = redirectUri }; // requestMessage.RequestUri = redirectUri; // responseMessage = httpClient.Send(requestMessage, cancellationToken); requestMessage = httpRequestMessageCtor.Invoke(null); requestUriProp.SetValue(requestMessage, redirectUri); if (async) { Task sendTask = (Task)sendAsyncMethod.Invoke(httpClient, new object[] { requestMessage, cancellationToken }) !; await sendTask.ConfigureAwait(false); responseMessage = taskOfHttpResponseMessageResultProp.GetValue(sendTask) !; } else { responseMessage = sendMethod.Invoke(httpClient, new object[] { requestMessage, cancellationToken }) !; } } if (hasRedirect && redirectUri == null) { return null; } // Equivalent of: // using Stream responseStream = resp.Content.ReadAsStream(); object content = responseContentProp.GetValue(responseMessage) !; using Stream responseStream = (Stream)readAsStreamMethod.Invoke(content, null) !; var result = new MemoryStream(); if (async) { await responseStream.CopyToAsync(result).ConfigureAwait(false); } else { responseStream.CopyTo(result); } ((IDisposable)responseMessage).Dispose(); return result.ToArray(); }); } catch { // We shouldn't have any exceptions, but if we do, ignore them all. return(null); } }
/// <summary> /// Calls the methods necessary to generate a SHPF URL /// /// Template IDs: /// mobileform01 -- Credit Card Only - White Vertical (mobile capable) /// default1v5 -- Credit Card Only - Gray Horizontal /// default7v5 -- Credit Card Only - Gray Horizontal Donation /// default7v5R -- Credit Card Only - Gray Horizontal Donation with Recurring /// default3v4 -- Credit Card Only - Blue Vertical with card swipe /// mobileform02 -- Credit Card & ACH - White Vertical (mobile capable) /// default8v5 -- Credit Card & ACH - Gray Horizontal Donation /// default8v5R -- Credit Card & ACH - Gray Horizontal Donation with Recurring /// mobileform03 -- ACH Only - White Vertical (mobile capable) /// mobileresult01 -- Default without signature line - White Responsive (mobile) /// defaultres1 -- Default without signature line – Blue /// V5results -- Default without signature line – Gray /// V5Iresults -- Default without signature line – White /// defaultres2 -- Default with signature line – Blue /// remote_url - Use a remote URL /// </summary> /// <param name="merchantName">Merchant name that will be displayed in the payment page.</param> /// <param name="returnURL">Link to be displayed on the transacton results page. Usually the merchant's web site home page.</param> /// <param name="transactionType">SALE/AUTH -- Whether the customer should be charged or only check for enough credit available.</param> /// <param name="acceptDiscover">Yes/No -- Yes for most US merchants. No for most Canadian merchants.</param> /// <param name="acceptAmex">Yes/No -- Has an American Express merchant account been set up?</param> /// <param name="amount">The amount if the merchant is setting the initial amount.</param> /// <param name="protectAmount">Yes/No -- Should the amount be protected from changes by the tamperproof seal?</param> /// <param name="rebilling">Yes/No -- Should a recurring transaction be set up?</param> /// <param name="rebProtect">Yes/No -- Should the rebilling fields be protected by the tamperproof seal?</param> /// <param name="rebAmount">Amount that will be charged when a recurring transaction occurs.</param> /// <param name="rebCycles">Number of times that the recurring transaction should occur. Not set if recurring transactions should continue until canceled.</param> /// <param name="rebStartDate">Date (yyyy-mm-dd) or period (x units) until the first recurring transaction should occur. Possible units are DAY, DAYS, WEEK, WEEKS, MONTH, MONTHS, YEAR or YEARS. (ex. 2016-04-01 or 1 MONTH)</param> /// <param name="rebFrequency">How often the recurring transaction should occur. Format is 'X UNITS'. Possible units are DAY, DAYS, WEEK, WEEKS, MONTH, MONTHS, YEAR or YEARS. (ex. 1 MONTH) </param> /// <param name="customID1">A merchant defined custom ID value.</param> /// <param name="protectCustomID1">Yes/No -- Should the Custom ID value be protected from change using the tamperproof seal?</param> /// <param name="customID2">A merchant defined custom ID 2 value.</param> /// <param name="protectCustomID2">Yes/No -- Should the Custom ID 2 value be protected from change using the tamperproof seal?</param> /// <param name="paymentTemplate">Select one of our payment form template IDs or your own customized template ID. If the customer should not be allowed to change the amount, add a 'D' to the end of the template ID. Example: 'mobileform01D'</param> /// <param name="receiptTemplate">Select one of our receipt form template IDs, your own customized template ID, or "remote_url" if you have one.</param> /// <param name="receiptTempRemoteURL">Your remote URL ** Only required if receiptTemplate = "remote_url".</param> public Uri GenerateURL(string merchantName, Uri?returnUrl, string transactionType, string acceptDiscover, string acceptAmex, decimal?amount = null, string?protectAmount = "No", string?rebilling = "No", string?rebProtect = "Yes", string?rebAmount = null, string?rebCycles = null, string?rebStartDate = null, string?rebFrequency = null, string?customID1 = null, string?protectCustomID1 = "No", string?customID2 = null, string?protectCustomID2 = "No", string?paymentTemplate = "mobileform01", string?receiptTemplate = "mobileresult01", Uri?receiptTempRemoteUrl = null) { _dba = merchantName; _returnUrl = returnUrl; _transType = transactionType; _discoverImage = acceptDiscover?.ToUpperInvariant()[0] == 'Y' ? "discvr.gif" : "spacer.gif"; _amexImage = acceptAmex?.ToUpperInvariant()[0] == 'Y' ? "amex.gif" : "spacer.gif"; _amount = amount?.ToString("0.00", CultureInfo.InvariantCulture); _protectAmount = protectAmount; _doRebill = rebilling?.ToUpperInvariant()[0] == 'Y' ? "1" : "0"; _rebillProtect = rebProtect; _rebillAmount = rebAmount; _rebillCycles = rebCycles; _rebillFirstDate = rebStartDate; _rebillExpr = rebFrequency; _customID1 = customID1; _protectCustomID1 = protectCustomID1; _customID2 = customID2; _protectCustomID2 = protectCustomID2; _shpfFormID = paymentTemplate; _receiptFormID = receiptTemplate; _remoteUrl = receiptTempRemoteUrl; _shpfTpsHashType = "HMAC_SHA512"; _receiptTpsHashType = _shpfTpsHashType; _tpsHashType = SetHashType(_tpsHashType); _cardTypes = SetCardTypes(); _receiptTpsDef = "SHPF_ACCOUNT_ID SHPF_FORM_ID RETURN_URL DBA AMEX_IMAGE DISCOVER_IMAGE SHPF_TPS_DEF SHPF_TPS_HASH_TYPE"; _receiptTpsString = SetReceiptTpsString(); _receiptTamperProofSeal = GenerateTPS(_receiptTpsString, _receiptTpsHashType); _receiptUrl = SetReceiptUrl(); _bp10emuTpsDef = AddDefProtectedStatus("MERCHANT APPROVED_URL DECLINED_URL MISSING_URL MODE TRANSACTION_TYPE TPS_DEF TPS_HASH_TYPE"); _bp10emuTpsString = SetBp10emuTpsString(); _bp10emuTamperProofSeal = GenerateTPS(_bp10emuTpsString, _tpsHashType); _shpfTpsDef = AddDefProtectedStatus("SHPF_FORM_ID SHPF_ACCOUNT_ID DBA TAMPER_PROOF_SEAL AMEX_IMAGE DISCOVER_IMAGE TPS_DEF TPS_HASH_TYPE SHPF_TPS_DEF SHPF_TPS_HASH_TYPE"); _shpfTpsString = SetShpfTpsString(); _shpfTamperProofSeal = GenerateTPS(_shpfTpsString, _shpfTpsHashType); return(CalcResponseUrl()); }
private bool TryParseUri(string value, [NotNullWhen(true)] out Uri?uri) => Uri.TryCreate(value, UriKind.Absolute, out uri);