Ignore:
Timestamp:
03/23/14 17:26:12 (11 years ago)
Author:
hendrikvanantwerpen
Message:
  • Dropped request module for a simple one we wrote ourselves using the native Node modules. Hopefully this one will not suffer from the problem that sometimes empty bodies are returned even when the statuscode and content-length of the request are ok & present.
  • Handle exceptions better in HTTPResult chain, they were hoisted in unknown responses before. Should we not include them in default processing, because they are special?
Location:
Dev/trunk/src/server/util
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • Dev/trunk/src/server/util/couch.js

    r525 r527  
    11var request = require('./request')
    22  , _ = require('underscore')
     3  , HTTPResult = require('./http-result')
    34  ;
    45
     
    4243}
    4344
    44 function couchRequest (method, url, body, opts) {
     45function couchRequest (method, url, body, opts, retries) {
     46    body = body || {};
     47    retries = _.isNumber(retries) ? retries : 3;
    4548    var options = {
    4649        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
    5254    };
    5355    if (opts) {
     
    5658    }
    5759    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    });
    6271}
    6372
  • Dev/trunk/src/server/util/http-result.js

    r525 r527  
    3939// _fire :: Handler -> ()
    4040HTTPResult.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);
    4651    }
    4752};
  • Dev/trunk/src/server/util/request.js

    r525 r527  
    11var HTTPResult = require('./http-result')
    2   , request = require('request')
    3   , url = require('url')
     2  , HTTP = require('http')
     3  , HTTPS = require('https')
     4  , URL = require('url')
    45  , _ = require('underscore')
    56  ;
     
    910    var result = new HTTPResult();
    1011   
    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        });
    2062    });
     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();
    2171
    2272    return result;
Note: See TracChangeset for help on using the changeset viewer.