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

Feature request: Parsing MultiPartForm *responses* (currently we can only parse multipart _requests_) #816

Closed
NHDaly opened this issue Mar 25, 2022 · 2 comments · Fixed by #817
Labels
enhancement parser HTTP (headers) parser

Comments

@NHDaly
Copy link
Collaborator

NHDaly commented Mar 25, 2022

(As of HTTP v0.9.17; Julia v1.7.2)

We currently support parsing a multi-part request, but don't support parsing a multi-part response. Here's the multi-part request parsing code:

function parse_multipart_form(req::Request)::Union{Vector{Multipart}, Nothing}
# parse boundary from Content-Type
m = match(r"multipart/form-data; boundary=(.*)$", req["Content-Type"])
m === nothing && return nothing
boundary_delimiter = m[1]
# [RFC2046 5.1.1](https://tools.ietf.org/html/rfc2046#section-5.1.1)
length(boundary_delimiter) > 70 && error("boundary delimiter must not be greater than 70 characters")
return parse_multipart_body(payload(req), boundary_delimiter)
end

From what I can tell, this would also work to support responses if we loosened the type restriction on the parameter to HTTP.Message instead of just HTTP.Request. This is what we've done in our own code, and it's been working well:

# Same as HTTP.parse_multipart_form, but this works on both requests and responses as
# opposed to only requests.
# TODO: we should probably PR this to HTTP.jl
function _parse_multipart_form(msg::HTTP.Message)
    # parse boundary from Content-Type
    m = match(r"multipart/form-data; boundary=(.*)$", msg["Content-Type"])
    m === nothing && return nothing

    boundary_delimiter = m[1]

    # [RFC2046 5.1.1](https://tools.ietf.org/html/rfc2046#section-5.1.1)
    length(boundary_delimiter) > 70 && error("boundary delimiter must not be greater than 70 characters")

    return HTTP.MultiPartParsing.parse_multipart_body(HTTP.payload(msg), boundary_delimiter)
end

This is me making good on this "TODO" 😁 Does a PR like that sound okay?

@NHDaly
Copy link
Collaborator Author

NHDaly commented Mar 25, 2022

(CC: @rgankema, the original author of the above TODO)

@NHDaly
Copy link
Collaborator Author

NHDaly commented Mar 25, 2022

I've put up a PR for this, here: #817, so there's something concrete to discuss. Thanks! :)

@fonsp fonsp added enhancement parser HTTP (headers) parser labels Mar 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement parser HTTP (headers) parser
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants