📄 mpitems.cxx
字号:
//--------------------------------------------------------------------------------------- const char * MpStartLine::getReasonPhrase(MpResponseCode code) { unsigned i; for(i = 0; mp_ReasonText[i].code; i++) { if(mp_ReasonText[i].code == code) return mp_ReasonText[i].text; } return 0; } //--------------------------------------------------------------------------------------- const char * MpStartLine::getMethodString(MpMsgType type) { unsigned i; for(i = 0; mp_MethodString[i].type != mtreqUNKNOWN; i++) { if(mp_MethodString[i].type == type) return mp_MethodString[i].str; } return 0; } //--------------------------------------------------------------------------------------- MpMsgType MpStartLine::getMethod(const std::string &method) { unsigned i; for(i = 0; mp_MethodString[i].type != mtreqUNKNOWN; i++) { if(mp_MethodString[i].str == method) break; } return mp_MethodString[i].type; } //--------------------------------------------------------------------------------------- bool MpStartLine::empty() const { if(atoi(mp_Type.c_str())) return false; return mp_Url.empty(); } //--------------------------------------------------------------------------------------- std::mstring MpStartLine::encode(bool) const { std::mstring ret; if(empty()) return ret; checkSyntax(); int code = atoi(mp_Type.c_str()); if(code == 0) //There is a request { MpMsgType type = getMethod(mp_Type); if(type != mtreqUNKNOWN) { ret = mp_Type; ret += ' '; ret += mp_Url.encode(); ret += ' '; ret += mp_Proto; ret += '/'; ret += mp_Version; } } else //There is a response { const char * ts = getReasonPhrase(MpResponseCode(code)); if(ts) { ret = mp_Proto; ret += '/'; ret += mp_Version; ret += ' '; ret += mp_Type; ret += ' '; if(mp_ReasonPhrase.empty()) ret += ts; else ret += mp_ReasonPhrase; } } return ret; } //--------------------------------------------------------------------------------------- void MpStartLine::decode(const std::mstring & str, bool) { erase(); std::mstring p1, p2, p3, ts; std::string::size_type start = 0; //Divide into 3 pieces start = str.token(&p1, start, " ", QuoteSymb, QPairSymb, std::mstring::sep_multiple); start = str.token(&p2, start, " ", QuoteSymb, QPairSymb, std::mstring::sep_multiple); start = str.token(&p3, start, "", QuoteSymb, QPairSymb, std::mstring::sep_multiple); p3.atrim(); MpMsgType type = getMethod(p1); if(type != mtreqUNKNOWN) //There is a request { start = p3.token(&mp_Proto, 0, "/", ""); start = p3.token(&mp_Version, start, "/", ""); mp_Type = p1; mp_ReasonPhrase = ""; mp_Url.decode(p2, false); } else //Try to decode a response { start = p1.token(&mp_Proto, 0, "/", ""); start = p1.token(&mp_Version, start, "/", ""); mp_Type = p2; mp_ReasonPhrase = p3; } checkSyntax(); } //--------------------------------------------------------------------------------------- void MpStartLine::checkSyntax() const { if(mp_Type.empty()) { throw MpSyntaxError("MpStartLine: No request method or response code"); } ValAssistant::check(mpiVia, mpnProto, mp_Proto); ValAssistant::check(mpiVia, mpnVersion, mp_Version); int code = atoi(mp_Type.c_str()); if(code == 0) { MpMsgType type = getMethod(mp_Type); if(type == mtreqUNKNOWN) { throw MpSyntaxError(std::string("MpStartLine: Invalid request method: ") + mp_Type); } } else { if(getReasonPhrase(MpResponseCode(code)) == 0) { throw MpSyntaxError(std::string("MpStartLine: Invalid response code: ") + mp_Type); } } mp_Url.checkSyntax(); } //--------------------------------------------------------------------------------------- void MpStartLine::erase() { mp_Type = ""; mp_ReasonPhrase = ""; mp_Url.erase(); } //--------------------------------------------------------------------------------------- MpParseItem & MpStartLine::operator [] (const char * name) { if(strcmp(name, mpiUrl) == 0) return mp_Url; throw MpAccessError(std::mstring("MpStartLine: Illegal nested name: ") + name); } //--------------------------------------------------------------------------------------- std::mstring & MpStartLine::val(const char * name) { if(strcmp(name, mpnMsgType) == 0) return mp_Type; if(strcmp(name, mpnMsgReasonPhrase) == 0) return mp_ReasonPhrase; if(strcmp(name, mpnProto) == 0) return mp_Proto; if(strcmp(name, mpnVersion) == 0) return mp_Version; throw MpAccessError(std::mstring("MpStartLine: Illegal Parameter: ") + name); } //--------------------------------------------------------------------------------------- void MpStartLine::get(unsigned i, std::mstring *name, std::mstring *value) const { switch(i) { case 0: if(name) *name = mpnMsgType; if(value) *value = mp_Type; return; case 1: if(name) *name = mpnMsgReasonPhrase; if(value) *value = mp_ReasonPhrase; return; case 2: if(name) *name = mpnProto; if(value) *value = mp_Proto; return; case 3: if(name) *name = mpnVersion; if(value) *value = mp_Version; return; } throw MpAccessError("MpStartLine: Out of index"); } //--------------------------------------------------------------------------------------- MpParseItem & MpStartLine::item(unsigned i, std::mstring *name) { if(i == 0) { if(name) *name = mpiUrl; return mp_Url; } throw MpAccessError("MpStartLine: Out of item index"); } //======================================================================================= //MpValParm Implementation //--------------------------------------------------------------------------------------- std::mstring MpValParm::encode(bool chk_syntax_flag) const { std::mstring ret; if(!empty()) { if(chk_syntax_flag) checkSyntax(); ret += mp_Value; if(mp_Separator) { if(!mp_SubVal.empty()) { ret += mp_Separator; ret += mp_SubVal; } } ret += mp_Parm.encode(false); } return ret; } //--------------------------------------------------------------------------------------- void MpValParm::decode(const std::mstring & str, bool chk_syntax_flag) { erase(); std::mstring parm; std::mstring val; std::string::size_type start = str.next_token(0, ";", QuoteSymb, QPairSymb, std::mstring::sep_single); if(start != std::string::npos) { parm.assign(str, start + 1, std::string::npos); val.assign(str, 0, start); parm.atrim(); val.atrim(); } else { val = str; } if(mp_Separator) { std::mstring sep; sep = mp_Separator; start = toktrim(val, &mp_Value, 0, sep); toktrim(val, &mp_SubVal, start, sep); } else { mp_Value = val; } mp_Parm.decode(parm, false); if(chk_syntax_flag) checkSyntax(); } //--------------------------------------------------------------------------------------- void MpValParm::checkSyntax() const { mp_Parm.checkSyntax(); } //--------------------------------------------------------------------------------------- MpParseItem & MpValParm::operator [] (const char * name) { if(strcmp(name, mpiParm) == 0) return mp_Parm; throw MpAccessError(std::mstring("MpValParm: Illegal nested name: ") + name); } //--------------------------------------------------------------------------------------- std::mstring & MpValParm::val(const char * name) { if(strcmp(name, mpnValue) == 0) return mp_Value; if(strcmp(name, mpnSubVal) == 0) return mp_SubVal; throw MpAccessError(std::mstring("MpValParm: Illegal Parameter: ") + name); } //--------------------------------------------------------------------------------------- void MpValParm::get(unsigned i, std::mstring *name, std::mstring *value) const { if(i == 0) { if(name) *name = mpnValue; if(value) *value = mp_Value; return; } if(i == 1) { if(name) *name = mpnSubVal; if(value) *value = mp_SubVal; return; } throw MpAccessError("MpValParm: Out of index"); } //--------------------------------------------------------------------------------------- MpParseItem & MpValParm::item(unsigned i, std::mstring *name) { if(i == 0) { if(name) *name = mpiParm; return mp_Parm; } throw MpAccessError("MpValParm: Out of item index"); } //======================================================================================= //MpValue Implementation //--------------------------------------------------------------------------------------- MpValue::MpValue(char sep) : mp_Separator(sep), mp_NVal(0) { memset(mp_Name, 0, sizeof(mp_Name)); memset(mp_Quoted, 0, sizeof(mp_Quoted)); set_val_name(0, mpnValue); } //--------------------------------------------------------------------------------------- MpHeader * MpValue::clone() const { MpHeader * header = new MpValue; header->setHeaderProperties(*this); return header; } //--------------------------------------------------------------------------------------- void MpValue::set_val_name(unsigned index, const char * name, bool quoted) { if(index < 15) { if(index + 1 > mp_NVal) mp_NVal = index + 1; mp_Name[index] = name; mp_Quoted[index] = quoted; } } //--------------------------------------------------------------------------------------- std::mstring MpValue::encode(bool chk_syntax_flag) const { if(chk_syntax_flag) checkSyntax(); unsigned i; if(mp_Separator == 0 || mp_NVal == 1) return mp_Value[0]; std::mstring ts; std::mstring ret; for(i = 0; i < mp_NVal; i++) { ts = mp_Value[i]; if(mp_Quoted[i]) { ts.ins_pair(QuoteSymb, QPairSymb); ts.quote(QuoteSymb, QuoteSymb); } ret += ts; if(mp_Value[i + 1].empty()) break; if(i != mp_NVal - 1) { ret += mp_Separator; } } return ret; } //--------------------------------------------------------------------------------------- void MpValue::decode(const std::mstring & str, bool chk_syntax_flag)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -