Fixes #35 for both client and server source
This commit is contained in:
parent
3341badb41
commit
3c4c378655
2 changed files with 42 additions and 6 deletions
|
|
@ -3,6 +3,8 @@
|
|||
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/utility/string_ref.hpp>
|
||||
#include <boost/algorithm/string/predicate.hpp>
|
||||
#include <boost/functional/hash.hpp>
|
||||
|
||||
#include <unordered_map>
|
||||
#include <map>
|
||||
|
|
@ -15,12 +17,27 @@ namespace SimpleWeb {
|
|||
class Response {
|
||||
friend class ClientBase<socket_type>;
|
||||
|
||||
class iequal_to {
|
||||
public:
|
||||
bool operator()(const std::string &key1, const std::string &key2) const {
|
||||
return boost::algorithm::iequals(key1, key2);
|
||||
}
|
||||
};
|
||||
class ihash {
|
||||
public:
|
||||
size_t operator()(const std::string &key) const {
|
||||
std::size_t seed=0;
|
||||
for(auto &c: key)
|
||||
boost::hash_combine(seed, std::tolower(c));
|
||||
return seed;
|
||||
}
|
||||
};
|
||||
public:
|
||||
std::string http_version, status_code;
|
||||
|
||||
std::istream content;
|
||||
|
||||
std::unordered_map<std::string, std::string> header;
|
||||
std::unordered_multimap<std::string, std::string, ihash, iequal_to> header;
|
||||
|
||||
private:
|
||||
boost::asio::streambuf content_buffer;
|
||||
|
|
@ -161,14 +178,15 @@ namespace SimpleWeb {
|
|||
|
||||
parse_response_header(response, response->content);
|
||||
|
||||
if(response->header.count("Content-Length")>0) {
|
||||
auto content_length=stoull(response->header["Content-Length"]);
|
||||
auto header_it=response->header.find("Content-Length");
|
||||
if(header_it!=response->header.end()) {
|
||||
auto content_length=stoull(header_it->second);
|
||||
if(content_length>num_additional_bytes) {
|
||||
boost::asio::read(*socket, response->content_buffer,
|
||||
boost::asio::transfer_exactly(content_length-num_additional_bytes));
|
||||
}
|
||||
}
|
||||
else if(response->header.count("Transfer-Encoding")>0 && response->header["Transfer-Encoding"]=="chunked") {
|
||||
else if((header_it=response->header.find("Transfer-Encoding"))!=response->header.end() && header_it->second=="chunked") {
|
||||
boost::asio::streambuf streambuf;
|
||||
std::ostream content(&streambuf);
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@
|
|||
#include <boost/asio.hpp>
|
||||
#include <boost/asio/spawn.hpp>
|
||||
#include <boost/regex.hpp>
|
||||
#include <boost/algorithm/string/predicate.hpp>
|
||||
#include <boost/functional/hash.hpp>
|
||||
|
||||
#include <unordered_map>
|
||||
#include <thread>
|
||||
|
|
@ -58,12 +60,28 @@ namespace SimpleWeb {
|
|||
|
||||
class Request {
|
||||
friend class ServerBase<socket_type>;
|
||||
|
||||
class iequal_to {
|
||||
public:
|
||||
bool operator()(const std::string &key1, const std::string &key2) const {
|
||||
return boost::algorithm::iequals(key1, key2);
|
||||
}
|
||||
};
|
||||
class ihash {
|
||||
public:
|
||||
size_t operator()(const std::string &key) const {
|
||||
std::size_t seed=0;
|
||||
for(auto &c: key)
|
||||
boost::hash_combine(seed, std::tolower(c));
|
||||
return seed;
|
||||
}
|
||||
};
|
||||
public:
|
||||
std::string method, path, http_version;
|
||||
|
||||
Content content;
|
||||
|
||||
std::unordered_multimap<std::string, std::string> header;
|
||||
std::unordered_multimap<std::string, std::string, ihash, iequal_to> header;
|
||||
|
||||
boost::smatch path_match;
|
||||
|
||||
|
|
@ -236,7 +254,7 @@ namespace SimpleWeb {
|
|||
parse_request(request, request->content);
|
||||
|
||||
//If content, read that as well
|
||||
const auto it=request->header.find("Content-Length");
|
||||
auto it=request->header.find("Content-Length");
|
||||
if(it!=request->header.end()) {
|
||||
//Set timeout on the following boost::asio::async-read or write function
|
||||
std::shared_ptr<boost::asio::deadline_timer> timer;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue