I am writing a basic http server in C. Handling a simple static .html file is easy but I have no idea how to handle dynamic .pl .cgi file extensions.
I know I will have to use exec() but how is my question?
I am writing a basic http server in C. Handling a simple static .html file is easy but I have no idea how to handle dynamic .pl .cgi file extensions.
I know I will have to use exec() but how is my question?
Take a look at the CGI Spec. Specifically sections 4 "Invoking the Script", and section 6 "Data Input to the CGI Script". You will need to set environment variables for the cgi script to read ( QUERY_STRING, SCRIPT_NAME, etc). This outta get you going.
The role of HTTP server is to implement HTTP protocol (essentially communication protocol sitting on top of TCP/IP)
Supporting .pl, .cgi, etc is a role of the Application Server. There are many good examples. For example in Ruby on Rails you can use web servers (Apache/nginx) and run ruby interpreters behind those (which in fact will process HTML with embedded Ruby code inside)
You really need to figure out what is your goal.
Yes, you should call exec. In particular, you'll probably want to run a shell, which will figure out what kind of script (e.g. perl, shell, etc.) or binary the cgi program is, and execute it properly.
The usually sequence is: create some pipes with pipe, fork to spawn a new process, dup2 to hook up stdin and stdout to the pipes, and exec (to run the new program).
You'll probably be calling the execle variant of exec. The last parameter is a set of environment variables for your cgi program. Set up the name value pairs in the cgi spec based on the incoming request. These will have names like REQUEST_METHOD
and QUERY_STRING
.
Next, write the request content to the cgi's standard input. This will be the request parameter string in the case of POST, for example. Finally, read the stdout and echo it back to the browser.