Jump to content

HTTP Headers in XML documents?


neylitalo

Recommended Posts

I've recently found myself in a situation where a frequently-modified XML document was being cached by the web browser, and the changes wouldn't take effect until the client's cache was cleared. The only solution to this, I think, is to send the "Cache-Control: no-cache" header with the XML document. However, I don't see anything in any W3C specifications, RFCs, or w3schools documents that indicate a way to send HTTP headers in an XML document. If someone has a link, can describe how it can be done, or can point me at a document saying that it can't be done, I would very much appreciate it.

Link to comment
Share on other sites

I considered that approach, but there was a problem: There are dozens of these XML files, and I certainly didn't want to go adding PHP code to all these XML files. There are also other applications, both on the server and client side, that reference and modify these XML files, and they wouldn't know how to handle the PHP code.

 

The next step was to find a way to isolate these few files with Apache directives and only send the Cache-Control header with those files. httpd.conf was out of the question, since this code base is used by a handful of developers and one production machine, and it would be a hassle to make everybody update their httpd.conf files. Per-directory configs (also known as .htaccess files :)) saved the day.

 

These directives in .htaccess files in the appropriate directories solved the problem:

 

<Files "filename.xml">
Header set Cache-Control no-cache
</Files>

Link to comment
Share on other sites

In my travels I've run across a little-known Apache feature, one that I thought might help me - the mod_asis module. With this module enabled, you can have Apache send files with a minimum set of headers, and specify the headers you want INSIDE the file - something I thought was pretty cool. :)

 

To shamelessly steal the example provided by Apache:

 

Here's an example of a file whose contents are sent as is so as to tell the client that a file has redirected.

 

Status: 301 Now where did I leave that URL
Location: http://xyz.abc.com/foo/bar.html
Content-type: text/html

<html>
<head>
<title>Lame excuses'R'us</title>
</head>
<body>
<h1>Fred's exceptionally wonderful page has moved to
<a href="http://xyz.abc.com/foo/bar.html">Joe's</a> site.
</h1>
</body>
</html>

 

 

See http://httpd.apache.org/docs/2.0/mod/mod_asis.html for more information.

Link to comment
Share on other sites

In reference to your initial question the reason XML doesn't have caching headers/tags defined is because it is an abstract description language, it was not designed specifically for the web.

Also, XML is content-level, so things such as Authentication/Caching/etc. are left up to the server/client to figure out AFAIK.

I think the Apache solution if the appropriate and best one.

Link to comment
Share on other sites

I didn't think there was a way to send HTTP headers in the official XML specification, but I was hoping for a particular parameter to set that was universally accepted as a way to set HTTP headers. But as you said, the Apache solution looks to be pretty easily implemented and very elegant.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.