333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
"use strict";
/*
 * Copyright (c) 2011 Vinay Pulim <vinay@milewise.com>
 * MIT Licensed
 */
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
            t[p[i]] = s[p[i]];
    return t;
};
exports.__esModule = true;
var debugBuilder = require("debug");
var httpNtlm = require("httpntlm");
var req = require("request");
var url = require("url");
var uuid_1 = require("uuid");
var debug = debugBuilder('node-soap');
var VERSION = require('../package.json').version;
/**
 * A class representing the http client
 * @param {Object} [options] Options object. It allows the customization of
 * `request` module
 *
 * @constructor
 */
var HttpClient = /** @class */ (function () {
    function HttpClient(options) {
        options = options || {};
        this._request = options.request || req;
    }
    /**
     * Build the HTTP request (method, uri, headers, ...)
     * @param {String} rurl The resource url
     * @param {Object|String} data The payload
     * @param {Object} exheaders Extra http headers
     * @param {Object} exoptions Extra options
     * @returns {Object} The http request object for the `request` module
     */
    HttpClient.prototype.buildRequest = function (rurl, data, exheaders, exoptions) {
        if (exoptions === void 0) { exoptions = {}; }
        var curl = url.parse(rurl);
        var secure = curl.protocol === 'https:';
        var host = curl.hostname;
        var port = parseInt(curl.port, 10);
        var path = [curl.pathname || '/', curl.search || '', curl.hash || ''].join('');
        var method = data ? 'POST' : 'GET';
        var headers = {
            'User-Agent': 'node-soap/' + VERSION,
            'Accept': 'text/html,application/xhtml+xml,application/xml,text/xml;q=0.9,*/*;q=0.8',
            'Accept-Encoding': 'none',
            'Accept-Charset': 'utf-8',
            'Connection': exoptions.forever ? 'keep-alive' : 'close',
            'Host': host + (isNaN(port) ? '' : ':' + port)
        };
        var mergeOptions = ['headers'];
        var _attachments = exoptions.attachments, newExoptions = __rest(exoptions, ["attachments"]);
        var attachments = _attachments || [];
        if (typeof data === 'string' && attachments.length === 0 && !exoptions.forceMTOM) {
            headers['Content-Length'] = Buffer.byteLength(data, 'utf8');
            headers['Content-Type'] = 'application/x-www-form-urlencoded';
        }
        exheaders = exheaders || {};
        for (var attr in exheaders) {
            headers[attr] = exheaders[attr];
        }
        var options = {
            uri: curl,
            method: method,
            headers: headers,
            followAllRedirects: true
        };
        if (exoptions.forceMTOM || attachments.length > 0) {
            var start = uuid_1.v4();
            var action = null;
            if (headers['Content-Type'].indexOf('action') > -1) {
                for (var _i = 0, _a = headers['Content-Type'].split('; '); _i < _a.length; _i++) {
                    var ct = _a[_i];
                    if (ct.indexOf('action') > -1) {
                        action = ct;
                    }
                }
            }
            headers['Content-Type'] =
                'multipart/related; type="application/xop+xml"; start="<' + start + '>"; start-info="text/xml"; boundary=' + uuid_1.v4();
            if (action) {
                headers['Content-Type'] = headers['Content-Type'] + '; ' + action;
            }
            var multipart_1 = [{
                    'Content-Type': 'application/xop+xml; charset=UTF-8; type="text/xml"',
                    'Content-ID': '<' + start + '>',
                    'body': data
                }];
            attachments.forEach(function (attachment) {
                multipart_1.push({
                    'Content-Type': attachment.mimetype,
                    'Content-Transfer-Encoding': 'binary',
                    'Content-ID': '<' + attachment.contentId + '>',
                    'Content-Disposition': 'attachment; filename="' + attachment.name + '"',
                    'body': attachment.body
                });
            });
            options.multipart = multipart_1;
        }
        else {
            options.body = data;
        }
        for (var attr in newExoptions) {
            if (mergeOptions.indexOf(attr) !== -1) {
                for (var header in exoptions[attr]) {
                    options[attr][header] = exoptions[attr][header];
                }
            }
            else {
                options[attr] = exoptions[attr];
            }
        }
        debug('Http request: %j', options);
        return options;
    };
    /**
     * Handle the http response
     * @param {Object} The req object
     * @param {Object} res The res object
     * @param {Object} body The http body
     * @param {Object} The parsed body
     */
    HttpClient.prototype.handleResponse = function (req, res, body) {
        debug('Http response body: %j', body);
        if (typeof body === 'string') {
            // Remove any extra characters that appear before or after the SOAP
            // envelope.
            var match = body.replace(/<!--[\s\S]*?-->/, '').match(/(?:<\?[^?]*\?>[\s]*)?<([^:]*):Envelope([\S\s]*)<\/\1:Envelope>/i);
            if (match) {
                body = match[0];
            }
        }
        return body;
    };
    HttpClient.prototype.request = function (rurl, data, callback, exheaders, exoptions, caller) {
        var _this = this;
        var options = this.buildRequest(rurl, data, exheaders, exoptions);
        var req;
        if (exoptions !== undefined && exoptions.hasOwnProperty('ntlm')) {
            // sadly when using ntlm nothing to return
            // Not sure if this can be handled in a cleaner way rather than an if/else,
            // will to tidy up if I get chance later, patches welcome - insanityinside
            // TODO - should the following be uri?
            options.url = rurl;
            httpNtlm[options.method.toLowerCase()](options, function (err, res) {
                if (err) {
                    return callback(err);
                }
                // if result is stream
                if (typeof res.body !== 'string') {
                    res.body = res.body.toString();
                }
                res.body = _this.handleResponse(req, res, res.body);
                callback(null, res, res.body);
            });
        }
        else {
            req = this._request(options, function (err, res, body) {
                if (err) {
                    return callback(err);
                }
                body = _this.handleResponse(req, res, body);
                callback(null, res, body);
            });
        }
        return req;
    };
    HttpClient.prototype.requestStream = function (rurl, data, exheaders, exoptions, caller) {
        var options = this.buildRequest(rurl, data, exheaders, exoptions);
        return this._request(options);
    };
    return HttpClient;
}());
exports.HttpClient = HttpClient;
//# sourceMappingURL=http.js.map