Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented streaming parsing multipart/form-data as turbo takes 3x memory of uploading file(s) size during parsing multipart/form-data request #367

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions examples/multipart.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
--- Turbo.lua Multipart example
--
-- Copyright 2023 Gary Liu
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

local turbo = require "turbo"

local UploadHandler = class("Upload", turbo.web.RequestHandler)

function UploadHandler:post()
local response = {}
local form_args = self.request.connection.arguments
for name, arg_parts in pairs(form_args) do
for ind, arg in ipairs(arg_parts) do
local tab = response[name] or {}
if arg["content-type"] then
tab[ind] = {
["content-type"] = arg["content-type"],
["content-disposition"] = arg["content-disposition"],
["filepath"] = arg["filepath"],
["filelen"] = arg["filelen"] or #arg[1],
}
else
tab[ind] = arg[1]
end
response[name] = tab
end
end
self:write(response)
end

turbo.web.Application({
{"^/$", UploadHandler}
}):listen(8888, nil, {
streaming_multipart_bytes = 1*1024*1024,
large_body_bytes = 1024,
})
turbo.ioloop.instance():start()
14 changes: 13 additions & 1 deletion turbo/httpserver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,19 @@ function httpserver.HTTPConnection:_on_headers(data)
if headers:get("Expect") == "100-continue" then
self.stream:write("HTTP/1.1 100 (Continue)\r\n\r\n")
end
self.stream:read_bytes(content_length, self._on_request_body, self)

local content_type = self._request.headers:get("Content-Type") or ""
if type(self.kwargs.streaming_multipart_bytes) == "number" and
content_length >= self.kwargs.streaming_multipart_bytes and
content_type:find("multipart/form-data", 1, true) then
local final_callback = function(self) self.request_callback(self._request) end
local stream_parse = httputil.StreamingParser:new(self)

self.stream:read_bytes_raw_buffer(content_length, final_callback, self,
stream_parse.parse_large_multipart_body, stream_parse)
else
self.stream:read_bytes(content_length, self._on_request_body, self)
end
return
end
end
Expand Down
Loading