Hunchentoot e REST, parte 2: suporte a ETags

27 de Abril de 2008 @ 17:18 - Lucindo
Arquivado sob common lisp, programação, web | Link desta publicação | Enviar por e-mail

Bom, esse post é mais um snippet de como implementar um serviço REST usando Hunchentoot. Dessa vez adicionei suporte a ETags no código do outro post. Ficou assim o tratamendo das requisições:

(defvar *data-id* “e8d8993494ffc11:b8e”)
(defun get-data-id (target)
  “get the last id for requested data”
  *data-id*)

(defmethod handle :around (request-method)
  “ETag support for all methods”
  (let ((data-id (get-data-id (script-name)))
        (last-id (header-in :If-None-Match)))
    (setf (header-out :ETag) data-id)
    (if (and last-id (equalp last-id data-id))
        (setf (return-code) +http-not-modified+)
        (call-next-method))))

Fazendo um request normal:

GET /rest HTTP/1.1
Host: eddie

HTTP/1.1 200 OK
Content-Length: 3
Content-Type: text/html; charset=iso-8859-1
Date: Sun, 27 Apr 2008 20:07:53 GMT
Server: Hunchentoot 0.15.6
Etag: e8d8993494ffc11:b8e

GET

Agora o cliente passando o ETag:

GET /rest HTTP/1.1
Host: eddie
If-None-Match: e8d8993494ffc11:b8e

HTTP/1.1 304 Not Modified

Suportar ETags é muito fácil e se o cliente do seu serviço souber usar é muito útil para os dois.

Download: rest.lisp

3 Comentários »

RSS de comentários deste artigo. URI para link desta publicação:

  1. Bacana! Hoje eu brinquei com lisp e fastcgi no lighttpd, mas não fui muito longe. Esbarrei no Hunchentoot tb :)

    Só estou na dúvida: sbcl ou clisp?

    Comentário de Walter Cruz — 28 de Abril de 2008 #

  2. Eu uso SBCL.

    Comentário de Lucindo — 28 de Abril de 2008 #

  3. The problem I see with this solution is that to calculate the ETag you will often have to generate the actual contents, i.e. something only done later further down the call-next-method chain.

    Wouldn’t it be better to modify the handling of (setf (header-out :ETag) …) in Hunchentoot itself so it would check these conditions and short-circuit further processing by e.g. raising a certain condition?

    (sorry for posting in English — I can barely read Portuguese…)

    Comentário de Holger — 22 de Setembro de 2008 #

Deixe um comentário


Hits para esta publicação: 312

(blog ‘lucindo) | http://blog.lucindo.com.br