1//vim :set tabstop=2 shiftwidth=2 expandtab
2/*
3 *    I am placing this code in the Public Domain. Do with it as you will.
4 *    This software comes with no guarantees or warranties but with
5 *    plenty of well-wishing instead!
6 */
7
8function Supa() {
9    this.ping = function (supaApplet) {
10        try {
11            // IE will throw an exception if you try to access the method in a
12            // scalar context, i.e. if( supaApplet.pasteFromClipboard ) ...
13            return supaApplet.ping();
14        } catch (e) {
15            return false;
16        }
17    };
18
19    this.ajax_post = function (actionUrl, bytes, fieldname_filename, filename, params) {
20        // some constants for the request body
21        //FIXME: make sure boundaryString is not part of bytes or the form values
22        var boundaryString = 'AaB03x' + parseInt(Math.random() * 9999999, 10),
23            boundary = '--' + boundaryString,
24            cr = '\r\n',
25      body,
26      i,
27      isAsync,
28      xrequest;
29
30        // sanity checks
31        if (!fieldname_filename || fieldname_filename === "") {
32            throw "Developer Error: fieldname_filename not set or empty";
33        }
34
35        if (!filename || filename === "") {
36            throw "Filename required";
37        }
38
39        // build request body
40        body = '';
41        body += boundary + cr;
42
43        if (isArray(params)) {
44            for (i = 0; i < params.length; i += 1) {
45                body += "Content-disposition: form-data; name=\"" + escape(params[i].name) + "\";" + cr;
46                body += cr;
47                body += encodeURI(params[i].value) + cr;
48                body += boundary + cr;
49            }
50        }
51
52        // add the screenshot as a file
53        //FIXME: is this the correct encoding?
54        body += "Content-Disposition: form-data; name=\"" + escape(fieldname_filename) + "\"; filename=\"" + encodeURI(filename) + "\"" + cr;
55        body += "Content-Type: application/octet-stream" + cr;
56        body += "Content-Transfer-Encoding: base64" + cr;
57        body += cr;
58        body += bytes + cr;
59        // last boundary, no extra cr here!
60        body += boundary + "--" + cr;
61
62        //alert( body );
63        // finally, the Ajax request
64        isAsync = false;
65        xrequest = new XMLHttpRequest();
66        xrequest.open("POST", actionUrl, isAsync);
67
68        // set request headers
69        // please note: chromium needs charset set explicitly.
70        //   It will autocomplete if it's missing but it won't work
71        //   (PHP backend only)?
72        // also: chromium considers setting Content-length and Connection unsafe
73        // this is no problem as all browsers seem to determine this automagically.
74        xrequest.setRequestHeader("Content-Type", "multipart/form-data; charset=UTF-8; boundary=" + boundaryString);
75        //xrequest.setRequestHeader( "Content-length", body.length );
76        //xrequest.setRequestHeader( "Connection", "close" );
77        xrequest.send(body);
78
79        return xrequest.responseText;
80    };
81}
82
83function supa() {
84    return new Supa();
85}
86
87