Handling CGI requests on a bare bone HTTP server
Yes, you should call exec. In particular, you probably want to start a shell, which will detect what type of script (like perl, shell, etc.) or cgi binary, and execute it correctly.
Usually the sequence is: create some pipes with pipe, fork to create a new process, dup2 to connect stdin and stdout to pipes, and exec (to start a new program).
You are probably calling the execle exec variant. The last parameter is a set of environment variables for your cgi program. Adjust the name value pairs in the cgi spec based on the incoming request. They will have names like REQUEST_METHOD
and QUERY_STRING
.
Then write the content of the request to cgi stdin. For example, this would be a string of query parameters in the case of POST. Finally, read stdout and retry it back to the browser.
source to share
The role of the HTTP server is to implement the HTTP protocol (essentially a communication protocol sitting on top of TCP / IP)
Support for .pl, .cgi, etc. is the 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 will actually process HTML with embedded Ruby code inside)
You really need to figure out what your goal is.
source to share