Results 1 to 10 of 10

Thread: Help with CGI

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Help with CGI

    Let's say I have a link to a URL like this:

    domainname.com/cgi-bin/photo.cgi?photo=9319.jpg

    Now, the CGI code should take the query string, look at "photo" which in this case is 9319.jpg, and should show the image 9319.jpg on the page.

    I don't know any CGI, so I'd really appreciate some code.

    Thanks in advance.

  2. #2

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    *FROGBUMP*

    If nobody knows CGI, at least a link?

  3. #3
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    Here ya go froggy.
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    What kind of CGI?

    I think the query string is the QUERYSTRING or QUERY or QUERY_STRING environment variable. Parse it in your favourite language. Then do
    print("Content-type: image/jpeg\n\n");
    or the equivalent.

    Finally, load the image and write it to stdout.



    Alternatively, do a HTTP redirect: instead of the Content-type, write a Location header:
    print("Location: parsedfilename.jpg");
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Originally posted by CornedBee
    I think the query string is the QUERYSTRING or QUERY or QUERY_STRING environment variable. Parse it in your favourite language. Then do
    print("Content-type: image/jpeg\n\n");
    or the equivalent.

    Finally, load the image and write it to stdout.


    Thanks for the link Memnoch. I obviously haven't gone through it all, since I"m looking for a very rough solution.

    Can you give me an example of the usage of the stdout?

    What I'm trying to do is this:

    if I put

    <img src="http://something.netfirms.com/cgi-bin/pic.cgi?hello.jpg">

    it should output the hello.jpg image, no html whatsoever.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    It's been a while since I've done Perl, so here's a C++ CGI:
    Code:
    #include <iostream>
    #include <string>
    #include <cstdlib>
    using namespace std;
    
    int main()
    {
      string query(getenv("QUERY"));
      query = query.substr(query.find('?')+1);
      cout << "Location: " << query << "\r\n" << flush;
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    While you were gone, I cheated on you and did a frantic search on google. I ended up with this code:

    Code:
    $picname = "$ENV{'QUERY_STRING'}";
    
    $MY_FILE_NAME = $picname;
        $CHUNK_SIZE = 4096;
        
        open( MY_FILE, "<$MY_FILE_NAME" )
            or die( "Can't open $MY_FILE_NAME: $!\n" );
        
        print "Content-type: image/jpeg\r\n";
        print "\r\n";
        
        binmode( MY_FILE ); # These are crucial!
        binmode( STDOUT );
        
        while ( $cb = read( MY_FILE, $data, $CHUNK_SIZE ) ) {
            print $data;
        }
        
        close( MY_FILE );
    Is it OK as well? How come yours is different from mine? And why does yours look so much like C++?

  8. #8

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    so here's a C++ CGI
    I didn't see that.

    Anyways, is my code OK as well? Does it make a difference if I do it in Perl or Cpp?

    Why does CGI suck so much?

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The C++ is more lightweight, the server doesn't have to start the full Perl interpreter for it.

    Still, the overhead of starting a process is always there, which is why PHP or mod_perl are better.


    The difference between the scripts:
    Mine tells the browser where to look for the real image (HTTP redirect, Location: header).
    Yours IS in effect the image.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Alright, thanks a bunch. Though crude, problem solved!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width