Handling CGI requests on a bare bone HTTP server

I am writing a basic http server in C. Handling a simple static .html file is simple, but I have no idea how to handle dynamic .pl.cgi extensions.

I know I will have to use exec (), but what is my question?

+2


source to share


3 answers


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.

+2


source


Take a look at the CGI Spec . In particular, sections 4 "Calling Script" and section 6 "Entering data into CGI Script". You will need to set environment variables for reading the cgi script (QUERY_STRING, SCRIPT_NAME, etc.). It will distract you.



+2


source


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.

+2


source







All Articles