Parsing a feed looks like an afternoon's work. RSS is XML, there's a <title> and some <item>s, and a Java library called ROME has read them for twenty years. Then you point your parser at real feeds. One has two blank lines before <?xml. One says it's UTF-8 and isn't. One has no guids, so every poll looks like twenty new posts. One writes its dates as 2026-11-02 10:00:00. One is a 2 GB file. One has a DOCTYPE that tries to read /etc/passwd. And the site you poll every ten minutes starts answering 429.
I hit all of these building Feedhouse, a feed reader written in BoxLang. bx-feeds is that work pulled out into a module: everything between "here's an address someone pasted" and "here are the new posts", for RSS, Atom, JSON Feed and podcasts. XML is parsed by ROME, which ships inside the module. The module adds what a reader needs around it.
This post walks through building the feed side of a reader, step by step:
- Turning a pasted address into a feed
- Fetching and parsing it into one struct, whatever the format
- Polling politely, and knowing which posts are new
- Podcasts, OPML import and export, and instant updates with WebSub
- What can go wrong when you fetch and parse things strangers publish
The output shown comes from a demo script I ran against a small local server while writing this, with the server's address swapped for example.com.
Step 1: Install the module
You need BoxLang 1.19 or later. Install from ForgeBox with any of these:
install-bx-module bx-feeds --local
install-bx-module bx-feeds
box install bx-feeds
The first installs into your project's boxlang_modules folder. The second installs into your BoxLang home, so every app on the machine gets it. The third uses CommandBox.
The module registers seven BIFs: findFeeds(), fetchFeed(), parseFeed(), discoverFeeds(), parseOpml(), toOpml() and webSub(). Each is a thin wrapper over a class under bxModules.bxFeeds.models, which you can use directly when you want to.
Set a User-Agent that says who you are before you fetch anything. Publishers block clients they can't identify. In boxlang.json:
"modules": {
"bxFeeds": {
"settings": {
"userAgent": "MyReader/1.0 (+https://myreader.example)"
}
}
}
Step 2: Turn a pasted address into a feed
People don't paste feed addresses. They paste example.com/blog, or a Mastodon profile, or a GitHub repository. findFeeds() takes whatever they gave you and returns the feeds behind it:
feeds = findFeeds( "example.com/blog" )
for ( feed in feeds ) {
println( "#feed.title# | #feed.url# | #feed.type# | #feed.source#" )
}
Example Blog | https://example.com/feed.xml | rss_2.0 | advertised
It tries four things in order and stops at the first that finds anything:
- The address is already a feed (
source: "itself").
- The page advertises feeds with
<link rel="alternate"> ("advertised"). Up to five are checked, and every one that really is a feed is returned, so you can offer a choice.
- The site is one whose feed address follows from the page's: a Reddit subreddit, a GitHub repository or user, a Mastodon profile or hashtag (
"pattern").
- One of the paths feeds usually live at, such as
/feed, /rss.xml or /atom.xml ("path").
Every feed it returns was fetched and parsed, so you never subscribe someone to a 404. Nothing found is an empty array. The whole search has a time budget, 60 seconds by default, and makes about sixteen requests at the very most.
If you already have a page's HTML, discoverFeeds( html, baseUrl ) does only the second step and makes no requests.
Step 3: Fetch and parse
fetchFeed() gets a feed and parses it in one call:
result = fetchFeed( "https://example.com/feed.xml" )
feed = result.feed
for ( item in feed.items ) {
when = isNull( item.published ) ? "no date" : item.published.toISOString()
println( "[#item.id#] #item.title# | #item.link# | #when#" )
}
[post-2] AT&T and me | https://example.com/posts/2 | 2026-11-02T10:00:00Z
[/posts/1] No guid here | https://example.com/posts/1 | no date
That output is from a feed with a few of the usual problems, and it shows what the module does about them:
- The title was written
AT&amp;T, escaped twice. It comes back as AT&T.
- The links were relative (
/posts/2). They come back absolute.
- The first date was
2026-11-02 10:00:00, which isn't a format any feed spec allows. It's read anyway, as UTC. The second was yesterday-ish, which is null. A date that can't be read is never an error.
- The second item has no guid. It still has an
id.
Whatever the format, the struct has the same keys:
feed = {
type, title, link, feedUrl, description, language, updated, image,
hubs, nextUrl, poll, podcast,
items: [ {
id, title, link, published, updated, authors, summary, contentHtml,
categories, enclosures, commentsUrl, image, durationSeconds, podcast
} ]
}
Text that's missing is "", a list that's missing is [], a number that's unknown is 0, and a date that's missing is null. You don't check which keys exist, and an Atom feed, an RSS 0.91 feed from 1999 and a JSON Feed all look the same to your code.
If you already have the document, because you read it from a file or fetched it yourself, parseFeed( input, options ) takes a string, bytes or a file path and returns the same struct.
What gets read through
Some feeds can't be parsed as they are. The module fixes what it safely can before giving up:
- Whitespace or a byte order mark before
<?xml
- A declared encoding that's wrong in either direction (says UTF-8 and is windows-1252, or says Latin-1 and is UTF-8)
- UTF-8 that was encoded twice, the kind that turns
It’s into It’s
- An attribute declared twice on the root element, which happens when two podcast plugins both add
xmlns:itunes
- A
<skipHours> with an hour that doesn't exist
- An old Netscape DOCTYPE on an RSS 0.91 feed
- HTML entities like
that XML doesn't define
Anything it can't read throws Feeds.ParseError, with a message you can show a user and the parser's reason in detail.
Step 4: Poll politely
A reader fetches the same feeds over and over, and most of the time nothing has changed. A conditional GET lets the server say so in a few bytes. Save the etag and lastModified each fetch returns, and send them back next time:
function poll( required struct subscription ) {
var result = fetchFeed( subscription.url, {
etag : subscription.etag,
lastModified : subscription.lastModified
} )
if ( len( result.permanentUrl ) ) {
// the feed has moved for good: remember its new address
subscription.url = result.permanentUrl
}
if ( result.notModified ) {
return []
}
if ( result.status != 200 ) {
// 404, 410, 429, 500...: result.feed is null
return []
}
subscription.etag = result.etag
subscription.lastModified = result.lastModified
return result.feed.items
}
Fetching the demo feed twice, then an old address for it:
200 etag="v1" maxAge=900 hubs=https://hub.example/ ttl=60
304 notModified=true feed is null=true
moved: 200 permanentUrl=https://example.com/feed.xml
A few things in the result are worth using:
permanentUrl is set when the feed answered with a 301 or 308. Update your stored address, or you'll follow that redirect forever. A temporary redirect (302, 307) is followed but not reported, because it may point somewhere else tomorrow.
- An HTTP error is not an exception. A 404 or a 500 comes back as
result.status with feed null, so one dead feed can't break a polling loop. Exceptions are for things that went wrong on the way: Feeds.FetchError and its subtypes for timeouts, oversized bodies and redirect loops.
maxAgeSeconds and feed.poll.minutes are the publisher telling you how often to check: the first from Cache-Control, the second from RSS <ttl> or sy:updatePeriod. Don't poll faster than they ask.
retryAfterSeconds is a server asking to be left alone, usually with a 429 or 503.
The fetch itself has limits. timeoutSeconds (20 by default) covers the whole thing, so a server that sends one byte a second can't hold a worker. maxBytes (10 MB) is counted after gzip is undone, so a tiny compressed file that expands to gigabytes is cut off too.
When a site says "slow down"
If a feed fetch gets a 429, the module leaves that whole site alone for a while: as long as Retry-After says, or 15 minutes when it doesn't say, doubling each time it happens again. Until then, a fetch of any feed on that site returns at once without making a request:
429 paused=false retryAfter=120
429 paused=true retryAfter=120
The first line is the site saying no. The second is a different feed on the same site, a moment later: paused is true and nothing was sent. A "site" is the registrable domain, so every *.tumblr.com blog shares one pause, the way they share one rate limit. You don't have to build any of this into your poller. Turn it off with pauseAfter429: false if you'd rather.
Step 5: Know which posts are new
A reader dedupes on item.id. The rule is fixed, so the same post gets the same id on every poll:
- The feed's own id, untouched: RSS
<guid>, Atom <id>, JSON Feed id.
- Otherwise the item's link, as the feed wrote it.
- With no link either,
"sha256:" and a hash of the title, date and content.
Store ids per feed, since they're only unique within one, and insert what you haven't seen:
for ( item in result.feed.items ) {
if ( !store.has( subscription.id, item.id ) ) {
store.add( subscription.id, item )
}
}
Two options help here. Some news sites number each edit in the guid, https://example.com/story#3, so every correction shows up as a new post. collapseRevisions: true takes the number off. And dropEmptyItems: true leaves out items with no title, link, content or enclosure, which is what a product feed looks like to a feed parser.
Step 6: Show the content, carefully
item.contentHtml is the best body the item has: the full content when the feed has it, the description when it doesn't. Relative URLs in it are made absolute, in href, src, srcset and style:
<p>See <a href="https://example.com/about">about</a> <img src="https://example.com/posts/img/a.png" onerror="alert(1)"></p>
Look at the end of that line. The onerror handler is still there. The module does not sanitize HTML. A feed is content from a stranger, and it can hold scripts, event handlers and tracking pixels. Run contentHtml and summary through an HTML sanitizer before you put them on a page. That's a job for a dedicated library, and it isn't this module's.
What the module can do is take dangerous URLs out of the fields that are just URLs. With httpOnly: true, every link, image, enclosure and so on that isn't http or https comes back blank, so a javascript: link in a feed can't end up in your <a href>.
Titles, authors and categories are plain text. Escape them on output like any other text.
Step 7: Podcasts
A podcast is a feed whose items have audio in enclosures. Every feed and item also has a podcast struct, read from iTunes and Podcasting 2.0 tags:
show = result.feed
println( "#show.title# by #show.podcast.author#" )
for ( episode in show.items ) {
audio = episode.enclosures.len() ? episode.enclosures[ 1 ].url : ""
println( "S#episode.podcast.season#E#episode.podcast.episode# #episode.title# (#episode.durationSeconds#s) #audio#" )
}
The show has its author, owner, categories, artwork (feed.image), hosts and funding links. An episode has its season and number, duration, artwork, transcripts, chapters, guests and soundbites. YouTube channel feeds work too: the video's thumbnail is item.image, and its description is the content.
The keys are the same on every feed, empty when it isn't a podcast, so you don't need to ask first.
Step 8: Import and export subscriptions
OPML is how people move between readers. parseOpml() reads a subscription list with its folders nested as they are in the file:
list = parseOpml( fileRead( "subscriptions.opml" ) )
// { title, outlines: [ { title, feedUrl, siteUrl, outlines: [] } ] }
An outline with a feedUrl is a subscription. One without is a folder, and its outlines are what's inside. toOpml() writes the same shape back out:
xml = toOpml( [
{ title: "News", outlines: [ { title: "Example Blog", feedUrl: "https://example.com/feed.xml", siteUrl: "https://example.com/" } ] },
{ title: "Unfiled", feedUrl: "https://other.example/rss" }
], "My feeds" )
<?xml version="1.0" encoding="UTF-8"?>
<opml version="2.0">
<head><title>My feeds</title><dateCreated>Wed, 25 Nov 2026 14:02:19 GMT</dateCreated></head>
<body>
<outline text="News" title="News">
<outline type="rss" text="Example Blog" title="Example Blog" xmlUrl="https://example.com/feed.xml" htmlUrl="https://example.com/"/>
</outline>
<outline type="rss" text="Unfiled" title="Unfiled" xmlUrl="https://other.example/rss"/>
</body>
</opml>
Exports from other readers are messy too. The parser accepts xmlUrl however it's capitalized, and reads past the unescaped & that exporters leave in feed addresses.
Step 9: Get new posts the moment they're published
Polling means a post can sit for an hour before you see it. Many feeds name a hub (feed.hubs) that will push new posts to you instead. The protocol is WebSub, and webSub() does the subscriber's side. You serve one callback address and keep a secret per subscription. There are three moments.
Ask the hub to start pushing:
hub = webSub()
secret = hub.newSecret() // save this with the subscription
hub.subscribe( feed.hubs[ 1 ], feed.feedUrl, "https://myreader.example/websub/#token#", { secret: secret } )
The hub then checks you really asked, with a GET to your callback. Echo its challenge back:
check = hub.challenge( queryParams, feed.feedUrl )
if ( check.accepted ) {
// reply 200 with check.challenge as the body
} else {
// reply 404
}
accepted=true challenge=abc123 lease=864000
From then on the hub POSTs new content to your callback. Anyone who learns the address can POST to it, so use the body only when it's signed with your secret:
if ( hub.isSigned( rawBody, headers[ "X-Hub-Signature" ], secret ) ) {
feed = parseFeed( rawBody, { baseUrl: subscription.url } )
}
signed=true forged=false
Put something unguessable in each callback address (the token above), renew before leaseSeconds runs out, and keep polling at a slower pace. Hubs fail quietly.
What can go wrong
A feed reader fetches addresses that users type and parses whatever comes back. Both halves are an attack surface, and the module closes the parts it can.
XML attacks. A feed can carry a DOCTYPE that defines an entity pointing at a file on your server, or at an internal address, or at itself a billion times over. The module never reads a DTD, so none of that works:
parseFeed( '<?xml version="1.0"?>
<!DOCTYPE rss [<!ENTITY xxe SYSTEM "file:///etc/passwd">]>
<rss version="2.0"><channel><title>&xxe;</title></channel></rss>' )
Feeds.ParseError: That isn't a readable RSS or Atom feed
The spec suite proves this against a canary file and a local server that must never be contacted. OPML files get the same treatment.
Private addresses. If your users choose what gets fetched, one of them will try http://localhost:8080/admin or the cloud metadata address. fetchFeed() refuses any address that isn't on the public internet, for the first request and for every redirect:
Feeds.FetchError.PrivateAddress: 127.0.0.1 is not a public address
Feeds.FetchError.PrivateAddress: 169.254.169.254 is not a public address
Set allowPrivateAddresses: true for feeds on your own network, and in tests.
Sites that don't want robots. Some sites refuse anything that isn't a browser, and some put a CAPTCHA in front of their feed. With fallbackUserAgents set, a 401 or 403 is retried as each fallback, and the one a host accepts is remembered. A refusal that's a bot check comes back with challenged: true, so you can tell the user "this site wants a browser" and not "feed not found". Use fallbacks for feeds a site publishes for the public, not to get around a block that's there on purpose.
Testing without the network
Feed code is hard to test against live sites: they change, they go down, and your tests shouldn't hammer them. The module's own specs never touch the network. Parsing is tested from fixture files, and fetching against a tiny HTTP server started on a random free port. You can do the same. BoxLang closures work as Java handlers, so the server is a few lines:
httpServer = createObject( "java", "com.sun.net.httpserver.HttpServer" )
.create( createObject( "java", "java.net.InetSocketAddress" ).init( "127.0.0.1", 0 ), 0 )
httpServer.createContext( "/", ( exchange ) => {
var body = fileReadBinary( "tests/resources/feed.xml" )
exchange.sendResponseHeaders( 200, javacast( "long", arrayLen( body ) ) )
exchange.getResponseBody().write( body )
exchange.close()
} )
httpServer.start()
result = fetchFeed( "http://127.0.0.1:#httpServer.getAddress().getPort()#/feed.xml", { allowPrivateAddresses: true } )
httpServer.stop( 0 )
examples/smoke.bxs in the module runs every BIF this way in one script.
What bx-feeds leaves out
- Sanitizing HTML. Covered above, and worth saying twice.
- Creating feeds. The module reads them. To publish one, Ortus's bx-rss is the
cffeed replacement.
- Storage and scheduling. It doesn't keep your subscriptions or decide when to poll. What it remembers (paused sites, accepted User-Agents) lives in memory and is gone on restart.
- Choosing for you. An episode can have several enclosures and several transcripts. You get all of them, in feed order, and pick.
Wrapping up
Reading feeds is mostly a matter of putting up with what's out there. bx-feeds is the part of a feed reader that does that: it finds the feed, fetches it without being rude, reads it even when it's broken, and hands you the same struct every time. The rest of the reader is yours.
It's MIT licensed. Issues and pull requests are welcome, and so are feeds that break it.
No comments yet — be the first.