X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fhttp%2Fheader.cpp;fp=source%2Fhttp%2Fheader.cpp;h=3027d17d887eddaba34ff03fc90ade533c4b55cd;hb=6af74d460a0ec4f53bb5cff328ee34d05131be9a;hp=02c82ca08001c10f898102afd1595a980ee3c8b8;hpb=a84075639100079175b0d99187b34f3ce319f9a7;p=libs%2Fnet.git diff --git a/source/http/header.cpp b/source/http/header.cpp index 02c82ca..3027d17 100644 --- a/source/http/header.cpp +++ b/source/http/header.cpp @@ -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() { + if(style==DEFAULT) + style = get_default_style(name); + + if(style==SINGLE_VALUE) + { + Value value; + value.value = strip(raw_value); + values.push_back(value); + return; + } + + char value_sep = (style==VALUE_WITH_ATTRIBUTES ? 0 : ','); + string::const_iterator i = raw_value.begin(); while(i!=raw_value.end()) { Value value; 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"); + 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!=',') + 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; } }