step-up-labs / firebase-storage-dotnet

C# library for Firebase Storage

Repository from Github https://github.comstep-up-labs/firebase-storage-dotnetRepository from Github https://github.comstep-up-labs/firebase-storage-dotnet

GetDownloadUrlAsync fails with 400

fabri777 opened this issue · comments

Depending on your configuration HttpClient.GetAsync(url) in PerformFetch may unescape or not the escaped path by GetEscapedPath, leading to a 400 response from Firebase.

The following will fix the issue:

 private async Task<T> PerformFetch<T>()
        {
            var url = this.GetDownloadUrl();
            var resultContent = "N/A";

            try
            {
                var uri = new Uri(url);
                ForceCanonicalPathAndQuery(uri);

                using (var http = await this.storage.Options.CreateHttpClientAsync().ConfigureAwait(false))
                {
                    var result = await http.GetAsync(uri);
                    resultContent = await result.Content.ReadAsStringAsync().ConfigureAwait(false);
                    var data = JsonConvert.DeserializeObject<T>(resultContent);

                    result.EnsureSuccessStatusCode();

                    return data;
                }

            }
            catch (Exception ex)
            {
                throw new FirebaseStorageException(url, resultContent, ex);
            }
        }

        void ForceCanonicalPathAndQuery(Uri uri)
        {
            string paq = uri.PathAndQuery; // need to access PathAndQuery
            FieldInfo flagsFieldInfo = typeof(Uri).GetField("m_Flags", BindingFlags.Instance | BindingFlags.NonPublic);
            ulong flags = (ulong)flagsFieldInfo.GetValue(uri);
            flags &= ~((ulong)0x30); // Flags.PathNotCanonical|Flags.QueryNotCanonical
            flagsFieldInfo.SetValue(uri, flags);
        }
commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

commented

Closing the issue due to inactivity. Feel free to re-open