Name: js-handler/node_modules/restify/lib/clients/string_client.js
| 1: | // Copyright 2012 Mark Cavage, Inc. All rights reserved. |
| 2: | |
| 3: | var crypto = require('crypto'); |
| 4: | var zlib = require('zlib'); |
| 5: | |
| 6: | var assert = require('assert-plus'); |
| 7: | var qs = require('querystring'); |
| 8: | var util = require('util'); |
| 9: | |
| 10: | var HttpClient = require('./http_client'); |
| 11: | |
| 12: | |
| 13: | |
| 14: | ///--- Helpers |
| 15: | |
| 16: | |
| 17: | ///--- API |
| 18: | |
| 19: | function StringClient(options) { |
| 20: | assert.object(options, 'options'); |
| 21: | assert.optionalObject(options.gzip, 'options.gzip'); |
| 22: | |
| 23: | options.accept = options.accept || 'text/plain'; |
| 24: | options.name = options.name || 'StringClient'; |
| 25: | options.contentType = |
| 26: | options.contentType || 'application/x-www-form-urlencoded'; |
| 27: | |
| 28: | HttpClient.call(this, options); |
| 29: | this.gzip = options.gzip; |
| 30: | } |
| 31: | util.inherits(StringClient, HttpClient); |
| 32: | module.exports = StringClient; |
| 33: | |
| 34: | |
| 35: | StringClient.prototype.post = function post(options, body, callback) { |
| 36: | var opts = this._options('POST', options); |
| 37: | if (typeof (body) === 'function') { |
| 38: | callback = body; |
| 39: | body = null; |
| 40: | } |
| 41: | |
| 42: | return (this.write(opts, body, callback)); |
| 43: | }; |
| 44: | |
| 45: | |
| 46: | StringClient.prototype.put = function put(options, body, callback) { |
| 47: | var opts = this._options('PUT', options); |
| 48: | if (typeof (body) === 'function') { |
| 49: | callback = body; |
| 50: | body = null; |
| 51: | } |
| 52: | |
| 53: | return (this.write(opts, body, callback)); |
| 54: | }; |
| 55: | |
| 56: | |
| 57: | StringClient.prototype.patch = function patch(options, body, callback) { |
| 58: | var opts = this._options('PATCH', options); |
| 59: | if (typeof (body) === 'function') { |
| 60: | callback = body; |
| 61: | body = null; |
| 62: | } |
| 63: | |
| 64: | return (this.write(opts, body, callback)); |
| 65: | }; |
| 66: | |
| 67: | |
| 68: | StringClient.prototype.read = function read(options, callback) { |
| 69: | var self = this; |
| 70: | this.request(options, function _parse(err, req) { |
| 71: | if (err) |
| 72: | return (callback(err, req)); |
| 73: | |
| 74: | req.once('result', self.parse(req, callback)); |
| 75: | return (req.end()); |
| 76: | }); |
| 77: | return (this); |
| 78: | }; |
| 79: | |
| 80: | |
| 81: | StringClient.prototype.write = function write(options, body, callback) { |
| 82: | if (body !== null && typeof (body) !== 'string') |
| 83: | body = qs.stringify(body); |
| 84: | |
| 85: | var self = this; |
| 86: | |
| 87: | function _write(data) { |
| 88: | if (data) { |
| 89: | var hash = crypto.createHash('md5'); |
| 90: | hash.update(data, 'utf8'); |
| 91: | options.headers['content-md5'] = hash.digest('base64'); |
| 92: | } |
| 93: | |
| 94: | self.request(options, function (err, req) { |
| 95: | if (err) { |
| 96: | callback(err, req); |
| 97: | return; |
| 98: | } |
| 99: | |
| 100: | req.once('result', self.parse(req, callback)); |
| 101: | req.end(data); |
| 102: | }); |
| 103: | } |
| 104: | |
| 105: | options.headers = options.headers || {}; |
| 106: | |
| 107: | if (this.gzip) |
| 108: | options.headers['accept-encoding'] = 'gzip'; |
| 109: | |
| 110: | if (body) { |
| 111: | if (this.gzip) { |
| 112: | options.headers['content-encoding'] = 'gzip'; |
| 113: | zlib.gzip(body, function (err, data) { |
| 114: | if (err) { |
| 115: | callback(err, null); |
| 116: | return; |
| 117: | } |
| 118: | |
| 119: | options.headers['content-length'] = data.length; |
| 120: | _write(data); |
| 121: | }); |
| 122: | } else { |
| 123: | options.headers['content-length'] = |
| 124: | Buffer.byteLength(body); |
| 125: | _write(body); |
| 126: | } |
| 127: | } else { |
| 128: | _write(); |
| 129: | } |
| 130: | |
| 131: | return (this); |
| 132: | }; |
| 133: | |
| 134: | |
| 135: | StringClient.prototype.parse = function parse(req, callback) { |
| 136: | function parseResponse(err, res) { |
| 137: | if (res) { |
| 138: | function done() { |
| 139: | res.log.trace('body received:\n%s', body); |
| 140: | res.body = body; |
| 141: | if (hash && md5 !== hash.digest('base64')) { |
| 142: | err = new Error('BadDigest'); |
| 143: | callback(err, req, res); |
| 144: | return; |
| 145: | } |
| 146: | |
| 147: | if (err) { |
| 148: | err.body = body; |
| 149: | err.message = body; |
| 150: | } |
| 151: | |
| 152: | callback(err, req, res, body); |
| 153: | } |
| 154: | |
| 155: | var body = ''; |
| 156: | var gz; |
| 157: | var hash; |
| 158: | var md5 = res.headers['content-md5']; |
| 159: | if (md5 && req.method !== 'HEAD') |
| 160: | hash = crypto.createHash('md5'); |
| 161: | |
| 162: | if (res.headers['content-encoding'] === 'gzip') { |
| 163: | gz = zlib.createGunzip(); |
| 164: | gz.on('data', function (chunk) { |
| 165: | body += chunk.toString('utf8'); |
| 166: | }); |
| 167: | gz.once('end', done); |
| 168: | res.once('end', gz.end.bind(gz)); |
| 169: | } else { |
| 170: | res.setEncoding('utf8'); |
| 171: | res.once('end', done); |
| 172: | } |
| 173: | |
| 174: | res.on('data', function onData(chunk) { |
| 175: | if (hash) |
| 176: | hash.update(chunk); |
| 177: | |
| 178: | if (gz) { |
| 179: | gz.write(chunk); |
| 180: | } else { |
| 181: | body += chunk; |
| 182: | } |
| 183: | }); |
| 184: | |
| 185: | } else { |
| 186: | callback(err, req, null, null); |
| 187: | } |
| 188: | } |
| 189: | |
| 190: | return (parseResponse); |
| 191: | }; |
