Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > CGI Request Failing

Reply
Thread Tools

CGI Request Failing

 
 
exiquio
Guest
Posts: n/a
 
      05-20-2010
I wrote the site at tetractysproductions.com. It is a CGI driven
webpage being served by Cherokee. It is pretty simple. I make
asyncronous requests t o fill the various "containers" around the
page. On my development machine running the same Arch Linux/Cherokee
combinations with the same settings every request is consistently
satisfied. However, when run on remote servers the requests are
inconsistent returning 500s on one occassion and 200s on the next.
Click around the menus on my site and you will likely reproduce this.

The file that answers the request contains the following code:

***START***
#!/usr/bin/env ruby

require 'json'

require 'cgi'

# CONSTANTS
DATA_PATH = 'data'
VALID_TYPES = [
:articles,
:article_title,
:menu,
:news,
roducts
]
VALID_CATEGORIES = [
:home,
:music,
:film,
:books,
:software,
:blog,
:all
]

# Validate parameters
cgi = CGI.new
cgi.out {'Bad Request: Incorrect value for parameter "type"'} unless
VALID_TYPES.include? cgi['type'].to_sym
cgi.out {'Bad Request: Incorrect value for parameter "category"'}
unless VALID_CATEGORIES.include? cgi['category'].to_sym


# DATA RETRIEVAL FUNCTIONS

def get_article(category, id)
data = JSON.parse(File.open("#{DATA_PATH}/articles.json", 'r') {|f|
f.read})
data['articles'].each do |section|
if section['category'] == category
section['entries'].each do |entry|
if entry['id'] == id
CGI.new.out {"Failed to require view: #{entry['path']}"}
unless require entry['path']
CGI.new.out {render}
end
end
end
end
CGI.new.out {"Failed to find #{id} in #{category}"}
end

def get_article_title(category, id)
data = JSON.parse(File.open("#{DATA_PATH}/articles.json", 'r') {|f|
f.read})
data['articles'].each do |section|
if section['category'] == category
section['entries'].each do |entry|
CGI.new.out {entry['title']} if entry['id'] == id
end
end
end
end

def get_menu(category)
data = JSON.parse(File.open("#{DATA_PATH}/menus.json", 'r') {|f|
f.read})
data['menus'].each {|menu| CGI.new.out
{JSON.generate(menu['entries'])} if menu['category'] == category}
CGI.new.out {"Menu category \"#{category}\" not found"}
end

def get_news(category)
data = JSON.parse(File.open("#{DATA_PATH}/news.json", 'r') {|f|
f.read})
if category == 'all'
CGI.new.out {JSON.generate(data['news'])}
else
data['news'].each {|article| CGI.new.out
{JSON.generate(article['articles'])} if article['category'] ==
category}
end
CGI.new.out {"News category \"#{category} not found"}
end

def get_products(category)
data = JSON.parse(File.open("#{DATA_PATH}/products.json", 'r') {|f|
f.read})
if category == 'all'
CGI.new.out {JSON.generate(data['products'])}
else
data['products'].each {|item| CGI.new.out
{JSON.generate(item['items'])} if item['category'] == category}
end
CGI.new.out {"Products category \"#{category} not found"}
end

# Determine operation
case cgi['type']
when 'articles'
cgi.out {'Bad Request: Incorrect value for parameter "id"'} unless
cgi['id'].to_i > 0
get_article(cgi['category'], cgi['id'])
when 'article_title'
cgi.out {'Bad Request: Incorrect value for parameter "id"'} unless
cgi['id'].to_i > 0
get_article_title(cgi['category'], cgi['id'])
when 'menu' then get_menu(cgi['category'])
when 'news' then get_news(cgi['category'])
when 'products' then get_products(cgi['category'])
else cgi.out {"Bad Request: #{cgi['type']}"}
end

***END***

I have never written CGI programs before so I assume I must be doing
something wrong. Are any of you good people able to spot my mistake
from either the brief description, the actual webpage or the code? I
appreciate your time and thanks you all in advance.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
https request failing mike Perl Misc 2 09-19-2012 03:02 PM
IIS6 POST request failing with 500 error Ed Dearlove ASP .Net 11 08-19-2004 11:06 AM
Simple Web Request failing with code 403 after several successful calls Anthony Frayling ASP .Net Web Services 0 03-05-2004 02:59 PM
Re: Accessing Request.InputStream / Request.BinaryRead *as the request is occuring*: How??? Brian Birtle ASP .Net 2 10-16-2003 02:11 PM
Re: CGI Perl "use CGI" statement fail Jürgen Exner Perl 0 07-31-2003 02:00 PM



Advertisments