Example #1
0
        public void Start()
        {
            Interlocked.Exchange(ref _running, 1);
            _listener.Start();

            Task.Run(async() => {
                try
                {
                    var service = new WasabiJsonRpcService();
                    var handler = new JsonRpcRequestHandler(service);

                    while (IsRunning)
                    {
                        var context  = _listener.GetContext();
                        var request  = context.Request;
                        var response = context.Response;

                        if (request.HttpMethod == "POST")
                        {
                            string body;
                            using (var reader = new StreamReader(request.InputStream))
                                body = await reader.ReadToEndAsync();

                            var identity = (HttpListenerBasicIdentity)context.User?.Identity;
                            if (!_config.RequiresCredentials || CheckValidCredentials(identity))
                            {
                                var result = await handler.HandleAsync(body, _cts);

                                // result is null only when the request is a notification.
                                if (!string.IsNullOrEmpty(result))
                                {
                                    response.ContentType = "application/json-rpc";
                                    var output           = response.OutputStream;
                                    var buffer           = Encoding.UTF8.GetBytes(result);
                                    await output.WriteAsync(buffer, 0, buffer.Length);
                                    await output.FlushAsync();
                                }
                            }
                            else
                            {
                                response.StatusCode = (int)HttpStatusCode.Unauthorized;
                            }
                        }
                        else
                        {
                            response.StatusCode = (int)HttpStatusCode.MethodNotAllowed;
                        }
                        response.Close();
                    }
                }
                finally
                {
                    Interlocked.CompareExchange(ref _running, 3, 2);                     // If IsStopping, make it stopped.
                }
            });
        }
Example #2
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            var handler = new JsonRpcRequestHandler <WasabiJsonRpcService>(Service);

            while (!stoppingToken.IsCancellationRequested)
            {
                try
                {
                    var context = await GetHttpContextAsync(stoppingToken).ConfigureAwait(false);

                    var request  = context.Request;
                    var response = context.Response;

                    if (request.HttpMethod == "POST")
                    {
                        using var reader = new StreamReader(request.InputStream);
                        string body = await reader.ReadToEndAsync().ConfigureAwait(false);

                        var identity = (HttpListenerBasicIdentity)context.User?.Identity;
                        if (!Config.RequiresCredentials || CheckValidCredentials(identity))
                        {
                            var result = await handler.HandleAsync(body, stoppingToken).ConfigureAwait(false);

                            // result is null only when the request is a notification.
                            if (!string.IsNullOrEmpty(result))
                            {
                                response.ContentType = "application/json-rpc";
                                var output = response.OutputStream;
                                var buffer = Encoding.UTF8.GetBytes(result);
                                await output.WriteAsync(buffer.AsMemory(0, buffer.Length), stoppingToken).ConfigureAwait(false);

                                await output.FlushAsync(stoppingToken).ConfigureAwait(false);
                            }
                        }
                        else
                        {
                            response.StatusCode = (int)HttpStatusCode.Unauthorized;
                        }
                    }
                    else
                    {
                        response.StatusCode = (int)HttpStatusCode.MethodNotAllowed;
                    }
                    response.Close();
                }
                catch (OperationCanceledException)
                {
                    break;
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex);
                }
            }
        }