spdy-http2 / node-spdy

SPDY server on Node.js
2.81k stars 196 forks source link

json response is truncated when the protocal is http2 #357

Open houfeng0923 opened 5 years ago

houfeng0923 commented 5 years ago

i found a problem in webpack-dev-server(use express and spdy ) ,when the protocal is http2, some big json response is truncated . it not happen when use http1.1 .

so i made a test code for proxy server based on express and spay :

var fs = require('fs')
var spdy = require('spdy')
var express = require('express')
var proxy = require('http-proxy-middleware')

// proxy middleware options
var options = {
  target: 'https://your api server/', // support http2
  secure: false,
  changeOrigin: true,
}

var exampleProxy = proxy(options)

var app = express()
app.use('/api', exampleProxy)

var httpsOptions = {
  key: fs.readFileSync('~Library/Application Support/mkcert/localhost+3-key.pem'),
  cert: fs.readFileSync('~/Library/Application Support/mkcert/localhost+3.pem'),
  // spdy: {
  //    protocols: ['h2', 'http/1.1'], //  optional
  // }
};

spdy.createServer(httpOptions, app).listen(3000)
curl https://127.0.0.1:3000/api/v1/response-long-json-string -http2

the truncated response like below:

image

now, i am not sure the reason in spdy or proxy . maybe it has related to chimurai/http-proxy-middleware#79

morkai commented 5 years ago

I've encountered a similar issue. I'm using spdy through redbird. Big response body is sometimes truncated but only in Firefox.

Server:

'use strict';

const fs = require('fs');
const http = require('spdy');

let data = [];

for (let i = 0; i < 1000000; ++i)
{
  data.push(Math.random());
}

data = JSON.stringify(data);

const server = http.createServer({
  key: fs.readFileSync(`./config/development/ssl/privkey.pem`, 'utf8'),
  cert: fs.readFileSync(`./config/development/ssl/fullchain.pem`, 'utf8')
});

server.listen(8443);

server.on('request', (req, res) =>
{
  console.log('>>>');
  console.log(`${req.method} ${req.url}`);
  console.log(req.headers);

  res.setHeader('Content-Type', 'application/json; charset=utf-8');
  res.setHeader('Access-Control-Allow-Origin', '*');
  res.setHeader('Access-Control-Allow-Methods', 'GET');
  res.setHeader('Access-Control-Allow-Headers', req.headers['access-control-request-headers'] || '');

  if (req.method === 'GET')
  {
    res.setHeader('Content-Length', data.length.toString());
    res.writeHead(200);
    res.end(data);
  }
  else
  {
    res.writeHead(200);
    res.end();
  }
});

curl 7.65.1 works everytime:

curl https://dev.wmes.pl:8443/ --http2-prior-knowledge > test.json

Chrome 74.0.3729.169 works everytime:

fetch('https://dev.wmes.pl:8443/')
  .then(res => res.json())
  .then(() => console.log('DONE!'));

The above fetch fails often in Firefox 68.0b10.

Firefox seems to be always working if spdy is replaced with nginx (with http2 enabled).

DEBUG=spdy:* logs: spdy-http2-debug-logs.zip

skerit commented 5 years ago

I have a similar issue: I request a (large) javascript file and by the end it is completely garbled.

Around 4/5ths of the file I start seeing this:

afbeelding

And by the end it is completely unrecognizable:

afbeelding

I downgraded tot 3.4.7 and the problem was fixed.