private string CheckUrlResponse(UrlResponse response, MonitoredUrl url) { string message = string.Empty; if (!string.IsNullOrWhiteSpace(url.BodyRegex)) { if (!Regex.IsMatch(response.Body, url.BodyRegex, RegexOptions.IgnoreCase | RegexOptions.Singleline)) { message += "- Failed to match body regex<br/>"; } } if (!string.IsNullOrWhiteSpace(url.HeadersRegex)) { bool foundOne = false; foreach (string header in response.Headers) { if (Regex.IsMatch(header, url.HeadersRegex, RegexOptions.IgnoreCase | RegexOptions.Singleline)) { foundOne = true; break; } } if (!foundOne) { message += "- Failed to match headers regex<br/>"; } } if (!string.IsNullOrWhiteSpace(url.StatusCodeRegex)) { string statusCodeString = response.StatusCode.ToString("D"); if (!Regex.IsMatch(statusCodeString, url.StatusCodeRegex, RegexOptions.IgnoreCase)) { message += "- Failed to match status code regex, got " + statusCodeString + "<br/>"; } } if (url.AlertIfChanged && (url.MD51 != 0 || url.MD52 != 0) && (url.MD51 != response.MD51 || url.MD52 != response.MD52)) { message += "- MD5 Changed, body contents are new<br/>"; } if (url.MaxTime.TotalSeconds > 0.0d && response.Duration > url.MaxTime) { message += "- URL took " + response.Duration.TotalSeconds.ToString("0.00") + " seconds, too long<br/>"; } url.MD51 = response.MD51; url.MD52 = response.MD52; if (message.Length != 0) { if (!string.IsNullOrWhiteSpace(url.MismatchMessage)) { message = url.MismatchMessage + "<br/>" + message; } message = "<a href='" + url.Path + "' title='Url Link'>" + url.Path + "</a><br/>" + message; } return(message); }
private void ProcessUrl(object state) { MonitoredUrl url = state as MonitoredUrl; try { UrlResponse response = GetResponseForUrl(url); string emailBody = CheckUrlResponse(response, url); if (emailBody.Length != 0) { SendEmail(url, emailBody); } } catch (Exception ex) { string msg = string.Format("Error accessing url {0}, error: {1}", url.Path, ex); Log.Write(LogLevel.Error, msg); WebException webException = ex as WebException; if (webException != null) { msg += ", status code: " + ((HttpWebResponse)webException.Response).StatusCode.ToString("D"); SendEmail(url, msg); } } finally { url.InProcess = false; Interlocked.Decrement(ref threadCount); } }
public IActionResult CreateShortUrl([FromBody] string requestJson) { try{ UrlRequest request = JsonConvert.DeserializeObject <UrlRequest>(requestJson); string longUrl = request.LongUrl; if (!longUrl.Contains("http", StringComparison.OrdinalIgnoreCase)) { longUrl = "http://" + longUrl; } if (!UrlUtils.IsUrlValid(longUrl)) { return(BadRequest()); } longUrl = UrlUtils.GetIdn(longUrl); string shortUrl = urlService.MapToShort(longUrl); UrlResponse urlResponse = new UrlResponse() { LongUrl = longUrl, ShortUrl = shortUrl }; urlService.SaveUrlMap(urlResponse); urlResponse.ShortUrl = "http://localhost:5000/" + shortUrl; return(Ok(urlResponse)); } catch (Exception e) { Console.WriteLine("invalid format for request. the error message is {0}", e.Message); return(BadRequest()); } }
/// <summary> /// Initializes a new instance of the <see cref="VisitItemEntity"/> class. /// </summary> /// <param name="payload"><see cref="UrlResponse"/> object.</param> public VisitItemEntity(UrlResponse payload) : this() { this.EntityId = payload.EntityId; this.ShortUrl = payload.ShortUrl; this.DateGenerated = payload.DateGenerated; this.UrlId = (payload as ExpanderResponse).UrlId; this.RequestHeaders = (payload as ExpanderResponse).RequestHeaders; this.RequestQueries = (payload as ExpanderResponse).RequestQueries; }
private async Task <IActionResult> UpdateUrlAction(HttpRequest req) { var dto = await Parser.Parse <UrlRequest>(req); var urlResult = await _urlService.Update(dto.SourceUrl, dto.Tail, dto.Description); var result = new UrlResponse(req.GetHostPath(), urlResult.LongUrl, urlResult.RowKey, urlResult.Description); _logger.LogInformation("Short Url updated."); return(new OkObjectResult(result)); }
public IActionResult RedirectToLong(string shortUrl) { UrlResponse savedUrl = urlService.GetSavedUrl(shortUrl); if (savedUrl == null) { return(NotFound()); } string longUrl = savedUrl.LongUrl; return(Redirect(longUrl)); }
public void NullEmptyTest() { UrlResponse uut = new UrlResponse(); uut.Title = null; Assert.IsFalse(uut.IsValid); uut.Title = string.Empty; Assert.IsFalse(uut.IsValid); uut.Title = " "; Assert.IsFalse(uut.IsValid); }
/// <summary> /// Initializes a new instance of the <see cref="UrlItemEntity"/> class. /// </summary> /// <param name="payload"><see cref="UrlResponse"/> object.</param> public UrlItemEntity(UrlResponse payload) : this() { this.EntityId = payload.EntityId; this.ShortUrl = payload.ShortUrl; this.OriginalUrl = payload.Original; this.Title = payload.Title; this.Description = payload.Description; this.Owner = payload.Owner; this.CoOwners = payload.CoOwners; this.DateGenerated = payload.DateGenerated; this.DateUpdated = payload.DateUpdated; this.HitCount = payload.HitCount; }
public async Task <ActionResult <UrlResponse> > CustomerLogin() { BaseAccessToken clientAccessTokenResult = await _clientOperationsBusinessLogic.ClientCredentialsAccessToken(); ConsentInfo consentResult = await _clientOperationsBusinessLogic.CreateAccountAccessConsents(); string customerLoginResult = await _clientOperationsBusinessLogic.CustomerLogin(); var result = new UrlResponse { url = customerLoginResult }; return(result); }
public void MaxCharacterTest() { StringBuilder builder = new StringBuilder(); for (int i = 0; i < IrcConnection.MaximumLength; ++i) { builder.Append((char)('0' + i) % 10); } string str = builder.ToString(); Assert.AreEqual(IrcConnection.MaximumLength, str.Length); UrlResponse response = new UrlResponse(); response.Title = str; Assert.IsTrue(response.IsValid); Assert.AreEqual(str, response.Title); Assert.AreEqual(str, response.TitleShortened); }
public void TestSavingUrl() { try { UrlResponse toBeSaved = new UrlResponse() { LongUrl = "https://www.google.com", ShortUrl = "aaaaaaaa" }; urlService.SaveUrlMap(toBeSaved); } catch (Exception e) { Console.WriteLine("Error occured while adding test data, error message is: {0}", e.Message); Console.WriteLine("That might be becuase test data already exists"); } UrlResponse savedResponse = urlService.GetSavedUrl("aaaaaaaa"); Assert.Equal("https://www.google.com", savedResponse.LongUrl); Assert.Equal("aaaaaaaa", savedResponse.ShortUrl); }
public void NewLineTest() { string str = "Hello" + Environment.NewLine + "World"; // Need a for-loop due to line endings StringBuilder builder = new StringBuilder(); builder.Append("Hello"); foreach (char ch in Environment.NewLine) { builder.Append(" "); } builder.Append("World"); UrlResponse response = new UrlResponse(); response.Title = str; Assert.IsTrue(response.IsValid); Assert.AreEqual(str, response.Title); Assert.AreEqual(builder.ToString(), response.TitleShortened); }
public void MoreThanMaxTest() { StringBuilder builder = new StringBuilder(); for (int i = 0; i < (IrcConnection.MaximumLength + 1); ++i) { builder.Append((char)('0' + i) % 10); } string str = builder.ToString(); Assert.AreEqual(IrcConnection.MaximumLength + 1, str.Length); string shortenedStr = str.Substring(0, IrcConnection.MaximumLength - 3) + "..."; Assert.AreEqual(IrcConnection.MaximumLength, shortenedStr.Length); UrlResponse response = new UrlResponse(); response.Title = str; Assert.IsTrue(response.IsValid); Assert.AreEqual(str, response.Title); Assert.AreEqual(shortenedStr, response.TitleShortened); }
/// <summary> /// Pollfors the report. /// </summary> /// <param name="reportId">The report identifier.</param> /// <param name="client">The client.</param> /// <param name="closeclient">if set to <c>true</c> [closeclient].</param> /// <returns></returns> public static UrlResponse PollforReport(string reportId, OlsaPortTypeClient client, bool closeclient = false) { //Set up our response object UrlResponse response = null; try { //Create our request PollForReportRequest request = new PollForReportRequest(); //Pull the OlsaAuthenticationBehviour so we can extract the customerid AuthenticationBehavior olsaCredentials = (AuthenticationBehavior)client.ChannelFactory.Endpoint.Behaviors.Where(p => p.GetType() == typeof(AuthenticationBehavior)).FirstOrDefault(); request.customerId = olsaCredentials.UserName; request.reportId = reportId; response = client.UTIL_PollForReport(request); } catch (WebException) { // This captures any Web Exceptions such as proxy errors etc // See http://msdn.microsoft.com/en-us/library/48ww3ee9(VS.80).aspx throw; } catch (TimeoutException) { //This captures the WCF timeout exception throw; } //WCF fault exception will be thrown for any other issues such as Security catch (FaultException fe) { if (fe.Message.ToLower(CultureInfo.InvariantCulture).Contains("the security token could not be authenticated or authorized")) { //The OLSA Credentials specified could not be authenticated throw; } throw; } catch (Exception) { //Any other type of exception, perhaps out of memory throw; } finally { //Shutdown and dispose of the client if (client != null) { if (client.State == CommunicationState.Faulted) { client.Abort(); } if (closeclient) { //We cannot resue client if we close client.Close(); } } } return(response); }
public void SaveUrlMap(UrlResponse urlResponse) { dbContext.UrlResponses.Add(urlResponse); dbContext.SaveChanges(); }
/// <summary> /// Called when [complete]. /// </summary> /// <param name="data">The data.</param> private void OnComplete(UrlResponse data) { Debug.LogFormat("Response data: {0}", data.data); }
static void Process(Uri endpoint, string customerId, string sharedSecret) { //Create the OLSA Web Services Client using code OlsaPortTypeClient client = Helpers.Olsa.GetOLSAClient(endpoint, customerId, sharedSecret); Console.WriteLine("Issuing the UD_SubmitReport Request"); Console.WriteLine("------------------------------------"); HandleResponse handleResponse = new HandleResponse(); //-------------------------------------------------------------------------------------------- //Define report settings here to ease changes string scopingUserId = "admin"; reportFormat reportFormat = reportFormat.CSV; string reportFilename = "report." + reportFormat.ToString(); string reportLanguage = "en_US"; //en_US only supported value int reportRetainperiod = 3; //1 - 1 hour, 2 - 8 hours, 3 - 24 hours. //For details of report names and parameters see https://documentation.skillsoft.com/en_us/skillport/8_0/ah/35465.htm string reportName = "summary_catalog"; //Define report parameters List <MapItem> paramList = new List <MapItem>(); paramList.Add(new MapItem() { key = "asset_category", value = "1,2,3,4,5,21" }); paramList.Add(new MapItem() { key = "display_options", value = "all" }); //-------------------------------------------------------------------------------------------- //Submit Report request try { handleResponse = Helpers.Olsa.SubmitReport(scopingUserId, reportFormat, reportName, paramList.ToArray(), reportLanguage, reportRetainperiod, client, false); //Minutes between polls int sleepInterval = 2; Console.WriteLine("Handle: {0}", handleResponse.handle); UrlResponse url = null; for (int i = 0; i < 10; i++) { Console.WriteLine("{0}: Sleeping {1} minutes", DateTime.UtcNow.ToLongTimeString(), sleepInterval); //ms - so here we sleep for 10 minutes System.Threading.Thread.Sleep(sleepInterval * 60 * 1000); try { url = Helpers.Olsa.PollforReport(handleResponse.handle, client, false); break; } catch (FaultException <Olsa.DataNotReadyYetFault> ) { //The report has not completed generation, we are checking for a Specific OLSA Exception Console.WriteLine("{0}: The specified report is not yet ready", DateTime.UtcNow.ToLongTimeString()); } catch (Exception ex) { Console.WriteLine("{0}: Issue while Polling for Report.", DateTime.UtcNow.ToLongTimeString()); Console.WriteLine("{0}: Exception: {1}", DateTime.UtcNow.ToLongTimeString(), ex.ToString()); throw; } } if (url != null) { WebClient myWebClient = new WebClient(); Console.WriteLine("{0}: Downloading Report: {1}", DateTime.UtcNow.ToLongTimeString(), url.olsaURL); myWebClient.DownloadFile(url.olsaURL, reportFilename); Console.WriteLine("{0}: Successfully Downloaded: {1}", DateTime.UtcNow.ToLongTimeString(), reportFilename); } } catch (Exception ex) { Console.WriteLine("{0}: Issue while Submitting Report.", DateTime.UtcNow.ToLongTimeString()); Console.WriteLine("{0}: Exception: {1}", DateTime.UtcNow.ToLongTimeString(), ex.ToString()); } //Now we can close the client if (client != null) { if (client.State == CommunicationState.Faulted) { client.Abort(); } client.Close(); } }
public void Init() { instance = new UrlResponse(); }