Overview

Overview

rss-glib provides GLib data objects and helper APIs for parsing RSS, Atom, and JSON Feed content, with feed discovery and download support built on libsoup.

Discovery and downloader fetches accept http and https by default. They reject file:// URIs unless local-file access is explicitly enabled, and they block loopback, link-local, private, multicast, and unspecified network addresses before fetching.

Downloaded and parsed feeds also have fixed safety limits. Feed input is capped at 10 MiB for HTTP responses, decompressed response data, local files, and direct RssParser input. Parsed RSS, Atom, and JSON Feed documents are capped at 10,000 items, XML nesting is capped at 128 elements, and individual parsed text fields are capped at 1 MiB. OPML parsing uses the same input and field limits, with 10,000 outlines and 64 outline nesting levels. Limit failures are reported as RSS_ERROR_LIMIT_EXCEEDED in the RSS_ERROR domain.

Article extraction accepts HTML from memory or streams and applies the same 10 MiB input limit to both paths. Extraction limit failures are reported as RSS_EXTRACTION_ERROR_TOO_LARGE in the RSS_EXTRACTOR_ERROR domain.

The library is centered around a small set of types:

  • RssDiscovery discovers the feed format for a URI and returns a RssFeed.
  • RssDownloader downloads a configured feed and returns an RssDownloadResult.
  • RssDownloadResult contains a parsed document or reports that it was not modified.
  • RssParser turns bytes into a RssDocument when you already know the format.
  • RssDocument stores feed metadata and a list model of RssArticle objects.
  • RssContent preserves the bytes, media type, markup kind, language, base URI, and optional external URI of feed content.
  • RssAttachment represents RSS enclosures, Atom enclosure links, and JSON Feed attachments through one API.

Quick Start

Include the umbrella header to get the full public API:

#include <rss-glib.h>

Discover And Download

The recommended flow is to discover the feed, then download and parse it:

#include <rss-glib.h>

static RssDocument *
load_feed (const char  *uri,
           GError     **error)
{
  g_autoptr(RssDiscovery) discovery = rss_discovery_new (uri);
  g_autoptr(RssDownloader) downloader = NULL;
  g_autoptr(RssDownloadResult) result = NULL;
  g_autoptr(RssFeed) feed = NULL;

  if (!(feed = dex_await_object (rss_discovery_discover (discovery), error)))
    return NULL;

  downloader = rss_downloader_new (feed, NULL);
  if (!(result = dex_await_object (rss_downloader_download (downloader, NULL, NULL, NULL), error)))
    return NULL;

  return rss_download_result_dup_document (result);
}

If you already know the feed URI and format, you can skip discovery:

#include <rss-glib.h>

static RssDocument *
download_known_feed (const char  *uri,
                     GType        format_type,
                     GError     **error)
{
  g_autoptr(RssFeed) feed = rss_feed_new ();
  g_autoptr(RssDownloader) downloader = NULL;
  g_autoptr(RssDownloadResult) result = NULL;

  rss_feed_set_uri (feed, uri);
  rss_feed_set_format_type (feed, format_type);

  downloader = rss_downloader_new (feed, NULL);
  if (!(result = dex_await_object (rss_downloader_download (downloader, NULL, NULL, NULL), error)))
    return NULL;

  return rss_download_result_dup_document (result);
}

Conditional Downloads

Applications can share a configured SoupSession between downloaders. To avoid downloading and parsing an unchanged feed, persist the effective URI, ETag, and Last-Modified values from RssDownloadResult and supply them to the next download:

downloader = rss_downloader_new (feed, session);
result = dex_await_object (rss_downloader_download (downloader,
                                                    previous_uri,
                                                    previous_etag,
                                                    previous_last_modified),
                           error);

if (result != NULL &&
    rss_download_result_get_status (result) == RSS_DOWNLOAD_STATUS_NOT_MODIFIED)
  {
    /* The server returned HTTP 304; no response body was parsed. */
  }

Validators are only sent to their associated effective URI, including while following redirects. A supplied session is referenced but is not reconfigured, so applications may configure connection limits, a user agent, or a SoupCache before sharing it.

For trusted local files, opt in explicitly before discovery or download:

rss_discovery_set_allow_file_uri (discovery, TRUE);
rss_feed_set_allow_file_uri (feed, TRUE);

Parse Bytes Directly

When you already have the feed contents, use RssParser directly:

#include <rss-glib.h>

static RssDocument *
parse_feed_bytes (const char  *uri_base,
                  GBytes      *bytes,
                  GError     **error)
{
  g_autoptr(RssParser) parser = rss_parser_new ();

  rss_parser_set_format_type (parser, RSS_TYPE_FORMAT_RSS);

  return dex_await_object (rss_parser_parse (parser, uri_base, bytes), error);
}

Inspect Articles

The document exposes articles through GListModel, which makes it easy to integrate with GTK and other GLib-based code:

#include <rss-glib.h>

static void
print_articles (RssDocument *document)
{
  g_autoptr(GListModel) articles = rss_document_list_articles (document);
  guint n_items = g_list_model_get_n_items (articles);

  for (guint i = 0; i < n_items; i++)
    {
      g_autoptr(RssArticle) article = g_list_model_get_item (articles, i);
      const char *title = rss_article_get_title (article);

      g_print ("%s\n", title != NULL ? title : "");
    }
}

Content Semantics

Feed formats do not all make the same promises about text and full content. rss-glib preserves those differences instead of guessing:

  • Atom title, summary, and content retain their declared text, HTML, XHTML, XML, binary, or external representation.
  • RSS titles use RSS_CONTENT_KIND_AMBIGUOUS, because RSS does not say whether title markup is literal text or HTML. RSS description is exposed as an article description because RSS permits it to be either a synopsis or the complete item. It is not reported as an explicit summary. The Content Module’s content:encoded is full content.
  • JSON Feed summary is an explicit summary. content_html and content_text are alternate full-content representations and both are retained.

The string convenience getters return values suitable for safe plain-text display. Known markup is stripped, while an ambiguous RSS title is returned literally instead of being guessed to be HTML. This makes rss_article_get_title(), rss_article_get_summary(), rss_article_get_description(), and rss_article_get_body() safe to pass to plain-text UI without deciding whether the source contained markup.

Use the typed API when rendering rich content:

static void
inspect_content (RssArticle *article)
{
  g_autoptr(RssContent) content = rss_article_dup_primary_content (article);

  if (content == NULL)
    return;

  if (rss_content_is_external (content))
    {
      g_print ("External content: %s\n", rss_content_get_uri (content));
      return;
    }

  switch (rss_content_get_kind (content))
    {
    case RSS_CONTENT_KIND_AMBIGUOUS:
      /* The source format did not say whether this is text or markup. */
      g_print ("%s\n", rss_content_get_plain_text (content));
      break;

    case RSS_CONTENT_KIND_TEXT:
      g_print ("%s\n", rss_content_get_text (content));
      break;

    case RSS_CONTENT_KIND_HTML:
    case RSS_CONTENT_KIND_XHTML:
    case RSS_CONTENT_KIND_XML:
      /* Serialized markup is untrusted and must be sanitized before rendering. */
      g_print ("%s\n", rss_content_get_text (content));
      break;

    case RSS_CONTENT_KIND_BINARY:
      {
        g_autoptr(GBytes) bytes = rss_content_dup_bytes (content);

        g_print ("Binary content: %zu bytes\n",
                 bytes != NULL ? g_bytes_get_size (bytes) : 0);
        break;
      }
    }
}

rss_article_list_contents() returns every full-content representation with the preferred representation first. This is useful for JSON Feed items that provide both HTML and plain text. rss_article_list_attachments() returns enclosures and attachments as RssAttachment objects.