Mercurial > hg > orthanc
diff Resources/Samples/Lua/CallWebService.js @ 1055:6f923d52a46c
call Web services from Lua
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 24 Jul 2014 11:37:02 +0200 |
parents | |
children | 6e7e5ed91c2d |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Resources/Samples/Lua/CallWebService.js Thu Jul 24 11:37:02 2014 +0200 @@ -0,0 +1,80 @@ +/** + * Orthanc - A Lightweight, RESTful DICOM Store + * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, + * Belgium + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + **/ + + +/** + * This file is a simple echo Web service implemented using + * "node.js". Whenever it receives a POST HTTP query, it echoes its + * body both to stdout and to the client. Credentials are checked. + **/ + + +// Parameters of the ECHO server +var port = 8000; +var username = 'alice'; +var password = 'alicePassword'; + + +var http = require('http'); +var authorization = 'Basic ' + new Buffer(username + ':' + password).toString('base64') + +var server = http.createServer(function(req, response) { + // Check the credentials + if (req.headers.authorization != authorization) + { + console.log('Bad credentials, access not allowed'); + response.writeHead(401); + response.end(); + return; + } + + switch (req.method) + { + case 'POST': + { + var body = ''; + + req.on('data', function (data) { + response.write(data); + body += data; + }); + + req.on('end', function () { + console.log('Message received: ' + body); + response.end(); + }); + + break; + } + + default: + console.log('Method ' + req.method + ' is not supported by this ECHO Web service'); + response.writeHead(405, {'Allow': 'POST'}); + response.end(); + } +}); + +server.listen(port);