Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 88 lines (73 sloc) 2.35 kb
a315444 Ian Bicking All my initial work
authored
1 sys = require 'sys'
2 http = require 'http'
3 path = require 'path'
4 fs = require 'fs'
5 url = require 'url'
6 spawn = require('child_process').spawn
7
8 nullString = (new Buffer([0])).toString()
9
10 requestJSON = (req, func) ->
11 allData = ''
12 req.on 'data', (data) ->
13 allData += data
14 req.on 'end', () ->
15 reqBody = JSON.parse(allData)
16 func(reqBody)
17
18
19 jsonpRunProgram = (req, res) ->
20 params = url.parse(req.url, true).query
21 cwd = params.cwd
22 env = process.env
23 if params.env
24 for name, value of JSON.parse(params.env)
25 if value == null
26 delete env[name]
27 else
28 env[name] = value
29 args = if params.args then JSON.parse(params.args) else []
30 callback = params.callback
31 callbackId = if params.callbackId then JSON.parse(params.callbackId) else null
32 console.log('args', JSON.stringify([params, params.command, args, {cwd: cwd, env: params.env}]))
33 proc = spawn(params.command, args, {cwd: cwd, env: env})
34 res.writeHead(200, 'Content-Type': 'text/javascript')
add1c84 Ian Bicking trac pid better
authored
35 sendData = (data) ->
36 res.write(callback + '(' + JSON.stringify(callbackId) +
37 ', ' + JSON.stringify(data) + ')\n')
38 sendData(pid: proc.pid)
39 res.write('\n')
a315444 Ian Bicking All my initial work
authored
40 proc.stdout.on 'data', (data) ->
41 sendData(stdout: data.toString())
42 proc.stderr.on 'data', (data) ->
43 sendData(stderr: data.toString())
44 proc.on 'exit', (code, signal) ->
45 sendData(code: code)
46 res.end()
47
48
49 envProgram = (req, res) ->
50 res.writeHead(200, 'Content-Type': 'application/json')
51 res.end(JSON.stringify(process.env))
52
53
54 serveStatic = (req, res) ->
55 p = url.parse(req.url).pathname
56 if not p or p == "/"
57 p = "/index.html"
58 filename = path.join process.cwd(), 'static', p
59 path.exists filename, (exists) ->
60 if not exists
61 res.writeHead 404, {"Content-Type": "text/plain"}
62 res.end "404 Not Found\n"
63 return
64
65 fs.readFile filename, "binary", (err, file) ->
66 if err
67 res.writeHead 500, {"Content-Type": "text/plain"}
68 res.write err + "\n"
69 return
70
71 res.writeHead 200
72 res.end file, "binary"
73
74
75 mainServer = (req, res) ->
76 p = url.parse(req.url).pathname
77 if req.method == 'POST'
78 xhrRunProgram(req, res)
79 else if p == '/call'
80 jsonpRunProgram(req, res)
81 else if p == '/env'
82 envProgram(req, res)
83 else
84 serveStatic(req, res)
85
86
87 http.createServer(mainServer).listen 8000
Something went wrong with that request. Please try again.