Skip to content

Keep original filename while injecting data files in HTTP with POST method #448

@matthieumarrast

Description

@matthieumarrast

In script content_by_lua.lua, when POST method is used for sending file through HTTP, the filename written in target incoming directory can only be initialized with a unique identifier.
As per the below code of content_by_lua.lu only the PUT method allows to keep the "original basename" of the file.

local function get_target_filepath(directory, filename)
    if filename == nil then
        return string.format("%s/%s/%s", data_in_dir, directory, make_unique_id())
    else
        return string.format("%s/%s/%s", data_in_dir, directory, filename)
    end
end

local function process()
    if method == "POST" then
            regex = "^/([a-zA-Z0-9,;:_%-%.]*)/*$"
            directory = string.match(uri, regex)
            if directory == nil then
                exit_with_ngx_error(400, string.format("POST request uri must match with %s lua pattern", regex))
            elseif directory == "" then
                directory = default_directory
            end
    elseif method == "PUT" then
        regex = "^/([a-zA-Z0-9,;:_%-%.]+)/*$"
        filename = string.match(uri, regex)
        if filename == nil then
            regex = "^/([a-zA-Z0-9,;:_%-%.]+)/([a-zA-Z0-9,;:_%-%.]+)/*$"
            directory, filename = string.match(uri, regex)
            if directory == nil or filename == nil then
                exit_with_ngx_error(400, string.format("PUT request uri must match with %s lua pattern", regex))
            end
        end
    else
    targetpath = get_target_filepath(directory, filename)

So, as per the documentation, using command
curl -X POST -d @{your file path} http://localhost:9091/incoming/
will write a file named a8879e2baefc6b16ac39a8cee7690703 (example) in the targetted incoming directory. The original filename is lost.

Solution:

  • Prefer using PUT method for injecting file:
    curl -X PUT -d @{file} http://localhost:9091/incoming/{file}
    or
    curl -X PUT --upload-file {file} http://localhost:9091/incoming/

=> update default config.ini of httpsend plugin, with PUT method by default, and precise that using /incoming/{ORIGINAL_BASENAME} cannot work with POST method.

Using command curl -v -X POST -d @{file} http://localhost:9091/incoming/{file}
will trigger error "HTTP/400 ERROR: POST request uri must match with ^/([a-zA-Z0-9,;:_%-%.])/$ lua pattern"
maybe a modification of the above lua script can be applied to allow this kind of request, and also make possible keeping original filename with a POST method.

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions