| 991 | } |
| 992 | |
| 993 | bool |
| 994 | CookieHelper::cookieModifyHelper(const char *cookies, const size_t cookies_len, std::string &updated_cookies, |
| 995 | const CookieHelper::CookieOp cookie_op, const std::string &cookie_key, |
| 996 | const std::string &cookie_value) |
| 997 | { |
| 998 | if (0 == cookie_key.size()) { |
| 999 | Dbg(pi_dbg_ctl, "CookieHelper::cookieModifyHelper, empty cookie_key"); |
| 1000 | return false; |
| 1001 | } |
| 1002 | |
| 1003 | for (size_t idx = 0; idx < cookies_len;) { |
| 1004 | // advance any leading spaces |
| 1005 | for (; idx < cookies_len && std::isspace(cookies[idx]); idx++) { |
| 1006 | ; |
| 1007 | } |
| 1008 | if (0 == strncmp(cookies + idx, cookie_key.c_str(), cookie_key.size())) { |
| 1009 | size_t key_start_idx = idx; |
| 1010 | // advance to past the name and any subsequent spaces |
| 1011 | for (idx += cookie_key.size(); idx < cookies_len && std::isspace(cookies[idx]); idx++) { |
| 1012 | ; |
| 1013 | } |
| 1014 | if (idx < cookies_len && cookies[idx++] == '=') { |
| 1015 | // cookie_key is found, then we don't need to add it. |
| 1016 | if (CookieHelper::COOKIE_OP_ADD == cookie_op) { |
| 1017 | return false; |
| 1018 | } |
| 1019 | for (; idx < cookies_len && std::isspace(cookies[idx]); idx++) { |
| 1020 | ; |
| 1021 | } |
| 1022 | size_t value_start_idx = idx; |
| 1023 | for (; idx < cookies_len && cookies[idx] != ';'; idx++) { |
| 1024 | ; |
| 1025 | } |
| 1026 | // If we have not reached the end and there is a space after the |
| 1027 | // semi-colon, advance one char |
| 1028 | if (idx + 1 < cookies_len && std::isspace(cookies[idx + 1])) { |
| 1029 | idx++; |
| 1030 | } |
| 1031 | // cookie value is found |
| 1032 | size_t value_end_idx = idx; |
| 1033 | if (CookieHelper::COOKIE_OP_SET == cookie_op) { |
| 1034 | updated_cookies.append(cookies, value_start_idx); |
| 1035 | updated_cookies.append(cookie_value); |
| 1036 | updated_cookies.append(cookies + value_end_idx, cookies_len - value_end_idx); |
| 1037 | return true; |
| 1038 | } |
| 1039 | |
| 1040 | if (CookieHelper::COOKIE_OP_DEL == cookie_op) { |
| 1041 | // +1 to skip the semi-colon after the cookie_value |
| 1042 | updated_cookies.append(cookies, key_start_idx); |
| 1043 | if (value_end_idx < cookies_len) { |
| 1044 | updated_cookies.append(cookies + value_end_idx + 1, cookies_len - value_end_idx - 1); |
| 1045 | } |
| 1046 | // if the cookie to delete is the last pair, |
| 1047 | // the semi-colon before this pair needs to be deleted |
| 1048 | // this handles the case "c = b; key=value", the expected result is "c = b" |
| 1049 | size_t last_semi_colon = updated_cookies.find_last_of(';'); |
| 1050 | if (last_semi_colon != std::string::npos) { |