<commit_before>var hyperquest = require('hyperquest')
  , XML = require('simple-xml')
  , StringStream = require('stream-ext').StringStream
  , zlib = require('zlib')
  ;

module.exports = function soap (uri, operation, action, message, options, callback) {
  if (typeof options === 'function') {
    callback = options;
    options = {};
  }
  var xml = envelope(operation, message, options);
  if (options.benchmark) console.time('soap request: ' + uri);

  var stream = new StringStream();
  stream.on('error', callback);
  stream.on('end', function (data) {
    if (options.benchmark) console.timeEnd('soap request: ' + uri);
    try {
      var obj = XML.parse(data)['Envelope']['Body'];
      callback(null, obj);
    }
    catch (err) {
      callback(err);
    }
  });

  var req = hyperquest.post(uri, {
    headers: headers(action, xml.length),
    rejectUnauthorized: options.rejectUnauthorized,
    secureProtocol: options.secureProtocol
  });
  req.on('error', callback);
  req.on('response', function (res) {
    if (isGzipped(res))
      res.pipe(gunzip(callback)).pipe(stream);
    else
      res.pipe(stream);
  });
  req.end(xml);
};

function envelope (operation, message, options) {
  var xml = '<?xml version="1.0" encoding="UTF-8"?>';
  xml += '<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
    'xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" ' + namespaces(options.namespaces) + '>';
  if (options.header) xml += '<env:Header>' + XML.stringify(options.header) + '</env:Header>';
  xml += '<env:Body>';
  xml += serializeOperation(operation, options); // '<' + operation + ' xmlns="' + options.namespace + '"' + '>';
  xml += XML.stringify(message);
  xml += '</' + operation + '>';
  xml += '</env:Body>';
  xml += '</env:Envelope>';

  return xml;
}

function headers (schema, length) {
  return {
    Soapaction: schema,
    'Content-Type': 'text/xml;charset=UTF-8',<commit_msg>possibility to set a plain string as the header option<commit_after>var hyperquest = require('hyperquest')
  , XML = require('simple-xml')
  , StringStream = require('stream-ext').StringStream
  , zlib = require('zlib')
  ;

module.exports = function soap (uri, operation, action, message, options, callback) {
  if (typeof options === 'function') {
    callback = options;
    options = {};
  }
  var xml = envelope(operation, message, options);
  if (options.benchmark) console.time('soap request: ' + uri);

  var stream = new StringStream();
  stream.on('error', callback);
  stream.on('end', function (data) {
    if (options.benchmark) console.timeEnd('soap request: ' + uri);
    try {
      var obj = XML.parse(data)['Envelope']['Body'];
      callback(null, obj);
    }
    catch (err) {
      callback(err);
    }
  });

  var req = hyperquest.post(uri, {
    headers: headers(action, xml.length),
    rejectUnauthorized: options.rejectUnauthorized,
    secureProtocol: options.secureProtocol
  });
  req.on('error', callback);
  req.on('response', function (res) {
    if (isGzipped(res))
      res.pipe(gunzip(callback)).pipe(stream);
    else
      res.pipe(stream);
  });
  req.end(xml);
};

function envelope (operation, message, options) {
  var xml = '<?xml version="1.0" encoding="UTF-8"?>';
  xml += '<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
    'xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" ' + namespaces(options.namespaces) + '>';

  if (options.header) {
    xml += '<env:Header>';
    xml += typeof options.header == 'object' ? XML.stringify(options.header) : options.header.toString();
    xml += '</env:Header>';
  }

  xml += '<env:Body>';
  xml += serializeOperation(operation, options); // '<' + operation + ' xmlns="' + options.namespace + '"' + '>';
  xml += XML.stringify(message);
  xml += '</' + operation + '>';
  xml += '</env:Body>';
  xml += '</env:Envelope>';

  return xml;
}

function headers (schema, length) {
  return {
    Soapaction: schema,
    'Content-Type': 'text/xml;charset=UTF-8',
{'fraction_non_alphanumeric': 0.18955842888509392, 'fraction_numerical': 0.006830934374237619, 'mean_word_length': 3.7785547785547786, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 2, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 44}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4448507', 'n_tokens_mistral': 1349, 'n_tokens_neox': 1312, 'n_words': 270}