@@ -16,13 +16,19 @@ bool is_valid(BasicHTTP::request req, std::string http_ver) {
1616 // if all tests pass, return true
1717 return (
1818 (http_ver == " HTTP/1.0" || http_ver == " HTTP/1.1" )
19- && req.method == " GET" // || POST
19+ && ( req.method == " GET" || req. method == " POST" )
2020 && req.uri .length () > 0
2121 );
2222}
2323
2424BasicHTTP::request BasicHTTP::parse_request (std::string req_str) {
25+ this ->logger ->debug (" Full request: " + req_str);
2526 request req;
27+ if (req_str.length () < 3 ) {
28+ req.valid = false ;
29+ return req;
30+ }
31+
2632 // From http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5
2733 // Request-Line = Method SP Request-URI SP HTTP-Version CRLF
2834 size_t pos = 0 , prev_pos = 0 ;
@@ -43,12 +49,24 @@ BasicHTTP::request BasicHTTP::parse_request(std::string req_str) {
4349 std::string http_ver;
4450 if ((pos = req_str.find (" \r\n " , prev_pos)) != std::string::npos) {
4551 http_ver = req_str.substr (prev_pos, pos-prev_pos);
46- prev_pos = pos + 1 ;
52+ prev_pos = pos + 2 ;
4753 }
4854 this ->logger ->info (" Method: " + req.method + " , HTTP version string: " + http_ver + " , URI: " + req.uri );
4955
5056 // now end with checking if it's actually valid
51- req.valid = is_valid (req, http_ver);
57+ req.valid = is_valid (req, http_ver);
58+
59+ // find and extract cookies, if available
60+ if (req.valid ) {
61+ req.cookies = BasicHTTP::fetch_cookies (req_str);
62+
63+ if (req.method == " GET" ) {
64+ handle_get_method (&req);
65+ }
66+ if (req.method == " POST" ) {
67+ handle_post_method (&req, req_str);
68+ }
69+ }
5270
5371 return req;
5472}
@@ -78,3 +96,61 @@ BasicHTTP::response BasicHTTP::render_headers(int code, DataHandler::resource rs
7896
7997 return resp;
8098}
99+
100+ std::string BasicHTTP::fetch_cookies (std::string req_str) {
101+ size_t pos = 0 , eol = 0 ;
102+
103+ std::string cookies = " " ;
104+ // to extract the POST params we first need to find it and it's length
105+ if ((pos = req_str.find (" Cookie: " )) != std::string::npos) {
106+ if ((eol = req_str.find (" \r\n " , pos)) != std::string::npos) {
107+ cookies = req_str.substr (pos + 8 , eol - pos - 8 );
108+ this ->logger ->debug (" Cookies: " + cookies);
109+ }
110+ }
111+
112+ return cookies;
113+ }
114+
115+ void BasicHTTP::handle_get_method (BasicHTTP::request * req) {
116+ size_t pos = 0 ;
117+
118+ req->data .type = req->method ;
119+ if ((pos = req->uri .find (" ?" , 0 )) != std::string::npos) {
120+ req->data .size = req->uri .length () - pos + 1 ;
121+ req->data .data = (char *) malloc ((req->data .size +1 ) * sizeof (char ));
122+ std::string params = req->uri .substr (pos+1 );
123+ for (int i = 0 ; i < req->data .size ; i++)
124+ req->data .data [i] = params[i];
125+ req->uri = req->uri .substr (0 , pos);
126+ }
127+ else {
128+ req->data .size = 0 ;
129+ req->data .data = (char *) malloc ((req->data .size +1 ) * sizeof (char ));
130+ req->data .data [0 ] = ' \0 ' ;
131+ }
132+ }
133+
134+ void BasicHTTP::handle_post_method (BasicHTTP::request * req, std::string req_str) {
135+ size_t pos = 0 , prev_pos = 0 ;
136+
137+ // to extract the POST params we first need to find it and it's length
138+ if ((pos = req_str.find (" Content-Length:" , prev_pos)) != std::string::npos) {
139+ size_t eol = 0 ;
140+ int content_length = 0 ;
141+ if ((eol = req_str.find (" \r\n " , pos)) != std::string::npos) {
142+ content_length = std::stoi (req_str.substr (pos + 16 , eol - pos + 16 ));
143+ this ->logger ->debug (" Content-Length: " + std::to_string (content_length));
144+ }
145+ // don't read anything, if content data was 0, or wasn't defined at all
146+ if (content_length > 0 && (pos = req_str.find (" \r\n\r\n " , eol)) != std::string::npos) {
147+ req->data .type = req->method ;
148+ req->data .size = content_length;
149+ req->data .data = (char *) malloc ((req->data .size + 1 )* sizeof (char ));
150+ std::string data = req_str.substr (pos+4 );
151+ for (int i = 0 ; i < req->data .size ; i++)
152+ req->data .data [i] = data[i];
153+ req->data .data [req->data .size ] = ' \0 ' ;
154+ }
155+ }
156+ }
0 commit comments