summaryrefslogtreecommitdiff
path: root/lib/jqueryRest.js
blob: 84a9731c7b7fa4e01bbea46c59fa9561754e309e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
jqueryRest = function() {

  function getUrl()
  {
    return "";
  }

  function paths(path)
  {
    return path
  }

  // Perform ajax requests at the specified path, with the specified parameters
  // Calling back the specified function.
  function ajaxRequest(relative_path, params, callback)
  {
    var full_url = this.geturl() + this.paths(relative_path);
    if( window.XMLHttpRequest)
      xhr = new XMLHttpRequest();
    else if (window.ActiveXObject){
      try{
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
      }catch (e){}
    }
    else
    {
      session.error_message("Ajax not supported.");
      return;
    }
    if(xhr){
      xhr.onreadystatechange = function() {
        if(xhr.readyState == 4 && xhr.status == 200) {
          callback(parseResponse());
        }
      };
      xhr.open("POST", full_url, true);
      xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xhr.setRequestHeader("Content-length", params.length);
      xhr.send(params);
    }
    else
    {
      session.error_message("Ajax failed.");
    }        
  };

  function parseResponse() {
    if (responseIsXML()) {
      return parseXML(xhr.responseXML);
    } else if (responseIsJSON()) {
      return JSON.parse(xhr.responseText);
    } 
  };

  function responseIsXML() {
    return (xhr.responseType == 'document') || 
           (xhr.getResponseHeader("Content-Type").indexOf('application/xml') >= 0)
  }

  function responseIsJSON() {
    return (xhr.responseType == 'json') || 
           (xhr.getResponseHeader("Content-Type").indexOf('application/json') >= 0)
  }

  function parseXML(xml) {
    if (xml.getElementsByTagName("r").length > 0) {
      return parseAttributesOfElement(xml.getElementsByTagName("r")[0]);
    } else {
      return parseNodes(xml.childNodes);
    }
  };

  function parseAttributesOfElement(elem) {
    var response = {};
    for (var i = 0; i < elem.attributes.length; i++) {
      var attrib = elem.attributes[i];
      if (attrib.specified) {
        response[attrib.name] = attrib.value;
      }
    }
    return response;
  };

  function parseNodes(nodes) {
    var response = {};
    for (var i = 0; i < nodes.length; i++) {
      var node = nodes[i];
      response[node.tagName] = node.textContent || true;
    }
    return response;
  };

  // we do not fetch the salt from the server
  function register(session, callback)
  {
    callback({salt: session.getSalt()});
  }

  function sendVerifier(session, callback) {
    this.ajaxRequest("users", "user[login]=" + session.getI() +
      "&user[password_salt]=" + session.getSalt() +
      "&user[password_verifier]=" + session.getV().toString(16), callback);
  }

  function handshake(I, Astr, callback) {
    this.ajaxRequest("handshake/", "I="+I+"&A="+Astr, callback);
  }

  function authenticate(M, callback) {
    this.ajaxRequest("authenticate/", "M="+M, callback);
  }

  function upgrade(M, callback) {
    this.ajaxRequest("upgrade/authenticate/", "M="+M, callback);
  }

  return {
    geturl: getUrl,
    paths: paths,
    ajaxRequest: ajaxRequest,
    register: register,
    register_send_verifier: sendVerifier,
    handshake: handshake,
    authenticate: authenticate,
    upgrade: upgrade
  }
}