Here's some code off the top of my head, which uses Metacard as it's own 
server (as opposed to a cgi which relies on a web server to send it requests).

<< 1.  An example of a web page (html) that had two fields (first name, last
name) and a post button, to send the info to a metacard http server stack. >>

Note: I'm using "[" and "]" for tags for the sake of those whose e-mail 
clients will go and render the HTML rather than displaying the source.

[HTML][BODY]
[FORM METHOD=GET ACTION=http://www.mysite.com:8080/myserver$msg$$=SayHello]
First name: [INPUT TYPE=TEXT NAME=FIRSTNAME][BR]
Last name: [INPUT TYPE=TEXT NAME=LASTNAME][BR]
[INPUT TYPE=SUBMIT]
[/FORM]
[/BODY][/HTML]

<<2.  A stack for the stacks-bin folder that would take their first and last
name and generate a web page saying "Nice to meet you, <first name> <last
name>!">>

Try these scripts, which rely on Flamethrower-style requests.
They are modified from my own working source, though untested after mods.
See the "SayHello" handler which works with the HTML page above.

on openStack
    ## accept incoming requests
    accept connections on port 8080 with message "newconnection"
end openStack

on newconnection s
  ## allow 1 minute for new connections to come through cleanly, otherwise 
scrap 'em
  set the socketTimeoutInterval to 60000
  read from socket s for 1 line with message "serverfirstline"
end newconnection 

on serverfirstline s,theData
  ## remember this and get the rest of the request
  global theRequests
  put theData into theRequests[s]
  read from socket s until empty with message "serverread"
end serverfirstline

on serverread s,theData
  global theRequests
  
  put theRequests[theSocket] into firstLine
  put firstLine&crLF&theData into theData

  put word 1 of theData into requestType
  if (requestType = "GET") then
    put word 2 of theData into theURL
    if (theURL contains "$$=") then
    ## find the message included in the request (see HTML source above)
      get matchText(theURL,"\$\$=([a-zA-Z]+)",theMessage)
      send theMessage&&s to this cd
    else
      ## Just like Flamethrower, I send a "cgiEvent" if no message is 
specified
      send "cgiEvent"&&s to this cd
    end if
  else 
    ## I don't do POST
  end if
end serverread

on cgiReply theData,theSocket,contentType
  ## create the HTTP headers, and send everything
  global theRequests
  put "" into theHeader

  put (theRequests[theSocket] contains "Connection: Keep-Alive") into 
keepThisSocket
  ##put FALSE into keepThisSocket
  
  put "HTTP/1.1 200 MyServer/Metacard"&crLF  after  theHeader
  if (contentType is empty) then
    put "Content-Type: text/html"&crLF  after theHeader
  else
    put "Content-Type:"&&contentType&crLF  after theHeader
  end if
  put "Content-Length:"&&length(theData)&crLF  after theHeader
  if (keepThisSocket) then
    put "Connection: Keep-Alive"&crLF after theHeader
  else
    put "Connection: Close"&crLF after theHeader
  end if

  delete theRequests[theSocket]
  
  if (keepThisSocket) then
    ##allow another request on this same socket when it is done writing
    write theHeader&crLF&theData to socket theSocket with message 
"newconnection"
  else
    -- kill off this socket
    write theHeader&crLF&theData to socket theSocket
    close socket theSocket
  end if
end cgiReply

function cgiSearchArgs theSocket
  ## extract the arguments for a GET request
  global theRequests
  set the itemDelimiter to "?"
  get word 2 of theRequests[theSocket]
  put item 2 of it into formData
  set the itemDelimiter to comma
  return formData
end cgiSearchArgs

on SayHello s
    ##parse out the first and last name requested, and merge them into a 
page, then reply with that page
    local firstName,lastName
    ## lets pretend we have a template file name "HelloPage.html"
    put cgiSearchArgs(s) into theArgs
    set the itemDelimiter to "&"
    repeat for each item thisArg in theArgs
        set the itemDelimiter to "="
        put item 1 of thisArg into argName
        put item 2 of thisArg into argValue
        do "put argValue into"&&argName
        set the itemDelimiter to "&"
    end repeat
    set the itemDelimiter to comma
    open file "HelloPage.html"
    read from file "HelloPage.html" until eof
    put it into thePage
    close file "HelloPage.html"
    replace "[[firstName]]" with firstName in thePage
    replace "[[lastName]]" with lastName in thePage
    cgiReply thePage,s
end SayHello

<<3.  Any modifications needed to be made to the metacard http server stack to
make this work.>>

Assuming my changes made in e-mail didn't break anything, the scripts above 
should work completely if placed in your stack script. You may also want to 
include socketError and socketTimeout handlers.
Have fun, questions/suggestions welcome!

Brian


Archives: http://www.mail-archive.com/metacard%40lists.best.com/
Info: http://www.xworlds.com/metacard/mailinglist.htm
Please send bug reports to <[EMAIL PROTECTED]>, not this list.

Reply via email to