CGI Request Failing

E

exiquio

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,
:products
]
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top