diff --git a/plugin/protocol/http/src/http_half.cpp b/plugin/protocol/http/src/http_half.cpp index 29048da..6a05f1a 100644 --- a/plugin/protocol/http/src/http_half.cpp +++ b/plugin/protocol/http/src/http_half.cpp @@ -517,41 +517,34 @@ int hf_ops_field_write(struct tfe_http_half * half, const struct http_field_name struct http_half_private * hf_private = to_hf_private(half); assert(hf_private->major == 0 || hf_private->major == 1); - struct http_header_private * __header_iter = NULL; - struct http_header_private * __header_found = NULL; - - TAILQ_FOREACH(__header_iter, &hf_private->header_list, next) + /* Add a k-v in the tail of the header list */ + if (value != NULL) { - if (http_field_name_compare(__header_iter->field, field) != 0) continue; - __header_found = __header_iter; - break; + struct http_header_private * new_header = ALLOC(struct http_header_private, 1); + new_header->field = http_field_name_duplicate(field); + new_header->value = tfe_strdup(value); + TAILQ_INSERT_TAIL(&hf_private->header_list, new_header, next); } - - /* Update the value */ - if (__header_found != NULL && value != NULL) - { - free(__header_found->value); - __header_found->value = tfe_strdup(value); - } - /* Delete the key and value */ - else if (__header_found != NULL && value == NULL) - { - TAILQ_REMOVE(&hf_private->header_list, __header_found, next); - free(__header_found->value); - free(__header_found); - } - /* Insert a new header k-v in the tail of the header list */ - else if (__header_found == NULL && value != NULL) - { - struct http_header_private * __header = ALLOC(struct http_header_private, 1); - __header->field = http_field_name_duplicate(field); - __header->value = tfe_strdup(value); - TAILQ_INSERT_TAIL(&hf_private->header_list, __header, next); - } - /* Nothing found, and delete nothing */ + /* Delete a value */ else { - return -1; + struct http_header_private * header_iter = NULL; + struct http_header_private * header_titer = NULL; + bool delete_success = false; + + TAILQ_FOREACH_SAFE(header_iter, &hf_private->header_list, next, header_titer) + { + if (http_field_name_compare(header_iter->field, field) != 0) + continue; + + TAILQ_REMOVE(&hf_private->header_list, header_iter, next); + http_field_name_destory(header_iter->field); + free(header_iter->value); + free(header_iter); + delete_success = true; + } + + return delete_success ? 0 : -ENOENT; } return 0;