ericlathrop

ericlathrop

Data_url: Parse data: URLs and access the data contained therein

I needed to parse some data: URLs to get the binary data, and the built-in URI module didn’t do it. So here’s a small library for parsing them. If someone wants to add %DataUrl{}to_string(), I’d gladly accept it.

From the README:

data_url

Hex.pm Documentation

Parse data: URLs and access the data contained therein

Usage

iex(1)> DataUrl.parse("data:text/plain;charset=ISO-8859-1;base64,SGVsbG8sIFdvcmxkIQ==")
{:ok,
 %DataUrl{data: "Hello, World!", mime_type: "text/plain;charset=ISO-8859-1"}}

First 6 of 6 Posts Switch mode

kip

kip

ex_cldr Core Team

ex_url also parses data URIs (as well as geo, tel, mailto and uuid URIs). It also includes a to_string/1 function :slight_smile:

iex> URL.parse "data:text/plain;charset=ISO-8859-1;base64,SGVsbG8sIFdvcmxkIQ=="                   
%URL{
  authority: nil,
  fragment: nil,
  host: nil,
  parsed_path: %URL.Data{
    data: "Hello, World!",
    mediatype: "text/plain",
    params: %{"charset" => "ISO-8859-1", "encoding" => "base64"}
  },
  path: "text/plain;charset=ISO-8859-1;base64,SGVsbG8sIFdvcmxkIQ==",
  port: nil,
  query: nil,
  scheme: "data",
  userinfo: nil
}

iex> URL.parse("data:text/plain;charset=ISO-8859-1;base64,SGVsbG8sIFdvcmxkIQ==") |> URL.to_string
"data:text/plain;charset=ISO-8859-1;base64,SGVsbG8sIFdvcmxkIQ=="

(I’m the author)

ericlathrop

ericlathrop OP

Cool! I did try searching hex & elsewhere for a library to parse data URLs before making this. I did manage to eventually find ex_url, but there were a few minor issues I saw with it so I decided to publish data_url anyway. Maybe I should just make a PR to ex_url.

  1. the charset should default to US-ASCII if the mediatype is omitted
  2. the charset is part of the media type, not a separate thing. If it were to be parsed, I’d prefer a separate MimeType struct.
  3. URL.parse("data:") explodes, but I would prefer an :error
  4. URL.parse("data:;base64,=") explodes, but I would prefer an :error
kip

kip

ex_cldr Core Team

Wow, appreciate the bug reports - thank you! I’m on it right away.

Also, hope I didn’t seem like I was hijacking your announcement - great to see new libraries getting built. Well done and much appreciated.

Now back to some bug eradication :slight_smile:

kip

kip

ex_cldr Core Team

On reviewing RFC 6838 it seems like UTF-8 should now be considered the default charset parameter for a mediatype? Your thoughts?

From Section 4.2.1:

If a “charset” parameter is specified, it SHOULD be a required parameter, eliminating the options of specifying a default value. If there is a strong reason for the parameter to be optional despite this advice, each subtype MAY specify its own default value, or alternatively, it MAY specify that there is no default value. Finally, the “UTF-8” charset [RFC3629] SHOULD be selected as the default. See [RFC6657] for additional information on the use of “charset” parameters in conjunction with subtypes of text.

Regardless of what approach is chosen, all new text/* registrations MUST clearly specify how the charset is determined; relying on the US-ASCII default defined in Section 4.1.2 of [RFC2046] is no longer permitted. If explanatory text is needed, this SHOULD be placed in the additional information section of the registration.

ericlathrop

ericlathrop OP

I was going mostly off this MDN page, which refers to RFC2397 which says:

If <mediatype> is omitted, it defaults to text/plain;charset=US-ASCII. As a shorthand,
“text/plain” can be omitted but the charset parameter supplied.

kip

kip

ex_cldr Core Team

Good call. I suspect is a case of RFP’s falling out of sync but I agree, US-ASCII it is.

— All posts loaded —

Where Next?

Trending in Announcing Top

bluzky
You may know https://ui.shadcn.com/, a UI component library for React. I really love it’s design style and components. I’ve built some co...
387 14960 120
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
shahryarjb
The Chelekom project is a library of Phoenix and LiveView components generated via Mix tasks to fit developer needs seamlessly. One of i...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

Other Trending Topics Top

type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
bjorng
We want to introduce a new native datatype to Erlang: native records. Although replacing all tuple records with native records is not our...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New

We're in Beta

About us Mission Statement