Re: Using SF as a CGI

From: Mike Ghan <mikeghan_at_logix-controls.com>
Date: Sat, 7 Dec 2002 09:24:12 -0800

-----Original Message-----
From: sftalk-bounce_at_forth.com [mailto:sftalk-bounce_at_forth.com]On
Behalf
Of Bob Nash
Sent: Friday, December 06, 2002 2:25 PM
To: SFTALK (E-mail)
Subject: [sftalk] Using SF as a CGI

> I am interested in using SF as a CGI. I would like to generate
minimal html
> pages in response to a GET method passed to a tiny HTTP server on my
home
> LAN. I was wondering if anyone had done something like this with SF
that
> they could share.
>
> Specifically, I would like to know if I am on the right path with
STDIN,
> STDOUT and environment variables.

It seems so but this is entirely dependent on the server app. Are any
parameters passed on the command line?

>
> First, I assume that I just make the API call to get a handle to
STDOUT and
> treat it like a normal file. For example: "STD_OUTPUT_HANDLE
GetStdHandle."
> From what I can tell, STDOUT is how the HTML pages that my app
generates are
> passed to the HTTP server. Am I on the right track here?

Yes - use Dosbox.F as a template - convert TYPE(C) to use WriteFile
instead of WriteConsole for redirection. If you require STDIN, you'll
need to use ReadFile (it appears the server passes parameters as
environment variables or maybe on the command line - this makes life
easier since you won't need STDIN).

>
> Second, I assume that when the HTTP server starts up my app, it
passes it
> environment variables containing the query from the remote PC. The
> environment variables passed from the HTTP server are strings such
as
> QUERY_STRING and CONTENT_LENGTH. Some time ago, Mike Ghan provided
a
> snippet to do get the environment variable string:
>
> \ Function: GetEnvironmentVariable ( lpName lpBuffer nBufSize --
#chars )
>
> : GET-ENV ( name len -- addr cnt )
> R-BUF R@ ZPLACE R> ( envName )
> 0 0 >QPAD ( buf ) 128 GetEnvironmentVariable
> QPAD SWAP ;

Here is a handy tool for parsing words:

: PARSE-WORD ( addr count char -- remainingAddr remainingCount
parsedAddr parsedCount )
   DUP >R SKIP R>
   LOCALS| char count addr |
   addr count char SCAN ( remAddr remCnt )
   addr count THIRD - 0 MAX ;

Try the following example:

S" QUERY_STRING=foobar" CHAR = PARSE-WORD
TYPE CHAR = SKIP ( past "=" ) TYPE

> Lastly, TinyWEB wants the CGI to run as a console application. I am
not
> sure what this means and how to do it. I do not plan to register a
window
> or anything like that, just output HTML on STDOUT and terminate. Is
there
> something I should know here?

A console app is just like the old familiar DOS app - no Windows
message loops, callbacks etc.

Here is an example console app:

REQUIRES DOSBOX \ <<< Nothing to do with DOS, its a Console

: TEST ( -- )
   CR CR ." This should be displayed in the console box."
   CR ." Press any key to exit" KEY DROP ;

: DO-IT ['] TEST DOS-CONSOLE P-EXECUTE ;

: MAIN ( -- )
   /DOS-CONSOLE
   ['] DO-IT CATCH DROP ( must return here! )
   0 ExitProcess ;

' MAIN 'MAIN !

/XREF \ Cleanup XRef
-1 THRESHOLD \ No Xref file

PROGRAM TESTCON

Let me know if you are interested in a SwiftForth HTML script engine,
I'll post it.

Hope this helps,

Mike

----------------------------------------------------------------------
sftalk_at_forth.com The SwiftForth programming discussion email list
To unsubscribe, send subject "unsubscribe sftalk" to listar_at_forth.com
For help with listar commands, send subject "help" to listar_at_forth.com
Archives are located at http://www.forth.com/sftalk -- check them out!
Search the archives! Visit http://www.forth.com/search for details.
Received on Sat Dec 07 2002 - 09:23:39 PST


Subscribe to our e-mail list service. It's free for all SwiftForth and SwiftX users!

This archive was generated 09-Feb-2012. Archive updated nightly.