public static async Task InvokeAsync(HttpContext context, CssFileWatcherService cssFileWatcherService)
        {
            if (!HttpMethods.IsPost(context.Request.Method))
            {
                context.Response.StatusCode = (int)HttpStatusCode.MethodNotAllowed;
                return;
            }
            if (!context.Request.ContentType.Split(';').Select(s => s.Trim()).Any(s => s == "application/json"))
            {
                context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return;
            }

            var option = new JsonSerializerOptions {
                PropertyNameCaseInsensitive = true
            };
            var requestArgs = await JsonSerializer.DeserializeAsync <CssWatchRequestArgs>(context.Request.Body, option);

            foreach (var href in requestArgs.Hrefs ?? Enumerable.Empty <string>())
            {
                cssFileWatcherService.TryAddWatch(href, requestArgs.ConnectedOnce, requestArgs.LastReloadedTime);
            }

            context.Response.StatusCode = (int)HttpStatusCode.NoContent;
        }
Beispiel #2
0
        public async Task InvokeAsync(HttpContext context)
        {
            await _next(context);

            var httpStatus = (HttpStatusCode)context.Response.StatusCode;

            if (httpStatus != HttpStatusCode.OK && httpStatus != HttpStatusCode.NotModified)
            {
                return;
            }
            if (context.Response.ContentType.Split(',', ';', ' ').FirstOrDefault() != "text/css")
            {
                return;
            }
            if (!HttpMethods.IsGet(context.Request.Method) && !HttpMethods.IsHead(context.Request.Method))
            {
                return;
            }

            var url = context.Request.GetDisplayUrl();

            url = Regex.Replace(url, @"(\?|&)(136bb8a9-b749-47e9-92e7-8b46e4a4f657=\d+&?)", "$1").TrimEnd('?', '&');
            _cssFileWatcherService.TryAddWatch(url);
        }