|
| 1 | +/** |
| 2 | + * Copyright Robert Groh and other contributors |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 5 | + * this software and associated documentation files (the 'Software'), to deal in |
| 6 | + * the Software without restriction, including without limitation the rights to |
| 7 | + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 8 | + * the Software, and to permit persons to whom the Software is furnished to do so, |
| 9 | + * subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in all |
| 12 | + * copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 16 | + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 17 | + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 18 | + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 19 | + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 20 | + */ |
| 21 | + |
| 22 | +'use strict'; |
| 23 | + |
| 24 | +var path = require('path') |
| 25 | + , fcgiFramework = require('../index.js'); //this we want to test |
| 26 | + |
| 27 | +var port = 8080; |
| 28 | +var socketPath = path.join(__dirname, 'echoServer'); |
| 29 | +try { |
| 30 | + require('fs').unlinkSync(socketPath); |
| 31 | +} catch (err) { |
| 32 | + //ignore if file doesn't exists |
| 33 | + if(err.code !== 'ENOENT') { |
| 34 | + throw err; |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +function answerWithError(res, err) { |
| 39 | + res.writeHead(500, { 'Content-Type': 'text/plain; charset=utf-8', 'Content-Length': err.stack.length }); |
| 40 | + res.end(err.stack + '\n'); |
| 41 | +} |
| 42 | + |
| 43 | +fcgiFramework.createServer( |
| 44 | + function echo(req, res) { |
| 45 | + var requestData; |
| 46 | + |
| 47 | + req.on('data', function (data) { |
| 48 | + requestData = requestData + data; |
| 49 | + }); |
| 50 | + |
| 51 | + req.on('complete', function writeReqAsJson() { |
| 52 | + var echoData |
| 53 | + , size; |
| 54 | + |
| 55 | + try { |
| 56 | + var strippedRequest = require('lodash').omit(req, 'connection', 'buffer', 'socket', '_events', '_readableState', 'data'); |
| 57 | + strippedRequest.data = requestData; |
| 58 | + |
| 59 | + echoData = JSON.stringify(strippedRequest, null, 4); //hopefully only here will an error be thrown |
| 60 | + size = Buffer.byteLength(echoData, 'utf8'); |
| 61 | + res.writeHead( |
| 62 | + 200, |
| 63 | + { |
| 64 | + 'Content-Type': 'application/json; charset=utf-8', |
| 65 | + 'Content-Length': size |
| 66 | + } |
| 67 | + ); |
| 68 | + res.end(echoData); |
| 69 | + } catch (err) { |
| 70 | + answerWithError(res, err); |
| 71 | + } |
| 72 | + }); |
| 73 | + |
| 74 | + req.on('error', answerWithError.bind(undefined, res)); |
| 75 | + } |
| 76 | +).listen(socketPath, function cgiStarted(err) { |
| 77 | + console.log('cgi app listen on socket:' + socketPath); |
| 78 | + if (err) { |
| 79 | + throw err; |
| 80 | + } else { |
| 81 | + var http = require('http'); |
| 82 | + var fcgiHandler = require('fcgi-handler'); |
| 83 | + |
| 84 | + var server = http.createServer(function (req, res) { |
| 85 | + fcgiHandler.connect({path: socketPath}, function (err, fcgiProcess) { |
| 86 | + if (err) { |
| 87 | + throw err; |
| 88 | + } else { |
| 89 | + //route all request to fcgi application |
| 90 | + fcgiProcess.handle(req, res, {/*empty Options*/}); |
| 91 | + } |
| 92 | + }); |
| 93 | + }); |
| 94 | + server.listen(port); |
| 95 | + } |
| 96 | +}); |
| 97 | + |
| 98 | + |
| 99 | + |
0 commit comments