| | | 1 | | using Elsa.Http.Options; |
| | | 2 | | using Microsoft.Extensions.Options; |
| | | 3 | | |
| | | 4 | | namespace Elsa.Http.Services; |
| | | 5 | | |
| | | 6 | | /// <inheritdoc /> |
| | | 7 | | public class DefaultAbsoluteUrlProvider : IAbsoluteUrlProvider |
| | | 8 | | { |
| | | 9 | | private readonly IOptions<HttpActivityOptions> _options; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Initializes a new instance of the <see cref="DefaultAbsoluteUrlProvider"/> class. |
| | | 13 | | /// </summary> |
| | 0 | 14 | | public DefaultAbsoluteUrlProvider(IOptions<HttpActivityOptions> options) => _options = options; |
| | | 15 | | |
| | | 16 | | /// <inheritdoc /> |
| | | 17 | | public Uri ToAbsoluteUrl(string relativePath) |
| | | 18 | | { |
| | 0 | 19 | | var baseUrl = _options.Value.BaseUrl; |
| | | 20 | | |
| | 0 | 21 | | if (baseUrl == null) |
| | 0 | 22 | | throw new Exception( |
| | 0 | 23 | | "There was no base URL configured, which means no absolute URL can be generated from outside the context |
| | | 24 | | |
| | | 25 | | // To not lose any base path information, we need to ensure that: |
| | | 26 | | // - Base path ends with a slash. |
| | | 27 | | // - Relative path does NOT start with a slash. |
| | 0 | 28 | | var baseUri = new Uri(baseUrl.ToString().TrimEnd('/') + '/'); |
| | 0 | 29 | | relativePath = relativePath.TrimStart('/'); |
| | | 30 | | |
| | 0 | 31 | | return new Uri(baseUri, relativePath); |
| | | 32 | | } |
| | | 33 | | } |