0

This question was motivated by the answers here: What to do with errors when streaming the body of an Http request

In this case, I have already written a HTTP 200 OK header, then I need to amend this if there is an error, by writing a trail header that says there was an error after writing a success header.

I have this Node.js code:

const writeResponse = function(file: string, socket: Socket){

   socket.write([
    'HTTP/1.1 200 OK',
    'Content-Type: text/javascript; charset=UTF-8',
    'Content-Encoding: UTF-8',
    'Accept-Ranges: bytes',
    'Connection: keep-alive',
   ].join('\n') + '\n\n');
        
  getStream(file)
    .pipe(socket)  
    .once('error', function (e: any) {

        // there was an error
        // how can I write trail headers here ?

        s.write('some bad shit happened\n')

    });

}

how do I write a useful trail header to the response that can be displayed well by the browser?

I think this is the relevant spec for trail headers: https://www.rfc-editor.org/rfc/rfc2616#section-14.40

I think they should be called "trailing headers", but whatever.

Community
  • 1
  • 1

1 Answers1

1

Firstly:

I think this is the relevant spec for trail headers: https://www.rfc-editor.org/rfc/rfc2616#section-14.40

RFC 2616 has been obsoleted by RFC 7230. The current spec for trailers is RFC 7230 § 4.1.2.

Secondly:

].join('\n') + '\n\n'

Lines in HTTP message framing are terminated with \r\n, not \n.

Thirdly:

Content-Encoding: UTF-8

Content-Encoding is for content codings (like gzip), not charsets (like UTF-8). You probably don’t need to indicate charset separately from Content-Type.

And lastly:

how do I write a useful trail header to the response that can be displayed well by the browser?

You don’t. Mainstream Web browsers do not care about trailers.

See also (by the same user?): How to write malformed HTTP response to “guarantee” something akin to HTTP 500

Community
  • 1
  • 1
Vasiliy Faronov
  • 11,840
  • 2
  • 38
  • 49
  • ok thanks, so how do I tell the browser that the response is actually a 500 after I have written a header saying it's a 200? that would happen if reading the file went awry as I hopefully explained well in the question. –  May 11 '18 at 23:56
  • That depends on which browser, what kind of file, and what it’s requested for. Judging by `Content-Type` in your question, you might be trying to prevent execution of a ``. Then see [my answer to your other question](https://stackoverflow.com/questions/50299300/) — no. 1 seems to do the trick, at least in Chromium. Otherwise, ask a separate question with specifics. – Vasiliy Faronov May 12 '18 at 00:09
  • The charset goes into the content type, semicolon separated. e.g. "application/json;charset=utf-8" – Stefan Steiger Jul 14 '20 at 15:22
  • @StefanSteiger Only if the media type defines such a parameter. In particular, `application/json` doesn’t — see [RFC 8259 § 11](https://tools.ietf.org/html/rfc8259#section-11) (although some applications might support it anyway). – Vasiliy Faronov Jul 15 '20 at 12:06