aboutsummaryrefslogtreecommitdiffstats
path: root/infrastructure/framework-src/modules/global/request.js
blob: a4327f9e1978838b2a9d6874a8d867fa0d927444 (plain) (blame)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/**
 * Copyright 2009 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS-IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import("stringutils.trim");
import("jsutils.scalaF0")

function _cx() { return appjet.context };

function _addIfNotPresent(obj, key, value) {
  if (!(key in obj)) obj[key] = value;
}

var request = {

get isDefined() {
  return (
    _cx() != null && 
    _cx().request() != null && 
    (! _cx().request().isFake()) && 
    _cx().request().req() != null
  );
},

get cache() {
  var req = _cx().request().req();
  if (req.getAttribute("jsCache") == null) {
    req.setAttribute("jsCache", {});
  }
  return req.getAttribute("jsCache");
},

get continuation() {
  if (this.isDefined) {
    var c = Packages.net.appjet.ajstdlib.execution.getContinuation(_cx());
    var u = this.underlying;
    return {
      suspend: function(timeout) { 
        return Packages.net.appjet.ajstdlib.execution.sync(
          u, scalaF0(function() { return c.suspend(timeout); }));
      },
      resume: function() { 
        Packages.net.appjet.ajstdlib.execution.sync(
          u, scalaF0(function() { c.resume(); })) 
      }
    }
  }
},

get underlying() {
  if (this.isDefined) {
    return _cx().request().req();
  }
},

/**
 * The request path following the hostname.  For example, if the user
 * is visiting yourapp.appjet.net/foo, then this will be set to
 * "/foo".
 *
 * This does not include CGI parameters or the domain name, and always
 * begins with a "/".
 *
 * @type string
 */
get path() {
  if (this.isDefined) {
    return String(_cx().request().path());
  }
},

/**
 * The value request query string.
 *
 * For example, if the user visits "yourapp.appjet.net/foo?id=20", then
 * query will be "id=20".
 *
 * @type string
 */
get query() {
  if (this.isDefined) {
    if (_cx().request().query() != null) {
      return _cx().request().query();
    }
  }
},

/**
 * The content of a POST request. Retrieving this value may interfere
 * with the ability to get post request parameters sent in the body of
 * a request via the "params" property. Use with care.
 *
 * @type string
 */
get content() {
  if (this.isDefined) {
    if (_cx().request().content() != null) {
      return _cx().request().content();
    }
  }
},

/**
 * Either "GET" or "POST" (uppercase).
 * @type string
 */
get method() {
  if (this.isDefined) {
    return String(_cx().request().method().toUpperCase());
  }
},

/**
 * Whether the curent HTTP request is a GET request.
 * @type boolean
 */
get isGet() {
  return (this.method == "GET");
},

/**
 * Whether the current HTTP request is a POST request.
 * @type boolean
 */
get isPost() {
  return (this.method == "POST");
},

/**
 * Either "http" or "https" (lowercase).
 * @type string
 */
get scheme() {
  if (this.isDefined) {
    return String(_cx().request().scheme());
  }
},

/**
 * Whether the current request arrived using HTTPS.
 * @type boolean
 */
get isSSL() {
  return (this.scheme == "https");
},

/**
 * Holds the IP address of the user making the request.
 * @type string
 */
get clientAddr() {
  if (this.isDefined) {
    return String(_cx().request().clientAddr());
  }
},

/**
 * Parameters associated with the request, either from the query string
 * or from the contents of a POST, e.g. from a form.  Parameters are accessible
 * by name as properties of this object.  The property value is either a
 * string (typically) or an array of strings (if the parameter occurs
 * multiple times in the request).
 *
 * @type object
 */
get params() {
  if (this.isDefined) {
    var cx = _cx();
    var req = cx.request();
    return cx.attributes().getOrElseUpdate("requestParams",
      scalaF0(function() { return req.params(cx.runner().globalScope()); }));
  }
},

/**
 * Uploaded files associated with the request, from the contents of a POST.
 * 
 * @type object
 */
get files() {
  if (this.isDefined) {
    var cx = _cx();
    var req = cx.request();
    return cx.attributes().getOrElseUpdate("requestFiles",
      scalaF0(function() { return req.files(cx.runner().globalScope()); }));
  }
},

/**
 * Used to access the HTTP headers of the current request.  Properties are
 * header names, and each value is either a string (typically) or an
 * array of strings (if the header occurs multiple times in the request).
 *
 * @example
print(request.headers["User-Agent"]);
 *
 * @type object
 */
get headers() {
  if (this.isDefined) {
    var cx = _cx();
    var req = cx.request();
    return cx.attributes().getOrElseUpdate("requestHeaders",
      scalaF0(function() { return req.headers(cx.runner().globalScope()); }));
  }
},

// TODO: this is super inefficient to do each time someone accesses
// request.cookies.foo.  We should probably store _cookies in the requestCache.
get cookies() {
  var _cookies = {};
  var cookieHeaderArray = this.headers['Cookie'];
  if (!cookieHeaderArray) { return {}; }
  if (!(cookieHeaderArray instanceof Array))
    cookieHeaderArray = [cookieHeaderArray];
  var name, val;

  cookieHeaderArray.forEach(function (cookieHeader) {
    cookieHeader.split(';').forEach(function(cs) {
      var parts = cs.split('=');
      if (parts.length == 2) {
	name = trim(parts[0]);
	val = trim(unescape(parts[1]));
	_addIfNotPresent(_cookies, name, val);
      }
    });
  });

  return _cookies;
},

/**
 * Holds the full URL of the request.
 */
get url() {
  if (this.isDefined) { 
    return this.scheme+"://"+this.host+this.path+(this.query ? "?"+this.query : "");
  }
},

get host() {
  if (this.isDefined) {
    // required by HTTP/1.1 to be present.
    return String(this.headers['Host']).toLowerCase();
  }
},

get domain() {
  if (this.isDefined) {
    // like host, but without the port if there is one.
    return this.host.split(':')[0];
  }
},

get uniqueId() {
  return String(_cx().executionId());
},

get protocol() {
  if (this.isDefined) {
    return String(_cx().request().protocol());
  }
},
  
get userAgent() {
  if (this.isDefined) {
    var agentString = (request.headers['User-Agent'] || "?");
    return {
      toString: function() { return agentString; },
      isIPhone: function() { return (agentString.indexOf("(iPhone;") > 0); }
    };
  }
},

get acceptsGzip() {
	if (this.isDefined) {
  	var headerArray = this.headers["Accept-Encoding"];
  	if (! (headerArray instanceof Array)) {
  		headerArray = [headerArray];
  	}
    // Want to see if some accept-encoding header OK's gzip.
    // Starting with: "Accept-Encoding: gzip; q=0.5, deflate; q=1.0"
    // 1. Split into ["gzip; q=0.5", "delfate; q=1.0"]
    // 2. See if some entry is gzip with q > 0. (q is optional.)
    return headerArray.some(function(header) {
      if (! header) return false;
      return header.split(/,\s*/).some(function(validEncoding) {
          if (!validEncoding.indexOf("gzip") == 0) {
              return false;
          }
          if (/q=[0\.]*$/.test(validEncoding)) {
              return false;
          }
          return true;
      });
    });
  }
}

}; // end: var request = {...