/* AnyURL.c CGI form action to combine inputs into a Location: URL origin massively modified from NCSA Mosaic post-query.c and query.c requires util.c (unmodified from NCSA Mosaic util.c) requires slurp.c requires nextok.c author Mark Renton (MACROSTAFF) renton@pmel.noaa.gov TOGA-TAO */ #define version "(1.5)" /* version 1.5 Sep 96 slurp.c checks for no REQUEST_METHOD version 1.4 Nov 95 version reporting, -DNOCONTENT, minor formatting version 1.3 Oct 95 use slurp.c allow FORM METHOD=GET in addition to METHOD=POST (workaround for AOL and Spry browsers bug) don't modify QUERY_STRING environment variable renamed variable 'm' to 'NE' (Number of Entries) renamed variable 'x' to 'E' (Entry) include documentation URL in error output version 1.2 Aug 95 hidden "debug" OK, add link on debug page version 1.1 May 95 multi-character input names version 1.0 Nov 94 first implementation "any URL" = let .html form feed us parts which we compose into a Location: URL Accepts CGI form input with variable names; "debug", "control" are special. Examples: 1. control = @a://@b/~c/ a = http b = www.pmel.noaa.gov c = renton yields Location: http://www.pmel.noaa.gov/~renton/ 2. control = @{mode}://@{node}/@{file} mode = ftp node,file = ftp.pmel.noaa.gov,OCRD/webutil/AnyURL.tar.Z yields Location: ftp://ftp.pmel.noaa.gov/OCRD/webutil/AnyURL.tar.Z If debug=on, will build a text/html showing you what you fed it plus a link. Default control is just to concatenate all a..z (@a@b@c...@z) There are so many special characters in use that we have difficulty choosing one for AnyURL. */ #include #include #include #include #include "nextok.c" #define MAX_ENTRIES 10000 #define MAX_LENGTH 1000 /* MWR */ typedef struct { char *name; char *val; } entry; char *makeword(char *line, char stop); char *fmakeword(FILE *f, char stop, int *len); char x2c(char *what); void unescape_url(char *url); void plustospace(char *str); entry entries[MAX_ENTRIES]; int E,NE=0; int cl; /* MWR - added lookup, value, hackvalues, decomma, use_control */ /* lookup: scan 'entries' for given name, result matching index or -1 */ int lookup(char *word) { register int i; /* printf("lookup nc=%d |%s|\n",strlen(word),word); {int j; for(j=0;j(sizeof(keyname)-1)) nc = sizeof(keyname)-1; strncpy(keyname,control,nc); keyname[nc]='\0'; /* printf("keyname=|%s|\n",keyname); */ control += nc; if(*control=='}') control++; temp=value(keyname); while(*temp) { *complete++ = *temp++; }; } else { /* @x */ keyname[0] = *control++; keyname[1] = '\0'; temp=value(keyname); while(*temp) { *complete++ = *temp++; }; } } } else *complete++ = *control++; }; *complete = '\0'; } int header_output=0; #define text_header if(header_output++==0) printf("Content-type: text/plain\n\n") #define html_header if(header_output++==0) printf(html_header_text) #define html_header_text "Content-type: text/html\n\n" #define lf printf("
\n") #define error_header html_header; lf; printf("AnyURL " version " encountered an error"); lf; lf #define forms_URL "http://www.ncsa.uiuc.edu/SDG/Software/Mosaic/Docs/fill-out-forms/overview.html" #define AnyURL_URL "http://www.pmel.noaa.gov/toga-tao/AnyURL.html" #define more_info \ printf(\ "

For more information, see NCSA's \n"\ "forms overview or the \n"\ "AnyURL documentation.\n"\ ) #include "slurp.c" main(int argc, char *argv[]) { char *control; char *complete = (char *) malloc(sizeof(char) * (MAX_LENGTH+ 1)); #define MAX_DEBUG_LINES (MAX_ENTRIES+10) #define MAX_DEBUG_LENGTH (MAX_LENGTH+100) char *debug_lines[MAX_DEBUG_LINES]; int last_debug_line=-1; #define DEBUG_PRINTF debug_lines[++last_debug_line] = \ (char *) malloc(sizeof(char) * MAX_DEBUG_LENGTH); \ sprintf (debug_lines[last_debug_line], /* slurp in that input, whether GET or POST */ slurp(); /* in case the value for "debug" is derived via hackvalues and decomma, we store the pre-{hackvalues-and-decomma} debug information in memory */ { DEBUG_PRINTF html_header_text); DEBUG_PRINTF "

AnyURL " version " was given these values:

"); DEBUG_PRINTF "
    "); for(E=0; E <= NE; E++) { DEBUG_PRINTF "
  • %s = %s", entries[E].name, entries[E].val); } /* if(!strcmp(value("control"),"")) DEBUG_PRINTF "
  • %s = %s", "control", value("control")); */ DEBUG_PRINTF "

"); }; hackvalues(); decomma(); if ( lookup("control") == -1 ) control="@a@b@c@d@e@f@g@h@i@j@k@l@m@n@o@p@q@r@s@t@u@v@w@x@y@z"; else control = value("control"); use_control(control,complete); if (!strcasecmp(value("debug"),"on")) { int j; for (j=0;j<=last_debug_line;j++) puts(debug_lines[j]); printf("

AnyURL resulted in these values:

"); printf("
    \n"); for(E=0; E <= NE; E++) if(*entries[E].name!='@') printf("
  • %s = %s\n", entries[E].name, entries[E].val); if(!strcmp(value("control"),"")) printf("
  • %s = %s\n","control",control); printf("
\n"); printf("Location: %s\n\n",complete); printf("

Attempted link -- %s\n\n",complete,complete); } else if (!strcasecmp(value("debug"),"show")) { html_header; printf("

Attempted link -- %s\n\n",complete,complete); } else { #ifndef NOCONTENT printf("Content-type: text/html\n"); /* should this be here??? */ #endif printf("Location: %s\n\n",complete); } }