Projekt

Obecné

Profil

Stáhnout (3.13 KB) Statistiky
| Větev: | Revize:
1
// Generated by CoffeeScript 1.12.7
2
(function() {
3
  var JsonpReceiver, transport,
4
    extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
5
    hasProp = {}.hasOwnProperty;
6

    
7
  transport = require('./transport');
8

    
9
  JsonpReceiver = (function(superClass) {
10
    extend(JsonpReceiver, superClass);
11

    
12
    JsonpReceiver.prototype.protocol = "jsonp-polling";
13

    
14
    JsonpReceiver.prototype.max_response_size = 1;
15

    
16
    function JsonpReceiver(req, res, options, callback1) {
17
      this.callback = callback1;
18
      JsonpReceiver.__super__.constructor.call(this, req, res, options);
19
    }
20

    
21
    JsonpReceiver.prototype.doSendFrame = function(payload) {
22
      return JsonpReceiver.__super__.doSendFrame.call(this, "/**/" + this.callback + "(" + JSON.stringify(payload) + ");\r\n");
23
    };
24

    
25
    return JsonpReceiver;
26

    
27
  })(transport.ResponseReceiver);
28

    
29
  exports.app = {
30
    jsonp: function(req, res, _, next_filter) {
31
      var callback;
32
      if (!('c' in req.query || 'callback' in req.query)) {
33
        throw {
34
          status: 500,
35
          message: '"callback" parameter required'
36
        };
37
      }
38
      callback = 'c' in req.query ? req.query['c'] : req.query['callback'];
39
      if (/[^a-zA-Z0-9-_.]/.test(callback) || callback.length > 32) {
40
        throw {
41
          status: 500,
42
          message: 'invalid "callback" parameter'
43
        };
44
      }
45
      res.setHeader('X-Content-Type-Options', 'nosniff');
46
      res.setHeader('Content-Type', 'application/javascript; charset=UTF-8');
47
      res.writeHead(200);
48
      transport.register(req, this, new JsonpReceiver(req, res, this.options, callback));
49
      return true;
50
    },
51
    jsonp_send: function(req, res, query) {
52
      var d, i, jsonp, len, message, x;
53
      if (!query) {
54
        throw {
55
          status: 500,
56
          message: 'Payload expected.'
57
        };
58
      }
59
      if (typeof query === 'string') {
60
        try {
61
          d = JSON.parse(query);
62
        } catch (error) {
63
          x = error;
64
          throw {
65
            status: 500,
66
            message: 'Broken JSON encoding.'
67
          };
68
        }
69
      } else {
70
        d = query.d;
71
      }
72
      if (typeof d === 'string' && d) {
73
        try {
74
          d = JSON.parse(d);
75
        } catch (error) {
76
          x = error;
77
          throw {
78
            status: 500,
79
            message: 'Broken JSON encoding.'
80
          };
81
        }
82
      }
83
      if (!d || d.__proto__.constructor !== Array) {
84
        throw {
85
          status: 500,
86
          message: 'Payload expected.'
87
        };
88
      }
89
      jsonp = transport.Session.bySessionId(req.session);
90
      if (jsonp === null) {
91
        throw {
92
          status: 404
93
        };
94
      }
95
      for (i = 0, len = d.length; i < len; i++) {
96
        message = d[i];
97
        jsonp.didMessage(message);
98
      }
99
      res.setHeader('Content-Length', '2');
100
      res.setHeader('Content-Type', 'text/plain; charset=UTF-8');
101
      res.writeHead(200);
102
      res.end('ok');
103
      return true;
104
    }
105
  };
106

    
107
}).call(this);
(6-6/11)