Changeset 527 for Dev/trunk/src/server/util
- Timestamp:
- 03/23/14 17:26:12 (11 years ago)
- Location:
- Dev/trunk/src/server/util
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
Dev/trunk/src/server/util/couch.js
r525 r527 1 1 var request = require('./request') 2 2 , _ = require('underscore') 3 , HTTPResult = require('./http-result') 3 4 ; 4 5 … … 42 43 } 43 44 44 function couchRequest (method, url, body, opts) { 45 function couchRequest (method, url, body, opts, retries) { 46 body = body || {}; 47 retries = _.isNumber(retries) ? retries : 3; 45 48 var options = { 46 49 method: method, 47 headers: { 48 'Content-Type': 'application/json; charset=utf-8', 49 'Accept': 'application/json' 50 }, 51 body: JSON.stringify(stringifyFunctions(body || {})) 50 headers: {}, 51 body: stringifyFunctions(body), 52 json: true, 53 timeout: 5000 52 54 }; 53 55 if (opts) { … … 56 58 } 57 59 return request(url, options) 58 .handle({ 59 '-1': _.identity, 60 default: function(status,result) { return JSON.parse(result); } 61 }); 60 .handle(function(status,body){ 61 if ( _.isUndefined(body) ) { 62 if ( retries > 0) { 63 return couchRequest(method,url,body,opts,retries-1); 64 } else { 65 return new HTTPResult(-1,new Error("No response after several retries.")); 66 } 67 } else { 68 return body; 69 } 70 }); 62 71 } 63 72 -
Dev/trunk/src/server/util/http-result.js
r525 r527 39 39 // _fire :: Handler -> () 40 40 HTTPResult.prototype._fire = function(handler) { 41 var ret = handler.f(this.status,this.result); 42 if ( ret instanceof HTTPResult ) { 43 ret.handle(handler.next.set.bind(handler.next)); 44 } else { 45 handler.next.set(this.status,ret); 41 try { 42 var ret = handler.f(this.status,this.result); 43 if ( ret instanceof HTTPResult ) { 44 ret.handle(handler.next.set.bind(handler.next)); 45 } else { 46 handler.next.set(this.status,ret); 47 } 48 } catch(ex) { 49 console.log('HTTPResult','exception',ex.toString(),ex.stack); 50 handler.next.set(-1,ex); 46 51 } 47 52 }; -
Dev/trunk/src/server/util/request.js
r525 r527 1 1 var HTTPResult = require('./http-result') 2 , request = require('request') 3 , url = require('url') 2 , HTTP = require('http') 3 , HTTPS = require('https') 4 , URL = require('url') 4 5 , _ = require('underscore') 5 6 ; … … 9 10 var result = new HTTPResult(); 10 11 11 options = options ? _.clone(options) : {}; 12 options.uri = url || options.uri; 13 14 request(options,function(err,res,body){ 15 if ( err ) { 16 result.set(-1,err); 17 } else { 18 result.set(res.statusCode,body); 19 } 12 var body = options.body; 13 delete options.body; 14 15 var json = options.json; 16 delete options.json; 17 18 var reqOpts = _.isString(url) ? URL.parse(url) : url; 19 _.extend(reqOpts,options); 20 if ( json === true ) { 21 options.headers = options.headers || {}; 22 _.extend(options.headers,{ 23 'Content-Type': 'application/json', 24 'Accepts': 'application/json' 25 }); 26 } 27 28 var protocol = reqOpts.protocol; 29 var httpModule; 30 if ( protocol === 'http:' ) { 31 httpModule = HTTP; 32 } else if ( protocol === 'https:' ) { 33 httpModule = HTTPS; 34 } else { 35 throw new Error("Unknown protocol "+protocol); 36 } 37 38 var req = httpModule.request(reqOpts); 39 req.on('response', function(response) { 40 var data = ""; 41 response.setEncoding('utf8'); 42 response.on('data', function(chunk) { 43 data += chunk; 44 }); 45 response.on('end', function() { 46 var body; 47 try { 48 if ( json ) { 49 if ( data ) { body = JSON.parse(data); } 50 } else { 51 body = data; 52 } 53 result.set(response.statusCode,body); 54 } catch (ex) { 55 console.log('error',data); 56 result.set(-1,ex); 57 } 58 }); 59 response.on('error', function(error) { 60 result.set(-1,error); 61 }); 20 62 }); 63 req.on('error', function(error){ 64 result.set(-1,error); 65 }); 66 67 if ( body ) { 68 req.write(json ? JSON.stringify(body) : body); 69 } 70 req.end(); 21 71 22 72 return result;
Note: See TracChangeset
for help on using the changeset viewer.