}
# Encode cookies before they're sent back to the user.
# The return value must only contain characters that are legal in cookie
# names and values, i.e. only printable characters, and no ";", ",", "=",
# or white space.
# cookie_encode() is called twice for each cookie: once to encode the cookie
# name, and once to encode the cookie value. The two are then joined with
# "=" and sent to the user.
# cookie_decode() must exactly undo whatever cookie_encode() does.
# Also, cookie_encode() must always encode a given input string into the
# same output string. This is because browsers need the cookie name to
# identify and manage a cookie, so the name must be consistent.
# This is not a bottleneck like proxy_encode() is, so speed is not critical.
# IMPORTANT: If you modify these routines, and if $PROXIFY_SCRIPTS is set
# below (on by default), then you MUST modify $ENCODE_DECODE_BLOCK_IN_JS
# below!! (You'll need to write corresponding routines in JavaScript to do
# the same as these routines in Perl, used when proxifying JavaScript.)
sub cookie_encode {
my($cookie)= @_ ;
# $cookie=~ s/(.)/ sprintf('%02x',ord($1)) /ge ; # each char -> 2-hex
# $cookie=~ tr/a-zA-Z/n-za-mN-ZA-M/ ; # rot-13
$cookie=~ s/(\W)/ '%' . sprintf('%02x',ord($1)) /ge ; # simple URL-encoding
return $cookie ;
}
sub cookie_decode {
my($enc_cookie)= @_ ;
$enc_cookie=~ s/%([\da-fA-F]{2})/ pack('C', hex($1)) /ge ; # URL-decode
# $enc_cookie=~ tr/a-zA-Z/n-za-mN-ZA-M/ ; # rot-13
# $enc_cookie=~ s/([\da-fA-F]{2})/ sprintf("%c",hex($1)) /ge ;
return $enc_cookie ;
}
# If $PROXIFY_SCRIPTS is true, and if you modify the routines above that
# encode cookies and URLs, then you need to modify $ENCODE_DECODE_BLOCK_IN_JS
# here. Explanation: When proxifying JavaScript, a library of JavaScript
# functions is used. In that library are a few JavaScript routines that do
# the same as their Perl counterparts in this script. Four of those routines
# are proxy_encode(), proxy_decode(), cookie_encode(), and cookie_decode().
# Thus, unfortunately, when you write your own versions of those Perl routines
# (or modify what's already there), you also need to write (or modify) these
# corresponding JavaScript routines to do the same thing. Put the routines in
# this long variable $ENCODE_DECODE_BLOCK_IN_JS, and it will be included in
# the JavaScript library when needed. Prefix the function names with
# "_proxy_jslib_", as below.
# The commented examples in the JavaScript routines below correspond exactly to
# the commented examples in the Perl routines above. Thus, if you modify the
# Perl routines by merely uncommenting the examples, you can do the same in
# these JavaScript routines. (JavaScript comments begin with "//".)
# [If you don't know Perl: Note that everything up until the line "EOB" is one
# long string value, called a "here document". $ENCODE_DECODE_BLOCK_IN_JS is
# set to the whole thing.]
# jsm-- String.charCodeAt not in MSIE 5.0.
# jsm-- String.replace() with function doesn't work in MSIE 5.0.
$ENCODE_DECODE_BLOCK_IN_JS= <<'EOB' ;
function _proxy_jslib_proxy_encode(URL) {
URL= URL.replace(/^([\w\+\.\-]+)\:\/\//, '$1/') ;
// URL= URL.replace(/(.)/g, function (s,p1) { return p1.charCodeAt(0).toString(16) } ) ;
// URL= URL.replace(/([a-mA-M])|[n-zN-Z]/g, function (s,p1) { return String.fromCharCode(s.charCodeAt(0)+(p1?13:-13)) }) ;
return URL ;
}
function _proxy_jslib_proxy_decode(enc_URL) {
// enc_URL= enc_URL.replace(/([a-mA-M])|[n-zN-Z]/g, function (s,p1) { return String.fromCharCode(s.charCodeAt(0)+(p1?13:-13))
}) ;
// enc_URL= enc_URL.replace(/([\da-fA-F]{2})/g, function (s,p1) { return String.fromCharCode(eval('0x'+p1)) } ) ;
enc_URL= enc_URL.replace(/^([\w\+\.\-]+)\//, '$1://') ;
return enc_URL ;
}
function _proxy_jslib_cookie_encode(cookie) {
// cookie= cookie.replace(/(.)/g, function (s,p1) { return p1.charCodeAt(0).toString(16) } ) ;
// cookie= cookie.replace(/([a-mA-M])|[n-zN-Z]/g, function (s,p1) { return String.fromCharCode(s.charCodeAt(0)+(p1?13:-13))
}) ;
cookie= cookie.replace(/(\W)/g, function (s,p1) { return '%'+p1.charCodeAt(0).toString(16) } ) ;
return cookie ;
}
function _proxy_jslib_cookie_decode(enc_cookie) {
enc_cookie= enc_cookie.replace(/%([\da-fA-F]{2})/g, function (s,p1) { return String.fromCharCode(eval('0x'+p1)) } ) ;
// enc_cookie= enc_cookie.replace(/([a-mA-M])|[n-zN-Z]/g, function (s,p1) { return String.fromCharCode(s.charCodeAt(0)+(p1?13:-13))
}) ;
// enc_cookie= enc_cookie.replace(/([\da-fA-F]{2})/g, function (s,p1) { return String.fromCharCode(eval('0x'+p1)) } ) ;
return enc_cookie ;
}
EOB
# Use @ALLOWED_SERVERS and @BANNED_SERVERS to restrict which servers a user
# can visit through this proxy. Any URL at a host matching a pattern in
# @BANNED_SERVERS will be forbidden. In addition, if @ALLOWED_SERVERS is
# not empty, then access is allowed *only* to servers that match a pattern
# in it. In other words, @BANNED_SERVERS means "ban these servers", and
# @ALLOWED_SERVERS (if not empty) means "allow only these servers". If a
=5= |