static inline int pcs_http_get_content_length_from_header(const char *header, int size) { int res = -1; char *val = NULL; const char *p = header, *key = "Content-Length", *end = NULL, *tmp; int i = 14;//strlen(key) = 14; while (*p) { if (*p == key[0] && pcs_utils_streq(p, key, i)) { tmp = p + i; PCS_SKIP_SPACE(tmp); if (*tmp != ':') continue; tmp++; PCS_SKIP_SPACE(tmp); end = tmp; while (*end && *end >= '0' && *end <= '9') end++; if (end > tmp) { val = (char *)pcs_malloc(end - tmp + 1); memcpy(val, tmp, end - tmp); val[end - tmp] = '\0'; res = atoi(val); pcs_free(val); break; } } p++; } return res; }
/*从html中解析出类似于 FileUtils.bdstoken="****" 的值。此例中,key传入"FileUtils.bdstoken",将返回"****"值*/ static char *pcs_get_value_by_key(const char *html, const char *key) { char *val = NULL; const char *p = html, *end = NULL, *tmp; int i = strlen(key);// \f\n\r\t\v while (*p) { if (*p == key[0] && pcs_utils_streq(p, key, i)) { tmp = p + i; PCS_SKIP_SPACE(tmp); if (*tmp != '=') continue; tmp++; PCS_SKIP_SPACE(tmp); if (*tmp != '"') continue; tmp++; end = tmp; while (*end && *end != '"') end++; if (*end == '"') { val = (char *)pcs_malloc(end - tmp + 1); memcpy(val, tmp, end - tmp); val[end - tmp] = '\0'; return val; } } p++; } return val; }
static inline char *pcs_http_get_charset_from_header(const char *header, int size) { char *res = NULL; const char *p = header, *key = "charset", *end = NULL, *tmp; int i = 7;//strlen(key) = 7; while (*p) { if (*p == key[0] && pcs_utils_streq(p, key, i)) { tmp = p + i; if (*tmp != '=') continue; tmp++; end = tmp; while (*end && PCS_IS_TOKEN_CHAR(*end)) end++; if (end > tmp) { res = (char *)pcs_malloc(end - tmp + 1); memcpy(res, tmp, end - tmp); res[end - tmp] = '\0'; break; } } p++; } return res; }
/*从URL地址的QueryString中解析出类似于 &token=abc_ef-g&a= 的值。此例中,key传入"&token",将返回abc_ef-g*/ static char *pcs_get_embed_query_token_by_key(const char *html, const char *key) { char *val = NULL; const char *p = html, *end = NULL, *tmp; int i = strlen(key);// \f\n\r\t\v while (*p) { if (*p == key[0] && pcs_utils_streq(p, key, i)) { tmp = p + i; if (*tmp != '=') continue; tmp++; end = tmp; while (*end && PCS_IS_TOKEN_CHAR(*end)) end++; if (end > tmp) { val = (char *)pcs_malloc(end - tmp + 1); memcpy(val, tmp, end - tmp); val[end - tmp] = '\0'; return val; } } p++; } return val; }