Sign Up
Log In
Log In
or
Sign Up
Places
All Projects
Status Monitor
Collapse sidebar
home:ahmedmoselhi2
xtream-download-manager
0001-fix-GetContentDispositionFileName-can-not-...
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File 0001-fix-GetContentDispositionFileName-can-not-correc.patch of Package xtream-download-manager
From 88359f964f83820da1f415c430948e55c51f2d84 Mon Sep 17 00:00:00 2001 From: ttyS3 <ttys3.rust@gmail.com> Date: Tue, 21 Mar 2023 15:35:58 +0800 Subject: [PATCH 1/1] fix: fix GetContentDispositionFileName can not correctly handle RFC 6266 specific `filename*` param Refs: https://www.rfc-editor.org/rfc/rfc6266#section-4.3 --- .../Clients/Http/WebRequestExtensions.cs | 34 +++++++++++-------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/app/XDM/XDM.Core/Clients/Http/WebRequestExtensions.cs b/app/XDM/XDM.Core/Clients/Http/WebRequestExtensions.cs index f93d85c..79ff1bf 100644 --- a/app/XDM/XDM.Core/Clients/Http/WebRequestExtensions.cs +++ b/app/XDM/XDM.Core/Clients/Http/WebRequestExtensions.cs @@ -12,6 +12,7 @@ namespace XDM.Core.Clients.Http { public static class WebRequestExtensions { + private static readonly char[] QuoteChars = new char[] { '"' }; public static void EnsureSuccessStatusCode(this HttpWebResponse response) { if (response.StatusCode != HttpStatusCode.OK && response.StatusCode != HttpStatusCode.PartialContent) @@ -92,28 +93,31 @@ namespace XDM.Core.Clients.Http public static string? GetContentDispositionFileName(string contentDisposition) { + //var contentDisposition = response.Headers.Get("Content-Disposition"); + if (string.IsNullOrEmpty(contentDisposition)) + { + return null; + } + + Log.Debug("Trying to get filename from: " + contentDisposition); try { - var r1 = new Regex(@"\s*filename\*\s*=\s*[^']*\'\s*\'(.*)"); - var r2 = new Regex("\\s*filename\\s*=\\s*\"([^\"]*)\""); - var r3 = new Regex("filename\\s*=\\s*([^\"]+)"); + var headerValue = System.Net.Http.Headers.ContentDispositionHeaderValue.Parse(contentDisposition); - //var contentDisposition = response.Headers.Get("Content-Disposition"); - if (contentDisposition != null) + if (headerValue.FileNameStar != null) { - Log.Debug("Trying to get filename from: " + contentDisposition); - foreach (var r in new Regex[] { r1, r2, r3 }) - { - var m = r.Match(contentDisposition); - if (m.Success && m.Groups.Count >= 2) - { - return FileHelper.SanitizeFileName(Uri.UnescapeDataString(m.Groups[1].Value)); - } - } + return FileHelper.SanitizeFileName(Uri.UnescapeDataString(headerValue.FileNameStar)); + } + else if (headerValue.FileName != null) + { + // https://github.com/dotnet/runtime/issues/32765#issuecomment-766768351 + // response.Content.Headers.ContentDisposition.FileName contains quotes #32765 + return FileHelper.SanitizeFileName(headerValue.FileName.Trim(QuoteChars)); } } - catch (Exception ex) + catch (FormatException ex) { + // Invalid Content-Disposition header format Log.Debug(ex, ex.Message); } -- 2.43.0
Locations
Projects
Search
Status Monitor
Help
OpenBuildService.org
Documentation
API Documentation
Code of Conduct
Contact
Support
@OBShq
Terms
openSUSE Build Service is sponsored by
The Open Build Service is an
openSUSE project
.
Sign Up
Log In
Places
Places
All Projects
Status Monitor