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
?
I think a simple approach would be:
read
a chunk of the response into a char array of known sizeread
more data from the socket into the known-size char arrayread
operations to get through the header section, then eachread
you can allocate a longer header string, copy your old data to it, and then append the next chunkread
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 headersThis 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.