📄 retr.c
字号:
the data that has been read is returned, but it will (obviously) not contain the terminator. The TERMINATOR function is called with three arguments: the beginning of the data read so far, the beginning of the current block of peeked-at data, and the length of the current block. Depending on its needs, the function is free to choose whether to analyze all data or just the newly arrived data. If TERMINATOR returns NULL, it means that the terminator has not been seen. Otherwise it should return a pointer to the charactre immediately following the terminator. The idea is to be able to read a line of input, or otherwise a hunk of text, such as the head of an HTTP request, without crossing the boundary, so that the next call to fd_read etc. reads the data after the hunk. To achieve that, this function does the following: 1. Peek at incoming data. 2. Determine whether the peeked data, along with the previously read data, includes the terminator. 2a. If yes, read the data until the end of the terminator, and exit. 2b. If no, read the peeked data and goto 1. The function is careful to assume as little as possible about the implementation of peeking. For example, every peek is followed by a read. If the read returns a different amount of data, the process is retried until all data arrives safely. SIZEHINT is the buffer size sufficient to hold all the data in the typical case (it is used as the initial buffer size). MAXSIZE is the maximum amount of memory this function is allowed to allocate, or 0 if no upper limit is to be enforced. This function should be used as a building block for other functions -- see fd_read_line as a simple example. */char *fd_read_hunk (int fd, hunk_terminator_t terminator, long sizehint, long maxsize){ long bufsize = sizehint; char *hunk = xmalloc (bufsize); int tail = 0; /* tail position in HUNK */ assert (maxsize >= bufsize); while (1) { const char *end; int pklen, rdlen, remain; /* First, peek at the available data. */ pklen = fd_peek (fd, hunk + tail, bufsize - 1 - tail, -1); if (pklen < 0) { xfree (hunk); return NULL; } end = terminator (hunk, hunk + tail, pklen); if (end) { /* The data contains the terminator: we'll drain the data up to the end of the terminator. */ remain = end - (hunk + tail); assert (remain >= 0); if (remain == 0) { /* No more data needs to be read. */ hunk[tail] = '\0'; return hunk; } if (bufsize - 1 < tail + remain) { bufsize = tail + remain + 1; hunk = xrealloc (hunk, bufsize); } } else /* No terminator: simply read the data we know is (or should be) available. */ remain = pklen; /* Now, read the data. Note that we make no assumptions about how much data we'll get. (Some TCP stacks are notorious for read returning less data than the previous MSG_PEEK.) */ rdlen = fd_read (fd, hunk + tail, remain, 0); if (rdlen < 0) { xfree_null (hunk); return NULL; } tail += rdlen; hunk[tail] = '\0'; if (rdlen == 0) { if (tail == 0) { /* EOF without anything having been read */ xfree (hunk); errno = 0; return NULL; } else /* EOF seen: return the data we've read. */ return hunk; } if (end && rdlen == remain) /* The terminator was seen and the remaining data drained -- we got what we came for. */ return hunk; /* Keep looping until all the data arrives. */ if (tail == bufsize - 1) { /* Double the buffer size, but refuse to allocate more than MAXSIZE bytes. */ if (maxsize && bufsize >= maxsize) { xfree (hunk); errno = ENOMEM; return NULL; } bufsize <<= 1; if (maxsize && bufsize > maxsize) bufsize = maxsize; hunk = xrealloc (hunk, bufsize); } }}static const char *line_terminator (const char *start, const char *peeked, int peeklen){ const char *p = memchr (peeked, '\n', peeklen); if (p) /* p+1 because the line must include '\n' */ return p + 1; return NULL;}/* The maximum size of the single line we agree to accept. This is not meant to impose an arbitrary limit, but to protect the user from Wget slurping up available memory upon encountering malicious or buggy server output. Define it to 0 to remove the limit. */#define FD_READ_LINE_MAX 4096/* Read one line from FD and return it. The line is allocated using malloc, but is never larger than FD_READ_LINE_MAX. If an error occurs, or if no data can be read, NULL is returned. In the former case errno indicates the error condition, and in the latter case, errno is NULL. */char *fd_read_line (int fd){ return fd_read_hunk (fd, line_terminator, 128, FD_READ_LINE_MAX);}/* Return a printed representation of the download rate, along with the units appropriate for the download speed. */const char *retr_rate (wgint bytes, double secs){ static char res[20]; static const char *rate_names[] = {"B/s", "KB/s", "MB/s", "GB/s" }; int units; double dlrate = calc_rate (bytes, secs, &units); /* Use more digits for smaller numbers (regardless of unit used), e.g. "1022", "247", "12.5", "2.38". */ sprintf (res, "%.*f %s", dlrate >= 99.95 ? 0 : dlrate >= 9.995 ? 1 : 2, dlrate, rate_names[units]); return res;}/* Calculate the download rate and trim it as appropriate for the speed. Appropriate means that if rate is greater than 1K/s, kilobytes are used, and if rate is greater than 1MB/s, megabytes are used. UNITS is zero for B/s, one for KB/s, two for MB/s, and three for GB/s. */doublecalc_rate (wgint bytes, double secs, int *units){ double dlrate; assert (secs >= 0); assert (bytes >= 0); if (secs == 0) /* If elapsed time is exactly zero, it means we're under the resolution of the timer. This can easily happen on systems that use time() for the timer. Since the interval lies between 0 and the timer's resolution, assume half the resolution. */ secs = ptimer_resolution () / 2.0; dlrate = bytes / secs; if (dlrate < 1024.0) *units = 0; else if (dlrate < 1024.0 * 1024.0) *units = 1, dlrate /= 1024.0; else if (dlrate < 1024.0 * 1024.0 * 1024.0) *units = 2, dlrate /= (1024.0 * 1024.0); else /* Maybe someone will need this, one day. */ *units = 3, dlrate /= (1024.0 * 1024.0 * 1024.0); return dlrate;}#define SUSPEND_POST_DATA do { \ post_data_suspended = true; \ saved_post_data = opt.post_data; \ saved_post_file_name = opt.post_file_name; \ opt.post_data = NULL; \ opt.post_file_name = NULL; \} while (0)#define RESTORE_POST_DATA do { \ if (post_data_suspended) \ { \ opt.post_data = saved_post_data; \ opt.post_file_name = saved_post_file_name; \ post_data_suspended = false; \ } \} while (0)static char *getproxy (struct url *);/* Retrieve the given URL. Decides which loop to call -- HTTP, FTP, FTP, proxy, etc. *//* #### This function should be rewritten so it doesn't return from multiple points. */uerr_tretrieve_url (const char *origurl, char **file, char **newloc, const char *refurl, int *dt, bool recursive){ uerr_t result; char *url; bool location_changed; int dummy; char *mynewloc, *proxy; struct url *u, *proxy_url; int up_error_code; /* url parse error code */ char *local_file; int redirection_count = 0; bool post_data_suspended = false; char *saved_post_data = NULL; char *saved_post_file_name = NULL; /* If dt is NULL, use local storage. */ if (!dt) { dt = &dummy; dummy = 0; } url = xstrdup (origurl); if (newloc) *newloc = NULL; if (file) *file = NULL; u = url_parse (url, &up_error_code); if (!u) { logprintf (LOG_NOTQUIET, "%s: %s.\n", url, url_error (up_error_code)); xfree (url); return URLERROR; } if (!refurl) refurl = opt.referer; redirected: result = NOCONERROR; mynewloc = NULL; local_file = NULL; proxy_url = NULL; proxy = getproxy (u); if (proxy) { /* Parse the proxy URL. */ proxy_url = url_parse (proxy, &up_error_code); if (!proxy_url) { logprintf (LOG_NOTQUIET, _("Error parsing proxy URL %s: %s.\n"), proxy, url_error (up_error_code)); xfree (url); RESTORE_POST_DATA; return PROXERR; } if (proxy_url->scheme != SCHEME_HTTP && proxy_url->scheme != u->scheme) { logprintf (LOG_NOTQUIET, _("Error in proxy URL %s: Must be HTTP.\n"), proxy); url_free (proxy_url); xfree (url); RESTORE_POST_DATA; return PROXERR; } } if (u->scheme == SCHEME_HTTP#ifdef HAVE_SSL || u->scheme == SCHEME_HTTPS#endif || (proxy_url && proxy_url->scheme == SCHEME_HTTP)) { result = http_loop (u, &mynewloc, &local_file, refurl, dt, proxy_url); } else if (u->scheme == SCHEME_FTP) { /* If this is a redirection, temporarily turn off opt.ftp_glob and opt.recursive, both being undesirable when following redirects. */ bool oldrec = recursive, glob = opt.ftp_glob; if (redirection_count) oldrec = glob = false; result = ftp_loop (u, dt, proxy_url, recursive, glob); recursive = oldrec; /* There is a possibility of having HTTP being redirected to FTP. In these cases we must decide whether the text is HTML according to the suffix. The HTML suffixes are `.html', `.htm' and a few others, case-insensitive. */ if (redirection_count && local_file && u->scheme == SCHEME_FTP) { if (has_html_suffix_p (local_file)) *dt |= TEXTHTML;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -