public static object RegisterAuthenticationHandler(this IOwinRequest request, AuthenticationHandler handler)
        {
            var chained = request.Get <AuthenticateDelegate>(Constants.SecurityAuthenticate);
            var hook    = new Hook(handler, chained);

            request.Set <AuthenticateDelegate>(Constants.SecurityAuthenticate, hook.AuthenticateAsync);
            return(hook);
        }
        public static void UnregisterAuthenticationHandler(this IOwinRequest request, object registration)
        {
            var hook = registration as Hook;

            if (hook == null)
            {
                throw new InvalidOperationException(Resources.Exception_UnhookAuthenticationStateType);
            }
            request.Set(Constants.SecurityAuthenticate, hook.Chained);
        }
Example #3
0
        internal static IDictionary <string, string> GetCookies(IOwinRequest request)
        {
            var cookies = request.Get <IDictionary <string, string> >("Microsoft.Owin.Cookies#dictionary");

            if (cookies == null)
            {
                cookies = new Dictionary <string, string>(StringComparer.Ordinal);
                request.Set("Microsoft.Owin.Cookies#dictionary", cookies);
            }

            string text = GetHeader(request.Headers, "Cookie");

            if (request.Get <string>("Microsoft.Owin.Cookies#text") != text)
            {
                cookies.Clear();
                ParseDelimited(text, SemicolonAndComma, AddCookieCallback, decodePlus: false, decodeKey: false, state: cookies);
                request.Set("Microsoft.Owin.Cookies#text", text);
            }
            return(cookies);
        }
Example #4
0
            internal RequestState(HttpRequestMessage request, CancellationToken cancellationToken)
            {
                _request        = request;
                _responseTcs    = new TaskCompletionSource <HttpResponseMessage>();
                _sendingHeaders = () => { };

                if (request.RequestUri.IsDefaultPort)
                {
                    request.Headers.Host = request.RequestUri.Host;
                }
                else
                {
                    request.Headers.Host = request.RequestUri.GetComponents(UriComponents.HostAndPort, UriFormat.UriEscaped);
                }

                OwinContext = new OwinContext();
                OwinContext.Set("owin.Version", "1.0");
                IOwinRequest owinRequest = OwinContext.Request;

                owinRequest.Protocol      = "HTTP/" + request.Version.ToString(2);
                owinRequest.Scheme        = request.RequestUri.Scheme;
                owinRequest.Method        = request.Method.ToString();
                owinRequest.Path          = PathString.FromUriComponent(request.RequestUri);
                owinRequest.PathBase      = PathString.Empty;
                owinRequest.QueryString   = QueryString.FromUriComponent(request.RequestUri);
                owinRequest.CallCancelled = cancellationToken;
                owinRequest.Set <Action <Action <object>, object> >("server.OnSendingHeaders", (callback, state) =>
                {
                    var prior       = _sendingHeaders;
                    _sendingHeaders = () =>
                    {
                        prior();
                        callback(state);
                    };
                });

                foreach (var header in request.Headers)
                {
                    owinRequest.Headers.AppendValues(header.Key, header.Value.ToArray());
                }
                HttpContent requestContent = request.Content;

                if (requestContent != null)
                {
                    foreach (var header in request.Content.Headers)
                    {
                        owinRequest.Headers.AppendValues(header.Key, header.Value.ToArray());
                    }
                }

                _responseStream                 = new ResponseStream(CompleteResponse);
                OwinContext.Response.Body       = _responseStream;
                OwinContext.Response.StatusCode = 200;
            }
Example #5
0
        public static Task <IFormCollection> ReadFormAsync(this IOwinRequest value)
        {
            var form = value.Get <IFormCollection>("Microsoft.Owin.Form#collection");

            if (form == null)
            {
                value.Body.Seek(0, System.IO.SeekOrigin.Begin); // needed so ReadToEndAsync() doesn't hang
                string text;
                // Don't close, it prevents re-winding.
                using (var reader = new System.IO.StreamReader(value.Body, Encoding.UTF8, detectEncodingFromByteOrderMarks: true, bufferSize: 4 * 1024, leaveOpen: true)) {
                    // *not* async - we're seeing hangs.  See another with this issue: https://github.com/damianh/LibOwin/issues/12
                    text = reader.ReadToEnd();
                }
                value.Body.Seek(0, System.IO.SeekOrigin.Begin); // subsequent calls will return nothing if this isn't re-wound
                form = GetForm(text);
                value.Set("Microsoft.Owin.Form#collection", form);
            }
            return(Task.FromResult(form));
        }
Example #6
0
        protected override void Push(IOwinRequest request, PushFunc pushPromise, string pushReference)
        {
            request.Set(CommonOwinKeys.AdditionalInfo, pushReference);
            // Copy the headers
            var headers = new HeaderDictionary(
                new Dictionary <string, string[]>(request.Headers, StringComparer.OrdinalIgnoreCase));

            // Populate special HTTP2 headers
            headers[CommonHeaders.Method] = request.Method;
            // TODO: Not all methods are allowed for push.  Don't push, or change to GET?
            headers[CommonHeaders.Scheme] = request.Scheme;
            headers.Remove("Host");
            headers[CommonHeaders.Authority] = request.Headers["Host"];
            headers[CommonHeaders.Path]      = BingRequestProcessor.GetTileQuadFromSoapUrl(pushReference);
            headers.Remove(CommonHeaders.ContentLength); // Push promises cannot emulate requests with bodies.

            // TODO: What about cache headers? If-Match, If-None-Match, If-Modified-Since, If-Unmodified-Since.
            // If-Match & If-None-Match are multi-value so the client could send e-tags for the primary resource and referenced resources.
            // If-Modified-Since and If-Unmodified-Since are single value, so it may not make sense to apply them for secondary resources.

            pushPromise(headers);
        }
        protected override void Push(IOwinRequest request, PushFunc pushPromise, string pushReference)
        {
            request.Set(CommonOwinKeys.AdditionalInfo, pushReference);
            // Copy the headers
            var headers = new HeaderDictionary(
                new Dictionary<string, string[]>(request.Headers, StringComparer.OrdinalIgnoreCase));

            // Populate special HTTP2 headers
            headers[CommonHeaders.Method] = request.Method;
                // TODO: Not all methods are allowed for push.  Don't push, or change to GET?
            headers[CommonHeaders.Scheme] = request.Scheme;
            headers.Remove("Host");
            headers[CommonHeaders.Authority] = request.Headers["Host"];
            headers[CommonHeaders.Path] = BingRequestProcessor.GetTileQuadFromSoapUrl(pushReference);
            headers.Remove(CommonHeaders.ContentLength); // Push promises cannot emulate requests with bodies.

            // TODO: What about cache headers? If-Match, If-None-Match, If-Modified-Since, If-Unmodified-Since.
            // If-Match & If-None-Match are multi-value so the client could send e-tags for the primary resource and referenced resources.
            // If-Modified-Since and If-Unmodified-Since are single value, so it may not make sense to apply them for secondary resources.

            pushPromise(headers);
        }
Example #8
0
 public IOwinRequest Set <T>(string key, T value)
 {
     return(_request.Set <T>(key, value));
 }
Example #9
0
 public static void SetServiceUnitContext(this IOwinRequest request, ServiceUnitContext suContext)
 {
     request.Set <ServiceUnitContext>(ServiceUnitContextKey, suContext);
 }