]> git.tdb.fi Git - libs/net.git/blobdiff - source/http/message.cpp
Add a dynamic receiver class for more flexible packet handling
[libs/net.git] / source / http / message.cpp
index 45392048e3c188cfbab45f17e600748cfe229da7..a6ab4653de9dec58c7c186ea5ecd5c6138545102 100644 (file)
@@ -1,20 +1,14 @@
+#include "message.h"
 #include <cstdlib>
 #include <msp/core/maputils.h>
 #include <msp/strings/format.h>
 #include <msp/strings/utils.h>
-#include "message.h"
 
 using namespace std;
 
 namespace Msp {
 namespace Http {
 
-Message::Message():
-       http_version(0x11),
-       chunk_length(0),
-       complete(false)
-{ }
-
 void Message::set_header(const string &hdr, const string &val)
 {
        headers[normalize_header_name(hdr)] = val;
@@ -35,7 +29,7 @@ void Message::add_content(const string &d)
        content += d;
        if(headers.count("Content-Type")==0)
                set_header("Content-Type", "text/plain");
-       set_header("Content-Length", lexical_cast(content.size()));
+       set_header("Content-Length", lexical_cast<string>(content.size()));
 }
 
 void Message::set_user_data(const Variant &d)
@@ -48,11 +42,11 @@ unsigned Message::parse_content(const string &d)
        if(complete)
                return 0;
 
-       HeaderMap::const_iterator i = headers.find("Content-Length");
+       auto i = headers.find("Content-Length");
        if(i!=headers.end())
        {
-               unsigned needed = lexical_cast<unsigned>(i->second)-content.size();
-               unsigned len = min(needed, d.size());
+               string::size_type needed = lexical_cast<string::size_type>(i->second)-content.size();
+               string::size_type len = min(needed, d.size());
                
                content.append(d, 0, len);
 
@@ -65,12 +59,12 @@ unsigned Message::parse_content(const string &d)
        i = headers.find("Transfer-Encoding");
        if(i!=headers.end() && strcasecmp(i->second, "chunked")==0)
        {
-               unsigned pos = 0;
+               string::size_type pos = 0;
                while(!complete && pos<d.size())
                {
                        if(chunk_length==0)
                        {
-                               unsigned lf = d.find('\n', pos);
+                               string::size_type lf = d.find('\n', pos);
                                if(lf==string::npos)
                                        return pos;
                                chunk_length = lexical_cast<unsigned>(strip(d.substr(pos, lf-pos)), "x");
@@ -80,7 +74,7 @@ unsigned Message::parse_content(const string &d)
                        }
                        else
                        {
-                               unsigned len = min(chunk_length, d.size()-pos);
+                               string::size_type len = min(chunk_length, d.size()-pos);
                                content.append(d, pos, len);
                                chunk_length -= len;
                                if((pos = d.find('\n', pos+len))!=string::npos)
@@ -97,16 +91,16 @@ unsigned Message::parse_content(const string &d)
 
 unsigned Message::parse_headers(const string &d)
 {
-       unsigned start = 0;
+       string::size_type start = 0;
        while(1)
        {
-               unsigned lf = d.find('\n', start);
+               string::size_type lf = d.find('\n', start);
                if(lf==string::npos)
                        throw invalid_argument("Message::parse_headers");
                if(lf==start || (d[start]=='\r' && lf==start+1))
                        return lf+1;
 
-               unsigned colon = d.find(':', start);
+               string::size_type colon = d.find(':', start);
                if(colon>lf)
                        throw invalid_argument("Message::parse_headers");
 
@@ -120,9 +114,9 @@ string Message::str_common() const
 {
        string result;
 
-       for(HeaderMap::const_iterator i=headers.begin(); i!=headers.end(); ++i)
-               if(i->first[0]!='-')
-                       result += format("%s: %s\r\n", i->first, i->second);
+       for(auto &kvp: headers)
+               if(kvp.first[0]!='-')
+                       result += format("%s: %s\r\n", kvp.first, kvp.second);
        result += "\r\n";
        result += content;
 
@@ -133,17 +127,17 @@ string Message::normalize_header_name(const string &hdr) const
 {
        string result = hdr;
        bool upper = true;
-       for(string::iterator i=result.begin(); i!=result.end(); ++i)
+       for(char &c: result)
        {
-               if(upper)
+               if(c=='-')
+                       upper = true;
+               else if(upper)
                {
-                       *i = toupper(*i);
+                       c = toupper(static_cast<unsigned char>(c));
                        upper = false;
                }
-               else if(*i=='-')
-                       upper = true;
                else
-                       *i = tolower(*i);
+                       c = tolower(static_cast<unsigned char>(c));
        }
        return result;
 }