xhrWindowNamePlugin.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.io.xhrWindowNamePlugin"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.io.xhrWindowNamePlugin"] = true;
  8. dojo.provide("dojox.io.xhrWindowNamePlugin");
  9. dojo.require("dojox.io.xhrPlugins");
  10. dojo.require("dojox.io.windowName");
  11. dojo.require("dojox.io.httpParse");
  12. dojo.require("dojox.secure.capability"); // would like to have a safe JSON verifier instead (more compact)
  13. dojox.io.xhrWindowNamePlugin = function(/*String*/url, /*Function?*/httpAdapter, /*Boolean?*/trusted){
  14. // summary:
  15. // Adds the windowName transport as an XHR plugin for the given site. See
  16. // dojox.io.windowName for more information on the transport.
  17. // url:
  18. // Url prefix of the site which can handle windowName requests.
  19. // httpAdapter: This allows for adapting HTTP requests that could not otherwise be
  20. // sent with window.name, so you can use a convention for headers and PUT/DELETE methods.
  21. dojox.io.xhrPlugins.register(
  22. "windowName",
  23. function(method,args){
  24. return args.sync !== true &&
  25. (method == "GET" || method == "POST" || httpAdapter) &&
  26. (args.url.substring(0,url.length) == url);
  27. },
  28. function(method,args,hasBody){
  29. var send = dojox.io.windowName.send;
  30. var load = args.load;
  31. args.load = undefined; //we don't want send to set this callback
  32. var dfd = (httpAdapter ? httpAdapter(send, true) : send)(method, args, hasBody); // use the windowName transport
  33. dfd.addCallback(function(result){
  34. var ioArgs = dfd.ioArgs;
  35. ioArgs.xhr = {
  36. getResponseHeader: function(name){
  37. // convert the hash to an object to act like response headers
  38. return dojo.queryToObject(ioArgs.hash.match(/[^#]*$/)[0])[name];
  39. }
  40. }
  41. // use the XHR content handlers for handling
  42. if(ioArgs.handleAs == 'json'){
  43. // use a secure json verifier, using object capability validator for now
  44. if(!trusted){
  45. dojox.secure.capability.validate(result,["Date"],{});
  46. }
  47. return dojo.fromJson(result);
  48. }
  49. return dojo._contentHandlers[ioArgs.handleAs || "text"]({responseText:result});
  50. });
  51. args.load = load;
  52. if(load){
  53. dfd.addCallback(load);
  54. }
  55. return dfd;
  56. }
  57. );
  58. };
  59. }