2009年 05月 17日

WAF

結局、アプリケーション (not Web App = WWW 非依存) なものを Web っぽい部分とつなぐ Dispatcher (Router) があればよくて、あとはおまけっぽいので、HTTP::Engine + Router で WAF っぽい部分は終わりになるような気がした。

uri path -> 実際の処理 の受け渡しが複数ファイルを見ないとわからないようなのはよくない。あっちいったりこっちいったりめんどうくさいし、ある uri へアクションを設定したいだけのためにファイル1個おいたり、とても無駄だ。

app 部分はまた別のものだ。

整理して github におきました: http://github.com/cho45/Chord/tree/master

#!/usr/bin/env perl
package FooRouter;

use Chord::Router::HTTP;
use base qw(Chord::Router::HTTP);

# define views
use JSON::XS;
sub json ($$) {
	my ($res, $stash) = @_;
	$res->header("Content-Type" => "application/json");
	$res->content(encode_json($stash));
}

use Text::MicroMason;
sub html ($%) {
	my ($res, $stash) = @_;
	my $m  = Text::MicroMason->new(qw/ -SafeServerPages -AllowGlobals /);
	$m->set_globals(map { ("\$$_", $stash->{$_}) } keys %$stash);

	my $content = $m->execute(text =>  q{
		<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
		<title><%= $title %></title>
		<p><%= $content %>
	});

	$res->header("Content-Type" => "text/html");
	$res->content($content);
}

# routing

route "/",
	action => sub {
		my ($req, $res) = @_;
		html $res, {
			title   => "Hello",
			content => "Hello",
		};
	};

route "/my/*path",
	action => sub {
		my ($req, $res) = @_;
		$res->code(302);
		$res->header("Location" => sprintf("/foo/%s", $req->param("path")));
	};

route "/:author/", author => qr/[a-z][a-z0-9]{1,30}/,
	action => sub {
		my ($req, $res) = @_;
		html $res, {
			title   => "Hello",
			content => sprintf("This is %s's page.", $req->param("author"))
		};
	};

route "/api/foo",
	action => sub {
		my ($req, $res) = @_;
		json $res, {
			foo => "bar"
		};
	};

route "/die",
	action => sub {
		die "Died";
	};


__PACKAGE__->run;
package Chord::Router::HTTP;
use Any::Moose;
use HTTP::Engine;

use Exporter::Lite;
our @EXPORT = qw(route);

our $routing = [];

sub route ($;%) {
	my ($path, %opts) = @_;
	my $regexp  = "^$path\$";
	my $capture = [];

	$regexp =~ s{([:*])(\w+)}{
		my $type = $1;
		my $name = $2;
		push @$capture, $name;
		sprintf("(%s)",
			$opts{$name} ||
			(($type eq "*") ? ".*": "[^\/]+")
		);
	}ge;

	push @$routing, {
		%opts,
		define  => $path,
		regexp  => $regexp,
		capture => $capture,
	};
}

sub dispatch {
	my ($self, $request) = @_;
	my $path   = $request->path;
	my $params = {};
	my $action;

	for my $route (@$routing) {
		if (my @capture = ($path =~ $route->{regexp})) {
			for my $name (@{ $route->{capture} }) {
				$params->{$name} = shift @capture;
			}
			$action = $route->{action};
			last;
		}
	}

	my $req = $request;
	$req->param(%$params);
	my $res = HTTP::Engine::Response->new(status => 200);
	$res->header("Content-Type" => "text/html");
	if ($action) {
		eval {
			$action->($req, $res);
		}; if ($@) {
			$res->code(500);
			$res->header("Content-Type" => "text/plain");
			$res->content($@);
		}
	} else {
		$res->code(404);
		$res->content("Not Found");
	}
	$res;
}

sub process {
	my ($self, $request) = @_;

	$self->dispatch($request);
}

sub run {
	my ($class, %opts) = @_;

	HTTP::Engine->new(
		interface => {
			module => 'ServerSimple',
			args   => {
				host => 'localhost',
				port =>  3001,
			},
			request_handler => sub {
				my $req = shift;
				$class->new->process($req);
			},
			%opts
		},
	)->run;
}


1;
__END__

Web App なのか App + Web なのか……

テストを考えなければどっちでもいいんだろうけどなぁ。

アプリケーションロジック (いくつかのモデルの結合) テストと、HTTP まで含めた結合テストは、はたして別れてたほうがいいのかなぁ。

2009年 05月 16日

memo

自分が JavaScript を書くときの規約みたいなのは既にだいたい決まっていて、結局のところ

  • クラス作成ライブラリなどを使わない
    • → そのライブラリを使わなくなったときにいちいち移植する必要があるから
    • → 普通に function Constructor () {} と prototype への代入を使う
    • → 継承みたいなことは基本しない。プロパティにオブジェクトをもって delegate する。
  • DOM, Event 関係はライブラリを使う
    • → クロスブラウザはここらへんに集約されているので……
  • 小さな snippet を組み合せる
    • → 必要なものを必要な分だけ
    • → クライアントサイドなので、変なものをいれない
2009年 05月 13日

JavaScript の正規表現のメタ文字をエスケープ

String.prototype.replace は正規表現じゃないと ignore case しつつ global マッチとかできないけど、JavaScript には RegExp.escape 的なものがないのでメタ文字が問題になる。

var escaped = 'f[oo'.replace(/[\s\S]/g, function (_) {
    return '\\u' + (0x10000 + _.charCodeAt(0)).toString(16).slice(1)
});

'F[oobarf[oof[oo'.replace(new RegExp(escaped, 'gi'), 'xxx');

たぶんこれでいける?っぽい。正確にはメタ文字のエスケープではなくて、全部エスケープしてメタ文字を無効化しているだけですね。

ignore case しなくてよくて、search が空文字列に絶対ならないなら

'f[oobarf[oo'.split('f[oo').join('xxx');

とかもいいのかな。どんなケースでもうまくいくかまでけんしょうしていないです。(頭まわってない)

前提が抜けてて大変申し分けないのですが、

  • JS は日常的に使用される処理系がいくつもある
  • 仕様が決まっているとはいえ特定文字だけエスケープすると漏れがでる可能性がある
    • 少なくとも全ブラウザであらゆるケースをテストする必要がでてくる
  • そもそも全部列挙するのがめんどくさい

ので、最も安全で楽な形を考えた、という感じです。

2009年 05月 11日

gerry++

tsocks 経由ってるかどうかプロンプトに表示するようにした

2009年 05月 10日

gerry++

2009年 05月 02日

gerry++

2009年 04月 27日

choww

2009年 04月 19日

gerry++