X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fhttp%2Fheader.cpp;h=a112e91b6246155139743bc063873aae5e2c41bd;hb=cc2e643d6ec3025db3fb6f0a4c1f77af05cf1026;hp=02c82ca08001c10f898102afd1595a980ee3c8b8;hpb=451c140747bef1829d55d20a33dd3543b9ab8c98;p=libs%2Fnet.git diff --git a/source/http/header.cpp b/source/http/header.cpp index 02c82ca..a112e91 100644 --- a/source/http/header.cpp +++ b/source/http/header.cpp @@ -1,6 +1,6 @@ +#include "header.h" #include #include -#include "header.h" #include "message.h" using namespace std; @@ -8,37 +8,74 @@ using namespace std; namespace Msp { namespace Http { -Header::Header(const Message &msg, const string &n): +Header::Header(const Message &msg, const string &n, Style s): name(n), + style(s), raw_value(msg.get_header(name)) { parse(); } -Header::Header(const string &n, const string &rv): +Header::Header(const string &n, const string &rv, Style s): name(n), + style(s), raw_value(rv) { parse(); } +Header::Style Header::get_default_style(const string &name) +{ + if(!strcasecmp(name, "content-disposition")) + return VALUE_WITH_ATTRIBUTES; + else if(!strcasecmp(name, "content-type")) + return VALUE_WITH_ATTRIBUTES; + else if(!strcasecmp(name, "cookie")) + return KEY_VALUE_LIST; + else if(!strcasecmp(name, "set-cookie")) + return VALUE_WITH_ATTRIBUTES; + else + return SINGLE_VALUE; +} + void Header::parse() { - string::const_iterator i = raw_value.begin(); - while(i!=raw_value.end()) + if(style==DEFAULT) + style = get_default_style(name); + + if(style==SINGLE_VALUE) { Value value; + value.value = strip(raw_value); + values.push_back(value); + return; + } - string::const_iterator start = i; - for(; (i!=raw_value.end() && *i!=';' && *i!=','); ++i) ; - value.value = strip(string(start, i)); - if(value.value.empty()) - throw invalid_argument("Header::parse"); + char value_sep = (style==VALUE_WITH_ATTRIBUTES ? 0 : ','); - while(i!=raw_value.end() && *i!=',') + auto i = raw_value.cbegin(); + while(i!=raw_value.cend()) + { + Value value; + + auto start = i; + if(style==KEY_VALUE_LIST) + value.value = name; + else + { + for(; (i!=raw_value.end() && *i!=';' && *i!=value_sep); ++i) ; + value.value = strip(string(start, i)); + if(value.value.empty()) + throw invalid_argument("Header::parse"); + } + + while(i!=raw_value.end() && (*i!=',' || style==KEY_VALUE_LIST) && style!=LIST) { - start = ++i; - for(; (i!=raw_value.end() && *i!=';' && *i!=',' && *i!='='); ++i) ; + if(*i==';' || *i==',') + ++i; + + start = i; + for(; (i!=raw_value.end() && *i!=';' && *i!=value_sep && *i!='='); ++i) ; string pname = strip(string(start, i)); if(pname.empty()) throw invalid_argument("Header::parse"); @@ -47,7 +84,7 @@ void Header::parse() if(i!=raw_value.end() && *i=='=') { for(++i; (i!=raw_value.end() && isspace(*i)); ++i) ; - if(i==raw_value.end() || *i==';' || *i==',') + if(i==raw_value.end() || *i==';' || *i==value_sep) throw invalid_argument("Header::parse"); if(*i=='"') @@ -59,14 +96,14 @@ void Header::parse() pvalue = string(start, i); - for(++i; (i!=raw_value.end() && *i!=';' && *i!=','); ++i) + for(++i; (i!=raw_value.end() && *i!=';' && *i!=value_sep); ++i) if(!isspace(*i)) throw invalid_argument("Header::parse"); } else { start = i; - for(; (i!=raw_value.end() && *i!=';' && *i!=','); ++i) ; + for(; (i!=raw_value.end() && *i!=';' && *i!=value_sep); ++i) ; pvalue = strip(string(start, i)); } } @@ -75,6 +112,9 @@ void Header::parse() } values.push_back(value); + + if(i!=raw_value.end() && (*i==';' || *i==',')) + ++i; } }