Beispiel #1
0
        public bool IsIn
        (
            HttpHeaderValueCollection <MediaTypeWithQualityHeaderValue> httpHeaderValueCollection,
            out MediaTypeWithQualityHeaderValue mediaType
        )
        {
            mediaType = httpHeaderValueCollection.FirstOrDefault(n => n.MediaType.Equals(MimeType, StringComparison.InvariantCultureIgnoreCase));

            return(mediaType != null);
        }
        protected override async Task<AuthenticationHeaderValue> CreateAuthorizationHeaderAsync(HttpRequestMessage request, HttpHeaderValueCollection<AuthenticationHeaderValue> challenges, CancellationToken cancellationToken)
        {
            AuthenticationHeaderValue bearerChallenge = challenges.FirstOrDefault(IsOAuth2BearerChallenge);
            if (bearerChallenge != null)
            {
                string accessToken = await GetAccessTokenAsync(request);
                if (accessToken != null)
                {
                    return new AuthenticationHeaderValue(BearerScheme, accessToken);                 
                }
            }
 	        return null;
        }
Beispiel #3
0
        protected override async Task <AuthenticationHeaderValue> CreateAuthorizationHeaderAsync(HttpRequestMessage request, HttpHeaderValueCollection <AuthenticationHeaderValue> challenges, CancellationToken cancellationToken)
        {
            AuthenticationHeaderValue bearerChallenge = challenges.FirstOrDefault(IsOAuth2BearerChallenge);

            if (bearerChallenge != null)
            {
                string accessToken = await GetAccessTokenAsync(request);

                if (accessToken != null)
                {
                    return(new AuthenticationHeaderValue(BearerScheme, accessToken));
                }
            }
            return(null);
        }
        private object RenderImage(Photo photo, HttpHeaderValueCollection <MediaTypeWithQualityHeaderValue> types)
        {
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
            var type = types.FirstOrDefault(t => codecs.Any(c => c.MimeType == t.MediaType));

            if (type == null)
            {
                return(Request.CreateResponse(HttpStatusCode.NotAcceptable));
            }
            var path  = HostingEnvironment.MapPath($"~/App_Data/photos/{photo.Username}/");
            var files = Directory.GetFiles(path, $"{photo.PhotoId}.*");

            if (!files.Any())
            {
                return(NotFound());
            }

            var codec  = codecs.FirstOrDefault(c => c.MimeType == type.MediaType);
            var format = new ImageFormat(codec.FormatID);


            foreach (var file in files)
            {
                var pathToReturn = file;
                using (var bitmap = Image.FromFile(file)) {
                    var requestedType = type.MediaType;
                    var availableType = GetMimeType(bitmap.RawFormat);
                    if (type.MediaType != availableType)
                    {
                        pathToReturn = Path.Combine(path, photo.PhotoId + "." + codec.GetWritableFileExtension());
                        bitmap.Save(pathToReturn, format);
                    }
                    return(File(pathToReturn, type.MediaType));
                }
            }
            return(Request.CreateResponse(HttpStatusCode.NotAcceptable));
        }
Beispiel #5
0
        public static string GetAcceptHeaderValue(this HttpRequestMessage request)
        {
            HttpHeaderValueCollection <MediaTypeWithQualityHeaderValue> headers = request.Headers.Accept;

            return(headers.FirstOrDefault()?.MediaType);
        }
        /**************************************************************************/

        private void ProcessResponseHttpHeaders(MacroscopeHttpTwoClientResponse Response)
        {
            HttpResponseMessage ResponseMessage = Response.GetResponse();
            HttpResponseHeaders ResponseHeaders = ResponseMessage.Headers;
            HttpContentHeaders  ContentHeaders  = ResponseMessage.Content.Headers;

            /** Status Code ------------------------------------------------------ **/

            this.SetStatusCode(ResponseMessage.StatusCode);

            this.SetErrorCondition(ResponseMessage.ReasonPhrase);

            try
            {
                switch (this.GetStatusCode())
                {
                // 200 Range

                case HttpStatusCode.OK:
                    this.SetIsNotRedirect();
                    break;

                // 300 Range

                case HttpStatusCode.Moved:
                    this.SetErrorCondition(HttpStatusCode.Moved.ToString());
                    this.SetIsRedirect();
                    break;

                case HttpStatusCode.SeeOther:
                    this.SetErrorCondition(HttpStatusCode.SeeOther.ToString());
                    this.SetIsRedirect();
                    break;

                case HttpStatusCode.Found:
                    this.SetErrorCondition(HttpStatusCode.Redirect.ToString());
                    this.SetIsRedirect();
                    break;

                // 400 Range

                case HttpStatusCode.BadRequest:
                    this.SetErrorCondition(HttpStatusCode.BadRequest.ToString());
                    this.SetIsNotRedirect();
                    break;

                case HttpStatusCode.Unauthorized:
                    this.SetErrorCondition(HttpStatusCode.Unauthorized.ToString());
                    this.SetIsNotRedirect();
                    break;

                case HttpStatusCode.PaymentRequired:
                    this.SetErrorCondition(HttpStatusCode.PaymentRequired.ToString());
                    this.SetIsNotRedirect();
                    break;

                case HttpStatusCode.Forbidden:
                    this.SetErrorCondition(HttpStatusCode.Forbidden.ToString());
                    this.SetIsNotRedirect();
                    break;

                case HttpStatusCode.NotFound:
                    this.SetErrorCondition(HttpStatusCode.NotFound.ToString());
                    this.SetIsNotRedirect();
                    break;

                case HttpStatusCode.MethodNotAllowed:
                    this.SetErrorCondition(HttpStatusCode.MethodNotAllowed.ToString());
                    this.SetIsNotRedirect();
                    break;

                case HttpStatusCode.Gone:
                    this.SetErrorCondition(HttpStatusCode.Gone.ToString());
                    this.SetIsNotRedirect();
                    break;

                case HttpStatusCode.RequestUriTooLong:
                    this.SetErrorCondition(HttpStatusCode.RequestUriTooLong.ToString());
                    this.SetIsNotRedirect();
                    break;

                // Unhandled

                default:
                    throw new MacroscopeDocumentException("Unhandled HttpStatusCode Type");
                }
            }
            catch (MacroscopeDocumentException ex)
            {
                this.DebugMsg(string.Format("MacroscopeDocumentException: {0}", ex.Message));
            }

            /** Raw HTTP Headers ------------------------------------------------- **/

            this.SetHttpResponseStatusLine(Response: Response);

            this.SetHttpResponseHeaders(Response: Response);

            /** Server Information ----------------------------------------------- **/

            /*{
             * this.ServerName = ResponseHeaders.Server.First().ToString();
             * }*/

            /** PROBE HTTP HEADERS ----------------------------------------------- **/

            /** Server HTTP Header ----------------------------------------------- **/
            try
            {
                HttpHeaderValueCollection <ProductInfoHeaderValue> HeaderValue = ResponseHeaders.Server;
                if (HeaderValue != null)
                {
                    if (HeaderValue.FirstOrDefault() != null)
                    {
                        this.SetServerName(HeaderValue.FirstOrDefault().ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                this.DebugMsg(ex.Message);
                FindHttpResponseHeaderCallback Callback = delegate(IEnumerable <string> HeaderValues)
                {
                    this.SetServerName(HeaderValues.First().ToString());
                    return(true);
                };
                if (!this.FindHttpResponseHeader(ResponseHeaders: ResponseHeaders, HeaderName: "server", Callback: Callback))
                {
                    this.FindHttpContentHeader(ContentHeaders: ContentHeaders, HeaderName: "server", Callback: Callback);
                }
            }

            this.DebugMsg(string.Format("this.ServerName: {0}", this.ServerName));

            /** Content-Type HTTP Header ----------------------------------------- **/
            try
            {
                MediaTypeHeaderValue HeaderValue = ContentHeaders.ContentType;
                if (HeaderValue != null)
                {
                    this.DebugMsg(string.Format("HeaderValue: {0}", HeaderValue));
                    this.MimeType = HeaderValue.MediaType;
                    if (HeaderValue.CharSet != null)
                    {
                        this.SetCharacterSet(HeaderValue.CharSet);
                        // TODO: Implement character set probing
                        this.SetCharacterEncoding(NewEncoding: new UTF8Encoding());
                    }
                }
            }
            catch (Exception ex)
            {
                this.DebugMsg(string.Format("MediaType Exception: {0}", ex.Message));
                this.MimeType = MacroscopeConstants.DefaultMimeType;
            }

            this.DebugMsg(string.Format("this.MimeType: {0}", this.MimeType));

            /** Content-Length HTTP Header --------------------------------------- **/
            try
            {
                long?HeaderValue = null;
                if (ContentHeaders.Contains("Content-Length"))
                {
                    HeaderValue = ContentHeaders.ContentLength;
                }
                if (HeaderValue != null)
                {
                    this.ContentLength = HeaderValue;
                }
                else
                {
                    this.ContentLength = 0;
                }
            }
            catch (Exception ex)
            {
                this.DebugMsg(ex.Message);
                this.SetContentLength(Length: 0);
                FindHttpResponseHeaderCallback Callback = delegate(IEnumerable <string> HeaderValues)
                {
                    this.SetContentLength(Length: long.Parse(HeaderValues.FirstOrDefault()));
                    return(true);
                };
                if (!this.FindHttpResponseHeader(ResponseHeaders: ResponseHeaders, HeaderName: "content-length", Callback: Callback))
                {
                    this.FindHttpContentHeader(ContentHeaders: ContentHeaders, HeaderName: "content-length", Callback: Callback);
                }
            }

            this.DebugMsg(string.Format("this.GetContentLength(): {0}", this.GetContentLength()));

            /** Content-Encoding HTTP Header ------------------------------------- **/
            try
            {
                ICollection <string> HeaderValue = ContentHeaders.ContentEncoding;
                if (HeaderValue != null)
                {
                    this.ContentEncoding = HeaderValue.FirstOrDefault();
                }
            }
            catch (Exception ex)
            {
                this.DebugMsg(ex.Message);
                FindHttpResponseHeaderCallback Callback = delegate(IEnumerable <string> HeaderValues)
                {
                    this.ContentEncoding = HeaderValues.FirstOrDefault();
                    return(true);
                };
                if (!this.FindHttpResponseHeader(ResponseHeaders: ResponseHeaders, HeaderName: "content-encoding", Callback: Callback))
                {
                    this.FindHttpContentHeader(ContentHeaders: ContentHeaders, HeaderName: "content-encoding", Callback: Callback);
                }
            }

            if (string.IsNullOrEmpty(this.CompressionMethod) && (!string.IsNullOrEmpty(this.ContentEncoding)))
            {
                this.IsCompressed      = true;
                this.CompressionMethod = this.ContentEncoding;
            }

            this.DebugMsg(string.Format("this.ContentEncoding: {0}", this.ContentEncoding));
            this.DebugMsg(string.Format("this.CompressionMethod: {0}", this.CompressionMethod));

            /** Date HTTP Header ------------------------------------------------- **/
            try
            {
                DateTimeOffset?HeaderValue = ResponseHeaders.Date;
                if (HeaderValue != null)
                {
                    this.DateServer = MacroscopeDateTools.ParseHttpDate(DateString: HeaderValue.ToString());
                }
            }
            catch (Exception ex)
            {
                this.DebugMsg(ex.Message);
                this.DateServer = new DateTime();
                FindHttpResponseHeaderCallback Callback = delegate(IEnumerable <string> HeaderValues)
                {
                    this.DateServer = MacroscopeDateTools.ParseHttpDate(DateString: HeaderValues.First().ToString());
                    return(true);
                };
                if (!this.FindHttpResponseHeader(ResponseHeaders: ResponseHeaders, HeaderName: "date", Callback: Callback))
                {
                    this.FindHttpContentHeader(ContentHeaders: ContentHeaders, HeaderName: "date", Callback: Callback);
                }
            }

            this.DebugMsg(string.Format("this.DateServer: {0}", this.DateServer));

            /** Last-Modified HTTP Header ---------------------------------------- **/
            try
            {
                DateTimeOffset?HeaderValue = ContentHeaders.LastModified;
                if (HeaderValue != null)
                {
                    this.DateModified = MacroscopeDateTools.ParseHttpDate(DateString: HeaderValue.ToString());
                }
            }
            catch (Exception ex)
            {
                this.DebugMsg(ex.Message);
                this.DateModified = new DateTime();
                FindHttpResponseHeaderCallback Callback = delegate(IEnumerable <string> HeaderValues)
                {
                    this.DateModified = MacroscopeDateTools.ParseHttpDate(DateString: HeaderValues.First().ToString());
                    return(true);
                };
                if (!this.FindHttpResponseHeader(ResponseHeaders: ResponseHeaders, HeaderName: "last-modified", Callback: Callback))
                {
                    this.FindHttpContentHeader(ContentHeaders: ContentHeaders, HeaderName: "last-modified", Callback: Callback);
                }
            }

            this.DebugMsg(string.Format("this.DateModified: {0}", this.DateModified));

            /** Expires HTTP Header ---------------------------------------------- **/
            try
            {
                DateTimeOffset?HeaderValue = ContentHeaders.Expires;
                if (HeaderValue != null)
                {
                    this.DateExpires = MacroscopeDateTools.ParseHttpDate(DateString: HeaderValue.ToString());
                }
            }
            catch (Exception ex)
            {
                this.DebugMsg(ex.Message);
                this.DateExpires = new DateTime();
                FindHttpResponseHeaderCallback Callback = delegate(IEnumerable <string> HeaderValues)
                {
                    this.DateExpires = MacroscopeDateTools.ParseHttpDate(DateString: HeaderValues.First().ToString());
                    return(true);
                };
                if (!this.FindHttpResponseHeader(ResponseHeaders: ResponseHeaders, HeaderName: "expires", Callback: Callback))
                {
                    this.FindHttpContentHeader(ContentHeaders: ContentHeaders, HeaderName: "expires", Callback: Callback);
                }
            }

            this.DebugMsg(string.Format("this.DateExpires: {0}", this.DateExpires));

            /** HTST Policy HTTP Header ------------------------------------------ **/
            // https://www.owasp.org/index.php/HTTP_Strict_Transport_Security_Cheat_Sheet
            // Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
            {
                FindHttpResponseHeaderCallback Callback = delegate(IEnumerable <string> HeaderValues)
                {
                    this.HypertextStrictTransportPolicy = true;
                    return(true);
                };
                if (!this.FindHttpResponseHeader(ResponseHeaders: ResponseHeaders, HeaderName: "strict-transport-security", Callback: Callback))
                {
                    this.FindHttpContentHeader(ContentHeaders: ContentHeaders, HeaderName: "strict-transport-security", Callback: Callback);
                }
            }

            this.DebugMsg(string.Format("this.HypertextStrictTransportPolicy: {0}", this.HypertextStrictTransportPolicy));

            /** Location (Redirect) HTTP Header ---------------------------------- **/
            try
            {
                Uri HeaderValue = ResponseHeaders.Location;
                if (HeaderValue != null)
                {
                    this.SetUrlRedirectTo(Url: HeaderValue.ToString());
                }
            }
            catch (Exception ex)
            {
                this.DebugMsg(ex.Message);
                FindHttpResponseHeaderCallback Callback = delegate(IEnumerable <string> HeaderValues)
                {
                    this.SetUrlRedirectTo(Url: HeaderValues.FirstOrDefault().ToString());
                    return(true);
                };
                if (!this.FindHttpResponseHeader(ResponseHeaders: ResponseHeaders, HeaderName: "location", Callback: Callback))
                {
                    this.FindHttpContentHeader(ContentHeaders: ContentHeaders, HeaderName: "location", Callback: Callback);
                }
            }

            this.DebugMsg(string.Format("this.GetIsRedirect(): {0}", this.GetIsRedirect()));
            this.DebugMsg(string.Format("this.GetUrlRedirectTo(): {0}", this.GetUrlRedirectTo()));

            /** Link HTTP Headers ------------------------------------------------ **/
            {
                FindHttpResponseHeaderCallback Callback = delegate(IEnumerable <string> HeaderValues)
                {
                    foreach (string HeaderValue in HeaderValues)
                    {
                        this.DebugMsg(string.Format("HeaderValue: {0}", HeaderValue));
                        this.ProcessHttpLinkHeader(HttpLinkHeader: HeaderValue);
                    }
                    return(true);
                };
                if (!this.FindHttpResponseHeader(ResponseHeaders: ResponseHeaders, HeaderName: "link", Callback: Callback))
                {
                    this.FindHttpContentHeader(ContentHeaders: ContentHeaders, HeaderName: "link", Callback: Callback);
                }
            }

            /** ETag HTTP Header ------------------------------------------------- **/
            try
            {
                EntityTagHeaderValue HeaderValue = ResponseHeaders.ETag;
                if (HeaderValue != null)
                {
                    string ETagValue = HeaderValue.Tag;
                    if (!string.IsNullOrEmpty(ETagValue))
                    {
                        this.SetEtag(HeaderValue.Tag);
                    }
                }
            }
            catch (Exception ex)
            {
                this.DebugMsg(ex.Message);
                FindHttpResponseHeaderCallback Callback = delegate(IEnumerable <string> HeaderValues)
                {
                    string HeaderValue = HeaderValues.FirstOrDefault();
                    if (HeaderValue != null)
                    {
                        if (!string.IsNullOrEmpty(HeaderValue))
                        {
                            this.SetEtag(HeaderValue);
                        }
                    }
                    return(true);
                };
                if (!this.FindHttpResponseHeader(ResponseHeaders: ResponseHeaders, HeaderName: "etag", Callback: Callback))
                {
                    this.FindHttpContentHeader(ContentHeaders: ContentHeaders, HeaderName: "etag", Callback: Callback);
                }
            }

            this.DebugMsg(string.Format("this.Etag: {0}", this.Etag));

            /** WWW-AUTHENTICATE HTTP Header ------------------------------------- **/
            // Reference: http://httpbin.org/basic-auth/user/passwd
            try
            {
                HttpHeaderValueCollection <AuthenticationHeaderValue> HeaderValue = ResponseHeaders.WwwAuthenticate;
                if (HeaderValue != null)
                {
                    string Scheme = null;
                    string Realm  = null;
                    foreach (AuthenticationHeaderValue AuthenticationValue in HeaderValue)
                    {
                        Scheme = AuthenticationValue.Scheme;
                        string Parameter = AuthenticationValue.Parameter;
                        Match  Matched   = Regex.Match(Parameter, "^[^\"]+\"([^\"]+)\"");
                        if (Matched.Success)
                        {
                            Realm = Matched.Groups[1].Value;
                        }
                    }
                    if (!string.IsNullOrEmpty(Scheme) && !string.IsNullOrEmpty(Realm))
                    {
                        if (Scheme.ToLower() == "basic")
                        {
                            this.SetAuthenticationType(MacroscopeConstants.AuthenticationType.BASIC);
                            this.SetAuthenticationRealm(Realm);
                        }
                        else
                        {
                            this.SetAuthenticationType(MacroscopeConstants.AuthenticationType.UNSUPPORTED);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                this.DebugMsg(ex.Message);
            }
            this.DebugMsg(string.Format("WwwAuthenticate: \"{0}\", Realm: \"{1}\"", this.GetAuthenticationType(), this.GetAuthenticationRealm()));

            /** Process Dates ---------------------------------------------------- **/
            {
                if (this.DateServer.Date == new DateTime().Date)
                {
                    this.DateServer = DateTime.UtcNow;
                }
                if (this.DateModified.Date == new DateTime().Date)
                {
                    this.DateModified = this.DateServer;
                }
            }

            /** Process MIME Type ------------------------------------------------ **/
            {
                Regex reIsHtml       = new Regex(@"^(text/html|application/xhtml+xml)", RegexOptions.IgnoreCase);
                Regex reIsCss        = new Regex(@"^text/css", RegexOptions.IgnoreCase);
                Regex reIsJavascript = new Regex(@"^(application/javascript|text/javascript)", RegexOptions.IgnoreCase);
                Regex reIsImage      = new Regex(@"^image/(gif|png|jpeg|bmp|webp|vnd.microsoft.icon|x-icon)", RegexOptions.IgnoreCase);
                Regex reIsPdf        = new Regex(@"^application/pdf", RegexOptions.IgnoreCase);
                Regex reIsAudio      = new Regex(@"^audio/[a-z0-9]+", RegexOptions.IgnoreCase);
                Regex reIsVideo      = new Regex(@"^video/[a-z0-9]+", RegexOptions.IgnoreCase);
                Regex reIsXml        = new Regex(@"^(application|text)/(atom\+xml|xml)", RegexOptions.IgnoreCase);
                Regex reIsText       = new Regex(@"^(text)/(plain)", RegexOptions.IgnoreCase);

                if (reIsHtml.IsMatch(this.MimeType))
                {
                    this.SetDocumentType(Type: MacroscopeConstants.DocumentType.HTML);
                }
                else
                if (reIsCss.IsMatch(this.MimeType))
                {
                    this.SetDocumentType(Type: MacroscopeConstants.DocumentType.CSS);
                }
                else
                if (reIsJavascript.IsMatch(this.MimeType))
                {
                    this.SetDocumentType(Type: MacroscopeConstants.DocumentType.JAVASCRIPT);
                }
                else
                if (reIsImage.IsMatch(this.MimeType))
                {
                    this.SetDocumentType(Type: MacroscopeConstants.DocumentType.IMAGE);
                }
                else
                if (reIsPdf.IsMatch(this.MimeType))
                {
                    this.SetDocumentType(Type: MacroscopeConstants.DocumentType.PDF);
                }
                else
                if (reIsAudio.IsMatch(this.MimeType))
                {
                    this.SetDocumentType(Type: MacroscopeConstants.DocumentType.AUDIO);
                }
                else
                if (reIsVideo.IsMatch(this.MimeType))
                {
                    this.SetDocumentType(Type: MacroscopeConstants.DocumentType.VIDEO);
                }
                else
                if (reIsXml.IsMatch(this.MimeType))
                {
                    this.SetDocumentType(Type: MacroscopeConstants.DocumentType.XML);
                }
                else
                if (reIsText.IsMatch(this.MimeType))
                {
                    this.SetDocumentType(Type: MacroscopeConstants.DocumentType.TEXT);
                }
                else
                {
                    this.SetDocumentType(Type: MacroscopeConstants.DocumentType.BINARY);
                }
            }

            /** Process Cookies -------------------------------------------------- **/
            // https://stackoverflow.com/questions/29224734/how-to-read-cookies-from-httpresponsemessage
            {
                try
                {
                    CookieContainer  CookieMonster = MacroscopeHttpTwoClient.GetCookieMonster();
                    CookieCollection Biscuits      = CookieMonster.GetCookies(uri: this.GetUri());
                    this.AddCookies(Cookies: Biscuits);
                    this.DebugMsg("cookies");


//          CookieContainer CookieTin = MacroscopeHttpTwoClient.GetCookieMonster();
//          string LimpBizkit = tin.GetCookieHeader( uri: Request.RequestUri );
                }
                catch (Exception ex)
                {
                    this.DebugMsg(ex.Message);
                }
            }

            return;
        }