in src/httpheaders.c [70:172]
static HTTP_HEADERS_RESULT headers_ReplaceHeaderNameValuePair(HTTP_HEADERS_HANDLE handle, const char* name, const char* value, bool replace)
{
HTTP_HEADERS_RESULT result;
/*Codes_SRS_HTTP_HEADERS_99_014:[ The function shall return when the handle is not valid or when name parameter is NULL or when value parameter is NULL.]*/
if (
(handle == NULL) ||
(name == NULL) ||
(value == NULL)
)
{
result = HTTP_HEADERS_INVALID_ARG;
LogError("invalid arg (NULL) , result= %" PRI_MU_ENUM "", MU_ENUM_VALUE(HTTP_HEADERS_RESULT, result));
}
else
{
/*Codes_SRS_HTTP_HEADERS_99_036:[ If name contains the characters outside character codes 33 to 126 then the return value shall be HTTP_HEADERS_INVALID_ARG]*/
/*Codes_SRS_HTTP_HEADERS_99_031:[ If name contains the character ":" then the return value shall be HTTP_HEADERS_INVALID_ARG.]*/
size_t i;
size_t nameLen = strlen(name);
for (i = 0; i < nameLen; i++)
{
if ((name[i] < 33) || (126 < name[i]) || (name[i] == ':'))
{
break;
}
}
if (i < nameLen)
{
result = HTTP_HEADERS_INVALID_ARG;
LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTP_HEADERS_RESULT, result));
}
else
{
HTTP_HEADERS_HANDLE_DATA* handleData = (HTTP_HEADERS_HANDLE_DATA*)handle;
const char* existingValue = Map_GetValueFromKey(handleData->headers, name);
/*eat up the whitespaces from value, as per RFC 2616, chapter 4.2 "The field value MAY be preceded by any amount of LWS, though a single SP is preferred."*/
/*Codes_SRS_HTTP_HEADERS_02_002: [The LWS from the beginning of the value shall not be stored.] */
while ((value[0] == ' ') || (value[0] == '\t') || (value[0] == '\r') || (value[0] == '\n'))
{
value++;
}
if (!replace && (existingValue != NULL))
{
char* newValue;
size_t existingValueLen = strlen(existingValue);
size_t valueLen = strlen(value);
size_t malloc_size = safe_add_size_t(existingValueLen, /*COMMA_AND_SPACE_LENGTH*/ 2);
malloc_size = safe_add_size_t(malloc_size, valueLen);
malloc_size = safe_add_size_t(malloc_size, /*EOL*/ 1);
malloc_size = safe_multiply_size_t(malloc_size, sizeof(char));
if (malloc_size == SIZE_MAX ||
(newValue = (char*)malloc(malloc_size)) == NULL)
{
/*Codes_SRS_HTTP_HEADERS_99_015:[ The function shall return HTTP_HEADERS_ALLOC_FAILED when an internal request to allocate memory fails.]*/
result = HTTP_HEADERS_ALLOC_FAILED;
LogError("failed to malloc, size= %zu, result= %" PRI_MU_ENUM "", malloc_size, MU_ENUM_VALUE(HTTP_HEADERS_RESULT, result));
}
else
{
char* runNewValue;
/*Codes_SRS_HTTP_HEADERS_99_017:[ If the name already exists in the collection of headers, the function shall concatenate the new value after the existing value, separated by a comma and a space as in: old-value+", "+new-value.]*/
(void)memcpy(newValue, existingValue, existingValueLen);
runNewValue = newValue + existingValueLen;
(*runNewValue++) = ',';
(*runNewValue++) = ' ';
(void)memcpy(runNewValue, value, valueLen + /*EOL*/ 1);
/*Codes_SRS_HTTP_HEADERS_99_016:[ The function shall store the name:value pair in such a way that when later retrieved by a call to GetHeader it will return a string that shall strcmp equal to the name+": "+value.]*/
if (Map_AddOrUpdate(handleData->headers, name, newValue) != MAP_OK)
{
/*Codes_SRS_HTTP_HEADERS_99_015:[ The function shall return HTTP_HEADERS_ALLOC_FAILED when an internal request to allocate memory fails.]*/
result = HTTP_HEADERS_ERROR;
LogError("failed to Map_AddOrUpdate, result= %" PRI_MU_ENUM "", MU_ENUM_VALUE(HTTP_HEADERS_RESULT, result));
}
else
{
/*Codes_SRS_HTTP_HEADERS_99_013:[ The function shall return HTTP_HEADERS_OK when execution is successful.]*/
result = HTTP_HEADERS_OK;
}
free(newValue);
}
}
else
{
/*Codes_SRS_HTTP_HEADERS_99_016:[ The function shall store the name:value pair in such a way that when later retrieved by a call to GetHeader it will return a string that shall strcmp equal to the name+": "+value.]*/
if (Map_AddOrUpdate(handleData->headers, name, value) != MAP_OK)
{
/*Codes_SRS_HTTP_HEADERS_99_015:[ The function shall return HTTP_HEADERS_ALLOC_FAILED when an internal request to allocate memory fails.]*/
result = HTTP_HEADERS_ALLOC_FAILED;
LogError("failed to Map_AddOrUpdate, result= %" PRI_MU_ENUM "", MU_ENUM_VALUE(HTTP_HEADERS_RESULT, result));
}
else
{
result = HTTP_HEADERS_OK;
}
}
}
}
return result;
}