Esempio n. 1
0
        /// <summary>
        /// Extract the payload using the provided extractors.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>A serializable representation of the payload.</returns>
        public object?ExtractPayload(IHttpRequest request)
        {
            // Not to throw on code that ignores nullability warnings.
            // ReSharper disable once ConditionIsAlwaysTrueOrFalse
            if (request is null)
            {
                return(null);
            }

            var size = _sizeSwitch();

            switch (size)
            {
            case RequestSize.Small when request.ContentLength < 1_000:
            case RequestSize.Medium when request.ContentLength < 10_000:
            case RequestSize.Always:
                _options.LogDebug("Attempting to read request body of size: {0}, configured max: {1}.",
                                  request.ContentLength, size);

                foreach (var extractor in Extractors)
                {
                    var data = extractor.ExtractPayload(request);

                    if (data == null ||
                        data is string dataString &&
                        string.IsNullOrEmpty(dataString))
                    {
                        continue;
                    }

                    return(data);
                }
                break;

            // Request body extraction is opt-in
            case RequestSize.None:
                _options.LogDebug("Skipping request body extraction.");
                return(null);
            }

            if (request.ContentLength is not null)
            {
                _options.LogWarning("Ignoring request with Size {0} and configuration RequestSize {1}",
                                    request.ContentLength, size);
            }

            return(null);
        }
        /// <summary>
        /// Stops the background worker and waits for it to empty the queue until 'shutdownTimeout' is reached
        /// </summary>
        /// <inheritdoc />
        public void Dispose()
        {
            _options.LogDebug("Disposing BackgroundWorker.");

            if (_disposed)
            {
                return;
            }

            _disposed = true;

            try
            {
                // Immediately requests the Worker to stop.
                _shutdownSource.Cancel();

                // If there's anything in the queue, it'll keep running until 'shutdownTimeout' is reached
                // If the queue is empty it will quit immediately
                _ = WorkerTask.Wait(_options.ShutdownTimeout);

                // Dispose the transport if needed
                (_transport as IDisposable)?.Dispose();
            }
            catch (OperationCanceledException)
            {
                _options.LogDebug("Stopping the background worker due to a cancellation");
            }
            catch (Exception exception)
            {
                _options.LogError("Stopping the background worker threw an exception.", exception);
            }

            if (!_queue.IsEmpty)
            {
                _options.LogWarning(
                    "Worker stopped while {0} were still in the queue.",
                    _queue.Count);
            }
        }