public void All_environment_variables_from_spec_are_available_as_typed_properties() { var headers = new Dictionary<string, string>(); var body = new BodyDelegate((next, error, complete) => () => { }).ToAction(); var env = new Dictionary<string, object> { {"owin.RequestMethod", "GET"}, {"owin.RequestPath", "/foo"}, {"owin.RequestHeaders", headers}, {"owin.RequestBody", body}, {"owin.RequestPathBase", "/my-app"}, {"owin.RequestQueryString", "hello=world"}, {"owin.RequestScheme", "https"}, {"owin.Version", "1.0"}, }; var environment = new Environment(env); Assert.That(environment.Method, Is.EqualTo("GET")); Assert.That(environment.Path, Is.EqualTo("/foo")); Assert.That(environment.Headers, Is.SameAs(headers)); Assert.That(environment.Body, Is.SameAs(body)); Assert.That(environment.PathBase, Is.EqualTo("/my-app")); Assert.That(environment.QueryString, Is.EqualTo("hello=world")); Assert.That(environment.Scheme, Is.EqualTo("https")); Assert.That(environment.Version, Is.EqualTo("1.0")); }
static BodyDelegate RescheduleBody(IScheduler theScheduler, BodyDelegate body) { // flush and end are tranported on theScheduler. // write is not, because you want the return value to be // false when the data is not buffering. return (write, end, cancel) => theScheduler.Post(() => body( (data, callback) => { if (callback == null) { return write(data, callback); } theScheduler.Post(() => { if (!write(data, () => { theScheduler.Post(callback); })) callback.Invoke(); }); return true; }, ex => theScheduler.Post(() => end(ex)), cancel)); }
static BodyDelegate WrapBodyDelegate(ExecutionContext context, BodyDelegate body) { return body == null ? (BodyDelegate)null : (write, flush, end, cancellationToken) => ExecutionContext.Run( context.CreateCopy(), _ => body(write, WrapFlushDelegate(context, flush), end, cancellationToken), null); }
public FakeApp(string status, BodyDelegate body) { Status = status ?? "200 OK"; Headers = Gate.Headers.New(); Body = body; AppDelegate = Call; }
public static BodyDelegate WrapOutputStream(BodyDelegate body) { return (output, cancel) => body(new StreamWrapper(output, OnWriteFilter), cancel) .Finally(() => output.Write(FinalChunk.Array, FinalChunk.Offset, FinalChunk.Count)); }
public void All_environment_variables_from_spec_are_available_as_typed_properties() { var headers = new Dictionary <string, string>(); BodyDelegate body = (next, error, complete) => () => { }; var env = new Dictionary <string, object> { { "owin.RequestMethod", "GET" }, { "owin.RequestUri", "/foo" }, { "owin.RequestHeaders", headers }, { "owin.RequestBody", body }, { "owin.BaseUri", "/my-app" }, { "owin.ServerName", "localhost" }, { "owin.ServerPort", "8080" }, { "owin.UriScheme", "https" }, { "owin.RemoteEndPoint", new IPEndPoint(IPAddress.Parse("127.0.0.1") ?? IPAddress.None, 80) }, { "owin.Version", "1.0" }, }; var environment = new Environment(env); Assert.That(environment.Method, Is.EqualTo("GET")); Assert.That(environment.RequestUri, Is.EqualTo("/foo")); Assert.That(environment.Headers, Is.SameAs(headers)); Assert.That(environment.Body, Is.SameAs(body)); Assert.That(environment.BaseUri, Is.EqualTo("/my-app")); Assert.That(environment.ServerName, Is.EqualTo("localhost")); Assert.That(environment.ServerPort, Is.EqualTo("8080")); Assert.That(environment.UriScheme, Is.EqualTo("https")); Assert.That(environment.RemoteEndPoint.ToString(), Is.EqualTo("127.0.0.1:80")); Assert.That(environment.Version, Is.EqualTo("1.0")); }
public FakeApp(string status, BodyDelegate body) { Status = status ?? "200 OK"; Headers = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase); Body = body; AppDelegate = Call; }
public FakeApp ForSynchronousResult(string status, IDictionary<string, string> headers, BodyDelegate body) { CallResultSynchronously = true; ResultStatus = status; ResultHeaders = headers; ResultBody = body; return this; }
public RequestWrapper(IDictionary<string, object> environment) { env = environment; headers = (IDictionary<string, IEnumerable<string>>)environment[OwinConstants.RequestHeaders]; bodyDelegate = (BodyDelegate)environment[OwinConstants.RequestBody]; bodyDelegate(reader, flush => false, ex=> { }, CancellationToken.None); }
public FakeApp ForSynchronousResult(string status, IDictionary <string, string> headers, BodyDelegate body) { CallResultSynchronously = true; ResultStatus = status; ResultHeaders = headers; ResultBody = body; return(this); }
public static BodyDelegate Wrap(BodyDelegate body) { if (body == null) { return null; } if (body.Method == RewindableBodyInvoke) { return body; } return new Wrapper(body.ToAction(), DefaultTempFileThresholdBytes).Invoke; }
static BodyDelegate WrapBodyDelegate(ExecutionContext context, BodyDelegate body) { if (body == null) { return null; } return (write, end, cancellationToken) => ExecutionContext.Run( context.CreateCopy(), _ => body(WrapWriteDelegate(context, write), end, cancellationToken), null); }
static BodyDelegate RescheduleBody(IScheduler theScheduler, BodyDelegate body) { // flush and end are tranported on theScheduler. // write is not, because you want the return value to be // false when the data is not buffering. return (write, flush, end, cancel) => theScheduler.Post(() => body( write, drained => { theScheduler.Post(() => { if (!flush(() => if (drained != null) theScheduler.Post(drained))) if (drained != null) drained.Invoke(); });
/// <summary> /// Invoke the body delegate /// </summary> /// <param name="bodyDelegate">The body delegate to invoke</param> public void InvokeBodyDelegate(BodyDelegate bodyDelegate, bool waitForComplete = true) { if (bodyDelegate == null) { throw new ArgumentNullException("bodyDelegate"); } this.sync.Reset(); this.dataStream = new MemoryStream(); this.cancelDelegate = bodyDelegate.Invoke(this.DataConsumer, this.OnError, this.OnComplete); this.bodyDelegateInvoked = true; if (waitForComplete) { this.sync.Wait(); } }
public void Environment_properties_may_be_used_to_initialize_env_dictionary() { var headers = new Dictionary <string, string>(); BodyDelegate body = (next, error, complete) => () => { }; var env = new Dictionary <string, object>(); var environment = new Environment(env) { Method = "GET", RequestUri = "/foo", Headers = headers, Body = body, BaseUri = "/my-app", ServerName = "localhost", ServerPort = "8080", UriScheme = "https", RemoteEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 80), Version = "1.0" }; Assert.That(environment.Method, Is.EqualTo("GET")); Assert.That(environment.RequestUri, Is.EqualTo("/foo")); Assert.That(environment.Headers, Is.SameAs(headers)); Assert.That(environment.Body, Is.SameAs(body)); Assert.That(environment.BaseUri, Is.EqualTo("/my-app")); Assert.That(environment.ServerName, Is.EqualTo("localhost")); Assert.That(environment.ServerPort, Is.EqualTo("8080")); Assert.That(environment.UriScheme, Is.EqualTo("https")); Assert.That(environment.RemoteEndPoint.ToString(), Is.EqualTo("127.0.0.1:80")); Assert.That(environment.Version, Is.EqualTo("1.0")); Assert.That(env["owin.RequestMethod"], Is.EqualTo("GET")); Assert.That(env["owin.RequestUri"], Is.EqualTo("/foo")); Assert.That(env["owin.RequestHeaders"], Is.SameAs(headers)); Assert.That(env["owin.RequestBody"], Is.SameAs(body)); Assert.That(env["owin.BaseUri"], Is.EqualTo("/my-app")); Assert.That(env["owin.ServerName"], Is.EqualTo("localhost")); Assert.That(env["owin.ServerPort"], Is.EqualTo("8080")); Assert.That(env["owin.UriScheme"], Is.EqualTo("https")); Assert.That(env["owin.RemoteEndPoint.ToString()"], Is.EqualTo("127.0.0.1:80")); Assert.That(env["owin.Version"], Is.EqualTo("1.0")); }
public void All_environment_variables_from_spec_are_available_as_typed_properties() { //"owin.RequestMethod" A string containing the HTTP request method of the request (e.g., "GET", "POST"). //"owin.RequestUri" A string containing the HTTP request URI of the request. The value must include the query string of the HTTP request URI (e.g., "/path/and?query=string"). The URI must be relative to the application delegate; see Paths. //"owin.RequestHeaders" An instance of IDictionary<string, string> which represents the HTTP headers present in the request (the request header dictionary); see Headers. //"owin.RequestBody" An instance of the body delegate representing the body of the request. May be null. //"owin.BaseUri" A string containing the portion of the request URI's path corresponding to the "root" of the application object. See Paths. //"owin.ServerName", "owin.ServerPort" Hosts should provide values which can be used to reconstruct the full URI of the request in absence of the HTTP Host header of the request. //"owin.UriScheme" A string representing the URI scheme (e.g. "http", "https") //"owin.RemoteEndPoint" A System.Net.IPEndPoint representing the connected client. //"owin.Version" The string "1.0" indicating OWIN version 1.0. var headers = new Dictionary <string, string>(); BodyDelegate body = (next, error, complete) => () => { }; var env = new Dictionary <string, object> { { "owin.RequestMethod", "GET" }, { "owin.RequestUri", "/foo" }, { "owin.RequestHeaders", headers }, { "owin.RequestBody", body }, { "owin.BaseUri", "/my-app" }, { "owin.ServerName", "localhost" }, { "owin.ServerPort", "8080" }, { "owin.UriScheme", "https" }, { "owin.RemoteEndPoint", new IPEndPoint(IPAddress.Parse("127.0.0.1") ?? IPAddress.None, 80) }, { "owin.Version", "1.0" }, }; var request = new Request(env); Assert.That(request.Method, Is.EqualTo("GET")); Assert.That(request.RequestUri, Is.EqualTo("/foo")); Assert.That(request.Headers, Is.SameAs(headers)); Assert.That(request.Body, Is.SameAs(body)); Assert.That(request.BaseUri, Is.EqualTo("/my-app")); Assert.That(request.ServerName, Is.EqualTo("localhost")); Assert.That(request.ServerPort, Is.EqualTo("8080")); Assert.That(request.UriScheme, Is.EqualTo("https")); Assert.That(request.RemoteEndPoint.ToString(), Is.EqualTo("127.0.0.1:80")); Assert.That(request.Version, Is.EqualTo("1.0")); }
public void Environment_properties_may_be_used_to_initialize_env_dictionary() { var headers = new Dictionary<string, string>(); var body = new BodyDelegate((next, error, complete) => () => { }).ToAction(); var environment = new Environment() { Method = "GET", Path = "/foo", Headers = headers, Body = body, PathBase = "/my-app", QueryString = "hello=world", Scheme = "https", Version = "1.0" }; IDictionary<string, object> env = environment; Assert.That(environment.Method, Is.EqualTo("GET")); Assert.That(environment.Path, Is.EqualTo("/foo")); Assert.That(environment.Headers, Is.SameAs(headers)); Assert.That(environment.Body, Is.SameAs(body)); Assert.That(environment.PathBase, Is.EqualTo("/my-app")); Assert.That(environment.QueryString, Is.EqualTo("hello=world")); Assert.That(environment.Scheme, Is.EqualTo("https")); Assert.That(environment.Version, Is.EqualTo("1.0")); Assert.That(env["owin.RequestMethod"], Is.EqualTo("GET")); Assert.That(env["owin.RequestPath"], Is.EqualTo("/foo")); Assert.That(env["owin.RequestHeaders"], Is.SameAs(headers)); Assert.That(env["owin.RequestBody"], Is.SameAs(body)); Assert.That(env["owin.RequestPathBase"], Is.EqualTo("/my-app")); Assert.That(env["owin.RequestQueryString"], Is.EqualTo("hello=world")); Assert.That(env["owin.RequestScheme"], Is.EqualTo("https")); Assert.That(env["owin.Version"], Is.EqualTo("1.0")); }
private void RedirectIfUnauthorized(string status, IDictionary<string, IEnumerable<string>> headers, BodyDelegate body) { if (status.ToLower() == "401 unauthorized") { var to = string.Format("{0}?client_id={1}&redirect_url={2}", m_config.AuthorizeEndpoint, m_config.ClientId, m_env.GetUri()); m_ctx.Redirect(to); } else { m_result(status, headers, body); } }
static Message CreateOwinResponse( WebOperationContext webResponse, string status, IDictionary<string, string> headers, BodyDelegate body) { //TODO: hardenning var statusCode = int.Parse(status.Substring(0, 3)); webResponse.OutgoingResponse.StatusCode = (HttpStatusCode) statusCode; webResponse.OutgoingResponse.StatusDescription = status.Substring(4); foreach (var header in Split(headers)) { webResponse.OutgoingResponse.Headers.Add(header.Key, header.Value); } string contentType; if (!headers.TryGetValue("Content-Type", out contentType)) contentType = "text/plain"; return webResponse.CreateStreamResponse( stream => { var done = new ManualResetEvent(false); body( (data, _) => { stream.Write(data.Array, data.Offset, data.Count); return false; }, ex => done.Set(), () => done.Set() ); done.WaitOne(); }, contentType); }
void DefaultResult(string status, IDictionary<string, IEnumerable<string>> headers, BodyDelegate body) { }
public void Go(BodyDelegate body) { body(OnWrite, OnFlush, OnEnd, CancellationToken); }
static void ResponseBody(BodyDelegate body, Stream stream, Action<Exception> error, Action complete) { new PipeResponse(stream, error, complete).Go(body); }
public DataProducer(BodyDelegate del) { this.del = del; }
public StaticApp(string status, IDictionary<string, string> headers, BodyDelegate body) { this.status = status; this.headers = headers; this.body = body; }
public void Init() { _status = null; _headers = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); _body = null; }
public AppWebRequest( RawRequestData aRawRequestData, Dictionary<string, IEnumerable<string>> aDefaultResponseHeaders, ResultDelegate aResult, BodyDelegate aBody) { RawRequestData = aRawRequestData; DefaultResponseHeaders = aDefaultResponseHeaders; iResult = aResult; Body = aBody; }
public void ServeLongPoll(string aStatus, Dictionary<string, IEnumerable<string>> aHeaders, string aContentType, BodyDelegate aBodyDelegate) { Dictionary<string, IEnumerable<string>> headers = new Dictionary<string, IEnumerable<string>>(aHeaders); //headers["Content-Length"] = new[] { aPageSource.ContentLength.ToString() }; headers["Content-Type"] = new[] { aContentType }; iResult(aStatus, headers, aBodyDelegate); }
public void SendResult(string aStatus, IDictionary<string, IEnumerable<string>> aHeaders, BodyDelegate aBody) { iResult(aStatus, aHeaders, aBody); }
static HttpResponseMessage MakeResponseMessage(int status, IDictionary<string, string[]> headers, BodyDelegate body, IDictionary<string, object> properties, CancellationToken cancel) { var response = new HttpResponseMessage((HttpStatusCode)status); if (body != null) { response.Content = new BodyDelegateHttpContent(body, cancel); } if (properties != null) { object value; if (properties.TryGetValue("owin.ReasonPhrase", out value)) { response.ReasonPhrase = Convert.ToString(value); } } if (headers != null) { foreach (var header in headers) { if (!response.Headers.TryAddWithoutValidation(header.Key, header.Value)) { if (response.Content == null) { response.Content = new ByteArrayContent(new byte[0]); } response.Content.Headers.TryAddWithoutValidation(header.Key, header.Value); } } } return response; }
public BodyDelegateHttpContent(BodyDelegate body, CancellationToken cancel) { _body = body; _cancel = cancel; }
/// <summary> /// Invoke the body delegate /// </summary> /// <param name="bodyDelegate">The body delegate to invoke</param> public void InvokeBodyDelegate(BodyDelegate bodyDelegate, bool waitForComplete = true) { if (bodyDelegate == null) { throw new ArgumentNullException("bodyDelegate"); } this.sync.Reset(); this.dataStream = new MemoryStream(); if (waitForComplete) { ThreadPool.QueueUserWorkItem(_ => { bodyDelegate.Invoke(this.OnWrite, this.OnEnd, this.CancellationToken); this.bodyDelegateInvoked = true; }); this.sync.Wait(); } else { bodyDelegate.Invoke(this.OnWrite, this.OnEnd, this.CancellationToken); this.bodyDelegateInvoked = true; } }
private static BodyDelegate BaseFramingProtocol( BodyDelegate requestBody, Func<Action<int, ArraySegment<byte>>, Action<int, ArraySegment<byte>>> service) { return (write, flush, end, cancel) => { Action<int, ArraySegment<byte>> outgoing = (opcode, data) => { Console.WriteLine("Outgoing opcode:{0}", opcode); var bytes = new byte[data.Count + 2]; bytes[0] = (byte)(0x80 | opcode); bytes[1] = (byte)data.Count; Array.Copy(data.Array, data.Offset, bytes, 2, data.Count); write(new ArraySegment<byte>(bytes, 0, bytes.Length)); }; var incoming = service(outgoing); var buffer = new ArraySegment<byte>(new byte[128], 0, 0); requestBody.Invoke( data => { buffer = Concat(buffer, data); var header = 2; if (buffer.Count < header) { return false; } var ch0 = buffer.Array[buffer.Offset]; var ch1 = buffer.Array[buffer.Offset + 1]; var fin = (ch0 >> 7) & 0x01; var opcode = (ch0 >> 0) & 0x0f; var mask = (ch1 >> 7) & 0x01; var maskKey = new byte[] {0, 0, 0, 0}; var len = (ch1 >> 0) & 0x7f; if (len == 126) { header = 4; if (buffer.Count < header) { return false; } len = (buffer.Array[buffer.Offset + 2] * 0x100) + buffer.Array[buffer.Offset + 3]; } else if (len == 127) { header = 10; if (buffer.Count < header) { return false; } len = (buffer.Array[buffer.Offset + 6] * 0x1000000) + (buffer.Array[buffer.Offset + 7] * 0x10000) + (buffer.Array[buffer.Offset + 8] * 0x100) + buffer.Array[buffer.Offset + 9]; } if (mask == 1) { header += 4; if (buffer.Count < header) { return false; } maskKey[0] = buffer.Array[buffer.Offset + header - 4]; maskKey[1] = buffer.Array[buffer.Offset + header - 3]; maskKey[2] = buffer.Array[buffer.Offset + header - 2]; maskKey[3] = buffer.Array[buffer.Offset + header - 1]; } if (buffer.Count < header + len) { return false; } Console.WriteLine("fin:{0} opcode:{1} mask:{2} len:{3}", fin, opcode, mask, len); if (mask == 1) { for (var index = 0; index != len; ++index) { buffer.Array[buffer.Offset + header + index] = (byte)(buffer.Array[buffer.Offset + header + index] ^ maskKey[index & 0x03]); } } var messageBody = new ArraySegment<byte>(buffer.Array, buffer.Offset + header, len); buffer = new ArraySegment<byte>( buffer.Array, buffer.Offset + header + len, buffer.Count - header - len); incoming(opcode, messageBody); return false; }, _ => false, ex => { Console.WriteLine("complete"); end(ex); }, cancel); }; }
void Result(string status, IDictionary<string, string> headers, BodyDelegate body) { _status = status; _headers = headers; _body = body; }
public RequestHttpContent(BodyDelegate body, CancellationToken cancellationToken) { _body = body; _cancellationToken = cancellationToken; }