[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

CGI help

Jeff Leggett

5/8/2009 10:04:00 PM

So, I am trying ot read the contents of a file and format the contents
there into a CGI form. At 6PM on A friday hough, I am just not seeing
my errors - appreciate another srt of eyes!

require "cgi"

Filename = 'classification.txt'

EMPTY_STRING = ''
TITLE = 'Vulnerability Classification'
cgi = CGI.new('html4')
output = cgi.html do
cgi.head { cgi.title { TITLE } } +
cgi.body { cgi.h1 { TITLE } } +
cgi.form('post') do
if File.readable?(Filename) then
f = File.open(Filename,"r")
f.each_line { |l| l.chomp
inputs = l.split(',')
puts inputs[0].to_s + ": "
opts = "\"" + inputs[1].to_s + "\""
2.times { inputs.delete_at(0) }
until inputs.empty? do
opts = opts + "[\"" + inputs[1].to_s + "\",\"" +
inputs[0].to_s + "\"]"
2.times { inputs.delete_at(0) }
end
cgi.popup_menu(opts) +
cgi.br +
}
f.close
end
cgi.submit
end
end
cgi.out { output.gsub('><', ">\n<") }
--
Posted via http://www.ruby-....

18 Answers

James Britt

5/8/2009 10:28:00 PM

0

Jeff Leggett wrote:
> So, I am trying ot read the contents of a file and format the contents
> there into a CGI form. At 6PM on A friday hough, I am just not seeing
> my errors - appreciate another srt of eyes!
>

Your file may need to start with a shebang line:

#!/usr/local/bin/ruby

or whatever the path to ruby on your system is.


Also, print out the correct headers

http://www.ruby-doc.org/docs/ProgrammingRuby/htm...


It helps to get the simplest possible CGI script running first, then
adding details, since debugging CGI is a painful.


--
James Britt

www.jamesbritt.com - Playing with Better Toys
www.ruby-doc.org - Ruby Help & Documentation
www.rubystuff.com - The Ruby Store for Ruby Stuff
www.neurogami.com - Smart application development

Jeff Leggett

5/9/2009 12:14:00 AM

0

James Britt wrote:

>
> Your file may need to start with a shebang line:
>
> #!/usr/local/bin/ruby
>

Yes, it does - I just left that out of the C&P.

>
>
> Also, print out the correct headers
>
> http://www.ruby-doc.org/docs/ProgrammingRuby/htm...
>

The CGI does all that for you... My problem is if I take out the inner
loop (starting with "if File.readable?(Filename) then" it works fine. I
just can't for the life of me see the syntax errors.

[jleggett@binford cgi-bin]$ ./vuln.rb
/vuln.rb:27: syntax error
/vuln.rb:29: syntax error
end
^
/vuln.rb:31: syntax error
end
^
/vuln.rb:37: syntax error
[jleggett@binford cgi-bin]$
--
Posted via http://www.ruby-....

7stud --

5/9/2009 5:59:00 AM

0

Jeff Leggett wrote:
> output = cgi.html do
> cgi.head { cgi.title { TITLE } } +
> cgi.body { cgi.h1 { TITLE } } +
How about getting rid of.........^ ?
--
Posted via http://www.ruby-....

Jeff Leggett

5/9/2009 4:36:00 PM

0

7stud -- wrote:
> Jeff Leggett wrote:
>> output = cgi.html do
>> cgi.head { cgi.title { TITLE } } +
>> cgi.body { cgi.h1 { TITLE } } +
> How about getting rid of.........^ ?

I changed it like that, EXACT same errors. I have matched up the 'ends'
and bracket more times than I can count now, even slept on it, thinking
my tired brain would see it right off this morning, but no... Thanks for
your help though guys...

#!/usr/bin/ruby

require "cgi"

Filename = 'classification.txt'

EMPTY_STRING = ''
TITLE = 'Vulnerability Classification'
cgi = CGI.new('html4')
output = cgi.html do
cgi.head { cgi.title { TITLE } } +
cgi.body { cgi.h1 { TITLE } +
cgi.form('post') do
if File.readable?(Filename) then
f = File.open(Filename,"r")
f.each_line { |l| l.chomp
inputs = l.split(',')
puts inputs[0].to_s + ": "
opts = "\"" + inputs[1].to_s + "\""
2.times { inputs.delete_at(0) }
until inputs.empty? do
opts = opts + "[\"" + inputs[1].to_s + "\",\"" +
inputs[0].to_s + "\"]"
2.times { inputs.delete_at(0) }
end
cgi.popup_menu(opts) +
cgi.br +
}
f.close
end
cgi.submit
end
}
end
cgi.out { output.gsub('><', ">\n<") }

[jleggett@binford cgi-bin]$ ./vuln.rb
/vuln.rb:27: syntax error
/vuln.rb:29: syntax error
end
^
/vuln.rb:31: syntax error
end
^
/vuln.rb:38: syntax error
[jleggett@binford cgi-bin]$
--
Posted via http://www.ruby-....

Douglas A. Seifert

5/9/2009 4:52:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

>
> cgi.popup_menu(opts) +
> cgi.br +
>
>
You have an extraneous '+' at the end of that last line. Remove it and your
script should start working.

Cheers,
Doug Seifert

Jeff Leggett

5/9/2009 5:06:00 PM

0

Douglas Seifert wrote:
>>
>> cgi.popup_menu(opts) +
>> cgi.br +
>>
>>
> You have an extraneous '+' at the end of that last line. Remove it and
> your
> script should start working.
>
> Cheers,
> Doug Seifert


*SIGH* Sure enough that fixed the syntax errors... Darn thing still
doesn't work though :( Writes the h1 TITLE, and the submit, nothing in
between...

My own fault trying to learn a new language - done it in perl I'd been
done yesterday at lunch LOL

Thanks for helping the n00b guys.
--
Posted via http://www.ruby-....

Douglas A. Seifert

5/9/2009 5:26:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

>
>
> *SIGH* Sure enough that fixed the syntax errors... Darn thing still
> doesn't work though :( Writes the h1 TITLE, and the submit, nothing in
> between...
>

Can you post the contents of classification.txt? Some sample data would
help. Is the file readable by the web server process? Are you sure that
the working directory when the cgi is run is such that opening
classification.txt with no other path info will work ( puts Dir.pwd ) ...?

-Doug Seifert

Jeff Leggett

5/9/2009 5:43:00 PM

0

Douglas Seifert wrote:
>>
>>
>> *SIGH* Sure enough that fixed the syntax errors... Darn thing still
>> doesn't work though :( Writes the h1 TITLE, and the submit, nothing in
>> between...
>>
>
> Can you post the contents of classification.txt? Some sample data would
> help. Is the file readable by the web server process? Are you sure
> that
> the working directory when the cgi is run is such that opening
> classification.txt with no other path info will work ( puts Dir.pwd )
> ...?
>
> -Doug Seifert

[jleggett@binford cgi-bin]$ cat classification.txt
Access Control,access,High,0,Medium,5,Low,10
Authentication,auth,High,0,Medium,5,Low,10
Confidentiality Impact,confi,High,10,Medium,5,Low,0
Confidentiality modifier,confm,Employee Data,30,Customer
Data,20,System/network Data,10,application metadata,5,N/A,0
integrity impact,integi,High,10,Medium,5,Low,0
integrity modifier,integm,System/Application,30,Customer
Information,20,Employee Info,30,Individual Info,10,N/A,0
Availability Impact,avail,High,10,medium,5,Low,0
[jleggett@binford cgi-bin]$

Yeah I added an else to the File.readable link to make sure it's
there... it's there... the problem seems to be now it's not building the
CGI all up... I get errors again.

1 #!/usr/bin/ruby

2 require "cgi"

3 Filename = 'classification.txt'

4 EMPTY_STRING = ''
5 TITLE = 'Vulnerability Classification'
6 cgi = CGI.new('html4')
7 output = cgi.html do
8 cgi.head { cgi.title { TITLE } } +
9 cgi.body { cgi.h1 { TITLE } +
10 cgi.form('post', 'http://binford.x.com/cgi-bin/vulncla...)
do
11 if File.readable?(Filename) then
12 f = File.open(Filename,"r")
13 f.each_line { |l| l.chomp
14 inputs = l.split(',')
15 cgi.p { inputs[0].to_s + ": " } +
16 #opts = "\"" + inputs[1].to_s + "\""
17 2.times { inputs.delete_at(0) }
18 until inputs.empty? do
19 opts = opts + "[\"" + inputs[1].to_s + "\",\""
+ inputs[0].to_s + "\"]"
20 2.times { inputs.delete_at(0) }
21 end
22 # puts opts
23 cgi.popup_menu(opts) +
24 cgi.br
25 }
26 f.close
27 else
28 puts "File not read-able!\n"
29 end
30 cgi.submit
31 end
32 }
33 end
34 cgi.out { output.gsub('><', ">\n<") }

Produces:

[jleggett@binford cgi-bin]$ ./vuln.rb
(offline mode: enter name=value pairs on standard input)
/vuln.rb:20:in `+': can't convert Fixnum into String (TypeError)
from ./vuln.rb:20
from ./vuln.rb:16:in `each_line'
from ./vuln.rb:16
from /usr/lib/ruby/1.8/cgi.rb:1557:in `form'
from ./vuln.rb:13
from (eval):1022:in `body'
from ./vuln.rb:12
from /usr/lib/ruby/1.8/cgi.rb:1657:in `html'
from (eval):1006:in `html'
from /usr/lib/ruby/1.8/cgi.rb:1657:in `html'
from ./vuln.rb:10
[jleggett@binford cgi-bin]$


--
Posted via http://www.ruby-....

7stud --

5/9/2009 5:47:00 PM

0

Jeff Leggett wrote:
> 7stud -- wrote:
>> Jeff Leggett wrote:
>>> output = cgi.html do
>>> cgi.head { cgi.title { TITLE } } +
>>> cgi.body { cgi.h1 { TITLE } } +
>> How about getting rid of.........^ ?
>
> I changed it like that, EXACT same errors. I have matched up the 'ends'
> and bracket more times than I can count now, even slept on it, thinking
> my tired brain would see it right off this morning, but no... Thanks for
> your help though guys...
>
> #!/usr/bin/ruby
>
> require "cgi"
>
> Filename = 'classification.txt'
>
> EMPTY_STRING = ''
> TITLE = 'Vulnerability Classification'
> cgi = CGI.new('html4')
> output = cgi.html do
> cgi.head { cgi.title { TITLE } } +
> cgi.body { cgi.h1 { TITLE } +
> cgi.form('post') do
> if File.readable?(Filename) then
> f = File.open(Filename,"r")
> f.each_line { |l| l.chomp
> inputs = l.split(',')
> puts inputs[0].to_s + ": "
> opts = "\"" + inputs[1].to_s + "\""
> 2.times { inputs.delete_at(0) }
> until inputs.empty? do
> opts = opts + "[\"" + inputs[1].to_s + "\",\"" +
> inputs[0].to_s + "\"]"
> 2.times { inputs.delete_at(0) }
> end
> cgi.popup_menu(opts) +
> cgi.br +
> }
> f.close
> end
> cgi.submit
> end
> }
> end
> cgi.out { output.gsub('><', ">\n<") }
>
> [jleggett@binford cgi-bin]$ ./vuln.rb
> ./vuln.rb:27: syntax error
> ./vuln.rb:29: syntax error
> end
> ^
> ./vuln.rb:31: syntax error
> end
> ^
> ./vuln.rb:38: syntax error
> [jleggett@binford cgi-bin]$

Putting these lines:

cgi.popup_menu(opts) +
cgi.br +

inside the if statement doesn't work for me: I get a bunch of kEND
errors. If I move those lines outside the if statement, then no errors:

cgi = CGI.new('html4')
output = cgi.html {
cgi.head { cgi.title { TITLE } } +
cgi.body { cgi.h1 { TITLE } +
cgi.form('post') {
if File.readable?(Filename)
f = File.open(Filename,"r")
opts = ""

f.each_line do |l|
l.chomp #does nothing
inputs = l.split(',')
puts inputs[0].to_s + ": "
opts << "\"" + inputs[1].to_s + "\""
2.times { inputs.delete_at(0) }
until inputs.empty?
opts << "[\"" + inputs[1].to_s + "\",\"" + inputs[0].to_s +
"\"]"
2.times { inputs.delete_at(0) }
end
end #each_line

f.close
end #if

cgi.popup_menu(opts) +
cgi.br +
cgi.submit
} #form

} #cgi.body
} #cgi.html

cgi.out { output.gsub('><', ">\n<") }


I have no idea why that is the case.

--
Posted via http://www.ruby-....

7stud --

5/9/2009 6:07:00 PM

0

7stud -- wrote:
>
> I have no idea why that is the case.
>

Here is a simplification of the issue if anyone wants to offer some
advice. The following code produces no errors:

#!/usr/bin/ruby

require "cgi"

cgi = CGI.new('html4')
output = cgi.html {
cgi.head { cgi.title { "Test" } } +
cgi.body {
cgi.form('post') {
cgi.textarea() +
cgi.submit
} #form
} #cgi.body
} #cgi.html



This code produces kEND errors:

#!/usr/bin/ruby

require "cgi"

cgi = CGI.new('html4')
output = cgi.html {
cgi.head { cgi.title { "Test" } } +
cgi.body {
cgi.form('post') {
if true
cgi.textarea() +
end
cgi.submit
} #form
} #cgi.body
} #cgi.html


--
Posted via http://www.ruby-....