[RFC] Form Processing

Discussion in 'Ruby' started by Dan Janowski, Dec 21, 2004.

  1. Dan Janowski

    Dan Janowski Guest

    Please comment if you think this is useful, good, bad (or ugly). It is
    all written and partially tested, but I am interested in some feedback
    before I release it or if I should.

    I wanted a bit more intelligence in CGI form processing. This is an
    effort to that end. I called it Forms, derived from the HTML entity and
    that it allows the representation of a Form within Ruby.

    Forms is a CGI parser, value manager and validator that works with
    cgi-bin, mod_ruby and WEBrick (in the most efficient block handling
    even though the body is available is completely conflicting ways). It
    performs only request processing and does nothing related to response
    generation or handling.

    Values are uniformly mapped to reduce the multi-value problems in other
    implementations. Each value is tagged with its source, :GET, :pOST,
    :COOKIE. Values from all sources are combined and accessed the same
    way.

    Validation is done by creating a form template before parsing. There
    are a few Input types which correspond to HTML form types and they do
    basic validation. Additional parsing/validation can be hooked in.

    multiparts are handled in a slightly different way than other
    libraries, if the input is a file, then it is always put to a Tempfile,
    otherwise it is kept in as a regular String. There is no use of
    StringIO. I think this keeps expectations in line with what is being
    asked for in the HTML Form. File input handling only keeps a trailing
    block of data in memory while looking for the boundary, everything else
    written to the temp file.

    Some other features:
    a. allow_generics=true enables capture of non-predefined values,
    otherwise they are dropped. Generic inputs do not get input validation
    since there is no basis.

    b. default values

    c. is_set? (only true if the value came from the client)

    ------------------------------------------------
    Here is a simple example for a cgi-bin:

    f=Forms.new
    f.allow_generics=true
    p=f.input_template(Forms::Text.new('hipmo'))
    p.parser=proc {|input,vsp|
    throw:)invalid) if vsp.value.empty?
    vsp.value.capitalize!
    }

    p=f.input_template(Forms::File.new('upfile'))
    p.parser= proc {|input,vsp|
    throw:)invalid) unless vsp.respond_to?:)filename) and vsp.filename =~
    /\.zip$/
    }

    f.parse $stdin


    ...
    <form method=POST enctype=multipart/form-data>
    <input type=text name=hipmo>
    <input type=file name=upfile>
    <input type=submit>
    </form>

    ...

    if f['upfile'].valid?
    puts "<pre>"+f['upfile'].content_type+"</pre>"
    puts "<pre>"+f['upfile'].filename+"</pre>"
    puts "<pre>"+f['upfile'].length.to_s+"</pre>"

    zipfile=Zip::ZipInputStream.open(f['upfile'].path)
    while (ze=zipfile.get_next_entry)
    puts "<pre>"+ze.name+"</pre>"
    end
    end
     
    Dan Janowski, Dec 21, 2004
    #1
    1. Advertising

  2. Hello,

    this library seems interesting. I would like to have a look at the
    actual implementation.

    regards,
    George
     
    George Moschovitis, Dec 22, 2004
    #2
    1. Advertising

  3. I have been looking for something in Ruby that would allow me to have
    a web user agent (a programmatic browser if you prefer) that can drive
    a series of Web page mostly for the purpose of doing testing of a Web
    based application.

    it looks like what you have done can help in this respect by allowing
    me to receive froms from the server and manipulate them as form
    objects then post the reply to the server.

    Am I correct?

    Laurent
     
    Laurent Julliard, Dec 23, 2004
    #3
    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. Jukka K. Korpela
    Replies:
    3
    Views:
    785
    Michael Winter
    Mar 6, 2004
  2. Michael Winter
    Replies:
    4
    Views:
    599
    Lasse Reichstein Nielsen
    Mar 6, 2004
  3. Robert
    Replies:
    0
    Views:
    517
    Robert
    Mar 6, 2004
  4. Ivan Shmakov
    Replies:
    3
    Views:
    1,174
    Kari Hurtta
    Feb 13, 2012
  5. Edward
    Replies:
    1
    Views:
    149
    Lasse Reichstein Nielsen
    Jan 4, 2004
Loading...

Share This Page