Revision d2535f7c

b/webodf/flashput/Makefile
1
all:
2
	/tmp/flex/bin/mxmlc PUT.as
b/webodf/flashput/PUT.as
1
package {
2
	import flash.display.Sprite;
3
	import flash.display.LoaderInfo;
4
	import flash.external.ExternalInterface;
5
	public class PUT extends Sprite {
6
		public function PUT() {
7
			ExternalInterface.addCallback("put", this.put);
8
			var callback:String = LoaderInfo(root.loaderInfo).parameters["readyCallback"];
9
			ExternalInterface.call(callback);
10
		}
11
		public function put(host:String, port:uint, path:String,
12
				data:String, callbackFunctionName:String,
13
                                callbackId:String):void {
14
			new Request(host, port, path, data,
15
					callbackFunctionName, callbackId);
16
		}
17
	}
18
}
b/webodf/flashput/Request.as
1
package {
2
	import flash.events.*;
3
	import flash.external.ExternalInterface;
4
	import flash.net.Socket;
5
	import flash.utils.ByteArray;
6
	import mx.utils.Base64Decoder;
7
	import mx.utils.Base64Encoder;
8
	internal class Request {
9
		private var id:String;
10
		private var header:String;
11
		private var data:ByteArray;
12
		private var callbackName:String;
13
		private var socket:Socket;
14
		public function Request(host:String, port:uint, path:String,
15
                                data:String, callbackName:String,
16
                                callbackId:String) {
17
			var atob:Base64Decoder = new Base64Decoder();
18
			atob.decode(data);
19
			this.data = atob.toByteArray();
20
			this.header = 
21
				"PUT " + path + " HTTP/1.1\r\n" +
22
				"Host: " + host + "\r\n" +
23
				"Content-Length: " + this.data.length + "\r\n" +
24
				"\r\n";
25
			this.callbackName = callbackName;
26
			this.id = callbackId;
27
			this.socket = new Socket();
28
			this.socket.timeout = 1000;
29
			this.socket.addEventListener(Event.CONNECT,
30
					connectHandler);
31
			this.socket.addEventListener(IOErrorEvent.IO_ERROR,
32
					ioErrorHandler);
33
			this.socket.addEventListener(
34
					SecurityErrorEvent.SECURITY_ERROR,
35
					securityErrorHandler);
36
			this.socket.addEventListener(Event.CLOSE, closeHandler);
37
			this.socket.addEventListener(ProgressEvent.SOCKET_DATA,
38
					dataHandler); 
39
			this.socket.connect(host, port);
40
		}
41
		private function connectHandler(connect:Event):void {
42
			this.socket.writeBytes(toByteArray(this.header));
43
			this.socket.writeBytes(this.data);
44
			this.socket.flush();
45
			this.header = null;
46
			this.data.length = 0;
47
		}
48
		private function ioErrorHandler(ioError:IOErrorEvent):void {
49
			ExternalInterface.call(this.callbackName, this.id,
50
					null);
51
			this.socket.close();
52
		}
53
		private function securityErrorHandler(
54
				securityError:SecurityErrorEvent):void {
55
			ExternalInterface.call(this.callbackName, this.id,
56
					null);
57
			this.socket.close();
58
		}
59
		private function closeHandler(close:Event):void {
60
			ExternalInterface.call(this.callbackName, this.id,
61
					null);
62
		}
63
		private function dataHandler(data:ProgressEvent):void {
64
			// assume it went well, TODO: parse reply, handle error
65
			this.socket.readBytes(this.data);
66
			var btoa:Base64Encoder = new Base64Encoder();
67
			btoa.encodeBytes(this.data);
68
			ExternalInterface.call(this.callbackName, this.id,
69
					btoa.toString());
70
			this.data.length = 0;
71
			this.socket.close();
72
		}
73
		public function toByteArray(str:String):ByteArray {
74
			var length:uint = str.length;
75
			var i:uint = 0;
76
			var data:ByteArray = new ByteArray();
77
			for (i; i < length; i++) {
78
				data.writeByte(str.charCodeAt(i) & 0xff);
79
			}
80
			return data;
81
		}
82
	}
83
}
b/webodf/flashput/test.html
1
<html>
2
	<head>
3
		<title>hi</title>
4
	</head>
5
	<body onload="init()">
6
		<script type="text/javascript">
7
/**
8
 * @constuctor
9
 */
10
function FlashPut(host, port, domelement) {
11
    if (port <= 0) {
12
        return;
13
    }
14
    var flashput = this,
15
        ready = false,
16
        id = "FlashPut" + String(Math.random()).substring(2),
17
        jobnumber = 0,
18
        queue = [],
19
        runningCalls = {},
20
        doc = domelement.ownerDocument,
21
        flash = doc.createElement("object"),
22
        param = doc.createElement("param");
23
    flash.setAttribute("data", "PUT.swf");
24
    flash.setAttribute("type", "application/x-shockwave-flash");
25
    flash.setAttribute("width", "1");
26
    flash.setAttribute("height", "1");
27
    param.setAttribute("name", "allowNetworking");
28
    param.setAttribute("value", "all");
29
    flash.appendChild(param);
30
    param = doc.createElement("param");
31
    param.setAttribute("name", "allowScriptAccess");
32
    param.setAttribute("value", "always");
33
    flash.appendChild(param);
34
    param.setAttribute("name", "FlashVars");
35
    param.setAttribute("value", "readyCallback=" + id);
36
    flash.appendChild(param);
37
    domelement.appendChild(flash);
38

  
39
    // function to be called when flash movie is ready
40
    window[id] = function () {
41
        window[id] = undefined;
42
        ready = true;
43
        var i, a;
44
        for (i = 0; i < queue.length; i += 1) {
45
            a = queue[i];
46
            put(a[0], a[1], a[2]);
47
        }
48
        queue = undefined;
49
    };
50
    function put(path, data, callback) {
51
        var id = Math.random();
52
        runningCalls[id] = callback;
53
        flash.put(host, port, path, data, "FlashPut.putCallback", id);
54
    }
55
    this.put = function (path, data, callback) {
56
        if (!ready) {
57
            queue.push([path, data, callback]);
58
        } else {
59
            put(path, data, callback);
60
        }
61
    }
62
    FlashPut.putCallback = function (id, result) {
63
        if (id in runningCalls) {
64
            runningCalls[id](result);
65
            runningCalls.remove(id);
66
        } else {
67
            console.log("running put call " + id + " was not found.");
68
        }
69
    }
70
};
71

  
72
function testFlash() {
73
    var i, l = 1000000, str = "", now = new Date();
74
    for (i = 0; i < l; i += 1) {
75
        str += String.fromCharCode(i & 0xff);
76
    }
77
    now = new Date();
78
    str = window.btoa(str);
79
    flashput.put("/zz", str, function (result) {
80
        alert("'" + result + "' " + now.toString() + " " + (new Date()).toString());
81
	var z = "hmm";
82
        try {
83
            z = window.atob(result);
84
        } catch (e) {
85
        }
86
        alert(z + " " + now.toString() + " " + (new Date()).toString());
87
    });
88
}
89
var flashput;
90
function init() {
91
    flashput = new FlashPut("", 8123, document.body);
92
    testFlash();
93
}
94
		</script>
95
	</body>
96
</html>

Also available in: Unified diff