platform/classes/Q/Request.js - Q Platform
Show:

File: platform/classes/Q/Request.js

  1. /**
  2. * @module Q
  3. */
  4. var Q = require('../Q');
  5. /**
  6. * @class Request
  7. * @namespace Q
  8. */
  9. var Request = {};
  10.  
  11. /**
  12. * Method for getting the app's canonical baseUrl from the config.
  13. * Note that this may not match the baseUrl for all PHP requests.
  14. */
  15. Request.baseUrl = function () {
  16. return Q.Config.expect(['Q', 'web', 'appRootUrl'])
  17. + Q.Config.get(['Q', 'web', 'controllerSuffix'], '');
  18. };
  19.  
  20. /**
  21. * Use this to determine what method to treat the request as.
  22. * @method method
  23. * @param {http.Request} req
  24. * @return {string} Returns an uppercase string such as "GET", "POST", "PUT", "DELETE"
  25. * See [Request methods](http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods)
  26. */
  27. Request.method = function (req) {
  28. var parts = require('url').parse(req.url, true);
  29. if (parts.query && parts.query["Q.method"]) {
  30. return parts.query["Q.method"].toUpperCase();
  31. }
  32. if (!req.method) {
  33. return 'GET';
  34. }
  35. return req.method.toUpperCase();
  36. };
  37.  
  38. /**
  39. * The names of slots that were requested, if any
  40. * @method slotNames
  41. * @param {http.Request} req
  42. * @param {boolean} [returnDefaults=false]If set to true, returns the array of slot names set in config field
  43. * named Q/response/$app/slotNames in the event that slotNames was not specified at all in the request.
  44. * @return {array}
  45. */
  46. Request.slotNames = function(req, returnDefaults) {
  47. var parts = require('url').parse(req.url, true);
  48. if (!parts.query || !parts.query["Q.slotNames"]) {
  49. if (!returnDefaults) {
  50. return null;
  51. }
  52. var app = Q.Config.expect(['Q', 'app']);
  53. return Q.Config.get(
  54. ['Q', 'response', 'slotNames'],
  55. ['content', 'dashboard', 'title', 'notices']
  56. );
  57. }
  58. var slotNames = parts.query["Q.slotNames"];
  59. if (typeof slotNames === 'string') {
  60. var result = [];
  61. var snp = slotNames.split(',');
  62. for (var i=0; i<snp.length; ++i) {
  63. result.push(snp[i]);
  64. }
  65. slotNames = result;
  66. }
  67. if (!slotNames || !slotNames.length) {
  68. return [];
  69. }
  70. return slotNames;
  71. }
  72.  
  73.  
  74. module.exports = Request;