forked from Kong/httpsnippet
-
Notifications
You must be signed in to change notification settings - Fork 6
fix: handling of multipart/form-data requests #1
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d493781
fix: moving form-data usage over to using the native FormData object
erunion 8001580
test: commenting out some tests while debugging
erunion ce5f277
fix: coding against native FormData and form-data
erunion 346db58
chore: rolling back dep changes
erunion 86c52ba
fix: setting the form-data boundary when under node
erunion b3268ef
docs: language fixes
erunion 02356b6
test: fixing broken multipart tests
erunion 731d664
test: cloning some data to another var to prevent test corruption
erunion 89177c3
fix: out of date regex that was causing readstreams to be stringified
erunion ea50f99
fix: updating the curl target to prioritize param.fileName
erunion 36a1a5b
fix: updating the node request target to prioritize param.fileName
erunion f98662c
fix: updating the node request target to prefer single quotes
erunion 146b8c3
docs: adjusting some incorrect comments
erunion 52ef611
style: removing some unused code
erunion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| { | ||
| "asi": true, | ||
| "browser": true, | ||
| "node": true | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| /** | ||
| * @license https://raw.githubusercontent.com/node-fetch/node-fetch/master/LICENSE.md | ||
| * | ||
| * The MIT License (MIT) | ||
| * | ||
| * Copyright (c) 2016 - 2020 Node Fetch Team | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in all | ||
| * copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| * SOFTWARE. | ||
| */ | ||
|
|
||
| const carriage = '\r\n' | ||
| const dashes = '-'.repeat(2) | ||
|
|
||
| const NAME = Symbol.toStringTag | ||
|
|
||
| const isBlob = object => { | ||
| return ( | ||
| typeof object === 'object' && | ||
| typeof object.arrayBuffer === 'function' && | ||
| typeof object.type === 'string' && | ||
| typeof object.stream === 'function' && | ||
| typeof object.constructor === 'function' && | ||
| /^(Blob|File)$/.test(object[NAME]) | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * @param {string} boundary | ||
| */ | ||
| const getFooter = boundary => `${dashes}${boundary}${dashes}${carriage.repeat(2)}` | ||
|
|
||
| /** | ||
| * @param {string} boundary | ||
| * @param {string} name | ||
| * @param {*} field | ||
| * | ||
| * @return {string} | ||
| */ | ||
| function getHeader (boundary, name, field) { | ||
| let header = '' | ||
|
|
||
| header += `${dashes}${boundary}${carriage}` | ||
| header += `Content-Disposition: form-data; name="${name}"` | ||
|
|
||
| if (isBlob(field)) { | ||
| header += `; filename="${field.name}"${carriage}` | ||
| header += `Content-Type: ${field.type || 'application/octet-stream'}` | ||
| } | ||
|
|
||
| return `${header}${carriage.repeat(2)}` | ||
| } | ||
|
|
||
| /** | ||
| * @return {string} | ||
| */ | ||
| module.exports.getBoundary = () => { | ||
| // This generates a 50 character boundary similar to those used by Firefox. | ||
| // They are optimized for boyer-moore parsing. | ||
| var boundary = '--------------------------' | ||
| for (var i = 0; i < 24; i++) { | ||
| boundary += Math.floor(Math.random() * 10).toString(16) | ||
| } | ||
|
|
||
| return boundary | ||
| } | ||
|
|
||
| /** | ||
| * @param {FormData} form | ||
| * @param {string} boundary | ||
| */ | ||
| module.exports.formDataIterator = function * (form, boundary) { | ||
| for (const [name, value] of form) { | ||
| yield getHeader(boundary, name, value) | ||
|
|
||
| if (isBlob(value)) { | ||
| yield * value.stream() | ||
| } else { | ||
| yield value | ||
| } | ||
|
|
||
| yield carriage | ||
| } | ||
|
|
||
| yield getFooter(boundary) | ||
| } | ||
|
|
||
| module.exports.isBlob = isBlob | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| POST /har HTTP/1.1 | ||
| Content-Type: application/x-www-form-urlencoded | ||
| Host: mockbin.com | ||
| Content-Length: 19 | ||
| POST /har HTTP/1.1 | ||
| Content-Type: application/x-www-form-urlencoded | ||
| Host: mockbin.com | ||
| Content-Length: 19 | ||
|
|
||
| foo=bar&hello=world |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| POST /har HTTP/1.1 | ||
| Content-Type: application/json | ||
| Host: mockbin.com | ||
| Content-Length: 118 | ||
| POST /har HTTP/1.1 | ||
| Content-Type: application/json | ||
| Host: mockbin.com | ||
| Content-Length: 118 | ||
|
|
||
| {"number":1,"string":"f\"oo","arr":[1,2,3],"nested":{"a":"b"},"arr_mix":[1,"a",{"arr_mix_nested":{}}],"boolean":false} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| POST /har HTTP/1.1 | ||
| Cookie: foo=bar; bar=baz | ||
| Host: mockbin.com | ||
| POST /har HTTP/1.1 | ||
| Cookie: foo=bar; bar=baz | ||
| Host: mockbin.com | ||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| PROPFIND /har HTTP/1.1 | ||
| Host: mockbin.com | ||
| PROPFIND /har HTTP/1.1 | ||
| Host: mockbin.com | ||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,8 @@ | ||
| POST /har?foo=bar&foo=baz&baz=abc&key=value HTTP/1.1 | ||
| Cookie: foo=bar; bar=baz | ||
| Accept: application/json | ||
| Content-Type: application/x-www-form-urlencoded | ||
| Host: mockbin.com | ||
| Content-Length: 7 | ||
| POST /har?foo=bar&foo=baz&baz=abc&key=value HTTP/1.1 | ||
| Cookie: foo=bar; bar=baz | ||
| Accept: application/json | ||
| Content-Type: application/x-www-form-urlencoded | ||
| Host: mockbin.com | ||
| Content-Length: 7 | ||
|
|
||
| foo=bar |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| GET /har HTTP/1.1 | ||
| Accept: application/json | ||
| X-Foo: Bar | ||
| Host: mockbin.com | ||
| GET /har HTTP/1.1 | ||
| Accept: application/json | ||
| X-Foo: Bar | ||
| Host: mockbin.com | ||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| GET /har HTTP/1.1 | ||
| Host: mockbin.com | ||
| GET /har HTTP/1.1 | ||
| Host: mockbin.com | ||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,8 @@ | ||
| POST /har HTTP/1.1 | ||
| Content-Type: application/json | ||
| Host: mockbin.com | ||
| Content-Length: 18 | ||
| POST /har HTTP/1.1 | ||
| Content-Type: application/json | ||
| Host: mockbin.com | ||
| Content-Length: 18 | ||
|
|
||
| { | ||
| "foo": "bar" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| POST /har HTTP/1.1 | ||
| Content-Type: application/json | ||
| Host: mockbin.com | ||
| Content-Length: 12 | ||
| POST /har HTTP/1.1 | ||
| Content-Type: application/json | ||
| Host: mockbin.com | ||
| Content-Length: 12 | ||
|
|
||
| {"foo":null} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,11 @@ | ||
| POST /har HTTP/1.1 | ||
| Content-Type: multipart/form-data; boundary=---011000010111000001101001 | ||
| Host: mockbin.com | ||
| Content-Length: 136 | ||
| -----011000010111000001101001 | ||
| Content-Disposition: form-data; name="foo"; filename="hello.txt" | ||
| Content-Type: text/plain | ||
| POST /har HTTP/1.1 | ||
| Content-Type: multipart/form-data; boundary=---011000010111000001101001 | ||
| Host: mockbin.com | ||
| Content-Length: 171 | ||
|
|
||
| -----011000010111000001101001 | ||
| Content-Disposition: form-data; name="foo"; filename="hello.txt" | ||
| Content-Type: text/plain | ||
|
|
||
| Hello World | ||
| -----011000010111000001101001-- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,11 @@ | ||
| POST /har HTTP/1.1 | ||
| Content-Type: multipart/form-data; boundary=---011000010111000001101001 | ||
| Host: mockbin.com | ||
| Content-Length: 125 | ||
|
|
||
| -----011000010111000001101001 | ||
| Content-Disposition: form-data; name="foo"; filename="hello.txt" | ||
| Content-Type: text/plain | ||
|
|
||
| POST /har HTTP/1.1 | ||
| Content-Type: multipart/form-data; boundary=---011000010111000001101001 | ||
| Host: mockbin.com | ||
| Content-Length: 160 | ||
|
|
||
| -----011000010111000001101001 | ||
| Content-Disposition: form-data; name="foo"; filename="hello.txt" | ||
| Content-Type: text/plain | ||
|
|
||
|
|
||
| -----011000010111000001101001-- |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Oh wow. A generator in action. I think it's the first time I've actually seen one in usage.
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.
I had to look up
function *to see what that was because I'd also never seen a JS generator in action before.