CGI Principle Parsing Series three----CGI how to complete get Web server data

Source: Internet
Author: User

Simulates the process of starting CGI on a server-side httpd process,
Full presentation of Get,post method support
Gcc-g Httpd_all.c-o httpd_all.ums;


#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>

#define CGI_NAME "Get_post.ums"
#define REQUEST_METHOD "Request_method=post"
#define Request_parameter "Myname=huangzhihui"

int main (int argc, char *argv[])
{
int parent[2],child[2];
if (pipe (parent) < 0)
{
printf ("Create pipe fail.\n");
}

if (pipe (child) < 0)
{
Close (parent[0]);
Close (parent[1]);
printf ("Create pipe fail.\n");
}

 pid_t pid;
 if (pid = fork ()) < 0)
 {
  printf ("fork fail.\n");
 }
 else if (pid > 0)
 {
  /* Parent */
  //Close redundant handles, construct a single function read-write channel
  close (Parent[0]);
  close (child[1]);
  
  //simulates transmitting data to CGI
  ssize_t length = strlen (request_parameter);
  if (Write (parent[1], request_parameter, length)! = length)
  {
   printf ("Write error to pipe\n");
  }
  close (parent[1]);
  
  //wait for the CGI child process to write the data completely,
  //the actual situation should be using Select or Epoll listening
   //usleep (+);

  //simulates receiving data    the CGI response;
  char buff[256] = {0};
  length = Read (child[0], buff, sizeof (buff));
  if (length <= 0)
  {
   printf ("read error from pipe\n");
  }
  else
  {
   printf ("pid%d read data=%u\n%s", Getpid () , length, buff);
  }
  close (child[0]);

  if (Waitpid (PID, NULL, 0) < 0)
  {
   printf ("Waitpid error\n");
  }
  exit (0);
 }
 else
 {
  /* Child */
  
  //Close redundant handles, construct a single-function read-write channel
  close (parent[1]);
  close (child[0]);
  
  //The input end of the redirect pipeline to the standard input
  if (parent[0]! = Stdin_fileno)
  {
   if (dup2 (parent[0), Stdin_fileno)! = Stdin_fileno)
   {
     printf ("Dup2 error to stdin");
   }
   close (parent[0]);
  }

Redirect the output of the pipeline to standard output
if (child[1]! = Stdout_fileno)
{
if (Dup2 (child[1], stdout_fileno)! = Stdout_fileno)
{
printf ("Dup2 error to stdout");
}
Close (child[1]);
}

Overwrite process space, set CGI environment variables
Char content_length[128] = {0};
sprintf (Content_length, "content_length=%u", strlen (Request_parameter));
Char *exec_argv[3] = {request_method, content_length};
if (Execve (CGI_NAME,ARGV,EXEC_ARGV) < 0)
{
printf ("Execl error for%s", cgi_name);
}
Exit (0);
}
Exit (0);
}

CGI Principle Parsing Series three----CGI how to complete get Web server data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.