Bug 77 - lua access to REST-API is null terminated
Summary: lua access to REST-API is null terminated
Status: RESOLVED FIXED
Alias: None
Product: Orthanc
Classification: Unclassified
Component: Orthanc Core (show other bugs)
Version: unspecified
Hardware: All All
: --- minor
Assignee: Sébastien Jodogne
URL:
Depends on:
Blocks:
 
Reported: 2020-06-29 15:13 CEST by Sébastien Jodogne
Modified: 2020-06-29 15:21 CEST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sébastien Jodogne 2020-06-29 15:13:25 CEST
[BitBucket user: Amos Onn]
[BitBucket date: 2017-12-05.17:56:07]

When attempting to access the preview of an Instance via the REST-API in a lua script, we only receive the null-terminated portion of the png (usually not much more than the magic value).
Comment 1 Sébastien Jodogne 2020-06-29 15:21:05 CEST
[BitBucket user: Sébastien Jodogne]
[BitBucket date: 2017-12-14.16:45:17]

Please could you provide a sample Lua script in order for us to be sure to work on your precise issue?
Comment 2 Sébastien Jodogne 2020-06-29 15:21:05 CEST
[BitBucket user: Amos Onn]
[BitBucket date: 2018-01-04.15:11:38]

```
function OnStoredInstance(instanceId, tags, metadata)
    local preview = RestApiGet('/instances/' .. instanceId .. '/preview')
    local headers = {
        ["content-type"]="Image/PNG",
    }
    HttpPost("http://172.20.5.1:1235", preview, headers)
    Delete(SendToModality(instanceId, 'server'))
end
```
This sent a POST request to the server, but only with the first 8 bytes which are the signature of a PNG file.
I looked through the LUA scripting part of the Orthanc source for `RestApiGet`, and it seems to be using the correct method of the LUA C-API (namely, the one that passes a buffer by size and not by null termination). I'm pretty sure I've attempted printing the output the received object and it looked the same; but now that I think of it, perhaps the log function used the null terminator, and `HttpPost` uses it as well?
Comment 3 Sébastien Jodogne 2020-06-29 15:21:54 CEST
[BitBucket user: Sébastien Jodogne]
[BitBucket date: 2018-07-12.13:54:10]

For reference, here is a sample [Flask](http://flask.pocoo.org/) script to mimic the behavior of Amos' HTTP server running on `http://172.20.5.1:1235`:

```
from flask import Flask
from flask import request
app = Flask(__name__)

@app.route('/', methods = [ 'POST' ])
def store():
    with open('/tmp/preview.png', 'wb') as f:
        f.write(request.get_data())
    return 'ok'

if __name__ == "__main__":
    app.run(debug = True, host = '0.0.0.0')
```

This Flask script writes all the HTTP POST requests it receives into file `/tmp/preview.png`. And here is the corresponding Lua script in Orthanc (the Flask script runs by default on `http://localhost:5000/`):

```
function OnStoredInstance(instanceId, tags, metadata)
    local preview = RestApiGet('/instances/' .. instanceId .. '/preview')
    local headers = {
        ['content-type'] = 'image/png',
    }
    HttpPost('http://localhost:5000', preview, headers)
    Delete(instanceId)
end
```
Comment 4 Sébastien Jodogne 2020-06-29 15:21:56 CEST
[BitBucket user: Sébastien Jodogne]
[BitBucket date: 2018-07-12.13:54:43]

fix issue #77

→ https://hg.orthanc-server.com/orthanc/changeset/c8d369400ae1