Commit 9ed0aa3d authored by Davis King's avatar Davis King

Added Steven Van Ingelgem's patch to add the body of data posted back to the

server into the incoming data object given to the server_http::on_request() handler.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402946
parent 3a9f6b4b
...@@ -66,11 +66,12 @@ namespace dlib ...@@ -66,11 +66,12 @@ namespace dlib
struct incoming_things struct incoming_things
{ {
incoming_things() : foreign_port(0), local_port(0), content_length(0) {} incoming_things() : foreign_port(0), local_port(0) {}
std::string path; std::string path;
std::string request_type; std::string request_type;
std::string content_type; std::string content_type;
std::string body;
key_value_map queries; key_value_map queries;
key_value_map cookies; key_value_map cookies;
...@@ -80,9 +81,6 @@ namespace dlib ...@@ -80,9 +81,6 @@ namespace dlib
unsigned short foreign_port; unsigned short foreign_port;
std::string local_ip; std::string local_ip;
unsigned short local_port; unsigned short local_port;
unsigned long content_length;
}; };
struct outgoing_things struct outgoing_things
...@@ -175,6 +173,36 @@ namespace dlib ...@@ -175,6 +173,36 @@ namespace dlib
return result; return result;
} }
void parse_url(std::string word, key_value_map& queries)
/*!
Parses the query string of a URL. word should be the stuff that comes
after the ? in the query URL.
!*/
{
std::string::size_type pos;
for (pos = 0; pos < word.size(); ++pos)
{
if (word[pos] == '&')
word[pos] = ' ';
}
std::istringstream sin(word);
sin >> word;
while (sin)
{
pos = word.find_first_of("=");
if (pos != std::string::npos)
{
std::string key = urldecode(word.substr(0,pos));
std::string value = urldecode(word.substr(pos+1));
queries[key] = value;
}
sin >> word;
}
}
void on_connect ( void on_connect (
std::istream& in, std::istream& in,
std::ostream& out, std::ostream& out,
...@@ -206,10 +234,9 @@ namespace dlib ...@@ -206,10 +234,9 @@ namespace dlib
key_value_map& incoming_headers = incoming.headers; key_value_map& incoming_headers = incoming.headers;
key_value_map& cookies = incoming.cookies; key_value_map& cookies = incoming.cookies;
key_value_map& queries = incoming.queries;
std::string& path = incoming.path; std::string& path = incoming.path;
std::string& content_type = incoming.content_type; std::string& content_type = incoming.content_type;
unsigned long& content_length = incoming.content_length; unsigned long content_length = 0;
string line; string line;
getline(in,line); getline(in,line);
...@@ -295,45 +322,26 @@ namespace dlib ...@@ -295,45 +322,26 @@ namespace dlib
getline(in,line); getline(in,line);
} // while (line.size() > 2 ) } // while (line.size() > 2 )
// If there is data being posted back to us then load it into the incoming.body
// string.
if ( content_length > 0 )
{
incoming.body.resize(content_length);
in.read(&incoming.body[0],content_length);
}
// If there is data being posted back to us as a query string then // If there is data being posted back to us as a query string then
// just stick it onto the end of the path so the following code can // pick out the queries using parse_url.
// then just pick it out like we do for GET requests.
if (strings_equal_ignore_case(incoming.request_type, "POST") && if (strings_equal_ignore_case(incoming.request_type, "POST") &&
strings_equal_ignore_case(left_substr(content_type,";"), "application/x-www-form-urlencoded") strings_equal_ignore_case(left_substr(content_type,";"), "application/x-www-form-urlencoded"))
&& content_length > 0)
{ {
line.resize(content_length); parse_url(incoming.body, incoming.queries);
in.read(&line[0],content_length);
path += (path.find('?') == std::string::npos) ? '?' : '&';
path += line;
} }
string::size_type pos = path.find_first_of("?"); string::size_type pos = path.find_first_of("?");
if (pos != string::npos) if (pos != string::npos)
{ {
std::string word = path.substr(pos+1); parse_url(path.substr(pos+1), incoming.queries);
path = path.substr(0,pos);
for (pos = 0; pos < word.size(); ++pos)
{
if (word[pos] == '&')
word[pos] = ' ';
}
istringstream sin(word);
sin >> word;
while (sin)
{
pos = word.find_first_of("=");
if (pos != string::npos)
{
string key = urldecode(word.substr(0,pos));
string value = urldecode(word.substr(pos+1));
queries[key] = value;
}
sin >> word;
}
} }
......
...@@ -95,6 +95,7 @@ namespace dlib ...@@ -95,6 +95,7 @@ namespace dlib
std::string path; std::string path;
std::string request_type; std::string request_type;
std::string content_type; std::string content_type;
std::string body;
key_value_map queries; key_value_map queries;
key_value_map cookies; key_value_map cookies;
...@@ -104,8 +105,6 @@ namespace dlib ...@@ -104,8 +105,6 @@ namespace dlib
unsigned short foreign_port; unsigned short foreign_port;
std::string local_ip; std::string local_ip;
unsigned short local_port; unsigned short local_port;
unsigned long content_length;
}; };
struct outgoing_things struct outgoing_things
...@@ -131,6 +130,9 @@ namespace dlib ...@@ -131,6 +130,9 @@ namespace dlib
- incoming.path == the path being requested by this request - incoming.path == the path being requested by this request
- incoming.request_type == the type of request, GET or POST - incoming.request_type == the type of request, GET or POST
- incoming.content_type == the content type associated with this request - incoming.content_type == the content type associated with this request
- incoming.body == a string that contains the data that was posted back to the
web server by the client (e.g. The string has the length specified by the
Content-Length header).
- incoming.queries == a map that contains all the key/value pairs in the query - incoming.queries == a map that contains all the key/value pairs in the query
string of this request. The key and value strings of the query string will string of this request. The key and value strings of the query string will
have been decoded back into their original form before being sent to this have been decoded back into their original form before being sent to this
...@@ -145,7 +147,6 @@ namespace dlib ...@@ -145,7 +147,6 @@ namespace dlib
- incoming.foreign_port == the foreign port number for this request - incoming.foreign_port == the foreign port number for this request
- incoming.local_ip == the IP of the local interface this request is coming in on - incoming.local_ip == the IP of the local interface this request is coming in on
- incoming.local_port == the local port number this request is coming in on - incoming.local_port == the local port number this request is coming in on
- incoming.content_length == the value from the incoming Content-Length header
- in outgoing: - in outgoing:
- outgoing.cookies.size() == 0 - outgoing.cookies.size() == 0
- outgoing.headers.size() == 0 - outgoing.headers.size() == 0
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment