[Zrouter-src] ZRouter.org: push to zconf++ libzhttp/request.hpp libzhttp/reque...

zrouter-src at zrouter.org zrouter-src at zrouter.org
Wed Dec 7 23:05:34 UTC 2011


details:   /rev/94a62d295395
changeset: 23:94a62d295395
user:      "Nicolai Petri <nicolai at petri.dk>"
date:      Wed Dec 07 23:57:17 2011 +0100
description:
Make very, very, very rough parsing of request bodies for PUT and POST requests.
 .. http layer should be rewritten at some point.

diffstat:

 libzhttp/request.hpp        |   8 ++++-
 libzhttp/request_parser.cpp |  59 ++++++++++++++++++++++++++++++++++++--------
 libzhttp/request_parser.hpp |   4 ++-
 3 files changed, 57 insertions(+), 14 deletions(-)

diffs (168 lines):

diff -r d80c02ff4e88 -r 94a62d295395 libzhttp/request.hpp
--- a/libzhttp/request.hpp	Sat Dec 03 21:41:36 2011 +0100
+++ b/libzhttp/request.hpp	Wed Dec 07 23:57:17 2011 +0100
@@ -12,9 +12,10 @@
 #define HTTP_REQUEST_HPP
 
 #include <string>
-#include <vector>
+#include <map>
 #include "header.hpp"
 
+
 namespace http {
 namespace server {
 
@@ -25,7 +26,10 @@
   std::string uri;
   int http_version_major;
   int http_version_minor;
-  std::vector<header> headers;
+	bool header_completed;
+  std::map<std::string, std::string> headers;
+	header cur_header;	
+	std::string body;
 };
 
 } // namespace server
diff -r d80c02ff4e88 -r 94a62d295395 libzhttp/request_parser.cpp
--- a/libzhttp/request_parser.cpp	Sat Dec 03 21:41:36 2011 +0100
+++ b/libzhttp/request_parser.cpp	Wed Dec 07 23:57:17 2011 +0100
@@ -10,6 +10,8 @@
 
 #include "request_parser.hpp"
 #include "request.hpp"
+#include <iostream>
+#include <boost/lexical_cast.hpp>
 
 namespace http {
 namespace server {
@@ -200,7 +202,7 @@
       state_ = expecting_newline_3;
       return boost::indeterminate;
     }
-    else if (!req.headers.empty() && (input == ' ' || input == '\t'))
+    else if (req.cur_header.name.size() > 0 && (input == ' ' || input == '\t'))
     {
       state_ = header_lws;
       return boost::indeterminate;
@@ -211,8 +213,8 @@
     }
     else
     {
-      req.headers.push_back(header());
-      req.headers.back().name.push_back(input);
+      req.cur_header = header();
+			req.cur_header.name.push_back(input);
       state_ = header_name;
       return boost::indeterminate;
     }
@@ -233,7 +235,7 @@
     else
     {
       state_ = header_value;
-      req.headers.back().value.push_back(input);
+			req.cur_header.value.push_back(input);
       return boost::indeterminate;
     }
   case header_name:
@@ -248,7 +250,7 @@
     }
     else
     {
-      req.headers.back().name.push_back(input);
+      req.cur_header.name.push_back(input);
       return boost::indeterminate;
     }
   case space_before_header_value:
@@ -264,6 +266,8 @@
   case header_value:
     if (input == '\r')
     {
+		//	std::cout << "Saving header : " << req.cur_header.name << std::endl;
+			req.headers[req.cur_header.name] = req.cur_header.value;
       state_ = expecting_newline_2;
       return boost::indeterminate;
     }
@@ -273,7 +277,7 @@
     }
     else
     {
-      req.headers.back().value.push_back(input);
+      req.cur_header.value.push_back(input);
       return boost::indeterminate;
     }
   case expecting_newline_2:
@@ -281,13 +285,46 @@
     {
       state_ = header_line_start;
       return boost::indeterminate;
-    }
-    else
-    {
-      return false;
+    } else if (req.method == "POST" || req.method == "PUT") {
+			state_ = expecting_body;
+      return boost::indeterminate;
+    } else {
+			req.header_completed = true;
+			std::cout << "REQ COMPLETED1" << std::endl;
+			return true;
     }
   case expecting_newline_3:
-    return (input == '\n');
+		std::cout << "body start HTTP/" << req.http_version_major << "." << req.http_version_minor << " |" << req.method << "|"  << req.uri << "|" << req.headers.size() << "|" << std::endl;
+    if (input == '\n') {
+			req.header_completed = true;
+			if (req.method == "POST" || req.method == "PUT") {
+				state_ = expecting_body;
+				return boost::indeterminate;
+			} else {
+				std::cout << "REQ COMPLETED2" << std::endl;
+				return true;
+			}
+		} else {
+			return false;
+		}
+	case expecting_body:
+		req.body += input;
+		// http 1.1 with content length
+		if (req.http_version_major == 1 && req.http_version_minor >= 1 && req.headers.find("Content-Length") != req.headers.end()) {
+			int expected_size = boost::lexical_cast<int>(req.headers["Content-Length"]);
+			//std::cout << " size " << expected_size << " vs " << req.body.size() << std::endl;
+			if (expected_size == req.body.size()) {
+				// Completed!!!
+				std::cout << "REQ COMPLETED3" << std::endl;
+				return true;
+			}
+
+		} else {	
+			std::cout << "3body |" << req.body << "|" << req.body.size() << " vs " << req.headers["Content-Length"] << std::endl;
+		}
+		
+		return boost::indeterminate;
+  //  return (input == '\n');
   default:
     return false;
   }
diff -r d80c02ff4e88 -r 94a62d295395 libzhttp/request_parser.hpp
--- a/libzhttp/request_parser.hpp	Sat Dec 03 21:41:36 2011 +0100
+++ b/libzhttp/request_parser.hpp	Wed Dec 07 23:57:17 2011 +0100
@@ -3,6 +3,7 @@
 // ~~~~~~~~~~~~~~~~~~
 //
 // Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+// Copyright (c) 2011 Nicolai Petri <nicolai (at) petri (dot) dk>
 //
 // Distributed under the Boost Software License, Version 1.0. (See accompanying
 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
@@ -86,7 +87,8 @@
     space_before_header_value,
     header_value,
     expecting_newline_2,
-    expecting_newline_3
+    expecting_newline_3,
+    expecting_body
   } state_;
 };
 


More information about the Zrouter-src mailing list