Examples of Intercepor: Refined

都合がつかず、第5回勉強会には行けませんでした... orz


今回紹介するのは、以前作成した Interceptor の修正・改良版です。

Piece_Unity_Plugin_Interceptor_Mobile

Piece_Unity_Plugin_Interceptor_MobileDetection
  • プラグイン名変更: Interceptor_MobileUserAgent -> Interceptor_MobileDetection
  • それに伴い、コンテキストに設定される値のキーも変更
    • MobileUserAgent_detector -> (廃止)
    • MobileUserAgent_isMobile -> MobileDetection_isMobile
    • MobileUserAgent_supportsCookie -> MobileDetection_supportsCookie
  • Cookie をサポートしない端末の場合、Renderer_Redirection の設定ポイント addSessionID を強制的に true にするようにした
  • 設定ポイントを追加・変更
キー 説明 デフォルト値
forcedUserAgent $_SERVER['HTTP_USER_AGENT'] を上書きする文字列 (開発用) null
lazyDetection (廃止)
struct (追加) Net_UserAgent_Mobile を用いて判定するか否か true
lazy (追加) 単純な正規表現で判定するか否か false
hostName ホスト名が一致するとき携帯モードとなる
/ で始まるときは正規表現として扱う
null
Piece_Unity_Plugin_Interceptor_MobileRenderer

変更なし

Piece_Unity_Plugin_Interceptor_MobileOutputFilter
  • 設定ポイントを追加
キー 説明 デフォルト値
headerCharset Content-Type HTTP ヘッダに挿入される値 Shift_JIS
outputCharset 出力 (変換先) エンコーディング SJIS-win
addSessionId (追加) output_add_rewrite_var() でクエリにセッション ID を付加するか否か false
convertKana (追加) mb_convert_kana() で仮名・アルファベットの変換をするか否か
値が true のときは k が、文字列のときはこの値が mb_convert_kana() の変換オプションとなる
false
Piece_Unity_Plugin_Interceptor_MobileInputFilter
キー 説明 デフォルト値
inputCharset 入力 (変換元) エンコーディング SJIS-win
includeKeys 変換対象にするパラメータ名 array()
excludeKeys 変換対象から外すパラメータ名 array()

Piece_Unity_Plugin_Interceptor_GarbageCollection

  • 常にガーベッジコレクションが実行されてしまう恥ずかしいバグを修正
  • 設定ポイント名を変更 (removeEmptySubDirectory -> removeSubDir)
  • targetDirectory 設定ポイントの各要素ごとに lifeTime, recursionDepth, removeSubDir, byAccessTime を個別に設定できるようにした

piece-unity-config.yaml の記述例

- name: InterceptorChain
  point:
    - name: interceptors
      type: extension
      value:
        - Interceptor_NullByteAttackPreventation
        # Interceptor_MobileDetection は 他の Interceptor_Mobile*
        # および Interceptor_Authentication より先に読み込む
        - Interceptor_MobileDetection
        - Interceptor_SessionStart
        - Interceptor_Authentication
        - Interceptor_MobileRenderer
        - Interceptor_MobileInputFilter
        - Interceptor_MobileOutputFilter
        - Interceptor_GarbageCollection

- name: Interceptor_MobileDetection
  point:
    # strict と lazy を併用
    - name: lazy
      type: configuration
      value: true
    # 実運用とローカルテストの携帯用ホスト名
    - name: hostName
      type: configuration
      value: /^(i?.example?.org|example-i?.localhost)$/

- name: Interceptor_MobileRenderer
  point:
    # 携帯用レイアウト
    - name: layoutView
      type: configuration
      value: MobileLayout

- name: Interceptor_MobileInputFilter
  point:
    # mod_rewrite で PATH_INFO から QUERY_STRING にされた値を除外
    - name: excludeKeys
      type: configuration
      value:
        - rewrittenValue1
        - rewrittenValue2

- name: Interceptor_MobileOutputFilter
  point:
    # session.use_trans_sid = 1 と等価
    - name: addSessionId
      type: configuration
      value: true
    # カタカナ・アルファベット・数字・スペース等を半角に
    - name: convertKana
      type: configuration
      value: ask

- name: Interceptor_GarbageCollection
  point:
    # GC 対象ディレクトリ
    - name: targetDirectories
      type: configuration
      value:
        # パスのみを指定するとき
        - ../webapp/cache/temp1
        - ../webapp/cache/temp2
        # session.save_path がルート直下でない場合は
        # files セッションハンドラの GC が効かないので
        # Interceptor_GarbageCollection でクリーニングする
        # このディレクトリだけ lifeTime と byAccessTime 設定ポイントを上書き
        - path: ../webapp/sessions
          lifeTime: 3600
          byAccessTime: true
    # デフォルトの有効期限は30分
    - name: lifeTime
      type: configuration
      value: 1800

追記:修正

影響するのは PHP5 で Content-Type が text/ 以外で始まる場合だけですが、これはいただけない...

Index: Piece/Unity/Plugin/Interceptor/MobileOutputFilter.php
===================================================================
--- Piece/Unity/Plugin/Interceptor/MobileOutputFilter.php       (リビジョン 40)
+++ Piece/Unity/Plugin/Interceptor/MobileOutputFilter.php       (作業コピー)
@@ -62,7 +62,7 @@
         if (function_exists('headers_list')) {
             foreach (headers_list() as $header) {
                 if (preg_match($header, '/^Content-Type:(.+)/i', $mimetype)) {
-                    if (strncmp(trim($mimetype[0]), 'text/', 5) == 0) {
+                    if (strncmp(trim($mimetype[1]), 'text/', 5) == 0) {
                         break;
                     } else {
                         return $buffer;

追記:修正2

「always」から「never」になってただけという... orz

Index: Piece/Unity/Plugin/Interceptor/GarbageCollection.php
===================================================================
--- Piece/Unity/Plugin/Interceptor/GarbageCollection.php        (リビジョン 45)
+++ Piece/Unity/Plugin/Interceptor/GarbageCollection.php        (作業コピー)
@@ -43,7 +43,7 @@
             return false;
         }
 
-        if (count($targetDirectories) == 0 || mt_rand(1, $divisor) >= $probability) {
+        if (count($targetDirectories) == 0 || mt_rand(1, $divisor) > $probability) {
             return true;
         }