目次

背景

apache2.0のmodでrequest filterを作ることになったので、その際のメモ。

modの要件

  • 任意のHTTP requestのHTTP headerを調べて、X-MAGE-TESTヘッダが無いrequestは403(Forbidden)で応答する
  • X-MAGE-TESTヘッダの値が、アクセス可能リストに含まれていない場合、406(Not Acceptable)で応答する
  • アクセス可能リストは1行に1個のアクセス可能なX-MAGE-TESTヘッダの値を記したテキストファイル
  • X-MAGE-TESTヘッダの値がアクセス可能リストに含まれる場合、後続にリクエストを通す
  • アクセス可能リストを記したファイルが存在しない場合、全てのリクエストを通す
  • アクセス可能リストの読み込みは、Apache起動時の1回だけ

httpd.confへの設定

参考

www.apache.orgより

おまけ?w

moduleベースでpyukiwiki焼き直しproject?
PBG4でも、ThinkPad?でもメモ帳代わりにApache+PyukiWikiを入れて使ってます。
お遊びでmoduleでwikiを作るのもありかなと思ってみました。mod_perlか aprでmoduleを書いてみようかなと。。。
  • どう思いますか? -- 2006-03-17 (Fri) 14:07:51

サンプル実装 1

とりあえず作ってみた版。これと httpd.confにLoadModule?を書いておけば、 全てのリクエストの呼び出しhookされる。

httpd.confへの記述

LoadModule magetest_module modules/mod_magetest.so

moduleソース

/* 
**  mod_magetest.c -- Apache sample magetest module
**  [Autogenerated via ``apxs -n magetest -g'']
**
**  To play with this sample module first compile it into a
**  DSO file and install it into Apache's modules directory 
**  by running:
**
**    $ apxs -c -i mod_magetest.c
**
**  Then activate it in Apache's httpd.conf file for instance
**  for the URL /magetest in as follows:
**
**    #   httpd.conf
**    LoadModule magetest_module modules/mod_magetest.so
**    <Location /magetest>
**    SetHandler magetest
**    </Location>
**
**  Then after restarting Apache via
**
**    $ apachectl restart
**
**  you immediately can request the URL /magetest and watch for the
**  output of this module. This can be achieved for instance via:
**
**    $ lynx -mime_header http://localhost/magetest 
**
**  The output should be similar to the following one:
**
**    HTTP/1.1 200 OK
**    Date: Tue, 31 Mar 1998 14:42:22 GMT
**    Server: Apache/1.3.4 (Unix)
**    Connection: close
**    Content-Type: text/html
**  
**    The sample page from mod_magetest.c
*/ 

#include "httpd.h"
#include "http_config.h"
#include "http_protocol.h"
#include "ap_config.h"

/* The sample content handler */
static int magetest_handler(request_rec *r)
{
	char *hdr;

	hdr = apr_table_get(r->headers_in, "X-MAGE-TEST");
	if ( hdr ) {
		return HTTP_FORBIDDEN;
	} else {
		// do nothing
	}

    return OK;
}

static void magetest_register_hooks(apr_pool_t *p)
{
    ap_hook_post_read_request(magetest_handler, NULL, NULL, APR_HOOK_MIDDLE);
    //ap_hook_handler(magetest_handler, NULL, NULL, APR_HOOK_MIDDLE);
    // この場合、Content Handlerになる。ハンドラ識別名は magetest。
    // SetHandlerや SetInputFilterの対象はこの名前にしとくこと。
}

/* Dispatch list for API hooks */
module AP_MODULE_DECLARE_DATA magetest_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       */
    magetest_register_hooks  /* register hooks                      */
};

Last-modified: 2006-03-24 16:58:28