In this HTML version of this source code I use the following colour conventions :

I provide also direct links to uncommented (at least, HTML-uncommented, ample Fortran comments inside) source code of all routines.



C
C       this is the test Fortran CGI program referred by the demos in
C       http://sax.lambrate.inaf.it/~lucio/WWW/cgi-lib/
C

C C these are just local variables C
CHARACTER prova*8,var*16,val*16,name*8 INTEGER ierr
C these constants holding HTML useful strings are not here, they are instead C in the include file cgi.inc, where further strings can be added ad libitum C * CHARACTER RED*20,BLU*20,GRN*20,BLK*7 * PARAMETER (RED='<font color=#ff0000>') * PARAMETER (BLU='<font color=#0000ff>') * PARAMETER (GRN='<font color=#00ff00>') * PARAMETER (BLK='</font>')
C this include file is referred here just to set the CGIDEBUG flag which C allows some debug WRITE to appear. The include file also refers to an C important COMMON block used internally to store the query string etc. C but need not to be used in the main program unless you want to play around C with the CGIDEBUG flag C INCLUDE 'cgi.inc' CGIDEBUG=.TRUE.
C these two utilities are better called first and in this order. The first is C actually mandatory to route the standard output where and how it has to be. C You select the kind of output : text/html, text/plain or whatever. C The second call just outputs <title>CGI program return<title> C CALL CGIHEADER('text/html') CALL CGITOKEN('title','CGI program return')
C this very important call retrieves the query string in POST or GET C method as appropriate. It is not necessary to call it in the main. It will C be called automatically by the first GET_NAMED_VARIABLE or GET_NEXT_VARIABLE C call to load the string in common. It is called here to place DEBUG information C first on the screen. C CALL CGIRETRIEVE
C what follows is just an example of decoding the query string for known C variables called by name. This works with the provaget and provapost forms, C but is likely to give "variable not found" if you try it with any other form C (including yours). C C It also demonstrates HTML output. This can be generated directly with WRITE C statements, or by utility routines. I have made only one, CGITOKEN, which C outputs a string enclosed in <any>...</any> clauses. C write(*,*)'<hr>' CALL CGITOKEN('h1','parsing 3 vars by name in 8-char buffer') C C Named variables can be called in any order, provided the name matches a C variable in the form. Here string PROVA will contain the returned value, and C since this is just 8 character long, may demonstrate a truncation error. C C Here I demonstrate the use of a variable name with imbedded URL-encoded C characters (look at the debug output). The variable is called 'tar$get'. C An error code 2 indicates truncation. C The same variable is retrieved untruncated in the example loop below. C NAME='tar$get' CALL GET_NAMED_VARIABLE(NAME,PROVA,IERR) CALL CGITOKEN('h3', . blu//name//blk//'is '''//red//prova//blk//'''') write(*,*)' error code' ,ierr C C Here I demonstrate the successful use of a plain variable name 'decs' C NAME='decs' CALL GET_NAMED_VARIABLE(NAME,PROVA,IERR) CALL CGITOKEN('h3', . blu//name//blk//'is '''//red//prova//blk//'''') write(*,*)' error code' ,ierr C C Here I try to retrieve a non-existing variable 'pippo', error code -1 C NAME='pippo' CALL GET_NAMED_VARIABLE(NAME,PROVA,IERR) CALL CGITOKEN('h3', . blu//name//blk//'is '''//red//prova//blk//'''') write(*,*)' error code' ,ierr
C what follows is a more regular example of decoding the query string for all C variables in the order they appear. For each it will return name and value. C C Some usual demo of HTML output. C write(*,*)'<hr>' CALL CGITOKEN('h1','parsing all variables in order') C C a loop retrieving each variable in turn until end of them C 111 CONTINUE CALL GET_NEXT_VARIABLE(val,var,IERR) IF(IERR.NE.-1)THEN CALL CGITOKEN('h3','variable '//blu//val//blk) write(*,*)'has value ',red,var,blk write(*,*)' error code' ,ierr GOTO 111 ENDIF
C ok that's all C CALL CGITOKEN('h1',grn//'end of parsing'//blk) STOP END
C You can have a look at individual routines C * include 'cgiheader.f' * include 'cgitoken.f' C C the next two parse the query string from var=val&var=val&...&var=val C * include 'get_named_variable.f' * include 'get_next_variable.f' * include 'cgiretrieve.f' C C the next two are service routines to handle URL-encoding. Strings are C encoded by http browsers with blanks replaced by '+' and most "funny" C characters replaced by hexadecimal %hh sequences. Decoding reconstructs C the original string, e.g. a(b) from a%28b%29 . Encoding does the reverse C and is necessary only to match named variables, which YOU provide in C unencoded form as NAME arguments to GET_NAMED_VARIABLE. C * include 'urldecode.f' * include 'urlencode.f'

sax.iasf-milano.inaf.it/~lucio/WWW/cgi-lib/prova.html :: original creation 2007 lug 11 10:51:16 CEST :: last edit 2007 Jul 11 10:51:16 CEST