Re: Stylesheet library
darryl <[email protected]> Thu, 15 Jul 2004 11:34:05 -0500
| Newsgroups | gmane.comp.web.syncato.general |
|---|---|
| Message-ID | <[email protected]> |
Kimbro Staken wrote:
> Cool.
>
> Did you ever get etags working?
>
>>
>>
Yes and no.
Getting etags working is really simple.
That being said there might be a subtle bug in there somewhere.
It seems to work well enough for aggregators but seems to not work
correctly with multiple reloads with a browser.
What i'm doing is a md5 digest of the result of a GET request. So in
order to get maximum mileage you have to minimize the dynamic content in
the stylesheets. For example the default rss20 template always supplies
the current date. This would, of course, cause the digest to change
every request. So in my template i dropped the date.
------------------8<-------------------------------------------------
def respondToGet(self, transaction):
m = md5.new()
request = transaction.request()
response = transaction.response()
try:
etag = request.serverDictionary()['HTTP_IF_NONE_MATCH']
except:
etag = None
try:
lastmodified =
request.serverDictionary()['HTTP_IF_MODIFIED_SINCE']
except:
lastmodified = None
pathInfo = request.extraURLPath()
print "pathInfo: %s" % pathInfo
try:
stylesheet = request.field('t', "")
# Extra optional argument that can be passed to the stylesheet
arg = request.field('a', "")
# Content query that can be applied as a final step to extract
# something from the rendered content
contentQuery = request.field('c', "")
result = self.weblog.handleRequest(pathInfo, stylesheet,
arg, contentQuery)
# Determine the content-type for the result
if (result.startswith("<?xml")):
contentType = "text/xml"
elif (result.startswith("<html")):
contentType = "text/html"
else:
contentType = "text/plain"
# here we are setting the db-status (which is the timestamp
# of the last modification) as the etag. i'm using an md5 hash
# which is probably completely unnecessary
xml = self.weblog.db.xpathQuery(None, "/status/db-status")
doc = libxml2.parseDoc(xml)
status = doc.xpathEval("//db-status")[0].content
modified = time.strftime("%a, %d %b %Y %H:%M:%S
GMT",time.gmtime(float(status)))
m.update(result)
digest = m.hexdigest()
if digest == etag or modified == lastmodified:
response.setStatus(304, 'Not Modified')
response.write()
else:
response.setStatus(200, 'OK')
response.setHeader('Content-type', contentType)
response.setHeader('Content-length', str(len(result)))
response.setHeader('Etag', digest)
response.setHeader('Last-Modified', modified)
response.write(result)
except NotFoundError:
response.setStatus(404, 'Not Found')
---------8<-----------------------------------------
-darryl
---------
http://randomthoughts.vandorp.ca/WK/blog
-------------------------------------------------------
This SF.Net email is sponsored by BEA Weblogic Workshop
FREE Java Enterprise J2EE developer tools!
Get your free copy of BEA WebLogic Workshop 8.1 today.
http://ads.osdn.com/?ad_id=4721&alloc_id=10040&op=click