Exemplo n.º 1
0
        public async void Create_and_update_entity_should_alter_url_and_add_three_urls_to_updatelog()
        {
            var result = await service.Create(testUrl1, new DateTime(2015, 01, 01));

            var response = await service.GetEntity(result.Id);

            await service.Update(result.Id, null, testUrl2 + "?1", DateTime.UtcNow.AddDays(1));

            await service.Update(result.Id, null, testUrl2 + "?2", DateTime.UtcNow.AddDays(1));

            await service.Update(result.Id, null, testUrl2 + "?3", DateTime.UtcNow.AddDays(1));

            response = await service.GetEntity(result.Id);
        }
Exemplo n.º 2
0
        private async Task InvokeGet(IOwinContext context)
        {
            var id = context.Request.Path.Value.Substring(1);

            if (id.StartsWith("s/"))
            {
                id = id.Substring(2);
            }

            string logPath = "entityupdatelogandurl/";

            if (!string.IsNullOrWhiteSpace(id) && id.StartsWith(logPath) && id.Substring(logPath.Length).Contains('/'))
            {
                var path = id.Substring(logPath.Length);

                var splitString = path.Split('/');

                if (splitString.Count() != 2)
                {
                    context.Response.StatusCode = 404;
                    await context.Response.WriteAsync("Not valid url pattern");

                    return;
                }

                id = path.Split('/').First();
                string token = path.Split('/').Last();

                var entity = await _service.GetEntityUpdateLog(id, token);

                List <string> urls = !string.IsNullOrWhiteSpace(entity.UpdateLog)
                    ? entity.UpdateLog.Split(',').ToList()
                    : new List <string>();
                urls.Add(entity.Url);

                context.Response.StatusCode = 200;
                await context.Response.WriteAsync(JsonConvert.SerializeObject(urls));

                return;
            }

            string prefix = null;

            if (id.Contains("/"))
            {
                var splitString = id.Split('/');

                if (splitString.Count() != 2)
                {
                    context.Response.StatusCode = 404;
                    await context.Response.WriteAsync("Not valid url pattern");

                    return;
                }

                id     = splitString.Last();
                prefix = splitString.First();
            }

            if (string.IsNullOrWhiteSpace(id) || id.Length > 20)
            {
                context.Response.StatusCode = 302;
                context.Response.Headers.Set("Location", string.Format("{0}html/index.html", AppSettingsReader.Baseurl));
                return;
                //context.Response.StatusCode = 404;
                //await context.Response.WriteAsync("URL id is missing or not matching this services requirements!");
            }

            try
            {
                var entity = await _service.GetEntity(id);

                if (!string.IsNullOrWhiteSpace(prefix) && !prefix.Equals(entity.Prefix))
                {
                    throw new NotFoundException(id);
                }

                int statusCode = entity.PermanentRedirect ? 301:  302;
                //if (entity.Url.ToLowerInvariant().Contains("https://"))
                //{
                //context.Response.Headers.Append("Strict-Transport-Security", AppSettingsReader.Baseurl.Contains("signere.no") ?
                //    "max-age=31536000; includeSubdomains; preload":
                //    "max-age=31536000");
                //}
                if (entity.BlockiFrame)
                {
                    context.Response.Headers.Append("X-Frame-Options", "DENY");
                    context.Response.Headers.Append("Content-Security-Policy", "frame-ancestors 'none'");
                }

                context.Response.StatusCode = statusCode;
                context.Response.Headers.Set("Location", entity.Url);
            }
            catch (NotFoundException e)
            {
                context.Response.StatusCode   = 404;
                context.Response.ReasonPhrase = e.Message;
                context.Response.ContentType  = "text/html";
                await context.Response.WriteAsync(File.ReadAllText(System.Web.Hosting.HostingEnvironment.MapPath("~/html/Notfound.html")));
            }
            catch (ExpiredException e)
            {
                context.Response.StatusCode = 410;
                await context.Response.WriteAsync(e.Message);
            }
            catch (Exception e)
            {
                context.Response.StatusCode = 500;
                await context.Response.WriteAsync("Server error" + (AppSettingsReader.Debug ? e.Message + Environment.NewLine + e.InnerException :""));
            }
        }