AtomPub tip: Watch out for relative paths


It’s not a secret that we here on the Windows Live Writer team are working towards adding support for the Atom Publishing Protocol (AtomPub). Since AtomPub is so new, I thought it might be useful to take notes as I go about working on our client implementation.

Tip #1: Watch out for relative paths (and be especially careful after a redirect).

2.1.3 Use of xml:base and xml:lang

XML elements defined by this specification MAY have an xml:base attribute [REC-xmlbase]. When xml:base is used, it serves the function described in Section 5.1.1 of URI Generic Syntax [RFC3986], by establishing the base URI (or IRI) for resolving relative references found within the scope of the xml:base attribute.

Any URI or IRI that you come across while parsing Atom data may be relative. To escape from relative to absolute (which I highly recommend you do as quickly as possible), you need to:

  1. Crawl up the element stack (i.e. current element, parent, grandparent … root), looking for the xml:base attribute.
  2. If applying the xml:base doesn’t result in an absolute URI/IRI, take the new relative URI/IRI and continue up the stack.
  3. If you get to the root and still have a relative value, then escape against the document’s own URI.

Now, note that the “document’s own URI” may not be the URI that you originally requested to get the document! If the server responded to your original request with a redirect, and the HTTP library you’re using supports automatic redirects, you need to be careful to use the URI that ultimately returned the document. (In the .NET Framework, this is available via the HttpWebResponse.ResponseUri property.)

%d bloggers like this: