Nhờ admin xem giùm:
- Upload ảnh xong không thấy hiện ra gì?
Khi viết flash application, bạn có thể gặp lỗi sau:
Error #2048: Security sandbox violation:…swf cannot load data from…
Với flash phiên bản mới hiện nay, lỗi trên có thể gặp ngay cả khi flash lấy dữ liệu từ cùng domain (vd: trường hợp flash lấy dữ liệu qua socket).
Định nghĩa file cross-domain policy file của adobe:
workflow
Như vậy flash từ domain a.com muốn lấy dữ liệu của b.com thì phải được b.com cho phép. Thông thường file cross-domain-policy.xml được đặt ở root của b.com.
Policy file xác định loại dữ liệu, xác định các domain được phép truy cập.
Trường hợp socket server hosts policy file: Trong trường hợp flash a.com lấy dữ liệu qua socket với server b.com, mỗi khi lấy dữ liệu, flash sẽ gửi một single node XML document chứa
Dưới đây là một mẫu code policy server nhặt được trong dự án game tá lả (có vấn đề đâu không chịu tránh nhiệm , có gì thì cứ đè đại ka ra ) )
Viết bằng perl:
#!/usr/bin/perl
#
# policyd.pl
# Simple socket policy file server
#
# Usage: policyd.pl [--port=N] –file=FILE
# Logs to stdout
#use strict;
use Socket;my $NULLBYTE = pack( ‘c’, 0 );
my $port = 843;
my $filePath;
my $content;### READ ARGS
while ( my $arg = shift @ARGV )
{
if ( $arg =~ m/^–port=(\d+)$/ )
{
$port = $1;
}
elsif ( $arg =~ m/^–file=(.*)/ )
{
$filePath = $1;
}
}unless ( $filePath )
{
die “Usage: policyd.pl [--port=N] –file=FILE\n”;
}### READ FILE
-f $filePath or die “No such file: ‘$filePath’\n”;
-s $filePath < 10_000 or die “File probably too large to be a policy file: ‘$filePath’\n”;local $/ = undef;
open POLICYFILE, “<$filePath” or die “Can’t open ‘$filePath’: $!\n”;
$content =;
close POLICYFILE;$content =~ m/cross-domain-policy/ or die “Not a valid policy file: ‘$filePath’\n”;
### BEGIN LISTENING
socket( LISTENSOCK, PF_INET, SOCK_STREAM, getprotobyname( ‘tcp’ ) ) or die “socket() error: $!”;
setsockopt( LISTENSOCK, SOL_SOCKET, SO_REUSEADDR, pack( ‘l’, 1 ) ) or die “setsockopt() error: $!”;
bind( LISTENSOCK, sockaddr_in( $port, INADDR_ANY ) ) or die “bind() error: $!”;
listen( LISTENSOCK, SOMAXCONN ) or die “listen() error: $!”;print STDOUT “\nListening on port $port\n\n”;
### HANDLE CONNECTIONS
while ( my $clientAddr = accept( CONNSOCK, LISTENSOCK ) )
{
my ( $clientPort, $clientIp ) = sockaddr_in( $clientAddr );
my $clientIpStr = inet_ntoa( $clientIp );
print STDOUT “Connection from $clientIpStr:$clientPort\n”;local $/ = $NULLBYTE;
my $request =;
chomp $request;if ( $request eq ‘
’ )
{
print STDOUT “Valid request received\n”;
}
else
{
print STDOUT “Unrecognized request: $request\n\n”;
close CONNSOCK;
next;
}print CONNSOCK $content;
print CONNSOCK $NULLBYTE;
close CONNSOCK;print STDOUT “Sent policy file\n\n”;
}# End of file.
Cái này bằng python:
#!/usr/bin/env python
#
# flashpolicyd.py
# Simple socket policy file server for Flash
#
# Usage: flashpolicyd.py [--port=N] –file=FILE
#
# Logs to stderr
# Requires Python 2.5 or laterfrom __future__ import with_statement
import sys
import optparse
import socket
import thread
import exceptions
import contextlibVERSION = 0.1
class policy_server(object):
def __init__(self, port, path):
self.port = port
self.path = path
self.policy = self.read_policy(path)
self.log(‘Listening on port %d\n’ % port)
try:
self.sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
except AttributeError:
# AttributeError catches Python built without IPv6
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
# socket.error catches OS with IPv6 disabled
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.bind((”, port))
self.sock.listen(5)
def read_policy(self, path):
with file(path, ‘rb’) as f:
policy = f.read(10001)
if len(policy) > 10000:
raise exceptions.RuntimeError(‘File probably too large to be a policy file’,
path)
if ‘cross-domain-policy’ not in policy:
raise exceptions.RuntimeError(‘Not a valid policy file’,
path)
return policy
def run(self):
try:
while True:
thread.start_new_thread(self.handle, self.sock.accept())
except socket.error, e:
self.log(‘Error accepting connection: %s’ % (e[1],))
def handle(self, conn, addr):
addrstr = ‘%s:%s’ % (addr[0],addr[1])
try:
self.log(‘Connection from %s’ % (addrstr,))
with contextlib.closing(conn):
# It’s possible that we won’t get the entire request in
# a single recv, but very unlikely.
request = conn.recv(1024).strip()
if request != ‘\0′:
self.log(‘Unrecognized request from %s: %s’ % (addrstr, request))
return
self.log(‘Valid request received from %s’ % (addrstr,))
conn.sendall(self.policy)
self.log(‘Sent policy file to %s’ % (addrstr,))
except socket.error, e:
self.log(‘Error handling connection from %s: %s’ % (addrstr, e[1]))
except Exception, e:
self.log(‘Error handling connection from %s: %s’ % (addrstr, e[1]))
def log(self, str):
print >>sys.stderr, strdef main():
parser = optparse.OptionParser(usage = ‘%prog [--port=PORT] –file=FILE’,
version=’%prog ‘ + str(VERSION))
parser.add_option(‘-p’, ‘–port’, dest=’port’, type=int, default=843,
help=’listen on port PORT’, metavar=’PORT’)
parser.add_option(‘-f’, ‘–file’, dest=’path’,
help=’server policy file FILE’, metavar=’FILE’)
opts, args = parser.parse_args()
if args:
parser.error(‘No arguments are needed. See help.’)
if not opts.path:
parser.error(‘File must be specified. See help.’)try:
policy_server(opts.port, opts.path).run()
except Exception, e:
print >> sys.stderr, e
sys.exit(1)
except KeyboardInterrupt:
passif __name__ == ‘__main__’:
main()
Một ví dụ về policy file (không nên dùng )
(i-php.net)