public Task Invoke(IDictionary <string, object> arg)
        {
            var env = new OwinEnvironment(arg);

            if (env.TryGetValue(OwinEnvironment.PeerCastStation.AccessControlInfo, out AccessControlInfo acinfo) &&
                (acinfo.Accepts & acceptType) != 0)
            {
                if (acinfo.AuthorizationRequired)
                {
                    if (CheckAuthorization(GetAuthorizationToken(env), acinfo))
                    {
                        return(nextApp.Invoke(arg));
                    }
                    else
                    {
                        env.Environment[OwinEnvironment.Owin.ResponseStatusCode] = 401;
                        env.SetResponseHeader("WWW-Authenticate", "Basic realm=\"PeerCastStation\"");
                        return(Task.Delay(0));
                    }
                }
                else
                {
                    return(nextApp.Invoke(arg));
                }
            }
            else
            {
                env.Environment[OwinEnvironment.Owin.ResponseStatusCode] = 403;
                return(Task.Delay(0));
            }
        }
        public Task Invoke(IDictionary <string, object> arg)
        {
            var env       = new OwinEnvironment(arg);
            var pathMatch = pathPattern.Match(env.Request.Path);

            if (pathMatch.Success)
            {
                if (env.TryGetValue(OwinEnvironment.Owin.RequestMethod, out string method) && methods.Contains(method))
                {
                    env.Response.StatusCode = System.Net.HttpStatusCode.OK;
                    env.Environment[OwinEnvironment.Owin.RequestPathMatch] = pathMatch;
                    var pathBase = env.Get(OwinEnvironment.Owin.RequestPathBase, "");
                    env.Environment[OwinEnvironment.Owin.RequestPathBase] = pathBase + pathMatch.Value;
                    env.Environment[OwinEnvironment.Owin.RequestPath]     = env.Request.Path.Substring(pathMatch.Length);
                    env.RemoveResponseHeader("Allow");
                    return(branch.Invoke(arg));
                }
                else
                {
                    env.Response.StatusCode = System.Net.HttpStatusCode.MethodNotAllowed;
                    var allow = env.GetResponseHeader("Allow", (string)null);
                    if (allow == null)
                    {
                        allow = String.Join(", ", methods);
                    }
                    else
                    {
                        allow = String.Join(", ", Enumerable.Repeat(allow, 1).Concat(methods));
                    }
                    env.SetResponseHeader("Allow", allow);
                }
            }
            return(nextApp.Invoke(arg));
        }
        private string GetAuthorizationToken(OwinEnvironment env)
        {
            string result = null;
            var    auth   = env.GetRequestHeader("Authorization", (string)null);

            if (auth != null)
            {
                var md = System.Text.RegularExpressions.Regex.Match(
                    auth,
                    @"\s*Basic (\S+)", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                if (md.Success)
                {
                    result = md.Groups[1].Value;
                }
            }
            if (result == null)
            {
                var query = env.GetQueryParameters();
                query.TryGetValue("auth", out result);
            }
            if (result == null)
            {
                var cookies = env.GetRequestCookies();
                cookies.TryGetValue("auth", out result);
            }
            return(result);
        }
Esempio n. 4
0
 internal OwinRequest(OwinEnvironment owner)
 {
     env     = owner;
     Headers = new RequestHeaders(env);
     Query   = new RequestQuery(env);
     Cookies = new RequestCookies(env);
     Body    = env.Get <System.IO.Stream>(Owin.RequestBody);
 }
Esempio n. 5
0
        public Task Invoke(IDictionary <string, object> arg)
        {
            var env = new OwinEnvironment(arg);

            if (env.TryGetValue(OwinEnvironment.Owin.RequestMethod, out string method) && methods.Contains(method))
            {
                return(branch.Invoke(arg));
            }
            else
            {
                return(nextApp.Invoke(arg));
            }
        }
        public Task Invoke(IDictionary <string, object> arg)
        {
            var env = new OwinEnvironment(arg);

            if (env.TryGetValue(OwinEnvironment.Owin.RequestMethod, out string method) && methods.Contains(method))
            {
                return(nextApp.Invoke(arg));
            }
            else
            {
                env.Environment[OwinEnvironment.Owin.ResponseStatusCode] = (int)HttpStatusCode.MethodNotAllowed;
                env.SetResponseHeader("Allow", String.Join(",", methods));
                return(Task.Delay(0));
            }
        }
Esempio n. 7
0
        public OwinContext(
            PeerCast peerCast,
            HttpRequest req,
            ConnectionStream stream,
            IPEndPoint localEndPoint,
            IPEndPoint remoteEndPoint,
            AccessControlInfo accessControlInfo)
        {
            Environment      = new OwinEnvironment();
            ConnectionStream = stream;
            RequestBody      = new OwinRequestBodyStream(this, ConnectionStream);
            ResponseBody     = new OwinResponseBodyStream(this, ConnectionStream);
            Environment.Environment[OwinEnvironment.Owin.Version]     = "1.0.1";
            Environment.Environment[OwinEnvironment.Host.TraceOutput] = TextWriter.Null;
            Environment.Environment[OwinEnvironment.Owin.RequestBody] = RequestBody;
            var requestHeaders = req.Headers.ToDictionary();

            if (!requestHeaders.TryGetValue("Host", out var values) || values.Length == 0 || String.IsNullOrEmpty(values[0]))
            {
                requestHeaders["Host"] = new string[] { localEndPoint.ToString() };
            }
            Environment.Environment[OwinEnvironment.Owin.RequestHeaders]               = requestHeaders;
            Environment.Environment[OwinEnvironment.Owin.RequestPath]                  = req.Path;
            Environment.Environment[OwinEnvironment.Owin.RequestPathBase]              = "";
            Environment.Environment[OwinEnvironment.Owin.RequestProtocol]              = req.Protocol;
            Environment.Environment[OwinEnvironment.Owin.RequestQueryString]           = req.QueryString;
            Environment.Environment[OwinEnvironment.Owin.RequestScheme]                = "http";
            Environment.Environment[OwinEnvironment.Owin.RequestMethod]                = req.Method;
            Environment.Environment[OwinEnvironment.Owin.ResponseBody]                 = ResponseBody;
            Environment.Environment[OwinEnvironment.Owin.ResponseHeaders]              = new Dictionary <string, string[]>(StringComparer.OrdinalIgnoreCase);
            Environment.Environment[OwinEnvironment.Server.RemoteIpAddress]            = remoteEndPoint.Address.ToString();
            Environment.Environment[OwinEnvironment.Server.RemotePort]                 = remoteEndPoint.Port.ToString();
            Environment.Environment[OwinEnvironment.Server.IsLocal]                    = remoteEndPoint.Address.GetAddressLocality() == 0;
            Environment.Environment[OwinEnvironment.Server.LocalIpAddress]             = localEndPoint.Address.ToString();
            Environment.Environment[OwinEnvironment.Server.LocalPort]                  = localEndPoint.Port.ToString();
            Environment.Environment[OwinEnvironment.Server.OnSendingHeaders]           = new Action <Action <object>, object>(OnSendingHeaders.Add);
            Environment.Environment[OwinEnvironment.PeerCastStation.PeerCast]          = peerCast;
            Environment.Environment[OwinEnvironment.PeerCastStation.AccessControlInfo] = accessControlInfo;
            Environment.Environment[OwinEnvironment.PeerCastStation.GetRecvRate]       = new Func <float>(() => stream.ReadRate);
            Environment.Environment[OwinEnvironment.PeerCastStation.GetSendRate]       = new Func <float>(() => stream.WriteRate);
            Environment.Environment[OwinEnvironment.Opaque.Upgrade]                    = new Action <IDictionary <string, object>, Func <IDictionary <string, object>, Task> >(OpaqueUpgrade);
        }
Esempio n. 8
0
 internal OwinResponse(OwinEnvironment owner)
 {
     env     = owner;
     Headers = new ResponseHeaders(env);
 }
Esempio n. 9
0
 internal ResponseHeaders(OwinEnvironment env)
 {
     this.env = env;
 }
Esempio n. 10
0
 public RequestCookies(OwinEnvironment env)
 {
     this.cookies = env.GetRequestCookies();
 }
Esempio n. 11
0
 internal RequestQuery(OwinEnvironment env)
 {
     this.env = env;
 }
Esempio n. 12
0
 internal RequestHeaders(OwinEnvironment env)
 {
     this.env = env;
 }