Skip to content

setting response header after content stream is read? #72

@nicholascloud

Description

@nicholascloud

I'm implementing a middleware that will set the Content-MD5 header (https://tools.ietf.org/html/rfc1864). This requires the full content of the response body to be read and then hashed. I'm trying to implement this by piping the content stream via through, but the Content-MD5 response header never makes it to the client. What would be the best way to go about this? My attempt is below.

'use strict';
var crypto = require('crypto');
var through = require('through');

function contentMD5(app, options) {
  return function (conn) {
    var md5sum = crypto.createHash('md5');
    return conn.call(app).then(function () {
      var response = conn.response;
      var headers = response.headers;

      response.content = response.content.pipe(through(function write (data) {
        md5sum.update(data);
        this.emit('data', data);
      }, function end () {
        var digest = md5sum.digest('hex');
        console.log('>>> here', digest);
        headers['Content-MD5'] = digest;
        this.emit('end');
      }));
    });
  };
}

module.exports = contentMD5;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions