#include #include "protocol.h" #include #include namespace browser_bridge { namespace { class Parser { public: explicit Parser(const std::string& s):s_(s) {} bool Object(Message& fields) { Skip(); if (!Take('{')) return false; Skip(); if (Take('}')) { Skip(); return at_==s_.size(); } do { std::string key,value; if (!String(key)) return false; Skip(); if (!Take(':')) return false; Skip(); if (!String(value) || fields.size()>=16 || !fields.emplace(key,value).second) return false; Skip(); if (Take('}')) { Skip(); return at_==s_.size(); } if (!Take(',')) return false; Skip(); } while (at_=s_.size() || s_[at_]!=c) return false; ++at_; return true; } bool Hex(unsigned& value) { value=0; for (int i=0;i<4;++i) { if (at_>=s_.size()) return false; char c=s_[at_++]; unsigned d; if (c>='0' && c<='9') d=c-'0'; else if (c>='a' && c<='f') d=c-'a'+10; else if (c>='A' && c<='F') d=c-'A'+10; else return false; value=value*16+d; } return true; } bool String(std::string& value) { if (!Take('"')) return false; while (at_(value.size()),nullptr,0)>0; if (c<32) return false; if (c!='\\') { value.push_back(c); continue; } if (at_>=s_.size()) return false; c=s_[at_++]; if (c=='"' || c=='\\' || c=='/') value.push_back(c); else if (c=='b') value+='\b'; else if (c=='f') value+='\f'; else if (c=='n') value+='\n'; else if (c=='r') value+='\r'; else if (c=='t') value+='\t'; else if (c=='u') { unsigned first,second; if (!Hex(first)) return false; wchar_t wide[2]={static_cast(first),0}; int count=1; if (first>=0xD800 && first<=0xDBFF) { if (!Take('\\') || !Take('u') || !Hex(second) || second<0xDC00 || second>0xDFFF) return false; wide[1]=static_cast(second); count=2; } else if (first>=0xDC00 && first<=0xDFFF) return false; char bytes[8]{}; int size=WideCharToMultiByte(CP_UTF8,WC_ERR_INVALID_CHARS,wide,count,bytes,8,nullptr,nullptr); if (!size) return false; value.append(bytes,size); } else return false; } return false; } const std::string& s_; size_t at_=0; }; std::string Quote(const std::string& text) { constexpr char hex[]="0123456789abcdef"; std::string result="\""; for (unsigned char c:text) { if (c=='"' || c=='\\') { result+='\\'; result+=c; } else if (c<32) { result+="\\u00"; result+=hex[c>>4]; result+=hex[c&15]; } else result+=c; } return result+'"'; } bool HttpUrl(const std::string& text) { size_t start=text.rfind("http://",0)==0?7:text.rfind("https://",0)==0?8:0; if (!start || text.size()>16384 || text.find_first_of("\r\n\t ")!=std::string::npos) return false; auto end=text.find_first_of("/?#",start); auto authority=text.substr(start,end==std::string::npos?end:end-start); return !authority.empty() && authority.find('@')==std::string::npos; } } bool Parse(const std::string& json,Message& fields,std::string& error) { fields.clear(); if (json.empty() || json.size()>kMaxMessage || !Parser(json).Object(fields)) { error="消息必须是有效的 UTF-8 字符串字段对象"; return false; } return true; } std::string Serialize(const Message& fields) { std::string json="{"; for (const auto& [key,value]:fields) { if (json.size()>1) json+=','; json+=Quote(key)+':'+Quote(value); } return json+'}'; } std::string Get(const Message& fields,const std::string& key) { auto it=fields.find(key); return it==fields.end()?"":it->second; } bool Validate(const Message& fields,std::string& error) { static const std::set allowed={"action","request_id","url","referrer","user_agent","filename","method","confirmation"}; for (const auto& [key,value]:fields) { if (!allowed.count(key) || value.find('\0')!=std::string::npos) { error="不支持的字段"; return false; } for (unsigned char c:value) if (c<32 || c==127) { error="字段不能含控制字符"; return false; } } const auto action=Get(fields,"action"),id=Get(fields,"request_id"); if (id.empty() || id.size()>80) { error="请求编号无效"; return false; } for (unsigned char c:id) if (!std::isalnum(c) && c!='-') { error="请求编号无效"; return false; } if ((action=="ping" || action=="query" || action=="cancel") && fields.size()<=2) return true; if (action!="add" || (!Get(fields,"confirmation").empty() && Get(fields,"confirmation")!="required") || Get(fields,"method")!="GET" || !HttpUrl(Get(fields,"url")) || (!Get(fields,"referrer").empty() && !HttpUrl(Get(fields,"referrer"))) || Get(fields,"user_agent").size()>2048 || Get(fields,"filename").size()>1024) { error="仅支持 HTTP/HTTPS GET 下载地址及合法上下文"; return false; } return true; } bool ReadFrame(std::istream& input,std::string& json) { unsigned char b[4]; if (!input.read(reinterpret_cast(b),4)) return false; std::uint32_t n=b[0]|(static_cast(b[1])<<8)|(static_cast(b[2])<<16)|(static_cast(b[3])<<24); if (!n || n>kMaxMessage) return false; json.resize(n); return static_cast(input.read(json.data(),n)); } bool WriteFrame(std::ostream& output,const std::string& json) { if (json.empty() || json.size()>kMaxMessage) return false; auto n=static_cast(json.size()); unsigned char b[4]={static_cast(n),static_cast(n>>8),static_cast(n>>16),static_cast(n>>24)}; output.write(reinterpret_cast(b),4); output.write(json.data(),json.size()); output.flush(); return static_cast(output); } Message Reply(const std::string& status,const std::string& message,const std::string& id,const std::string& task) { return {{"status",status},{"message",message},{"request_id",id},{"task_id",task}}; } }