<commit_before>/*
 * Copyright (c) 2011 <NAME> <<EMAIL>>
 * MIT Licensed
 */

function findKey(obj, val) {
    for (var n in obj) if (obj[n] === val) return n;
}

var url = require('url'),
    compress = null;

try { compress = require("compress"); } catch(e) {}

var Server = function(server, path, services, wsdl) {
    var self = this;

    this.path = path;     
    this.services = services;
    this.wsdl = wsdl;

    if (path[path.length-1] != '/') path += '/';
    wsdl.onReady(function(err) {
        var listeners = server.listeners('request').slice();

        server.removeAllListeners('request');
        server.addListener('request', function(req, res) {
            if (typeof self.authorizeConnection === 'function') {
              if (!self.authorizeConnection(req.connection.remoteAddress)) {
                res.end();
                return;
              }
            }
            var reqPath = url.parse(req.url).pathname;
            if (reqPath[reqPath.length-1] != '/') reqPath += '/';
            if (path === reqPath) {
                self._requestListener(req, res);
            }
            else {
                for (var i = 0, len = listeners.length; i < len; i++){
                  listeners[i].call(this, req, res);
                }            
            }
        });        
    })
}

Server.prototype._requestListener = function(req, res) {
    var self = this;
    var reqParse = url.parse(req.url);
    var reqPath = reqParse.pathname;
    var reqQuery = reqParse.search;

    self.log("info", "Handling "+req.method+" on "+req.url);

    if (req.method === 'GET') {
        if (reqQuery && reqQuery.toLowerCase() === '?wsdl') {
            self.log("info", "Wants the WSDL");
            res.setHeader("Content-Type", "application/xml");
            res.write(self.wsdl.toXML());
        }
        res.end();
    }
    else if (req.method === 'POST') {
        res.setHeader('Content-Type', req.headers['content-type']);
        var chunks = [], gunzip;
        if (compress && req.headers["content-encoding"] == "gzip") {
            gunzip = new compress.Gunzip;    
            gunzip.init();
        }
        req.on('data', function(chunk) {
            if (gunzip) chunk = gunzip.inflate(chunk, "binary");
            chunks.push(chunk);
        })
        req.on('end', function() {
            var xml = chunks.join(''), result;
            if (gunzip) {
                gunzip.end();
                gunzip = null
            }
            try {
                self._process(xml, req.url, function(result) {
                    res.write(result);
                    res.end();
                    if (typeof self.log === 'function') {
                      self.log("received", xml);
                      self.log("replied", result);
                    }
                });
            }
            catch(err) {
                err = err.stack || err;
                res.write(err);
                res.end();
                if (typeof self.log === 'function') {
                  self.log("error", err);
                }
            }
        });
    }
    else {
        res.end();
    }    
}

Server.prototype._process = function(input, URL, callback) {
    var self = this,
        pathname = url.parse(URL).pathname.replace(/\/$/,''),
        obj = this.wsdl.xmlToObject(input),
        body = obj.Body,
        bindings = this.wsdl.definitions.bindings, binding, 
        methods, method, methodName,
        serviceName, portName;
        
    if (typeof self.authenticate === 'function') {
      if (obj.Header == null || obj.Header.Security == null) {
        throw new Error('No security header');
      }
      if (!self.authenticate(obj.Header.Security)) {
        throw new Error('Invalid username or password');
      }
    }
    
    self.log("info", "Attempting to bind to "+pathname);

    // use port.location and current url to find the right binding
    binding = (function(self){
        var services = self.wsdl.definitions.services;
        var firstPort = undefined;
        for(serviceName in services ) {
            var service = services[serviceName];
            var ports = service.ports;
            for(portName in ports) {
                var port = ports[portName];
                var portPathname = url.parse(port.location).pathname.replace(/\/$/,'');
                self.log("info", "Trying "+portName+" from path "+portPathname);
                if(portPathname===pathname) 
                    return port.binding;
                    <commit_msg>Log only when logger is set; fixes tests<commit_after>/*
 * Copyright (c) 2011 <NAME> <<EMAIL>>
 * MIT Licensed
 */

function findKey(obj, val) {
    for (var n in obj) if (obj[n] === val) return n;
}

var url = require('url'),
    compress = null;

try { compress = require("compress"); } catch(e) {}

var Server = function(server, path, services, wsdl) {
    var self = this;

    this.path = path;     
    this.services = services;
    this.wsdl = wsdl;

    if (path[path.length-1] != '/') path += '/';
    wsdl.onReady(function(err) {
        var listeners = server.listeners('request').slice();

        server.removeAllListeners('request');
        server.addListener('request', function(req, res) {
            if (typeof self.authorizeConnection === 'function') {
              if (!self.authorizeConnection(req.connection.remoteAddress)) {
                res.end();
                return;
              }
            }
            var reqPath = url.parse(req.url).pathname;
            if (reqPath[reqPath.length-1] != '/') reqPath += '/';
            if (path === reqPath) {
                self._requestListener(req, res);
            }
            else {
                for (var i = 0, len = listeners.length; i < len; i++){
                  listeners[i].call(this, req, res);
                }            
            }
        });        
    })
}

Server.prototype._requestListener = function(req, res) {
    var self = this;
    var reqParse = url.parse(req.url);
    var reqPath = reqParse.pathname;
    var reqQuery = reqParse.search;

    if (typeof self.log === 'function') {
        self.log("info", "Handling "+req.method+" on "+req.url);
    }

    if (req.method === 'GET') {
        if (reqQuery && reqQuery.toLowerCase() === '?wsdl') {
            if (typeof self.log === 'function') {
                self.log("info", "Wants the WSDL");
            }
            res.setHeader("Content-Type", "application/xml");
            res.write(self.wsdl.toXML());
        }
        res.end();
    }
    else if (req.method === 'POST') {
        res.setHeader('Content-Type', req.headers['content-type']);
        var chunks = [], gunzip;
        if (compress && req.headers["content-encoding"] == "gzip") {
            gunzip = new compress.Gunzip;    
            gunzip.init();
        }
        req.on('data', function(chunk) {
            if (gunzip) chunk = gunzip.inflate(chunk, "binary");
            chunks.push(chunk);
        })
        req.on('end', function() {
            var xml = chunks.join(''), result;
            if (gunzip) {
                gunzip.end();
                gunzip = null
            }
            try {
                self._process(xml, req.url, function(result) {
                    res.write(result);
                    res.end();
                    if (typeof self.log === 'function') {
                      self.log("received", xml);
                      self.log("replied", result);
                    }
                });
            }
            catch(err) {
                err = err.stack || err;
                res.write(err);
                res.end();
                if (typeof self.log === 'function') {
                  self.log("error", err);
                }
            }
        });
    }
    else {
        res.end();
    }    
}

Server.prototype._process = function(input, URL, callback) {
    var self = this,
        pathname = url.parse(URL).pathname.replace(/\/$/,''),
        obj = this.wsdl.xmlToObject(input),
        body = obj.Body,
        bindings = this.wsdl.definitions.bindings, binding, 
        methods, method, methodName,
        serviceName, portName;
        
    if (typeof self.authenticate === 'function') {
      if (obj.Header == null || obj.Header.Security == null) {
        throw new Error('No security header');
      }
      if (!self.authenticate(obj.Header.Security)) {
        throw new Error('Invalid username or password');
      }
    }
    
    if (typeof self.log === 'function') {
        self.log("info", "Attempting to bind to "+pathname);
    }

    // use port.location and current url to find the right binding
    binding = (function(self){
        var services = self.wsdl.definitions.services;
        var firstPort = undefined;
        for(serviceName in services ) {
            var service = services[serviceName];
            var ports = service.ports;
            for(portName in ports) {
                var port = ports[portName];
                var portPathname = url.parse(port.location).pathname.replace(/\/$/,'');

                if (typeof self.log === 'function') {
                    self.log("info", "Trying "+portName+" from path "+portPathname);
                }

                if(portPathname===pathname) 
                    return port.binding;
                    
{'fraction_non_alphanumeric': 0.14541387024608501, 'fraction_numerical': 0.0014914243102162564, 'mean_word_length': 1.5933701657458563, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, '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': '1144872', 'n_tokens_mistral': 2701, 'n_tokens_neox': 2594, 'n_words': 585}