Skip to content
Browse files

Added Action<FileInfo> to Response Body delegate

  • Loading branch information...
1 parent d39425e commit 7ae03417fa8524ba18d487408ee2fbe6d2e80232 @markrendle committed Jan 10, 2011
View
66 FileHandler/FileHandler.csproj
@@ -0,0 +1,66 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProductVersion>8.0.30703</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{88EEAA81-6680-4EAD-B66B-D14667EA3236}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>FileHandler</RootNamespace>
+ <AssemblyName>FileHandler</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.Xml.Linq" />
+ <Reference Include="System.Data.DataSetExtensions" />
+ <Reference Include="Microsoft.CSharp" />
+ <Reference Include="System.Data" />
+ <Reference Include="System.Xml" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="FileReturner.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\OwinHelpers\OwinHelpers.csproj">
+ <Project>{92C89FAF-D1F0-4E9E-B2C6-49D7587E01B3}</Project>
+ <Name>OwinHelpers</Name>
+ </ProjectReference>
+ </ItemGroup>
+ <ItemGroup>
+ <Content Include="index.html">
+ <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+ </Content>
+ </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>
View
48 FileHandler/FileReturner.cs
@@ -0,0 +1,48 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel.Composition;
+using System.IO;
+using System.Linq;
+using System.Text;
+using OwinHelpers;
+using ResponseHandler = System.Action<int, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>>, System.Action<System.Action<System.ArraySegment<byte>>, System.Action<System.IO.FileInfo>, System.Action, System.Action<System.Exception>>>;
+using App = System.Action<System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object>>, System.Func<byte[]>, System.Action<int, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>>, System.Action<System.Action<System.ArraySegment<byte>>, System.Action<System.IO.FileInfo>, System.Action, System.Action<System.Exception>>>, System.Delegate>;
+
+namespace FileHandler
+{
+ public class FileReturner
+ {
+ [Export("Owin.Application")]
+ public void PrintInfo(IEnumerable<KeyValuePair<string, object>> env, Func<byte[]> body,
+ ResponseHandler responseHandler, Delegate next)
+ {
+ try
+ {
+ if (env.GetScriptName().ToLower().Equals("/index.html", StringComparison.CurrentCultureIgnoreCase))
+ {
+ HandleRequest(responseHandler);
+ }
+ else
+ {
+ next.InvokeAsNextApp(env, body, responseHandler);
+ }
+ }
+ catch (Exception ex)
+ {
+ responseHandler(0, null, Body.FromException(ex));
+ }
+ }
+
+ private static void HandleRequest(ResponseHandler responseHandler)
+ {
+ var fileInfo = new FileInfo(Path.Combine(Environment.CurrentDirectory, "index.html"));
+ var body = Body.FromFile(fileInfo);
+ var headers = new Dictionary<string, string>
+ {
+ { "Content-Type", "text/html" },
+ { "Content-Length", fileInfo.Length.ToString() }
+ };
+ responseHandler(200, headers, body);
+ }
+ }
+}
View
36 FileHandler/Properties/AssemblyInfo.cs
@@ -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("FileHandler")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("Microsoft")]
+[assembly: AssemblyProduct("FileHandler")]
+[assembly: AssemblyCopyright("Copyright © Microsoft 2011")]
+[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("a01a8db1-2ccc-47e7-b221-bf807feb80be")]
+
+// 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")]
View
9 FileHandler/index.html
@@ -0,0 +1,9 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
+<html>
+ <head>
+ <title>Static HTML file</title>
+ </head>
+ <body>
+ <h1>This is a static HTML file.</h1>
+ </body>
+</html>
View
12 Fix.sln
@@ -20,6 +20,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FixUp", "FixConsole\FixUp.c
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OwinHelpers", "OwinHelpers\OwinHelpers.csproj", "{92C89FAF-D1F0-4E9E-B2C6-49D7587E01B3}"
EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FileHandler", "FileHandler\FileHandler.csproj", "{88EEAA81-6680-4EAD-B66B-D14667EA3236}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -100,6 +102,16 @@ Global
{92C89FAF-D1F0-4E9E-B2C6-49D7587E01B3}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{92C89FAF-D1F0-4E9E-B2C6-49D7587E01B3}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{92C89FAF-D1F0-4E9E-B2C6-49D7587E01B3}.Release|x86.ActiveCfg = Release|Any CPU
+ {88EEAA81-6680-4EAD-B66B-D14667EA3236}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {88EEAA81-6680-4EAD-B66B-D14667EA3236}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {88EEAA81-6680-4EAD-B66B-D14667EA3236}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
+ {88EEAA81-6680-4EAD-B66B-D14667EA3236}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
+ {88EEAA81-6680-4EAD-B66B-D14667EA3236}.Debug|x86.ActiveCfg = Debug|Any CPU
+ {88EEAA81-6680-4EAD-B66B-D14667EA3236}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {88EEAA81-6680-4EAD-B66B-D14667EA3236}.Release|Any CPU.Build.0 = Release|Any CPU
+ {88EEAA81-6680-4EAD-B66B-D14667EA3236}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
+ {88EEAA81-6680-4EAD-B66B-D14667EA3236}.Release|Mixed Platforms.Build.0 = Release|Any CPU
+ {88EEAA81-6680-4EAD-B66B-D14667EA3236}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
View
8 Fix/Fixer.cs
@@ -4,9 +4,9 @@
using System.Linq;
using System.Threading;
using OwinHelpers;
-using App = System.Action<System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string,object>>, System.Func<byte[]>, System.Action<int, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>>, System.Action<System.Action<System.ArraySegment<byte>>, System.Action, System.Action<System.Exception>>>, System.Delegate>;
-using ResponseHandler = System.Action<int, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>>, System.Action<System.Action<System.ArraySegment<byte>>, System.Action, System.Action<System.Exception>>>;
-using Starter = System.Action<System.Action<System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string,object>>, System.Func<byte[]>, System.Action<int, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>>, System.Action<System.Action<System.ArraySegment<byte>>, System.Action, System.Action<System.Exception>>>, System.Delegate>>;
+using App = System.Action<System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string,object>>, System.Func<byte[]>, System.Action<int, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>>, System.Action<System.Action<System.ArraySegment<byte>>, System.Action<System.IO.FileInfo>, System.Action, System.Action<System.Exception>>>, System.Delegate>;
+using ResponseHandler = System.Action<int, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>>, System.Action<System.Action<System.ArraySegment<byte>>, System.Action<System.IO.FileInfo>, System.Action, System.Action<System.Exception>>>;
+using Starter = System.Action<System.Action<System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object>>, System.Func<byte[]>, System.Action<int, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>>, System.Action<System.Action<System.ArraySegment<byte>>, System.Action<System.IO.FileInfo>, System.Action, System.Action<System.Exception>>>, System.Delegate>>;
namespace Fix
{
@@ -104,7 +104,7 @@ private static void DefaultInfix(IEnumerable<KeyValuePair<string,object>> env, F
}
catch (Exception ex)
{
- responseHandler(0, null, new ExceptionBody(ex).ToAction());
+ responseHandler(0, null, Body.FromException(ex));
}
}
}
View
4 FixConsole/FixUp.csproj
@@ -57,6 +57,10 @@
<None Include="app.config" />
</ItemGroup>
<ItemGroup>
+ <ProjectReference Include="..\FileHandler\FileHandler.csproj">
+ <Project>{88EEAA81-6680-4EAD-B66B-D14667EA3236}</Project>
+ <Name>FileHandler</Name>
+ </ProjectReference>
<ProjectReference Include="..\Fix\Fix.csproj">
<Project>{AE24B4A3-30AE-4479-BB37-A37BB65BFBC0}</Project>
<Name>Fix</Name>
View
13 Info/InfoPrinter.cs
@@ -3,8 +3,8 @@
using System.ComponentModel.Composition;
using System.Text;
using OwinHelpers;
-using ResponseHandler = System.Action<int, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>>, System.Action<System.Action<System.ArraySegment<byte>>, System.Action, System.Action<System.Exception>>>;
-using App = System.Action<System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string,object>>, System.Func<byte[]>, System.Action<int, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>>, System.Action<System.Action<System.ArraySegment<byte>>, System.Action, System.Action<System.Exception>>>, System.Delegate>;
+using ResponseHandler = System.Action<int, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>>, System.Action<System.Action<System.ArraySegment<byte>>, System.Action<System.IO.FileInfo>, System.Action, System.Action<System.Exception>>>;
+using App = System.Action<System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object>>, System.Func<byte[]>, System.Action<int, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>>, System.Action<System.Action<System.ArraySegment<byte>>, System.Action<System.IO.FileInfo>, System.Action, System.Action<System.Exception>>>, System.Delegate>;
namespace Info
{
@@ -27,19 +27,20 @@ public class InfoPrinter
}
catch (Exception ex)
{
- responseHandler(0, null, new ExceptionBody(ex).ToAction());
+ responseHandler(0, null, Body.FromException(ex));
}
}
private static void HandleRequest(ResponseHandler responseHandler)
{
- var body = new StringBody("<html><body><h1>This server is running on <a href=\"http://github.com/markrendle/Fix\">Fix</a>.</h1></body></html>");
+ const string html = "<html><body><h1>This server is running on <a href=\"http://github.com/markrendle/Fix\">Fix</a>.</h1></body></html>";
+ var body = Body.FromString(html);
var headers = new Dictionary<string, string>
{
{ "Content-Type", "text/html" },
- { "Content-Length", body.Length.ToString() }
+ { "Content-Length", html.Length.ToString() }
};
- responseHandler(200, headers, body.ToAction());
+ responseHandler(200, headers, body);
}
}
}
View
41 OwinHelpers/ActionObserver.cs
@@ -0,0 +1,41 @@
+using System;
+using System.IO;
+
+namespace OwinHelpers
+{
+ internal class ActionObserver<T> : IObserver<T>
+ {
+ private readonly Action<FileInfo> _onFile;
+ private readonly Action<T> _onNext;
+ private readonly Action _onCompleted;
+ private readonly Action<Exception> _onError;
+
+ public ActionObserver(Action<T> onNext, Action<FileInfo> onFile, Action onCompleted, Action<Exception> onError)
+ {
+ _onFile = onFile;
+ _onNext = onNext ?? new Action<T>(x => { });
+ _onError = onError ?? new Action<Exception>(x => { });
+ _onCompleted = onCompleted ?? new Action(() => { });
+ }
+
+ public void OnNext(T value)
+ {
+ _onNext(value);
+ }
+
+ public void OnFile(FileInfo fileInfo)
+ {
+ _onFile(fileInfo);
+ }
+
+ public void OnError(Exception error)
+ {
+ _onError(error);
+ }
+
+ public void OnCompleted()
+ {
+ _onCompleted();
+ }
+ }
+}
View
15 OwinHelpers/Body.cs
@@ -1,15 +1,26 @@
using System;
using System.Collections.Generic;
+using System.IO;
using System.Linq;
using System.Text;
namespace OwinHelpers
{
public static class Body
{
- public static Action<Action<ArraySegment<byte>>, Action, Action<Exception>> FromException(Exception ex)
+ public static Action<Action<ArraySegment<byte>>, Action<FileInfo>, Action, Action<Exception>> FromException(Exception ex)
{
- return (onNext, onCompleted, onException) => onException(ex);
+ return new ExceptionBody(ex).ToAction();
+ }
+
+ public static Action<Action<ArraySegment<byte>>, Action<FileInfo>, Action, Action<Exception>> FromString(string text)
+ {
+ return new StringBody(text).ToAction();
+ }
+
+ public static Action<Action<ArraySegment<byte>>, Action<FileInfo>, Action, Action<Exception>> FromFile(FileInfo fileInfo)
+ {
+ return new FileBody(fileInfo).ToAction();
}
}
}
View
4 OwinHelpers/DelegateExtensions.cs
@@ -2,8 +2,8 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
-using App = System.Action<System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string,object>>, System.Func<byte[]>, System.Action<int, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>>, System.Action<System.Action<System.ArraySegment<byte>>, System.Action, System.Action<System.Exception>>>, System.Delegate>;
-using ResponseHandler = System.Action<int, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>>, System.Action<System.Action<System.ArraySegment<byte>>, System.Action, System.Action<System.Exception>>>;
+using App = System.Action<System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object>>, System.Func<byte[]>, System.Action<int, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>>, System.Action<System.Action<System.ArraySegment<byte>>, System.Action<System.IO.FileInfo>, System.Action, System.Action<System.Exception>>>, System.Delegate>;
+using ResponseHandler = System.Action<int, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>>, System.Action<System.Action<System.ArraySegment<byte>>, System.Action<System.IO.FileInfo>, System.Action, System.Action<System.Exception>>>;
namespace OwinHelpers
{
View
2 OwinHelpers/ExceptionBody.cs
@@ -5,7 +5,7 @@
namespace OwinHelpers
{
- public class ExceptionBody : IObservable<ArraySegment<byte>>
+ internal class ExceptionBody : IObservable<ArraySegment<byte>>
{
private readonly Exception _error;
View
21 OwinHelpers/FileBody.cs
@@ -6,7 +6,7 @@
namespace OwinHelpers
{
- public class FileBody : IObservable<ArraySegment<byte>>
+ internal class FileBody
{
private readonly FileInfo _fileInfo;
@@ -15,24 +15,27 @@ public FileBody(FileInfo fileInfo)
_fileInfo = fileInfo;
}
- public IDisposable Subscribe(IObserver<ArraySegment<byte>> observer)
+ internal IDisposable Subscribe(ActionObserver<ArraySegment<byte>> observer)
{
Action action = () => WriteBody(observer);
action.BeginInvoke(action.EndInvoke, null);
return new NullDisposable();
}
- private void WriteBody(IObserver<ArraySegment<byte>> observer)
+ public Action<Action<ArraySegment<byte>>, Action<FileInfo>, Action, Action<Exception>> ToAction()
+ {
+ return
+ (onNext, onFile, onCompleted, onError) =>
+ this.Subscribe(new ActionObserver<ArraySegment<byte>>(onNext, onFile, onCompleted, onError));
+
+ }
+
+ private void WriteBody(ActionObserver<ArraySegment<byte>> observer)
{
try
{
- using (var stream = _fileInfo.OpenRead())
- {
- var buffer = new byte[stream.Length];
- stream.Read(buffer, 0, buffer.Length);
- observer.OnNext(new ArraySegment<byte>(buffer));
- }
+ observer.OnFile(_fileInfo);
observer.OnCompleted();
}
catch (Exception ex)
View
33 OwinHelpers/ObservableExtensions.cs
@@ -1,45 +1,18 @@
using System;
using System.Collections.Generic;
+using System.IO;
using System.Linq;
using System.Text;
namespace OwinHelpers
{
public static class ObservableExtensions
{
- public static Action<Action<T> , Action, Action<Exception>> ToAction<T>(this IObservable<T> source)
+ public static Action<Action<T>, Action<FileInfo>, Action, Action<Exception>> ToAction<T>(this IObservable<T> source)
{
return
- (onNext, onCompleted, onError) => source.Subscribe(new ActionObserver<T>(onNext, onCompleted, onError));
+ (onNext, onFile, onCompleted, onError) => source.Subscribe(new ActionObserver<T>(onNext, onFile, onCompleted, onError));
}
- private class ActionObserver<T> : IObserver<T>
- {
- private readonly Action<T> _onNext;
- private readonly Action _onCompleted;
- private readonly Action<Exception> _onError;
-
- public ActionObserver(Action<T> onNext, Action onCompleted, Action<Exception> onError)
- {
- _onNext = onNext ?? new Action<T>(x => { });
- _onError = onError ?? new Action<Exception>(x => { });
- _onCompleted = onCompleted ?? new Action(() => { });
- }
-
- public void OnNext(T value)
- {
- _onNext(value);
- }
-
- public void OnError(Exception error)
- {
- _onError(error);
- }
-
- public void OnCompleted()
- {
- _onCompleted();
- }
- }
}
}
View
1 OwinHelpers/OwinHelpers.csproj
@@ -40,6 +40,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="ActionObserver.cs" />
<Compile Include="Body.cs" />
<Compile Include="BodyExtensions.cs" />
<Compile Include="DelegateExtensions.cs" />
View
2 OwinHelpers/StringBody.cs
@@ -5,7 +5,7 @@
namespace OwinHelpers
{
- public class StringBody : IObservable<ArraySegment<byte>>
+ internal class StringBody : IObservable<ArraySegment<byte>>
{
private readonly byte[] _bytes;
View
15 Print/RequestPrinter.cs
@@ -3,8 +3,8 @@
using System.ComponentModel.Composition;
using System.Text;
using OwinHelpers;
-using App = System.Action<System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string,object>>, System.Func<byte[]>, System.Action<int, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>>, System.Action<System.Action<System.ArraySegment<byte>>, System.Action, System.Action<System.Exception>>>, System.Delegate>;
-using ResponseHandler = System.Action<int, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>>, System.Action<System.Action<System.ArraySegment<byte>>, System.Action, System.Action<System.Exception>>>;
+using App = System.Action<System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object>>, System.Func<byte[]>, System.Action<int, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>>, System.Action<System.Action<System.ArraySegment<byte>>, System.Action<System.IO.FileInfo>, System.Action, System.Action<System.Exception>>>, System.Delegate>;
+using ResponseHandler = System.Action<int, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>>, System.Action<System.Action<System.ArraySegment<byte>>, System.Action<System.IO.FileInfo>, System.Action, System.Action<System.Exception>>>;
namespace Print
{
@@ -16,7 +16,8 @@ public class RequestPrinter
{
try
{
- if (!env.GetScriptName().ToLower().Contains("/info"))
+ var scriptName = env.GetScriptName().ToLower();
+ if (!(scriptName.Contains("/info") || scriptName.Contains(".")))
{
HandlePrintRequest(env.ToDictionary(), responseHandler);
}
@@ -27,7 +28,7 @@ public class RequestPrinter
}
catch (Exception ex)
{
- responseHandler(0, null, new ExceptionBody(ex).ToAction());
+ responseHandler(0, null, Body.FromException(ex));
}
}
@@ -41,13 +42,13 @@ private static void HandlePrintRequest(IDictionary<string,object> env, ResponseH
builder.AppendFormat("<p><strong>{0}</strong>: {1}</p>", header.Key, header.Value);
}
builder.Append("</body></html>");
- var body = new StringBody(builder.ToString());
+ var body = Body.FromString(builder.ToString());
var headers = new Dictionary<string, string>
{
{ "Content-Type", "text/html" },
- { "Content-Length", body.Length.ToString() }
+ { "Content-Length", builder.Length.ToString() }
};
- responseHandler(200, headers, body.ToAction());
+ responseHandler(200, headers, body);
}
private static string ConstructUri(IDictionary<string,object> env)
View
4 TestModule/MethodDownshifter.cs
@@ -2,8 +2,8 @@
using System.Collections.Generic;
using System.ComponentModel.Composition;
using OwinHelpers;
-using App = System.Action<System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string,object>>, System.Func<byte[]>, System.Action<int, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>>, System.Action<System.Action<System.ArraySegment<byte>>, System.Action, System.Action<System.Exception>>>, System.Delegate>;
-using ResponseHandler = System.Action<int, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>>, System.Action<System.Action<System.ArraySegment<byte>>, System.Action, System.Action<System.Exception>>>;
+using App = System.Action<System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object>>, System.Func<byte[]>, System.Action<int, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>>, System.Action<System.Action<System.ArraySegment<byte>>, System.Action<System.IO.FileInfo>, System.Action, System.Action<System.Exception>>>, System.Delegate>;
+using ResponseHandler = System.Action<int, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>>, System.Action<System.Action<System.ArraySegment<byte>>, System.Action<System.IO.FileInfo>, System.Action, System.Action<System.Exception>>>;
namespace TestModule
{
View
29 TestServer/Server.cs
@@ -4,9 +4,9 @@
using System.Linq;
using System.Net;
using System.Text;
-using App = System.Action<System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string,object>>, System.Func<byte[]>, System.Action<int, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>>, System.Action<System.Action<System.ArraySegment<byte>>, System.Action, System.Action<System.Exception>>>, System.Delegate>;
-using ResponseHandler = System.Action<int, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>>, System.Action<System.Action<System.ArraySegment<byte>>, System.Action, System.Action<System.Exception>>>;
-using Body = System.Action<System.Action<System.ArraySegment<byte>>, System.Action, System.Action<System.Exception>>;
+using App = System.Action<System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string,object>>, System.Func<byte[]>, System.Action<int, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>>, System.Action<System.Action<System.ArraySegment<byte>>, System.Action<System.IO.FileInfo>, System.Action, System.Action<System.Exception>>>, System.Delegate>;
+using ResponseHandler = System.Action<int, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>>, System.Action<System.Action<System.ArraySegment<byte>>, System.Action<System.IO.FileInfo>, System.Action, System.Action<System.Exception>>>;
+using Body = System.Action<System.Action<System.ArraySegment<byte>>, System.Action<System.IO.FileInfo>, System.Action, System.Action<System.Exception>>;
namespace TestServer
{
public class Server : IDisposable
@@ -93,8 +93,15 @@ static void Respond(HttpListenerContext context, IEnumerable<KeyValuePair<string
context.Response.Headers[header.Key] = header.Value;
}
}
- var bodyObserver = new BodyObserver(context);
- body(bodyObserver.OnNext, bodyObserver.OnCompleted, bodyObserver.OnError);
+ if (body != null)
+ {
+ var bodyObserver = new BodyObserver(context);
+ body(bodyObserver.OnData, bodyObserver.OnFile, bodyObserver.OnCompleted, bodyObserver.OnError);
+ }
+ else
+ {
+ context.Response.Close();
+ }
}
catch (Exception ex)
{
@@ -117,7 +124,7 @@ private static string GetStatusText(int status)
{200, "OK"},
};
- private class BodyObserver : IObserver<ArraySegment<byte>>
+ private class BodyObserver
{
private readonly HttpListenerContext _context;
@@ -126,11 +133,19 @@ public BodyObserver(HttpListenerContext context)
_context = context;
}
- public void OnNext(ArraySegment<byte> value)
+ public void OnData(ArraySegment<byte> value)
{
_context.Response.OutputStream.Write(value.Array, value.Offset, value.Count);
}
+ public void OnFile(FileInfo fileInfo)
+ {
+ using (var stream = fileInfo.OpenRead())
+ {
+ stream.CopyTo(_context.Response.OutputStream);
+ }
+ }
+
public void OnError(Exception error)
{
_context.Response.Close();

0 comments on commit 7ae0341

Please sign in to comment.
Something went wrong with that request. Please try again.