Re: How to implement CGI in a Web Server

This is in reference to my mail below regarding implementing CGI in a
Web Server.

After some work I found that this way:
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(cmdarray, envp);

of invoking the CGI program will not work on Windows (I am working on
Win XP). Because the command is executed already, and there is no way
I can pass POST data to a java.lang.Process object.

As a workaround, I am (my web server) now writing POST data to a file.
And the CGI script is reading from the file.

Following is the pseudocode in the Web Server:
InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();

//write to file as interface for POST data
FileWriter fw = new FileWriter("postdata");
fw.write(httpbody);
fw.flush();
fw.close();

//execute CGI program
String program = resource.substring(resource.indexOf("/") + 1);
String[] cmdarray = {"C:\\php-5.0.5\\php.exe", "htdocs\\"+program, line};
String[] envp = {"CONTENT_LENGTH="+clength,
"CONTENT_TYPE=application/x-www-form-urlencoded"};
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(cmdarray, envp);

//send output to browser
String header = "HTTP/1.1 200 OK\n";
header += "Content-Type: text/html\n\n";
os.write(header.getBytes());
os.flush();

InputStream pis = process.getInputStream();

while ((n = pis.read()) != -1) {
    char c = (char)n;
    os.write(c);
    os.flush();
}

pis.close();

//delete file "postdata"
File pd = new File("postdata");
pd.delete();

Now in the PHP file, I do following to read POST data:
<?php

  //read post data from file
  $handle = @fopen("postdata", "r");
  $postdata = "";
  if ($handle) {
    while (!feof($handle)) {
      $buffer = fgets($handle, 4096);
      $postdata = $postdata . $buffer;
    }
    fclose($handle);
  }

  //decode post data
  $a = explode('&', $postdata);
  $i = 0;
  while ($i < count($a)) {
    $b = split('=', $a[$i]);
    echo 'Value for parameter ', htmlspecialchars(urldecode($b[0])),
           ' is ', htmlspecialchars(urldecode($b[1])), "<br />\n";
    $i++;
  }

?>

This works. But does this violate CGI 1.1 spec in any way?

Regards,
Mukul

On 5/3/06, Mukul Gandhi <gandhi.mukul@gmail.com> wrote:
> I am trying to write a small HTTP server using Java. I have some doubt
> about how to implement CGI.
>
> This is a portion of Java code that handles HTTP POST method, and
> tries to execute a PHP script. I feel this is the correct way to
> implement CGI (of course there are many more environment variables
> which I must set for CGI, which I'll do later). Later I'll try to
> incorporate CGI programs written in any CGI programming language.
>
> String[] cmdarray = {"C:\\php-5.0.5\\php.exe", "htdocs\\test.php"};
> String[] envp = {"CONTENT_LENGTH="+clength,
> "CONTENT_TYPE=application/x-www-form-urlencoded"};
>
> Runtime runtime = Runtime.getRuntime();
> Process process = runtime.exec(cmdarray, envp);
> InputStream pis = process.getInputStream();
>
> String header = "HTTP/1.1 200 OK\n";
> header += "Content-Type: text/html\n\n";
>
> os.write(header.getBytes());
> os.flush();
>
> int n = 0;
> while ((n = pis.read()) != -1) {
>  char c = (char)n;
>  os.write(c);
>  os.flush();
> }
>
> I am correctly assigning the value to variable clength for
> CONTENT_LENGTH environment variable (taken from request header
> Content-Length). os is the OutputStream attached to the socket which
> is bound to a specific connection from the browser.
>
> The PHP script (test.php) gets correctly called, and the output from
> PHP script correctly gets displayed on the browser. But the problem
> happens when I submit a HTML form to be handled by PHP. How will the
> PHP script read form data from HTTP body in the request? For
> information I must tell that HTML form POST data is accessible in the
> PHP script as $_POST["name"] (which presently incorrectly sets to
> null).
>
> I'll be grateful for any pointers to solve this problem, and also any
> comments on the CGI implementation approach I am using..
>
> Regards,
> Mukul

Received on Sunday, 7 May 2006 17:15:58 UTC