CGI Programming - PowerPoint PPT Presentation

About This Presentation
Title:

CGI Programming

Description:

Cart itself is a hash loaded in this order: userDb, sessionDb, CGI variables. ... Routines in cart.h handle loading and access to this hash. You can explicitly ... – PowerPoint PPT presentation

Number of Views:64
Avg rating:3.0/5.0
Slides: 12
Provided by: jimk88
Category:

less

Transcript and Presenter's Notes

Title: CGI Programming


1
CGI Programming
  • Making web works at UCSC

2
CGI Common Gateway Interface
  • CGI - oldest method for executing a program over
    the web. Very portable.
  • A round trip between browser and server is
    required to update a page with pure CGI.
  • Can add Javascript for increased interactivity at
    the expense of portability and complexity.

3
Anatomy of a CGI Script
  • Gets called by Web Server when user clicks submit
    or follows a cgi link.
  • Input is in environment variables and sometimes
    also stdin.
  • Output is to stdout, primarily in html format.
  • In the middle often access a database, do some
    calculations, etc.
  • Sometimes in the middle create an image.

4
Challenges of CGI
  • Each click launches program anew.
  • User state has to be maintained by some special
    mechanism. We use the cart.
  • Run from Web Server, harder to debug
  • A program crash will cause an error 500 message,
    with truncated, often completely invisible
    output.
  • Needs to run quickly enough to preserve sense of
    interactivity, ideally in less than 2 seconds.

5
CGI Input
  • Input is contents filled out web form in one of
    three methods
  • Get varval pairs in URL itself
  • http//google.com/search?rlsen-usqgenomebrowse
    rbtnGSearch
  • Post varval pairs in stdin
  • Multipart stdin contains uploaded files as well
    as varval pairs.
  • Routines in inc/cheapcgi.h transparently put data
    from all methods into a hash keyed by variable
    name.
  • Typically we use a higher level interface,
    inc/hg/cart.h.

6
Persistence of State
  • If have more than one web page on a site, want to
    preserve information entered in one page for
    later.
  • Two basic methods exist for this
  • Hidden variables - variable in form without
    associated controls.
  • Cookies - Sent in http header. can be short or
    long term.
  • Both hidden vars and cookies have limited space,
    so usually put most of data in a database, and
    just put key in var/cookies.
  • Our database is called the cart.

7
How the cart works
  • Two tables in database hgCommon - sessionDb and
    userDb.
  • sessionDbs key is hidden var hgsid
  • userDbs key is permanent cookie hguid
  • Table contains key, blob with var/val pairs and
    date and use counts.
  • Cart itself is a hash loaded in this order
    userDb, sessionDb, CGI variables.
  • Routines in cart.h handle loading and access to
    this hash.
  • You can explicitly add and remove things from
    cart as well.
  • At end of program, cart is written back to tables.

8
Output of CGI Scripts
  • Start out with HTTP header. Try
  • htmlCheck getHeader http//genome.ucsc.edu/cgi-bin
    /hgTracks
  • (htmlCheck very useful in general, based on
    htmlPage.c HTML parsing library.)
  • Next comes HTML, most often a FORM.
  • Often can include an image. Typically generate
    the image on the fly and include the URL for the
    image in the HTML.

9
CGI Script Output
  • Typically HTTP header, and beginning/end of HTML
    is generated by routines in lib/htmlShell.c
    (vanilla) or hg/lib/web.c (UCSC look and feel)
  • A wide variety of options are available, the
    newProg -cgi program will generate something
    basic.
  • Use routines in cheapcgi.c to generate form
    elements such as drop-downs.
  • Images are embedded with IMG tags or with INPUT
    image tags.

10
Images in CGIs
  • Generate a file name in ../trash dir with
    makeTempName()
  • Use vgOpenGif()/vgCloseGif() to start/end image.
  • Draw with vgLine, vgBox, vgText, etc.
  • Limited to 256 colors.
  • Embed URL including trash dir in HTML.
  • Theoretically could call a CGI that write image
    to stdout instead via URL. GMOD works this way.
  • Use image map tags to make hyperlinks over image.
  • Can also instead get mouse x/y position of image
    click.

11
Examples
  • CGI that draws a rectangle, box, or line in
    user-defined colors.
  • CGI that looks up common name from scientific
    name and vice versa.
  • Use uniProt database and routines in
    hg/inc/spDb.h including spBinomialToTaxon.
  • CGI that lets you paste in two sequences and
    returns alignment
  • Use src/utils/faAlign and src/hg/mouseStuff/axtPre
    tty as starting point.
Write a Comment
User Comments (0)
About PowerShow.com