CGI Request Failing

Discussion in 'Ruby' started by exiquio, May 20, 2010.

  1. exiquio

    exiquio Guest

    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.
    exiquio, May 20, 2010
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jürgen Exner

    Re: CGI Perl "use CGI" statement fail

    Jürgen Exner, Jul 31, 2003, in forum: Perl
    Replies:
    0
    Views:
    1,254
    Jürgen Exner
    Jul 31, 2003
  2. Brian Birtle
    Replies:
    2
    Views:
    2,015
    John Saunders
    Oct 16, 2003
  3. Ed Dearlove

    IIS6 POST request failing with 500 error

    Ed Dearlove, Aug 19, 2004, in forum: ASP .Net
    Replies:
    11
    Views:
    4,568
    Ed Dearlove
    Aug 19, 2004
  4. Anthony Frayling
    Replies:
    0
    Views:
    191
    Anthony Frayling
    Mar 5, 2004
  5. mike

    https request failing

    mike, Sep 18, 2012, in forum: Perl Misc
    Replies:
    2
    Views:
    407
    Dr.Ruud
    Sep 19, 2012
Loading...

Share This Page