8000 setting response header after content stream is read? · Issue #72 · mjackson/mach · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

setting response header after content stream is read? #72

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

Open
nicholascloud opened this issue Apr 10, 2015 · 0 comments
Open

setting response header after content stream is read? #72

nicholascloud opened this issue Apr 10, 2015 · 0 comments

Comments

@nicholascloud
Copy link
Contributor

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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant
0