*** empty log message ***
authorMatthew Mondor <mmondor@pulsar-zone.net>
Wed, 22 Nov 2006 23:05:47 +0000 (23:05 +0000)
committerMatthew Mondor <mmondor@pulsar-zone.net>
Wed, 22 Nov 2006 23:05:47 +0000 (23:05 +0000)
mmsoftware/js/js-sh/app/httpd/httpd.js

index 13f4cd5..8846c25 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: httpd.js,v 1.42 2006/11/16 06:59:18 mmondor Exp $ */
+/* $Id: httpd.js,v 1.43 2006/11/22 23:05:47 mmondor Exp $ */
 
 /*
  * Copyright (c) 2005-2006, Matthew Mondor
@@ -75,8 +75,8 @@
 /*
  * Server identification
  */
-const SERVER_VERSION = 'mmondor_js_httpd/0.1.0 (NetBSD)';
-const SERVER_CVSID = '$Id: httpd.js,v 1.42 2006/11/16 06:59:18 mmondor Exp $';
+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 $';
 
 
 
@@ -443,6 +443,9 @@ HTTPReply.prototype = {
 
                this.headers.push('Content-Length: ' +
                    (size == null ? contents.length : size));
+               /* 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";
@@ -479,7 +482,32 @@ function http_error(fd, code, desc, ldesc)
        res.flush(fd, null);
 }
 
+/*
+ * Redirects the HTTP client to another path.
+ */
+function http_redirect(fd, vpath)
+{
+       var res, path;
+
+       if (!(path = fd.http_vhost.htdocs_root.valid_virtual(vpath))) {
+               http_error(this, 403, 'Permission Denied',
+                   'You do not have the permission to access this ' +
+                   'resource.');
+               return;
+       }
+
+       res = new HTTPReply(301, 'Moved Permanently',
+           'text/html; charset=' + options.default_charset);
+       res.addNoCacheHeaders();
 
+       res.addHeader('Location: http://' + fd.http_host + path.virtual);
+       res.addContent('<html><head><title>301 Moved Permanently</title>' +
+           '</head><body><h1>301 Moved Permanently</h1><p>The document was ' +
+           'permanently moved<a href="http://' + fd.http_host + path.virtual +
+           '">here</a>.</p></body></html>');
+
+       res.flush(fd, null);
+}
 
 /*
  * Add new methods to the FD prototype object (JavaScript is great like that).
@@ -740,7 +768,7 @@ FD.prototype.parseQuery = function(time)
        /* Initial HTTP query state */
        this.http_protocol = '';
        this.http_method = '';
-       this.http_vhost = 'n/a';
+       this.http_vhost = this.http_host = 'n/a';
        this.http_path = '';
        this.http_vars_get = {};
        this.http_vars_post = {};
@@ -757,6 +785,9 @@ FD.prototype.parseQuery = function(time)
 
        /* Split request lines */
        lines = this.query_data;
+       /* 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) {
@@ -790,6 +821,7 @@ FD.prototype.parseQuery = function(time)
                        if (w == 'host:' && words.length == 2) {
                                var i2;
 
+                               this.http_host = words[1];
                                if ((i2 = words[1].indexOf(':')) != -1)
                                        words[1] = words[1].substr(0, i2);
                                vhost = words[1];
@@ -962,7 +994,7 @@ FD.prototype.parsePost = function(time)
  */
 FD.prototype.httpRespond = function(time)
 {
-       var path, fd, st, res, ext, mimetype, i, size, appserv_match;
+       var path, fd, st, res, ext, mimetype, i, size, appserv_match, doc;
        /*
        var sess;
        */
@@ -1024,9 +1056,37 @@ FD.prototype.httpRespond = function(time)
                        return PSTAT_CLOSE_SUCCESS;
                }
 
+               /* XXX In case of query to a directory done without the
+                * trailing '/', apache redirects as follows:
+                *
+                * "HTTP/1.1 301 Moved Permanently\r
+                * Date: Wed, 22 Nov 2006 04:01:03 GMT\r
+                * Server: Apache/1.3.34 (Unix) mod_ssl/2.8.25 OpenSSL/0.9.7d\r
+                * Location: http://mmondor.pulsar-zone.net/img_gallery/\r
+                * Connection: close\r
+                * Content-Type: text/html; charset=iso-8859-1\r
+                * \r
+                * <!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">
+                * <HTML><HEAD>
+                * <TITLE>301 Moved Permanently</TITLE>
+                * </HEAD><BODY>
+                * <H1>Moved Permanently</H1>
+                * The document has moved <A
+                * HREF=\"http://mmondor.pulsar-zone.net/img_ga\
+                * llery/\">here</A>.<P>
+                * </BODY></HTML>
+                *
+                * This however does not allow the client to know that the
+                * current URL internally set to an index such as index.html.
+                * Perhaps e should redirect to the full URL?
+                * Except for directories without an index for which
+                * auto-indexing is to be performed, of course.
+                */
+
                /* Directory? */
                if ((st.st_mode & FD.S_IFDIR) != 0) {
                        var error = false;
+                       var redirect = true;
 
                        /*
                         * Refit URL to configured index,
@@ -1054,11 +1114,18 @@ FD.prototype.httpRespond = function(time)
                                if (!(appserv_match = this.http_vhost.
                                    host_appmatch(path.virtual))) {
 
-                                       /* Attempt to reopen file */
+                                       /*
+                                        * Attempt to reopen static file.
+                                        * On success, close it and redirect
+                                        * client to it.
+                                        */
                                        try {
                                                fd.open(path.real,
                                                    FD.O_RDONLY);
-                                               st = fd.fstat();
+                                               fd.close();
+                                               http_redirect(this,
+                                                   path.virtual);
+                                               return PSTAT_CLOSE_SUCCESS;
                                        } catch (x) {
                                                error = true;
                                        }