static STRING_HANDLE construct_http_data()

in src/uhttp.c [989:1044]


static STRING_HANDLE construct_http_data(HTTP_CLIENT_REQUEST_TYPE request_type, const char* relative_path, STRING_HANDLE http_line)
{
    STRING_HANDLE result;

    const char* method = (request_type == HTTP_CLIENT_REQUEST_GET) ? "GET"
        : (request_type == HTTP_CLIENT_REQUEST_OPTIONS) ? "OPTIONS"
        : (request_type == HTTP_CLIENT_REQUEST_POST) ? "POST"
        : (request_type == HTTP_CLIENT_REQUEST_PUT) ? "PUT"
        : (request_type == HTTP_CLIENT_REQUEST_DELETE) ? "DELETE"
        : (request_type == HTTP_CLIENT_REQUEST_PATCH) ? "PATCH"
        : NULL;
    /* Codes_SRS_UHTTP_07_014: [If the request_type is not a valid request http_client_execute_request shall return HTTP_CLIENT_ERROR] */
    if (method == NULL)
    {
        LogError("Invalid request method %s specified", method);
        result = NULL;
    }
    else
    {
        size_t buffLen = safe_add_size_t(strlen(HTTP_REQUEST_LINE_FMT), strlen(method));
        buffLen = safe_add_size_t(buffLen, strlen(relative_path));

        size_t malloc_size = safe_add_size_t(buffLen, 1);
        char* request;
        if (malloc_size == SIZE_MAX ||
            (request = malloc(malloc_size)) == NULL)
        {
            result = NULL;
            LogError("Failure allocating Request data, size:%zu", malloc_size);
        }
        else
        {
            if (snprintf(request, malloc_size, HTTP_REQUEST_LINE_FMT, method, relative_path) <= 0)
            {
                result = NULL;
                LogError("Failure writing request buffer");
            }
            else
            {
                result = STRING_construct(request);
                if (result == NULL)
                {
                    LogError("Failure creating buffer object");
                }
                else if (STRING_concat_with_STRING(result, http_line) != 0)
                {
                    STRING_delete(result);
                    result = NULL;
                    LogError("Failure writing request buffers");
                }
            }
            free(request);
        }
    }
    return result;
}