Write a cgi code in C

Is it possible to write CGI code in C or C ++? please give me "hello world !!!". Example.

+4
source share
4 answers

That's right.

#include <stdio.h> int main(int argc, char *argv[]]) { printf("Content-type: text/plain\n\n"); printf("Hello, world!\n") } 
+17
source

Eve, as soon as you understand the basics of Ignacio's answer (cgi-bin, executed from a browser, web server, etc.), there are some very useful libraries that will help you with your web type.

Here is the library that I used for my cgi in C that works great, saves your days:
(cgihtml is a collection of CGI and HTML routines written for C)
http://eekim.com/software/cgihtml/index.html

you can add html templates to display large amounts of data:
http://www.algonet.se/~thunberg/template2doc/

Lightweight WebServers:
http://en.wikipedia.org/wiki/Comparison_of_lightweight_web_servers

and additional resources:
http://cgi.resourceindex.com/Programs_and_Scripts/C_and_C++/Libraries_and_Classes/

http://en.wikipedia.org/wiki/Common_Gateway_Interface

+3
source

Compile this simple source code into an executable file:

 #include <stdio.h> int main() { printf("content-type: text/plain\n\n"); printf("Hello, world!"); return 0; } 

I assume that the compiled cgi-app.cgi :

 gcc cgi-src.c -o cgi-app.cgi 

If you run httpd as your server software, you can put cgi-app.cgi in your directories:

  • cgi-bin : allowed to run CGI in most cases.
  • htdocs : add 2 lines to your .htaccess file:
Options + ExecCGI AddHandler cgi-script .cgi

Remember to set the appropriate execute permission for .htaccess and cgi-app.cgi

+1
source

Look kcgi

kcgi is an open source CGI library and FastCGI for C web applications. It is minimal, secure, and auditable.

 #include <sys/types.h> /* size_t, ssize_t */ #include <stdarg.h> /* va_list */ #include <stddef.h> /* NULL */ #include <stdint.h> /* int64_t */ #include <stdlib.h> /* EXIT_SUCCESS, etc. */ #include <kcgi.h> int main(void) { struct kreq r; const char *page = "index"; if (KCGI_OK != khttp_parse(&r, NULL, 0, &page, 1, 0)) return(EXIT_FAILURE); khttp_head(&r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]); khttp_head(&r, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME__MAX == r.mime ? KMIME_APP_OCTET_STREAM : r.mime]); khttp_body(&r); khttp_puts(&r, "Hello, world!"); khttp_free(&r); return(EXIT_SUCCESS); } 
0
source

All Articles