Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
I am in the process of building my first website and have decided to write my own cgi for form handling and browser response. I know that in windows programming output is directed to a window and in console programming output is directed to the console. To what does one direct output meant for the browser when writing a cgi?
Posted 2-Nov-11 6:52am
Gbenbam988
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Just print to the console, and remember to set the right Content-Type.
  Permalink  
v2
Comments
Gbenbam at 2-Nov-11 11:58am
   
You mean the MIME ?
Nishant Sivakumar at 2-Nov-11 12:04pm
   
No, the HTTP headers.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

To save you some future debugging / headaches.
 
1) you have to have proper http headers *before* the data is output and they contain the number of characters in the output. Therefore, you need to "buffer up" all your output inside the application before finally dumping it to 'stdout'
 
2) C/C++ Newline ('\n') will only count as 1 character in a buffer but will become two characters when you print it on 'stdout' so count the number of newlines separately and add that to the length of the buffered output.
 
Below is the bottom of my CGI app. I buffer the output into total_printout and count newlines in total_newlines. How you do your output buffering and the like is up to you.
 
printf("Content-type: text/html\n");        // make receiving browser happy
if (total_printout != NULL)
{
    printf("Content-length: %d\n\n", strlen(total_printout)+total_newlines);
    printf("%s", total_printout);           // now send it
    free(total_printout);                   // return accumulated output
}
else
{
    printf("Content-length: 0\n\n");        // no output
}
  Permalink  
v2
Comments
Gbenbam at 27-Dec-11 12:47pm
   
From what does a cgi program read an html for input? I know cgi programs direct output to the console. But I wish to know from what it gets its form input from browser. It certainly cannot be the keyboard.
 
Where can I get a sample code of cgi getting form input?
 
Athough this is not programmming related, do you by chance know the answer to the following questions?
1. How does one e-mail ann e-mail form in HTML. By this I mean what will the action attribute form an e-mail foirm be set to.Will it be a mailto:\\username@DomainnName.com I just want o know how to include e-mail sending to my website.
 
2.How can I make my website receive e-mail. Where do I get a sample cgi program that handles reception of e-mails. How is coding for e-mail reception done?
 
3.Where can I get a sample server-side image-map map program with explanation on how it receives input from browser. How is coding for server-side image map done. How does a server-side image map file look like.
 
4. Where can I get a sample code for downloading/uploading files from/to a remote server computer in a network when
1. The computers belong to the same domain;
2. the computers belong to the same work group.
Chuck O'Toole at 27-Dec-11 12:57pm
   
Sorry pal, you'll have to ask those to the general forum. I don't have any quick pointers to documents for you and you can search for them just as easily as I could.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 587
1 Sergey Alexandrovich Kryukov 519
2 Maciej Los 305
3 BillWoodruff 250
4 Mathew Soji 195
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,777
2 DamithSL 5,461
3 Manas Bhardwaj 4,916
4 Maciej Los 4,475


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 2 Nov 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100