<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<title>Paul James</title>
	<subtitle>The rants and ravings of a Web developer living in an un-RESTful world.</subtitle>
	
	<link rel="alternate" type="text/html" href="http://www.peej.co.uk/" />
	<updated>2007-11-04T12:30:01Z</updated>
	<author>
		<name>Paul James</name>
		<uri>http://www.peej.co.uk/about</uri>
	</author>
	<id>http://www.peej.co.uk/default.atom</id>
		<link rel="self" href="http://feeds.feedburner.com/pauljames" type="application/atom+xml" /><entry>
		<title>E-mail Sign On</title>
		<link rel="alternate" type="text/html" href="http://www.peej.co.uk/articles/email-sign-on.html" />
		<id>http://www.peej.co.uk/articles/email-sign-on.html</id>
		<published>2008-12-05T20:26:29Z</published>
		<updated>2008-12-06T13:16:43Z</updated>
		<content type="html">&lt;h2&gt;E-mail Sign On&lt;/h2&gt;

&lt;p&gt;Single sign on, a great thing, saving us from "yet another password", or is
it? &lt;a href="http://www.peej.co.uk/http://www.google.com/search?q=openid"&gt;OpenID&lt;/a&gt; is great, I have huge
respect for all the people involved in the generation of this awesome specification,
but is all this really needed? After all, we all already have a password and a distributed
system for proving we know it.&lt;/p&gt;

&lt;p&gt;Yes, e-mail.&lt;/p&gt;

&lt;h3&gt;E-mail as login credentials&lt;/h3&gt;

&lt;p&gt;Imagine this.&lt;/p&gt;

&lt;ol&gt;
    &lt;li&gt;You come to my site and I need you to log in for some reason.&lt;/li&gt;
    &lt;li&gt;So I prompt you for your e-mail address and you kindly provide it.&lt;/li&gt;
    &lt;li&gt;I send you an e-mail and then tell you to go check your inbox.&lt;/li&gt;
    &lt;li&gt;You go grab your mail, however you like to do that, and follow a link in the
    e-mail to a unique one time signin page.&lt;/li&gt;
    &lt;li&gt;That page gets your browser to set a cookie and effectively logs you in.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Simple, and no username or password in sight, apart from the ones you use to access
your e-mail already. If you need to log in again, you just follow the process again. Simple.&lt;/p&gt;

&lt;p&gt;Plus most sites do this already with forgotten password functionality so it's not a
foreign experience to Web users.&lt;/p&gt;

&lt;p&gt;This is basically what OpenID does but without the human intervention of having
to open an e-mail and click a link. I think having to check your e-mail and click a link
is a pretty good compromise for not having to set up some new authentication system that
may or may not be usable on the site you're visiting.&lt;/p&gt;

&lt;h3&gt;Benefits&lt;/h3&gt;

&lt;p&gt;Everyone has an e-mail address, I mean, why reinvent the wheel when we already have
a system for doing this that everyone already uses. You're not going to get every Web
user to start using OpenID, it's just to much bother when they can just remember (or get
their browser to remember) a username/password pair.&lt;/p&gt;

&lt;p&gt;One of the problems with any single sign on provider is that you have to trust that
provider excusively (sure you can run your own OpenID server but who actually does
that?). Everyone already implicitly trusts their e-mail provider, at least they do with
their e-mail, which in all sense and purposes is all that counts due to forgotten password
e-mails. I know that I'd prefer to only have to trust one entity, rather than an OpenID
provider too.&lt;/p&gt;

&lt;h3&gt;Disadvantages&lt;/h3&gt;

&lt;p&gt;The main advantage of OpenID is that once you've authorised a site, you don't have to
lift a finger again, the negotiation occurs in the background between the site and your
OpenID provider without you doing a thing.&lt;/p&gt;

&lt;p&gt;But this too could be handled via Web-mail providers and some browser hackery:&lt;/p&gt;

&lt;ol&gt;
    &lt;li&gt;Rather than tell you to go check your e-mail in point 3 above, if I can detect
    that you are using a Web-mail provider, I can stuff an &lt;code&gt;iframe&lt;/code&gt; into the
    page pointing to a specific Web page provided by your Web-mail provider that supports
    this technique.&lt;/li&gt;
    &lt;li&gt;This page will of cource be on your Web-mails domain and so as long as you are
    logged into your Web-mail, will be authenticated as you and can thus show the latest
    e-mail in your inbox sent from me.&lt;/li&gt;
    &lt;li&gt;You can then click straight away back into my site without leaving your browser
    or even having to do any funky redirection or message passing magic.&lt;/p&gt;
&lt;/ol&gt;

&lt;p&gt;Not quite as seemless as OpenID, but a hell of a lot simpler, and the infrastructure
from a user point of view is already there.&lt;/p&gt;

&lt;p&gt;How I detect that you're using a Web-mail provider and where their little iframe page
is is a little more tricky. We could do add a TXT entry to DNS for the domain to point
to the pages URL, or maybe eventually we can just rely on the browser to provide the users
e-mail address and Web-mail provider details automatically on the click of a button. Who
knows, there's plenty to inovate around in this space.&lt;/p&gt;

&lt;h3&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;Yes, existing identity providers opening up their systems to support OpenID helps
since it gives many "normal" folk an instant OpenID, but many of these users don't
understand what OpenID is or that they can use their Yahoo ID to log into OpenID supporting
sites.&lt;/p&gt;

&lt;p&gt;Using e-mail as an authentication mechanism makes sense. It makes sense for users and it
makes sense for site developers, and it can be implemented and used today, without any
additional infrastructure or extra support from 3rd parties.&lt;/p&gt;

&lt;p&gt;Although I've never used it, apparently &lt;a href="http://www.peej.co.uk/"&gt;Craigslist&lt;/a&gt; do something very
similar to this. You can create a posting without signing up to an account and all you
have to provide is an e-mail address to which they send you a link that allows you to
update your posting.&lt;/p&gt;

&lt;p&gt;Some would call this security by obscurity, others would call it capability-based
security, I call it clever.&lt;/p&gt;
</content>
	</entry>
		<entry>
		<title>Introducing the RMR Web Architecture</title>
		<link rel="alternate" type="text/html" href="http://www.peej.co.uk/articles/rmr-architecture.html" />
		<id>http://www.peej.co.uk/articles/rmr-architecture.html</id>
		<published>2008-11-28T17:43:01Z</published>
		<updated>2008-11-28T17:43:03Z</updated>
		<content type="html">&lt;h2&gt;Introducing the RMR Web Architecture&lt;/h2&gt;

&lt;p&gt;I talk a lot to people about REST, but with the acronym having become a commonplace
among Web folk, I often find myself having to explain that what they have read about
as REST or think of as REST is actually POX, HTTP-RPC or whatever you want to call it.&lt;/p&gt;

&lt;p&gt;I'm also an advocate of the idea that REST isn't just about "Web services" but is
also a good model for building fast sustainable Web applications. As such, in my
quest to educate the ideas of REST, today I want to outline a different way to build
Web apps, a RESTful way, an alternative to the popular MVC model, a model I call
RMR, or Resource-Method-Representation.&lt;/p&gt;

&lt;h3&gt;Model-View-Controller&lt;/h3&gt;

&lt;p&gt;The current popular way of building Web applications is using the MVC pattern. This
pattern separates the application into three parts:&lt;/p&gt;

&lt;ul&gt;
    &lt;li&gt;The Model models your business objects, the "things" in your application,
    wrapping up data handling and logic.&lt;/li&gt;
    &lt;li&gt;The View is the output to the client.&lt;/li&gt;
    &lt;li&gt;The Controller manages the application flow manipulating data from the Model
    and generate a view.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This architecture has been widely embraced by Web developers even though it was
designed for desktop applications. As far as I'm concerned, MVC doesn't really work
on the Web, it doesn't model and expose resources in a convenient way (URL routing is
evil and actions are methods on controllers exposed as resources rather than methods
on resources themselves.&lt;/p&gt;
&lt;p&gt;It just doesn't model resources, the fundamental element of the Web is totally
ignored. So how can we fix this?&lt;/p&gt;

&lt;h3&gt;Resource-Method-Representation&lt;/h3&gt;

&lt;p&gt;We all agree that slicing our code up into pieces is a good idea, separating concerns
allows us to concentrate on the particular task in hand and make maintenance easier, how
can we separate it that makes more sense in a Web context than MVC?&lt;/p&gt;

&lt;h4&gt;Resource&lt;/h4&gt;

&lt;p&gt;We start with resources. The Web is made up of resources, so to make our application
work with the Web we should model resources. A resource is an object within a RESTful
system, they contain information, are identified a unique identifier (URLs in HTTP), and
respond to the standard interface (in HTTP the standard HTTP methods of GET, PUT, etc.).&lt;/p&gt;

&lt;p&gt;So in an OO language, a HTTP resource can be thought of as an object with private member
variables and a number of public methods that correspond to the standard HTTP methods.
From an MVC point of view, a resource can be thought of as a model with a bit of
controller thrown in.&lt;/p&gt;

&lt;h4&gt;Method&lt;/h4&gt;

&lt;p&gt;Each request is routed to a resource automatically since each resource has a unique URL,
and the method corresponding to the HTTP request method is run. This acts like an MVC
controller, doing any operations necessary to prepare the request response based on the
request.&lt;/p&gt;

&lt;h4&gt;Representation&lt;/h4&gt;

&lt;p&gt;Finally, the last part of the puzzle is the response. In a RESTful system, resources
are exposed to the client as representations, where a representation is in essence a
concreting of the resource into a wire format.&lt;/p&gt;

&lt;p&gt;This Web page you are looking at right now for example, is a HTML representation of the
resource that is this article. There could be other representations, you could view this
article as a ASCII text file, or a PDF document, if I'd made them available to you.&lt;/p&gt;

&lt;p&gt;So the representation is like a view in MVC, we give it a resource object and tell it
to serialize the data into it's output format.&lt;/p&gt;

&lt;h3&gt;Putting it into Practice&lt;/h3&gt;

&lt;p&gt;So lets create some example code of how we might set up an RMR system. We'll do it in
pseudo-code to keep things quick and simple.&lt;/p&gt;

&lt;h4&gt;Front Controller&lt;/h4&gt;

&lt;p&gt;First we'll need a front controller to handle all incoming requests and to do the routing:&lt;/p&gt;&lt;/p&gt;

&lt;div class="code"&gt;request = readRequestData();
resource = loadResourceForThisRequest(request.url);
response = callRequestMethod(request, resource);
response.output();&lt;/div&gt;

&lt;p&gt;This is pretty similar to an MVC front controller, we read data from the request and
do something depending on what's there. The only real question is where do we load a resource
from for a give URL?&lt;/p&gt;

&lt;p&gt;A simple resource it might just be a class that pulls some data from somewhere, or no
data as all the data may be in the representation (as with a HTML page).&lt;/p&gt;

&lt;p&gt;The other case is that we have a collection of data we want to expose as resources, like
a bunch of weblog posts, or the users of the application, or stock in a warehouse,.. you
get the idea. In is case we probably want to map our collection URLs to a resource class
that pulls a row from a database table.&lt;/p&gt;

&lt;h4&gt;Resource Class&lt;/h4&gt;

&lt;p&gt;To program our application, we write resource classes to model our application data, so
what could a base resource class look like:&lt;/p&gt;

&lt;pre&gt;&lt;div class="code"&gt;class Resource {
    
    private resourceData = [];
    
    method constructor(request, dataSource) {
        // load data from data source
    }
    
    method get(request) {
        return new Response(200, getRepresentation(request.url, resourceData));
    }
    
    method put(request) {
        return new Response(405);
    }
    
    method post(request) {
        return new Response(405);
    }
    
    method delete(request) {
        return new Response(405);
    }
}&lt;/div&gt;&lt;/pre&gt;

&lt;p&gt;Our class will load it's data on construction and then run one of the request methods.
The methods that correspond to PUT, POST and DELETE requests all return a 405 response
object (Method Not Allowed) since we don't want them to do anything by default, while our
GET method returns a representation that corresponds to the request URL and the data
loaded into the resource.&lt;/p&gt;

&lt;h4&gt;Routing&lt;/h4&gt;

&lt;p&gt;The final piece of the puzzle is how we route incoming requests to their respective
resource classes. There are many ways to do this and the way you choose depends on personal
preference.&lt;/p&gt;

&lt;p&gt;We could follow a convention, saying that a URL of a format &lt;code&gt;/something&lt;/code&gt; maps
to a resource class called &lt;code&gt;Something&lt;/code&gt;, and then for collections of resources we
could say that any URL of a format &lt;code&gt;/something/item&lt;/code&gt; would map to a single
resource class representing the resources in the collection called something like &lt;code&gt;SomethingItem&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The other extreme would be to map each URL or collection of URLs explicitly to a
resource class.&lt;/p&gt;

&lt;h3&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;So given a bunch of resource classes, some representations, and a way to map URLs
to resource classes and resource methods to representations, we can build up a Web application
in a RESTful way. Add in some HTTP goodness like conditional GET and cache headers, and
we've got a pretty good system.&lt;/p&gt;
</content>
	</entry>
		<entry>
		<title>Virtual World</title>
		<link rel="alternate" type="text/html" href="http://www.peej.co.uk/articles/virtual-world.html" />
		<id>http://www.peej.co.uk/articles/virtual-world.html</id>
		<published>2008-10-28T23:04:28Z</published>
		<updated>2008-11-12T22:56:55Z</updated>
		<content type="html">&lt;h2&gt;Learning From the Web; Building an Open Virtual World&lt;/h2&gt;

&lt;p&gt;The time of Virtual Worlds might just be upon us. Technology has come far enough
that it's now possible for clients to render complex 3D environments and to transfer
a good amount of data over the network.&lt;/p&gt;
&lt;p&gt;You only have to look at online gaming to see that we've been in this position
for some time for a small portion of society, but now with faster and cheaper hardware
we're finally reaching the point where many people's everyday desktop is perfectly
capable of offering a 3D massively multiuser virtual environment.&lt;/p&gt;

&lt;h3&gt;The Walled Garden&lt;/h3&gt;

&lt;p&gt;So what do we have out there today? In 2003 Linden Labs launched Second Life and
There Inc. launched There, both are closed worlds run completely by their parent
companies. There are a number of other 3d worlds that have appeared since, many aimed
at children and teenagers, but all of which are closed. This limits the their
usefulness and cuts them off from the factors that made the Web the successful tool
it is today.&lt;/p&gt;

&lt;h3&gt;The Lessons of the Web&lt;/h3&gt;

&lt;p&gt;So can we build an open 3D world, a 3D Web if you want to call it that, based upon
the things we've learnt from the Web?&lt;/p&gt;
&lt;p&gt;Sure we can, but first lets look at what it is that has made the Web what it is:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Simple open protocol and data formats&lt;/li&gt;
    &lt;li&gt;Open source server and client code&lt;/li&gt;
    &lt;li&gt;Stateless protocol to enable scalability&lt;/li&gt;
    &lt;li&gt;Decentralised architecture&lt;/li&gt;
    &lt;li&gt;Hypermedia&lt;/li&gt;
    &lt;li&gt;Constrained protocol grammar&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So with that in mind, what parts do we need to build a 3D Web?&lt;/p&gt;

&lt;h4&gt;Application protocol&lt;/h4&gt;
&lt;p&gt;If we want our 3D world to be part of the Web, or at least a close cousin of the
Web, it makes sense for us to use HTTP to move data from server to client. HTTP is
great, it does it's job really well and is one of the three pillars of the Web.&lt;/p&gt;
&lt;p&gt;So it makes perfect sense use HTTP to move the representations of our 3D world
to our clients, but what formats should our representations take?&lt;/p&gt;

&lt;h4&gt;Content description language&lt;/h4&gt;
&lt;p&gt;There are two major 3D description formats, X3D and 3DMLW.&lt;/p&gt;
&lt;p&gt;X3D is the 3rd evolution of the original VRML project started in 1994. It is a full
blown 3D modelling language able to be generated by 3D modelling software, it's big
and quite scary.&lt;/p&gt;
&lt;p&gt;3DMLW is a lighter weight format, it simply describes a 3D space and allows you to
place objects within it. These objects can either be 3D primatives defined in the
source file itself or 3D models in a number of existing common 3D formats.&lt;/p&gt;
&lt;p&gt;The thing I like about 3DMLW is that it mirrors HTML in structure, being a simple
document structure that links in more complex media via hypermedia (the power of
the URL).&lt;/p&gt;

&lt;h4&gt;Code on demand&lt;/h4&gt;
&lt;p&gt;Javascript, Lua&lt;/p&gt;

&lt;h4&gt;Hypermedia&lt;/h4&gt;
&lt;p&gt;XHTML, Xlink&lt;/p&gt;
&lt;pre&gt;
&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;document&amp;gt;
    ...
    &amp;lt;content3d id="content" x="0" y="0" width="100" height="100"&amp;gt;
        ...
        &amp;lt;object xlink:href="http://www.peej.co.uk/another.3dmlw" x="1659.999878" y="-17.869379" z="18" pitch="0" yaw="0" roll="0" /&amp;gt;
    &amp;lt;/content3d&amp;gt;
&amp;lt;/document&amp;gt;
&lt;/pre&gt;

&lt;h4&gt;Presence and chat&lt;/h4&gt;
&lt;p&gt;XMPP, IRC&lt;/p&gt;

&lt;pre&gt;
&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;document presence="irc://irc.example.org:194/#example"&amp;gt;
    ...
&amp;lt;/document&amp;gt;
&lt;/pre&gt;

&lt;pre&gt;
PRIVMSG #example :LOC -2.958862 13.826897 13.263217 48
&lt;/pre&gt;

&lt;pre&gt;
LOCATION #example :-2.958862 13.826897 13.263217 48
&lt;/pre&gt;
</content>
	</entry>
		<entry>
		<title>Hypermedia as the Engine of Application State</title>
		<link rel="alternate" type="text/html" href="http://www.peej.co.uk/articles/hypermedia-as-the-engine-of-application-state.html" />
		<id>http://www.peej.co.uk/articles/hypermedia-as-the-engine-of-application-state.html</id>
		<published>2007-12-29T18:54:51Z</published>
		<updated>2007-12-29T18:08:39Z</updated>
		<content type="html">&lt;h2&gt;Hypermedia as the Engine of Application State&lt;/h2&gt;

&lt;p&gt;REST defines four constraints upon the architecture of a Web application:&lt;/p&gt;

&lt;ul&gt;
	&lt;li&gt;A universal syntax for resource-identification&lt;/li&gt;
	&lt;li&gt;A set of well-defined operations&lt;/li&gt;
	&lt;li&gt;Having a shared set of media-types&lt;/li&gt;
	&lt;li&gt;Hypermedia for application state-transitions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The first three are well understood but the fourth tends to slip through peoples
thoughts when in fact it's one of the most important and easiest to grasp of the
RESTful concepts.&lt;/p&gt;

&lt;p&gt;There's always talk of a need for some kind of description language for the Web,
a way for clients to know what resources are out there and which actions can be
done upon them. But of course, in a RESTful system, you don't need such a thing
as hypermedia and the well-defined set of operations (the HTTP methods) take care
of it.&lt;/p&gt;

&lt;h3&gt;Using hypermedia&lt;/h3&gt;

&lt;p&gt;So, lets say we have an application, maybe it's a ordering process application
that allows clients to place orders for different kinds of widgets.&lt;/p&gt;

&lt;p&gt;We could have a client application that has all of the processing data baked
into it, it could know the IDs of all the widgets up front, it could know how to
format and submit an order, but this would closely tie our client application to
our server application creating tight coupling between the two.&lt;/p&gt;

&lt;p&gt;RESTful systems avoid this kind of tight coupling by using hypermedia to guide
the client application. So instead of having a hard coded client, we have a generic
"order processing" client that knows how to interpret the representations our server
produces and take certain actions depending on their contents.&lt;/p&gt;

&lt;p&gt;So lets presume that we want an automated system to place an order for widgets
everyday based on the level of widget stock in our own stores, we wouldn't want to
run out of widgets now would we. An interaction with the service might look something
like this:&lt;/p&gt;

&lt;dl&gt;
	&lt;dt&gt;Client request&lt;/dt&gt;
	&lt;dd&gt;
		&lt;pre&gt;GET /
Host: widget-store.example.com
Accept: application/x-order-process+xml&lt;/pre&gt;
	&lt;/dd&gt;
	&lt;dt&gt;Server response&lt;/dt&gt;
	&lt;dd&gt;
		&lt;pre&gt;Etag: "1f0417-14d6-7f24a1c0"
Content-Length: 369
Content-Type: application/x-order-process+xml

&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;shop checkout="/checkout"&amp;gt;
	&amp;lt;catalogue&amp;gt;
		&amp;lt;item stockNumber="123" name="Small Widget" href="http://www.peej.co.uk/products/123" /&amp;gt;
		&amp;lt;item stockNumber="456" name="Medium Widget" href="http://www.peej.co.uk/products/456" /&amp;gt;
		&amp;lt;item stockNumber="789" name="Large Widget" href="http://www.peej.co.uk/products/789" /&amp;gt;
		&amp;lt;item stockNumber="abc" name="Sparkling Widget" href="http://www.peej.co.uk/products/abc" /&amp;gt;
	&amp;lt;/catalogue&amp;gt;
&amp;lt;/shop&amp;gt;&lt;/pre&gt;
	&lt;/dd&gt;
&lt;/dl&gt;

&lt;p&gt;So our generic client application makes a HTTP GET request to our widget stores
primary URL (aka its homepage). This is the only thing we need to tell our application
up front, as long as both the widget store and our application can understand each
others representations we're in business, this is why we have standard for document
formats (like HTML, CSS, JPEG, etc. or in this case the freshly created just for this
task "&lt;code&gt;application/x-order-process+xml&lt;/code&gt;").&lt;/p&gt;

&lt;p&gt;The respresentation our application gets back outlines the widgets available from
the store and how to place an order. Our application knows that item nodes within
a catalogue node are widgets we can purchase, it also knows that the checkout attribute
of the shop node contains the URL that begins the order placing process.&lt;/p&gt;

&lt;dl&gt;
	&lt;dt&gt;Client request&lt;/dt&gt;
	&lt;dd&gt;
		&lt;pre&gt;GET /products/789
Host: widget-store.example.com
Accept: application/x-order-process+xml&lt;/pre&gt;
	&lt;/dd&gt;
	&lt;dt&gt;Server response&lt;/dt&gt;
	&lt;dd&gt;
		&lt;pre&gt;
Content-Type: application/x-order-process+xml

&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;product&amp;gt;
	&amp;lt;stockNumber&amp;gt;789&amp;lt;/stockNumber&amp;gt;
	&amp;lt;name&amp;gt;Large Widget&amp;lt;/name&amp;gt;
	&amp;lt;stockLevel&amp;gt;2&amp;lt;/stockLevel&amp;gt;
	&amp;lt;price currency="GBP"&amp;gt;7.99&amp;lt;/price&amp;gt;
	&amp;lt;description&amp;gt;This widget is larger than the standard widget in both size and price&amp;lt;/description&amp;gt;
&amp;lt;/product&amp;gt;&lt;/pre&gt;
	&lt;/dd&gt;
&lt;/dl&gt;

&lt;p&gt;Our client application knows that we're running out of "789" widgets, so we need
to order some more. So we get the URL for the "789" widgets from the stock list and
GET a representation that shows our application that they have 2 in stock. Luckily
we don't use many "789" widgets so we only need to order 1 to keep our stock levels
happy.&lt;/p&gt;

&lt;p&gt;If we also need some other widgets we can repeat this procedure building up a list
of stock numbers and quantities of all the widgets we require. If they don't have
enough in stock, our application can issue some kind of warning, maybe it can e-mail
you to warn you that we might be about to run out of something.&lt;/p&gt;

&lt;p&gt;Once our application knows what it wants to order, it can GET the checkout URL:&lt;/p&gt;

&lt;dl&gt;
	&lt;dt&gt;Client request&lt;/dt&gt;
	&lt;dd&gt;
		&lt;pre&gt;GET /checkout
Host: widget-store.example.com
Accept: application/x-order-process+xml&lt;/pre&gt;
	&lt;/dd&gt;
	&lt;dt&gt;Server response&lt;/dt&gt;
	&lt;dd&gt;
		&lt;pre&gt;
Content-Type: application/x-order-process+xml

&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;order
	submit="/checkout/submit"
	method="post"
	type="application/x-order-process+xml"
&amp;gt;
	&amp;lt;input name="name"/&amp;gt;
	&amp;lt;input name="company"/&amp;gt;
	&amp;lt;input name="address"/&amp;gt;
	&amp;lt;multiple name="item"&amp;gt;
		&amp;lt;input name="stockNumber"/&amp;gt;
		&amp;lt;input name="quantity"/&amp;gt;
		&amp;lt;input name="totalPrice"/&amp;gt;
	&amp;lt;/multiple&amp;gt;
	&amp;lt;input name="total"/&amp;gt;
&amp;lt;/order&amp;gt;&lt;/pre&gt;
	&lt;/dd&gt;
&lt;/dl&gt;

&lt;p&gt;Now if you're thinking "that looks like a HTML form with different tags" then
you're right. HTML forms are HTMLs way of showing the client (in its case a HTML
browser) how to send data to the server, and this is our "order process" applications
way.&lt;/p&gt;

&lt;p&gt;Using this, our application can formulate a representation to send to the server
to place our order:&lt;/p&gt;

&lt;dl&gt;
	&lt;dt&gt;Client request&lt;/dt&gt;
	&lt;dd&gt;
		&lt;pre&gt;POST /checkout/submit
Host: widget-store.example.com
Content-type: application/x-order-process+xml

&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;order&amp;gt;
	&amp;lt;name&amp;gt;Jack Bauer&amp;lt;/name&amp;gt;
	&amp;lt;company&amp;gt;CTU&amp;lt;/company&amp;gt;
	&amp;lt;address&amp;gt;Los Angeles&amp;lt;/address&amp;gt;
	&amp;lt;item&amp;gt;
		&amp;lt;stockNumber&amp;gt;789&amp;lt;/stockNumber&amp;gt;
		&amp;lt;quantity&amp;gt;1&amp;lt;/quantity&amp;gt;
		&amp;lt;totalPrice&amp;gt;7.99&amp;lt;/totalPrice&amp;gt;
	&amp;lt;/item&amp;gt;
	&amp;lt;total&amp;gt;7.99&amp;lt;/total&amp;gt;
&amp;lt;/order&amp;gt;
&lt;/pre&gt;
	&lt;/dd&gt;
	&lt;dt&gt;Server response&lt;/dt&gt;
	&lt;dd&gt;
		&lt;pre&gt;
Content-Type: application/x-order-process+xml

&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;confirmation orderNumber="123-456" href="http://www.peej.co.uk/orders/123-456"/&amp;gt;&lt;/pre&gt;
	&lt;/dd&gt;
&lt;/dl&gt;

&lt;p&gt;But why go through all of this rather than just hardcode the
required representation into our client? Well we could, but then the client would
be tightly coupled to the server, so if the server changed, the client would need
to change too. Not a problem when you control both the server and the client, but if
like in our example, the widget company runs the server and we run the client, then
we'd be in trouble if the widget company decided that an extra step was required
in the ordering process, say an extra confirmation stage.&lt;/p&gt;

&lt;dl&gt;
	&lt;dt&gt;Client request&lt;/dt&gt;
	&lt;dd&gt;
		&lt;pre&gt;POST /checkout/submit
Host: widget-store.example.com
Content-type: application/x-order-process+xml

&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;order&amp;gt;
	&amp;lt;name&amp;gt;Jack Bauer&amp;lt;/name&amp;gt;
	&amp;lt;company&amp;gt;CTU&amp;lt;/company&amp;gt;
	&amp;lt;address&amp;gt;Los Angeles&amp;lt;/address&amp;gt;
	&amp;lt;item&amp;gt;
		&amp;lt;stockNumber&amp;gt;789&amp;lt;/stockNumber&amp;gt;
		&amp;lt;quantity&amp;gt;1&amp;lt;/quantity&amp;gt;
		&amp;lt;totalPrice&amp;gt;7.99&amp;lt;/totalPrice&amp;gt;
	&amp;lt;/item&amp;gt;
	&amp;lt;total&amp;gt;7.99&amp;lt;/total&amp;gt;
&amp;lt;/order&amp;gt;
&lt;/pre&gt;
	&lt;/dd&gt;
	&lt;dt&gt;Server response&lt;/dt&gt;
	&lt;dd&gt;
		&lt;pre&gt;
Content-Type: application/x-order-process+xml

&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;confirmation
	orderNumber="123-456"
	href="http://www.peej.co.uk/orders/123-456"
	submit="/orders/123-456/confirm" method="post"
	type="application/x-order-process+xml"
/&amp;gt;&lt;/pre&gt;
	&lt;/dd&gt;
	&lt;dt&gt;Client request&lt;/dt&gt;
	&lt;dd&gt;
		&lt;pre&gt;POST /orders/123-456/confirm
Host: widget-store.example.com
Content-type: application/x-order-process+xml

&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;order&amp;gt;
	&amp;lt;name&amp;gt;Jack Bauer&amp;lt;/name&amp;gt;
	&amp;lt;company&amp;gt;CTU&amp;lt;/company&amp;gt;
	&amp;lt;address&amp;gt;Los Angeles&amp;lt;/address&amp;gt;
	&amp;lt;item&amp;gt;
		&amp;lt;stockNumber&amp;gt;789&amp;lt;/stockNumber&amp;gt;
		&amp;lt;quantity&amp;gt;1&amp;lt;/quantity&amp;gt;
		&amp;lt;totalPrice&amp;gt;7.99&amp;lt;/totalPrice&amp;gt;
	&amp;lt;/item&amp;gt;
	&amp;lt;total&amp;gt;7.99&amp;lt;/total&amp;gt;
&amp;lt;/order&amp;gt;
&lt;/pre&gt;
	&lt;/dd&gt;
	&lt;dt&gt;Server response&lt;/dt&gt;
	&lt;dd&gt;
		&lt;pre&gt;
Content-Type: application/x-order-process+xml

&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;confirmation orderNumber="123-456" href="http://www.peej.co.uk/orders/123-456"/&amp;gt;&lt;/pre&gt;
	&lt;/dd&gt;
&lt;/dl&gt;

&lt;p&gt;So as long as our client application knows how to understand and generate
"&lt;code&gt;application/x-order-process+xml&lt;/code&gt;" documents, and as long as the server
also only generates and receives "&lt;code&gt;application/x-order-process+xml&lt;/code&gt;"
documents, then everything will keep on working, even if things change at either
end.&lt;/p&gt;

&lt;p&gt;All of this and no resource description in sight. All the co-ordination is dictated
by the fact that we have a uniform interface defined by the HTTP and URI specifications,
and that we have decided upon a standard grammar for our representation exchanges.&lt;/p&gt;

&lt;h3&gt;Web services as Web sites&lt;/h3&gt;

&lt;p&gt;None of this is rocket science, none of it is new, we've been building Web applications
for human consumption using HTML as our representation grammar for the last 15 years,
so why change anything for robot consumption?&lt;/p&gt;

&lt;p&gt;When it comes to building a Web "service", remember that you are really just
building a Web site with a different representation format. The most important thing
you can do is to get your format hammered down from the start (or pick an existing
format if it suits your needs or extend an existing format if it doesn't so at least
you can gain some network effects from it, &lt;a href="http://www.peej.co.uk/"&gt;Atom&lt;/a&gt; is a good place to start for many uses).&lt;/p&gt;

&lt;p&gt;So next time someone goes on about how RESTful systems can't possibly work because
they need some kind of description language to enable clients to be wired to servers,
remind them of hypermedia and point them to any part of the Web for a real World
example of REST in action.&lt;/p&gt;
</content>
	</entry>
		<entry>
		<title>Is Bit Torrent the future of the Web?</title>
		<link rel="alternate" type="text/html" href="http://www.peej.co.uk/articles/peer-to-peer-web.html" />
		<id>http://www.peej.co.uk/articles/peer-to-peer-web.html</id>
		<published>2007-03-13T00:00:00Z</published>
		<updated>2007-11-03T18:02:49Z</updated>
		<content type="html">&lt;h2&gt;Is Bit Torrent the future of the Web?&lt;/h2&gt;

&lt;p&gt;Håkon Lie of Opera Software spoke at a &lt;a href="http://www.peej.co.uk/http://video.yahoo.com/video/play?vid=cccd4aa02a3993ab06e56af731346f78.2006940&amp;fr="&gt;Silicon
Valley WebBuilder event&lt;/a&gt; at Yahoo earier this month talking about the future of
the browser. One thing he talked about was experimental native support for video
formats, bypassing the 12 year old and crumbling Netscape plug-in format. This seems
like an obvious idea seeing as the amount of video we have online these days, why
not roll these things into a browser and make a more seemless experience.&lt;/p&gt;

&lt;p&gt;This got me thinking about what else could be in the future for the browser and
for the Web in general, not the obvious things in the near future like HTML5 support,
CSS3 or native media format support. My mind went straight to Bit Torrent.&lt;/p&gt;

&lt;p&gt;The obvious place for Bit Torrent is for file downloads, again Opera are miles
ahead of the game by baking a torrent client into Opera in 2005, but I'm thinking
of something all together more radical.&lt;/p&gt;

&lt;p&gt;A major problem with the Web as it is today is bandwidth. We're all familiar with
the &lt;a href="http://www.peej.co.uk/http://en.wikipedia.org/wiki/Slashdot_effect"&gt;Slashdot effect&lt;/a&gt;, as
soon as something becomes popular the servers melt and server admins get very nervous.
Bit Torrent on the other hand loves it when the heat is on, so surely there's a way
we can leverage that power in the Web.&lt;/p&gt;

&lt;h3&gt;Using Bit Torrent to power the Web&lt;/h3&gt;

&lt;p&gt;The obvious way to use Bit Torrent would be to fetch inline assets associated
with a Web page; images, Flash movies, and other static content we'd otherwise be
HTTP GETting. I'm not an expect in how the Bit Torrent protocol works, but I'd imagine
that it wouldn't be too difficult to provide all the assets for a page as a torrent
file and cause each browser accessing the page (and sitting on the page after it's
loaded) to join the swarm for that torrent. This way, any assets on the page would
be fetched by Bit Torrent from the swarm so as soon as our Slashdot friends arrive
our bandwidth and our servers are spared the majority of the load.&lt;/p&gt;

&lt;p&gt;Of course we'd have to implement this in a backwards compatible way, the HTML
link element could provide the solution, something like this:&lt;/p&gt;

&lt;pre class="code"&gt;
&amp;lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&amp;gt;
&amp;lt;html lang="en"&amp;gt;
	&amp;lt;head&amp;gt;
		&amp;lt;title&amp;gt;Page to be Slashdotted&amp;lt;/title&amp;gt;

		&amp;lt;link type="text/css" rel="stylesheet" href="http://www.peej.co.uk/default.css"&amp;gt;
		&amp;lt;link type="application/x-bittorrent" rel="torrent" href="http://www.peej.co.uk/heavy-lifting.torrent"&amp;gt;
	&amp;lt;/head&amp;gt;
	&amp;lt;body&amp;gt;
		&amp;lt;h1&amp;gt;Welcome Slashdotters&amp;lt;/h1&amp;gt;

		&amp;lt;p&amp;gt;Here's the big image you were after:&amp;lt;/p&amp;gt;
		&amp;lt;img src="http://www.peej.co.uk/big-image.jpg"&amp;gt;
	&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/pre&gt;

&lt;p&gt;Upon spotting the "torrent" link element, our Bit Torrent enabled browser would
leap into action, downloading the torrent file and joining the swarm. Then for any
inline resources that we need to download, we can check the swarm and grab the file
from it rather than issuing a HTTP GET to the server.&lt;/p&gt;

&lt;p&gt;Our server would obviously have to act as a Bit Torrent tracker and seeder, but
this would be easy to implement as an Apache module, automatically seeding each asset
of a particular mimetype.&lt;/p&gt;

&lt;p&gt;I'm sure that either people are working on this right now, or there's something
fundamentally wrong with my idea, either way, there are still plenty of Web problems
out there to be solved.&lt;/p&gt;
</content>
	</entry>
		<entry>
		<title>Living Without Sessions</title>
		<link rel="alternate" type="text/html" href="http://www.peej.co.uk/articles/no-sessions.html" />
		<id>http://www.peej.co.uk/articles/no-sessions.html</id>
		<published>2006-09-27T00:00:00Z</published>
		<updated>2007-11-03T18:04:10Z</updated>
		<content type="html">&lt;h2&gt;Living Without Sessions&lt;/h2&gt;

&lt;p&gt;When we talk about sessions in Web app development, we're usually talking about
server stored data about a particular client. Some Web frameworks use session state
to track and hold information about the user throughout their journey through the
site, however they go against the RESTful principles and should really be treated
as a bug.&lt;/p&gt;
&lt;p&gt;Developers became used to having sessions available to them, so when systems
grew, became more complex, and started spreading over multiple servers, more and more
hacks had to be introduced to keep the session support working, when in reality,
sessions should never have been introduced in the first place.&lt;/p&gt;
&lt;p&gt;This article isn't to argue the pros and cons of server sessions, you can research
that for yourself on the Web, we're going to look at an example system that is commonly
built using sessions and how we can avoid sessions and come up with a better system.&lt;/p&gt;

&lt;h3&gt;Building a shopping cart with sessions&lt;/h3&gt;

&lt;p&gt;We're going to look at a standard shopping cart application, it will do the usual,
display a bunch of products to the client and allow them to place them into their cart.&lt;/p&gt;
&lt;p&gt;With sessions, we'd create a session, tie it to the client, and use it to store
the IDs of each product the user places in their basket. Great, until we get a gazillion
users and our server burns up in a firey memory related hell or we try to load balance
across multiple servers so we need to buy expensive load balancers with layer 7 sticky
session support. Yuk! There must be a better way, well infact there isn't a better way,
there are two.&lt;/p&gt;

&lt;h3&gt;Cart on the client&lt;/h3&gt;
&lt;p&gt;When you think about someone in real life going into a shop and placing items into
their shopping basket, where is the basket? It's with the user. So why don't we model
our online shop to mirror the real life scenario.&lt;/p&gt;
&lt;p&gt;Web browsers used to be a document reader for displaying hypertext documents transfered
over the HTTP protocol, but nowerdays they are an application platform thanks to the
development and deployment of Javascript within the HTML document and the browser.
So we can use Javascript to extend our clients browser to be able to store their shopping
cart until they reach the checkout.&lt;/p&gt;
&lt;p&gt;We have to do a little work to make the browser hold it's state between page requests.
We have a few choices:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Place the catelogue part of the site in a frameset with a "basket" frame
    that stores and displays the users basket to them. Pretty easy to do but not
    great since frames are a usability nightmare and we have to do extra work to
    provide bookmarkable URLs to individual products.&lt;/li&gt;
    &lt;li&gt;Expect the client to also support cookies and stuff the cart data in there.
    Very easy to do and they can persist for as long as we need, but cookies weren't
    designed for client state storage and they'll be sent to the server with every
    request when we only need them to be available to our client.&lt;/li&gt;
    &lt;li&gt;Wait until the &lt;a href="http://www.peej.co.uk/http://whatwg.org/specs/web-apps/current-work/"&gt;WHATWG
    Web Application 1.0 spec&lt;/a&gt; is finished and implemented in browsers and use
    the new &lt;a href="http://www.peej.co.uk/http://whatwg.org/specs/web-apps/current-work/#scs-client-side"&gt;sessionStorage
    object&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Cart on the server&lt;/h3&gt;
&lt;p&gt;On the server, isn't that the same as storing a session on the server?&lt;/p&gt;
&lt;p&gt;Yes and no. We can store the users cart on the server as long as we do it in a
RESTful way we won't hit any of the problems associated with user sessions. The
problem with sessions is that we're storing client state on the server, however
if we don't treat the clients cart as being part of their state and rather treat it
as being part of the shop itself, then we can get around the problems with sessions.&lt;/p&gt;
&lt;p&gt;Imagine a shop where rather than being self-service, there is a shopkeeper who
upon being asked, goes and fetches the items you want and rings them up on the till.
Now if we were to model that as our online shop we'd see that the client no longer
has a basket as part of their state, the basket is part of the shop.&lt;/p&gt;
&lt;p&gt;So how would this work? Looking at the interactions between the client and the
server, it might look something like this:&lt;/p&gt;
&lt;table&gt;
    &lt;tr&gt;
        &lt;th&gt;Client&lt;/th&gt;
        &lt;td&gt;Show me your products.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;th&gt;Server&lt;/th&gt;
        &lt;td&gt;Here's a list of all the products you can buy at this shop.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;th&gt;Client&lt;/th&gt;
        &lt;td&gt;Good, okay, I'd like to buy 1 of http://example.org/shop/product/X, please place it in my basket,
        my username is "JohnDoe" and my password is "secretPassword".&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;th&gt;Server&lt;/th&gt;
        &lt;td&gt;Okay, I've added 1 of http://example.org/shop/product/X into your basket, you can review your basket
        at http://example.org/shop/users/johndoe/basket&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;th&gt;Client&lt;/th&gt;
        &lt;td&gt;I'd like to buy 1 of http://example.org/shop/product/Y as well, please place one in my basket,
        my username is "JohnDoe" and my password is "secretPassword".&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;th&gt;Server&lt;/th&gt;
        &lt;td&gt;Okay, I've added 1 of http://example.org/shop/product/Y into your basket as well, you can still review
        your basket at http://example.org/shop/users/johndoe/basket&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;th&gt;Client&lt;/th&gt;
        &lt;td&gt;Actually I don't want http://example.org/shop/product/X after all, please remove it from my cart,
        my username is "JohnDoe" and my password is "secretPassword".&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;th&gt;Server&lt;/th&gt;
        &lt;td&gt;Okay, I've removed http://example.org/shop/product/X from your basket, you can review your updated
        basket at http://example.org/shop/users/johndoe/basket&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;th&gt;Client&lt;/th&gt;
        &lt;td&gt;Okay I'm done, ring 'em up, my username is "JohnDoe" and my password is
        "secretPassword".&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;th&gt;Server&lt;/th&gt;
        &lt;td&gt;Should I charge that to your expense account?&lt;/td&gt;
    &lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;The thing to notice about this conversation is that it is stateless, every action
from the client is independent of any other. This means that at any time, the user
can run off and do something else, come back a few days later and carry on. It also
means they could get some other service to add things to their shopping basket easily.&lt;/p&gt;

&lt;h4&gt;Ain't that just a session?&lt;/h4&gt;
&lt;p&gt;So how does this differ from storing the cart in a user session on the server?
After all doesn't the conversation above also apply no matter whether we're storing
the cart in a session or in a resource?&lt;/p&gt;
&lt;p&gt;Firstly a users session is transitory, it's there when the user is there, but will
be cleaned up and lost once they leave, in our RESTful design the cart is as integral
as a user account, so whatever means we use to store and process user accounts we
use to store and process the users cart (so it might be a MySQL database cluster
with Memcached in front of it).&lt;/p&gt;
&lt;p&gt;Secondly we can grab hold of the users cart since it's a resource that has a URL,
so we can query and manipulate it at will, pass the URL to other services, etc.&lt;/p&gt;
&lt;p&gt;The differences can seem very subtle, but it's the subtleties that make the
difference, since we're explicitly creating and adjusting resources on the server
all the problems of session handling disappear and are covered by our resource handling
solution (our DB cluster for example).&lt;/p&gt;

&lt;h3&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;Avoiding sessions is a bit of a purest stance, but it does lead to a more scalable
and usable Web app. Keeping the clients state on the client is always a good idea, you
can't scale better than by utilising someone elses computer, of course depending on your
application and the technologies you are using your mileage may vary.&lt;/p&gt;
&lt;p&gt;If you do find you need to store transitory client data on the server, think about
re-working that data or the way your app. works so that the data has meaning, give it
a URL and turn it into a resource the user can manipulate.&lt;/p&gt;
</content>
	</entry>
		<entry>
		<title>HTTP Caching</title>
		<link rel="alternate" type="text/html" href="http://www.peej.co.uk/articles/http-caching.html" />
		<id>http://www.peej.co.uk/articles/http-caching.html</id>
		<published>2006-09-27T00:00:00Z</published>
		<updated>2007-11-03T18:03:50Z</updated>
		<content type="html">&lt;h2&gt;HTTP Caching&lt;/h2&gt;

&lt;p&gt;HTTP has a very thorough and well supported caching mechanism, but in this age of
the dynamic Web page, it often goes unused when it is needed the most. So what do we, as
Web programmers, need to do to make sure our pages are cached correctly?  Let's have
a look.&lt;/p&gt;

&lt;h3&gt;The reasoning&lt;/h3&gt;
&lt;p&gt;What is the point of caching? The idea is that on the Web, it is often better to have
stale data than to wait for the network. Data only changes every so often and even if it
has changed, it's not always important that the client has the latest data, so caching
it either at the client, or at an intermediary, isn't a problem and should infact be a
good idea. &lt;a href="http://www.peej.co.uk/http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html"&gt;RFC2616&lt;/a&gt; says:&lt;/p&gt;
&lt;blockquote cite="http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html"&gt;
    &lt;p&gt;Caching would be useless if it did not significantly improve performance. The goal
    of caching in HTTP/1.1 is to eliminate the need to send requests in many cases, and
    to eliminate the need to send full responses in many other cases. The former reduces
    the number of network round-trips required for many operations; we use an "expiration"
    mechanism for this purpose. The latter reduces network bandwidth requirements; we use
    a "validation" mechanism for this purpose.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Allowing clients and intermediaries to cache our pages will lighten the load on our
servers and create a faster user experience. One of the problems with dynamic Web applications
is that your Web server thinks it is sending new data every request and so can't set the
correct caching headers, you need to do a little extra work yourself within your application
to make sure caching is working for you.&lt;/p&gt;
&lt;p&gt;There are two types of caching in HTTP, we'll look at both, how they work, how they work
together, and how to make sure your Web application is taking advantage of them.&lt;/p&gt;

&lt;h3&gt;Expiration&lt;/h3&gt;
&lt;p&gt;The expiration model is a way for the server to say how long a requested resource stays
fresh for, user agents should cache the resource response and re-use it until its cache
is no longer fresh.&lt;/p&gt;
&lt;p&gt;Expiration is excellent for resources that change at known times or the change very rarely
and whose staleness does not matter. For example, images and style sheets that are used
across a site often do not change very often and so should be sent with a expiration date
of at least 24 hours. The user agent will then only download them once, no matter how many
pages of the site they visit.&lt;/p&gt;
&lt;p&gt;If we are serving resources from a dynamic data source, for example say we have a graphic
that portrays the current weather outside, if we know that the data only updates once an hour,
we can set the expiration date to an hour so that clients only request it once per hour.&lt;/p&gt;
&lt;h4&gt;Expires header&lt;/h4&gt;
&lt;p&gt;The simpliest way of doing this is with the HTTP Expires header, it just contains the date
and time of when the resource will become stale:&lt;/p&gt;
&lt;div class="code"&gt;Expires: Sun, 25 Jun 2006 14:57:12 GMT&lt;/div&gt;
&lt;h4&gt;Cache-Control header&lt;/h4&gt;
&lt;p&gt;The Expires header has a few problems like requiring the server and client to have clocks
that are in sync. So HTTP 1.1 replaced it with the Cache-Control header that offers more
flexibility:&lt;/p&gt;
&lt;div class="code"&gt;Cache-Control: max-age=86400, must-revalidate&lt;/div&gt;
&lt;p&gt;The cache control header has a number of clauses that can be used to control the way the
client caches the resource.&lt;/p&gt;
&lt;dl&gt;
    &lt;dt&gt;max-age=86400&lt;/dt&gt;
    &lt;dd&gt;The number of seconds the resource will be considered fresh, similar to the Expires
    header except the number of seconds from now is used rather than a specific date/time.&lt;/dd&gt;
    &lt;dt&gt;s-maxage=86400&lt;/dt&gt;
    &lt;dd&gt;The same as max-age, except that it only applies to shared caches.&lt;/dd&gt;
    &lt;dt&gt;public&lt;/dt&gt;
    &lt;dd&gt;Makes the response always cacheable even if it wouldn't normally be cacheable (behind
    authentication, etc.)&lt;/dd&gt;
    &lt;dt&gt;no-cache&lt;/dt&gt;
    &lt;dd&gt;Forces caches ask the server for validation before releasing a cached copy, so if the
    server says that the cached version is still fresh it is served from the cache.&lt;/dd&gt;
    &lt;dt&gt;must-revalidate&lt;/dt&gt;
    &lt;dd&gt;Forces caches to obey any freshness information you give them about a resource.&lt;/dd&gt;
    &lt;dt&gt;proxy-revalidate&lt;/dt&gt;
    &lt;dd&gt;The same as must-revalidate except that it only applies to shared (proxy) caches.&lt;/dd&gt;
&lt;/dl&gt;

&lt;h3&gt;Validation&lt;/h3&gt;
&lt;p&gt;The validation model allows a client to ask the server whether a cached version of a resource
is still fresh. If the client doesn't have a fresh cached version, the server will respond with
a fresh version of the resource, while if it does, the server will say so and send nothing.&lt;/p&gt;
&lt;p&gt;This is useful as it allows our server to tell a client it already has the freshest version
and not do any processing. If the resource is dynamic, we can save our server from having to do
all the work involved in producing the response since the client already has it in its cache.&lt;/p&gt;
&lt;h4&gt;Last-Modified header&lt;/h4&gt;
&lt;p&gt;Like the expiration model, there are two HTTP headers that control validation, the Last-Modified
header is defined in HTTP 1.0 and sends the client the date/time when the resource was last modified:&lt;/p&gt;
&lt;div class="code"&gt;Last-Modified: Sun, 25 Jun 2006 14:57:12 GMT&lt;/div&gt;
&lt;p&gt;When a client sends a "If-Modified-Since" request header, the date/time sent in that header
can be compared with the resources last modified date/time, and if it matches, a 304 Not Modified
HTTP response sent.&lt;/p&gt;
&lt;h4&gt;Etag header&lt;/h4&gt;
&lt;p&gt;The Last-Modified header suffers the same problems as the Expires header, and thus was replaced
in HTTP 1.1 with the Etag header. An Etag is a string that uniquely identifies a resource, they
should be generated by the server in a way as to change whenever the resource does, a common Etag
value is the MD5 hash of the resource or of the resources URL and its modified date/time stamp.&lt;/p&gt;
&lt;p&gt;When a client sends a "If-None-Match" request header, the Etag value in that header should
be compared to the resource and if it matches, a 304 Not Modified response sent.&lt;/p&gt;

&lt;h3&gt;Using caching&lt;/h3&gt;
&lt;p&gt;So to make sure your dynamic resource behaves well when it comes to HTTP caching, you need to
support validation and optionally send a expiration header from within your script.&lt;/p&gt;
&lt;p&gt;Supporting validation in PHP this is pretty simple, sending the correct headers is just a
case of:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre&gt;function sendValidationHeaders($mtime, $etag)
{
    header('Last-Modified: '.gmdate('D, j M Y H:i:s', $mtime).' GMT');
    header('ETag: '.$etag);
}
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And doing the actual validation just requires a function like:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre&gt;function isModified($mtime, $etag)
{
    return !((isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &amp;&amp;
        strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) &gt;= $mtime) ||
        (isset($_SERVER['HTTP_IF_NONE_MATCH']) &amp;&amp;
        $_SERVER['HTTP_IF_NONE_MATCH'] == $etag));
}
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If you want to tell clients when your resource expires, you need a function like:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre&gt;function sendExpireHeaders($seconds)
{
    header('Expires: '.gmdate('D, j M Y H:i:s T', time() + $seconds));
    header('Cache-Control: max-age='.$seconds.', must-revalidate');
}
&lt;/pre&gt;&lt;/div&gt;

&lt;h3&gt;Other caches&lt;/h3&gt;
&lt;p&gt;As well as HTTP caching, your Web application may also want to use various application caching
mechanisms. If you're hitting a database often you may want to place an object cache like
&lt;a href="http://www.peej.co.uk/http://www.danga.com/memcached/"&gt;Memcache&lt;/a&gt; in front of it so that often requested
data can be cached in Web server memory rather than being re-requested from the database. If your
dynamic app has lots of pages that don't update very often you may want to place a reverse proxy
like &lt;a href="http://www.peej.co.uk/http://www.squid-cache.org/"&gt;Squid&lt;/a&gt; in front of it to save re-generation of
all non-changed pages across users. The setting up and using of other caches are beyond the
scope of this article, &lt;a href="http://www.peej.co.uk/http://wiki.squid-cache.org/SquidFaq/ReverseProxy"&gt;an intro to
using Squid as a reverse proxy can be found here&lt;/a&gt; and the &lt;a href="http://www.peej.co.uk/http://www.php.net/memcache"&gt;PHP
memcached extensions documentation is here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For more info on HTTP caching, check out &lt;a href="http://www.peej.co.uk/http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html"&gt;section
10 of RFC2616&lt;/a&gt; and for an easier read, have a look at Mark Nottingham's &lt;a href="http://www.peej.co.uk/http://www.mnot.net/cache_docs/"&gt;Caching
Tutorial&lt;/a&gt;.&lt;/p&gt;
</content>
	</entry>
		<entry>
		<title>HTTP Authentication with HTML Forms</title>
		<link rel="alternate" type="text/html" href="http://www.peej.co.uk/articles/http-auth-with-html-forms.html" />
		<id>http://www.peej.co.uk/articles/http-auth-with-html-forms.html</id>
		<published>2006-02-03T16:28:00Z</published>
		<updated>2008-04-25T18:26:41Z</updated>
		<content type="html">&lt;h2&gt;HTTP Authentication with HTML Forms&lt;/h2&gt;
&lt;p&gt;Authentication in Web applications has been highjacked, HTTP defines a standard
way of providing authentication but most apps use the evil spawn of Netscape, otherwise
known as cookies. Why is this? Cookies aren't designed for authentication, they're a
pain to use for it, insecure unless you know what you're doing, non-standard, and
unRESTful.&lt;/p&gt;

&lt;p class="warning"&gt;&lt;strong&gt;Warning:&lt;/strong&gt; The solution outlined in this article is
experimental and might be a complete lie, be warned that your mileage may/will vary.&lt;/p&gt;

&lt;p&gt;The main reason people walk away from using HTTP authentication is that they want
control over the look of the login form and most browsers display an awful looking dialog
box. So what we need is a way for HTML forms to pass HTTP auth data when it's submitted.
The HTML spec provides HTML forms as a way to create queries and to POST urlencoded
data to a URL, but can we subvert it?&lt;/p&gt;
&lt;h3&gt;With the power of Javascript we can&lt;/h3&gt;
&lt;p&gt;We could add an &lt;code&gt;onsubmit&lt;/code&gt; event to our login form that pushes the username
and password values within our form into the URL in the forms &lt;code&gt;action&lt;/code&gt; attribute.
That way our login request would supply the users credentials in the URL and avoid the
server returning a 401 response and causing our browser from showing the HTTP auth box.&lt;/p&gt;
&lt;p&gt;Great, and pretty easy. We could even write the HTML form out with Javascript and
provide a simple link to non-Javascript enabled browsers. But there's a problem, IE
doesn't support usernames and passwords in URLs, &lt;a href="http://www.peej.co.uk/http://support.microsoft.com/kb/834489"&gt;
they were removed due to a security scare&lt;/a&gt;, and anyway, the HTTP spec doesn't say
we're allowed to have URLs with usernames and passwords in them so we can't guarentee
that they work anywhere else either.&lt;/p&gt;
&lt;p&gt;So is there an alternative way that doesn't require us to mung the username and
password into the URL? Yes, our new friend, XMLHTTPRequest, it can submit the correct
HTTP auth headers for us. Rather than adjusting the URL the form submits to, we can
use XMLHTTPRequest to do a request before the form submits supplying the entered username
and password. This will set up the browser with the HTTP auth credentials so it'll also
send them with our actual form submission login request.&lt;/p&gt;
&lt;h3&gt;An example&lt;/h3&gt;
&lt;p&gt;Enough talking, here's some code. This is our login function that we bind to our
form submission:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre&gt;function login()
{
    var username = document.getElementById(this.id + "-username").value;
    var password = document.getElementById(this.id + "-password").value;
    this.http.open("get", this.action, false, username, password);
    this.http.send("");
    if (http.status == 200) {
        document.location = this.action;
    } else {
        alert("Incorrect username and/or password.");
    }
    return false;
}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;It sends our XHR request with the given username and password, and then redirects the
client on success or displays a Javascript alert on error. We need our standard
&lt;code&gt;getHTTPObject()&lt;/code&gt; function &lt;a href="http://www.peej.co.uk/articles/rich-user-experience.html"&gt;
that I introduced here&lt;/a&gt;, and then some code to create our login form and set everything
up:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre&gt;window.onload = function()
{
    var http = getHTTPObject();
    if (http) {
        var anchors = document.getElementsByTagName("a");
        for (var foo = 0; foo &amp;lt; anchors.length; foo++) {
            if (anchors[foo].className == "httpauth") {
                createForm(anchors[foo]);
            }
        }
    }
}

function createForm(jshttpauth)
{
    var form = document.createElement("form");
    form.action = jshttpauth.href;
    form.method = "get";
    form.onsubmit = login;
    form.id = httpauth.id;
    var username = document.createElement("label");
    var usernameInput = document.createElement("input");
    usernameInput.name = "username";
    usernameInput.type = "text";
    usernameInput.id = httpauth.id + "-username";
    username.appendChild(document.createTextNode("Username:"));
    username.appendChild(usernameInput);
    var password = document.createElement("label");
    var passwordInput = document.createElement("input");
    passwordInput.name = "password";
    passwordInput.type = "password";
    passwordInput.id = httpauth.id + "-password";
    password.appendChild(document.createTextNode("Password:"));
    password.appendChild(passwordInput);
    var submit = document.createElement("input");
    submit.type = "submit";
    submit.value = "Log in";
    form.appendChild(username);
    form.appendChild(password);
    form.appendChild(submit);
    jshttpauth.parentNode.replaceChild(form, jshttpauth);
}
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This looks for all anchors with the class of &lt;code&gt;httpauth&lt;/code&gt; and replaces it with
a nice HTML form with the &lt;code&gt;login()&lt;/code&gt; function bound to it's &lt;code&gt;onsubmit&lt;/code&gt;
event.&lt;/p&gt;
&lt;p&gt;So now if we set up a HTML page like the following, the login link will be replaced
with a login form (if the user has Javascript support) but authenticate the user using
HTTP auth:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre&gt;&amp;lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&amp;gt;
&amp;lt;html lang="en"&amp;gt;
    &amp;lt;head&amp;gt;
        &amp;lt;title&amp;gt;Javascript HTTP Auth Test&amp;lt;/title&amp;gt;
        &amp;lt;script type="text/javascript" src="http://www.peej.co.uk/jshttpauth.js"&amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;/head&amp;gt;
    &amp;lt;body&amp;gt;
        &amp;lt;a class="httpauth" id="private" href="http://www.peej.co.uk/private.php"&amp;gt;Log in&amp;lt;/a&amp;gt;
    &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If our &lt;code&gt;private.php&lt;/code&gt; looks like this, then we'll see that everything
works as we'd like:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre&gt;&amp;lt;?php

define('USER', 'user');
define('PASSWORD', 'password');

if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW'])) {
    header('WWW-Authenticate: Basic realm="My Realm"');
    header('HTTP/1.0 401 Unauthorized');
    echo 'You hit cancel, good on you.';
    
} elseif (
    isset($_SERVER['PHP_AUTH_USER']) &amp;amp;&amp;amp; $_SERVER['PHP_AUTH_USER'] == USER &amp;amp;&amp;amp;
    isset($_SERVER['PHP_AUTH_PW']) &amp;amp;&amp;amp; $_SERVER['PHP_AUTH_PW'] == PASSWORD
) {
    echo "&amp;lt;p&amp;gt;Hello {$_SERVER['PHP_AUTH_USER']}.&amp;lt;/p&amp;gt;";
    echo "&amp;lt;p&amp;gt;You entered '{$_SERVER['PHP_AUTH_PW']}' as your password.&amp;lt;/p&amp;gt;";
    
} else {
    header('HTTP/1.0 400 Bad Request');
    echo "&amp;lt;p&amp;gt;You shall not pass!&amp;lt;/p&amp;gt;";
}

?&amp;gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The thing to notice about this script is the third part of the 'if' statement. If the request
doesn't have any auth details we send a standard auth response, if it does and the details are
correct then we let them in, otherwise we send back a 400 error. This stops the clients browser
from asking the user for details again allowing the Javascript to process the failure.&lt;/p&gt;
&lt;p&gt;This also means that if the client doesn't have Javascript and fails to authenticate correctly,
it will never be asked for new credentials as long as it keeps sending the bad auth data (which
browsers will do).&lt;/p&gt;

&lt;h3&gt;Problems&lt;/h3&gt;
&lt;p&gt;It's not all good just yet, this technique works in IE6 and Firefox but is known not to work
in both Opera and Safari, so if you care about those browsers you may want to think again about
using this (or to spend some time investigating why it fails in those browsers).&lt;/p&gt;

&lt;h3&gt;HTTP Digest Auth&lt;/h3&gt;
&lt;p&gt;&lt;a href="http://www.peej.co.uk/projects/phphttpdigest.html"&gt;HTTP Digest&lt;/a&gt; is a way of authenticating a client
while never sending the clients password over the wire. To use Digest rather than Basic HTTP
Auth, we only need to adjust our PHP script to implement digest:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre&gt;&amp;lt;?php

include 'digest.php';

$HTTPDigest =&amp;amp; new HTTPDigest();
$users = array(
    'user' =&amp;gt; md5('user:'.$HTTPDigest-&amp;gt;getRealm().':password')
);

if (!$HTTPDigest-&amp;gt;getAuthHeader()) {
    $HTTPDigest-&amp;gt;send();
    echo 'You hit cancel, good on you.';
    
} elseif ($username = $HTTPDigest-&amp;gt;authenticate($users)) {
    echo "&amp;lt;p&amp;gt;Hello $username.&amp;lt;/p&amp;gt;";
    echo "&amp;lt;p&amp;gt;This resource is protected by HTTP digest.&amp;lt;/p&amp;gt;";
    
} else {
    header('HTTP/1.0 400 Bad Request');
    echo "&amp;lt;p&amp;gt;You shall not pass!&amp;lt;/p&amp;gt;";
}

?&amp;gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h3&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;So there we have it, there is no need for HTTP authentication to be shunned, even
for aesthetic reasons. HTTP auth offers lots of advantages over using cookies:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;It's simple to implement and no hassle to use, and clients love it.&lt;/li&gt;
    &lt;li&gt;It carries no baggage unlike cookies.&lt;/li&gt;
    &lt;li&gt;It's tried and tested, it's a standard and it works, your cookie based security
    model won't be as good.&lt;/li&gt;
    &lt;li&gt;We can use HTTP Digest which is pretty secure.&lt;/li&gt;
    &lt;li&gt;It's about as RESTful as you can get with authentication.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So we should be using it, there's no excuses anymore.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.peej.co.uk/sandbox/htmlhttpauth/"&gt;I have put up an example of this code as a demonstration&lt;/a&gt;.&lt;/p&gt;
&lt;div class="footnote"&gt;
    &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; I was first introduced to this idea by Christian Jensen
    and Jan Algermissen on &lt;a href="http://www.peej.co.uk/http://groups.yahoo.com/group/rest-discuss/message/5623"&gt;the
    REST Discuss mailing list&lt;/a&gt;, so credit should be sent their way.&lt;/p&gt;
    &lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; &lt;a href="http://www.peej.co.uk/http://glazkov.com/"&gt;Dimitri Glazkov&lt;/a&gt; made me revisit using HTTP Digest with
    this technique and realise that it does actually work, thanks Dimitri.&lt;/p&gt;
    &lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; Travis Estill and &lt;a href="http://www.peej.co.uk/http://david.kleinschmidt.name"&gt;David Kleinschmidt&lt;/a&gt;
    reminded me that 401 responses shouldn't be returned without an Auth header and so a 403 is a better response
    code. This also helps to make Safari behave too.&lt;/p&gt;
&lt;/div&gt;
</content>
	</entry>
		<entry>
		<title>A RESTful Web service, an example</title>
		<link rel="alternate" type="text/html" href="http://www.peej.co.uk/articles/restfully-delicious.html" />
		<id>http://www.peej.co.uk/articles/restfully-delicious.html</id>
		<published>2005-10-30T08:00:00Z</published>
		<updated>2007-12-29T12:57:52Z</updated>
		<content type="html">&lt;h2&gt;A RESTful Web service, an example&lt;/h2&gt;

&lt;p&gt;It's often hard for people to "get" REST, this is mostly due to the fact that
REST isn't a tangible thing like a piece of software or even a specification,
it's a selection of ideals, of best practices distilled from the HTTP specs.&lt;/p&gt;

&lt;p&gt;I've always found that the best way to understand something is to see an example,
to see the principles in action first and worry about the details later once I
understand the general gist. So here's a little example of a RESTful version of
a simple Web service you might already know about, &lt;a href="http://www.peej.co.uk/http://del.icio.us/doc/api"&gt;the
Delicious API&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Delicious has &lt;a href="http://www.peej.co.uk/http://del.icio.us/doc/about"&gt;&lt;q cite="http://del.icio.us/doc/about"&gt;a simple REST API&lt;/q&gt;&lt;/a&gt;,
or rather, a simple &lt;abbr title="Plain Old XML"&gt;POX&lt;/abbr&gt; over HTTP API, that
is, it has a perfectly usable HTTP and XML based API for accessing your bookmarks
and tags, but it isn't very RESTful. Why not?&lt;/p&gt;

&lt;ul&gt;
    &lt;li&gt;First class objects aren't exposed as resources, so we can't access our
    bookmarks or tags directly.&lt;/li&gt;
    &lt;li&gt;HTTP methods not used correctly, everything is done via GET even operations
    that change things.&lt;/li&gt;
    &lt;li&gt;Resource representations not interconnected, you can't traverse from a
    list of bookmarks to a single bookmark.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So (ignoring why) how can we make it better, make it more RESTful? Lets start
with describing what it is the Web service does.&lt;/p&gt;

&lt;p&gt;It allows us to:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Get a list of all our bookmarks and to filter that list by tag or date or limit by number&lt;/li&gt;
    &lt;li&gt;Get the number of bookmarks created on different dates&lt;/li&gt;
    &lt;li&gt;Get the last time we updated our bookmarks&lt;/li&gt;
    &lt;li&gt;Get a list of all our tags&lt;/li&gt;
    &lt;li&gt;Add a bookmark&lt;/li&gt;
    &lt;li&gt;Edit a bookmark&lt;/li&gt;
    &lt;li&gt;Delete a bookmark&lt;/li&gt;
    &lt;li&gt;Rename a tag&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;From this we can identify two first class objects; bookmarks and tags, so we'll
expose both of these as resources at the URL paths of &lt;code&gt;http://del.icio.us/api/[username]/bookmarks&lt;/code&gt;
and &lt;code&gt;http://del.icio.us/api/[username]/tags&lt;/code&gt; where &lt;code&gt;[username]&lt;/code&gt;
is the username of the users bookmarks we're interested in. We'll still use HTTP
Authentication to limit peoples access to bookmarks, but we've added the users
username into the URL since otherwise we'd be using HTTP Auth as part of our
resource identification instead of just the URL and that'd be unRESTful.&lt;/p&gt;

&lt;p&gt;We'll need to define some XML document formats to represent our bookmarks and
tags. It doesn't matter exactly what they look like as long as they convery the
correct information and we define mimetypes to identify them, but I'll include
examples of my versions of them as we go along to make things clearer.&lt;/p&gt;

&lt;table&gt;
    &lt;caption&gt;Table of XML document types we'll invent&lt;/caption&gt;
    &lt;tr&gt;&lt;th&gt;Mimetype&lt;/th&gt;&lt;th&gt;Description&lt;/th&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;delicious/bookmarks+xml&lt;/td&gt;&lt;td&gt;A list of bookmarks&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;delicious/bookmark+xml&lt;/td&gt;&lt;td&gt;A bookmark&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;delicious/bookmarkcount+xml&lt;/td&gt;&lt;td&gt;Count of bookmarks per date&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;delicious/update+xml&lt;/td&gt;&lt;td&gt;When the bookmarks were last updated&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;delicious/tags+xml&lt;/td&gt;&lt;td&gt;A list of tags&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;delicious/tag+xml&lt;/td&gt;&lt;td&gt;A tag&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;

&lt;h3&gt;Getting Bookmarks&lt;/h3&gt;

&lt;p&gt;In the POX Delicious API bookmarks are accessed by a RPC style request to
&lt;a href="http://www.peej.co.uk/http://del.icio.us/api/posts/get"&gt;http://del.icio.us/api/posts/get&lt;/a&gt;
with a number of optional querystring parameters that influence the returned
results. To do a similar thing RESTfully, we'll define a similar resource at
&lt;code&gt;http://del.icio.us/api/[username]/bookmarks/&lt;/code&gt; that returns a list of
bookmarks. We'll
also define an infinite number of resources at &lt;code&gt;http://del.icio.us/api/[username]/bookmarks/[hash]&lt;/code&gt;
that represent our individual bookmarks where &lt;code&gt;[hash]&lt;/code&gt; is the Delicious
hash used to identify a bookmark.&lt;/p&gt;

&lt;h4&gt;Get all bookmarks&lt;/h4&gt;
&lt;table&gt;
    &lt;tr&gt;&lt;td&gt;URL&lt;/td&gt;&lt;td colspan="2"&gt;http://del.icio.us/api/[username]/bookmarks/&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;Method&lt;/td&gt;&lt;td colspan="2"&gt;GET&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td rowspan="4"&gt;Querystring&lt;/td&gt;&lt;td&gt;tag=&lt;/td&gt;&lt;td&gt;Filter by tag&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;dt=&lt;/td&gt;&lt;td&gt;Filter by date&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;start=&lt;/td&gt;&lt;td&gt;The number of the first bookmark to return&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;end=&lt;/td&gt;&lt;td&gt;The number of the last bookmark to return&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td rowspan="3"&gt;Returns&lt;/td&gt;&lt;td colspan="2"&gt;200 OK &amp;amp; XML (delicious/bookmarks+xml)&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td colspan="2"&gt;401 Unauthorized&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td colspan="2"&gt;404 Not Found&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;The bookmarks XML should include the URLs of each individual bookmark so a
program can easily traverse to a single bookmark from the list of bookmarks. An
example &lt;code&gt;delicious/bookmarks+xml&lt;/code&gt; document might look something like:&lt;/p&gt;
&lt;div class="code"&gt;GET http://del.icio.us/api/peej/bookmarks/?start=1&amp;amp;end=2

&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;bookmarks start="1" end="2"
    next="http://del.icio.us/api/peej/bookmarks?start=3&amp;amp;amp;end=4"&amp;gt;
    &amp;lt;bookmark url="http://www.example.org/one" tags="example,test"
        href="http://www.peej.co.uk/http://del.icio.us/api/peej/bookmarks/a211528fb5108cddaa4b0d3aeccdbdcf"
        time="2005-10-21T19:07:30Z"&amp;gt;
        Example of a Delicious bookmark
    &amp;lt;/bookmark&amp;gt;
    &amp;lt;bookmark url="http://www.example.org/two" tags="example,test"
        href="http://www.peej.co.uk/http://del.icio.us/api/peej/bookmarks/e47d06a59309774edab56813438bd3ce"
        time="2005-10-21T19:34:16Z"&amp;gt;
        Another example of a Delicious bookmark
    &amp;lt;/bookmark&amp;gt;
&amp;lt;/bookmarks&amp;gt;
&lt;/div&gt;
&lt;p&gt;You'll notice the &lt;code&gt;next&lt;/code&gt; attribute of the bookmarks element, this
contains a URL to the next "page" of our returned results. Since a user might
have thousands of bookmarks and we don't want to return them all, we page the
results and provide &lt;code&gt;next&lt;/code&gt; and &lt;code&gt;previous&lt;/code&gt; attributes to
allow us to get the the next and previous pages (we have no &lt;code&gt;previous&lt;/code&gt;
attribute in this example since we are on the first page). These URLs use the &lt;code&gt;start&lt;/code&gt;
and &lt;code&gt;end&lt;/code&gt; querystring parameters to limit the results to a specific
page.&lt;/p&gt;

&lt;h4&gt;Get info on a specific bookmark&lt;/h4&gt;
&lt;table&gt;
    &lt;tr&gt;&lt;td&gt;URL&lt;/td&gt;&lt;td&gt;http://del.icio.us/api/[username]/bookmarks/[hash]&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;Method&lt;/td&gt;&lt;td&gt;GET&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td rowspan="3"&gt;Returns&lt;/td&gt;&lt;td&gt;200 OK &amp;amp; XML (delicious/bookmark+xml)&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;401 Unauthorized&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;404 Not Found&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;Since a bookmark has a number of tags, it'd be RESTful to have the bookmark
XML provide links to each tag:&lt;/p&gt;
&lt;div class="code"&gt;GET http://del.icio.us/api/peej/bookmarks/a211528fb5108cddaa4b0d3aeccdbdcf

&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;bookmark url="http://www.example.org/one" time="2005-10-21T19:07:30Z"&amp;gt;
    &amp;lt;description&amp;gt;
        Example of a Delicious bookmark
    &amp;lt;/description&amp;gt;
    &amp;lt;tags count="2"&amp;gt;
        &amp;lt;tag name="example" href="http://www.peej.co.uk/http://del.icio.us/api/peej/tags/example"/&amp;gt;
        &amp;lt;tag name="test" href="http://www.peej.co.uk/http://del.icio.us/api/peej/tags/test"/&amp;gt;
    &amp;lt;/tags&amp;gt;
&amp;lt;/bookmark&amp;gt;
&lt;/div&gt;

&lt;p&gt;Delicious provides two further retrieval "operations" on your list of bookmarks, a count
of the number of bookmarks created on each date, and the time the user last
updated their bookmarks. We'll implement these as two resources within the
bookmarks path that return this information.&lt;/p&gt;

&lt;h4&gt;Get count of posts per date&lt;/h4&gt;
&lt;table&gt;
    &lt;tr&gt;&lt;td&gt;URL&lt;/td&gt;&lt;td colspan="2"&gt;http://del.icio.us/api/[username]/bookmarks/count&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;Method&lt;/td&gt;&lt;td colspan="2"&gt;GET&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;Querystring&lt;/td&gt;&lt;td&gt;tag=&lt;/td&gt;&lt;td&gt;filter by tag&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td rowspan="3"&gt;Returns&lt;/td&gt;&lt;td colspan="2"&gt;200 OK &amp;amp; XML (delicious/bookmarkcount+xml)&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td colspan="2"&gt;401 Unauthorized&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td colspan="2"&gt;404 Not Found&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;

&lt;h4&gt;Get last update time for the user&lt;/h4&gt;
&lt;table&gt;
    &lt;tr&gt;&lt;td&gt;URL&lt;/td&gt;&lt;td&gt;http://del.icio.us/api/[username]/bookmarks/update&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;Method&lt;/td&gt;&lt;td colspan="2"&gt;GET&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td rowspan="3"&gt;Returns&lt;/td&gt;&lt;td colspan="2"&gt;200 OK &amp;amp; XML (delicious/update+xml)&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td colspan="2"&gt;401 Unauthorized&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td colspan="2"&gt;404 Not Found&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;

&lt;h3&gt;Modifying Bookmarks&lt;/h3&gt;

&lt;p&gt;In the original API, you add a new bookmark by issuing a RPC style GET request
to an add URL. Not very RESTful. Modifying is done by using the same add URL, and
deleting is done in the same way but via a delete URL. We'll be more RESTful by
using the correct HTTP methods on existing bookmark resources to add, modify and
delete bookmarks.&lt;/p&gt;

&lt;h4&gt;Add a bookmark to Delicious&lt;/h4&gt;
&lt;p&gt;To create a bookmark we'll POST an XML representation of a bookmark to our
bookmarks URL.&lt;/p&gt;
&lt;table&gt;
    &lt;tr&gt;&lt;td&gt;URL&lt;/td&gt;&lt;td&gt;http://del.icio.us/api/[username]/bookmarks/&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;Method&lt;/td&gt;&lt;td&gt;POST&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;Request Body&lt;/td&gt;&lt;td&gt;XML (delicious/bookmark+xml)&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td rowspan="3"&gt;Returns&lt;/td&gt;&lt;td&gt;201 Created &amp;amp; Location&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;401 Unauthorized&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;415 Unsupported Media Type&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;Since to create a new bookmark Delicious doesn't need all of the information
that we've added to our bookmark XML format, we'll allow some of the XML attributes
and nodes to be optional:&lt;/p&gt;
&lt;div class="code"&gt;POST http://del.icio.us/api/peej/bookmarks/

&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;bookmark url="http://www.example.org/one" time="2005-10-21T19:07:30Z"&amp;gt;
    &amp;lt;description&amp;gt;
        Example of a Delicious bookmark
    &amp;lt;/description&amp;gt;
    &amp;lt;tags&amp;gt;
        &amp;lt;tag name="example"/&amp;gt;
        &amp;lt;tag name="test"/&amp;gt;
    &amp;lt;/tags&amp;gt;
&amp;lt;/bookmark&amp;gt;
&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;415 Unsupported Media Type&lt;/code&gt; error is returned if an incorrect
XML document is sent.&lt;/p&gt;
&lt;p&gt;On success, a &lt;code&gt;201 Created&lt;/code&gt; response is returned along with a location header
containing the URL of the newly created bookmark resource.&lt;/p&gt;

&lt;h4&gt;Modify a bookmark&lt;/h4&gt;
&lt;p&gt;To modify an existing bookmark, we'll PUT an XML representation of a bookmark
to the URL of our bookmark.&lt;/p&gt;
&lt;table&gt;
    &lt;tr&gt;&lt;td&gt;URL&lt;/td&gt;&lt;td&gt;http://del.icio.us/api/[username]/bookmarks/[hash]&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;Method&lt;/td&gt;&lt;td&gt;PUT&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;Request Body&lt;/td&gt;&lt;td&gt;XML (delicious/bookmark+xml)&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td rowspan="4"&gt;Returns&lt;/td&gt;&lt;td&gt;201 Created &amp;amp; Location&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;401 Unauthorized&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;404 Not Found&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;415 Unsupported Media Type&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;Since we don't want people to create new bookmarks using PUT, only update
existing bookmarks, we'll return a &lt;code&gt;404 Not Found&lt;/code&gt; error if the
resource doesn't already exist.&lt;/p&gt;

&lt;h4&gt;Delete a bookmark from Delicious&lt;/h4&gt;
&lt;p&gt;To delete a bookmark we'll use the HTTP DELETE method.&lt;/p&gt;
&lt;table&gt;
    &lt;tr&gt;&lt;td&gt;URL&lt;/td&gt;&lt;td&gt;http://del.icio.us/api/[username]/bookmarks/[hash]&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;Method&lt;/td&gt;&lt;td&gt;DELETE&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td rowspan="3"&gt;Returns&lt;/td&gt;&lt;td&gt;204 No Content&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;401 Unauthorized&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;404 Not Found&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;On success, we'll return a &lt;code&gt;204 No Content&lt;/code&gt; code since we won't
return an response body.&lt;/p&gt;

&lt;h3&gt;Tags&lt;/h3&gt;
&lt;p&gt;The last part of the Delicious API deals with the users tags, it allows us to
get tags and change the names of them.&lt;/p&gt;

&lt;h4&gt;Get all tags&lt;/h4&gt;
&lt;table&gt;
    &lt;tr&gt;&lt;td&gt;URL&lt;/td&gt;&lt;td colspan="2"&gt;http://del.icio.us/api/[username]/tags/&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;Method&lt;/td&gt;&lt;td colspan="2"&gt;GET&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td rowspan="2"&gt;Querystring&lt;/td&gt;&lt;td&gt;start=&lt;/td&gt;&lt;td&gt;The number of the first tag to return&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;end=&lt;/td&gt;&lt;td&gt;The number of the last tag to return&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td rowspan="3"&gt;Returns&lt;/td&gt;&lt;td colspan="2"&gt;200 OK &amp;amp; XML (delicious/tags+xml)&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td colspan="2"&gt;401 Unauthorized&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td colspan="2"&gt;404 Not Found&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;Like the bookmarks XML, the tags XML should include the URLs of each individual
tag:&lt;/p&gt;
&lt;div class="code"&gt;GET http://del.icio.us/api/peej/tags/?start=1&amp;amp;end=2

&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;tags start="1" end="2" next="http://del.icio.us/api/peej/tags?start=3&amp;amp;amp;end=4"&amp;gt;
    &amp;lt;tag name="example" count="2" href="http://www.peej.co.uk/http://del.icio.us/api/peej/tags/example"/&amp;gt;
    &amp;lt;tag name="test" count="2" href="http://www.peej.co.uk/http://del.icio.us/api/peej/tags/test"/&amp;gt;
&amp;lt;/tags&amp;gt;
&lt;/div&gt;

&lt;h4&gt;Get info on a specific tag&lt;/h4&gt;
&lt;table&gt;
    &lt;tr&gt;&lt;td&gt;URL&lt;/td&gt;&lt;td colspan="2"&gt;http://del.icio.us/api/[username]/tags/[name]&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;Method&lt;/td&gt;&lt;td colspan="2"&gt;GET&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td rowspan="2"&gt;Querystring&lt;/td&gt;&lt;td&gt;start=&lt;/td&gt;&lt;td&gt;The number of the first bookmark for this tag to return&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;end=&lt;/td&gt;&lt;td&gt;The number of the last bookmark for this tag to return&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td rowspan="3"&gt;Returns&lt;/td&gt;&lt;td colspan="2"&gt;200 OK &amp;amp; XML (delicious/tag+xml)&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td colspan="2"&gt;401 Unauthorized&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td colspan="2"&gt;404 Not Found&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;Since a tag has a number of bookmarks, it'd be RESTful to have the tag
XML provide links to each bookmark:&lt;/p&gt;
&lt;div class="code"&gt;GET http://del.icio.us/api/peej/tags/example?start=1&amp;amp;end=2

&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;tag name="example"&amp;gt;
    &amp;lt;bookmarks start="1" end="2"
        next="http://del.icio.us/api/peej/tags/example?start=3&amp;amp;amp;end=4"&amp;gt;
        &amp;lt;bookmark url="http://www.example.org/one"
        href="http://www.peej.co.uk/http://del.icio.us/api/peej/bookmarks/a211528fb5108cddaa4b0d3aeccdbdcf"/&amp;gt;
        &amp;lt;bookmark url="http://www.example.org/two"
        href="http://www.peej.co.uk/http://del.icio.us/api/peej/bookmarks/e47d06a59309774edab56813438bd3ce"/&amp;gt;
    &amp;lt;/tags&amp;gt;
&amp;lt;/tag&amp;gt;
&lt;/div&gt;

&lt;h4&gt;Rename a tag&lt;/h4&gt;
&lt;p&gt;To rename a tag we'll use the PUT method of a tag to upload a new representation
of the tag. The problem with this is that the name of the tag is part of the tags
URL, so changing its name also changes its URL, so we return a location header of
the resources new URL.&lt;/p&gt;
&lt;table&gt;
    &lt;tr&gt;&lt;td&gt;URL&lt;/td&gt;&lt;td&gt;http://del.icio.us/api/[username]/tags/[name]&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;Method&lt;/td&gt;&lt;td&gt;PUT&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;Request Body&lt;/td&gt;&lt;td&gt;XML (delicious/tag+xml)&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td rowspan="3"&gt;Returns&lt;/td&gt;&lt;td&gt;204 No Content &amp;amp; Location&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;401 Unauthorized&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;404 Not Found&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;

&lt;h3&gt;Finishing the API&lt;/h3&gt;

&lt;p&gt;Okay, so now that we've defined our new RESTful API, how do we use it? Well,
REST says to "use hypertext as the engine of application-state transitions",
meaning, given a starting URL, we should be able to fetch a resource and use the
URLs within it to navigate to the other resources that we want. We shouldn't have
to guess or construct any URLs ourselves since URLs are transparent and don't have
to follow any particular formula or pattern (and since software can't easily spot
patterns and construct URLs by itself).&lt;/p&gt;

&lt;p&gt;So we're missing a homepage for our API, let's create one. This will be the
entry point for our RESTful API, by giving this URL to a program that knows about
our Delicious XML data formats, it can find all the data it requires without
having to know anything about the rest of the API.&lt;/p&gt;
&lt;table&gt;
    &lt;tr&gt;&lt;td&gt;URL&lt;/td&gt;&lt;td colspan="2"&gt;http://del.icio.us/api/&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;Method&lt;/td&gt;&lt;td colspan="2"&gt;GET&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td rowspan="2"&gt;Querystring&lt;/td&gt;&lt;td&gt;start=&lt;/td&gt;&lt;td&gt;The number of the first user to return&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;end=&lt;/td&gt;&lt;td&gt;The number of the last user to return&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;Returns&lt;/td&gt;&lt;td colspan="2"&gt;200 OK&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;div class="code"&gt;GET http://del.icio.us/api/?start=1&amp;amp;end=2

&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;users start="1" end="2" next="http://del.icio.us/api/?start=3&amp;amp;amp;end=4"&amp;gt;
    &amp;lt;user name="Peej"
        href="http://www.peej.co.uk/http://del.icio.us/api/peej/"
        bookmarks="http://del.icio.us/api/peej/bookmarks/"
        tags="http://del.icio.us/api/peej/tags"/&amp;gt;
    &amp;lt;user name="Joshua"
        href="http://www.peej.co.uk/http://del.icio.us/api/joshua/"
        bookmarks="http://del.icio.us/api/joshua/bookmarks/"
        tags="http://del.icio.us/api/joshua/tags"/&amp;gt;
&amp;lt;/users&amp;gt;
&lt;/div&gt;

&lt;p&gt;Each user then needs to be defined as a resource, we failed to spot the user
as a first class object when we started, so we'll add it now.&lt;/p&gt;

&lt;table&gt;
    &lt;tr&gt;&lt;td&gt;URL&lt;/td&gt;&lt;td colspan="2"&gt;http://del.icio.us/api/[username]/&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;Method&lt;/td&gt;&lt;td colspan="2"&gt;GET&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td rowspan="2"&gt;Querystring&lt;/td&gt;&lt;td&gt;start=&lt;/td&gt;&lt;td&gt;The number of the first bookmark to return&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;end=&lt;/td&gt;&lt;td&gt;The number of the last bookmark to return&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td rowspan="3"&gt;Returns&lt;/td&gt;&lt;td colspan="2"&gt;200 OK&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td colspan="2"&gt;401 Unauthorized&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td colspan="2"&gt;404 Not Found&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;div class="code"&gt;GET http://del.icio.us/api/peej/?start=1&amp;amp;end=2

&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;user email="paul@peej.co.uk" name="Paul James" href="http://www.peej.co.uk/http://www.peej.co.uk/"&amp;gt;
    &amp;lt;bookmarks href="http://www.peej.co.uk/http://del.icio.us/api/peej/bookmarks/" start="1" end="2"
        next="http://del.icio.us/api/peej/?start=3&amp;amp;amp;end=4"&amp;gt;
        &amp;lt;bookmark url="http://www.example.org/one"
        href="http://www.peej.co.uk/http://del.icio.us/api/peej/bookmarks/a211528fb5108cddaa4b0d3aeccdbdcf"/&amp;gt;
        &amp;lt;bookmark url="http://www.example.org/two"
        href="http://www.peej.co.uk/http://del.icio.us/api/peej/bookmarks/e47d06a59309774edab56813438bd3ce"/&amp;gt;
    &amp;lt;/bookmarks&amp;gt;
    &amp;lt;tags href="http://www.peej.co.uk/http://del.icio.us/api/peej/tags/"&amp;gt;
        &amp;lt;tag name="example" count="2" href="http://www.peej.co.uk/http://del.icio.us/api/peej/tags/example"/&amp;gt;
        &amp;lt;tag name="test" count="2" href="http://www.peej.co.uk/http://del.icio.us/api/peej/tags/test"/&amp;gt;
    &amp;lt;/tags&amp;gt;
&amp;lt;/user&amp;gt;
&lt;/div&gt;

&lt;p&gt;The user resource could have a PUT method to allow updating of users settings
and a DELETE method to remove the user and all their bookmarks and tags.&lt;/p&gt;

&lt;h3&gt;An example of using the API&lt;/h3&gt;

&lt;p&gt;Let's finish this example RESTful API with an example of a client program
using it.&lt;/p&gt;

&lt;p&gt;I've been messing around with Delicious and have created a number of bookmarks
for testing purposes. I don't want them anymore, so our pretend program is going
to delete them for me. I tagged them all up with the tag "test" so I'd be able
to find them later, I'll use this tag to track them down and remove them.&lt;/p&gt;

&lt;ol&gt;
    &lt;li&gt;
        &lt;h4&gt;GET http://del.icio.us/api/&lt;/h4&gt;
        &lt;p&gt;First, we start like every client, by fetching the APIs homepage. We
        parse the response XML and find the tags URL of the user with the name
        "Peej". This gives us http://del.icio.us/api/peej/tags/.&lt;/p&gt;
    &lt;/li&gt;
    &lt;li&gt;
        &lt;h4&gt;GET http://del.icio.us/api/peej/tags/&lt;/h4&gt;
        &lt;p&gt;So we get the resource that represents my tags. We parse the XML and
        see if the tag "test" is mentioned. If it isn't, then we GET the URL in
        the "next" attribute and try again. Eventually we'll get the URL
        http://del.icio.us/api/peej/tags/test&lt;/p&gt;
    &lt;/li&gt;
    &lt;li&gt;
        &lt;h4&gt;GET http://del.icio.us/api/peej/tags/test&lt;/h4&gt;
        &lt;p&gt;Now we've got the details for the tag "test", so we parse the XML and
        get a list of bookmarks. If there is a "next" attribute, we'll follow that
        to get the next page of bookmarks. Eventually we'll have a list of all
        bookmarks tagged with "test".&lt;/p&gt;
    &lt;/li&gt;
    &lt;li&gt;
        &lt;h4&gt;DELETE http://del.icio.us/api/peej/bookmarks/[hash]&lt;/h4&gt;
        &lt;p&gt;Finally we can walk through our list of bookmarks and call the DELETE
        HTTP method on each of their URLs.&lt;/p&gt;
    &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now this is quite involved and we could take shortcuts by assuming that we know
the URL of the tag we want is &lt;code&gt;http://del.icio.us/api/peej/tags/test&lt;/code&gt;.
What it does show is that with a RESTful API it is possible for a piece of
software to use the Web in a similar way that a human does to perform a task,
there's no need for description languages like WSDL, REST is self descriptive.&lt;/p&gt;

&lt;h3&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;Most "REST" APIs around today are not very RESTful, most are as RESTful as
equivalent SOAP or XML-RPC services. If you are using HTTP you are being RESTful
to some degree since HTTP is a REST protocol, but to take full advantage of the
platform, APIs should embrace RESTful practices as much as possible.&lt;/p&gt;

&lt;p&gt;I hope this article has been intersting, and I hope your next Web software
product will embrace REST principles for its machine and human interfaces.&lt;/p&gt;
</content>
	</entry>
		<entry>
		<title>Content Negotiation</title>
		<link rel="alternate" type="text/html" href="http://www.peej.co.uk/articles/conneg.html" />
		<id>http://www.peej.co.uk/articles/conneg.html</id>
		<published>2005-07-23T07:00:00Z</published>
		<updated>2007-11-03T08:46:44Z</updated>
		<content type="html">&lt;h2&gt;Content Negotiation&lt;/h2&gt;

&lt;p&gt;&lt;a href="http://www.peej.co.uk/http://gewis.win.tue.nl/%7Ekoen/conneg/"&gt;Content negotiation&lt;/a&gt;
is something I've been looking at lately as part of the development of
my RESTful Web management system I'm working on in my spare time. The
idea is as old as the Web itself and is simple to understand, for a
single resource you have multiple representations in different data
formats and which one we send depends on the accept headers sent by the
client.&lt;/p&gt;

&lt;p&gt;We all know it's good practice to trim file extensions from your
URLs so that you are not tied to a particular data format. Content
negotiation says that a URL like &lt;code&gt;http://www.example.org/example&lt;/code&gt; would be translated by &lt;abbr title="content negotiation"&gt;conneg&lt;/abbr&gt; to &lt;code&gt;http://www.example.org/example.html&lt;/code&gt; if the client wants HTML and the file &lt;code&gt;example.html&lt;/code&gt; exists.&lt;/p&gt;

&lt;p&gt;By translated, I don't mean changed, this all happens transparently
to the client. Also, an interesting affect is that now we have multiple
URLs for the same resource. We have a single URL that will in effect
alias the URL that best suits the client, and a number of URLs for each
data format.&lt;/p&gt;

&lt;p&gt;I came across this article on &lt;a href="http://www.peej.co.uk/http://msdn.microsoft.com/msdnmag/default.aspx"&gt;the MSDN Magazine site&lt;/a&gt;, the first article in the "inside MSDN" column about the new MSDN site, &lt;a href="http://www.peej.co.uk/http://msdn.microsoft.com/msdnmag/issues/05/02/InsideMSDN/default.aspx"&gt;Designing URLs for MSDN2&lt;/a&gt;.
Someone's going to fix the mess that is MSDN I thought, excellent,
maybe someone will tidy up those URLs and remove all those .aspx
extensions.&lt;/p&gt;

&lt;blockquote cite="http://msdn.microsoft.com/msdnmag/issues/05/02/InsideMSDN/default.aspx#S3"&gt;
&lt;p&gt;Some people wonder why we bother with the .aspx extension, as it
ties us to a particular technology. Others wonder why we aren't using
ASP.NET when we tell them they should (we are using ASP.NET, but
without the .aspx it might appear as though we're not). To make both
groups happy, we chose to use the .aspx extension, but you can leave it
off if you feel like it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So sod having the ability to deliver content in the format best
suited to clients, and lets use our URLs to advertise our server
technology. And while we're at it lets not use language negotiation to
deliver the correct language for the clients locale, but instead use a
made up bracket notation which we'll append to the URL.&lt;/p&gt;

&lt;p&gt;Conneg is great, for example, you can have one URL for your homepage
and deliver HTML to Web browsers and RSS to feed readers, all totally
transparently to the clients. People should be using this stuff, just a
shame that a high tech. company like Microsoft isn't leading the way.&lt;/p&gt;

&lt;p&gt;With &lt;a href="http://www.peej.co.uk/http://groups.yahoo.com/group/rest-discuss/message/5138"&gt;a
recent thread&lt;/a&gt; on &lt;a href="http://www.peej.co.uk/http://groups.yahoo.com/group/rest-discuss"&gt;the
REST Discuss mailing list&lt;/a&gt; I thought it'd be useful to recap the issues
it raised.&lt;/p&gt;

&lt;p&gt;The idea behind content negotiation is that a resource may have multiple
representations in different formats, so a client can recieve the best
representation for it's abilities. So for example requesting http://example.org/theNews
could return a HTML document for a HTML browser and a PDF document for a PDF
viewer.&lt;/p&gt;

&lt;p&gt;But what happens when I want to PUT a new HTML version of my resource,
one that fixes a mistake in my markup, if I PUT to http://example.org/theNews
then how does my server know which representation to alter? Am I replacing
both representations with a single new resource, or just updating one? That's
why one of the golden rules of REST &lt;a href="http://www.peej.co.uk/http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm#sec_5_2_1_1"&gt;as
mentioned in Roy's thesis&lt;/a&gt; is:&lt;/p&gt;

&lt;blockquote cite="http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm#sec_5_2_1_1"&gt;
&lt;p&gt;The key abstraction of information in REST is a resource. Any information
that can be named can be a resource: a document or image, a temporal service
(e.g. "today's weather in Los Angeles"), a collection of other resources, a
non-virtual object (e.g. a person), and so on. In other words, any concept
that might be the target of an author's hypertext reference must fit within
the definition of a resource. A resource is a conceptual mapping to a set
of entities, not the entity that corresponds to the mapping at any particular
point in time.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So for our example URL of http://example.org/theNews that returns either
a HTML or a PDF document, we also have two other resources, one for the HTML
document and one for the PDF document. By convention (and because it makes
&lt;abbr title="content negotiation"&gt;conneg&lt;/abbr&gt; easier) we'll reference these
at http://example.org/theNews.html and http://example.org/theNews.pdf
respectively.&lt;/p&gt;

&lt;p&gt;In this way, we get the power of being able to say to a client, "go to
this URL and there'll be something useful you can consume", but we can also
do our CRUD operations to manage each individual representation.&lt;/p&gt;

&lt;p&gt;A lot of this is academic in the real world, content negotiation is one
of those things which will rarely be used, however it does a good job at
showing why the golden rule of "everything should be a resource" is important.
If we might want to be able to access it, edit it, delete it, make it a
resource, &lt;a href="http://www.peej.co.uk/http://lists.w3.org/Archives/Public/www-archive/2002Sep/0152.html"&gt;URLs
are cheap&lt;/a&gt;.&lt;/p&gt;
</content>
	</entry>
		<entry>
		<title>XML is not a Data Transfer Format</title>
		<link rel="alternate" type="text/html" href="http://www.peej.co.uk/articles/xml-data-transfer-format.html" />
		<id>http://www.peej.co.uk/articles/xml-data-transfer-format.html</id>
		<published>2005-06-18T07:00:00Z</published>
		<updated>2007-11-03T09:00:12Z</updated>
		<content type="html">&lt;h2&gt;XML is not a Data Transfer Format&lt;/h2&gt;
    
&lt;p&gt;A project I've been having a small part of at work involves a Flash
element talking to a bunch of heavy chunking PHP scripts thrashing a
MySQL data to within an inch of it's life. The Flash comes back to the
server every now and then asking for new data for that particular user
(and sends some data back too).&lt;/p&gt;

&lt;p&gt;It was decided without any thought as to why by the Flash team to
format this data as XML. The reason being that they already had
libraries for parsing XML into native ActionScript variables.&lt;/p&gt;

&lt;p&gt;The problem with this is of course that XML is a markup language for
formatting documents and adding meaning to data, not a data transfer or
serialisation format. It's just too verbose for doing that, especially
when you consider that this campaign is going out to a large userbase
and every ounce of server power and bandwidth is precious.&lt;/p&gt;

&lt;p&gt;XML is useful if the data you are transfering is actually a document
and needs marking up like you would a document, but if it's not, if
you're just doing data transfer then there ar far better formats which
are just as easy to parse (&lt;a href="http://www.peej.co.uk/http://www.json.org/"&gt;JSON&lt;/a&gt;
for one rings a bell). In our case, the data sent to the Flash is
always the same, so a simple string of characters pushed over HTTP
would have worked great, have made data parsing and generation fast,
and have cut the bandwidth used by a factor of 5 at least. When you're
talking about half a million requests an hour, cutting the
client/server bandwidth by 5 is a big deal.&lt;/p&gt;

&lt;p&gt;The moral of this story is, use the correct encoding format for the
correct situation. XML is great, but if all you have is a hammer
everything looks like a nail. Another reason for REST/HTTP's success,
content type neutrality.&lt;/p&gt;
</content>
	</entry>
		<entry>
		<title>3 Tiered REST Architecture</title>
		<link rel="alternate" type="text/html" href="http://www.peej.co.uk/articles/3-tiered-rest-architecture.html" />
		<id>http://www.peej.co.uk/articles/3-tiered-rest-architecture.html</id>
		<published>2005-03-01T08:00:00Z</published>
		<updated>2007-11-03T08:46:28Z</updated>
		<content type="html">&lt;h2&gt;3 Tiered REST Architecture&lt;/h2&gt;

&lt;p&gt;I've been working on RESTifying my website management software a little
since I want to learn a little bit about XUL and thought writing a XUL
interface to manage my site would be a fun project. A clean RESTful
HTTP interface is a great way to allow a client side application to
talk to the server.&lt;/p&gt;

&lt;p&gt;I quickly ran into a problem however. My software already placed
resources at the center of the system, but as well as just
storing the resource content data it also stores some metadata about
that resource. So my problem was that sometimes I want to fetch the
resource data and other times it's metadata.&lt;/p&gt;

&lt;h3&gt;A multitude of solutions&lt;/h3&gt;

&lt;p&gt;As with all good architectures, there are a number of ways of doing
the same thing. I couldn't decide on which was the best, there are
multiple ways of asking for the metadata rather than the resource
content, and multiple ways of delivering it.&lt;/p&gt;

&lt;p&gt;Requesting metadata&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Use a HTTP HEAD request.&lt;/li&gt;
    &lt;li&gt;Use a new HTTP method (META for example).&lt;/li&gt;
    &lt;li&gt;Use a querystring value.&lt;/li&gt;
    &lt;li&gt;Use a custom request header.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Metadata response&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;In the response body.&lt;/li&gt;
    &lt;li&gt;As custom HTTP response headers.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So the question was, which of these was the best, most meaningful, more RESTful solution?&lt;/p&gt;

&lt;p&gt;I liked the idea of sending a HTTP HEAD request and getting custom
headers back with the meta data. HTTP headers make perfect sense for
metadata, since they are name/value pairs too, and HEAD makes sense
since we're asking for header information only and we don't want the
overhead of sending all the metadata with every HTTP response.&lt;/p&gt;

&lt;p&gt;I also liked the idea of creating a new HTTP method for getting back
the metadata. This would allow the metadata to be sent in the response
body thus opening up content negotiation so we could return the
metadata in multiple formats if required.&lt;/p&gt;

&lt;h3&gt;The solution&lt;/h3&gt;

&lt;p&gt;The solution I decided upon... none of the above.&lt;/p&gt;

&lt;p&gt;It suddenly dawned on me that I wasn't thinking in a RESTful way.
It's very easy to fall back into a procedual way of thinking when what
you really need to do is take a step back and re-factor things into
more resources. So that's what I did.&lt;/p&gt;

&lt;p&gt;On retrospect it's obvious that metadata for a resource is another
resource entirely, it just so happens to relate to an existing
resource. With that change implemented I suddenly have HTTP GET and PUT
methods for recieving and updating resource metadata, as well as POST
for appending a single piece of metadata and DELETE for removing all
metadata.&lt;/p&gt;

&lt;p&gt;So the moral of this story, think RESTfully, at all times. Hit a
problem, backtrack and think about it from a resource/URL perspective.
HTTP is simple, it doesn't do very much, but that's its power, don't
try to shoehorn complex ideas onto HTTP requests, break them down into
simpler parts that &lt;em&gt;can&lt;/em&gt; be modelled using REST and HTTP, and you'll save yourself a lot of pain and come out of it with a more elegant RESTful system.&lt;/p&gt;

&lt;h3&gt;The 3 tiered REST architecture&lt;/h3&gt;

&lt;p&gt;Today on the list was &lt;a href="http://www.peej.co.uk/http://groups.yahoo.com/group/rest-discuss/message/4921"&gt;a post by Donald Strong&lt;/a&gt;
that describes something that I've thought for a while but never
coherently enough to put into words. There are two types of transaction
within a REST system.&lt;/p&gt;

&lt;p&gt;The first uses GET and POST(p) in what Donald describes as an
untrusted transaction, the kind of transaction carried out everyday by
the Web browser. The client has no direct access to manipulate
resources but POSTs to a URL that processes the input and changes
resource states as needed. The client is dumb while the server is
intelligent, making sure that resources are always left in a valid
state.&lt;/p&gt;

&lt;p&gt;The second transaction type is trusted, using GET, PUT, POST(a)
and DELETE, the types of transactions that Web services and rich
clients want to perform. The client has total control over the servers
resources in rather the same way as a SQL database client does. In this
case the server is dumb with reguard to the resources and the client is
intelligent.&lt;/p&gt;

&lt;p&gt;Read &lt;a href="http://www.peej.co.uk/http://groups.yahoo.com/group/rest-discuss/message/4921"&gt;Donalds post&lt;/a&gt; for more in depth information and a better explaination (complete with ASCII art).&lt;/p&gt;

&lt;p&gt;What Donald then goes on to say is that if you use these two types
of transaction together you end up with your classic 3 tier software
architecture. The dumb client acts as the presentation layer talking to
a server via GET and POST(p). This server is the application or
business layer, doing all the heavy lifting and manipulating state by
talking via GET, PUT, POST(a) and DELETE to another server, the data
layer.&lt;/p&gt;

&lt;img src="http://www.peej.co.uk/gfx/rest-web-application-arch.png" alt="REST Web Application Architecture diagram" height="543" width="600" class="right"&gt;

&lt;p&gt;Now this is very clever, but when I think about it, it is what I've
been thinking all along. Kind of. My theory has always been that there
is no harm using POST(p) on a "do some process" URL to carry out a PUT,
POST(a) or DELETE on the Web browsers behalf. It's the structure that
&lt;a href="http://www.peej.co.uk/http://tonic.sourceforge.net/"&gt;Tonic&lt;/a&gt; uses, all resources
on the server can be manipulated by the four HTTP methods, but they can
also be changed via GETting a HTML form from a "process this" URL and
then POSTing data to it. All this URL does is manipulate the sites
resources (files in this case) using the standard four methods (although
not via HTTP since it's on the same server).&lt;/p&gt;

&lt;p&gt;In this setup, the "process" URLs HTML representations are the user
interface, the "process" URLs processing code is the business logic,
and the resources we're manipulating are our data. This makes it very
easy for us to bolt a new frontend onto our system in place of our HTML
(for example a XUL interface, a shell script, or a GUI application) by
automatically giving us a nice RESTful interface.&lt;/p&gt;
</content>
	</entry>
		<entry>
		<title>What are Web Services?</title>
		<link rel="alternate" type="text/html" href="http://www.peej.co.uk/articles/web-services.html" />
		<id>http://www.peej.co.uk/articles/web-services.html</id>
		<published>2005-02-22T08:00:00Z</published>
		<updated>2007-11-03T08:59:35Z</updated>
		<content type="html">&lt;h2&gt;What are Web Services?&lt;/h2&gt;

&lt;p&gt;&lt;a href="http://www.peej.co.uk/http://www.krasama.com/"&gt;Justin Sampson&lt;/a&gt; posted &lt;a href="http://www.peej.co.uk/http://groups.yahoo.com/group/rest-discuss/message/4870"&gt;a great reply&lt;/a&gt; to a question on the &lt;a href="http://www.peej.co.uk/http://groups.yahoo.com/group/rest-discuss"&gt;REST Discuss mailing list&lt;/a&gt; this evening, here's a quote:&lt;/p&gt;

&lt;blockquote cite="http://groups.yahoo.com/group/rest-discuss/message/4870"&gt;
&lt;p&gt;The fundamental insight I take from REST as it relates to Web
services is that well-behaved Web services should feel a lot like
well-behaved Web sites. A Web service is just a machine-readable Web
application, and a Web application is just a dynamic Web site. The
principles of the Web architecture that made it so successful apply to
all three.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Stop trying to build RESTful Web services and starting just doing
things the way you've been doing them for years, if it ain't broke,
don't fix it.&lt;/p&gt;

&lt;h3&gt;What REST is Not&lt;/h3&gt;
    
&lt;p&gt;With all the coverage REST is getting, lots of systems are being touted as
RESTful, so I thought it was about time I wrote not about what REST is,
but what REST is not.&lt;/p&gt;

&lt;h4&gt;A messaging system&lt;/h4&gt;
&lt;p&gt;REST is not a protocol, it is an architectural style, it is a way of
transfering representations of resources between two points. A resource
can be anything, an application object, a database record, etc. and a
representation is a way of expressing that resource in a useful way,
whether that be as a HTML file, an XML document, an image, a comma
separated value, etc.&lt;/p&gt;
&lt;p&gt;A messaging system is a way of passing messages between entities, SOAP is a messaging system. Messaging systems, &lt;abbr title="remote procedure call"&gt;RPC&lt;/abbr&gt;

systems, are useful, they allow our applications to talk to each other
over the network by sending short messages between one another. The
problem with messaging systems is that they work fine when you control
both ends of the equation but when it comes to scaling them up to the
size of the Internet, where anyone can send a message to anyone else,
they quickly fall apart.&lt;/p&gt;
&lt;p&gt;HTTP may look like a messaging system at first glance, but it's not,
it's a representation transfer system, no messages are ever sent back
and forth, only requests and responses as representations of the
resources modelled on the server.&lt;/p&gt;

&lt;h4&gt;XML over HTTP&lt;/h4&gt;
&lt;p&gt;Just because you are sending XML over HTTP does not mean your Web
service is RESTful, a lot of Web services that claim to be REST
services are nothing more than XML over HTTP. To be RESTful requires
that you embrace "the cult of REST". This includes:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Everything is a resource and every resource has a URL. If you need to manipulate it, it needs to be a resource.&lt;/li&gt;
    &lt;li&gt;Resources can be manipulated by the four CRUD methods GET, PUT, POST and DELETE.&lt;/li&gt;
    &lt;li&gt;Resources can have multiple representations in different formats,
    but they must all represent the same data, the data that is the
    resource.&lt;/li&gt;
    &lt;li&gt;Resources should link together to allow auto-discovery and provide
    context. Just because it's not HTML being sent doesn't mean you forget
    everything that's good about the Web, a Web service is just a machine
    readable Web site.&lt;/li&gt;
    &lt;li&gt;URLs should encode the nature of the resource and indicate any
    heirarchy between resources. Querystrings should be used for queries,
    not for returning different resources.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you don't cover all these points then you're not being RESTful,
you're just delivering XML over HTTP (which is fair enough, just don't
claim to be RESTful).&lt;/p&gt;

&lt;h4&gt;Tidy URLs&lt;/h4&gt;
&lt;p&gt;URLs without large querystrings has been a trendy thing in Web
circles for the last 4 or so years and it is one of the things REST
says is "a good thing". But when it comes to REST it isn't just about
being neat and tidy, the poritions of a URL have a meaning, it's not a
random string of characters that happens to point to a file on your Web
server.&lt;/p&gt;
&lt;p&gt;URLs do not point to files on Web servers, they point to resources.
The querystring portion of the URL is used to pass in query parameters
to a URL, so if you have an index file listing a number of other
resources a querystring can be used to limit the URLs returned in the
listing (kind of like a WHERE clause in SQL). Just because your URLs
are tidy, doesn't mean you're being RESTful.&lt;/p&gt;
</content>
	</entry>
		<entry>
		<title>REST Data Format Confusion</title>
		<link rel="alternate" type="text/html" href="http://www.peej.co.uk/articles/rest-data-formats.html" />
		<id>http://www.peej.co.uk/articles/rest-data-formats.html</id>
		<published>2005-01-15T08:00:00Z</published>
		<updated>2007-11-03T08:58:03Z</updated>
		<content type="html">&lt;h2&gt;REST Data Format Confusion&lt;/h2&gt;

&lt;p&gt;One of the problems people have with the REST architectural style is its
lack of definition of a common data format, but that is actually one of
the powerful features of REST based systems.&lt;/p&gt;

&lt;p&gt;It's very attractive to programmers to have a single all in one
protocol and encoding format, programmers like to think in terms of
functions and parameters which is what you get with XML-RPC and SOAP.
They have their own way of representing common data formats in XML,
meaning you can pass values back and forth without having to write or
decode any XML, the libraries do it all for you.&lt;/p&gt;

&lt;p&gt;The problem comes when you want to pass something more than a simple
data value back and forth. If you define a data format like XML-RPC or
SOAP do, then you'd better make sure it covers all posibilities. How
for example do I pass a binary file back from an XML-RPC request? I
have to encode somehow into the XML-RPC XML response. Fun.&lt;/p&gt;

&lt;p&gt;REST doesn't work that way, which is alien to most programmers. It
works with URLs and representations, so rather than calling a function
with a set of parameters, you're fetching the state of a resource.
Working with REST requires you to write applications in a RESTful way,
rather than just bolting HTTP based RPC onto a procedural way of
thinking.&lt;/p&gt;

&lt;p&gt;With a REST based system, my HTTP response body for that request is
just the image itself, no messing around with XML, HTTP handles it all,
and why not, it's proved technology, millions of images are already
transfered that way everyday.&lt;/p&gt;

&lt;p&gt;XML is great, it gives us a way of formatting and encoding arbitory
data in a way we can easily get it back with standard tools. But XML is
not the answer to all of our problems, it's a tool suitable for some
problems, you wouldn't use a hammer to fit your broken window, don't
use XML to do things other formats do better. That said, XML is great,
the best thing about it is the X, the extensible, the fact it lets us
define our own formats for our data, with SOAP and XML-RPC we throw
away that power.&lt;/p&gt;

&lt;p&gt;Mixing transport with data encoding is limiting and dangerous. What
if HTTP had also defined its data encoding instead of HTML being a
separate layer on top? Then the Web wouldn't have any graphics, no CSS
or Javascript, no binary file downloads, and no XML or Web Services.&lt;/p&gt;

&lt;p&gt;One of peoples favourite arguments about having a standard data encoding is that it means it saves you from writing extra code. &lt;a href="http://www.peej.co.uk/http://inessential.com/"&gt;Brent Simmons&lt;/a&gt; said recently:&lt;/p&gt;

&lt;blockquote cite="http://inessential.com/?comments=1&amp;amp;postid=2991"&gt;

&lt;p&gt;[REST] doesn't say anything about how data is formatted. So you have
to parse documents. With XML-RPC, assuming you have a library, you
never have to parse anything.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Well, assuming you have an XML library, you never have to parse
anything, that's the reason for XML, to create a standard document
format you can use off the shelf libraries to parse. It doesn't matter
what tags are in the XML, a decent XML library will get you the values
and structures from the XML document in a nice native format for your
programming language without you having to lift a finger.&lt;/p&gt;

&lt;p&gt;Further more, REST doesn't stop you from using a standard XML data
format if that's what's best for your application, although it is most
likely you'll end up with a meaningless response package that only
makes sense in the context of your application and is less RESTful than
it could be. Instead of XML containing users with addresses, you'll
have arrays full of strings, not very semantic, not very RESTful, not
really what everyone had in mind when XML was invented.&lt;/p&gt;

&lt;p&gt;So get over the common data format idea, embrace custom response
formats that make sense with respect to the resource being sent, and
think RESTfully when designing your applications from the ground up.&lt;/p&gt;
</content>
	</entry>
		<entry>
		<title>XMLHttpRequest, REST and the Rich User Experience</title>
		<link rel="alternate" type="text/html" href="http://www.peej.co.uk/articles/rich-user-experience.html" />
		<id>http://www.peej.co.uk/articles/rich-user-experience.html</id>
		<published>2004-10-23T07:00:00Z</published>
		<updated>2007-11-03T08:59:02Z</updated>
		<content type="html">&lt;h2&gt;XMLHttpRequest, REST and the Rich User Experience&lt;/h2&gt;

&lt;p&gt;Over the last few years Javascript has come of age. Gone are the inconsistent browser implementations, so not only is CSS mature enough to be used seriously, but so is Javascript. And now with the major browsers all supporting the XMLHttpRequest object, we can create a truely interactive interface to our web applications.&lt;/p&gt;

&lt;p&gt;Using a combination of Javascript with the XMLHttpRequest object, a server side language like PHP and &lt;a href="http://www.peej.co.uk/articles/rest"&gt;REST&lt;/a&gt; to do the talking in between, we can add dynamic elements to otherwise clumsy HTML forms.&lt;/p&gt;

&lt;h3&gt;Using the XMLHttpRequest Object&lt;/h3&gt;

&lt;p&gt;XMLHttpRequest is a Microsoft addition to IE5+ that was cloned by the Mozilla Project and now by Apple for Safari and Opera (at the time of writing support in Safari and Opera are new and incomplete, but this should improve with time). It allows Javascript to send a HTTP request, but unlike using an Image object, the XMLHttpRequest object allows you to  receive and process the response, it will even parse out an XML response for you (thus the XML in XMLHttpRequest, although any response file type will do).&lt;/p&gt;

&lt;p&gt;Creating and using the object is easy. Depending on whether the client is using IE with ActiveX or a standards compliant browser, there are different ways of instantiating the object, so this function wraps the process up into an easy to call package:&lt;/p&gt;

&lt;div class="code"&gt;&lt;pre&gt;function getHTTPObject() {
    if (typeof XMLHttpRequest != 'undefined') {
        return new XMLHttpRequest();
    }
    try {
        return new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            return new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
    }
    return false;
}&lt;/pre&gt;&lt;/div&gt;

&lt;p class="note"&gt;&lt;strong&gt;Note:&lt;/strong&gt; This is an updated version of my original Javascript as written by &lt;a href="http://www.peej.co.uk/http://simon.incutio.com/archive/2006/06/25/fjax"&gt;Simon Willison&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Then it's just a case of setting the objects properties and letting it go:&lt;/p&gt;

&lt;div class="code"&gt;&lt;pre&gt;var http = getHTTPObject();
http.open("GET", "http://example.org/test.txt", true);
http.onreadystatechange = function() {
	if (http.readyState == 4) {
		doSomethingWith(http.responseText);
	}
}
input.http.send(null);&lt;/pre&gt;&lt;/div&gt;

&lt;h3&gt;And Now the REST&lt;/h3&gt;

&lt;p&gt;There are a couple of XML based RPC formats available and suitable for sending information to our Javascript (XML-RPC, SOAP), however they are all large, bloated and don't use HTTP as it was designed. REST on the other hand is small, simple and a proven technology, and perfect for linking our Javascript with our serverside functions.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Since we're getting information from our server, we use our XMLHttpRequest object to issue a HTTP GET request to the URL of our serverside script.&lt;/li&gt;
&lt;li&gt;The script extracts request data from the URL querystring and processes the request responding with a plain text formatted file, or as a simple XML document.&lt;/li&gt;
&lt;li&gt;Our XMLHttpRequest object receives the response and parses out the data into Javascript variables. Our client has it's new data without the HTML page (the browser or the user) having to issue a separate HTTP request.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Obviously a new HTTP request has happened, but it has occured transparently in the background without our HTML page changing, allowing us to manipulate our pages DOM with the information received without having to do a full round trip to the server (and thus refreshing the page).&lt;/p&gt;

&lt;h3&gt;An Example&lt;/h3&gt;

&lt;p&gt;Time for an example. Lets say we have a HTML form that collects a users address, a usual form for an e-commerce system. Now to make our form more user friendly we'd like to allow the user to enter their postal code and get their address automatically entered into the form.&lt;/p&gt;

&lt;p&gt;In the past this would have required a round trip to the server, reposting form data and refreshing the page (or using a popup window and some Javascript), but using the XMLHttpRequest object we can do it all transparently to the user.&lt;/p&gt;

&lt;p&gt;So first we need our form, here's a quick XHTML file:&lt;/p&gt;

&lt;div class="code"&gt;&amp;lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&amp;gt;
&amp;lt;html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"&amp;gt;

  &amp;lt;head&amp;gt;
    &amp;lt;title&amp;gt;Your details&amp;lt;/title&amp;gt;
    &amp;lt;script type="text/javascript" src="http://www.peej.co.uk/address_from_postcode.js"&amp;gt;&amp;lt;/script&amp;gt;
  &amp;lt;/head&amp;gt;

  &amp;lt;body&amp;gt;
    &amp;lt;form method="post" action="process.php"&amp;gt;
      &amp;lt;fieldset class="address"&amp;gt;
        &amp;lt;input type="text" name="address[postcode]" class="postcode" /&amp;gt;
        &amp;lt;input type="button" value="Lookup address" class="lookup" /&amp;gt;
        &amp;lt;textarea class="address"&amp;gt;&amp;lt;/textarea&amp;gt;
      &amp;lt;/fieldset&amp;gt;
      &amp;lt;fieldset&amp;gt;
        &amp;lt;input type="submit" value="Save" /&amp;gt;
      &amp;lt;/fieldset&amp;gt;
    &amp;lt;/form&amp;gt;
  &amp;lt;/body&amp;gt;

&amp;lt;/html&amp;gt;&lt;/div&gt;
&lt;div class="caption"&gt;address.html - HTML page markup&lt;/div&gt;

&lt;p&gt;We've included our Javascript file &lt;code&gt;address_from_postcode.js&lt;/code&gt; in the header and given our address fieldset a classname of &lt;code&gt;address&lt;/code&gt; that we'll use to find and attach our behaviour to it's children. We do this rather than using an ID so that we can have multiple address fields on the same page.&lt;/p&gt;

&lt;div class="code"&gt;&lt;pre&gt;window.onload = function() {
  var url = "http://example.org/address_from_postcode.txt?postcode=";
  var fieldsets = document.getElementsByTagName("fieldset");
  for (var foo in fieldsets) {
    if (fieldsets[foo].className == "address") {
      var textareas = fieldsets[foo].getElementsByTagName("textarea");
      for (var bar in textareas) {
        if (textareas[bar].className == "address") {
          fieldsets[foo].address = textareas[bar];
          break;
        }
      }
      if (fieldsets[foo].address) {
        var inputs = fieldsets[foo].getElementsByTagName("input");
        for (var bar in inputs) {
          if (inputs[bar].className == "lookup") {
            inputs[bar].http = getHTTPObject();
            inputs[bar].working = false;
            inputs[bar].onclick = lookupAddress;
          }
          if (inputs[bar].className == "postcode") {
            fieldsets[foo].postcode = inputs[bar];
          }
        }
      } else {
        alert("No address textarea defined within address fieldset!");
      }
    }
  }

  function lookupAddress() {
    if (!this.working) {
      var http = this.http;
      var address = this.parentNode.address;
      this.http.open("GET", url + escape(this.parentNode.postcode.value), true);
      this.http.onreadystatechange = function() {
          if (http.readyState == 4) {
            this.working = false;
            address.innerHTML = http.responseText;
          }
      }
      this.http.send(null);
      this.working = true;
    }
  }

}&lt;/pre&gt;&lt;/div&gt;
&lt;div class="caption"&gt;address_from_postcode.js - Javascript behaviour layer&lt;/div&gt;

&lt;p&gt;This code does the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Search the DOM for all fieldsets with a class of "address".&lt;/li&gt;
&lt;li&gt;For each fieldset, get the first textarea with a class of "address".&lt;/li&gt;
&lt;li&gt;Also get the first inputs with a class of "lookup" and "postcode".&lt;/li&gt;
&lt;li&gt;Attach to the lookup button, a HTTP object and an on click event.&lt;/li&gt;
&lt;li&gt;On click of the button, make sure we're not already waiting for a HTTP response and if not issue the HTTP request.&lt;/li&gt;
&lt;li&gt;On reception of the HTTP response, set the address textarea to the recieved value.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So now to add this complex behaviour to our HTML forms, all we need to do is include the Javascript file and add the class names to the relevant elements.&lt;/p&gt;

&lt;p&gt;Of course this is just a simple example, I've been using it to dynamically update dropdowns and autocomplete fields from existing data, the number of applications of this type of rich forms are unlimited.&lt;/p&gt;</content>
	</entry>
		<entry>
		<title>HTML Overlays</title>
		<link rel="alternate" type="text/html" href="http://www.peej.co.uk/articles/html-overlays.html" />
		<id>http://www.peej.co.uk/articles/html-overlays.html</id>
		<published>2004-09-15T02:34:00Z</published>
		<updated>2007-11-03T08:47:26Z</updated>
		<content type="html">&lt;h2&gt;HTML Overlays&lt;/h2&gt;

&lt;p&gt;XUL overlays is a great feature of the excellent Mozilla application platform,
and now &lt;a href="http://www.peej.co.uk/http://ljouanneau.com/blog/"&gt;Laurent Jouanneau&lt;/a&gt; and &lt;a href="http://www.peej.co.uk/http://www.glazman.org/weblog"&gt;Daniel
Glazman&lt;/a&gt; have created &lt;a href="http://www.peej.co.uk/http://disruptive-innovations.com/zoo/20040830/HTMLoverlays.html" title="HTML Overlays"&gt;a
version for HTML&lt;/a&gt; using Javascript to do the legwork.&lt;/p&gt;
&lt;p&gt;The idea is to remove navigational elements and other pieces of HTML document
that are repeated across multiple pages, and place them in separately downloaded
files that can be cached and combined with the rest of the pages content by the browser.
A link tag is used to specify which overlays to use, and a Javascript script is used
to get the overlays and patch them into the document. The idea is good and the implementation
is solid, being based upon the proven technology of XUL overlays.&lt;/p&gt;

&lt;h3&gt;Backwards compatibility&lt;/h3&gt;
&lt;p&gt;I have a problem with the backwards compatibilty of the implementation. Without
Javascript, the browser doesn't load the overlays and you lose your navigational
elements, not only bad for those with JS turned off but also for &lt;a href="http://www.peej.co.uk/http://www.google.com/"&gt;Google&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;How about instead of using XML we use HTML, the id's in the overlay file matching
up with the id's in our host document, and the rest of the HTML being disgarded by
the Javascript. That way we can use another mechanism to include our overlays if Javascript
isn't present (iframes, object tags, a simple hyperlink) and they are human readable
in everyones browsers.&lt;/p&gt;
&lt;p&gt;This may remove some of the flexibility, and it doesn't answer all of the questions,
but personally I think this would be a saner way to introduce HTML overlays.&lt;/p&gt;
&lt;p&gt;The problem is that we still have to parse out our HTML. The useful thing about
using XML is that XMLHttpRequest will parse it and return you a nice DOM tree. We
could send our HTML overlay as XML to XMLHttpRequests and as HTML for regular requests,
but this makes things messy and adds some unwanted complexity. Mozilla and the latest
version of Opera provide us with a DOMParser object we can use to turn our HTML string
into a DOM tree, but that doesn't help IE users.&lt;/p&gt;

&lt;h3&gt;A better solution&lt;/h3&gt;
&lt;p&gt;The best solution I can think of is to add a new &amp;lt;overlay&amp;gt; element to our
HTML overlays to make them easy to extract with a regular expression. Browsers displaying
the HTML would ignore it and our Javascript to grab the overlays from the HTML would
be quick and simple.&lt;/p&gt;
&lt;div class="code"&gt;&amp;lt;overlay id="myOverlay"&amp;gt;
  &amp;lt;p&amp;gt;This is content in an overlay.&amp;lt;/p&amp;gt;
&amp;lt;/overlay&amp;gt;&lt;/div&gt;
&lt;p&gt;I've put up a demo of my version of &lt;a href="http://www.peej.co.uk/sandbox/overlay/"&gt;HTML overlays in action&lt;/a&gt;.&lt;/p&gt;</content>
	</entry>
		<entry>
		<title>REST</title>
		<link rel="alternate" type="text/html" href="http://www.peej.co.uk/articles/rest.html" />
		<id>http://www.peej.co.uk/articles/rest.html</id>
		<published>2002-05-02T07:00:00Z</published>
		<updated>2007-11-03T08:58:17Z</updated>
		<content type="html">&lt;h2&gt;Representational State Transfer (REST)&lt;/h2&gt;

&lt;p&gt;REST (REpresentational State Transfer) is a phrase coined by &lt;a href="http://www.peej.co.uk/http://www.ics.uci.edu/~fielding/"&gt;Roy Fielding&lt;/a&gt; in his dissertation &lt;a href="http://www.peej.co.uk/http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm"&gt;Architectural Styles and the Design of Network-based Software Architectures&lt;/a&gt;. It is an attempt to describe the undocumented architectural design principles behind the Web. In fact you are using the World's largest and most popular REST system right now, yes, the World Wide Web.&lt;/p&gt;
&lt;blockquote&gt;
    &lt;p&gt;"The World Wide Web architecture has evolved into a novel architectural style that I call 'representational state transfer.' Using elements of the client/server, pipe-and-filter, and distributed objects paradigms, this style optimises the network transfer of representations of a resource. A Web-based application can be viewed as a dynamic graph of state representations (pages) and the potential transitions (links) between states. The result is an architecture that separates server implementation from the client's perception of resources, scales well with large numbers of clients, enables transfer of data in streams of unlimited size and type, supports intermediaries (proxies and gateways) as data transformation and caching components, and concentrates the application state within the user agent components." - Roy Fielding&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;With all the talk of Web Services by the big software companies of this World, REST has (or will, maybe) come back into the limelight as an HTTP RPC protocol (like SOAP and XML-RPC).&lt;/p&gt;

&lt;h3&gt;The Web As We Know It&lt;/h3&gt;

&lt;p&gt;What REST basically says is "the Web is cool, HTTP is cool, and it already does everything we want and it works! Why do we need anything more?"&lt;/p&gt;
&lt;p&gt;The Web as defined by Tim Berners-Lee consists of three elements:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;URI (Uniform Resource Identifier) - The way of uniquely identifying resources on the network.&lt;/li&gt;
&lt;li&gt;HTTP (HyperText Transfer Protocol) - The protocol used to request a resource from a URI and respond to requests.&lt;/li&gt;
&lt;li&gt;HTML (HyperText Markup Language) - The content format of resources to be returned.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The interesting aspect of this mixture is the HTTP.&lt;/p&gt;
&lt;p&gt;We all use it everyday, but why is it so interesting. Well HTTP is a very cleverly designed protocol, it defines a small global set of verbs (the HTTP Methods: GET, POST, PUT, DELETE, etc) and applies them to a potentially infinite set of nouns (URIs). Most of the time when using the Web, you use the HTTP GET method to retrieve documents, if you're submitting some form data you may use the POST method, there are however a whole handful of other useful methods defined but rarely used on the Web but perfect for REST based Web Services.&lt;/p&gt;
&lt;p&gt;HTTPs power comes from its simplicity and extensibility. The ability to distribute any payload with headers, using predefined methods and its built-in support for URIs and resources, makes it really special. URIs are the defining characteristic of the Web, the mojo that makes it work and scale. HTTP as a protocol keeps them front and center by defining all methods as operations on URI-addressed resources.&lt;/p&gt;

&lt;h3&gt;Tell Me The REST&lt;/h3&gt;

&lt;p&gt;Stupid acronym puns aside, the idea of REST is to take what we all take for granted in the Web (ie. HTTP and URIs) and use it with other payloads (other than HTML, images, etc.) and other HTTP Methods than the usual GET and POST. REST defines identifiable resources (URIs), and methods for accessing and manipulating the state of those resources (HTTP Methods).&lt;/p&gt;
&lt;p&gt;HTTP messaging formats like SOAP and XML-RPC are supposedly the next big thing in the ever-developing world of the Web and the Internet. The idea of being able to link applications together using a standard open RPC format over HTTP connections shows great promise for distributed computing and interoperability, but the major problem with them is they purposefully break the HTTP spec by adding another layer of abstraction onto HTTP rather than using the protocol as it was designed. REST says this extra layer is an unnecessary layer of complexity.&lt;/p&gt;
&lt;p&gt;SOAP and XML-RPC are both designed to operate from a single URI with methods being invoked from within the request payload. This fails to use URIs as they were designed to be used. A URI is a unique resource on the network and as such, each method in your RPC call should use a unique URI.&lt;/p&gt;
&lt;p&gt;Using REST this is the case, and depending on your request method, different actions can be invoked. REST uses HTTP as it was designed, if you want to get some data you use a HTTP GET request, if you want to delete a record from a database you use a HTTP DELETE request, etc.&lt;/p&gt;

&lt;h3&gt;Advantages of REST&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;It uses well documented, well established, well used technology and methodology.&lt;/li&gt;
&lt;li&gt;It's already here today; in fact it's been here for the last 12 years!&lt;/li&gt;
&lt;li&gt;Resource centric rather than method centric.&lt;/li&gt;
&lt;li&gt;Given a URI anyone already knows how to access it.&lt;/li&gt;
&lt;li&gt;It's not another protocol on top of another protocol on top of another protocol on top of...&lt;/li&gt;
&lt;li&gt;The response payload can be of any format (some may call this a disadvantage, however the Web copes with it, it's just a case of defining the application grammar).&lt;/li&gt;
&lt;li&gt;Uses the inherent HTTP security model, certain methods to certain URIs can easily be restricted by firewall configuration, unlike other XML over HTTP messaging formats.&lt;/li&gt;
&lt;li&gt;REST makes sense, use what we already have; it is the next logical extension of the web.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Differences Between REST and RPC&lt;/h3&gt;

&lt;p&gt;REST is not RPC, RPC says, "define some methods that do something" whereas REST says, "define some resources and they will have these methods".&lt;/p&gt;
&lt;p&gt;It is a subtle but vital difference, when given a URI anyone knows they can interact with it via the predefined set of methods and receive standard HTTP responses in return. So given http://www.peej.co.uk/ I know I can issue a GET on it and receive something meaningful back. I may then try a PUT on it to change it and receive a meaningful HTTP error code since I'm not authorised to meddle with it.&lt;/p&gt;

&lt;h3&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;The Web of yesterday was all about transferring documents over the network to a users eyes, the Web of today adds the delivering of Web based services (ala e-mail, recruitment, etc.) to the mix, the Web of tomorrow will extend this information and service source from just being a user experience to being a generic client system.&lt;/p&gt;
&lt;p&gt;SOAP, XML-RPC, and other XML messaging over HTTP protocols may be the way this new era of the Web is created, the big (and not so big) corporates certainly think so, but REST shows that the infrastructure to do these types of machine to machine communications is already in place and has been since HTTP, the URI, and the Web was invented.&lt;/p&gt;

&lt;h4&gt;References&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://www.peej.co.uk/http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm"&gt;Architectural Styles and the Design of Network-based Software Architectures&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.peej.co.uk/http://internet.conveyor.com/RESTwiki/"&gt;RESTWiki&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content>
	</entry>
		<entry>
		<title>XHTML</title>
		<link rel="alternate" type="text/html" href="http://www.peej.co.uk/articles/xhtml.html" />
		<id>http://www.peej.co.uk/articles/xhtml.html</id>
		<published>2002-02-14T08:00:00Z</published>
		<updated>2008-04-16T17:56:49Z</updated>
		<content type="html">&lt;h2&gt;XHTML&lt;/h2&gt;
&lt;p&gt;What is it, why is it, when is it, and how to use it.&lt;/p&gt;

&lt;p class="warning"&gt;&lt;strong&gt;Warning:&lt;/strong&gt; This article was written in 2002, 3 years after XHTML became a standard. I
no longer believe that XHTML is a good choice for authoring on the Web. For more information please read
&lt;a href="http://www.peej.co.uk/http://hixie.ch/advocacy/xhtml"&gt;Ian Hickson's "Sending XHTML as text/html Considered Harmful"&lt;/a&gt; for more information.&lt;/p&gt;

&lt;h3&gt;Case Of The X&lt;/h3&gt;
&lt;p&gt;HTML4 as been around for a while, it is the basis of the modern web, however it is far from perfect and far from supported. Most people know about browser incompatibilities, but what most people do not realise is that HTML has a standard grammar; it is just that browsers do not comply.&lt;/p&gt;
&lt;p&gt;Actually it is not the browser author's fault. To explain this we need first to look at a little history.&lt;/p&gt;

&lt;h3&gt;HTML History Lesson&lt;/h3&gt;
&lt;p&gt;SGML (Standard Generalized Markup Language) is a grammar used to mark-up documents for formatting, it was invented in the 1980's as a standard way of defining formats on electronic documents.&lt;/p&gt;
&lt;p&gt;HTML is a SGML application. That is, it is a set of mark-up that conforms to the SGML grammar and tells people (or computers) how to format a document. SGML says an element must look like this, an attribute like this, etc.&lt;/p&gt;
&lt;p&gt;As HTML evolved and the web became more popular, the SGML roots of HTML started to slip and browsers started allowing HTML authors get away with not conforming to the HTML standard. This was good for developers as it gives them an easier ride, however over time it adds inconsistencies between browsers and leaves us in the current mess of browser conformity.&lt;/p&gt;

&lt;h3&gt;How This Relates To XML&lt;/h3&gt;
&lt;p&gt;You've probably all heard of XML. It is a grammar like SGML, but it has been specifically designed for the Internet. It too defines how to structure documents in a rigid way, which makes it easy for programs (called XML parsers) to interpret XML documents. XHTML is an implementation of HTML as an XML application, in similar ways that HTML is an SGML application.&lt;/p&gt;
&lt;p&gt;The ultimate goal of XHTML is to allow browsers to do away with their HTML parsers and basically become XML (and XSL and CSS) parsers. This has two benefits:&lt;/p&gt;
&lt;p&gt;Browsers based on an XML parser can parser and display any XML document which it has a stylesheet for, whether it be XHTML, WML, or any other XML application (even one you invented yourself, thus eXtensible).&lt;/p&gt;
&lt;p&gt;It removes incompatibilities, as the formatting of an XML document is defined in a stylesheet and an XML document is strongly typed (an illegal XML document will generate an XML parser error), so all clients will display documents the same.&lt;/p&gt;

&lt;h3&gt;But Browsers Only Support HTML 4&lt;/h3&gt;
&lt;p&gt;Wrong! XHTML is an implementation of HTML 4 in XML, thus it is HTML 4 compatible, it will be displayed fine in browsers which support HTML 4. All that XHTML really does is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Cut out the crap&lt;/li&gt;
&lt;li&gt;Make HTML a valid XML document&lt;/li&gt;
&lt;li&gt;Force authors to create valid HTML documents&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;So XHTML Is The Standard?&lt;/h3&gt;
&lt;p&gt;Yes, XHTML has been a W3C (the web standards people) recommendation for over a year (since Jan 2000 to be exact). You can (and should) use it right now.&lt;/p&gt;
&lt;p&gt;"But why should I?" I hear you cry:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Do you hate browser incompatibility? Then use XHTML.&lt;/li&gt;
&lt;li&gt;Are you fed up of having to check your HTML in hundreds of browsers? Then use XHTML.&lt;/li&gt;
&lt;li&gt;Do you want to help push for a standardised XML based web? Then use XHTML.&lt;/li&gt;
&lt;li&gt;Do you want your documents to be future proof? The use XHTML.&lt;/li&gt;
&lt;li&gt;Do you take pride in your HTML? Then use XHTML.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You probably think that you can write proper HTML4, I did, but if you haven't read and understood the HTML4 spec then you can guarentee that your documents are not standard, try running your HTML through the HTML validator, you will be surprised, I know I was.&lt;/p&gt;

&lt;h3&gt;So How Do I Write XHTML?&lt;/h3&gt;
&lt;p&gt;The only way to really know is to read the XHTML spec, however it isn't the easiest document in the World to read, so here are the basic rules:&lt;/p&gt;

&lt;h4&gt;Documents must be sent with the correct content type.&lt;/h4&gt;

&lt;p&gt;Web servers send HTML files with a mimetype definition of &lt;code&gt;text/html&lt;/code&gt;, this tells the browser what type of document to expect from a given request. XHTML is not HTML and should not be sent with the same mimetype, XHTML should be sent with the mimetype of &lt;code&gt;application/xhtml+xml&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;However it's not quite that simple as Internet Explorer does not understand the &lt;code&gt;application/xhtml+xml&lt;/code&gt; mimetype and interprets it as &lt;code&gt;text/xml&lt;/code&gt; displaying your XHTML file as an XML file. Luckily the W3C allow us to send HTML conforming XHTML documents with the &lt;code&gt;text/html&lt;/code&gt; mimetype which IE will understand and render our document correctly.&lt;/p&gt;
&lt;p&gt;Best practice is to get your server to check the types of files the browser will except and send the content as &lt;code&gt;application/xhtml+xml&lt;/code&gt; to browsers that support it, and as &lt;code&gt;text/html&lt;/code&gt; to those that don't.&lt;/p&gt;

&lt;h4&gt;Documents must start with a DOCTYPE definition.&lt;/h4&gt;

&lt;p&gt;This rule exists for HTML4 too, but browsers let you get away with missing it out, so most people do. The first line of any XHTML document should look like this:&lt;/p&gt;
&lt;code&gt;&amp;lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&amp;gt;&lt;/code&gt;
&lt;p&gt;This defines the document as XHTML1 and tells the browser which DTD (Document Type Definition) to use. There are three DTDs for XHTML, Transitional is the best to use if you are moving from HTML4 and want maximum backwards compatibility.&lt;/p&gt;

&lt;h4&gt;All tags must be lower case.&lt;/h4&gt;

&lt;p&gt;Yes, I don't care if you've used upper case tags before, XML is case sensitive so &amp;lt;B&amp;gt; is different to &amp;lt;b&amp;gt;, the XHTML DTDs define all tags in lower case only, so for your tags to work they must be lower case.&lt;/p&gt;

&lt;h4&gt;All attributes must be in quotes.&lt;/h4&gt;

&lt;p&gt;That is:&lt;/p&gt;

&lt;code&gt;&amp;lt;a href=http://www.example.com&amp;gt;Example&amp;lt;/a&amp;gt;&lt;/code&gt;

&lt;p&gt;is not valid XHTML. You must put quotes around the attribute body such as:&lt;/p&gt;

&lt;code&gt;&amp;lt;a href="http://www.peej.co.uk/http://www.example.com"&amp;gt;Example&amp;lt;/a&amp;gt;&lt;/code&gt;

&lt;h4&gt;All tags must be closed.&lt;/h4&gt;

&lt;p&gt;That is if you open a tag like &amp;lt;p&amp;gt; then you most also close it with &amp;lt;/p&amp;gt;, makes sense really.&lt;/p&gt;

&lt;h4&gt;Empty tags must be closed.&lt;/h4&gt;

&lt;p&gt;This is probably the most alien concept from HTML 4. A tag is empty if it does not contain text between itself and a closing tag, however in a valid XML document when you open a tag you must also close it. So:&lt;/p&gt;

&lt;code&gt;&amp;lt;img src="http://www.peej.co.uk/http://www.example.com/image.jpg"&amp;gt;&lt;/code&gt;

&lt;p&gt;is not valid XHTML. You must close the tag like this:&lt;/p&gt;

&lt;p class="code"&gt;&amp;lt;img src="http://www.peej.co.uk/http://www.
example.com/image.jpg"&amp;gt;&amp;lt;/img&amp;gt;&lt;/p&gt;

&lt;p&gt;This is however annoying, so XML defines a shorthand way of closing empty tags like this:&lt;/p&gt;

&lt;code&gt;&amp;lt;img src="http://www.peej.co.uk/http://www.example.com/image.jpg" /&amp;gt;&lt;/code&gt;

&lt;p&gt;Even the humble line break tag must be closed, ie:&lt;/p&gt;

&lt;p class="code"&gt;&amp;lt;br /&amp;gt;&lt;/p&gt;

&lt;h4&gt;You must nest tags correctly.&lt;/h4&gt;

&lt;p&gt;So you cannot open two tags, and then close them in a different order, eg:&lt;/p&gt;

&lt;code&gt;&amp;lt;b&amp;gt;&amp;lt;i&amp;gt;This is bold and italic&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt;&lt;/code&gt;

&lt;p&gt;is not valid, you must nest them correctly like:&lt;/p&gt;

&lt;code&gt;&amp;lt;b&amp;gt;&amp;lt;i&amp;gt;This is bold and italic&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&lt;/code&gt;

&lt;h4&gt;Tag context&lt;/h4&gt;

&lt;p&gt;Last but not least and the hardest concept to get hold of, you must use tags in the correct context. This is almost impossible to get right without practice or learning the XHTML spec inside out, so the best way to check if you have done something wrong is to check with the W3C validator.&lt;/p&gt;

&lt;h3&gt;Quick Review&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;XHTML is HTML4 compatible&lt;/li&gt;
&lt;li&gt;Don't forget the DOCTYPE&lt;/li&gt;
&lt;li&gt;Don't forget to close all tags&lt;/li&gt;
&lt;li&gt;Use W3Cs validator to check your XHTML (or if I've failed to convert you at least your HTML).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Now go forth and prepare your web sites for XML.&lt;/p&gt;

&lt;h4&gt;XHTML Links &amp;amp; References&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://www.peej.co.uk/http://www.w3.org/TR/xhtml1/"&gt;W3C's XHTML Spec&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.peej.co.uk/http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;W3C's XHTML Transitional DTD&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.peej.co.uk/http://www.zvon.org/xxl/xhtmlReference/Output/"&gt;ZVon XHTML Reference&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.peej.co.uk/http://www.xhtml.org/"&gt;XHTML.org&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.peej.co.uk/http://www.wdvl.com/Authoring/Languages/XML/XHTML/"&gt;Webdev Virtual Library Introdution to XHTML&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.peej.co.uk/http://www.oreillynet.com/pub/a/network/2000/04/28/feature/xhtml_rev.html"&gt;O'Reilly XHTML: The Clean Code Solution&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.peej.co.uk/http://hotwired.lycos.com/webmonkey/00/50/index2a.html"&gt;Web Monkey XHTML Overview&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content>
	</entry>
		<entry>
		<title>Cross site scripting</title>
		<link rel="alternate" type="text/html" href="http://www.peej.co.uk/articles/cross-site-scripting.html" />
		<id>http://www.peej.co.uk/articles/cross-site-scripting.html</id>
		<published>2001-09-27T07:00:00Z</published>
		<updated>2007-11-03T08:47:10Z</updated>
		<content type="html">&lt;h2&gt;What Is Cross Site Scripting?&lt;/h2&gt;
&lt;p&gt;Cross site scripting is the name given to web site vulnerabilities arising from the embedding of malicious HTML tags into a HTML document which is generated dynamically on the server. By entering HTML tags into an input field of a web site (either via a HTTP POST or GET i.e. as a form post or through the query string), these tags can then be included in a HTML page served from the web site.&lt;/p&gt;
&lt;p&gt;Web pages contain both text and HTML markup that is generated by the server and interpreted by the client browser. Servers that generate static pages have full control over how the client will interpret the pages sent by the server. However, servers that generate dynamic pages do not have complete control over how their output is interpreted by the client.&lt;/p&gt;
&lt;p&gt;These attacks do not affect the security of the server itself, as they only create malicious tags or code that is sent to the client. However, these tags can be used to annoy users with large graphics and/or sound files, change the expected look of a web page, change the functionality of the web page and thus capture sensitive user information, etc.&lt;/p&gt;
&lt;p&gt;The heart of the issue comes back to the age-old knowledge that you cannot trust the client for anything.&lt;/p&gt;

&lt;h3&gt;Script Tag Include&lt;/h3&gt;
&lt;p&gt;Sites that host discussion groups with web interfaces have long guarded against a vulnerability where one client embeds malicious HTML tags in a message intended for another client. For example, an attacker might post a message like:&lt;/p&gt;
&lt;blockquote&gt;Hello message board. This is a message. &amp;lt;script&amp;gt;malicious code&amp;lt;/script&amp;gt; This is the end of my message.&lt;/blockquote&gt;
&lt;p&gt;When a client with scripts enabled in their browser reads this message, the malicious code may be executed unexpectedly. Scripting tags that can be embedded in this way include &amp;lt;script&amp;gt;, &amp;lt;object&amp;gt;, &amp;lt;applet&amp;gt;, and &amp;lt;embed&amp;gt;.&lt;/p&gt;
&lt;p&gt;This type of vulnerability is easy to stop by simply filtering/encoding any user input for the offending tags, however as we will learn, the issue is a little more complicated that this.&lt;/p&gt;

&lt;h3&gt;Malicious Link&lt;/h3&gt;
&lt;p&gt;Many Internet web sites overlook the possibility that a client may send malicious data intended to be used only by itself. This is an easy mistake to make. After all, why would a user enter malicious code that only the user will see?&lt;/p&gt;
&lt;p&gt;However, this situation may occur when the client relies on an untrustworthy source of information when submitting a request. For example, an attacker may construct a malicious link such as:&lt;/p&gt;
&lt;code&gt;&amp;lt;a href="http://www.peej.co.uk/http://example.com/comment.cgi?mycomment=&amp;lt;script&amp;gt;malicious code&amp;lt;/script&amp;gt;"&amp;gt;Click here&amp;lt;/a&amp;gt;&lt;/code&gt;
&lt;p&gt;When an unsuspecting user clicks on this link, the URL sent to example.com includes the malicious code. If the web server sends a page back to the user including the value of mycomment, the malicious code may be executed unexpectedly on the client.&lt;/p&gt;

&lt;h3&gt;Inline Frame / Script Include&lt;/h3&gt;
&lt;p&gt;The inline frame tag &amp;lt;iframe&amp;gt; added to Microsoft IE in version 4, allows the inclusion of a remote document within a parent document:&lt;/p&gt;
&lt;code&gt;&amp;lt;iframe src="http://www.peej.co.uk/http://exampl.com/malicious.html"&amp;gt;&lt;/code&gt;
&lt;p&gt;The script tag can also be used to include a remote script block into the parent document:&lt;/p&gt;
&lt;code&gt;&amp;lt;script src="http://www.peej.co.uk/http://example.com/malicious.js"&amp;gt;&lt;/code&gt;

&lt;h3&gt;Form Tag Include&lt;/h3&gt;
&lt;p&gt;In addition to scripting tags, other HTML tags such as the &amp;lt;form&amp;gt; tag have the potential to be abused by an attacker. For example, by embedding malicious &amp;lt;form&amp;gt; tags at the right place, an intruder can trick users into revealing sensitive information by modifying the behavior of an existing form.&lt;/p&gt;

&lt;h2&gt;How To Limit The Effects Of Cross Site Scripting&lt;/h2&gt;
&lt;p&gt;So far the issue seems pretty simple. Filter an input to make sure tags that could be used for a malicious reason. However the problem is a little deeper than that, as I will explain below.&lt;/p&gt;
&lt;p&gt;Any data inserted into an output stream originating from a server is presented as originating from that server, even if it contains output that originates from a user. Web developers must evaluate whether their sites will send untrusted data as part of an output stream, or if they will validate any data before it is included.&lt;/p&gt;
&lt;p&gt;Untrusted input can come from, but is not limited to,&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;URL parameters&lt;/li&gt;
&lt;li&gt;Form elements&lt;/li&gt;
&lt;li&gt;Cookies&lt;/li&gt;
&lt;li&gt;Databases queries&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A combination of steps must be taken to mitigate this vulnerability. These steps include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Explicitly setting the character set encoding for each page generated by the web server&lt;/li&gt;
&lt;li&gt;Identifying special characters&lt;/li&gt;
&lt;li&gt;Encoding dynamic output elements&lt;/li&gt;
&lt;li&gt;Filtering specific characters in dynamic elements&lt;/li&gt;
&lt;li&gt;Examine cookies&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Explicitly Setting the Character Encoding&lt;/h3&gt;
&lt;p&gt;If the web server does not specify which character encoding is in use, the client can not tell which characters are special. Web pages with unspecified character encoding work most of the time because most character sets assign the same characters to byte values below 128. The question comes with the characters above 128, which of these are special?&lt;/p&gt;
&lt;p&gt;Some 16-bit character-encoding schemes have additional multi-byte representations for special characters such as "&amp;lt;". Some browsers recognize this alternative encoding and act on it. This is correct behaviour, but it makes attacks using malicious scripts much harder to prevent. The server simply doesn't know which byte sequences represent the special characters.&lt;/p&gt;
&lt;p&gt;Web servers should set the character set, then make sure that the data they insert is free from byte sequences that are special in the specified encoding. For example:&lt;/p&gt;
&lt;code&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
&amp;lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /&amp;gt;&lt;br /&gt;
&amp;lt;title&amp;gt;Using ISO-8859-1 Charset&amp;lt;/title&amp;gt;&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
.... &lt;br /&gt;
&lt;/code&gt;
&lt;p&gt;The &amp;lt;meta&amp;gt; tag in the &amp;lt;head&amp;gt; section of this sample HTML forces the page to use the ISO-8859-1 character set encoding.&lt;/p&gt;

&lt;h3&gt;Identifying the Special Characters&lt;/h3&gt;
&lt;p&gt;The next two steps, encoding and filtering, first require an understanding of "special characters". The HTML specification determines which characters are "special", because they have an effect on how the page is displayed. However, many web browsers try to correct common errors in HTML. As a result, they sometimes treat characters as special when, according to the specification, they are not.&lt;/p&gt;
&lt;p&gt;In body content:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;"&amp;lt;" is special because it introduces a tag.&lt;/li&gt;
&lt;li&gt;"&amp;amp;" is special because it introduces a character entity.&lt;/li&gt;
&lt;li&gt;"&amp;gt;" is special because some browsers treat it as special, on the assumption that the author of the page really meant to put in an opening "&amp;lt;", but omitted it in error.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In attributes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;In attribute values enclosed with double quotes, the double quotes are special because they mark the end of the attribute value.&lt;/li&gt;
&lt;li&gt;In attribute values enclosed with single quotes, the single quotes are special because they mark the end of the attribute value.&lt;/li&gt;
&lt;li&gt;Attribute values without any quotes make the white-space characters such as space and tab special.&lt;/li&gt;
&lt;li&gt;"&amp;amp;" is special when used in conjunction with some attributes because it introduces a character entity.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In URLs:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Space, tab, and new line are special because they mark the end of the URL.&lt;/li&gt;
&lt;li&gt;"&amp;amp;" is special because it introduces a character entity or separates querystring parameters.&lt;/li&gt;
&lt;li&gt;Non-ASCII characters (that is, everything above 128 in the ISO-8859-1 encoding) are not allowed in URLs, so they are all special here.&lt;/li&gt;
&lt;li&gt;The "%" must be filtered from input anywhere parameters encoded with HTTP escape sequences are decoded by server-side code.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Encoding Dynamic Output Elements&lt;/h3&gt;
&lt;p&gt;Each character in the ISO-8859-1 specification can be encoded using its numeric entry value.&lt;/p&gt;
&lt;p&gt;The following example uses the copyright mark in an HTML document:&lt;/p&gt;
&lt;p&gt;&amp;amp;#169; 2001 Mauldeth Int.&lt;/p&gt;
&lt;p&gt;The copyright character is 169 and using the &amp;amp;# syntax allows the author to insert encoded characters that will be interpreted by the browser.&lt;/p&gt;
&lt;p&gt;Encoding untrusted data has benefits over filtering untrusted data, including the preservation of visual appearance in the browser. This is important when special characters are considered acceptable.&lt;/p&gt;

&lt;h3&gt;Filtering Dynamic Content&lt;/h3&gt;
&lt;p&gt;Unfortunately, it is unclear whether there are any other characters or character combinations that can be used to expose other vulnerabilities. The recommended method is to select the set of characters that is known to be safe rather than excluding the set of characters that might be bad.&lt;/p&gt;
&lt;p&gt;For example, a form element that is expecting a person's age can be limited to only accept numeric characters between a certain range (1 to 99 would probably do). There is no reason for this age element to accept any letters or other special characters. Using this positive approach of selecting the characters that are acceptable will help to reduce the ability to exploit other yet unknown vulnerabilities.&lt;/p&gt;
&lt;p&gt;The filtering process can be done as part of the data input process, the data output process, or both. I would recommend filtering the data during the input process as this has the benefits of only needing to be done once, and (if the input is going to be stored in a database) can be included as part of your database input validation (to continue the example above, an age would be expected to be held in a numeric database field, trying to insert a string into this field would cause an input error anyway). The only disadvantage of filtering on input is that you must make sure that all input is filtered at every entry point to the storage device.&lt;/p&gt;

&lt;h3&gt;Examine Cookies&lt;/h3&gt;
&lt;p&gt;Cookies can often be overlooked. Since cookies are just text documents held on the client's machine, it is easy for a client to alter their cookie to allow inclusion of malicious content or send bogus information in their HTTP requests. Like anything else from the client, cookies should not be trusted and the information within them should be verified before being used.&lt;/p&gt;

&lt;h2&gt;So Why Is It Called "Cross Site Scripting"?&lt;/h2&gt;
&lt;p&gt;Since the issue isn't just about scripting, and there isn't necessarily anything cross site about it, the name Cross Site Scripting is a little ambiguous. It was coined earlier on when the problem was less understood, and has stuck, it's as simple as that.&lt;/p&gt;</content>
	</entry>
		<entry>
		<title>SQL Injection</title>
		<link rel="alternate" type="text/html" href="http://www.peej.co.uk/articles/sql-injection.html" />
		<id>http://www.peej.co.uk/articles/sql-injection.html</id>
		<published>2001-08-01T07:00:00Z</published>
		<updated>2007-11-03T08:59:19Z</updated>
		<content type="html">&lt;h2&gt;SQL Injection&lt;/h2&gt;
&lt;p&gt;The checking of user input is often overlooked by web developers but is very important to close serious vulnerabilities in web systems. I am currently working on a project for a client that involves rewriting and designing their existing web application. This process started with looking at their existing system.&lt;/p&gt;
&lt;p&gt;Within 10 minutes of investigation I was able to drop their entire database just through their web sites log in box. I was able to do this for the following reasons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The log in form textbox had no maximum input length set, allowing me to enter a large input string including my concoction of SQL. This hole is not required as I could have written a spoof form without a maximum input length, but it made my life easier non-the-less.&lt;/li&gt;
&lt;li&gt;There was no length checking for the username field. Their username database field is 10 chars long, and yet the server code did not check to make sure my input was 10 or fewer characters.&lt;/li&gt;
&lt;li&gt;There was no invalid character stripping or conversion. By allowing me to place a single quote in the input text, I was able to fool their system into allowing me to run arbitrary SQL code on their database server.&lt;/li&gt;
&lt;li&gt;The database user had full permissions on the database, including delete and drop permission.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;By submitting code similar to what I've written below, I was able to execute whatever SQL I wanted on their database just through my web browser:&lt;/p&gt;
&lt;blockquote&gt;'; DROP DATABASE dbname; SELECT * FROM tbluser WHERE username = '&lt;/blockquote&gt;
&lt;p&gt;When this input is concatenated with the rest of the database query string, the following SQL query is created and executed on the database:&lt;/p&gt;
&lt;blockquote&gt;SELECT * FROM tbluser WHERE username = ''; DROP DATABASE dbname; SELECT * FROM tbluser WHERE username = '';&lt;/blockquote&gt;
&lt;p&gt;The first two characters of the user input terminate the first select statement, the next statement does the malicious drop of the database, and the last select statement is a dummy statement to use the original closing quote and make sure the whole query string is valid.&lt;/p&gt;
&lt;p&gt;Of course, the attacker needs to know the name of the database or any other database structures that they need to reference, but these are often easy to guess by trial and error or by making the database throw up errors if they are not caught by the web application.&lt;/p&gt;
&lt;p&gt;I hope this little demonstration of web app security is useful to web developers and makes clear the point of why checking user input from web forms is important.&lt;/p&gt;

&lt;p class="note" title="Additional note"&gt;This type of web application attack has been given the name of an "SQL Injection" attack since I originally wrote this article, so the title of the article has been changed to reflect this fact even though the validation of user input effects more than just SQL injection attacks.&lt;/p&gt;</content>
	</entry>
	</feed>
