Esempio n. 1
0
 public void StoreTest()
 {
     var urlStore = new UrlStore();
     var uri = new Uri("http://byterot.blogspot.com");
     urlStore.Store(uri);
     Assert.True(urlStore.Exists(uri));
 }
Esempio n. 2
0
 public static string ShortenUrl(this string longUrl)
 {
     var datastore = new UrlStore();
       var shortUrlPath = longUrl.GetHashCode().ToString();
       datastore.SaveUrl(longUrl, shortUrlPath);
       return shortUrlPath;
 }
Esempio n. 3
0
        private Negotiator ShortenUrl(UrlStore urlStore)
        {
            string longUrl = Request.Form.url;
            var shortUrl = ShortenUrl(longUrl);
            urlStore.SaveUrl(longUrl, shortUrl);

            return View["shortened_url", new { Request.Headers.Host, ShortUrl = shortUrl }];
        }
        public void StoreTest()
        {
            var urlStore = new UrlStore();
            var uri      = new Uri("http://byterot.blogspot.com");

            urlStore.Store(uri);
            Assert.True(urlStore.Exists(uri));
        }
Esempio n. 5
0
        private Response ShortenUrl(UrlStore urlStore)
        {
            string longUrl  = Request.Form.url;
            var    shortUrl = ShortenUrl(longUrl);

            urlStore.SaveUrl(longUrl, shortUrl);

            return(View["shortened_url", new { Request.Headers.Host, ShortUrl = shortUrl }]);
        }
Esempio n. 6
0
        public ApiModule(UrlStore urlStore) : base("/api")
        {
            Get["/items"] = parameters => GetSavedItems(urlStore);

            Get["/stats"] = parameters => GetAllStats(urlStore);

            Get["/{shorturl}/stats"] = parameters => GetShortUrlStats(parameters, urlStore);

            Get["/{shorturl}/content"] = parameters => GetShortUrlContent(parameters, urlStore);
        }
Esempio n. 7
0
 public ShortUrlModule(UrlStore urlStore)
 {
     Get["/"]           = _ => View["index.html"];
     Post["/"]          = _ => ShortenUrl(urlStore);
     Get["/{shorturl}"] = param =>
     {
         string shortUrl = param.shorturl;
         return(Response.AsRedirect(urlStore.GetUrlFor(shortUrl.ToString()), Nancy.Responses.RedirectResponse.RedirectType.Temporary));
     };
 }
Esempio n. 8
0
 public ShortUrlModule(UrlStore urlStore)
 {
     Get["/"] = _ => View["index.html"];
     Post["/"] = _ => ShortenUrl(urlStore);
     Get["/{shorturl}"] = param =>
     {
         string shortUrl = param.shorturl;
         return Response.AsRedirect(urlStore.GetUrlFor(shortUrl.ToString()));
     };
 }
Esempio n. 9
0
 public void QueueTest()
 {
     var urlStore = new UrlStore();
     var uri = new Uri("http://byterot.blogspot.com");
     urlStore.Enqueue(uri);
     Uri uri2 = null;
     Assert.True(urlStore.TryDequeue(out uri2));
     Assert.NotNull(uri2);
     Assert.Equal(uri.ToString(), uri2.ToString());
 }
Esempio n. 10
0
        public void QueueTest()
        {
            var urlStore = new UrlStore();
            var uri      = new Uri("http://byterot.blogspot.com");

            urlStore.Enqueue(uri);
            Uri uri2 = null;

            Assert.True(urlStore.TryDequeue(out uri2));
            Assert.NotNull(uri2);
            Assert.Equal(uri.ToString(), uri2.ToString());
        }
Esempio n. 11
0
        private dynamic GetAllStats(UrlStore urlStore)
        {
            var stats = urlStore.GetAllStats();

            var jsonBytes = Encoding.UTF8.GetBytes(stats.ToJson());

            return(new Response
            {
                ContentType = "application/json",
                Contents = s => s.Write(jsonBytes, 0, jsonBytes.Length)
            });
        }
Esempio n. 12
0
        private dynamic GetLongUrl(dynamic parameters, UrlStore urlStore)
        {
            string shortUrl = parameters.shorturl;

            //*
            var bsonHeaders = new BsonDocument();

            foreach (String key in Request.Headers.Keys)
            {
                IEnumerable <string> values = Request.Headers[key];

                int count = values.Count();

                if (count == 1)
                {
                    bsonHeaders.Add(key, values.First());
                }
                else
                {
                    BsonArray bsonArray = new BsonArray(values);
                    bsonHeaders.Add(key, bsonArray);
                }
            }
            //*/

            var logRequest = new BsonDocument
            {
                { "shortUrl", shortUrl },
                { "userHostAddress", GetUserHostAddress() },
                { "headers", bsonHeaders },
                { "userAgent", Request.Headers.UserAgent },
                { "referrer", Request.Headers.Referrer },
                { "timestamp", DateTime.UtcNow },
                { "statusCode", 200 }
            };


            string longUrl = urlStore.GetUrlForNav(shortUrl.ToString(), logRequest);

            if (String.IsNullOrEmpty(longUrl))
            {
                return(View["404.html"]);
            }
            else
            {
                return(Response.AsRedirect(longUrl));
            }
        }
Esempio n. 13
0
        private dynamic GetShortUrlStats(dynamic parameters, UrlStore urlStore)
        {
            string shortUrl = parameters.shorturl;

            var stats = urlStore.GetStatsFor(shortUrl);

            var json = stats.ToJson();

            //return Negotiate.WithModel(result);

            var jsonBytes = Encoding.UTF8.GetBytes(json);

            return(new Response
            {
                ContentType = "application/json",
                Contents = s => s.Write(jsonBytes, 0, jsonBytes.Length)
            });
        }
Esempio n. 14
0
        public ShortUrlModule(UrlStore urlStore)
        {
            Get["/"] = _ => View["index.html"];

            Post["/"] = _ => ShortenAndSaveUrl(urlStore);

            Post["/new"] = _ => ShortenAndSaveUrl(urlStore);

            Post["/test"] = parameters => TestMethod(parameters);

            Get["/{shorturl}"] = parameters => GetLongUrl(parameters, urlStore);

            Get["/items"] = parameters => GetSavedItems(urlStore);

            Get["/stats"] = parameters => GetAllStats(urlStore);

            Get["/{shorturl}/stats"] = parameters => GetShortUrlStats(parameters, urlStore);

            Get["/{shorturl}/content"] = parameters => GetShortUrlContent(parameters, urlStore);

            Get["/cleanup"] = _ => CleanupCollections(urlStore);
        }
Esempio n. 15
0
        private Negotiator ShortenAndSaveUrl(UrlStore urlStore)
        {
            string longUrl = Request.Form.url;

            if (longUrl == null)
            {
                var newUrl = this.Bind <NewUrl>();
                longUrl = newUrl.Url;
            }

            if (longUrl == null)
            {
                return(Negotiate.WithModel(new { Message = "Url parameter is null" }));
            }

            var shortUrl = ShortenUrl(longUrl);

            if (urlStore.GetUrlFor(shortUrl) == null)
            {
                urlStore.SaveUrl(longUrl, shortUrl);
            }

            return(Negotiate.WithModel(new { ShortUrl = ("http://" + Request.Headers.Host + "/" + shortUrl) }));
        }
Esempio n. 16
0
        private dynamic CleanupCollections(UrlStore urlStore)
        {
            urlStore.ClearCollections();

            return(View["404.html"]);
        }
Esempio n. 17
0
 private dynamic GetShortUrlContent(dynamic parameters, UrlStore urlStore)
 {
     return(Negotiate.WithModel(new { Message = "Calling method GetShortUrlContent" }));
 }