bash-script as cgi-script

bash-script as cgi-script

Post by Tedd » Sat, 06 Jan 2001 19:39:49



I tried to use a bash-script as cgi-script. how can I read the variables
I entered in the URL ex.
www.myhost.com/cgi-bin/mybashscript?myvar=myvalue

--
Regs
Teddy

Sent via Deja.com
http://www.deja.com/

 
 
 

bash-script as cgi-script

Post by Anthony Borl » Sat, 06 Jan 2001 21:56:08



Quote:> I tried to use a bash-script as cgi-script. how can I read the variables
> I entered in the URL ex.
> www.myhost.com/cgi-bin/mybashscript?myvar=myvalue

I use a little .C utility program called 'proccgi.c'; code is posted below.
It works for both "GET" and "POST" methods, and is used as follows in the
shell script:

...
# Extract Form Fields. Fields available as 'FORM_fieldname'
eval `./proccgi $*`
...

So, for example, 'myvar' would appear as 'FORM_myvar' in your CGI script.

I hope this helps.

/*
 * Proccgi
 *
 * Reads form variables and dumps them on standard output.
 * Distributed by the GNU General Public License. Use and be happy.
 *
 * Frank Pilhofer

 *
 * Last changed 11/06/1997
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <memory.h>

/*
 * Duplicate string
 */

char *
FP_strdup (char *string)
{
  char *result;

  if (string == NULL)
    return NULL;

  if ((result = (char *) malloc (strlen (string) + 1)) == NULL) {
    fprintf (stderr, "proccgi -- out of memory dupping %d bytes\n",
      (int) strlen (string));
    return NULL;
  }

  strcpy (result, string);
  return result;

Quote:}

/*
 * Read CGI input
 */

char *
LoadInput (void)
{
  char *result, *method, *p;
  int length, ts;

  if ((method = getenv ("REQUEST_METHOD")) == NULL) {
    return NULL;
  }

  if (strcmp (method, "GET") == 0) {
    if ((p = getenv ("QUERY_STRING")) == NULL)
      return NULL;
    else
      result = FP_strdup (p);
  }
  else if (strcmp (method, "POST") == 0) {
    if ((length = atoi (getenv ("CONTENT_LENGTH"))) == 0)
      return NULL;

    if ((result = malloc (length + 1)) == NULL) {
      fprintf (stderr, "proccgi -- out of memory allocating %d bytes\n",
        length);
      return NULL;
    }

    if ((ts = fread (result, sizeof (char), length, stdin)) < length) {
      fprintf (stderr, "proccgi -- error reading post data, %d bytes read,
%d expedted\n",
        ts, length);
    }
    result[length] = '\0';
  }
  else {
    return NULL;
  }

  return result;

Quote:}

/*
 * Parse + and %XX in CGI data
 */

char *
ParseString (char *instring)
{
  char *ptr1=instring, *ptr2=instring;

  if (instring == NULL)
    return instring;

  while (isspace (*ptr1))
    ptr1++;

  while (*ptr1) {
    if (*ptr1 == '+') {
      ptr1++; *ptr2++=' ';
    }
    else if (*ptr1 == '%' && isxdigit (*(ptr1+1)) && isxdigit (*(ptr1+2))) {
      ptr1++;
      *ptr2    =
((*ptr1>='0'&&*ptr1<='9')?(*ptr1-'0'):((char)toupper(*ptr1)-'A'+10)) << 4;
      ptr1++;
      *ptr2++ |=
((*ptr1>='0'&&*ptr1<='9')?(*ptr1-'0'):((char)toupper(*ptr1)-'A'+10));
      ptr1++;
    }
    else
      *ptr2++ = *ptr1++;
  }
  while (ptr2>instring && isspace(*(ptr2-1)))
    ptr2--;

  *ptr2 = '\0';

  return instring;

Quote:}

/*
 * break into attribute/value pair. Mustn't use strtok, which is
 * already used one level below. We assume that the attribute doesn't
 * have any special characters.
 */

void
HandleString (char *input)
{
  char *data, *ptr, *p2;

  if (input == NULL) {
    return;
  }

  data = FP_strdup   (input);
  ptr  = ParseString (data);

  /*
   * Security:
   *
   * only accept all-alphanumeric attributes, and don't accept empty
   * values
   */

  if (!isalpha(*ptr) && *ptr != '_') {free (data); return;}
  ptr++;
  while (isalnum(*ptr) || *ptr == '_') ptr++;
  if (*ptr != '=') {free (data); return;}

  *ptr = '\0';
  p2 = ptr+1;

  fprintf (stdout, "FORM_%s=\"", data);

  /*
   * escape value
   */

  while (*p2) {
    switch (*p2) {
    case '"': case '\\': case '`': case '$':
      putc ('\\', stdout);
    default:
      putc (*p2,  stdout);
      break;
    }
    p2++;
  }
  putc ('"',  stdout);
  putc ('\n', stdout);
  *ptr = '=';
  free (data);

Quote:}

int
main (int argc, char *argv[])
{
  char *ptr, *data = LoadInput();
  int i;

  /*
   * Handle CGI data
   */

  ptr = strtok (data, "&");
  while (ptr) {
    HandleString (ptr);
    ptr = strtok (NULL, "&");
  }
  free (data);

  /*
   * Add Path info
   */

  if (getenv ("PATH_INFO") != NULL) {
    data = FP_strdup (getenv ("PATH_INFO"));
    ptr = strtok (data, "/");
    while (ptr) {
      HandleString (ptr);
      ptr = strtok (NULL, "/");
    }
    free (data);
  }

  /*
   * Add args
   */

  for (i=1; i<argc; i++) {
    HandleString (argv[i]);
  }

  /*
   * done
   */

  return 0;

Quote:}


 
 
 

bash-script as cgi-script

Post by Kurt J. Lanz » Sat, 06 Jan 2001 23:22:03



> I tried to use a bash-script as cgi-script. how can I read the variables
> I entered in the URL ex.
> www.myhost.com/cgi-bin/mybashscript?myvar=myvalue

It depends on how your server does CGI's. Apache passes
everything after the question mark in the shell variable
QUERY_STRING.
 
 
 

bash-script as cgi-script

Post by Peter Sundstro » Tue, 09 Jan 2001 04:40:42



Quote:> I tried to use a bash-script as cgi-script. how can I read the variables
> I entered in the URL ex.
> www.myhost.com/cgi-bin/mybashscript?myvar=myvalue

Have a look at http://www.midwinter.com/~koreth/uncgi.html

There are other similar ones available.  I would recommend that you *don't*
try any shell solutions as they are slow and don't work for all cases.

 
 
 

bash-script as cgi-script

Post by kevin_coll.. » Wed, 10 Jan 2001 08:04:38


Kurt,

    QUERY_STRING is used depending on whether the CGI is specified as a
GET or a PUT - it is not because the server is Apache or something
else. This is an HTML/CGI standard.

Kevin




> > I tried to use a bash-script as cgi-script. how can I read the
variables
> > I entered in the URL ex.
> > www.myhost.com/cgi-bin/mybashscript?myvar=myvalue

> It depends on how your server does CGI's. Apache passes
> everything after the question mark in the shell variable
> QUERY_STRING.

Sent via Deja.com
http://www.deja.com/
 
 
 

bash-script as cgi-script

Post by Heiner Steve » Thu, 11 Jan 2001 05:57:55


 > I tried to use a bash-script as cgi-script. how can I read the variables
 > I entered in the URL ex.
 > www.myhost.com/cgi-bin/mybashscript?myvar=myvalue

Try

    http://www.oase-shareware.org/shell/scripts/cmds/urlgetopt

Heiner
--
 ___ _

\__ \  _/ -_) V / -_) ' \    SHELLdorado for Shell Script Programmers:
|___/\__\___|\_/\___|_||_|   http://www.oase-shareware.org/shell/

 
 
 

1. bash cgi-script prints exit codes

I got a problem with a bash cgi script, which does print the exit
codes into the on-the-fly generated HTML page.

if expr "$capital" : 'on';    then capital_token=""
        else capital_token="-i"
        fi

if expr "$operator" : 'or';     then
...

The output on the HTML Page is as much zeros as I use
if..fi constructions.

What can I do to surpress them or to disable the
error codes?

2. Maximum number of channels with ISDN

3. Bash/CGI - - HTML call to Bash script

4. pwd and symbolic links in shells other than ksh

5. cgi scripts showing up as script rather as binary

6. Can't reach network via eth0

7. Initialization script for bash script

8. How to setup a secondary mail server in case the first one goes down?

9. convert trivial Dos bat script into bash script

10. bash script, wget queue script......

11. Script or executable to format bash scripts

12. Copy files using filenames from text files with shell script or bash script

13. Perl Scripts as CGI scripts