|
1 | 1 | /* |
| 2 | +https://stackoverflow.com/questions/11208299/http-get-request-using-c-without-libcurl/35680609#35680609 |
| 3 | +
|
2 | 4 | Fetches a web page and print it to stdout. |
3 | 5 |
|
4 | 6 | example.com: |
@@ -45,6 +47,8 @@ int main(int argc, char** argv) { |
45 | 47 |
|
46 | 48 | if (argc > 1) |
47 | 49 | hostname = argv[1]; |
| 50 | + if (argc > 2) |
| 51 | + server_port = strtoul(argv[2], NULL, 10); |
48 | 52 |
|
49 | 53 | request_len = snprintf(request, MAX_REQUEST_LEN, request_template, hostname); |
50 | 54 | if (request_len >= MAX_REQUEST_LEN) { |
@@ -96,14 +100,21 @@ int main(int argc, char** argv) { |
96 | 100 | nbytes_total += nbytes_last; |
97 | 101 | } |
98 | 102 |
|
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 | + **/ |
107 | 118 | fprintf(stderr, "debug: before first read\n"); |
108 | 119 | while ((nbytes_total = read(socket_file_descriptor, buffer, BUFSIZ)) > 0) { |
109 | 120 | fprintf(stderr, "debug: after a read\n"); |
|
0 commit comments