Mercurial > psgxs
annotate modules/mod_http.js @ 41:bc717575e66a
Much better handling of modules.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sun, 20 Feb 2011 15:51:12 +0100 |
parents | 62cbb1c49bc5 |
children | 023f767662d3 |
rev | line source |
---|---|
41
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
1 var sites = {}; |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
2 |
33
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
3 var http = require('http'); |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
4 |
41
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
5 var fs = require('fs'); |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
6 var dir = require('./configuration').pluginsDir; |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
7 var files = fs.readdirSync(dir + '/http'); |
33
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
8 var regex = /^mod_.*\.js/; |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
9 |
41
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
10 var Script = process.binding('evals').Script; |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
11 var sandbox = { |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
12 config: require('./configuration'), |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
13 xmpp: require('xmpp'), |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
14 errors: require('./errors'), |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
15 forms: require('./forms'), |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
16 makeError: require('./errors').makeError, |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
17 notifs: require('./notifs'), |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
18 NS: require('./namespaces'), |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
19 require: require, |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
20 storage: require('./storage'), |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
21 toBareJID: require('./fdsq').toBareJID, |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
22 exports: {} |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
23 }; |
33
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
24 |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
25 for (var i in files) { |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
26 var file = files[i]; |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
27 if (!regex.test(file)) |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
28 continue; |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
29 |
41
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
30 fs.readFile(dir + '/http/' + file, function(err, data) { |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
31 if (err) { |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
32 console.log('ERROR: loading http module “'+file+'”.'); // FIXME: file is always the last file of the list… |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
33 return; |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
34 } |
33
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
35 |
41
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
36 Script.runInNewContext(data, sandbox, dir + '/http/' + file); // FIXME: the same. |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
37 var module = sandbox.exports; |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
38 sandbox.exports = {}; |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
39 |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
40 for (var j in module) { |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
41 if (typeof sites[j] != 'undefined') |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
42 console.log('WARNING: module http/'+j+' already loaded.'); |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
43 |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
44 sites[j] = module[j]; |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
45 } |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
46 }); |
33
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
47 } |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
48 |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
49 http.createServer(function (req, res) { |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
50 var sent = false; |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
51 for (var i in sites) { |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
52 var site = sites[i]; |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
53 if (!site.url.test(req.url)) |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
54 continue; |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
55 |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
56 sent = site.func(req, res); |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
57 return; |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
58 } |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
59 if (!sent) { |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
60 res.writeHead(404, {'Content-Type': 'text/plain'}); |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
61 res.end('Error 404'); |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
62 } |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
63 }).listen(8484); |