"Initials" by "Florian Körner", licensed under "CC0 1.0". / Remix of the original. - Created with dicebear.comInitialsFlorian Körnerhttps://github.com/dicebear/dicebearC_

How do I dynamically allocate a string such that it can contain the response header?

I am reading data through a socket connected to a proxy, which in turn connects to a website of a choice, based on the IP and port I've assigned. After the successful connection, I am supposed to start sending headers to the server I've successfully connected to:

    ...
    char *request_header = "HEAD / HTTP/1.0\r\n";

    int response_size = 512;
    char *response_header = malloc(response_size * sizeof(char));

    write(proxy_fd, request_header, strlen(request_header));
    read(proxy_fd, response_header, response_size);

    printf("Response header:\n%s\n", response_header);
    free(response_header);
    ...

Now, creating a statically-allocated string array is problematic, because the response header can have any size. Is there a way to dynamically allocate the string response_header?

5
1
Comments 1