// Token: 0x060018D8 RID: 6360 RVA: 0x000560A4 File Offset: 0x000542A4
        protected string GetImageUrlAbsolutePath(string imageUrl)
        {
            if (imageUrl == null)
            {
                return(imageUrl);
            }
            if (imageUrl.StartsWith("//"))
            {
                this.logger.Set(GetLinkPreviewMetadata.InvalidImageUrl, imageUrl);
                return(null);
            }
            Uri uri;

            if (!Uri.TryCreate(imageUrl, UriKind.RelativeOrAbsolute, out uri))
            {
                GetLinkPreview.ThrowInvalidRequestException("InvalidImageUrl", string.Format("Image url {0} is invalid.", imageUrl));
            }
            if (!uri.IsAbsoluteUri)
            {
                UriBuilder uriBuilder = new UriBuilder(this.responseUri.Scheme, this.responseUri.Host);
                if (Uri.TryCreate(uriBuilder.Uri, imageUrl, out uri))
                {
                    imageUrl = uri.ToString();
                }
                else
                {
                    GetLinkPreview.ThrowInvalidRequestException("InvalidImageUrl", string.Format("Image url {0} is invalid.", imageUrl));
                }
            }
            if (imageUrl != null && imageUrl.Length > 500)
            {
                GetLinkPreview.ThrowInvalidRequestException("MaxImageUrlLengthExceeded", string.Format("Image url length {0} exceeds the maximum length allowed.", imageUrl.Length));
            }
            return(imageUrl);
        }
Esempio n. 2
0
        // Token: 0x060018F0 RID: 6384 RVA: 0x00056B30 File Offset: 0x00054D30
        public static void ThrowInvalidRequestException(string error, string errorMessage)
        {
            OwaInvalidRequestException ex = new OwaInvalidRequestException(errorMessage);

            GetLinkPreview.SetErrorData(ex, error);
            throw ex;
        }
        private static Uri CreateUri(string url)
        {
            Uri uri = null;

            if (Uri.TryCreate(url, UriKind.Absolute, out uri))
            {
                string text = null;
                try
                {
                    IdnMapping idnMapping = new IdnMapping();
                    text = idnMapping.GetAscii(uri.Host);
                }
                catch (ArgumentException)
                {
                    GetLinkPreview.ThrowInvalidRequestException("InvalidUrl", "Request url is invalid");
                }
                if (string.CompareOrdinal(text, uri.Host) != 0)
                {
                    uri = new UriBuilder(uri)
                    {
                        Host = text
                    }.Uri;
                }
                return(uri);
            }
            GetLinkPreview.ThrowInvalidRequestException("InvalidUrl", "Request url is invalid");
            return(uri);
        }
Esempio n. 4
0
        // Token: 0x060018F6 RID: 6390 RVA: 0x00056C38 File Offset: 0x00054E38
        private GetLinkPreviewResponse CreateErrorResponse(Exception exception)
        {
            string error            = (exception.Data["ErrorKey"] != null) ? exception.Data["ErrorKey"].ToString() : exception.GetType().Name;
            string exceptionMessage = GetLinkPreview.GetExceptionMessage(exception);

            return(this.CreateErrorResponse(error, exceptionMessage));
        }
Esempio n. 5
0
        // Token: 0x060018CE RID: 6350 RVA: 0x00055CC0 File Offset: 0x00053EC0
        protected static string ConvertToSafeHtml(string html)
        {
            string text = null;

            if (html != null)
            {
                HtmlToHtml htmlToHtml = new HtmlToHtml();
                htmlToHtml.FilterHtml         = true;
                htmlToHtml.OutputHtmlFragment = true;
                using (TextReader textReader = new StringReader(html))
                {
                    using (TextWriter textWriter = new StringWriter())
                    {
                        try
                        {
                            htmlToHtml.Convert(textReader, textWriter);
                            text = textWriter.ToString().Trim();
                            if (text.StartsWith("<div>", StringComparison.OrdinalIgnoreCase))
                            {
                                text = text.Substring("<div>".Length, text.Length - "<div>".Length - "</div>".Length);
                            }
                        }
                        catch (ExchangeDataException localizedException)
                        {
                            GetLinkPreview.ThrowLocalizedException("HtmlConversionFailed", localizedException);
                        }
                    }
                }
            }
            return(text);
        }
Esempio n. 6
0
 // Token: 0x06001952 RID: 6482 RVA: 0x00058395 File Offset: 0x00056595
 protected override void RestrictContentLength(long?contentLength)
 {
     if (contentLength != null && contentLength.Value > 524288L)
     {
         GetLinkPreview.ThrowInvalidRequestException("MaxContentLengthExceeded", string.Format("Content length {0} exceeds maximum size allowed.", contentLength.Value));
     }
 }
Esempio n. 7
0
        // Token: 0x060018EB RID: 6379 RVA: 0x0005698C File Offset: 0x00054B8C
        protected override async Task <GetLinkPreviewResponse> InternalExecute()
        {
            GetLinkPreviewResponse getLinkPreviewResponse;

            try
            {
                DataProviderInformation dataProviderInformation = null;
                long elapsedTimeToWebPageStepCompletion         = 0L;
                long elapsedTimeToRegExStepCompletion           = 0L;
                base.CallContext.ProtocolLog.Set(GetLinkPreviewMetadata.Url, this.request.Url);
                if (GetLinkPreview.GetPreviewsDisabled())
                {
                    return(this.CreateDisabledResponse());
                }
                if (Interlocked.Increment(ref GetLinkPreview.getPreviewRequestCount) > GetLinkPreview.getPreviewRequestCountMax)
                {
                    return(this.CreateErrorResponse("MaxConcurrentRequestExceeded", "The maximum number of concurrent requests has been exceeded."));
                }
                bool      activeViewConvergenceEnabled = GetLinkPreview.GetActiveViewsConvergenceFlightEnabled(base.CallContext.ProtocolLog);
                Stopwatch stopwatch = Stopwatch.StartNew();
                LinkPreviewDataProvider dataProvider = null;
                dataProvider            = LinkPreviewDataProvider.GetDataProvider(this.request, base.CallContext.ProtocolLog, activeViewConvergenceEnabled);
                dataProviderInformation = await dataProvider.GetDataProviderInformation();

                elapsedTimeToWebPageStepCompletion = stopwatch.ElapsedMilliseconds;
                getLinkPreviewResponse             = dataProvider.CreatePreview(dataProviderInformation);
                stopwatch.Stop();
                elapsedTimeToRegExStepCompletion = stopwatch.ElapsedMilliseconds;
                getLinkPreviewResponse.ElapsedTimeToWebPageStepCompletion = elapsedTimeToWebPageStepCompletion;
                getLinkPreviewResponse.ElapsedTimeToRegExStepCompletion   = elapsedTimeToRegExStepCompletion;
                getLinkPreviewResponse.WebPageContentLength = dataProvider.ContentLength;
                this.LogWebMethodData(getLinkPreviewResponse);
            }
            catch (OwaPermanentException exception)
            {
                getLinkPreviewResponse = this.CreateErrorResponse(exception);
            }
            catch (LocalizedException exception2)
            {
                getLinkPreviewResponse = this.CreateErrorResponse(exception2);
            }
            catch (HttpRequestException requestException)
            {
                getLinkPreviewResponse = this.CreateErrorResponse(requestException);
            }
            catch (TaskCanceledException)
            {
                getLinkPreviewResponse = this.CreateErrorResponse("RequestTimeout", "The web page request timed out.");
            }
            catch (WebException webException)
            {
                getLinkPreviewResponse = this.CreateErrorResponse(webException);
            }
            finally
            {
                Interlocked.Decrement(ref GetLinkPreview.getPreviewRequestCount);
            }
            return(getLinkPreviewResponse);
        }
Esempio n. 8
0
        // Token: 0x060018EF RID: 6383 RVA: 0x00056AFC File Offset: 0x00054CFC
        public static void ThrowInvalidRequestException(HttpResponseMessage responseMessage)
        {
            string error                  = responseMessage.StatusCode.ToString();
            string reasonPhrase           = responseMessage.ReasonPhrase;
            OwaInvalidRequestException ex = new OwaInvalidRequestException(reasonPhrase);

            GetLinkPreview.SetErrorData(ex, error);
            throw ex;
        }
Esempio n. 9
0
        private static Uri GetOEmbedRequestUri(Uri uri)
        {
            string oembedQueryForUri = OEmbedVideoPreviewBuilder.GetOEmbedQueryForUri(uri);

            if (oembedQueryForUri != null)
            {
                return(new Uri(oembedQueryForUri));
            }
            GetLinkPreview.ThrowInvalidRequestException("OEmbedQueryStringNotFound", string.Format("Could not get OEmbed query string for url {0}", uri.AbsoluteUri));
            return(null);
        }
Esempio n. 10
0
        // Token: 0x060018F4 RID: 6388 RVA: 0x00056B88 File Offset: 0x00054D88
        private GetLinkPreviewResponse CreateErrorResponse(HttpRequestException requestException)
        {
            WebException ex = requestException.InnerException as WebException;

            if (ex != null)
            {
                return(this.CreateErrorResponse(ex));
            }
            string name             = requestException.GetType().Name;
            string exceptionMessage = GetLinkPreview.GetExceptionMessage(requestException);

            return(this.CreateErrorResponse(name, exceptionMessage));
        }
Esempio n. 11
0
        // Token: 0x06001951 RID: 6481 RVA: 0x00058324 File Offset: 0x00056524
        protected override void RequireContentType(MediaTypeHeaderValue contentType)
        {
            if (contentType == null || string.IsNullOrWhiteSpace(contentType.MediaType))
            {
                GetLinkPreview.ThrowInvalidRequestException("UnsupportedContentType", string.Format("Content type {0} is not supported.", "null"));
            }
            string text = contentType.MediaType.ToLower();

            if (!text.Contains("text/html") && !text.Contains("application/xhtml+xml"))
            {
                GetLinkPreview.ThrowInvalidRequestException("UnsupportedContentType", string.Format("Content type {0} is not supported.", text));
            }
        }
Esempio n. 12
0
        // Token: 0x060018CD RID: 6349 RVA: 0x00055C7C File Offset: 0x00053E7C
        protected static MatchCollection ExecuteRegExForMatchCollection(Regex regEx, string matchString, string propertyName)
        {
            MatchCollection result = null;

            try
            {
                result = regEx.Matches(matchString);
            }
            catch (RegexMatchTimeoutException)
            {
                GetLinkPreview.ThrowInvalidRequestException("RegExTimeout", string.Format("The regex timed out on property {0}.", propertyName));
            }
            return(result);
        }
Esempio n. 13
0
        protected override string GetImage(out int imageTagCount)
        {
            string thumbnailUrl = this.oEmbedResponse.ThumbnailUrl;

            if (thumbnailUrl != null)
            {
                if (thumbnailUrl.Length > 500)
                {
                    GetLinkPreview.ThrowInvalidRequestException("MaxImageUrlLengthExceeded", string.Format("Image url length {0} exceeds the maximum length allowed.", thumbnailUrl.Length));
                }
                imageTagCount = 1;
                return(thumbnailUrl);
            }
            imageTagCount = 0;
            return(null);
        }
Esempio n. 14
0
        // Token: 0x0600194E RID: 6478 RVA: 0x0005818C File Offset: 0x0005638C
        private static string GetString(Encoding encoding, byte[] bytes)
        {
            string result = null;

            try
            {
                result = encoding.GetString(bytes);
            }
            catch (DecoderFallbackException ex)
            {
                GetLinkPreview.ThrowInvalidRequestException("EncodingGetStringFailed", string.Format("Encoding {0} failed with {1}", encoding.EncodingName, ex.Message));
            }
            catch (ArgumentException ex2)
            {
                GetLinkPreview.ThrowInvalidRequestException("EncodingGetStringFailed", string.Format("Encoding {0} failed with {1}", encoding.EncodingName, ex2.Message));
            }
            return(result);
        }
        private Encoding GetResponseHeaderEncoding(string characterSet)
        {
            Encoding result = null;

            if (characterSet != null)
            {
                try
                {
                    return(Encoding.GetEncoding(characterSet));
                }
                catch (ArgumentException)
                {
                    GetLinkPreview.ThrowInvalidRequestException("GetEncodingFailed", string.Format("Get encoding failed for {0}", characterSet));
                    return(result);
                }
            }
            result = Encoding.GetEncoding("ISO-8859-1");
            return(result);
        }
Esempio n. 16
0
        protected static DataProviderInformation ProcessOEmbedResponseStream(Uri responseUri, Encoding responseHeaderEncoding, MemoryStream memoryStream, RequestDetailsLogger logger)
        {
            memoryStream.Position = 0L;
            OEmbedResponse oembedResponse = null;

            try
            {
                DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(OEmbedResponse));
                oembedResponse = (OEmbedResponse)dataContractJsonSerializer.ReadObject(memoryStream);
            }
            catch (SerializationException ex)
            {
                GetLinkPreview.ThrowInvalidRequestException("OEmbedResponseSerializationReadFailed", string.Format("Failed to read the OEmbed response object. Error {0}", ex.Message));
            }
            return(new OEmbedInformation
            {
                Text = null,
                ResponseUri = responseUri,
                OEmbedResponse = oembedResponse
            });
        }
        protected async Task <DataProviderInformation> MakeAndProcessHttpRequest(HttpClient httpClient, Uri requestUri, LinkPreviewDataProvider.ProcessResponseStreamDelegate processResponseStream)
        {
            DataProviderInformation dataProviderInformation;

            using (HttpResponseMessage httpResponseMessage = await httpClient.GetAsync(requestUri, HttpCompletionOption.ResponseHeadersRead))
            {
                if (!httpResponseMessage.IsSuccessStatusCode)
                {
                    GetLinkPreview.ThrowInvalidRequestException(httpResponseMessage);
                }
                this.RequireContentType(httpResponseMessage.Content.Headers.ContentType);
                this.RestrictContentLength(httpResponseMessage.Content.Headers.ContentLength);
                this.ContentLength = ((httpResponseMessage.Content.Headers.ContentLength != null) ? httpResponseMessage.Content.Headers.ContentLength.Value : 0L);
                Encoding responseHeaderEncoding = this.GetResponseHeaderEncoding(httpResponseMessage.Content.Headers.ContentType.CharSet);
                Uri      responseUri            = httpResponseMessage.RequestMessage.RequestUri;
                int      maxByteCount           = this.GetMaxByteCount(responseUri);
                using (MemoryStream memoryStream = new MemoryStream(maxByteCount))
                {
                    using (Stream responseStream = await httpResponseMessage.Content.ReadAsStreamAsync())
                    {
                        byte[] buffer         = new byte[1024];
                        int    readCount      = 0;
                        int    totalReadCount = 0;
                        do
                        {
                            readCount = await responseStream.ReadAsync(buffer, 0, buffer.Length);

                            memoryStream.Write(buffer, 0, readCount);
                            totalReadCount += readCount;
                        }while (readCount > 0 && totalReadCount < maxByteCount);
                        if (totalReadCount <= 0)
                        {
                            GetLinkPreview.ThrowInvalidRequestException("EmptyContent", "Url returns no content.");
                        }
                    }
                    dataProviderInformation = processResponseStream(responseUri, responseHeaderEncoding, memoryStream, this.logger);
                }
            }
            return(dataProviderInformation);
        }
Esempio n. 18
0
        // Token: 0x060018F5 RID: 6389 RVA: 0x00056BC8 File Offset: 0x00054DC8
        private GetLinkPreviewResponse CreateErrorResponse(WebException webException)
        {
            string error;

            if (webException.Status == WebExceptionStatus.ProtocolError)
            {
                string          str             = null;
                HttpWebResponse httpWebResponse = webException.Response as HttpWebResponse;
                if (httpWebResponse != null)
                {
                    str = ((int)httpWebResponse.StatusCode).ToString();
                }
                error = webException.Status.ToString() + str;
            }
            else
            {
                error = webException.Status.ToString();
            }
            string exceptionMessage = GetLinkPreview.GetExceptionMessage(webException);

            return(this.CreateErrorResponse(error, exceptionMessage));
        }
Esempio n. 19
0
        protected override void SetAdditionalProperties(LinkPreview linkPreview)
        {
            if (this.oEmbedResponse == null)
            {
                GetLinkPreview.ThrowInvalidRequestException("OEmbedResponseNull", string.Format("The OEmbedResponse was null for the webpage information for {0}", this.responseUri.AbsoluteUri));
            }
            if (this.oEmbedResponse.Html == null)
            {
                GetLinkPreview.ThrowInvalidRequestException("OEmbedResponseHtmlNull", string.Format("The OEmbedResponse HTML was null for the webpage information for {0}", this.responseUri.AbsoluteUri));
            }
            string text = this.oEmbedResponse.Html;

            if (this.oEmbedResponse.ProviderName.Equals("YouTube") || this.oEmbedResponse.ProviderName.Equals("Dailymotion"))
            {
                text = text.Replace("http://", "https://");
            }
            else if (this.oEmbedResponse.ProviderName.Equals("Hulu"))
            {
                text = text.Replace("http://www.hulu.com", "https://secure.hulu.com");
            }
            ((OEmbedVideoPreview)linkPreview).EmbeddedHtml = text;
        }
        // Token: 0x060018D0 RID: 6352 RVA: 0x00055DB0 File Offset: 0x00053FB0
        internal override GetLinkPreviewResponse Execute()
        {
            GetLinkPreviewResponse getLinkPreviewResponse = new GetLinkPreviewResponse();
            LinkPreview            linkPreview            = this.CreateLinkPreviewInstance();

            linkPreview.Id  = this.id;
            linkPreview.Url = this.url;
            linkPreview.RequestStartTimeMilliseconds = this.requestStartTimeMilliseconds;
            int imageTagCount;

            linkPreview.ImageUrl = this.GetImage(out imageTagCount);
            linkPreview.Title    = this.GetTitle();
            int descriptionTagCount;

            linkPreview.Description = this.GetDescription(out descriptionTagCount);
            linkPreview.IsVideo     = base.IsVideo;
            if (string.IsNullOrWhiteSpace(linkPreview.Title))
            {
                if (string.IsNullOrWhiteSpace(linkPreview.Description))
                {
                    GetLinkPreview.ThrowInvalidRequestException("TitleAndDescriptionNotFound", "No title or description were found.");
                }
                else if (string.IsNullOrWhiteSpace(linkPreview.ImageUrl))
                {
                    GetLinkPreview.ThrowInvalidRequestException("TitleAndImageNotFound", "No title or image were found.");
                }
            }
            else if (string.IsNullOrWhiteSpace(linkPreview.Description) && string.IsNullOrWhiteSpace(linkPreview.ImageUrl))
            {
                GetLinkPreview.ThrowInvalidRequestException("DescriptionAndImageNotFound", "No description or image were found.");
            }
            this.SetAdditionalProperties(linkPreview);
            getLinkPreviewResponse.LinkPreview         = linkPreview;
            getLinkPreviewResponse.ImageTagCount       = imageTagCount;
            getLinkPreviewResponse.DescriptionTagCount = descriptionTagCount;
            return(getLinkPreviewResponse);
        }
Esempio n. 21
0
        // Token: 0x0600194F RID: 6479 RVA: 0x00058208 File Offset: 0x00056408
        private static Encoding GetWebPageEncoding(string webPageString, RequestDetailsLogger logger)
        {
            Encoding  result    = null;
            string    text      = null;
            int       num       = 0;
            Stopwatch stopwatch = Stopwatch.StartNew();

            text = WebPageDataProvider.GetWebPageEncoding(webPageString, WebPageDataProvider.GetXmlEncodingRegEx, "xml encoding");
            num++;
            if (text == null)
            {
                text = WebPageDataProvider.GetWebPageEncoding(webPageString, WebPageDataProvider.GetMetaEncodingRegEx, "meta encoding");
                num++;
            }
            if (text == null)
            {
                text = WebPageDataProvider.GetWebPageEncoding(webPageString, WebPageDataProvider.GetMeta5EncodingRegEx, "meta encoding");
                num++;
            }
            if (text != null)
            {
                try
                {
                    result = Encoding.GetEncoding(text);
                }
                catch (ArgumentException)
                {
                    GetLinkPreview.ThrowInvalidRequestException("GetEncodingFailed", string.Format("Get encoding failed for {0}", text));
                }
            }
            stopwatch.Stop();
            long elapsedMilliseconds = stopwatch.ElapsedMilliseconds;

            logger.Set(GetLinkPreviewMetadata.EncodingRegExCount, num);
            logger.Set(GetLinkPreviewMetadata.ElapsedTimeToGetWebPageEncoding, elapsedMilliseconds);
            return(result);
        }
Esempio n. 22
0
 // Token: 0x060018F1 RID: 6385 RVA: 0x00056B4C File Offset: 0x00054D4C
 public static void ThrowLocalizedException(string error, LocalizedException localizedException)
 {
     GetLinkPreview.SetErrorData(localizedException, error);
     throw localizedException;
 }