11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst http = require('http');
51cb0ef41Sopenharmony_ciconst MakeDuplexPair = require('../common/duplexpair');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci// Regression test for the crash reported in
81cb0ef41Sopenharmony_ci// https://github.com/nodejs/node/issues/15102 (httpParser.finish() is called
91cb0ef41Sopenharmony_ci// during httpParser.execute()):
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci{
121cb0ef41Sopenharmony_ci  const { clientSide, serverSide } = MakeDuplexPair();
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci  serverSide.on('data', common.mustCall((data) => {
151cb0ef41Sopenharmony_ci    assert.strictEqual(data.toString('utf8'), `\
161cb0ef41Sopenharmony_ciGET / HTTP/1.1
171cb0ef41Sopenharmony_ciExpect: 100-continue
181cb0ef41Sopenharmony_ciHost: localhost:80
191cb0ef41Sopenharmony_ciConnection: close
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci`.replace(/\n/g, '\r\n'));
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci    setImmediate(() => {
241cb0ef41Sopenharmony_ci      serverSide.write('HTTP/1.1 100 Continue\r\n\r\n');
251cb0ef41Sopenharmony_ci    });
261cb0ef41Sopenharmony_ci  }));
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci  const req = http.request({
291cb0ef41Sopenharmony_ci    createConnection: common.mustCall(() => clientSide),
301cb0ef41Sopenharmony_ci    headers: {
311cb0ef41Sopenharmony_ci      'Expect': '100-continue'
321cb0ef41Sopenharmony_ci    }
331cb0ef41Sopenharmony_ci  });
341cb0ef41Sopenharmony_ci  req.on('continue', common.mustCall((res) => {
351cb0ef41Sopenharmony_ci    let sync = true;
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci    clientSide._writev = null;
381cb0ef41Sopenharmony_ci    clientSide._write = common.mustCall((chunk, enc, cb) => {
391cb0ef41Sopenharmony_ci      assert(sync);
401cb0ef41Sopenharmony_ci      // On affected versions of Node.js, the error would be emitted on `req`
411cb0ef41Sopenharmony_ci      // synchronously (i.e. before commit f663b31cc2aec), which would cause
421cb0ef41Sopenharmony_ci      // parser.finish() to be called while we are here in the 'continue'
431cb0ef41Sopenharmony_ci      // callback, which is inside a parser.execute() call.
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci      assert.strictEqual(chunk.length, 4);
461cb0ef41Sopenharmony_ci      clientSide.destroy(new Error('sometimes the code just doesn’t work'), cb);
471cb0ef41Sopenharmony_ci    });
481cb0ef41Sopenharmony_ci    req.on('error', common.mustCall());
491cb0ef41Sopenharmony_ci    req.end('data');
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci    sync = false;
521cb0ef41Sopenharmony_ci  }));
531cb0ef41Sopenharmony_ci}
54