private async Task OnViewXML(OWINEnv env, Dictionary<string, string> query, CancellationToken cancel_token) { var data = BuildViewXml(); env.SetResponseStatusCode(HttpStatusCode.OK); env.SetResponseHeader("Content-Type", "text/xml"); env.SetResponseHeader("Content-Length", data.Length.ToString()); if (env.RequestMethod!="HEAD") { await env.ResponseBody.WriteAsync(data, 0, data.Length, cancel_token).ConfigureAwait(false); } }
private string GetPhysicalPath(OWINEnv env) { string physical_path; if (virtualPhysicalPathMap.TryGetValue(env.RequestPathBase, out physical_path)) { return(Path.GetFullPath(Path.Combine(physical_path, env.RequestPath))); } else { return(null); } }
private async Task OnStop(OWINEnv env, Dictionary<string, string> query, CancellationToken cancel_token) { var channel = FindChannelFromQuery(query); if (channel!=null) { Application.PeerCast.CloseChannel(channel); env.SetResponseStatusCode(HttpStatusCode.OK); await env.SetResponseBodyAsync("OK", cancel_token).ConfigureAwait(false); } else { env.SetResponseStatusCode(HttpStatusCode.NotFound); await env.SetResponseBodyAsync("Channel NotFound", cancel_token).ConfigureAwait(false); } }
private async Task SendResponseMoveToIndex(OWINEnv env, CancellationToken cancel_token) { var content = System.Text.Encoding.UTF8.GetBytes("Moving..."); env.SetResponseHeader("Content-Type", "text/plain"); env.SetResponseHeader("Content-Length", content.Length.ToString()); env.SetResponseHeader("Location", "/html/index.html"); if (env.AccessControlInfo.AuthenticationKey != null) { env.SetResponseHeader("Set-Cookie", "auth=" + HTTPUtils.CreateAuthorizationToken(env.AccessControlInfo.AuthenticationKey)); } env.ResponseStatusCode = (int)HttpStatusCode.Moved; if (env.RequestMethod == "GET") { await env.ResponseBody.WriteAsync(content, 0, content.Length, cancel_token).ConfigureAwait(false); } }
private async Task SendResponseChannelList(OWINEnv env, CancellationToken cancel_token) { var channel_list = await GetYPChannels().ConfigureAwait(false); var contents = System.Text.Encoding.UTF8.GetBytes(ChannelsToIndex(channel_list)); env.SetResponseHeader("Content-Type", "text/plain;charset=utf-8"); env.SetResponseHeader("Content-Length", contents.Length.ToString()); if (env.AccessControlInfo.AuthenticationKey != null) { env.SetResponseHeader("Set-Cookie", "auth=" + HTTPUtils.CreateAuthorizationToken(env.AccessControlInfo.AuthenticationKey)); } if (env.RequestMethod == "GET") { await env.ResponseBody.WriteAsync(contents, 0, contents.Length, cancel_token).ConfigureAwait(false); } }
private async Task OnProcess(IDictionary<string, object> owinenv) { var env = new OWINEnv(owinenv); var cancel_token = env.CallCanlelled; try { if (!HTTPUtils.CheckAuthorization(env.GetAuthorizationToken(), env.AccessControlInfo)) { throw new HTTPError(HttpStatusCode.Unauthorized); } if (env.RequestMethod!="HEAD" && env.RequestMethod!="GET") { throw new HTTPError(HttpStatusCode.MethodNotAllowed); } var query = env.RequestParameters; string value; if (query.TryGetValue("cmd", out value)) { switch (value) { case "viewxml": //リレー情報XML出力 await OnViewXML(env, query, cancel_token).ConfigureAwait(false); break; case "stop": //チャンネル停止 await OnStop(env, query, cancel_token).ConfigureAwait(false); break; case "bump": //チャンネル再接続 await OnBump(env, query, cancel_token).ConfigureAwait(false); break; default: throw new HTTPError(HttpStatusCode.BadRequest); } } else { throw new HTTPError(HttpStatusCode.BadRequest); } } catch (HTTPError err) { env.ResponseStatusCode = (int)err.StatusCode; } catch (UnauthorizedAccessException) { env.ResponseStatusCode = (int)HttpStatusCode.Forbidden; } }
private async Task OnProcess(IDictionary<string, object> owinenv) { var env = new OWINEnv(owinenv); var cancel_token = env.CallCanlelled; try { if (!HTTPUtils.CheckAuthorization(env.GetAuthorizationToken(), env.AccessControlInfo)) { throw new HTTPError(HttpStatusCode.Unauthorized); } if (env.RequestMethod!="HEAD" && env.RequestMethod!="GET") { throw new HTTPError(HttpStatusCode.MethodNotAllowed); } var query = env.RequestParameters; string value; if (query.TryGetValue("cmd", out value)) { switch (value) { case "viewxml": //リレー情報XML出力 await OnViewXML(env, query, cancel_token); break; case "stop": //チャンネル停止 await OnStop(env, query, cancel_token); break; case "bump": //チャンネル再接続 await OnBump(env, query, cancel_token); break; default: throw new HTTPError(HttpStatusCode.BadRequest); } } else { throw new HTTPError(HttpStatusCode.BadRequest); } } catch (HTTPError err) { env.ResponseStatusCode = (int)err.StatusCode; } catch (UnauthorizedAccessException) { env.ResponseStatusCode = (int)HttpStatusCode.Forbidden; } }
private async Task OnRedirect(IDictionary <string, object> owinenv) { var env = new OWINEnv(owinenv); var cancel_token = env.CallCanlelled; try { if (!HTTPUtils.CheckAuthorization(env.GetAuthorizationToken(), env.AccessControlInfo)) { throw new HTTPError(HttpStatusCode.Unauthorized); } if (env.RequestMethod != "HEAD" && env.RequestMethod != "GET") { throw new HTTPError(HttpStatusCode.MethodNotAllowed); } await SendResponseMoveToIndex(env, cancel_token).ConfigureAwait(false); } catch (HTTPError err) { env.ResponseStatusCode = (int)err.StatusCode; } catch (UnauthorizedAccessException) { env.ResponseStatusCode = (int)HttpStatusCode.Forbidden; } }
private async Task SendResponseFileContent(OWINEnv env, CancellationToken cancel_token) { var localpath = GetPhysicalPath(env); if (localpath == null) { throw new HTTPError(HttpStatusCode.Forbidden); } if (Directory.Exists(localpath)) { localpath = Path.Combine(localpath, "index.html"); if (!File.Exists(localpath)) { throw new HTTPError(HttpStatusCode.Forbidden); } } if (File.Exists(localpath)) { var contents = File.ReadAllBytes(localpath); var content_desc = GetFileDesc(Path.GetExtension(localpath)); env.SetResponseHeader("Content-Type", content_desc.MimeType); env.SetResponseHeader("Content-Length", contents.Length.ToString()); if (env.AccessControlInfo.AuthenticationKey != null) { env.SetResponseHeader("Set-Cookie", "auth=" + HTTPUtils.CreateAuthorizationToken(env.AccessControlInfo.AuthenticationKey)); } if (env.RequestMethod == "GET") { await env.ResponseBody.WriteAsync(contents, 0, contents.Length, cancel_token).ConfigureAwait(false); } } else { throw new HTTPError(HttpStatusCode.NotFound); } }
private async Task SendJson(OWINEnv env, JToken token, bool send_body, CancellationToken cancel_token) { var body = System.Text.Encoding.UTF8.GetBytes(token.ToString()); env.AddResponseHeader("Content-Type", "application/json"); env.AddResponseHeader("Content-Length", body.Length.ToString()); env.ResponseStatusCode = (int)HttpStatusCode.OK; if (send_body) { await env.ResponseBody.WriteAsync(body, 0, body.Length, cancel_token); } }
private async Task OnProcess(IDictionary<string, object> owinenv) { var env = new OWINEnv(owinenv); var cancel_token = env.CallCanlelled; try { if (!HTTPUtils.CheckAuthorization(env.GetAuthorizationToken(), env.AccessControlInfo)) { throw new HTTPError(HttpStatusCode.Unauthorized); } var ctx = new APIContext(this, this.Application.PeerCast, env.AccessControlInfo); var rpc_host = new JSONRPCHost(ctx); switch (env.RequestMethod) { case "HEAD": case "GET": await SendJson(env, ctx.GetVersionInfo(), env.RequestMethod!="HEAD", cancel_token); break; case "POST": { if (!env.RequestHeaders.ContainsKey("X-REQUESTED-WITH")) { throw new HTTPError(HttpStatusCode.BadRequest); } if (!env.RequestHeaders.ContainsKey("CONTENT-LENGTH")) { throw new HTTPError(HttpStatusCode.LengthRequired); } var body = env.RequestBody; var len = body.Length; if (len<=0 || RequestLimit<len) { throw new HTTPError(HttpStatusCode.BadRequest); } try { var timeout_token = new CancellationTokenSource(TimeoutLimit); var buf = await body.ReadBytesAsync((int)len, CancellationTokenSource.CreateLinkedTokenSource(cancel_token, timeout_token.Token).Token); var request_str = System.Text.Encoding.UTF8.GetString(buf); JToken res = rpc_host.ProcessRequest(request_str); if (res!=null) { await SendJson(env, res, true, cancel_token); } else { throw new HTTPError(HttpStatusCode.NoContent); } } catch (OperationCanceledException) { throw new HTTPError(HttpStatusCode.RequestTimeout); } } break; default: throw new HTTPError(HttpStatusCode.MethodNotAllowed); } } catch (HTTPError err) { env.ResponseStatusCode = (int)err.StatusCode; } catch (UnauthorizedAccessException) { env.ResponseStatusCode = (int)HttpStatusCode.Forbidden; } }
private async Task OnStop(OWINEnv env, Dictionary<string, string> query, CancellationToken cancel_token) { var channel = FindChannelFromQuery(query); if (channel!=null) { Application.PeerCast.CloseChannel(channel); env.SetResponseStatusCode(HttpStatusCode.OK); await env.SetResponseBodyAsync("OK", cancel_token); } else { env.SetResponseStatusCode(HttpStatusCode.NotFound); await env.SetResponseBodyAsync("Channel NotFound", cancel_token); } }
private async Task OnBump(OWINEnv env, Dictionary<string, string> query, CancellationToken cancel_token) { var channel = FindChannelFromQuery(query); if (channel!=null) { channel.Reconnect(); env.SetResponseStatusCode(HttpStatusCode.OK); await env.SetResponseBodyAsync("OK", cancel_token); } else { env.SetResponseStatusCode(HttpStatusCode.NotFound); await env.SetResponseBodyAsync("Channel NotFound", cancel_token); } }
private async Task OnViewXML(OWINEnv env, Dictionary<string, string> query, CancellationToken cancel_token) { var data = BuildViewXml(); env.SetResponseStatusCode(HttpStatusCode.OK); env.SetResponseHeader("Content-Type", "text/xml"); env.SetResponseHeader("Content-Length", data.Length.ToString()); if (env.RequestMethod!="HEAD") { await env.ResponseBody.WriteAsync(data, 0, data.Length, cancel_token); } }
private async Task SendResponseMoveToIndex(OWINEnv env, CancellationToken cancel_token) { var content = System.Text.Encoding.UTF8.GetBytes("Moving..."); env.SetResponseHeader("Content-Type", "text/plain"); env.SetResponseHeader("Content-Length", content.Length.ToString()); env.SetResponseHeader("Location", "/html/index.html"); if (env.AccessControlInfo.AuthenticationKey!=null) { env.SetResponseHeader("Set-Cookie", "auth=" + HTTPUtils.CreateAuthorizationToken(env.AccessControlInfo.AuthenticationKey)); } env.ResponseStatusCode = (int)HttpStatusCode.Moved; if (env.RequestMethod=="GET") { await env.ResponseBody.WriteAsync(content, 0, content.Length, cancel_token); } }
private string GetPhysicalPath(OWINEnv env) { string physical_path; if (virtualPhysicalPathMap.TryGetValue(env.RequestPathBase, out physical_path)) { return Path.GetFullPath(Path.Combine(physical_path, env.RequestPath)); } else { return null; } }
private async Task OnRedirect(IDictionary<string, object> owinenv) { var env = new OWINEnv(owinenv); var cancel_token = env.CallCanlelled; try { if (!HTTPUtils.CheckAuthorization(env.GetAuthorizationToken(), env.AccessControlInfo)) { throw new HTTPError(HttpStatusCode.Unauthorized); } if (env.RequestMethod!="HEAD" && env.RequestMethod!="GET") { throw new HTTPError(HttpStatusCode.MethodNotAllowed); } await SendResponseMoveToIndex(env, cancel_token); } catch (HTTPError err) { env.ResponseStatusCode = (int)err.StatusCode; } catch (UnauthorizedAccessException) { env.ResponseStatusCode = (int)HttpStatusCode.Forbidden; } }
private async Task SendResponseFileContent(OWINEnv env, CancellationToken cancel_token) { var localpath = GetPhysicalPath(env); if (localpath==null) throw new HTTPError(HttpStatusCode.Forbidden); if (Directory.Exists(localpath)) { localpath = Path.Combine(localpath, "index.html"); if (!File.Exists(localpath)) throw new HTTPError(HttpStatusCode.Forbidden); } if (File.Exists(localpath)) { var contents = File.ReadAllBytes(localpath); var content_desc = GetFileDesc(Path.GetExtension(localpath)); env.SetResponseHeader("Content-Type", content_desc.MimeType); env.SetResponseHeader("Content-Length", contents.Length.ToString()); if (env.AccessControlInfo.AuthenticationKey!=null) { env.SetResponseHeader("Set-Cookie", "auth=" + HTTPUtils.CreateAuthorizationToken(env.AccessControlInfo.AuthenticationKey)); } if (env.RequestMethod=="GET") { await env.ResponseBody.WriteAsync(contents, 0, contents.Length, cancel_token); } } else { throw new HTTPError(HttpStatusCode.NotFound); } }