| | 1 | | // Copyright (c) Microsoft and contributors. All rights reserved. |
| | 2 | | // |
| | 3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
| | 4 | | // you may not use this file except in compliance with the License. |
| | 5 | | // You may obtain a copy of the License at |
| | 6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
| | 7 | | // |
| | 8 | | // Unless required by applicable law or agreed to in writing, software |
| | 9 | | // distributed under the License is distributed on an "AS IS" BASIS, |
| | 10 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| | 11 | | // |
| | 12 | | // See the License for the specific language governing permissions and |
| | 13 | | // limitations under the License. |
| | 14 | |
|
| | 15 | | using System; |
| | 16 | | using System.Collections.Generic; |
| | 17 | | using System.Linq; |
| | 18 | | using System.Text; |
| | 19 | | using System.Threading.Tasks; |
| | 20 | |
|
| | 21 | | namespace Microsoft.Azure.Batch.Conventions.Files.Utilities |
| | 22 | | { |
| | 23 | | internal static class UrlUtils |
| | 24 | | { |
| | 25 | | internal static string GetUrlValueSegment(string url, string containerSegment) |
| | 26 | | { |
| 10 | 27 | | Validate.IsNotNullOrEmpty(url, nameof(url)); |
| 8 | 28 | | Validate.IsNotNullOrEmpty(containerSegment, nameof(containerSegment)); |
| | 29 | |
|
| | 30 | | Uri uri; |
| 6 | 31 | | if (!Uri.TryCreate(url, UriKind.Absolute, out uri)) |
| | 32 | | { |
| 2 | 33 | | throw new ArgumentException("url must be a valid absolute URI", nameof(url)); |
| | 34 | | } |
| | 35 | |
|
| 4 | 36 | | var segments = uri.Segments |
| 23 | 37 | | .Select(s => s.Trim('/')) |
| 4 | 38 | | .ToList(); |
| | 39 | |
|
| 19 | 40 | | var containerSegmentIndex = segments.FindIndex(s => String.Equals(containerSegment, s, StringComparison.Ordi |
| | 41 | |
|
| | 42 | | // If the containerSegment is not present, or is the last segment, then there |
| | 43 | | // is no value. |
| 4 | 44 | | if (containerSegmentIndex < 0) |
| | 45 | | { |
| 1 | 46 | | return null; |
| | 47 | | } |
| 3 | 48 | | if (containerSegmentIndex == segments.Count - 1) |
| | 49 | | { |
| 1 | 50 | | return null; |
| | 51 | | } |
| | 52 | |
|
| 2 | 53 | | var valueSegmentIndex = containerSegmentIndex + 1; |
| | 54 | |
|
| 2 | 55 | | return segments[valueSegmentIndex]; |
| | 56 | | } |
| | 57 | | } |
| | 58 | | } |