Skip to content

celsworth/cae-multipart_parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Event-driven HTTP Multipart Parser

This is based on https://github.com/danabr/multipart-parser, with modifications to suit my use-case.

It currently depends on finding a Content-Length part header to avoid having to scan the entire body. It will raise Cae::MultipartParser::Parser::ContentLengthUnsetError if this header is not present.

Usage

parser = Cae::MultipartParser::Parser.new(boundary: boundary)

parser.parse fh do |part|
	# part.headers and part.content_length should be set now
	# headers are underscored and uppercased:
	if part.headers['CONTENT_TYPE'] == 'text/html'
		# ...
	end

	# content_length is an integer:
	if part.content_length < 1024
		# ...
	end

	while part.body.read(chunksize, buf)
		# buf contains up to chunksize bytes of data.
		# Do not assume if less than chunksize is returned, you're done,
		# for internal as-yet-to-be-fixed reasons.
		# Only stop when #read returns nil. Note that #read is NOT IO#read,
		# but is mostly compatible.
	end

	# all the part body has now been read
end

About

Event-driven HTTP Multipart parser

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages