~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to www/media/common/util.js

  • Committer: wagrant
  • Date: 2008-07-14 01:31:14 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:851
Give CGIRequest an exception handler which turns any exception into
HTTP headers. interpret reads these, and raises an IVLEJailError, which
dispatch should eventually look at.

This is now enabled in serveservice.

Show diffs side-by-side

added added

removed removed

Lines of Context:
512
512
    return make_path(path_join.apply(null, arguments));
513
513
}
514
514
 
515
 
/** Same as app_path but creates a properly-escaped site-relative URL.
516
 
 */
517
 
function app_url(app /*,...*/)
518
 
{
519
 
    return urlencode_path(app_path.apply(null, arguments));
520
 
}
521
 
 
522
515
/** Generates an absolute URL to a public application
523
516
 */
524
 
function public_app_url(app /*,...*/)
 
517
function public_app_path(app /*,...*/)
525
518
{
526
 
    return "http://" + public_host + app_url.apply(null, arguments);
 
519
    return location.protocol + "//" + public_host
 
520
        + make_path(path_join.apply(null, arguments));
527
521
}
528
522
 
529
523
/** Given a path, gets the "basename" (the last path segment).
544
538
    return str.substr(str.length - substring.length) == substring;
545
539
}
546
540
 
 
541
/** Equivalent to Python's repr.
 
542
 * Gets the JavaScript string representation.
 
543
 * Actually just calls JSON.stringify.
 
544
 */
 
545
function repr(str)
 
546
{
 
547
    return JSON.stringify(str);
 
548
}
 
549
 
547
550
/** Removes all occurences of a value from an array.
548
551
 */
549
552
Array.prototype.removeall = function(val)
617
620
    return str.join('');
618
621
}
619
622
 
620
 
/** Makes an XMLHttpRequest call to the server.
 
623
/** Makes an asynchronous XMLHttpRequest call to the server.
621
624
 * Sends the XMLHttpRequest object containing the completed response to a
622
625
 * specified callback function.
623
626
 *
624
627
 * \param callback A callback function. Will be called when the response is
625
628
 *      complete. Passed 1 parameter, an XMLHttpRequest object containing the
626
 
 *      completed response. If callback is null this is a syncronous request 
627
 
 *      otherwise this is an asynchronous request.
 
629
 *      completed response.
628
630
 * \param app IVLE app to call (such as "fileservice").
629
631
 * \param path URL path to make the request to, within the application.
630
632
 * \param args Argument object, as described in parse_url and friends.
631
 
 * \param method String; "GET", "POST", "PUT", or "PATCH"
632
 
 * \param content_type String, optional.
 
633
 * \param method String; "GET" or "POST"
 
634
 * \param content_type String, optional. Only applies if method is "POST".
 
635
 *      May be "application/x-www-form-urlencoded" or "multipart/form-data".
633
636
 *      Defaults to "application/x-www-form-urlencoded".
634
637
 */
635
638
function ajax_call(callback, app, path, args, method, content_type)
636
639
{
637
 
    if (!content_type)
 
640
    if (content_type != "multipart/form-data")
638
641
        content_type = "application/x-www-form-urlencoded";
639
642
    path = app_path(app, path);
640
643
    var url;
643
646
     * used within this function) */
644
647
    var boundary = random_string(20);
645
648
    var xhr = new_xmlhttprequest();
646
 
    var asyncronous = callback != null;
647
 
    if (asyncronous)
648
 
    {
649
 
        xhr.onreadystatechange = function()
 
649
    xhr.onreadystatechange = function()
 
650
        {
 
651
            if (xhr.readyState == 4)
650
652
            {
651
 
                if (xhr.readyState == 4)
652
 
                {
653
 
                    callback(xhr);
654
 
                }
 
653
                callback(xhr);
655
654
            }
656
 
    }
 
655
        }
657
656
    if (method == "GET")
658
657
    {
659
658
        /* GET sends the args in the URL */
660
659
        url = build_url({"path": path, "args": args});
661
660
        /* open's 3rd argument = true -> asynchronous */
662
 
        xhr.open(method, url, asyncronous);
 
661
        xhr.open(method, url, true);
663
662
        xhr.send(null);
664
663
    }
665
664
    else
666
665
    {
667
 
        /* POST & PUT & PATCH sends the args in the request body */
668
 
        url = urlencode_path(path);
669
 
        xhr.open(method, url, asyncronous);
 
666
        /* POST sends the args in application/x-www-form-urlencoded */
 
667
        url = encodeURI(path);
 
668
        xhr.open(method, url, true);
670
669
        var message;
671
670
        if (content_type == "multipart/form-data")
672
671
        {
674
673
                "multipart/form-data; boundary=" + boundary);
675
674
            message = make_multipart_formdata(args, boundary);
676
675
        }
677
 
        else if (content_type == "application/x-www-form-urlencoded")
 
676
        else
678
677
        {
679
678
            xhr.setRequestHeader("Content-Type", content_type);
680
679
            message = make_query_string(args);
681
680
        }
682
 
        else if (content_type == "application/json")
683
 
        {
684
 
            xhr.setRequestHeader("Content-Type", content_type);
685
 
            message = JSON.stringify(args);
686
 
        }
687
 
        else
688
 
        {
689
 
            xhr.setRequestHeader("Content-Type", content_type);
690
 
            message = args;
691
 
        }
692
681
        xhr.send(message);
693
682
    }
694
 
    /* Only return the XHR for syncronous requests */
695
 
    if (!asyncronous)
696
 
    { 
697
 
        return xhr;
698
 
    }
699
683
}
700
684
 
701
 
/** Attempts to JSON decodes a response object
702
 
 * If a non-200 response or the JSON decode fails then returns null
703
 
 */
704
 
function decode_response(response)
705
 
{
706
 
    if (response.status == 200)
707
 
    {
708
 
        try
709
 
        {
710
 
            var responseText = response.responseText;
711
 
            return JSON.parse(responseText);
712
 
        }
713
 
        catch (e)
714
 
        {
715
 
            // Pass
716
 
        }
717
 
     }
718
 
    
719
 
     return null;
720
 
}