本文整理汇总了C#中System.Net.Http.HttpRequestMessage.IsLocal方法的典型用法代码示例。如果您正苦于以下问题:C# HttpRequestMessage.IsLocal方法的具体用法?C# HttpRequestMessage.IsLocal怎么用?C# HttpRequestMessage.IsLocal使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Http.HttpRequestMessage
的用法示例。
在下文中一共展示了HttpRequestMessage.IsLocal方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: IsLocal_MS_IsLocalPropertyTrue_True
public void IsLocal_MS_IsLocalPropertyTrue_True()
{
var request = new HttpRequestMessage();
request.Properties.Add("MS_IsLocal", new Lazy<bool>(() => true));
Assert.IsTrue(request.IsLocal());
}
示例2: IsLocal_MS_IsLocalPropertyFalse_False
public void IsLocal_MS_IsLocalPropertyFalse_False()
{
var request = new HttpRequestMessage();
request.Properties.Add("MS_IsLocal", new Lazy<bool>(() => false));
Assert.IsFalse(request.IsLocal());
}
示例3: SendAsync
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) {
if (!request.IsLocal() && request.RequestUri.Scheme != Uri.UriSchemeHttps) {
return Task<HttpResponseMessage>.Factory.StartNew(() =>
{
var response = new HttpResponseMessage(HttpStatusCode.Forbidden)
{
Content = new StringContent("HTTPS Required")
};
return response;
});
}
return base.SendAsync(request, cancellationToken);
}
示例4: SendAsync
protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
HttpResponseMessage response;
// prevent unsecure communication for non-local requests
if (request.IsLocal() || request.RequestUri.Scheme.Equals(Uri.UriSchemeHttps))
{
// continue with request as normal
response = await base.SendAsync(request, cancellationToken);
}
else
{
// stop request and return 403 error
response = new HttpResponseMessage(System.Net.HttpStatusCode.Forbidden)
{
ReasonPhrase = "Connection is unsecured. HTTPS is required",
};
}
return response;
}
示例5: GetResponse
/// <summary>
/// Provide a single method to try to build a <see cref="HttpResponseHeaders" /> from <see cref="CacheControlHeaderValue" /> and <see cref="HttpRequestMessage" />
/// </summary>
public virtual HttpResponseMessage GetResponse(CacheControlHeaderValue cacheControl, WebApiCacheItem cacheItem, HttpRequestMessage request)
{
if (cacheControl != null &&
cacheControl.Extensions != null &&
cacheControl.Extensions.Any(x => x.Name == WebApiExtensions.__cacheControl_flatwhite_force_refresh) &&
request.IsLocal())
{
return null;
}
if (cacheControl != null && cacheControl.OnlyIfCached && cacheItem == null)
{
var errorResponse = new HttpResponseMessage {StatusCode = HttpStatusCode.GatewayTimeout};
errorResponse.Headers.Add("X-Flatwhite-Message", "no cache available");
return errorResponse;
}
if (cacheItem == null)
{
return null;
}
var ageInSeconds = cacheItem.Age;
var responseCacheControl = new CacheControlHeaderValue
{
MaxAge = TimeSpan.FromSeconds(Math.Max(cacheItem.MaxAge - ageInSeconds, 0)),
};
var cacheNotQualified = false;
bool stale = cacheControl?.MaxAge?.TotalSeconds > 0 && cacheControl.MaxAge.Value.TotalSeconds < ageInSeconds;
if (cacheItem.IsStale())
{
stale = true;
Global.Logger.Info($"Stale key \"{cacheItem.Key}\", age: \"{ageInSeconds}\", store: \"{cacheItem.StoreId}\", request: {request.RequestUri.PathAndQuery}");
if (cacheItem.StaleWhileRevalidate > 0 &&
cacheControl != null &&
cacheControl.MaxStale &&
cacheControl.MaxStaleLimit.HasValue &&
cacheControl.MaxStaleLimit.Value.TotalSeconds > (ageInSeconds - cacheItem.MaxAge))
{
// https://tools.ietf.org/html/rfc5861
responseCacheControl.Extensions.Add(new NameValueHeaderValue("stale-while-revalidate", cacheItem.StaleWhileRevalidate.ToString()));
}
if (!Global.Cache.PhoenixFireCage.ContainsKey(cacheItem.Key))
{
// No phoenix yet, let the OutputCacheFilter created the phoenix and call the builder again
return null;
}
if (!cacheItem.AutoRefresh)
{
Global.Cache.PhoenixFireCage[cacheItem.Key].Reborn();
}
}
var response = request.CreateResponse();
if (cacheControl?.MinFresh?.TotalSeconds > ageInSeconds)
{
response.Headers.Add("X-Flatwhite-Warning", "Cache freshness lifetime not qualified");
cacheNotQualified = true;
}
if ((stale || cacheNotQualified) && !cacheItem.IgnoreRevalidationRequest)
{
return null;
}
if (stale)
{
request.Properties[WebApiExtensions.__webApi_cache_is_stale] = true;
response.Headers.Add("X-Flatwhite-Warning", "Response is Stale");
//https://tools.ietf.org/html/rfc7234#page-31
response.Headers.Add("Warning", $"110 - \"Response is Stale\"");
}
response.Headers.Age = TimeSpan.FromSeconds(ageInSeconds);
response.Headers.CacheControl = responseCacheControl;
if (request.Properties.ContainsKey(WebApiExtensions.__webApi_etag_matched))
{
response.StatusCode = HttpStatusCode.NotModified;
}
else
{
response.StatusCode = HttpStatusCode.OK;
response.Content = new ByteArrayContent(cacheItem.Content);
response.Content.Headers.ContentType = new MediaTypeHeaderValue(cacheItem.ResponseMediaType)
{
CharSet = cacheItem.ResponseCharSet
};
response.Headers.ETag = new EntityTagHeaderValue($"\"{cacheItem.Key}-{cacheItem.Checksum}\"");
}
return response;
//.........这里部分代码省略.........
示例6: IsLocal_NoMS_IsLocalProperty_False
public void IsLocal_NoMS_IsLocalProperty_False()
{
var request = new HttpRequestMessage();
Assert.IsFalse(request.IsLocal());
}
示例7: EnsureSecureConnection
protected virtual void EnsureSecureConnection(HttpRequestMessage request)
{
if (request == null)
{
throw new ArgumentNullException("request");
}
if (!request.IsLocal() && !request.RequestUri.IsHttps())
{
string msg = string.Format(CultureInfo.CurrentCulture, ReceiverResources.Receiver_NoHttps, GetType().Name, Uri.UriSchemeHttps);
request.GetConfiguration().DependencyResolver.GetLogger().Error(msg);
HttpResponseMessage noHttps = request.CreateErrorResponse(HttpStatusCode.BadRequest, msg);
throw new HttpResponseException(noHttps);
}
}