Skip to content

Commit

Permalink
Works with Simple.Web
Browse files Browse the repository at this point in the history
  • Loading branch information
markrendle committed Jul 26, 2012
1 parent cfc889e commit c6fc615
Show file tree
Hide file tree
Showing 18 changed files with 236 additions and 51 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ TestResults/*
Releases/2*
*ncrunch*
Fix.sln.DotSettings.user
*.nupkg
57 changes: 34 additions & 23 deletions FixAsp/Bridge.cs → Fix.AspNet/Bridge.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace FixAsp
namespace Fix.AspNet
{
using System;
using System.Collections.Generic;
Expand All @@ -16,7 +16,7 @@
using Starter = System.Action<System.Func<System.Collections.Generic.IDictionary<string, object>, System.Collections.Generic.IDictionary<string, string[]>, System.IO.Stream, System.Threading.CancellationToken, System.Func<int, System.Collections.Generic.IDictionary<string, string[]>, System.Func<System.IO.Stream, System.Threading.CancellationToken, System.Threading.Tasks.Task>, System.Threading.Tasks.Task>, System.Delegate, System.Threading.Tasks.Task>>;
using System.Linq;

public class Bridge
internal class Bridge
{
private static readonly object SyncApp = new object();
private static volatile App _app;
Expand All @@ -25,32 +25,41 @@ public static Task RunContext(HttpContext context)
{
if (_app == null)
{
lock (SyncApp)
{
if (_app == null)
{
var fixer = new Fixer();
string path = Path.Combine(context.Request.PhysicalApplicationPath, "bin");
using (var catalog = new DirectoryCatalog(path))
{
var container = new CompositionContainer(catalog);
container.ComposeParts(fixer);
}
_app = fixer.BuildApp();
}
}
Initialize(context);
}
var env = CreateEnvironmentHash(context.Request);
var env = CreateEnvironmentHash(context);
var headers = CreateRequestHeaders(context.Request);
return _app(env, headers, context.Request.InputStream, CancellationToken.None,
(status, outputHeaders, bodyDelegate) =>
{
context.Response.StatusCode = status > 0 ? status : 404;
WriteHeaders(outputHeaders, context);
return bodyDelegate(context.Response.OutputStream, CancellationToken.None);
if (bodyDelegate != null)
{
return bodyDelegate(context.Response.OutputStream, CancellationToken.None);
}
return TaskHelper.Completed();
}, null);
}

private static void Initialize(HttpContext context)
{
lock (SyncApp)
{
if (_app == null)
{
var fixer = new Fixer();
string path = Path.Combine(context.Request.PhysicalApplicationPath, "bin");
using (var catalog = new DirectoryCatalog(path))
{
var container = new CompositionContainer(catalog);
container.ComposeParts(fixer);
}
_app = fixer.BuildApp();
}
}
}

private static void WriteHeaders(OwinHeaders outputHeaders, HttpContext context)
{
if (outputHeaders != null)
Expand Down Expand Up @@ -82,20 +91,22 @@ private static bool SpecialCase(string key, string[] value, HttpResponse respons
return false;
}

private static OwinEnvironment CreateEnvironmentHash(HttpRequest request)
private static OwinEnvironment CreateEnvironmentHash(HttpContext context)
{
var request = context.Request;
return new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase)
{

{"owin.RequestMethod", request.HttpMethod},
{"owin.RequestPath", request.Url.AbsolutePath},
{"owin.RequestPathBase", string.Empty},
{"owin.RequestQueryString", request.Url.Query},
{"owin.RequestQueryString", request.Url.Query.TrimStart('?')},
{"host.ServerName", request.Url.Host},
{"host.ServerPort", request.Url.Port.ToString()},
{"owin.RequestProtocol", request.ServerVariables["HTTP_VERSION"][0]},
{"host.ServerPort", request.Url.Port},
{"owin.RequestProtocol", request.ServerVariables["HTTP_VERSION"]},
{"owin.RequestScheme", request.Url.Scheme},
{"owin.Version", "1.0"},
{"aspnet.Context", context},
};
}

Expand All @@ -105,4 +116,4 @@ private static IDictionary<string, string[]> CreateRequestHeaders(HttpRequest re
StringComparer.OrdinalIgnoreCase);
}
}
}
}
62 changes: 62 additions & 0 deletions Fix.AspNet/Fix.AspNet.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{CCAC0A87-18F1-4DB9-BF12-9185A81917CD}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Fix.AspNet</RootNamespace>
<AssemblyName>Fix.AspNet</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Core" />
<Reference Include="System.Web" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Bridge.cs" />
<Compile Include="FixHttpHandler.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Fix\Fix.csproj">
<Project>{AE24B4A3-30AE-4479-BB37-A37BB65BFBC0}</Project>
<Name>Fix</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
2 changes: 1 addition & 1 deletion FixAsp/FixHttpHandler.cs → Fix.AspNet/FixHttpHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace FixAsp
namespace Fix.AspNet
{
using System;
using System.Web;
Expand Down
36 changes: 36 additions & 0 deletions Fix.AspNet/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Fix.AspNet")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Fix.AspNet")]
[assembly: AssemblyCopyright("Copyright © 2012")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("869e113e-7cb3-4adf-94d3-2b7ea7a7958b")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
12 changes: 12 additions & 0 deletions Fix.sln
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FileHandler", "FileHandler\
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FixAsp", "FixAsp\FixAsp.csproj", "{34AA28DF-087C-46DA-9E62-652EB36789AA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Fix.AspNet", "Fix.AspNet\Fix.AspNet.csproj", "{CCAC0A87-18F1-4DB9-BF12-9185A81917CD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -124,6 +126,16 @@ Global
{CAFD0C47-ADF9-43F6-BCA5-8FF093B98386}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{CAFD0C47-ADF9-43F6-BCA5-8FF093B98386}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{CAFD0C47-ADF9-43F6-BCA5-8FF093B98386}.Release|x86.ActiveCfg = Release|Any CPU
{CCAC0A87-18F1-4DB9-BF12-9185A81917CD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CCAC0A87-18F1-4DB9-BF12-9185A81917CD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CCAC0A87-18F1-4DB9-BF12-9185A81917CD}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{CCAC0A87-18F1-4DB9-BF12-9185A81917CD}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{CCAC0A87-18F1-4DB9-BF12-9185A81917CD}.Debug|x86.ActiveCfg = Debug|Any CPU
{CCAC0A87-18F1-4DB9-BF12-9185A81917CD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CCAC0A87-18F1-4DB9-BF12-9185A81917CD}.Release|Any CPU.Build.0 = Release|Any CPU
{CCAC0A87-18F1-4DB9-BF12-9185A81917CD}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{CCAC0A87-18F1-4DB9-BF12-9185A81917CD}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{CCAC0A87-18F1-4DB9-BF12-9185A81917CD}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
15 changes: 15 additions & 0 deletions Fix/Body.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Fix
{
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

public static class Body
{
public static Func<Stream, CancellationToken, Task> FromException(Exception ex)
{
return new ExceptionBody(ex).ToAction();
}
}
}
24 changes: 24 additions & 0 deletions Fix/ExceptionBody.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace Fix
{
using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

internal class ExceptionBody
{
private readonly Exception _error;

public ExceptionBody(Exception error)
{
_error = error;
}

public Func<Stream, CancellationToken, Task> ToAction()
{
var buffer = Encoding.Default.GetBytes(_error.ToString());
return (stream, token) => Task.Factory.FromAsync(stream.BeginWrite, stream.EndWrite, buffer, 0, buffer.Length, null);
}
}
}
11 changes: 3 additions & 8 deletions Fix/Fix.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,12 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Converter.cs" />
<Compile Include="Body.cs" />
<Compile Include="EnumerableEx.cs" />
<Compile Include="ExceptionBody.cs" />
<Compile Include="Fixer.cs" />
<Compile Include="InvokeAndForget.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\OwinHelpers\OwinHelpers.csproj">
<Project>{92C89FAF-D1F0-4E9E-B2C6-49D7587E01B3}</Project>
<Name>OwinHelpers</Name>
</ProjectReference>
<Compile Include="TaskHelper.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
15 changes: 15 additions & 0 deletions Fix/Fix.nuspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>Simple.Web</id>
<version>0.1</version>
<authors>Mark Rendle</authors>
<owners>Mark Rendle</owners>
<copyright>Mark Rendle</copyright>
<licenseUrl>http://www.opensource.org/licenses/mit-license.php</licenseUrl>
<projectUrl>http://github.com/markrendle/Fix</projectUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>A glue for OWIN.</description>
<tags>fix owin</tags>
</metadata>
</package>
1 change: 0 additions & 1 deletion Fix/Fixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.ComponentModel.Composition;
using System.Linq;
using System.Threading;
using OwinHelpers;
using OwinEnvironment = System.Collections.Generic.IDictionary<string, object>;
using OwinHeaders = System.Collections.Generic.IDictionary<string, string[]>;
using ResponseHandler = System.Func<int, System.Collections.Generic.IDictionary<string, string[]>, System.Func<System.IO.Stream, System.Threading.CancellationToken, System.Threading.Tasks.Task>, System.Threading.Tasks.Task>;
Expand Down
14 changes: 7 additions & 7 deletions Fix/InvokeAndForget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

namespace Fix
{
static class InvokeAndForgetEx
{
public static void InvokeAndForget(this RequestHandler handler, IEnumerable<KeyValuePair<string,object>> env, Func<byte[]> body, ResponseHandler responseHandler, Action<Exception> exceptionHandler, Delegate next)
{
handler.BeginInvoke(env, body, responseHandler, exceptionHandler, next, handler.EndInvoke, null);
}
}
//static class InvokeAndForgetEx
//{
// public static void InvokeAndForget(this RequestHandler handler, IEnumerable<KeyValuePair<string,object>> env, Func<byte[]> body, ResponseHandler responseHandler, Action<Exception> exceptionHandler, Delegate next)
// {
// handler.BeginInvoke(env, body, responseHandler, exceptionHandler, next, handler.EndInvoke, null);
// }
//}
}
1 change: 1 addition & 0 deletions Fix/NugetPack.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nuget pack -sym Fix.csproj -Properties Configuration=Release;Platform=AnyCPU -Build
19 changes: 19 additions & 0 deletions Fix/TaskHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Fix
{
using System.Threading.Tasks;

public static class TaskHelper
{
public static Task Completed()
{
var tcs = new TaskCompletionSource<object>();
tcs.SetResult(null);
return tcs.Task;
}
}
}
7 changes: 4 additions & 3 deletions FixAsp/FixAsp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@
<Content Include="Web.config" />
</ItemGroup>
<ItemGroup>
<Compile Include="Bridge.cs" />
<Compile Include="FixHttpHandler.cs" />
<Compile Include="FixUp.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand All @@ -77,6 +74,10 @@
</Content>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Fix.AspNet\Fix.AspNet.csproj">
<Project>{ccac0a87-18f1-4db9-bf12-9185a81917cd}</Project>
<Name>Fix.AspNet</Name>
</ProjectReference>
<ProjectReference Include="..\Fix\Fix.csproj">
<Project>{AE24B4A3-30AE-4479-BB37-A37BB65BFBC0}</Project>
<Name>Fix</Name>
Expand Down
Loading

0 comments on commit c6fc615

Please sign in to comment.