Skip to content

Remove or relax the length limitation of Uri #96544

@hez2010

Description

@hez2010

The data is a valid scheme which can carry some data other than path in a Uri. For example, we can save an image as base64 data in a Uri:

var uri = new Uri("data:image/jpeg;base64,...");

However, Uri in .NET has a maximum length limitation of 0xFFF0 which makes it unable to save some large data (especially, base64 with length more than 65496 (0xFFF0 - "data:image/jpeg;base64,".Length)).

It's common that a HTML contains an image tag which use base64 encoded data for its src attribute:

<image src="data:image/jpeg;base64,..." />

However, we are unable to get the src as a Uri if the length of src is too large, for example, an 1 mb image.

new Uri("data:image/jpeg;base64," + Enumerable.Repeat("a", 70000).Aggregate((s, n) => $"{s}{n}"))

The above code will throw a UriFormatException with Invalid URI: The Uri string is too long..

While the same code is perfect valid in JavaScript: new URL("data:image/jpeg;base64," + "a".repeat(70000)). The limitation in .NET here also makes it hard to do interop with JavaScript URL types (such as Blazor), because we cannot simply project the Uri type to the JavaScript URL type.

In the real-world scenario, I hit this issue while using semantic-kernel with GPT-4 Vision, which uses base64 image data in its image_url field. I have to resize the images to make them less than the length limitation.

I would like to suggest remove this limitation, or relax it, for example, relax the limitation from 0xFFF0 to 0x7FFFFFF0.

Metadata

Metadata

Assignees

Labels

area-System.Netin-prThere is an active PR which will close this issue when it is merged

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions