# CGI Request Failing

**URL:** <https://www.ruby-forum.com/t/cgi-request-failing/188933>\
**Category:** Ruby\
**Created:** [May 20, 2010, 6:05pm UTC](https://www.ruby-forum.com/t/cgi-request-failing/188933 "2010-05-20T18:05:29Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![exiquio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/77aa72/32.png) [@exiquio](https://www.ruby-forum.com/u/exiquio)\
**Post date:** [May 20, 2010, 6:05pm UTC](https://www.ruby-forum.com/t/cgi-request-failing/188933/1 "2010-05-20T18:05:29Z")

</div>

I wrote the site at [tetractysproductions.com](http://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.
