示例#1
0
	void HttpRespond::setCookie(String* name, String* value, const int64_t max_age){
		HttpCookie* cookie =SafeNew<HttpCookie>();
		cookie->setName(name);
		cookie->setValue(value);
		cookie->setMaxAge(max_age);
		m_cookie_tb->set(name, cookie);
	}
示例#2
0
http_header& http_header::add_cookie(const char* name, const char* value,
	const char* domain /* = NULL */, const char* path /* = NULL */,
	time_t expires /* = 0 */)
{
	if (name == NULL || *name == 0 || value == NULL)
		return *this;

	HttpCookie* cookie = NEW HttpCookie(name, value);
	if (domain && *domain)
		cookie->setDomain(domain);
	if (path && *path)
		cookie->setPath(path);
	if (expires > 0)
		cookie->setExpires(expires);
	cookies_.push_back(cookie);
	return *this;
}
示例#3
0
	/** parse **/
	HttpCookie* HttpCookie::Parse(const char* str){
		if(!str){
			return 0;
		}
		// separate tokens
		Array* ls =String::New(str)->split(";");
		if(!ls){
			ERROR("invalid cookie `%s` syntax", str);
			return 0;
		}
		const int64_t count =ls->size();
		for(int64_t i=0; i<count; ++i){
			String* token =static_cast< String* >(ls->get(i));
			ls->set(i, token->trim());
		}

		// parse
		HttpCookie* cookie =SafeNew<HttpCookie>();

		// name=value
		if(String* token =static_cast< String* >(ls->get(0))){
			const int64_t idx =token->indexOf("=");
			if(idx < 0){
				ERROR("invalid cookie `%s` syntax", str);
				return 0;
			}
			cookie->setName(token->subString(0, idx));
			cookie->setValue(token->subString(idx+1, -1));
		}

		// others
		for(int64_t i=1; i<count; ++i){
			String* token =static_cast< String* >(ls->get(i));
			const char* token_str =token->c_str();
			if(0 == strcasecmp(token_str, "secure")){
				cookie->setSecure(true);
			}
			else if(0==strcasecmp(token_str, "HttpOnly") || 0==strcasecmp(token_str, "http-only")){
				cookie->setHttpOnly(true);
			}
			else{
				const int64_t idx =token->indexOf("=");
				if(idx < 0){
					ERROR("invalid cookie `%s` syntax", str);
					return 0;
				}
				String* k =token->subString(0, idx);
				String* v =token->subString(idx+1, -1);
				const char* k_str =k->c_str();
				if(0 == strcasecmp(k_str, "max-age")){
					int64_t max_age =0;
					if(!FromString<int64_t>(v, max_age)){
						ERROR("invalid cookie `%s` syntax", str);
						return 0;
					}
					cookie->setMaxAge(max_age);
				}
				else if(0 == strcasecmp(k_str, "path")){
					cookie->setPath(v);
				}
				else if(0 == strcasecmp(k_str, "domain")){
					cookie->setDomain(v);
				}
				else if(0 == strcasecmp(k_str, "expires")){
					cookie->setExpireTime(v);
				}
			}
		}
		return cookie;
	}