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?

  • OneCardboardBox@lemmy.sdf.org
    link
    fedilink
    English
    arrow-up
    0
    ·
    edit-2
    28 days ago

    I think a simple approach would be:

    • read a chunk of the response into a char array of known size
    • iterate through the buffer, looking for the CRLF characters that signify the end of the header section
    • copy the header into a dynamically-allocated char array
    • if you didn’t get to the CRLF characters that signify the end of the header section, read more data from the socket into the known-size char array
    • if it takes multiple read operations to get through the header section, then each read you can allocate a longer header string, copy your old data to it, and then append the next chunk
    • when you hit the CRLF, or if read indicates no more data is waiting in the socket (eg connection terminated early) then you can throw a null character at the end of your string and that’s the headers

    This is a somewhat naive implementation. Instead of reallocating and copying the header string array for each read, a linked-list data structure could hold the chunks of buffered data. Then at the end you combine them into a string that’s exactly the right size.