Author image Nils Diewald
and 1 contributors


Mojolicious::Plugin::XRD - XRD Document Handling with Mojolicious


  # Mojolicious

  # In controller
  my $xrd = $c->new_xrd;
  $xrd->link(profile => '/me.html');

  # Render as XRD or JRD, depending on request

  # Content-Type: application/xrd+xml
  # <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  # <XRD xmlns=""
  #      xmlns:xsi="">
  #   <Subject>acct:akron@sojolicious.example</Subject>
  #   <Link href="/me.html"
  #         rel="profile" />
  # </XRD>

  # or:
  # Content-Type: application/jrd+json
  # {
  #   "subject":"acct:akron@sojolicious.example",
  #   "links":[{"rel":"profile","href":"\/me.html"}]
  # }

  my $gmail_hm = $c->get_xrd('//');
  print $gmail_hm->link('lrdd')->attrs('template');


Mojolicious::Plugin::XRD is a plugin to support Extensible Resource Descriptor documents through XML::Loy::XRD.

Additionally it supports the rel parameter of the WebFinger specification.



  # Mojolicious

  # Mojolicious::Lite
  plugin 'XRD';

Called when registering the plugin.



  # In Controller:
  my $xrd = $self->new_xrd;

Returns a new XML::Loy::XRD object without extensions.


  # In Controller:
  my $xrd = $self->get_xrd('//');

  # In array context
  my ($xrd, $headers) = $self->get_xrd('//');

  # With relation restrictions and security flag
  $xrd = $self->get_xrd('' => ['lrdd']);

  # With additional headers
  $xrd = $self->get_xrd('' => {
    'X-My-HTTP-Header' => 'Just for Fun'
  } => ['lrdd']);

  # Non-blocking
  $self->get_xrd('//' => sub {
    my ($xrd, $headers) = @_;
    print $xrd->host;

Fetches an XRD document from a given resource and returns it as XML::Loy::XRD document. In array context it additionally returns the response headers as a Mojo::Headers object.

Expects a valid URL. In case no scheme is given (e.g., //, the method will first try to fetch the resource with https and on failure fetches the resource with http, supporting redirections. If the given scheme is https, the discovery will be secured, even disallowing redirections. The second argument may be a hash reference containing HTTP headers. An additional array reference may limit the relations to be retrieved (see the WebFinger specification for further explanation).

This method can be used in a blocking or non-blocking way. For non-blocking retrieval, pass a callback function as the last argument. As the first passed response is the XML::Loy::XRD document, you have to use an offset of 0 in begin for parallel requests using Mojo::IOLoop::Delay.

This method is experimental and may change wihout warnings.


  # In Controllers
  $self->reply->xrd( $xrd );
  $self->reply->xrd( undef, 'acct:acron@sojolicious.example' );

The helper reply->xrd renders an XRD object either in xml or in json notation, depending on the request. If an XRD object is empty, it renders a 404 error and accepts a second parameter as the subject of the error document.


There are different versions of XRD and JRD with different MIME types defined. In some cases you may have to change the MIME type manually.


Mojolicious, Mojolicious::Plugin::XML::Loy.



Copyright (C) 2011-2021, Nils Diewald.

This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.