當前位置: 首頁>>代碼示例>>C#>>正文


C# HttpRequestMessage.IsLocal方法代碼示例

本文整理匯總了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());
        }
開發者ID:HKV309,項目名稱:HelperSharp,代碼行數:7,代碼來源:HttpRequestMessageExtensionsTest.cs

示例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());
        }
開發者ID:HKV309,項目名稱:HelperSharp,代碼行數:7,代碼來源:HttpRequestMessageExtensionsTest.cs

示例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);
        }
開發者ID:nvdvlies,項目名稱:MileageTracker.AspNetWebApi,代碼行數:16,代碼來源:RequireHttpsHandler.cs

示例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;
    }
開發者ID:aspirinonfire,項目名稱:goalcoach,代碼行數:21,代碼來源:SSLHandler.cs

示例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;
//.........這裏部分代碼省略.........
開發者ID:vanthoainguyen,項目名稱:Flatwhite,代碼行數:101,代碼來源:CacheResponseBuilder.cs

示例6: IsLocal_NoMS_IsLocalProperty_False

 public void IsLocal_NoMS_IsLocalProperty_False()
 {
     var request = new HttpRequestMessage();
     Assert.IsFalse(request.IsLocal());
 }
開發者ID:HKV309,項目名稱:HelperSharp,代碼行數:5,代碼來源:HttpRequestMessageExtensionsTest.cs

示例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);
            }
        }
開發者ID:Joshzx,項目名稱:WebHooks,代碼行數:15,代碼來源:WebHookReceiver.cs


注:本文中的System.Net.Http.HttpRequestMessage.IsLocal方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。