Skip to content

Commit

Permalink
[Ruby] Fix Content-Transfer-Encoding binary unpacking
Browse files Browse the repository at this point in the history
  • Loading branch information
sirwolfgang committed Jul 10, 2024
1 parent 9948ed2 commit 04907a7
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,35 +117,41 @@
request.options.on_data = Proc.new do |chunk, overall_received_bytes|
stream << chunk
end

stream
end

def deserialize_file(response, stream)
body = response.body
if @config.return_binary_data == true
# return byte stream
encoding = body.encoding
stream.join.force_encoding(encoding)
body = response.body
encoding = body.encoding

# reconstruct content
content = stream.join
content = content.unpack('m').join if response.headers['Content-Transfer-Encoding'] == 'binary'
content = content.force_encoding(encoding)

# return byte stream
return content if @config.return_binary_data == true

# return file instead of binary data
content_disposition = response.headers['Content-Disposition']
if content_disposition && content_disposition =~ /filename=/i
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
prefix = sanitize_filename(filename)
else
# return file instead of binary data
content_disposition = response.headers['Content-Disposition']
if content_disposition && content_disposition =~ /filename=/i
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
prefix = sanitize_filename(filename)
else
prefix = 'download-'
end
prefix = prefix + '-' unless prefix.end_with?('-')
encoding = body.encoding
tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
tempfile.write(stream.join.force_encoding(encoding))
tempfile.close
config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
"with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
"explicitly with `tempfile.delete`"
tempfile
prefix = 'download-'
end
prefix = prefix + '-' unless prefix.end_with?('-')

tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
tempfile.write(content)
tempfile.close

config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
"with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
"explicitly with `tempfile.delete`"
tempfile
end

def connection(opts)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,35 +164,41 @@ def download_file(request)
request.options.on_data = Proc.new do |chunk, overall_received_bytes|
stream << chunk
end

stream
end

def deserialize_file(response, stream)
body = response.body
if @config.return_binary_data == true
# return byte stream
encoding = body.encoding
stream.join.force_encoding(encoding)
body = response.body
encoding = body.encoding

# reconstruct content
content = stream.join
content = content.unpack('m').join if response.headers['Content-Transfer-Encoding'] == 'binary'
content = content.force_encoding(encoding)

# return byte stream
return content if @config.return_binary_data == true

# return file instead of binary data
content_disposition = response.headers['Content-Disposition']
if content_disposition && content_disposition =~ /filename=/i
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
prefix = sanitize_filename(filename)
else
# return file instead of binary data
content_disposition = response.headers['Content-Disposition']
if content_disposition && content_disposition =~ /filename=/i
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
prefix = sanitize_filename(filename)
else
prefix = 'download-'
end
prefix = prefix + '-' unless prefix.end_with?('-')
encoding = body.encoding
tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
tempfile.write(stream.join.force_encoding(encoding))
tempfile.close
config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
"with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
"explicitly with `tempfile.delete`"
tempfile
prefix = 'download-'
end
prefix = prefix + '-' unless prefix.end_with?('-')

tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
tempfile.write(content)
tempfile.close

config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
"with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
"explicitly with `tempfile.delete`"
tempfile
end

def connection(opts)
Expand Down
52 changes: 29 additions & 23 deletions samples/client/petstore/ruby-faraday/lib/petstore/api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,35 +164,41 @@ def download_file(request)
request.options.on_data = Proc.new do |chunk, overall_received_bytes|
stream << chunk
end

stream
end

def deserialize_file(response, stream)
body = response.body
if @config.return_binary_data == true
# return byte stream
encoding = body.encoding
stream.join.force_encoding(encoding)
body = response.body
encoding = body.encoding

# reconstruct content
content = stream.join
content = content.unpack('m').join if response.headers['Content-Transfer-Encoding'] == 'binary'
content = content.force_encoding(encoding)

# return byte stream
return content if @config.return_binary_data == true

# return file instead of binary data
content_disposition = response.headers['Content-Disposition']
if content_disposition && content_disposition =~ /filename=/i
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
prefix = sanitize_filename(filename)
else
# return file instead of binary data
content_disposition = response.headers['Content-Disposition']
if content_disposition && content_disposition =~ /filename=/i
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
prefix = sanitize_filename(filename)
else
prefix = 'download-'
end
prefix = prefix + '-' unless prefix.end_with?('-')
encoding = body.encoding
tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
tempfile.write(stream.join.force_encoding(encoding))
tempfile.close
config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
"with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
"explicitly with `tempfile.delete`"
tempfile
prefix = 'download-'
end
prefix = prefix + '-' unless prefix.end_with?('-')

tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
tempfile.write(content)
tempfile.close

config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
"with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
"explicitly with `tempfile.delete`"
tempfile
end

def connection(opts)
Expand Down

0 comments on commit 04907a7

Please sign in to comment.