forked from JimBobSquarePants/ImageProcessor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUrlParser.cs
More file actions
74 lines (66 loc) · 3.22 KB
/
Copy pathUrlParser.cs
File metadata and controls
74 lines (66 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UrlParser.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// A helper class for decoding and parsing request urls.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
using System.Text;
namespace ImageProcessor.Web.Helpers
{
using System;
using System.Linq;
using System.Text.RegularExpressions;
using ImageProcessor.Web.Extensions;
/// <summary>
/// A helper class for decoding and parsing request URLs.
/// </summary>
public class UrlParser
{
/// <summary>
/// Parses the given URL adjusting the request path to a value that can then be interpreted by an image service.
/// </summary>
/// <param name="url">The url.</param>
/// <param name="servicePrefix">The service prefix.</param>
/// <param name="requestPath">The request path.</param>
/// <param name="queryString">The query string.</param>
public static void ParseUrl(string url, string servicePrefix, out string requestPath, out string queryString)
{
// Remove any service identifier prefixes from the url.
if (!string.IsNullOrWhiteSpace(servicePrefix))
{
url = Regex.Split(url, servicePrefix, RegexOptions.IgnoreCase)[1].TrimStart("?");
}
// Workaround for handling entirely encoded path for https://github.com/JimBobSquarePants/ImageProcessor/issues/478
// If url does not contain a query delimiter but does contain an encoded questionmark,
// treat the last encoded questionmark as the query delimiter
if (url.IndexOf('?') == -1 && url.IndexOf("%3F", StringComparison.Ordinal) > 0)
{
int idx = url.LastIndexOf("%3F", StringComparison.Ordinal);
url = url.Remove(idx, 3).Insert(idx, "?");
}
// Identify each part of the incoming request.
int queryCount = url.Count(f => f == '?');
bool hasParams = queryCount > 0;
bool hasMultiParams = queryCount > 1;
string[] splitPath = url.Split('?');
// Ensure we include any relevent querystring parameters into our request path for third party requests.
// Url decode passed request path #506
// Use Uri.UnescapeDataString instead of HttpUtility.UrlDecode to maintain plus-characters (+)
requestPath = Uri.UnescapeDataString(splitPath[0]);
queryString = hasParams ? splitPath[splitPath.Length - 1] : string.Empty;
// Certain Facebook requests require ony the first part to be decoded.
if (hasMultiParams)
{
StringBuilder sb = new StringBuilder(requestPath);
for (int i = 1; i < splitPath.Length - 1; i++)
{
sb.AppendFormat("?{0}", splitPath[i]);
}
requestPath = sb.ToString();
}
}
}
}