How to use C as a scripting language?

#1

I am using php in nginx. I wonder how can I modify nginx configuration
to work with C too. Considered a simple c file (hello.c and compiled as
a.out) with printf of

printf(“Content-type: text/html\r\nStatus: 200 OK\r\n\r\n”);
printf(“Hello World”);

how can I display Hello World by browsing the compiled version on
localhost/a.out

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,219531,219531#msg-219531

#2

On 3 Dez 2011 07h38 WET, removed_email_address@domain.invalid wrote:

I am using php in nginx. I wonder how can I modify nginx
configuration to work with C too. Considered a simple c file
(hello.c and compiled as a.out) with printf of

printf(“Content-type: text/html\r\nStatus: 200 OK\r\n\r\n”);
printf(“Hello World”);

how can I display Hello World by browsing the compiled version on
localhost/a.out

Use either:

  1. fcgiwrap http://wiki.nginx.org/Fcgiwrap

  2. thttpd http://wiki.nginx.org/ThttpdCGI

  3. mini-httpd

Nginx doesn’t support CGI.

— appa

#3

On 03/12/11 07:38, etrader wrote:

I am using php in nginx. I wonder how can I modify nginx configuration
to work with C too. Considered a simple c file (hello.c and compiled as
a.out) with printf of

printf(“Content-type: text/html\r\nStatus: 200 OK\r\n\r\n”);
printf(“Hello World”);

how can I display Hello World by browsing the compiled version on
localhost/a.out

The best solution probably depends on what you’re trying to achieve and
your particular circumstances. As well as the solutions already
suggested, one method I’ve used is to build the C program against
libevent, effectively using libevent to build a mini webserver that is
running all the time. nginx can then use that as an upstream proxy.

James

#4

Thanks James!
The idea of building a mini-webserver for C programming is brilliant. I
searched a lot to find a guidance for building very basic web server in
C (obviously I prefer to write the web server in C with native API for C
files); but I was unable to find. All web servers in C are advanced
applications such as nginx. Could you please give me a hint how to start
to write a mini web server in C.

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,219531,219552#msg-219552

#5

Today Dec 3, 2011 at 07:44 etrader wrote:

The idea of building a mini-webserver for C programming is brilliant. I
searched a lot to find a guidance for building very basic web server in
C (obviously I prefer to write the web server in C with native API for C
files); but I was unable to find. All web servers in C are advanced
applications such as nginx. Could you please give me a hint how to start
to write a mini web server in C.

http://www.google.com/search?q=libevent+webserver+in+40+lines


WNGS-RIPE

#6

On 03/12/11 12:44, etrader wrote:

Thanks James!
The idea of building a mini-webserver for C programming is brilliant. I
searched a lot to find a guidance for building very basic web server in
C (obviously I prefer to write the web server in C with native API for C
files); but I was unable to find. All web servers in C are advanced
applications such as nginx. Could you please give me a hint how to start
to write a mini web server in C.

Start here:

http://libevent.org/

libevent has all of the hard stuff done for you. You basically just
need to use it’s API to get hold of all the HTTP request information
and to let it handle the response to the user.

James

#7

On Saturday 03 December 2011 16:44:56 etrader wrote:

Thanks James!
The idea of building a mini-webserver for C programming is brilliant. I
searched a lot to find a guidance for building very basic web server in
C (obviously I prefer to write the web server in C with native API for C
files); but I was unable to find. All web servers in C are advanced
applications such as nginx. Could you please give me a hint how to start
to write a mini web server in C.

Instead of building a web server, you can consider to write a fastcgi
or scgi application.

http://www.fastcgi.com/devkit/doc/fastcgi-prog-guide/ap_guide.htm

wbr, Valentin V. Bartenev

#8

Instead of building a web server, you can consider to write a fastcgi
or scgi application.

http://www.fastcgi.com/devkit/doc/fastcgi-prog-guide/ap_guide.htm

wbr, Valentin V. Bartenev

Another solution would be writing uWSGI plugins:

http://projects.unbit.it/uwsgi/browser/plugins/example/example_plugin.c

You will end with items passed by nginx to your app easy accessible and
you will not need to learn how to program a server/daemon
and focus on your app.

In addition to this your app will be accessibile via uwsgi/http/fastcgi
and can use the whole uwsgi api and operational modes
(preforked/threaded/async/green).

The doc on writing plugins is not available, but you can subscribe to
the
uWSGI list and ask for help (or read the sources of the other plugins).


Roberto De Ioris
http://unbit.it

#9

You could look at the lua module. I hardly ever write C for nginx now.

http://openresty.org/ has some examples.

Also, I’m a big fan of using HTTP over things like fastcgi,scgi, etc.
Much easier to test, debug, etc. But that’s just my opinion.

–Brian