public void IsSupportedContentTypes_DetectsXhtmlInContentType()
        {
            string[] xhtmlContentTypes = new string[]
            {
                "application/xhtml+xml",
                "APPLICATION/xhTml+Xml",
                "application/xhtml+xml; charset=utf-8",
                "application/xhtml+xml; any parameter",
            };

            string[] nonXhtmlContentTypes = new string[]
            {
                "application/xml",
                "application/xhtml+xml charset=utf-8",
                null,
                "",
                "application/xhtm",
            };

            foreach (string xhtmlContentType in xhtmlContentTypes)
            {
                Assert.True(ContentTypeUtil.IsSupportedContentTypes(xhtmlContentType), xhtmlContentType);
            }

            foreach (string nonXhtmlContentType in nonXhtmlContentTypes)
            {
                Assert.False(ContentTypeUtil.IsSupportedContentTypes(nonXhtmlContentType), nonXhtmlContentType);
            }
        }
        public void IsSupportedContentTypes_DetectsTextHtmlInContentType()
        {
            string[] htmlContentTypes = new string[]
            {
                "text/html",
                "TEXT/hTmL",
                "text/html; charset=utf-8",
                "text/html; any parameter",
            };

            string[] nonHtmlContentTypes = new string[]
            {
                "text/htmlx",
                "text/html charset=utf-8",
                null,
                "",
                "text/htm",
            };

            foreach (string htmlContentType in htmlContentTypes)
            {
                Assert.True(ContentTypeUtil.IsSupportedContentTypes(htmlContentType), htmlContentType);
            }

            foreach (string nonHtmlContentType in nonHtmlContentTypes)
            {
                Assert.False(ContentTypeUtil.IsSupportedContentTypes(nonHtmlContentType), nonHtmlContentType);
            }
        }
        public void IsHtml_DetectsNonHtmlInBuffer()
        {
            // Arrange
            byte[] buffer = Encoding.ASCII.GetBytes("var j = 10;");

            // Act
            bool result = ContentTypeUtil.IsHtml("default.html", buffer, 0, buffer.Length);

            // Assert
            Assert.False(result);
        }
        public void IsHtml_DetectsHtmlInBuffer()
        {
            // Arrange
            byte[] buffer = Encoding.ASCII.GetBytes("<html><head></head><body></body></html>");

            // Act
            bool result = ContentTypeUtil.IsHtml("default.html", buffer, 0, buffer.Length);

            // Assert
            Assert.True(result);
        }
        private async Task ExecuteWithFilter(IHttpSocketAdapter injectScriptSocket, string requestId, HttpContext httpContext)
        {
            ScriptInjectionFilterContext filterContext = new ScriptInjectionFilterContext(httpContext);
            int currentPort = -1;

            PreprocessRequestHeader(httpContext, ref currentPort);

            RequestHeaders requestHeader = new RequestHeaders(httpContext.Request.Headers);

            if (currentPort == -1)
            {
                BrowserLinkMiddleWareUtil.RemoveETagAndTimeStamp(requestHeader);
            }

            using (ScriptInjectionFilterStream filter = new ScriptInjectionFilterStream(injectScriptSocket, filterContext))
            {
                httpContext.Response.Body = filter;
                httpContext.Response.OnStarting(delegate()
                {
                    if (ContentTypeUtil.IsSupportedContentTypes(httpContext.Response.ContentType))
                    {
                        httpContext.Response.ContentLength = null;
                    }

                    ResponseHeaders responseHeader = new ResponseHeaders(httpContext.Response.Headers);

                    BrowserLinkMiddleWareUtil.AddToETag(responseHeader, currentPort);

                    return(StaticTaskResult.True);
                });

                IHttpSendFileFeature originalSendFile = httpContext.Features.Get <IHttpSendFileFeature>();
                httpContext.Features.Set <IHttpSendFileFeature>(new SendFilesWrapper(originalSendFile, httpContext.Response));

                using (AddPageExecutionListenerFeatureTo(httpContext, requestId))
                {
                    await _next(httpContext);

                    await filter.WaitForFilterComplete();

                    if (filter.ScriptInjectionTimedOut)
                    {
                        _filterRequestTimeouts++;
                    }
                    else
                    {
                        _filterRequestTimeouts = 0;
                    }
                }
            }
        }
        public void IsHtml_DetectsHtmlInBufferWithOffset()
        {
            // Arrange
            byte[] buffer = Encoding.ASCII.GetBytes("<html><head></head><body></body></html>");
            byte[] bufferWithLeadingBytes = new byte[100 + buffer.Length];

            Array.Copy(buffer, 0, bufferWithLeadingBytes, 100, buffer.Length);

            // Act
            bool result = ContentTypeUtil.IsHtml("default.html", bufferWithLeadingBytes, 100, buffer.Length);

            // Assert
            Assert.True(result);
        }
        private void DetermineIfFilterShouldBePassthrough(byte[] buffer, int offset, int count)
        {
            string contentTypeFromHeader = _context.ResponseContentType;
            string path = _context.RequestPath;

            if (!ContentTypeUtil.IsSupportedContentTypes(contentTypeFromHeader))
            {
                CloseInjectScriptSocketAndBecomePassthrough();
            }

            else if (!ContentTypeUtil.IsHtml(path, buffer, offset, count))
            {
                CloseInjectScriptSocketAndBecomePassthrough();
            }
        }