Skip to content

Commit

Permalink
Code Submit
Browse files Browse the repository at this point in the history
  • Loading branch information
scegg committed Dec 12, 2019
1 parent d3bcf82 commit 71c0fb9
Show file tree
Hide file tree
Showing 19 changed files with 1,377 additions and 0 deletions.
63 changes: 63 additions & 0 deletions .gitattributes
@@ -0,0 +1,63 @@
###############################################################################
# Set default behavior to automatically normalize line endings.
###############################################################################
* text=auto

###############################################################################
# Set default behavior for command prompt diff.
#
# This is need for earlier builds of msysgit that does not have it on by
# default for csharp files.
# Note: This is only used by command line
###############################################################################
#*.cs diff=csharp

###############################################################################
# Set the merge driver for project and solution files
#
# Merging from the command prompt will add diff markers to the files if there
# are conflicts (Merging from VS is not affected by the settings below, in VS
# the diff markers are never inserted). Diff markers may cause the following
# file extensions to fail to load in VS. An alternative would be to treat
# these files as binary and thus will always conflict and require user
# intervention with every merge. To do so, just uncomment the entries below
###############################################################################
#*.sln merge=binary
#*.csproj merge=binary
#*.vbproj merge=binary
#*.vcxproj merge=binary
#*.vcproj merge=binary
#*.dbproj merge=binary
#*.fsproj merge=binary
#*.lsproj merge=binary
#*.wixproj merge=binary
#*.modelproj merge=binary
#*.sqlproj merge=binary
#*.wwaproj merge=binary

###############################################################################
# behavior for image files
#
# image files are treated as binary by default.
###############################################################################
#*.jpg binary
#*.png binary
#*.gif binary

###############################################################################
# diff behavior for common document formats
#
# Convert binary document formats to text before diffing them. This feature
# is only available from the command line. Turn it on by uncommenting the
# entries below.
###############################################################################
#*.doc diff=astextplain
#*.DOC diff=astextplain
#*.docx diff=astextplain
#*.DOCX diff=astextplain
#*.dot diff=astextplain
#*.DOT diff=astextplain
#*.pdf diff=astextplain
#*.PDF diff=astextplain
#*.rtf diff=astextplain
#*.RTF diff=astextplain
168 changes: 168 additions & 0 deletions src/ContextProcessFacade.cs
@@ -0,0 +1,168 @@
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SecretNest.ShortUrl
{
public static class ContextProcessFacade
{
public static async Task Process(HttpContext context, Func<Task> nextHandler)
{
var host = context.GetHost();
var accessKey = context.GetAccessKey();

if (SettingHost.ServiceSetting.GlobalManagementKey == accessKey &&
(SettingHost.ServiceSetting.GlobalManagementEnabledHosts.Count == 0 || SettingHost.ServiceSetting.GlobalManagementEnabledHosts.Contains(host)))
{
//Global Management
try
{
var result = GlobalManager.GlobalManage(context);
await context.ProcessOtherResultAsync(result);
}
catch
{
await context.ProcessOtherResultAsync(new Status500Result());
}
}
else
{
//Alias remap
while (SettingHost.ServiceSetting.Aliases.TryGetValue(host, out string target))
{
host = target;
}

if (SettingHost.ServiceSetting.Domains.TryGetValue(host, out DomainSetting domain))
{
//Domain matched
if (accessKey == domain.ManagementKey)
{
//Domain Management
try
{
var result = DomainManager.DomainManage(context, domain);
await context.ProcessOtherResultAsync(result);
}
catch
{
await context.ProcessOtherResultAsync(new Status500Result());
}
}
else if (domain.Redirects.TryGetValue(accessKey, out RedirectTarget target))
{
//Record matched
context.Redirect(target);
}
else
{
//Domain default
context.Redirect(domain.DefaultTarget);
}
}
else
{
//Global default
context.Redirect(SettingHost.ServiceSetting.DefaultTarget);
}
}
}

internal static string GetHost(this HttpContext context)
{
if (SettingHost.ServiceSetting.PreferXForwardedHost)
{
var xForwardedHost = context.Request.Headers["X-Forwarded-Host"].FirstOrDefault();
if (!string.IsNullOrEmpty(xForwardedHost))
{
return xForwardedHost;
}
}
if (context.Request.Host.Port != null && context.Request.Host.Port != 80 && context.Request.Host.Port != 443)
{
return string.Format("{0}:{1}", context.Request.Host.Host, context.Request.Host.Port);
}
else
{
return context.Request.Host.Value;
}
}

internal static string GetQueryTextParameter(this HttpContext context, string parameter)
{
return context.Request.GetQueryTextParameter(parameter);
}

internal static string GetQueryOptionalTextParameter(this HttpContext context, string parameter)
{
return context.Request.GetQueryOptionalTextParameter(parameter);
}

internal static bool GetQueryBooleanParameter(this HttpContext context, string parameter)
{
return context.Request.GetQueryBooleanParameter(parameter);
}

internal static string GetQueryTextParameter(this HttpRequest request, string parameter)
{
return request.Query[parameter].First();
}

internal static string GetQueryOptionalTextParameter(this HttpRequest request, string parameter)
{
return request.Query[parameter].FirstOrDefault();
}

internal static bool GetQueryBooleanParameter(this HttpRequest request, string parameter)
{
return request.Query[parameter].FirstOrDefault() == "1";
}

static string GetAccessKey(this HttpContext context)
{
if (context.Request.Path.HasValue)
{
return context.Request.Path.Value.Substring(1);
}
else
{
return null;
}
}

static void Redirect(this HttpContext context, RedirectTarget target)
{
var url = target.Target;

var query = context.Request.QueryString;
if (query.HasValue && query.Value != string.Empty)
{
if (target.QueryProcess == RedirectQueryProcess.AppendDirectly)
{
url += query.Value;
}
else if (target.QueryProcess == RedirectQueryProcess.AppendAfterExisted)
{
if (query.Value.StartsWith("?"))
url += "&" + query.Value.Substring(1);
else
url += "&" + query.Value;
}
}

context.Response.Redirect(target.Target, target.Permanent);
}

static async Task ProcessOtherResultAsync(this HttpContext context, OtherResult result)
{
context.Response.StatusCode = result.StatusCode;
if (result.HasContent)
{
context.Response.ContentType = result.ContentType;
await context.Response.WriteAsync(result.Context);
}
}
}
}

0 comments on commit 71c0fb9

Please sign in to comment.