Skip to content

Commit 98907e4

Browse files
committed
wget: improve hang comment
1 parent 402d3d8 commit 98907e4

1 file changed

Lines changed: 19 additions & 8 deletions

File tree

‎posix/socket/wget.c‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/*
2+
https://stackoverflow.com/questions/11208299/http-get-request-using-c-without-libcurl/35680609#35680609
3+
24
Fetches a web page and print it to stdout.
35
46
example.com:
@@ -45,6 +47,8 @@ int main(int argc, char** argv) {
4547

4648
if (argc > 1)
4749
hostname = argv[1];
50+
if (argc > 2)
51+
server_port = strtoul(argv[2], NULL, 10);
4852

4953
request_len = snprintf(request, MAX_REQUEST_LEN, request_template, hostname);
5054
if (request_len >= MAX_REQUEST_LEN) {
@@ -96,14 +100,21 @@ int main(int argc, char** argv) {
96100
nbytes_total += nbytes_last;
97101
}
98102

99-
/*
100-
Read the response. TODO: for example.com, the second read hangs for a few seconds.
101-
Why? Does the connection keep open until I close it?
102-
Must I parse the returned string for Content-Length to see if the HTTP response is over,
103-
and close it then? http://stackoverflow.com/a/25586633/895245 says that if Content-Length
104-
is not sent, the server can just close to determine length.
105-
curl example.com does not lag at all.
106-
*/
103+
/* Read the response.
104+
*
105+
* The second read hangs for a few seconds, until the server times out.
106+
*
107+
* Either server or client has to close the connection.
108+
*
109+
* We are not doing it, and neither is the server, likely to make serving the page faster
110+
* to allow fetching HTML, CSS, Javascript and images in a single connection.
111+
*
112+
* The solution is to parse Content-Length to see if the HTTP response is over,
113+
* and close it then.
114+
*
115+
* http://stackoverflow.com/a/25586633/895245 says that if Content-Length
116+
* is not sent, the server can just close to determine length.
117+
**/
107118
fprintf(stderr, "debug: before first read\n");
108119
while ((nbytes_total = read(socket_file_descriptor, buffer, BUFSIZ)) > 0) {
109120
fprintf(stderr, "debug: after a read\n");

0 commit comments

Comments
 (0)