311
310
if (("path" in obj) && obj.path != null)
313
312
var path = urlencode_path(obj.path.toString());
314
if (url.length > 0 && path.length > 0 && path.charAt(0) != "/")
313
if (url.length > 0 && path.length > 0 && path[0] != "/")
315
314
path = "/" + path;
491
490
function pathlist_to_path(pathlist)
493
492
ret = path_join.apply(null, pathlist);
494
if (ret.charAt(ret.length-1) != '/')
493
if (ret[ret.length-1] != '/')
512
511
return make_path(path_join.apply(null, arguments));
515
/** Generates an absolute URL to a public application
517
function public_app_path(app /*,...*/)
519
return location.protocol + "//" + public_host
520
+ make_path(path_join.apply(null, arguments));
523
514
/** Given a path, gets the "basename" (the last path segment).
525
516
function path_basename(path)
606
/** Creates a random string of length length,
607
* consisting of alphanumeric characters.
609
var rand_chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZ"
610
+ "abcdefghiklmnopqrstuvwxyz";
611
function random_string(length)
613
var str = Array(length);
615
for (var i=0; i<length; i++)
617
v = Math.floor(Math.random() * rand_chars.length);
618
str[i] = rand_chars.charAt(v);
623
/** Makes an XMLHttpRequest call to the server.
597
/** Makes an asynchronous XMLHttpRequest call to the server.
624
598
* Sends the XMLHttpRequest object containing the completed response to a
625
599
* specified callback function.
627
601
* \param callback A callback function. Will be called when the response is
628
602
* complete. Passed 1 parameter, an XMLHttpRequest object containing the
629
* completed response. If callback is null this is a syncronous request
630
* otherwise this is an asynchronous request.
603
* completed response.
631
604
* \param app IVLE app to call (such as "fileservice").
632
605
* \param path URL path to make the request to, within the application.
633
606
* \param args Argument object, as described in parse_url and friends.
645
618
/* A random string, for multipart/form-data
646
619
* (This is not checked against anywhere else, it is solely defined and
647
620
* used within this function) */
648
var boundary = random_string(20);
621
var boundary = "48234n334nu7n4n2ynonjn234t683jyh80j";
649
622
var xhr = new_xmlhttprequest();
650
var asyncronous = callback != null;
653
xhr.onreadystatechange = function()
623
xhr.onreadystatechange = function()
625
if (xhr.readyState == 4)
655
if (xhr.readyState == 4)
661
630
if (method == "GET")
663
632
/* GET sends the args in the URL */
664
633
url = build_url({"path": path, "args": args});
665
634
/* open's 3rd argument = true -> asynchronous */
666
xhr.open(method, url, asyncronous);
635
xhr.open(method, url, true);
671
640
/* POST sends the args in application/x-www-form-urlencoded */
672
641
url = encodeURI(path);
673
xhr.open(method, url, asyncronous);
642
xhr.open(method, url, true);
675
644
if (content_type == "multipart/form-data")
677
646
xhr.setRequestHeader("Content-Type",
678
"multipart/form-data; boundary=" + boundary);
647
"multipart/form-data, boundary=" + boundary);
679
648
message = make_multipart_formdata(args, boundary);
683
652
xhr.setRequestHeader("Content-Type", content_type);
684
653
message = make_query_string(args);
655
xhr.setRequestHeader("Content-Length", message.length);
686
656
xhr.send(message);
688
/* Only return the XHR for syncronous requests */
695
/** Attempts to JSON decodes a response object
696
* If a non-200 response or the JSON decode fails then returns null
698
function decode_response(response)
700
if (response.status == 200)
704
var responseText = response.responseText;
705
return JSON.parse(responseText);