CGIs outside the cgi-bin directory

Discussion about the creation of websites in general, HTML, PHP, Java.

CGIs outside the cgi-bin directory

Postby hafner on Wed May 24, 2006 11:41 pm

Hi,

do you also like to place your CGI scripts in any directories, not
only in the cgi-bin directory? Once I tried it and added AddHandler
cgi-script .cgi
to my .htaccess, but it didn't work. Maybe I was
wrong, or infinity's configuration does not allow it for some reason -
doesn't matter, I've found another way: a rewrite rule causes URLs
with trailing .cgi to be directed to a single script in cgi-bin/ ,
which in turn automatically executes the CGI scripts in different
directories. See below the details.

For the rewrite rule in public_html/.htaccess:
Code: Select all
RewriteEngine On
RewriteBase /
RewriteRule \.cgi$    cgi-bin/cgi [L]
Here's the script cgi-bin/cgi:
Code: Select all
#!/usr/bin/env gosh
;;-*- scheme -*-

;; Copyright (c) 2006 Thomas Hafner <[email protected]>

;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2 of the
;; License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.

;; History:
;;
;; v0.1 2006-05-24 Thomas Hafner: First version.

(define (apply-r al f)
  (apply f al))

(define (die error)
  (format #t "Content-Type: text/plain\n\nERROR: ~a\n" error)
  (exit))

(define (getenv-or-die varname)
  (or (sys-getenv varname)
      (die (format #f "Environment variable ~s not set." varname))))

(define (split-path path)
   (rxmatch #/^(.*?\/)(.*)$/ path))

(define (remove-common-dirs x y k)
  (let ((mx (split-path x))
        (my (split-path y)))
    (if (and mx my (string=? (mx 1) (my 1)))
        (remove-common-dirs (mx 2) (my 2) k)
        (k x y))))

(define (mk-replace-trailing-string from to)
  (let ((fromsl (string-length from)))   
    (lambda (s)
      (let* ((ssl (string-length s))
             (s1sl (- ssl fromsl)))
        (and (>= s1sl 0)
             (let ((s1 (substring s 0 s1sl))
                   (s2 (substring s s1sl ssl)))
               (and (string=? from s2)
                    (string-append s1 to))))))))

(apply-r
(map getenv-or-die '("SCRIPT_NAME" "REDIRECT_URL" "SCRIPT_FILENAME"))
(lambda (script_name redirect_url script_filename)
   (remove-common-dirs
    script_name redirect_url
    (lambda (from to)
      (define proc (mk-replace-trailing-string from to))
      (let ((nsf (proc script_filename))
            (nsn (proc script_name)))
        (sys-putenv "SCRIPT_FILENAME" nsf)
        (sys-putenv "SCRIPT_NAME" nsn)
        (and (sys-access nsf (logior R_OK X_OK))
             (sys-chdir (sys-dirname nsf))
             (sys-exec nsf (list nsf)))
        (die (format #f "~s could not be executed." nsf)))))))
Thanks again to Tim for having followed my suggestion to install
Gauche Scheme. Of course this simple task could be done in whatever
programming language (and probably somebody did it already), but it
was real fun to use Gauche.

I didn't try, but I guess the script may fail if the path and/or URL
contain non ASCII characters.

Regards
Thomas
hafner
Contributer
Contributer
 
Posts: 73
Joined: Tue Mar 07, 2006 2:18 pm
Location: München (Germany)

Postby Tim on Thu May 25, 2006 9:05 am

CGI is handled in the Directory blocks of the httpd.conf, its something like Options +ExecCGI, adding a handler isn't enough Wink
User avatar
Tim
root
root
 
Posts: 736
Joined: Thu Mar 10, 2005 6:31 am
Location: Australia

Postby hafner on Thu May 25, 2006 3:25 pm

Tim is right, so forget about my script (at least it makes me think
that almost all can also be done by mod_rewrite).

Actually the handler for .cgi is already globally added, so the
minimal thing to do is adding the line
Code: Select all
Options +ExecCGI
into .htaccess .

Regards
Thomas
hafner
Contributer
Contributer
 
Posts: 73
Joined: Tue Mar 07, 2006 2:18 pm
Location: München (Germany)


Return to General Webmastering

Who is online

Users browsing this forum: No registered users and 1 guest

cron