Test failed at read a header after refactoring

Hi I can’t understand why I keep getting this error while testing

[tester::#FS3] Sent bytes: "GET /user-agent HTTP/1.1\r\nHost: localhost:4221\r\nUser-Agent: pear/banana\r\n\r\n"
[your_program] Client connected
[your_program] Received request:
[your_program] GET /user-agent HTTP/1.1
[your_program] Host: localhost:4221
[your_program] User-Agent: pear/banana
[your_program] 
[your_program] 
[tester::#FS3] Failed to read response: 
[tester::#FS3] Received: "" (no content received)
[tester::#FS3]            ^ error
[tester::#FS3] Error: Expected: HTTP-version, Received: ""
[tester::#FS3] Test failed

when I test by myself through curl I get this

curl -v --header "User-Agent: banana/apple-blueberry" http://localhost:4221/user-agent
* processing: http://localhost:4221/user-agent
*   Trying [::1]:4221...
* connect to ::1 port 4221 failed: Connection refused
*   Trying 127.0.0.1:4221...
* Connected to localhost (127.0.0.1) port 4221
> GET /user-agent HTTP/1.1
> Host: localhost:4221
> Accept: */*
> User-Agent: banana/apple-blueberry
> 
< HTTP/1.1 200 OK
< Content-Type: text/plain
< Content-Length: 22
< 
* Connection #0 to host localhost left intact
banana/apple-blueberry

firefox is also able to receive both http version status code content type length and body

here is the formatting of the response

if (strcmp(request.path, "/user-agent") == 0)
        {
			printf("request: %s\n", request.user_agent);
            char format[] = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: %d\r\n\r\n%s";
            char response[1024];
            int response_length = snprintf(response, sizeof(response), format, strlen(request.user_agent), request.user_agent);
			printf("Response: %s\n", response);
            send_response(conn, response);
        }

and the two function that pull out the information form the header

typedef struct 
{
    char *method;
    char *path;
    char *user_agent;
    char *header;
    char *body;
} HttpRequest;

char *extract_header_value(const char *header, const char *header_name)
{
    const char *header_start = strcasestr(header, header_name);
    if (header_start)
    {
        const char *value_start = header_start + strlen(header_name);
        // Skip any whitespace or colon after the header name
        while (*value_start == ' ' || *value_start == ':')
        {
            value_start++;
        }
        const char *value_end = strstr(value_start, "\r\n");
        if (value_end)
        {
            size_t value_length = value_end - value_start;
            char *value = (char *)malloc(value_length + 1);
            if (value)
            {
                strncpy(value, value_start, value_length);
                value[value_length] = '\0';
                return value;
            }
            else
            {
                printf("Memory allocation failed\n");
            }
        }
        else
        {
            size_t value_length = strlen(value_start);
            char *value = (char *)malloc(value_length + 1);
            if (value)
            {
                strncpy(value, value_start, value_length);
                value[value_length] = '\0';
                return value;
            }
            else
            {
                printf("Memory allocation failed\n");
            }
        }
    }
    else
    {
        printf("header_start not found for %s\n", header_name);
    }
    return NULL;
}

HttpRequest split_request(const char *request)
{
    HttpRequest result;
    const char *separator = "\r\n\r\n";
    char *separator_position;

    result.method = NULL;
    result.path = NULL;
    result.header = NULL;
    result.body = NULL;
    result.user_agent = NULL;

    const char *method_end = strchr(request, ' ');
    if (method_end != NULL)
    {
        size_t method_length = method_end - request;
        result.method = (char *)malloc(method_length + 1);
        if (result.method != NULL)
        {
            strncpy(result.method, request, method_length);
            result.method[method_length] = '\0';
        }
    }

    if (method_end != NULL)
    {
        const char *path_start = method_end + 1;
        const char *path_end = strchr(path_start, ' ');
        if (path_end != NULL)
        {
            size_t path_length = path_end - path_start;
            result.path = (char *)malloc(path_length + 1);
            if (result.path != NULL)
            {
                strncpy(result.path, path_start, path_length);
                result.path[path_length] = '\0';
            }
        }
    }

    separator_position = strstr(request, separator);

    if (separator_position != NULL)
    {
        size_t header_length = separator_position - request;
        size_t body_length = strlen(separator_position + 4);

        result.header = (char *)malloc(header_length + 1);
        result.body = (char *)malloc(body_length + 1);

        if (result.header != NULL && result.body != NULL)
        {
            strncpy(result.header, request, header_length);
            result.header[header_length] = '\0';

            strcpy(result.body, separator_position + 4);

            result.user_agent = extract_header_value(result.header, "User-Agent:");
            if (!result.user_agent)
            {
                result.user_agent = strdup("NULL");
            }
        }
    }

    return result;
}

I hope some can help thanks in advanced

Closing this thread due to inactivity. If you still need assistance, feel free to start a new discussion!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.