Skip to content

Instantly share code, notes, and snippets.

@jsclayton
Created May 6, 2011 16:08
Show Gist options
  • Save jsclayton/959253 to your computer and use it in GitHub Desktop.
Save jsclayton/959253 to your computer and use it in GitHub Desktop.
AppHarbor URL handling
using System.Web;
namespace NetflixDemo
{
public class AppHarborHttpContextWrapper : HttpContextWrapper
{
private readonly HttpContext httpContext;
public AppHarborHttpContextWrapper(HttpContext httpContext) : base(httpContext)
{
this.httpContext = httpContext;
}
public override HttpRequestBase Request
{
get { return new AppHarborRequestWrapper(httpContext.Request); }
}
}
}
using System;
using System.Web;
namespace NetflixDemo
{
class AppHarborRequestWrapper : HttpRequestWrapper
{
private readonly HttpRequest httpRequest;
public AppHarborRequestWrapper(HttpRequest httpRequest) : base(httpRequest)
{
this.httpRequest = httpRequest;
}
public override Uri Url
{
get
{
var builder = new UriBuilder
{
Scheme = httpRequest.Url.Scheme,
Host = httpRequest.Url.Host,
Port = 80,
Path = httpRequest.Url.AbsolutePath,
Fragment = httpRequest.Url.Fragment,
Query = httpRequest.Url.Query.Replace("?", "")
};
var forwardedProtocol = httpRequest.Headers["X-Forwarded-Proto"];
if (!String.IsNullOrEmpty(forwardedProtocol))
builder.Scheme = forwardedProtocol;
if (builder.Scheme == "https")
builder.Port = 443;
if (httpRequest.IsLocal)
builder.Port = httpRequest.Url.Port;
return builder.Uri;
}
}
}
}
using System.Web.Mvc;
using System.Web.Routing;
namespace NetflixDemo.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
return View();
}
protected override void Initialize(RequestContext requestContext)
{
base.Initialize(new RequestContext(new AppHarborHttpContextWrapper(System.Web.HttpContext.Current), requestContext.RouteData));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment