blib/lib/Apache/Filter/HanConvert.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 # $File: //member/autrijus/Apache-Filter-HanConvert/HanConvert.pm $ $Author: autrijus $
2 # $Revision: #4 $ $Change: 2690 $ $DateTime: 2002/12/12 06:47:15 $
3  
4 package Apache::Filter::HanConvert;
5 $Apache::Filter::HanConvert::VERSION = '0.02';
6  
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Apache::Filter::HanConvert - Filter between Chinese variant and encodings
13  
14 =head1 VERSION
15  
16 This document describes version 0.02 of Apache::Filter::HanConvert, released
17 December 12, 2002.
18  
19 =head1 SYNOPSIS
20  
21 In F<httpd.conf>:
22  
23 PerlModule Apache::Filter::HanConvert
24 PerlOutputFilterHandler Apache::Filter::HanConvert
25 PerlSetVar HanConvertFromVariant "traditional"
26  
27 =head1 DESCRIPTION
28  
29 This module utilizes the B<Encode::HanConvert> module with B<Apache2>'s
30 output filtering mechanism, to provide a flexible and customizable
31 solution for serving multiple encoding/variants from the same source
32 documents.
33  
34 From the settings in L</SYNOPSIS>, the server would negotiate with the
35 client's browser about their C<Accept-Language> preference (C<zh-cn> and
36 C<zh> means Simplified, other C<zh-*> means Traditional), as well as the
37 preferred C<Accept-Charset> setting (defaults to C<utf8> if nothing
38 was explicitly specified).
39  
40 The C<Content-Type> header will be rewritten to reflect the final
41 encoding used.
42  
43 If you want to use other encodings, try adding these lines:
44  
45 PerlSetVar HanConvertFromEncoding "UTF-8"
46 PerlSetVar HanConvertToEncodingTraditional "big5"
47 PerlSetVar HanConvertToEncodingSimplified "gbk"
48  
49 Finally, if you'd like to dictate it to always convert to a specific
50 variant/encoding, use this:
51  
52 PerlSetVar HanConvertToVariant "simplified"
53 PerlSetVar HanConvertToEncoding "gbk"
54  
55 =head1 CAVEATS
56  
57 The C<HanConvertFromEncoding> config probably could take multiple
58 encodings and apply L<Encode::Guess> to find out the correct source
59 encoding.
60  
61 Currently this module does not work with C<mod_dir>, so the server's
62 C<DirectoryIndex> setting won't be honored. Patches welcome!
63  
64 =cut
65  
66 1 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
67 1 ▶use Encode::HanConvert 0.10 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 0 ▶use Apache2 ();
Subroutine BEGIN: called CC=1 SCAR=2.6
70 use Apache::Filter ();
71 use Apache::RequestRec ();
72  
73 use APR::Brigade ();
74 use APR::Bucket ();
75  
76 use Apache::Const -compile => qw(OK DECLINED);
77 use APR::Const -compile => ':common';
78  
79 my %variants = (
80 'TS' => 'trad-simp',
81 'ST' => 'simp-trad',
82 'XS' => 'trad-simp',
83 'XT' => 'simp-trad',
84 );
85  
86 my %encodings = (
87 'T' => 'HanConvertToEncodingTraditional',
88 'S' => 'HanConvertToEncodingSimplified',
89 );
90  
91 my %charsets = (
92 'T' => qr{
93 big-?5 |
94 big5-?et(:en)? |
95 (?:tca|tw)[-_]?big5 |
96 big5-?hk(?:scs)? |
97 hk(?:scs)?[-_]?big5 |
98 MacChineseTrad |
99 cp950 |
100 (?:x-)winddows-950 |
101 (?:cmex[-_]|tw[-_])?big5-?e(?:xt)? |
102 (?:cmex[-_]|tw[-_])?big5-?p(?:lus)? |
103 (?:cmex[-_]|tw[-_])?big5\+ |
104 (?:ccag[-_])?cccii |
105 euc[-_]tw |
106 tw[-_]euc |
107 utf-?8 |
108 ucs-?2[bl]e |
109 utf-?(?:16|32)(?:[bl]e)?
110 }x,
111 'S' => qr{
112 euc[-_]cn |
113 cn[-_]euc |
114 iso-ir-165 |
115 MacChineseSimp |
116 cp936 |
117 (?:x-)winddows-936 |
118 hz |
119 gb[-_ ]?2312(?:\D+)? |
120 gb[-_ ]?18030 |
121 utf-?8 |
122 ucs-?2[bl]e |
123 utf-?(?:16|32)(?:[bl]e)?
124 }x
125 );
126  
127 sub Apache::Filter::HanConvert::handler {
128 my($filter, $bb) = @_;
129  
130 my $r = $filter->r;
131 my $content_type = $r->content_type;
132  
133 return Apache::DECLINED
134 if defined( $content_type ) and $content_type !~ m|^text/|io;
135  
136 my $from_variant = uc(substr($r->dir_config("HanConvertFromVariant"), 0, 1)) || 'X';
137 my $from_encoding = $r->dir_config("HanConvertFromEncoding");
138 my $to_variant = uc(substr($r->dir_config("HanConvertToVariant"), 0, 1));
139 my $to_encoding = $r->dir_config("HanConvertToEncoding");
140  
141 if (!$to_variant) {
142 my $langs = $r->headers_in->get('Accept-Language');
143  
144 $to_variant = (($1 and $1 ne 'cn') ? 'T' : 'S')
145 if $langs =~ /\bzh(?:-(tw|cn|hk|sg))?\b/;
146 }
147  
148 return Apache::DECLINED unless $to_variant;
149  
150 $to_encoding ||= $r->dir_config($encodings{$to_variant});
151
152 if (!$to_encoding) {
153 my $chars = $r->headers_in->get('Accept-Charset');
154  
155 $to_encoding = $1
156 if $chars =~ /\b($charsets{$to_variant})\b/i;
157 }
158  
159 my $var_enc = $variants{"$from_variant$to_variant"} || 'utf8';
160 $from_encoding = Encode::resolve_alias($from_encoding) || 'utf8';
161 $to_encoding = Encode::resolve_alias($to_encoding) || 'utf8';
162  
163 return Apache::DECLINED if $from_encoding eq $to_encoding
164 and $from_variant eq $to_variant;
165  
166 my $charset = ($to_encoding eq 'utf8' ? 'utf-8' : $to_encoding);
167 $content_type =~ s/(?:;charset=[^;]+(.*))?$/;charset=$charset$1/;
168 $r->content_type($content_type);
169  
170 my $c = $filter->c;
171 my $bb_ctx = APR::Brigade->new($c->pool, $c->bucket_alloc);
172 my $data = '';
173  
174 while (!$bb->empty) {
175 my $bucket = $bb->first;
176  
177 $bucket->remove;
178  
179 if ($bucket->is_eos) {
180 $bb_ctx->insert_tail($bucket);
181 last;
182 }
183  
184 my $buffer;
185 my $status = $bucket->read($buffer);
186 return $status unless $status == APR::SUCCESS;
187  
188 Encode::from_to($buffer, $from_encoding => 'utf8', Encode::FB_HTMLCREF)
189 if $from_encoding ne 'utf8';
190  
191 if ($var_enc eq $to_encoding) {
192 $bucket = APR::Bucket->new( $buffer );
193 }
194 elsif ($data .= $buffer) {
195 $bucket = APR::Bucket->new( Encode::encode(
196 $to_encoding, Encode::decode($var_enc, $data, Encode::FB_QUIET)
197 ) );
198 }
199  
200 $bb_ctx->insert_tail($bucket);
201 }
202  
203 my $rv = $filter->next->pass_brigade($bb_ctx);
204 return $rv unless $rv == APR::SUCCESS;
205  
206 Apache::OK;
207 }
208  
209 1;
210  
211 __END__
↑ Summary