[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  Programming
. -> [Folder]  General
. . -> [Subject]  lua and io.read

lua and io.read

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Jayveesea   (5 posts)  [Biography] bio
Date Thu 18 Aug 2011 03:02 AM (UTC)
Message
i have been going through this info posted about using lua on a server... http://www.gammon.com.au/forum/?id=6498 ...but i am having difficulty with the POST method. it seems to hang at the point where the POST data is retrieved via io.read. here is an excerpt from the "cgiutils.lua" portion...

-----begin-----
local post_length = tonumber (os.getenv ("CONTENT_LENGTH")) or 0
if os.getenv ("REQUEST_METHOD") == "POST" and post_length > 0 then
for _, v in ipairs (split (io.read (post_length), "&")) do
assemble_value (v, post_data)
end -- for
end -- if post
------end------

is there any trick when using io.read? did i miss something?

[Go to top] top

Posted by Nick Gammon   Australia  (22,985 posts)  [Biography] bio   Forum Administrator
Date Reply #1 on Thu 18 Aug 2011 03:22 AM (UTC)
Message
Did you read the notes on page 2 of that thread about sending:


  print ("Connection: close")


?

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Jayveesea   (5 posts)  [Biography] bio
Date Reply #2 on Sun 21 Aug 2011 12:53 AM (UTC)
Message
i did not see that... so i gave it a try, but no luck.

i also got a response here... http://forum.luahub.com/index.php?topic=3184.0
...this worked for me, so i adapted it to cgiutils.lua, although i do not see that it is any different (besides being its own function.


-- get the post data from form
function getpost()
  
  local post_data={}
  
  local post_length = tonumber (os.getenv ("CONTENT_LENGTH")) or 0
  if os.getenv ("REQUEST_METHOD") == "POST" and post_length > 0 then
    
        local n = tonumber(os.getenv("CONTENT_LENGTH"))
	
	local post=io.read(n)
	
	for _, v in ipairs (split (post, "&")) do
          assemble_value (v, post_data)
        end -- for
	
  end -- if
  
  return post_data
  
end -- getpost
[Go to top] top

Posted by Nick Gammon   Australia  (22,985 posts)  [Biography] bio   Forum Administrator
Date Reply #3 on Sun 21 Aug 2011 08:24 PM (UTC)
Message
What you posted on the other forum, namely:


 while io.stdin:read("*line") do
    g = io.stdin:read("*all")
  end


is not what you had (or I had) in the cgiutils.lua.

Are you using cgiutils.lua exactly as posted in my other thread? If not, can you post your entire modified version please? The change you made indeed doesn't seem very different, except that you are doing os.getenv ("CONTENT_LENGTH") twice, which shouldn't make any difference.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Jayveesea   (5 posts)  [Biography] bio
Date Reply #4 on Mon 22 Aug 2011 01:35 AM (UTC)
Message
what i had posted there was a simple script i was trying to get to work... i had narrowed the problem down to the part in cgiutils.lua where "io.read ("*a")" was happening for a POST retrieval... so i started experimenting with other syntax to see if it would work.

so here is what i had that would not work...

with this page...
#! /usr/bin/lua

dofile "cgiutils.lua"

-- HTTP header
print [[
Content-Type: text/html; charset=iso-8859-1

]]

-- body of page

-- XML header, doctype, page header

print [[
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head> 
<title>My title</title>
</head>
<body>
]]

--- form test

print [[
<form METHOD="post" ACTION=http://10.0.0.2/cgi-bin/test4.lua?humphrey=bogart>
<p>Enter data here: <input type=text Name="Widget" size=20 maxlength=20>
<input Type=hidden Name=date Value="2006-04-28">
<input Type=hidden Name=action Value=fubar>
<input Type=submit Name=Submit Value="Submit">
</p>
</form>
]]

---- end form test

get_data, cookie_data, post_data, request_method = get_user_input ()

print "<h1>GET data</h1>\n"
show_table (get_data)

print "<h1>COOKIE data</h1>\n"
show_table (cookie_data)

print "<h1>POST data</h1>\n"
show_table (post_data)

print ("<p>Request method = ", request_method, "</p>")

-- end of body

print [[
</body>
</html>
]]

[Go to top] top

Posted by Jayveesea   (5 posts)  [Biography] bio
Date Reply #5 on Mon 22 Aug 2011 01:40 AM (UTC)
Message
...and i can not post the cgiutils.lua because i am hitting a 6000 char max (even with header removed)... but it is the final version on page1 of http://www.gammon.com.au/forum/bbshowpost.php?id=6498&page=1 with the modifications you pointed out from page 2.

also, it appears that mongoose server does not use PATH_INFO, so that part of
cgiutils does not work for me... as of right now i just comment that part out.

[Go to top] top

Posted by Nick Gammon   Australia  (22,985 posts)  [Biography] bio   Forum Administrator
Date Reply #6 on Mon 22 Aug 2011 04:56 AM (UTC)
Message
The post on the other thread that starts "Below is the improved cgiutils.lua file." has the corrected version, that uses this:


local post_length = tonumber(os.getenv("CONTENT_LENGTH"))


The earlier version (when I was experimenting) used:


POST_DATA = io.read ("*a")  -- read all of stdin


... which, as you found, can hang because it is trying to read "all data" without knowing how much all is.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Jayveesea   (5 posts)  [Biography] bio
Date Reply #7 on Tue 23 Aug 2011 03:44 AM (UTC)
Message
i tried both methods, both would hang when using the mongoose server.
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


20,195 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]