コード例 #1
0
            public async Task Invoke(IOwinContext ctx)
            {
                var cancel_token = ctx.Request.CallCancelled;
                var localpath    = Path.GetFullPath(CombinePath(LocalPath, ctx.Request.Path.Value));

                if (Directory.Exists(localpath))
                {
                    localpath = Path.Combine(localpath, "index.html");
                    if (!File.Exists(localpath))
                    {
                        ctx.Response.StatusCode = (int)HttpStatusCode.Forbidden;
                        return;
                    }
                }
                if (File.Exists(localpath))
                {
                    var contents     = File.ReadAllBytes(localpath);
                    var content_desc = GetFileDesc(Path.GetExtension(localpath));
                    ctx.Response.ContentType   = content_desc.MimeType;
                    ctx.Response.ContentLength = contents.LongLength;
                    var acinfo = ctx.GetAccessControlInfo();
                    if (acinfo?.AuthenticationKey != null)
                    {
                        ctx.Response.Headers.Append("Set-Cookie", $"auth={acinfo.AuthenticationKey.GetToken()}; Path=/");
                    }
                    await ctx.Response.WriteAsync(contents, cancel_token).ConfigureAwait(false);
                }
                else
                {
                    ctx.Response.StatusCode = (int)HttpStatusCode.NotFound;
                }
            }
コード例 #2
0
        public async Task Invoke(IOwinContext ctx)
        {
            var cancel_token = ctx.Request.CallCancelled;

            ctx.Response.ContentType   = "application/javascript";
            ctx.Response.ContentLength = contents.LongLength;
            var acinfo = ctx.GetAccessControlInfo();

            if (acinfo?.AuthenticationKey != null)
            {
                ctx.Response.Headers.Append("Set-Cookie", $"auth={acinfo.AuthenticationKey.GetToken()}; Path=/");
            }
            await ctx.Response.WriteAsync(contents, cancel_token).ConfigureAwait(false);
        }
コード例 #3
0
        private static async Task InvokeRedirect(IOwinContext ctx)
        {
            var cancel_token = ctx.Request.CallCancelled;

            ctx.Response.ContentType = "text/plain;charset=utf-8";
            ctx.Response.Headers.Set("Location", "/html/index.html");
            var acinfo = ctx.GetAccessControlInfo();

            if (acinfo?.AuthenticationKey != null)
            {
                ctx.Response.Headers.Append("Set-Cookie", $"auth={acinfo.AuthenticationKey.GetToken()}; Path=/");
            }
            ctx.Response.StatusCode = (int)HttpStatusCode.Moved;
            await ctx.Response.WriteAsync("Moving...", cancel_token).ConfigureAwait(false);
        }
コード例 #4
0
ファイル: HTMLHost.cs プロジェクト: Philmist/peercaststation
        private static async Task InvokeIndexTXT(IOwinContext ctx)
        {
            var cancel_token = ctx.Request.CallCancelled;

            ctx.Response.ContentType = "text/plain;charset=utf-8";
            var acinfo = ctx.GetAccessControlInfo();

            if (acinfo?.AuthenticationKey != null)
            {
                ctx.Response.Headers.Append("Set-Cookie", $"auth={acinfo.AuthenticationKey.GetToken()}; Path=/");
            }
            var peercast = ctx.GetPeerCast();
            var indextxt = String.Join("\r\n", peercast.Channels.Select(c => BuildIndexTXTEntry(peercast, c)));
            await ctx.Response.WriteAsync(indextxt, cancel_token).ConfigureAwait(false);
        }
コード例 #5
0
        private async Task Invoke(IOwinContext ctx)
        {
            var cancel_token = ctx.Request.CallCancelled;
            var channel_list = await GetYPChannelsAsync(cancel_token).ConfigureAwait(false);

            var contents = System.Text.Encoding.UTF8.GetBytes(ChannelsToIndex(channel_list));

            ctx.Response.ContentType   = "text/plain;charset=utf-8";
            ctx.Response.ContentLength = contents.LongLength;
            var acinfo = ctx.GetAccessControlInfo();

            if (acinfo?.AuthenticationKey != null)
            {
                ctx.Response.Headers.Append("Set-Cookie", $"auth={acinfo.AuthenticationKey.GetToken()}; Path=/");
            }
            await ctx.Response.WriteAsync(contents, cancel_token).ConfigureAwait(false);
        }
コード例 #6
0
        private async Task PlayListHandler(IOwinContext ctx, ParsedRequest req, Channel channel)
        {
            var ct      = ctx.Request.CallCancelled;
            var session = req.Session;

            if (String.IsNullOrWhiteSpace(session))
            {
                var source = new IPEndPoint(IPAddress.Parse(ctx.Request.RemoteIpAddress), ctx.Request.RemotePort ?? 0).ToString();
                using (var md5 = System.Security.Cryptography.MD5.Create()) {
                    session =
                        md5
                        .ComputeHash(System.Text.Encoding.ASCII.GetBytes(source))
                        .Aggregate(new System.Text.StringBuilder(), (builder, v) => builder.Append(v.ToString("X2")))
                        .ToString();
                }
                var location = new UriBuilder(ctx.Request.Uri);
                if (String.IsNullOrEmpty(location.Query))
                {
                    location.Query = $"session={session}";
                }
                else
                {
                    location.Query = location.Query.Substring(1) + $"&session={session}";
                }
                ctx.Response.Redirect(location.Uri.ToString());
                return;
            }
            var pls = new M3U8PlayList(ctx.Request.Query.Get("scheme"), channel);

            ctx.Response.StatusCode = (int)HttpStatusCode.OK;
            ctx.Response.Headers.Add("Cache-Control", new string [] { "private" });
            ctx.Response.Headers.Add("Cache-Disposition", new string [] { "inline" });
            ctx.Response.Headers.Add("Access-Control-Allow-Origin", new string[] { "*" });
            ctx.Response.ContentType = pls.MIMEType;
            using (var subscription = HLSChannelSink.GetSubscription(this, channel, ctx, session)) {
                subscription.Stopped.ThrowIfCancellationRequested();
                byte[] body;
                try {
                    var baseuri = new Uri(
                        new Uri(ctx.Request.Uri.GetComponents(UriComponents.SchemeAndServer | UriComponents.UserInfo, UriFormat.UriEscaped)),
                        "hls/");
                    var acinfo = ctx.GetAccessControlInfo();
                    using (var cts = CancellationTokenSource.CreateLinkedTokenSource(ct, subscription.Stopped)) {
                        cts.CancelAfter(10000);
                        if (acinfo.AuthorizationRequired)
                        {
                            var parameters = new Dictionary <string, string>()
                            {
                                { "auth", acinfo.AuthenticationKey.GetToken() },
                                { "session", subscription.SessionId },
                            };
                            body = await pls.CreatePlayListAsync(baseuri, parameters, subscription.Segmenter, cts.Token).ConfigureAwait(false);
                        }
                        else
                        {
                            var parameters = new Dictionary <string, string>()
                            {
                                { "session", subscription.SessionId },
                            };
                            body = await pls.CreatePlayListAsync(baseuri, parameters, subscription.Segmenter, cts.Token).ConfigureAwait(false);
                        }
                    }
                }
                catch (OperationCanceledException) {
                    ctx.Response.StatusCode = (int)HttpStatusCode.GatewayTimeout;
                    return;
                }
                ctx.Response.StatusCode = (int)HttpStatusCode.OK;
                await ctx.Response.WriteAsync(body, ct).ConfigureAwait(false);
            }
        }
コード例 #7
0
        private static async Task PlayListHandler(IOwinContext ctx)
        {
            var ct  = ctx.Request.CallCancelled;
            var req = ParsedRequest.Parse(ctx);

            if (!req.IsValid)
            {
                ctx.Response.StatusCode = (int)req.Status;
                return;
            }
            Channel channel;

            try {
                channel = await GetChannelAsync(ctx, req, ct).ConfigureAwait(false);
            }
            catch (TaskCanceledException) {
                ctx.Response.StatusCode = (int)HttpStatusCode.GatewayTimeout;
                return;
            }
            if (channel == null)
            {
                ctx.Response.StatusCode = (int)HttpStatusCode.NotFound;
                return;
            }

            var fmt = ctx.Request.Query.Get("pls") ?? req.Extension;

            //m3u8のプレイリストを要求された時はHLS用のパスに転送する
            if (fmt?.ToLowerInvariant() == "m3u8")
            {
                ctx.Response.StatusCode = (int)HttpStatusCode.MovedPermanently;
                var location = new UriBuilder(ctx.Request.Uri);
                location.Path = $"/hls/{req.ChannelId.ToString("N")}";
                if (location.Query.Contains("pls=m3u8"))
                {
                    var queries = location.Query.Substring(1).Split('&').Where(seg => seg != "pls=m3u8").ToArray();
                    if (queries.Length > 0)
                    {
                        location.Query = String.Join("&", queries);
                    }
                    else
                    {
                        location.Query = null;
                    }
                }
                ctx.Response.Headers.Add("Location", new string [] { location.Uri.ToString() });
                return;
            }

            var scheme = ctx.Request.Query.Get("scheme");
            var pls    = CreatePlaylist(channel, fmt, scheme);

            ctx.Response.StatusCode = (int)HttpStatusCode.OK;
            ctx.Response.Headers.Add("Cache-Control", new string [] { "private" });
            ctx.Response.Headers.Add("Cache-Disposition", new string [] { "inline" });
            ctx.Response.Headers.Add("Access-Control-Allow-Origin", new string[] { "*" });
            ctx.Response.ContentType = pls.MIMEType;
            byte[] body;
            try {
                var baseuri = new Uri(
                    new Uri(ctx.Request.Uri.GetComponents(UriComponents.SchemeAndServer | UriComponents.UserInfo, UriFormat.UriEscaped)),
                    "stream/");
                var acinfo = ctx.GetAccessControlInfo();
                using (var cts = CancellationTokenSource.CreateLinkedTokenSource(ct)) {
                    cts.CancelAfter(10000);
                    if (acinfo.AuthorizationRequired)
                    {
                        var parameters = new Dictionary <string, string>()
                        {
                            { "auth", acinfo.AuthenticationKey.GetToken() },
                        };
                        body = await pls.CreatePlayListAsync(baseuri, parameters, cts.Token).ConfigureAwait(false);
                    }
                    else
                    {
                        body = await pls.CreatePlayListAsync(baseuri, Enumerable.Empty <KeyValuePair <string, string> >(), cts.Token).ConfigureAwait(false);
                    }
                }
            }
            catch (OperationCanceledException) {
                ctx.Response.StatusCode = (int)HttpStatusCode.GatewayTimeout;
                return;
            }
            ctx.Response.StatusCode = (int)HttpStatusCode.OK;
            await ctx.Response.WriteAsync(body, ct).ConfigureAwait(false);
        }