Added logging support; Currently uses syslog(3) to LOG_AUTH facility
authorMatthew Mondor <mmondor@pulsar-zone.net>
Fri, 24 Nov 2006 00:53:54 +0000 (00:53 +0000)
committerMatthew Mondor <mmondor@pulsar-zone.net>
Fri, 24 Nov 2006 00:53:54 +0000 (00:53 +0000)
mmsoftware/js/js-sh/app/httpd/httpd.js

index 8846c25..5622c5d 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: httpd.js,v 1.43 2006/11/22 23:05:47 mmondor Exp $ */
+/* $Id: httpd.js,v 1.44 2006/11/24 00:53:54 mmondor Exp $ */
 
 /*
  * Copyright (c) 2005-2006, Matthew Mondor
@@ -76,7 +76,7 @@
  * Server identification
  */
 const SERVER_VERSION = 'mmondor_js_httpd/0.2.1 (NetBSD)';
-const SERVER_CVSID = '$Id: httpd.js,v 1.43 2006/11/22 23:05:47 mmondor Exp $';
+const SERVER_CVSID = '$Id: httpd.js,v 1.44 2006/11/24 00:53:54 mmondor Exp $';
 
 
 
@@ -436,16 +436,33 @@ HTTPReply.prototype = {
        {
                var headers = '';
                var contents = '';
-               var i;
+               var i, t;
 
                for (i = 0; i < this.contents.length; i++)
                        contents += this.contents[i];
 
+               /*
+               70.50.245.131 - - [23/Nov/2006:16:43:40 -0500] ascpi.net "POST
+                  /inscription/admin_recherche_do.php HTTP/1.0" 302 167 "-"
+                  "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.8)
+                  Gecko/20050511 Firefox/1.0.4"
+               LogFormat "%h %l %u %t %v \"%r\" %>s %b \"%{Referer}i\"
+               \"%{User-Agent}i\"" combined
+               */
+
+               t = new Date();
+               Syslog.log(Syslog.LOG_NOTICE, fd.client_addr + ' - - ' +
+                   http_log_date() + ' ' + fd.http_vhost.name + ' "' +
+                   fd.http_request + '" ' + this.code + ' ' +
+                   contents.length + ' "' + fd.http_referer + '" "' +
+                   fd.http_agent + '"');
+
                this.headers.push('Content-Length: ' +
                    (size == null ? contents.length : size));
-               /* XXX */
+               /* XXX
                stdout.write(' --> ' + this.code + ' ' +
                    this.headers.toSource() + "\n");
+                */
                headers += 'HTTP/1.1 ' + this.code + ' ' + this.desc + "\r\n";
                for (i = 0; i < this.headers.length; i++)
                        headers += this.headers[i] + "\r\n";
@@ -509,6 +526,53 @@ function http_redirect(fd, vpath)
        res.flush(fd, null);
 }
 
+
+function http_log_init()
+{
+       var i;
+
+       /* XXX Make facility configurable */
+       Syslog.open('js-httpd', Syslog.LOG_PID | Syslog.LOG_NDELAY,
+           Syslog.LOG_AUTH);
+
+       http_log_months = [
+          'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
+          'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ];
+
+       http_log_digits = [];
+       for (i = 0; i < 60; i++) {
+               if (i < 10)
+                       http_log_digits[i] = '0' + i;
+               else
+                       http_log_digits[i] = i;
+       }
+
+       http_log_zone = (new Date).getTimezoneOffset() / 60;
+       if (http_log_zone > 0) {
+               if (http_log_zone < 10)
+                       http_log_zone = '0' + http_log_zone;
+               http_log_zone = '+' + http_log_zone;
+       } else {
+               http_log_zone = http_log_zone.toString();
+               if (http_log_zone.length < 3)
+                       http_log_zone = '-0' + http_log_zone.charAt(1);
+       }
+       http_log_zone += '00';
+}
+
+function http_log_date()
+{       
+       var t = new Date();
+       var s = '[' + t.getDate() + '/' + http_log_months[t.getMonth()] + '/' +
+           (1900 + t.getYear()) + ':' + http_log_digits[t.getHours()] + ':' +
+           http_log_digits[t.getMinutes()] + ':' +
+           http_log_digits[t.getSeconds()] + ' ' +
+           http_log_zone + ']';
+
+       return s;
+}
+
+
 /*
  * Add new methods to the FD prototype object (JavaScript is great like that).
  * We need to add these properties one by one to the prototype object of FD,
@@ -768,29 +832,33 @@ FD.prototype.parseQuery = function(time)
        /* Initial HTTP query state */
        this.http_protocol = '';
        this.http_method = '';
+       this.http_request = undefined;
        this.http_vhost = this.http_host = 'n/a';
        this.http_path = '';
        this.http_vars_get = {};
        this.http_vars_post = {};
        this.http_vars_cookies = {};
        this.http_vars_cookies_count = 0;
-       this.http_agent = '';
+       this.http_agent = '-';
        this.http_content_length = -1;
        this.http_modified_since = undefined;
        /*
        this.http_sessid = undefined;
        */
        this.http_range = undefined;
+       this.http_referer = '-';
        this.http_old_get = false;
 
        /* Split request lines */
        lines = this.query_data;
-       /* XXX */
+       /* XXX
        stdout.write(this.client_addr + ':' + this.client_port + ' ' +
            lines.toSource());
+        */
 
        /* Verify if first line has a request which seems valid */
        if (lines.length > 0) {
+               this.http_request = lines[0];
                words = lines[0].split(' ');
                if (words.length == 2 && words[0] == 'GET') {
                        this.http_method = words[0];
@@ -847,6 +915,8 @@ FD.prototype.parseQuery = function(time)
                                    Date.parse(lines[i].substr(19)) / 1000);
                        else if (w == 'range:')
                                this.http_range = lines[i].substr(7);
+                       else if (w == 'referer:')
+                               this.http_referer = lines[i].substr(9);
                }
        }
 
@@ -1715,6 +1785,8 @@ function main() {
        */
        var fd, e, efd, flush;
 
+       http_log_init();
+
        /*
         * Populate vhosts database
         */