Permalink
Browse files

CORS for crossdomain javascript access added

  • Loading branch information...
1 parent 20b99b4 commit cbaa4450b0b2503884cfda2af2041392458d3e5b @MikeBild MikeBild committed Mar 10, 2012
@@ -93,13 +93,6 @@ public HttpHead(HttpListenerRequest request, HttpListenerResponse response)
{
}
}
- public class HttpOptions : HttpContext
- {
- public HttpOptions(HttpListenerRequest request, HttpListenerResponse response)
- : base(request, response)
- {
- }
- }
public class HttpTrace : HttpContext
{
public HttpTrace(HttpListenerRequest request, HttpListenerResponse response)
@@ -1,6 +1,7 @@
using System;
using System.Reactive.Disposables;
using System.Reactive.Linq;
+using Minimod.HttpMessageStream.Utils;
using Minimod.MessageProcessor;
namespace Minimod.HttpMessageStream
@@ -17,7 +18,7 @@ public class RouteFoundMessage : IMessage
}
/// <summary>
- /// Minimod.HttpMessageStream, Version 0.0.5
+ /// Minimod.HttpMessageStream, Version 0.0.6
/// <para>Small embedded web server based on Rx and working like node.js.</para>
/// </summary>
/// <remarks>
@@ -33,7 +34,7 @@ public void OnReceive<T>(string uri, Func<IObservable<T>, IObservable<T>> action
{
var contextPipe = Stream
.OfType<HttpContext>()
- .Where(httpContext => httpContext.TryHandleRoute(uri))
+ .Where(httpContext => httpContext.Request.HttpMethod == "OPTIONS" || httpContext.TryHandleRoute(uri))
.Select(httpContext =>
{
switch (httpContext.Request.HttpMethod)
@@ -49,7 +50,7 @@ public void OnReceive<T>(string uri, Func<IObservable<T>, IObservable<T>> action
case "HEAD":
return new HttpHead(httpContext.ListenerRequest, httpContext.ListenerResponse) as T;
case "OPTIONS":
- return new HttpOptions(httpContext.ListenerRequest, httpContext.ListenerResponse) as T;
+ return new HttpGet(httpContext.ListenerRequest, httpContext.ListenerResponse) as T;
case "TRACE":
return new HttpTrace(httpContext.ListenerRequest, httpContext.ListenerResponse) as T;
default:
@@ -65,7 +66,16 @@ public void OnReceive<T>(string uri, Func<IObservable<T>, IObservable<T>> action
.OfType<HttpContext>()
.Do(httpContext =>
{
- try { httpContext.ExecuteResult().Send(httpContext.ListenerResponse); }
+ try
+ {
+ var result = httpContext.ExecuteResult();
+ result.Headers.Add("Access-Control-Allow-Origin", "*");
+ result.Headers.Add("Access-Control-Allow-Methods", "PUT,PATCH,GET,DELETE,POST");
+ result.Headers.Add("Access-Control-Max-Age", "1728000");
+ result.Headers.Add("Access-Control-Allow-Headers", "*");
+ result.Headers.Add("Origin", httpContext.Request.Url.GetServerBaseUri());
+ result.Send(httpContext.ListenerResponse);
+ }
catch (Exception error)
{
var message = error.Message;
@@ -7,7 +7,7 @@
namespace Minimod.HttpMessageStream
{
/// <summary>
- /// Minimod.HttpMessageStream, Version 0.0.5
+ /// Minimod.HttpMessageStream, Version 0.0.6
/// <para>Small embedded web server based on Rx and working like node.js.</para>
/// </summary>
/// <remarks>
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
- <version>0.0.5</version>
+ <version>0.0.6</version>
<authors>Mike Bild</authors>
<owners>Mike Bild</owners>
<licenseUrl>http://www.apache.org/licenses/LICENSE-2.0</licenseUrl>
@@ -1,113 +1,113 @@
-using System;
-using System.Collections.Generic;
-using System.IO;
-using System.Linq;
-using System.Net;
-using System.Reactive.Linq;
-using System.Text;
-
-namespace Minimod.HttpMessageStream
-{
- public abstract class Result
- {
- public int StatusCode { get; set; }
- public IDictionary<string, string> Headers { get; set; }
- protected Result(int statusCode = 200)
- {
- Headers = new Dictionary<string, string>();
- StatusCode = statusCode;
- }
-
- protected virtual IObservable<Stream> WriteStream(Stream stream)
- {
- return Observable.Return(stream);
- }
-
- public void Send(HttpListenerResponse listenerResponse)
- {
- Headers
- .Where(r => r.Key != "Content-Type")
- .ForEach(header => listenerResponse.AddHeader(header.Key, header.Value));
-
- listenerResponse.ContentType = Headers["Content-Type"];
- listenerResponse.StatusCode = StatusCode;
- WriteStream(listenerResponse.OutputStream)
- .Do(stream =>
- {
- try
- {
- stream.Close(); stream.Dispose();
- }
- catch
- {
- }
-
- }, error => { try { listenerResponse.StatusCode = 500; listenerResponse.OutputStream.Close(); } catch { } })
- .Retry()
- .Subscribe();
- }
- }
- public class HtmlTextResult : Result
- {
- public HtmlTextResult(int statusCode = 200)
- : base(statusCode)
- {
- Headers.Add("Content-Type", "text/html");
- }
-
- }
- public class EmptyHtmlTextResult : HtmlTextResult
- {
- public EmptyHtmlTextResult(int statusCode = 204)
- {
- StatusCode = statusCode;
- }
- }
- public class BinaryHtmlTextResult : HtmlTextResult
- {
- private readonly byte[] _binary;
-
- public BinaryHtmlTextResult(byte[] binary, int statusCode = 200)
- : base(statusCode)
- {
- _binary = binary;
- }
-
- protected override IObservable<Stream> WriteStream(Stream stream)
- {
- stream.Write(_binary, 0, _binary.Length);
- return Observable.Return(stream);
- }
- }
- public class StringHtmlTextResult : BinaryHtmlTextResult
- {
- public StringHtmlTextResult(string message, int statusCode = 200)
- : base(Encoding.UTF8.GetBytes(message), statusCode)
- {
- }
- }
- public class StaticFileHtmlTextResult : HtmlTextResult
- {
- private readonly string _file;
-
- public StaticFileHtmlTextResult(string file)
- {
- _file = file;
- }
-
- protected override IObservable<Stream> WriteStream(Stream stream)
- {
- var data = File.ReadAllBytes(_file);
- try
- {
- stream.Write(data, 0, data.Length);
- }
- catch (Exception)
- {
- stream.Write(new byte[0], 0, 0);
- }
-
- return Observable.Return(stream);
- }
- }
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Net;
+using System.Reactive.Linq;
+using System.Text;
+
+namespace Minimod.HttpMessageStream
+{
+ public abstract class Result
+ {
+ public int StatusCode { get; set; }
+ public IDictionary<string, string> Headers { get; set; }
+ protected Result(int statusCode = 200)
+ {
+ Headers = new Dictionary<string, string>();
+ StatusCode = statusCode;
+ }
+
+ protected virtual IObservable<Stream> WriteStream(Stream stream)
+ {
+ return Observable.Return(stream);
+ }
+
+ public void Send(HttpListenerResponse listenerResponse)
+ {
+ Headers
+ .Where(r => r.Key != "Content-Type")
+ .ForEach(header => listenerResponse.AddHeader(header.Key, header.Value));
+
+ listenerResponse.ContentType = Headers["Content-Type"];
+ listenerResponse.StatusCode = StatusCode;
+ WriteStream(listenerResponse.OutputStream)
+ .Do(stream =>
+ {
+ try
+ {
+ stream.Close(); stream.Dispose();
+ }
+ catch
+ {
+ }
+
+ }, error => { try { listenerResponse.StatusCode = 500; listenerResponse.OutputStream.Close(); } catch { } })
+ .Retry()
+ .Subscribe();
+ }
+ }
+ public class HtmlTextResult : Result
+ {
+ public HtmlTextResult(int statusCode = 200)
+ : base(statusCode)
+ {
+ Headers.Add("Content-Type", "text/html");
+ }
+
+ }
+ public class EmptyHtmlTextResult : HtmlTextResult
+ {
+ public EmptyHtmlTextResult(int statusCode = 204)
+ {
+ StatusCode = statusCode;
+ }
+ }
+ public class BinaryHtmlTextResult : HtmlTextResult
+ {
+ private readonly byte[] _binary;
+
+ public BinaryHtmlTextResult(byte[] binary, int statusCode = 200)
+ : base(statusCode)
+ {
+ _binary = binary;
+ }
+
+ protected override IObservable<Stream> WriteStream(Stream stream)
+ {
+ stream.Write(_binary, 0, _binary.Length);
+ return Observable.Return(stream);
+ }
+ }
+ public class StringHtmlTextResult : BinaryHtmlTextResult
+ {
+ public StringHtmlTextResult(string message, int statusCode = 200)
+ : base(Encoding.UTF8.GetBytes(message), statusCode)
+ {
+ }
+ }
+ public class StaticFileHtmlTextResult : HtmlTextResult
+ {
+ private readonly string _file;
+
+ public StaticFileHtmlTextResult(string file)
+ {
+ _file = file;
+ }
+
+ protected override IObservable<Stream> WriteStream(Stream stream)
+ {
+ var data = File.ReadAllBytes(_file);
+ try
+ {
+ stream.Write(data, 0, data.Length);
+ }
+ catch (Exception)
+ {
+ stream.Write(new byte[0], 0, 0);
+ }
+
+ return Observable.Return(stream);
+ }
+ }
}

0 comments on commit cbaa445

Please sign in to comment.