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

No Content-Type header when returning a stream #3891

Closed
sholladay opened this Issue Nov 29, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@sholladay
Copy link

sholladay commented Nov 29, 2018

What are you trying to achieve or the steps to reproduce?

Trying to respond with a stream and receive an appropriate content-type header on the client.

Under most circumstances, hapi sends a content-type header automatically based on whatever the return value of my handler is. However, if the handler returns a stream, this doesn't happen.

Here is a reproducible example that shows no content-type header being sent for a simple "Hello, world!" stream. The response headers are logged to the server's console when starting the server and also rendered in the browser if you visit /.

Edit o5wlywl7pz

Source code
"use strict";

const hapi = require("hapi");
const { Readable } = require("stream");

const server = hapi.server({ port: 8000 });
server.route({
  method: "GET",
  path: "/hello",
  handler() {
    const stream = new Readable({
      read() {
        this.push("Hello, world!");
        this.push(null);
      }
    });

    return stream;
  }
});

server.route({
  method: "GET",
  path: "/{anything*}",
  handler() {
    return `
      <html><script>
        fetch('/hello').then((response) => {
          const headers = Object.assign(...Array.from(response.headers, ([key, value]) => {
            return { [key]: value };
          }));
          const pre = document.createElement('pre');
          pre.textContent = JSON.stringify(headers, null, 4);
          document.body.appendChild(pre);
        });
      </script></html>
    `;
  }
});

server.inject("/hello").then(response => {
  console.log(JSON.stringify(response.headers, null, 4));
});

server.start();

What was the result you received?

No content-type header is sent in the response when the handler's return value is a simple "Hello, world!" readable stream.

What did you expect?

I expected hapi to set a content-type header whenever possible.

Ideally, I'd like it to be based on the actual contents of the stream, which could be determined by reading the first few bytes to inspect the magic number (also see the file-type module). But even the generic application/octect-stream would be better than nothing.

Context

  • node version: 8.11.3
  • hapi version: 17.6.0
  • os: macOS 10.13.6
@hueniverse

This comment has been minimized.

Copy link
Member

hueniverse commented Jan 6, 2019

hapi is never going to try and guess the content type by peeking at the first few bytes. You can set your own content type when returning a stream using h.response(stream).type('text/html'). I'll look into setting a default value.

@hueniverse hueniverse self-assigned this Jan 6, 2019

@hueniverse hueniverse added request lts and removed lts labels Jan 6, 2019

@hueniverse hueniverse added this to the 18.0.0 milestone Jan 6, 2019

@hueniverse hueniverse closed this in c88bda0 Jan 6, 2019

@hueniverse hueniverse added bug and removed request labels Jan 18, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment