sending an HTML page to a browser as response to a form query

I know that I’m not even in the right ballpark with this but maybe someone here will see where I’m am falling down.

What I am attempting to do is:

An end user goes to my website and fills out a form requesting the next place in the queue for his job. The form is handled by the protocol handler and looks at the database to see if the end user is registered. Is they are not then they get an HTML page served to them detailing the error and steps to correct it, if they are the applescript reads the database and decides which site has the most available bandwidth and generates a link to the WEBDAV folder the job should be placed in.

I can get everything working except sending them the dynamic webpage (I assume that ‘Return’ is NOT how it is done). Any applescripting webmasters who know what my mistake is?

--> Get the next available Mac from the queue list and send to user via HTML

on sFormQueue(lFormVars)
	try
		set lNewVars to rest of lFormVars
		-- Decide which Mac will process this job
		-- Send the WEBDAV link back to the user as HTML
		set lHTMLTitle to "Thank you"
		set lPageHeader to "It has been determined that the following queue would be the fastest to process your job -"
		set lBodyCopy to "192.168.1.136/Company Name/inbox" -- This must be a WEBDAV link determined at runtime
		set lHTMLReply to ("Content-type: text/html" & return & return & "<HTML><HEAD><TITLE>" & lHTMLTitle & "</TITLE></HEAD><BODY><H1>" & lPageHeader & "</H1>" & return & return & lBodyCopy & "</BODY></HTML>") as text
		-- Log the assignment to the Main server DB
		-- beep 3
		return lHTMLReply
		
	on error
		-- beep
		return ("Content-type: text/html" & return & return & "<HTML><HEAD><TITLE>An error has occurred</TITLE></HEAD><BODY><H1>We are disappointed to tell you that...</H1>" & return & return & "The information you are using to try to log in does not exist yet in our database.</BODY></HTML>") as text
	end try
end sFormQueue

BTW: I forgot to mention that I wrote the CGI as an applescript application because I don’t know any other languages like Perl. If I did this wouldn’t be an issue.

Hi,

Have you installed the ACGI Dispatcher on your Mac server? This software is essential when running AppleScript CGIs on Mac OS X. Moreover I am missing an «on handle CGI request»-handler in your script. Take a look at this sample script to get the idea:


-- carriage return and line feed 
property crlf : (ASCII character 13) & (ASCII character 10)
-- this builds the normal HTTP header for regular accrss 
property http_header : "HTTP/1.0 200 0K" & crlf & "Server: MacHTTP" & crlf & "MIME-Version: 1.0" & crlf & "Content-type: text/html" & crlf & crlf
-- the HTML opening 
property HTML_opening : "<HTML>" & return & "<HEAD><TITLE>CGI Results</TITLE></HEAD>" & return & "<BODY BGCOLOR=\"#FFFFFF\"><FONT FACE=\"Geneva\" SIZE=\"-1\">" & return
-- the HTML closing 
property HTML_closing : "</FONT></BODY>" & return & "</HTML>"

on handle CGI request this_request searching for search_string with posted data post_arguments using access method GET_or_POST from address this_client from user user_name using password user_password with user info user_information from server server_application via port server_IP_port executing by this_script_path of content type MIME_type referred by referring_page from browser client_browser using action CGI_path of action type action_type with full request request_data from client IP address client_address with connection ID server_connection from virtual host root_URL
	
	set the form_contents to my process_arguments(post_arguments)
	-- returns a list of form items and their values 
	-- {{"FORMITEM1", "FORMITEM1VALUE"}, {"FORMITEM2", "FORMITEM2VALUE"},etc.} 
	
	-- ACTIONS WITH PROCESSED FORM DATA GO HERE 
	-- this example returns the form items and their values 
	set the body_content to ""
	repeat with i from 1 to the count of the form_contents
		copy item i of the form_contents to {form_item, item_value}
		set the body_content to the body_content & form_item & ": " & item_value & "<BR>" & return
	end repeat
	
	-- return HTML to the user 
	return http_header & HTML_opening & body_content & HTML_closing
end handle CGI request

on process_arguments(arguments_string)
	if the arguments_string contains "&" then -- more than one passed parameter 
		set the parameters_list to my convert2list(arguments_string, "&")
	else -- single passed parameter 
		set the parameters_list to the arguments_string as list
	end if
	--{"PARAMETER=PARAMETER1VALUE", "PARAMETER2=PARAMETER2VALUE"} 
	set parameters_list to my convert_list_elements(parameters_list, "=")
	-- {{"PARAMETER1", "PARAMETER1VALUE"}, {"PARAMETER2", "PARAMETER2VALUE"}} 
	repeat with i from 1 to the count of the parameters_list
		copy item i of the parameters_list to {this_parameter, this_value}
		set this_parameter to decode_text(this_parameter)
		set this_value to decode_text(this_value)
		set this_parameter to replace_chars(this_parameter, "+", " ")
		set this_value to replace_chars(this_value, "+", " ")
		set item i of the parameters_list to {this_parameter, this_value}
	end repeat
	return parameters_list
end process_arguments

on convert2list(this_string, this_delimiter)
	set AppleScript's text item delimiters to this_delimiter
	set the item_list to every text item of this_string
	set AppleScript's text item delimiters to ""
	return item_list
end convert2list

on convert_list_elements(this_list, this_delimiter)
	set AppleScript's text item delimiters to this_delimiter
	repeat with i from 1 to the count of the this_list
		set this_item to item i of the this_list
		set this_item to every text item of this_item
		set item i of the this_list to this_item
	end repeat
	set AppleScript's text item delimiters to ""
	return this_list
end convert_list_elements

on list2string(this_list, this_delimiter)
	set AppleScript's text item delimiters to this_delimiter
	set this_string to this_list as string
	set AppleScript's text item delimiters to ""
	return this_string
end list2string

on decode_text(this_text)
	set flag_A to false
	set flag_B to false
	set temp_char to ""
	set the character_list to {}
	repeat with this_char in this_text
		set this_char to the contents of this_char
		if this_char is "%" then
			set flag_A to true
		else if flag_A is true then
			set the temp_char to this_char
			set flag_A to false
			set flag_B to true
		else if flag_B is true then
			set the end of the character_list to my decode_chars(("%" & temp_char & this_char) as string)
			set the temp_char to ""
			set flag_A to false
			set flag_B to false
		else
			set the end of the character_list to this_char
		end if
	end repeat
	return the character_list as string
end decode_text

-- this sub-routine is used to decode a hex string 
on decode_chars(these_chars)
	copy these_chars to {indentifying_char, multiplier_char, remainder_char}
	set the hex_list to "123456789ABCDEF"
	if the multiplier_char is in "ABCDEF" then
		set the multiplier_amt to the offset of the multiplier_char in the hex_list
	else
		set the multiplier_amt to the multiplier_char as integer
	end if
	if the remainder_char is in "ABCDEF" then
		set the remainder_amt to the offset of the remainder_char in the hex_list
	else
		set the remainder_amt to the remainder_char as integer
	end if
	set the ASCII_num to (multiplier_amt * 16) + remainder_amt
	return (ASCII character ASCII_num)
end decode_chars

on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

Finally I would absolutely advise you to use PHP, Python, Perl or Ruby to create CGI scripts, it’s just so much easier once you got into it. Really.

Martin,

Sorry for any confusion. The bit of code I posted was only a subroutine in the script that handles database requests, and it hasn’t really been fleshed out all that much yet since my priority (currently) is in making sure I can serve an HTML page back to a users browser. I also have scripts that handle report requests, run processes remotely, etc… The CGI application is a separate entity entirely that calls the script when it is needed.

I’m wondering if this ACGI Dispatcher is basically doing the same thing I am attempting to do via applescript.

I will download it and see what it is all about, but I am also going to continue to try to make the script I began work as well, I have too much time in it just to give up now.

Hi REB,

You cannot easily print a HTML page to a user’s browser without running ACGI Dispatcher.

AppleScripts also cannot print output to the shell. You cannot write an AppleScript that can be executed from within the Terminal and that simply prints “Hello World!” on the command line and then quits.

And that is because AppleScript does not have anything like a print command. But the print command is exactly what is used in other CGI languages (Perl, Python, Ruby) to print the HTML content to the user’s browser.

So I guess you should try to install ACGI Dispatcher and use the «on CGI reques»-handler.

True. But can I write an applescript that opens a terminal, prints “hello world” and then quits? I don’t THINK I need to execute the applescript from within the terminal…do I? How does this work:

  1. A form residing on my Macintosh is accessed by a remote user.
  2. After filling out the information in the text fields they hit the ‘submit’ button.
  3. SOMEHOW (and I am fuzzy on how I receive this information from the form) I get something that might resemble this - interlink://com.ecp.interlink?field1=monkey&field2=finger&button=submit
  4. My CGI application (InterLink) kicks off and parses the data passed and runs whichever script is called for
  5. Then I try to serve a webpage back to the user and this is where I get stuck.

I know that the ACGI app will probably do whatever I need currently, but it won’t teach me anything. I believe that if I can just route the webpage (or print it) via the terminal back to the originator in step 3 I will have managed to do what it is I hope to accomplish. This IS no doubt way beyond my capabilities at present, but I have to keep trying. If I didn’t I would never have learned as much applescript as I have (which I admit, isn’t enough either).

I guess I could just punk out and send them the link via email because my automated email system works like a charm, but that’s kind of a cop out. And lazy. Not that I’ve never been accused of NOT being lazy mind you, it’s just irritating is all.:expressionless:

I found this:

#!/bin/sh

echo Content-type: text/html
echo

echo \<html\>\<head\>\<title\>404 Errors\</title\>\</head\>

on a site describing using unix scripting as cgi. If echo = print then this might point in the right direction. I think I might also need to look into how Apache server processes a form.