Commit c03f1abb authored by nitroxis's avatar nitroxis

Merge branch 'master' of https://gitlab.com/nitroxis/urlshortener

parents 015fcdc2 2e495520
using System;
using System.Diagnostics;
using System.Linq;
using System.Web;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using UrlShortener.Models;
......@@ -13,7 +12,7 @@ namespace UrlShortener.Controllers
#region Fields
private readonly Database db;
private readonly UriSettings uriSettings;
private readonly ShortenerSettings settings;
#endregion
......@@ -23,25 +22,29 @@ namespace UrlShortener.Controllers
#region Constructors
public HomeController(Database db, UriSettings uriSettings)
public HomeController(Database db, ShortenerSettings settings)
{
this.db = db;
this.uriSettings = uriSettings;
this.settings = settings;
}
#endregion
#region Methods
private string shorten(string longUri, out string error)
private string shorten(string longUri, string pw, out string error)
{
error = null;
if (string.IsNullOrWhiteSpace(longUri))
if (this.settings.Password != null && pw != this.settings.Password)
{
error = "Invalid password.";
}
else if (string.IsNullOrWhiteSpace(longUri))
{
error = "No URI specified.";
}
else if (longUri.Length > this.uriSettings.MaxLength)
else if (longUri.Length > this.settings.MaxLength)
{
error = "URI too long.";
}
......@@ -49,7 +52,7 @@ namespace UrlShortener.Controllers
{
error = "Invalid URI.";
}
else if (!this.uriSettings.AllowedSchemes.Contains(uri.Scheme))
else if (!this.settings.AllowedSchemes.Contains(uri.Scheme))
{
error = $"URI scheme \"{uri.Scheme}\" is not allowed.";
}
......@@ -84,34 +87,21 @@ namespace UrlShortener.Controllers
public IActionResult Index()
{
return this.View(new ShortenerModel());
return this.View(new ShortenerModel {RequiresPassword = this.settings.Password != null});
}
[HttpPost]
public IActionResult Index(ShortenerModel model)
{
if (model.Email != null)
{
model.Error = "no u";
}
else
{
string longUri = model.LongUri;
model.ShortUri = this.shorten(longUri, out string error);
model.Error = error;
}
model.ShortUri = this.shorten(model.LongUri, model.Password, out string error);
model.Error = error;
model.RequiresPassword = this.settings.Password != null;
return this.View(model);
}
public IActionResult Shorten()
public IActionResult Shorten(string url, string pw = null)
{
string longUri = this.HttpContext.Request.QueryString.Value;
if (longUri.StartsWith('?'))
longUri = longUri.Substring(1);
longUri = HttpUtility.UrlDecode(longUri);
string shortUri = this.shorten(longUri, out string error);
string shortUri = this.shorten(url, pw, out string error);
if (error != null)
return this.BadRequest(error);
......
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace UrlShortener
{
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace UrlShortener
namespace UrlShortener
{
public struct DatabaseEntry
{
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace UrlShortener
namespace UrlShortener
{
public static class IDGenerator
{
......
......@@ -11,7 +11,8 @@ namespace UrlShortener.Models
public string Error { get; set; }
[DataType(DataType.EmailAddress)]
public string Email { get; set; } = "";
public bool RequiresPassword { get; set; }
public string Password { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
namespace UrlShortener
{
......
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace UrlShortener
{
public sealed class UriSettings
public sealed class ShortenerSettings
{
public int MaxLength { get; }
public IReadOnlyList<string> AllowedSchemes { get; }
public string Password { get; }
public UriSettings(int maxLength, IEnumerable<string> allowedSchemes)
public ShortenerSettings(int maxLength, IEnumerable<string> allowedSchemes, string password)
{
this.MaxLength = maxLength;
this.AllowedSchemes = allowedSchemes.ToArray();
this.Password = password;
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
......@@ -14,7 +9,7 @@ namespace UrlShortener
public class Startup
{
private readonly Database database;
private readonly UriSettings uriSettings;
private readonly ShortenerSettings uriSettings;
public Startup(IConfiguration configuration)
{
......@@ -23,8 +18,9 @@ namespace UrlShortener
Stream stream = new FileStream(this.Configuration.GetValue("Database", "urls.txt"), FileMode.OpenOrCreate, FileAccess.ReadWrite);
int maxUriLength = this.Configuration.GetValue("MaxUriLength", 8192);
string[] schemes = this.Configuration.GetValue("AllowedSchemes", new[] {"http", "https", "ftp", "ftps"});
string password = this.Configuration.GetValue<string>("Password", null);
this.database = new Database(stream, maxUriLength);
this.uriSettings = new UriSettings(maxUriLength, schemes);
this.uriSettings = new ShortenerSettings(maxUriLength, schemes, password);
}
public IConfiguration Configuration { get; }
......
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BuildBundlerMinifier" Version="2.8.391" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.1.4" />
<PackageReference Include="BuildBundlerMinifier" Version="2.9.406" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.2.3" />
</ItemGroup>
<ItemGroup>
......
......@@ -12,9 +12,14 @@
{
scheme = Context.Request.Scheme;
}
}
@using Microsoft.AspNetCore.Http
string apiUrl = $"{scheme}://{Context.Request.Host}/Add?url=%URL%";
if (Model.RequiresPassword)
{
apiUrl += "&pw=<password>";
}
}
@using Microsoft.Extensions.Primitives
@model ShortenerModel
......@@ -27,10 +32,16 @@
<input id="button" type="submit" class="btn btn-secondary" value="Shorten" />
</span>
</div>
<div class="pls-no-spam-thx">
<input asp-for="Email" class="form-control" />
</div>
@if (Model.RequiresPassword)
{
<div class="form-group">
<label asp-for="Password">Password</label>
<div class="input-group">
<input asp-for="Password" class="form-control"/>
</div>
</div>
}
</form>
@if (Model.Error != null)
......@@ -49,6 +60,6 @@
}
<p>Enter your long URL and click "Shorten" to get a shortened link which will automatically redirect to the long one. The number of requests you can make is limited to prevent spam. If you see a "Service Temporarily Unavailable" error, try again in a few seconds.</p>
<p>If you want to use this service in Firefox, you can install <a href="https://addons.mozilla.org/en-US/firefox/addon/copy-shorturl/">this add-on</a>. It adds a "Copy ShortURL" entry to the right-click menu. You just have to set "Custom URL" to <code>@scheme://@Context.Request.Host/Add?%URL%</code> in the add-on settings.</p>
<p>Made by <a href="//nxs.re">nitroxis</a>. You can find the source code <a href="https://gitlab.com/nitroxis/urlshortener">here</a> (C#, ASP.NET Core).</p>
<p>If you want to use this service in Firefox, you can install <a href="https://addons.mozilla.org/en-US/firefox/addon/copy-shorturl/">this add-on</a>. It adds a "Copy ShortURL" entry to the right-click menu. You just have to set "Custom URL" to <code>@apiUrl</code> in the add-on settings.</p>
<p>Made by <a href="//nxs.re">nitroxis</a>. Contact me for the password or host your own. You can find the source code <a href="https://gitlab.com/nitroxis/urlshortener">here</a> (C#, ASP.NET Core).</p>
......@@ -8,5 +8,6 @@
"Password": "benis",
"Database": "urls.txt",
"MaxUriLength": 10000,
"Password": "secret",
"AllowedSchemes": ["http", "https", "ftp", "ftps"]
}
......@@ -3,10 +3,10 @@
"private": true,
"dependencies": {
"jQuery": "3.3.1",
"bootstrap": "v4.1.3"
"bootstrap": "v4.3.1"
},
"resolutions": {
"jQuery": "3.3.1",
"bootstrap": "v4.1.3"
"bootstrap": "v4.3.1"
}
}
http://benis.com/
https://0x.re/
https://s.nxs.re/20181012010521.png
http://www.example.com/düsseldorf?neighbourhood=Lörick
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment