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

[Storage] Workaround to avoid duplicated 'content-length' headers on HTTP requests [storage-file] #3273

Merged
merged 11 commits into from
Jun 6, 2019

Conversation

kinelski
Copy link
Member

@kinelski kinelski commented May 29, 2019

Both SDK and Axios module add a content-length header to HTTP requests. This workaround fixes this duplicated header issue while axios/axios#2107 is not solved.

A similar PR "fixed" the same problem in the storage-queue package (#2422). A different approach is being adopted this time because the previous "fix" wouldn't be enough to avoid the bug in storage-file.

The Axios HTTP adapter won't always include its own Content-Length header: when the request body is a stream, the header is not added. The piece of code responsible for this behavior:

if (data && !utils.isStream(data)) {
  if (Buffer.isBuffer(data)) {
    // Nothing to do...
  } else if (utils.isArrayBuffer(data)) {
    data = Buffer.from(new Uint8Array(data));
  } else if (utils.isString(data)) {
    data = Buffer.from(data, 'utf-8');
  } else {
    return reject(createError(
      'Data after transformation must be a string, an ArrayBuffer, a Buffer, or a Stream',
      config
    ));
  }


  // Add Content-Length header if data exists
  headers['Content-Length'] = data.length;
}

We need to make sure to remove the content-length header from the SDK requests only when necessary.

There are two possible scenarios we need to pay attention to:

    let axiosBody =
      // Workaround for https://github.com/axios/axios/issues/755
      // tslint:disable-next-line:no-null-keyword
      typeof httpRequestBody === "undefined" ? null :
      typeof httpRequestBody === "function" ? httpRequestBody() :
      httpRequestBody;

This scenario is commonly triggered in Highlevel tests, since most of them make use of streams to upload files to Azure.

    const onUploadProgress = httpRequest.onUploadProgress;
    if (onUploadProgress && axiosBody) {
      let loadedBytes = 0;
      const uploadReportStream = new Transform({
        transform: (chunk: string | Buffer, _encoding, callback) => {
          loadedBytes += chunk.length;
          onUploadProgress({ loadedBytes });
          callback(undefined, chunk);
        }
      });
      if (isReadableStream(axiosBody)) {
        axiosBody.pipe(uploadReportStream);
      } else {
        uploadReportStream.end(axiosBody);
      }
      axiosBody = uploadReportStream;
    }

When any of the aforementioned scenarios happens, Axios won't include its own Content-Length header and we must keep ours.

@kinelski kinelski added bug This issue requires a change to an existing behavior in the product in order to be resolved. Client This issue points to a problem in the data-plane of the library. Storage Storage Service (Queues, Blobs, Files) labels May 29, 2019
@kinelski kinelski self-assigned this May 29, 2019
Copy link
Member

@XiaoningLiu XiaoningLiu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM Thanks for your investigation!

@kinelski kinelski merged commit bc58b65 into Azure:master Jun 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue requires a change to an existing behavior in the product in order to be resolved. Client This issue points to a problem in the data-plane of the library. Storage Storage Service (Queues, Blobs, Files)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants