+ fprintf(sfp, "Range: bytes=%ld-\r\n", beg_range);
+ fprintf(sfp,"Connection: close\r\n\r\n");
+
+ /*
+ * Retrieve HTTP response line and check for "200" status code.
+ */
+ if (fgets(buf, sizeof(buf), sfp) == NULL)
+ close_delete_and_die("no response from server");
+
+ for (s = buf ; *s != '\0' && !isspace(*s) ; ++s)
+ ;
+ for ( ; isspace(*s) ; ++s)
+ ;
+ switch (status = atoi(s)) {
+ case 0:
+ case 200:
+ if (do_continue && output != stdout)
+ output = freopen(fname_out, "w", output);
+ do_continue = 0;
+ break;
+ case 300: /* redirection */
+ case 301:
+ case 302:
+ case 303:
+ break;
+ case 206:
+ if (do_continue)
+ break;
+ /*FALLTHRU*/
+ default:
+ chomp(buf);
+ close_delete_and_die("server returned error %d: %s", atoi(s), buf);
+ }
+
+ /*
+ * Retrieve HTTP headers.
+ */
+ while ((s = gethdr(buf, sizeof(buf), sfp, &n)) != NULL) {
+ if (strcasecmp(buf, "content-length") == 0) {
+ filesize = atol(s);
+ got_clen = 1;
+ continue;
+ }
+ if (strcasecmp(buf, "transfer-encoding") == 0)
+ close_delete_and_die("server wants to do %s transfer encoding", s);
+
+ if (strcasecmp(buf, "location") == 0) {
+ if (s[0] == '/')
+ target.path = xstrdup(s+1);
+ else {
+ parse_url(xstrdup(s), &target);
+ if (!proxy) {
+ server.host = target.host;
+ server.port = target.port;
+ }
+ }
+ }
+ }
+ } while(status >= 300);
+
+ dfp = sfp;