static http_cookie_t *cookie_parse(const char *value, const char *host, const char *path) { http_cookie_t *cookie = calloc( 1, sizeof( http_cookie_t ) ); if ( unlikely( !cookie ) ) return NULL; char *content = cookie_get_content(value); if ( !content ) { cookie_destroy( cookie ); return NULL; } const char *eq = strchr( content, '=' ); if ( eq ) { cookie->psz_name = strndup( content, eq-content ); cookie->psz_value = strdup( eq + 1 ); } else { cookie->psz_name = strdup( content ); cookie->psz_value = NULL; } cookie->psz_domain = cookie_get_domain(value); if ( !cookie->psz_domain || strlen(cookie->psz_domain) == 0 ) { free(cookie->psz_domain); cookie->psz_domain = strdup(host); cookie->b_host_only = true; } else cookie->b_host_only = false; cookie->psz_path = cookie_get_attribute_value(value, "path" ); if ( !cookie->psz_path || strlen(cookie->psz_path) == 0 ) { free(cookie->psz_path); cookie->psz_path = cookie_default_path(path); } cookie->b_secure = cookie_has_attribute(value, "secure" ); FREENULL( content ); if ( !cookie->psz_domain || !cookie->psz_path || !cookie->psz_name ) { cookie_destroy( cookie ); return NULL; } return cookie; }
int main(void) { cookie c; size_t i, niter = 10000; const char* name = "name"; const char* value = "value"; const char* domain = "DOMAIN"; const char* path = "PATH"; const char* comment = "THIS IS A COMMENT"; int max_age = 0; int secure = 1; int version = 1; for(i = 0; i < niter; i++) { if( (c = cookie_new()) == NULL) return 77; if(!cookie_set_name(c, name)) return 77; if(!cookie_set_value(c, value)) return 77; if(!cookie_set_domain(c, domain)) return 77; if(!cookie_set_path(c, path)) return 77; if(!cookie_set_comment(c, comment)) return 77; if(!cookie_set_max_age(c, max_age)) return 77; cookie_set_secure(c, secure); cookie_set_version(c, version); if(strcmp(cookie_get_name(c), name) != 0) return 77; if(strcmp(cookie_get_value(c), value) != 0) return 77; if(strcmp(cookie_get_domain(c), domain) != 0) return 77; if(strcmp(cookie_get_path(c), path) != 0) return 77; if(strcmp(cookie_get_comment(c), comment) != 0) return 77; if(cookie_get_max_age(c) != max_age) return 77; if(cookie_get_secure(c) != secure) return 77; if(cookie_get_version(c) != version) return 77; cookie_free(c); } return 0; }