first pass conneg for exyus

2007-11-22 @ 02:10#

ok, i got the first pass for content negotiation working in the exyus engine. was not too tough. just a couple 'gotchas.'

first, writing the mediatype-checking code was a bit dicey. i actually opted to simplify it for now. i treat wildcards wa bit loosely and do not currently handle the other optional items (other than 'q') properly, but they are rare anyway.

second, the code to cache the items using the media type was not hard at all. it's isolated enough that changing the cache key was only a minor thing. and recalling the proper item from cache was also simple. the key was already composed and everything worked fine from there.

however, keeping the cache fresh was another story. first, i had to implement a SupportedMediaTypes array for the base object. this is used to both do the best-match work upon request and also to scan for items in the cache when it comes time to clear out stale items. next, i had to modify the Requestor.Execute() routine to support the new media types collection. essentially, i now take the collection of URLs to refresh and call that URL for each supported media-type for that resource. this makes sure the cache is clean and has the added benefit of placing a fresh item in cache as soon as it's updated. nice.

the only challenges came when i discovered that the caches was not clearing properly. turns out it had to do with the way the HttpWebRequest.Headers.Add method works. it actually *adds* items - even if they are a duplicate. i was inadvertently adding multiple "Accept" headers to the request as i walked through the list - ick! instead i used Headers.Set and that makes sure to replace the item if it already exists in the collection.

with that done, it as just a matter of cleaning up a few things and all was done. my initial tests weem to work fine (using text/xml and test/html media types) right now. i'll do some more testing over the weekend and, if it all works out, will then work on improving the best-match algorithm.

not bad for a few hours, eh?