Mercurial > hg > orthanc
annotate OrthancServer/Resources/Samples/Lua/CallWebService.js @ 5654:b89439c14916
back to mainline
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 05 Jun 2024 17:53:00 +0200 |
parents | f7adfb22e20e |
children |
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 |
5640
f7adfb22e20e
updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5485
diff
changeset
|
5 * Copyright (C) 2017-2023 Osimis S.A., Belgium |
f7adfb22e20e
updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5485
diff
changeset
|
6 * Copyright (C) 2024-2024 Orthanc Team SRL, Belgium |
5485
48b8dae6dc77
upgrade to year 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5185
diff
changeset
|
7 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
1055
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
8 * |
1375
3d76e26b3865
fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
9 * 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
|
10 * 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
|
11 * 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
|
12 * License, or (at your option) any later version. |
3d76e26b3865
fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
13 * |
3d76e26b3865
fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
14 * 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
|
15 * 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
|
16 * 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
|
17 * General Public License for more details. |
1055
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
18 * |
1375
3d76e26b3865
fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
19 * 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
|
20 * 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
|
21 **/ |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
22 |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
23 |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
24 /** |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
25 * 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
|
26 * "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
|
27 * 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
|
28 **/ |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
29 |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
30 |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
31 // Parameters of the ECHO server |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
32 var port = 8000; |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
33 var username = 'alice'; |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
34 var password = 'alicePassword'; |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
35 |
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 http = require('http'); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
38 var authorization = 'Basic ' + new Buffer(username + ':' + password).toString('base64') |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
39 |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
40 var server = http.createServer(function(req, response) { |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
41 // Check the credentials |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
42 if (req.headers.authorization != authorization) |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
43 { |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
44 console.log('Bad credentials, access not allowed'); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
45 response.writeHead(401); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
46 response.end(); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
47 return; |
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 |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
50 switch (req.method) |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
51 { |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
52 case 'POST': |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
53 { |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
54 var body = ''; |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
55 |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
56 req.on('data', function (data) { |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
57 response.write(data); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
58 body += data; |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
59 }); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
60 |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
61 req.on('end', function () { |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
62 console.log('Message received: ' + body); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
63 response.end(); |
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 break; |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
67 } |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
68 |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
69 default: |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
70 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
|
71 response.writeHead(405, {'Allow': 'POST'}); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
72 response.end(); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
73 } |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
74 }); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
75 |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
76 server.listen(port); |