Mercurial > hg > orthanc
annotate Resources/Samples/Lua/CallWebService.js @ 2131:bb199bccdc45
reorganization
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 08 Nov 2016 10:49:45 +0100 |
parents | b1291df2f780 |
children | a3a65de1840f |
rev | line source |
---|---|
1055
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
1 /** |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
2 * Orthanc - A Lightweight, RESTful DICOM Store |
1900 | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1055
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
1055
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
5 * |
1375
3d76e26b3865
fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
6 * This program is free software: you can redistribute it and/or |
3d76e26b3865
fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
7 * modify it under the terms of the GNU General Public License as |
3d76e26b3865
fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
8 * published by the Free Software Foundation, either version 3 of the |
3d76e26b3865
fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
9 * License, or (at your option) any later version. |
3d76e26b3865
fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
10 * |
3d76e26b3865
fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
11 * This program is distributed in the hope that it will be useful, but |
3d76e26b3865
fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
12 * WITHOUT ANY WARRANTY; without even the implied warranty of |
3d76e26b3865
fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3d76e26b3865
fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
14 * General Public License for more details. |
1055
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
15 * |
1375
3d76e26b3865
fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
16 * You should have received a copy of the GNU General Public License |
3d76e26b3865
fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
1055
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
18 **/ |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
19 |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
20 |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
21 /** |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
22 * This file is a simple echo Web service implemented using |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
23 * "node.js". Whenever it receives a POST HTTP query, it echoes its |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
24 * body both to stdout and to the client. Credentials are checked. |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
25 **/ |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
26 |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
27 |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
28 // Parameters of the ECHO server |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
29 var port = 8000; |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
30 var username = 'alice'; |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
31 var password = 'alicePassword'; |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
32 |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
33 |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
34 var http = require('http'); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
35 var authorization = 'Basic ' + new Buffer(username + ':' + password).toString('base64') |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
36 |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
37 var server = http.createServer(function(req, response) { |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
38 // Check the credentials |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
39 if (req.headers.authorization != authorization) |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
40 { |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
41 console.log('Bad credentials, access not allowed'); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
42 response.writeHead(401); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
43 response.end(); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
44 return; |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
45 } |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
46 |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
47 switch (req.method) |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
48 { |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
49 case 'POST': |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
50 { |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
51 var body = ''; |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
52 |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
53 req.on('data', function (data) { |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
54 response.write(data); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
55 body += data; |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
56 }); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
57 |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
58 req.on('end', function () { |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
59 console.log('Message received: ' + body); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
60 response.end(); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
61 }); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
62 |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
63 break; |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
64 } |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
65 |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
66 default: |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
67 console.log('Method ' + req.method + ' is not supported by this ECHO Web service'); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
68 response.writeHead(405, {'Allow': 'POST'}); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
69 response.end(); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
70 } |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
71 }); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
72 |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
73 server.listen(port); |