-
Notifications
You must be signed in to change notification settings - Fork 41
feat(http): ability to control Content-Type of binary responses #311
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
Conversation
Add the ability to explicitly set the response emitter encoding type. This is used to set the encoding type of the response emitter to something other than the default. For example, when setting the encoding type to `Gzip` when compressed output is being emitted. Needed for: ipfs/kubo#2376
- add Tar encoding type with application/x-tar MIME type - use application/gzip per RFC 6713 (removed non-standard charset=binary parameter, which only applies to text/* types) - keep application/x-gzip as legacy alias for response parsing - document SetEncodingType interface with usage guidelines - add tests for Content-Type headers and backward compatibility
replace the Tar and Gzip encoding types with a generic OctetStream
encoding type and add SetContentType() method to ResponseEmitter.
this allows commands to specify custom MIME types for binary data
without needing specific encoding types for each format. commands
can now use:
res.SetEncodingType(cmds.OctetStream)
res.SetContentType("application/vnd.ipld.car")
to set any Content-Type header for HTTP responses.
changes:
- remove Tar and Gzip encoding types from encoding.go
- add OctetStream encoding type (application/octet-stream)
- add SetContentType(string) to ResponseEmitter interface
- implement SetContentType in http responseEmitter
- add no-op SetContentType to chan, cli, and writer emitters
- update MIMEEncodings and mimeTypes maps
- update tests for new encoding types
adds HelpText.HTTP field with HTTPHelpText struct containing: - ResponseContentType: documents Content-Type header for HTTP responses - Description: additional HTTP-specific notes shown after Tagline this enables tools like http-api-docs to generate better API reference by reading HTTP-specific metadata directly from command definitions.
lidel
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the initial implementation!
I adjusted the approach to make it more generic and future-proof. Instead of specific encoding types for each format, we now have:
- A generic
OctetStreambinary response type that usesapplication/octet-stream SetContentType()function that allows commands likeipfs getto programmatically adjust the Content-Type header based on runtime conditions (e.g.,--compressflag). Wired it and tested in ipfs/kubo#11067 and it works as expected.
This way we don't need to add new encoding types every time we want a different Content-Type, and commands have full control over the header when needed.
The change is backward-compatible and opt-in (existing commands work exactly as before), so merging this as there's no risk shipping it.
Add the ability to explicitly set the response emitter encoding type.
This is used to set the encoding type of the response emitter to something other than the default. For example, when setting the encoding type to
Gzipwhen compressed output is being emitted.Needed for:
ipfs get --compressgives incorrect Header kubo#2376I do not know if this is the right way to do this, but testing with kubo PR above indicates it does work.
TODO: add unit test to check HTTP headers