-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix multipart/related requests when uploading multiple attachments. F…
…ixes issue#238 (#279) Co-authored-by: Glynn Bird <glynnbird@apache.org>
- Loading branch information
Showing
4 changed files
with
125 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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,53 @@ | ||
const CRLF = '\r\n' | ||
const DASHES = '--' | ||
|
||
// generate the payload, boundary and header for a multipart/related request | ||
// to upload binary attachments to CouchDB. | ||
// https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html | ||
class MultiPartFactory { | ||
// constructor | ||
constructor (parts) { | ||
// generate a unique id that forms the boundary between parts | ||
this.boundary = this.uuid() | ||
const bufferList = [] | ||
|
||
// for each part to be processed | ||
for (const part of parts) { | ||
// start with the boundary e.g. --0559337432997171\r\n | ||
bufferList.push(Buffer.from(DASHES + this.boundary + CRLF)) | ||
|
||
// state the type and length of the following part | ||
bufferList.push(Buffer.from(`content-type: ${part.content_type}${CRLF}`)) | ||
bufferList.push(Buffer.from(`content-length: ${part.data.length}${CRLF}`)) | ||
|
||
// two \r\n marks start of the part itself | ||
bufferList.push(Buffer.from(CRLF)) | ||
|
||
// output the string/buffer | ||
bufferList.push(typeof part.data === 'string' ? Buffer.from(part.data) : part.data) | ||
|
||
// followed by /r/n | ||
bufferList.push(Buffer.from(CRLF)) | ||
} | ||
|
||
// right at the end we have an end marker e.g. --0559337432997171--\r\n | ||
bufferList.push(Buffer.from(DASHES + this.boundary + DASHES + CRLF)) | ||
|
||
// buid up a single Buffer from the array of bits | ||
this.data = Buffer.concat(bufferList) | ||
|
||
// calculate the Content-Type header required to send with this request | ||
this.header = `multipart/related; boundary=${this.boundary}` | ||
} | ||
|
||
uuid () { | ||
const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('') | ||
let retval = '' | ||
for (let i = 0; i < 16; i++) { | ||
retval += chars[Math.floor(Math.random() * chars.length)] | ||
} | ||
return retval | ||
} | ||
} | ||
|
||
module.exports = MultiPartFactory |
This file contains 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 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 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,56 @@ | ||
const MultiPartFactory = require('../lib/multipart.js') | ||
const textAttachment = { name: 'test.txt', data: 'Hello\r\nWorld!', content_type: 'text/plain' } | ||
const anotherTextAttachment = { name: 'test2.txt', data: 'the quick brown fox', content_type: 'text/plain' } | ||
|
||
test('should return different boundary each time', async () => { | ||
const mf1 = new MultiPartFactory([]) | ||
const mf2 = new MultiPartFactory([]) | ||
const mf3 = new MultiPartFactory([]) | ||
|
||
expect(typeof mf1.boundary).toBe('string') | ||
expect(typeof mf2.boundary).toBe('string') | ||
expect(typeof mf3.boundary).toBe('string') | ||
expect(mf1.boundary.length).toBe(16) | ||
expect(mf2.boundary.length).toBe(16) | ||
expect(mf3.boundary.length).toBe(16) | ||
expect(mf1).not.toEqual(mf2) | ||
expect(mf1).not.toEqual(mf3) | ||
expect(mf2).not.toEqual(mf3) | ||
}) | ||
|
||
test('should return boundary in header', async () => { | ||
const mf1 = new MultiPartFactory([]) | ||
const boundary = mf1.boundary | ||
const header = mf1.header | ||
expect(header).toEqual(`multipart/related; boundary=${boundary}`) | ||
}) | ||
|
||
test('should handle single attachments', async () => { | ||
const mf1 = new MultiPartFactory([textAttachment]) | ||
expect(typeof mf1.data).toEqual('object') | ||
expect(Buffer.isBuffer(mf1.data)).toEqual(true) | ||
const lines = mf1.data.toString().split('\r\n') | ||
expect(lines).toContain(`--${mf1.boundary}`) | ||
expect(lines).toContain('content-type: text/plain') | ||
expect(lines).toContain('content-length: 13') | ||
expect(lines).toContain('') | ||
expect(lines).toContain('Hello') | ||
expect(lines).toContain('World!') | ||
expect(lines).toContain(`--${mf1.boundary}--`) | ||
}) | ||
|
||
test('should handle two attachments', async () => { | ||
const mf1 = new MultiPartFactory([textAttachment, anotherTextAttachment]) | ||
expect(typeof mf1.data).toEqual('object') | ||
expect(Buffer.isBuffer(mf1.data)).toEqual(true) | ||
const lines = mf1.data.toString().split('\r\n') | ||
expect(lines).toContain(`--${mf1.boundary}`) | ||
expect(lines).toContain('content-type: text/plain') | ||
expect(lines).toContain('content-length: 13') | ||
expect(lines).toContain('') | ||
expect(lines).toContain('Hello') | ||
expect(lines).toContain('World!') | ||
expect(lines).toContain('content-length: 19') | ||
expect(lines).toContain('the quick brown fox') | ||
expect(lines).toContain(`--${mf1.boundary}--`) | ||
}) |