Skip to content
Snippets Groups Projects
  • Oleg Jukovec's avatar
    417c6cb7
    httpc: introduce stream input/output interface · 417c6cb7
    Oleg Jukovec authored
    Add a streaming data input/output object for http.client. The
    input/output object can be created using the same methods and the
    same options as a normal request, but with a new option
    {chunked = true}.
    
    Closes #7845
    
    @TarantoolBot document
    Title: Stream input/output interface for http.client
    
    An uncompleted io object has only headers and cookies fields. A
    completed io object has the same fields as a normal request, but
    without the `body` field.
    
    The io object interface looks like the socket object interface
    and should have the same description:
    
    ```
    io_object:read(chunk[, timeout])
    io_object:read(delimiter[, timeout])
    io_object:read({chunk = chunk, delimiter = delimiter}[, timeout])
    io_object:write(data[, timeout])
    ```
    
    The difference is in the method `finish`. Unlike socket:close()
    it has an optional parameter `timeout`:
    
    ```
    io_object:finish([timeout])
    ```
    
    Be careful, the call may yield a fiber. The idea is to wait
    until a HTTP connection is finished by the server-side or
    force finish the connection from client-time after a timeout
    value.
    
    The default timeout value is 10 seconds for all methods.
    
    Usage example:
    
    ```lua
    local io = httpc:post(url, nil, {chunked = true})
    
    local write_chan = fiber.channel()
    fiber.new(function()
        fiber.name("write to " .. url)
        while true do
            local data = write_chan:get()
            if data == nil then
                break
            end
            io:write(data, 1)
        end
    end)
    
    local recvdata
    while recvdata = io:read('\r\n', 1) do
        local decoded = json.decode(recvdata)
        if condition(decoded) then
            write_chan:put(data)
        end
        if condition(decoded) then
            io:finish(1)
        end
    end
    write_chan:close()
    ```
    
    See also:
    * https://www.tarantool.io/en/doc/latest/reference/reference_lua/socket/#lua-function.socket_object.read
    * https://github.com/tarantool/tarantool/issues/7845#issuecomment-1298538412
    * https://github.com/tarantool/tarantool/issues/7845#issuecomment-1298821779
    417c6cb7
    History
    httpc: introduce stream input/output interface
    Oleg Jukovec authored
    Add a streaming data input/output object for http.client. The
    input/output object can be created using the same methods and the
    same options as a normal request, but with a new option
    {chunked = true}.
    
    Closes #7845
    
    @TarantoolBot document
    Title: Stream input/output interface for http.client
    
    An uncompleted io object has only headers and cookies fields. A
    completed io object has the same fields as a normal request, but
    without the `body` field.
    
    The io object interface looks like the socket object interface
    and should have the same description:
    
    ```
    io_object:read(chunk[, timeout])
    io_object:read(delimiter[, timeout])
    io_object:read({chunk = chunk, delimiter = delimiter}[, timeout])
    io_object:write(data[, timeout])
    ```
    
    The difference is in the method `finish`. Unlike socket:close()
    it has an optional parameter `timeout`:
    
    ```
    io_object:finish([timeout])
    ```
    
    Be careful, the call may yield a fiber. The idea is to wait
    until a HTTP connection is finished by the server-side or
    force finish the connection from client-time after a timeout
    value.
    
    The default timeout value is 10 seconds for all methods.
    
    Usage example:
    
    ```lua
    local io = httpc:post(url, nil, {chunked = true})
    
    local write_chan = fiber.channel()
    fiber.new(function()
        fiber.name("write to " .. url)
        while true do
            local data = write_chan:get()
            if data == nil then
                break
            end
            io:write(data, 1)
        end
    end)
    
    local recvdata
    while recvdata = io:read('\r\n', 1) do
        local decoded = json.decode(recvdata)
        if condition(decoded) then
            write_chan:put(data)
        end
        if condition(decoded) then
            io:finish(1)
        end
    end
    write_chan:close()
    ```
    
    See also:
    * https://www.tarantool.io/en/doc/latest/reference/reference_lua/socket/#lua-function.socket_object.read
    * https://github.com/tarantool/tarantool/issues/7845#issuecomment-1298538412
    * https://github.com/tarantool/tarantool/issues/7845#issuecomment-1298821779