mod_teapot

http://trackback.blogsys.jp/livedoor/faulist/1296865 を見て、HTTP のステータスコードに 418 があることを知った。正確には HTTP ではなく、RFC 2324 で定義されている Hyper Text Coffee Pot Control Protocol(HTCPCP/1.0) なんだけど。
そんなわけで、お年寄りから小さなお子様まで、誰にでも簡単にティーポットでコーヒーを淹れようとしたことがわかるようにモジュール化してみる。

テンプレートの作成
% apxs2 -gn teapot
Creating [DIR]  teapot
Creating [FILE] teapot/Makefile
Creating [FILE] teapot/modules.mk
Creating [FILE] teapot/mod_teapot.c
Creating [FILE] teapot/.deps
コード
  • teapot/mod_teapot.c
    • teapot_handler() 以外はテンプレートそのまま。
#include "httpd.h"
#include "http_config.h"
#include "http_protocol.h"
#include "ap_config.h"

/* The sample content handler */
static int teapot_handler(request_rec *r)
{
    r->content_type = "text/html";      
    apr_table_set(r->notes, "error-notes", "<p>I'm a teapot.");
    apr_table_set(r->notes, "verbose-error-to", "*");
    r->status_line = "418 I'm a teapot";
    return 418;
}

static void teapot_register_hooks(apr_pool_t *p)
{
    ap_hook_handler(teapot_handler, NULL, NULL, APR_HOOK_MIDDLE);
}

/* Dispatch list for API hooks */
module AP_MODULE_DECLARE_DATA teapot_module = {
    STANDARD20_MODULE_STUFF, 
    NULL,                  /* create per-dir    config structures */
    NULL,                  /* merge  per-dir    config structures */
    NULL,                  /* create per-server config structures */
    NULL,                  /* merge  per-server config structures */
    NULL,                  /* table of config file commands       */
    teapot_register_hooks  /* register hooks                      */
};
コンパイルとモジュールの配置
% sudo apxs2 -ci mod_teapot.c 
Apache の設定
  • /etc/apache2/mods-available/teapot.conf
<IfModule mod_teapot.c>
  SetHandler teapot
</ifModule>
  • /etc/apache2/mods-available/teapot.load
LoadModule teapot_module /usr/lib/apache2/modules/mod_teapot.so
  • 設定の読み込み
# a2enmod teapot
# /etc/init.d/apache2 restart
コーヒーをいれる


おぉ、いかんいかん、ティーポットだった。

参考

Apache のモジュールの作成方法は Apacheモジュールの作成とgdbとloggerでのデバッグ方法 - よねのはてな を参考にしました。