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; } }
public APIHostOutputStream( APIHost owner, PeerCast peercast, Stream input_stream, Stream output_stream, EndPoint remote_endpoint, AccessControlInfo access_control, HTTPRequest request, byte[] header) : base(peercast, input_stream, output_stream, remote_endpoint, access_control, null, header) { this.owner = owner; this.request = request; this.rpcHost = new JSONRPCHost(this); Logger.Debug("Initialized: Remote {0}", remote_endpoint); }