11.12

run server

(cd ./site/content/chapter11/code; make && ./tiny.12 5000)

visit http://localhost:5000/post-home.html and submit

POST method pass param by message-body behind Headers part. When we input 9 and 10 and submit, socket pass content

POST /cgi-bin/post-adder HTTP/1.1
Host: localhost:5000
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded
Content-Length: 16
Referer: http://localhost:5000/post-home.html
Cookie: _ga=GA1.1.1286836072.1494744693
Connection: keep-alive
Upgrade-Insecure-Requests: 1
(CRLF here, message-body below)
first=9&second=10

Content-Length marks the length of message-body. use readnb to fetch post param.

--- tiny.origin.c	2021-02-25 07:26:33.302592754 +0000
+++ tiny.12.c	2021-02-25 07:26:33.302592754 +0000
@@ -5,7 +5,7 @@
 #include "csapp.h"
 
 void doit(int fd);
-void read_requesthdrs(rio_t *rp);
+int read_requesthdrs(rio_t *rp, char *method);
 int parse_uri(char *uri, char *filename, char *cgiargs);
 void serve_static(int fd, char *filename, int filesize);
 void get_filetype(char *filename, char *filetype);
@@ -55,12 +55,14 @@
     return;
   printf("%s", buf);
   sscanf(buf, "%s %s %s", method, uri, version);       //line:netp:doit:parserequest
-  if (strcasecmp(method, "GET")) {                     //line:netp:doit:beginrequesterr
+  if (!(strcasecmp(method, "GET") == 0 || strcasecmp(method, "POST") == 0)) {
     clienterror(fd, method, "501", "Not Implemented",
         "Tiny does not implement this method");
     return;
   }                                                    //line:netp:doit:endrequesterr
-  read_requesthdrs(&rio);                              //line:netp:doit:readrequesthdrs
+  int param_len = read_requesthdrs(&rio, method);
+
+  Rio_readnb(&rio, buf, param_len);
 
   /* Parse URI from GET request */
   is_static = parse_uri(uri, filename, cgiargs);       //line:netp:doit:staticcheck
@@ -84,24 +86,29 @@
           "Tiny couldn't run the CGI program");
       return;
     }
-    serve_dynamic(fd, filename, cgiargs);            //line:netp:doit:servedynamic
+    if (strcasecmp(method, "GET") == 0)
+      serve_dynamic(fd, filename, cgiargs);
+    else
+      serve_dynamic(fd, filename, buf);
   }
 }
 
 /*
  * read_requesthdrs - read HTTP request headers
  */
-void read_requesthdrs(rio_t *rp)
+int read_requesthdrs(rio_t *rp, char *method)
 {
   char buf[MAXLINE];
+  int len = 0;
 
-  Rio_readlineb(rp, buf, MAXLINE);
-  printf("%s", buf);
-  while(strcmp(buf, "\r\n")) {          //line:netp:readhdrs:checkterm
+  do {
     Rio_readlineb(rp, buf, MAXLINE);
     printf("%s", buf);
-  }
-  return;
+    if (strcasecmp(method, "POST") == 0 && strncasecmp(buf, "Content-Length:", 15) == 0)
+      sscanf(buf, "Content-Length: %d", &len);
+  } while(strcmp(buf, "\r\n"));
+
+  return len;
 }
 /*
  * parse_uri - parse URI into filename and CGI args

post-adder code

/*
 * post-adder.c - a minimal CGI program that adds two numbers together
 */
#include "../csapp.h"

int main(void) {
  char *buf, *p;
  char arg1[MAXLINE], arg2[MAXLINE], content[MAXLINE];
  int n1=0, n2=0;

  /* Extract the two arguments */
  if ((buf = getenv("QUERY_STRING")) != NULL) {
    p = strchr(buf, '&');
    *p = '\0';
    sscanf(buf, "first=%d", &n1);
    sscanf(p+1, "second=%d", &n2);
  }

  /* Make the response body */
  sprintf(content, "Welcome to add.com: ");
  sprintf(content, "%sTHE Internet addition portal.\r\n<p>", content);
  sprintf(content, "%sThe answer is: %d + %d = %d\r\n<p>",
      content, n1, n2, n1 + n2);
  sprintf(content, "%sThanks for visiting!\r\n", content);

  /* Generate the HTTP response */
  printf("Connection: close\r\n");
  printf("Content-length: %d\r\n", (int)strlen(content));
  printf("Content-type: text/html\r\n\r\n");
  printf("%s", content);
  fflush(stdout);

  exit(0);
}
comments powered by Disqus