Sending data from client stack to CGI-Data stack?

Are you using LiveCode to create server scripts or CGIs?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
jpatten
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 45
Joined: Tue May 06, 2008 11:24 pm

Sending data from client stack to CGI-Data stack?

Post by jpatten » Mon Jun 16, 2008 12:45 am

Hi All...

I have two stacks.

One is set up on the server and contains 1 field and the stack script:

on librarystack
put the $QUERY_STRING into theTerms
put theTerms into cd fld "data" of cd 1 of current stack
end libraryStack


The second stack is the client stack and it contains just one field and a button. The button script is:

on mouseUp
put cd fld "theInput" int theDataToSend
post theDataToSend to URL "http:10.51 0.12/cgi-bin/postdata.cgi"
--put it
end MouseUp

I also have a simple "postdata.cgi" file that points to the server stack:

#!rev
on startup
library "postdata.rev"
end startup


I'm attempting to wrap my head around the idea of using stack cgi to save and retrieve data from other stacks. However, I am obviously missing a few steps.

All the example cgi that rely on text cgi, and stack cgi work fine for me. So I know that the rev cgi is working.

I have not been able to find an example of a stack that uses the post/get command to send data to the rev cgi script in a stack library and then saves the data to the rev stack.

I'm attempting to create the most basic example to get this to work first be fore moving on.

When I get "it" the result is:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>500 Internal Server Error</title>
</head><body>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error or
misconfiguration and was unable to complete
your request.</p>
<p>Please contact the server administrator,
you@example.com and inform them of the time the error occurred,
and anything you might have done that may have
caused the error.</p>
<p>More information about this error may be available
in the server error log.</p>
</body></html>

Thanks in advance for any suggestions you may have!

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Mon Jun 16, 2008 1:43 pm

Hi jpatten,

Please read the entry for Start Using in the docs.

$QUERY_STRING is a variable, not a property. It should not be preceded by "the".

Make sure that line endings are correct, depending on the operating system on the server.

Is the name of the Revolution executable really "rev" and is it in the cgi-bin directory, together with postdata.cgi and postdata.rev?

Make sure that both the executable and the cgi script have sufficient permissions.

Why don't you put all scripts in the same cgi file? Make it work first, and increasy complexity later, but adding libraries.

This is what I can think of, for now. I hope this helps.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

jpatten
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 45
Joined: Tue May 06, 2008 11:24 pm

Post by jpatten » Tue Jun 17, 2008 5:42 pm

Good advice Mark! Thanks!

One little issue, I can't seem to get the web form data into the card field. It will return it fine to the browser, but it won't store it in the card fld?

Here's my script:

#!rev
on startup
put "Data.rev" into theStack
put $QUERY_STRING into theTerm
if theTerm = "" then
put "You don't have any data stupid!" into buffer
else if there is no stack theStack then
put "No stack of that name." after buffer
put cr & theStack after buffer
else
put theTerm into theTermsArray
split theTermsArray by "&" and "="
put urlDecode(theTermsArray[terms]) into theIdentifiedTerm
put theIdentifiedTerm after buffer
end if
put "content-Type: text/plain" & cr & cr
start using stack theStack
put "number of cards:" & the number of cards of stack theStack & cr before buffer
put buffer into cd fld "theData" of cd 1 of stack theStack
save stack theStack
put buffer
end startup

Any ideas on what I'm missing?

Thank you!

jpatten
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 45
Joined: Tue May 06, 2008 11:24 pm

Post by jpatten » Tue Jun 17, 2008 6:02 pm

okay...i think i figured it out finally. I have been running this from my laptop locally. I have the "data.rev" stack open, my browser open, and text wrangler, and I have been making changes, saving, and testing, etc. etc. I was expecting to see the text from the web form pop up in the data.rev stack after I submitted some text. It never did. So I was under the impression it was not working. Apparently, the data from the web form is being processed correctly by the rev cgi, and it is placing it into the correct field like I asked it to. However, the text does not appear in the field. It is still the old text.

By chance, I thought I'd just try getting what was in the card field and "putting" it back out to the browser. To my surprise, the text was what was submitted by the form, and not what was visible in the actual field. Do I need to refresh the field?

Here's my script:

#!rev
on startup
put "Data.rev" into theStack
put $QUERY_STRING into theTerm
if theTerm = "" then
put "You don't have any data stupid!" into buffer
else if there is no stack theStack then
put "No stack of that name." after buffer
put cr & theStack after buffer
else

put theTerm into theTermsArray
split theTermsArray by "&" and "="
put urlDecode(theTermsArray["terms"]) into theIdentifiedTerm
put theIdentifiedTerm after buffer
end if
put "content-Type: text/plain" & cr & cr
start using stack theStack
put "number of cards:" & the number of cards of stack theStack & cr before buffer
put theIdentifiedTerm into cd fld "theData" of cd 1 of stack theStack
save stack theStack
put cr & cd fld "thedata" of cd 1 of stack theStack after buffer
put cr & cd fld "thedata2" of cd 2 of stack theStack after buffer
put buffer
end startup

jpatten
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 45
Joined: Tue May 06, 2008 11:24 pm

Post by jpatten » Tue Jun 17, 2008 8:15 pm

Update...

My attempts of saving some text to a card field have not been completely successful. The post information is only persistent on my data stack at the time it is submitted. In other words, I can post text from a web form or another rev stack and it will run through the script (text cgi) on the server, apparently putting the data in the proper fields on the proper cards. The same text cgi reports back to the browser the contents of those fields appropriately at the end of the cgi text script. However, when I examine the data.rev stack, the text that was supposed to be placed in those fields is not there. Furthermore, I can't make any changes to my data.rev stack via the cgi text script. For example, I can't create a new card in the stack.

I thought maybe it was something related to permissions, so chmod all the relevant files from 755 to 777. Still no luck.

What would prevent my cgi text script from directly manipulating the data.rev stack (such as put text into a fld, or make new card)?

Thank you!

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9842
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Post by FourthWorld » Tue Jun 17, 2008 9:42 pm

Not sure what's happening there, but I've successfully created stacks from CGIs, and cards, buttons and fields within them, and stored data persistently in both fields and custom properties (though it's worth noting that the latter is much faster than the former).

FWIW, if the stack isn't a library there should be no need to use "start using".

It may be helpful to set the destroyStack of the data stack to true. Without that, perhaps the data you're seeing when you open it locally is an older cached copy?

Let me know how it works out....
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

jpatten
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 45
Joined: Tue May 06, 2008 11:24 pm

Post by jpatten » Wed Jun 18, 2008 12:33 am

Thanks Richard... still having problems though...

Here is the text from my "postdata.cgi"

#!rev
on startup
put "Data.rev" into theStack
put empty into theTerm
repeat until length(theTerm) = $Content_Length
read from stdin until empty
put it after theTerm
end repeat
if theTerm = "" then
put "You don't have any data stupid!" into buffer
else if there is no stack theStack then
put "No stack of that name." after buffer
put cr & theStack after buffer
else

put theTerm into theTermsArray
split theTermsArray by "&" and "="
put urlDecode(theTermsArray["terms"]) into theIdentifiedTerm
end if
put "content-Type: text/plain" & cr & cr

put "number of cards:" & the number of cards of stack theStack & cr before buffer
set the destroyStack of stack theStack to true
set the defaultstack to theStack
create card
put it
put "number of cards:" & the number of cards of stack theStack & cr before buffer
put buffer & cr & theIdentifiedTerm & cr & theStack & cr & cd fld "thedata" of cd 1 of stack theStack
put theIdentifiedTerm into cd fld "theData3" of cd 3 of stack theStack
--put the result
put cr & cd fld "thedata" of cd 1 of stack theStack after buffer
put cr & cd fld "thedata2" of cd 2 of stack theStack after buffer
put cr & cd fld "theData3" of cd 3 of stack theStack after buffer

save stack theStack
put buffer
close stack theStack
quit rev
end startup

What is really weird is when I get get the data back from my browser is reports:

card id 1016 of stack "/Library/WebServer/CGI-Executables/Data.rev"
number of cards:4
number of cards:3

bleep bleep 135 --this is what I typed in on the web form
Data.rev -- name of theStack
test -- text in cd fld 1

So it shows a card being created. Also, notice it does not appear to be getting the text from fields from card 2 or card 3. Even after I comment out the first field extraction from card 1, it does not work. Also, if i run this over and over, it is still reporting number of cards 4 and then number of cards 3. It's as if the response is cached. But when I open the data stack up in rev 2.9, there are only 3 cards. Also, there is no card id 1016 even though it appears to report back that there is.

I'm running the 2.9 but "saving as" in legacy mode. Also I'm doing this from my laptop and just using the web sharing in Mac OSX (Leopard).

Thanks for any assistance...I've got to give this a rest, it's driving me bonkers <LOL> :shock:

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9842
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Post by FourthWorld » Wed Jun 18, 2008 4:23 am

In my quick review of your code I didn't see anything that stands out as problematic.

I think your plan is good: set it aside for at least the evening, then get back to it fresh. You may very well see quite easily then what is so elusive now. :)
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Post by Klaus » Wed Jun 18, 2008 11:47 am

What engine are you running as your CGI?

Please keep in mind that features like "split tArray ..." was added in 2.7 or later and thus are not availalbe in 2.5x or 2.6x!

I was also trapped by this, it was "filter tList without xyz" in my case that made ME some serious headache :-)


Best

Klaus

jpatten
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 45
Joined: Tue May 06, 2008 11:24 pm

Post by jpatten » Wed Jun 18, 2008 7:40 pm

Arrrrg!!!

I should have search the list archive first!!!!

Found the solution. Rev can't write to a stack when it's in the cgi-bin folder. ( I suppose due to Apache restrictions).

Here the discussion from the mail list:

http://lists.runrev.com/pipermail/metac ... 09287.html

Cheers!

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9842
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Post by FourthWorld » Wed Jun 18, 2008 9:33 pm

> Rev can't write to a stack when it's in the cgi-bin folder.

Interesting. Maybe it's a default that can be changed, since the tests I ran for you successfully yesterday had the data stack in the same folder as the engine and the CGI script.

But hey, if it's working now just go with it. :)
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply

Return to “CGIs and the Server”