+ if (ret < -1)
+ ret = -1;
+
+ out:
+ if (progress)
+ progress_finish (progress, ptimer_read (timer));
+
+ if (elapsed)
+ *elapsed = ptimer_read (timer);
+ if (timer)
+ ptimer_destroy (timer);
+
+ if (qtyread)
+ *qtyread += sum_read;
+ if (qtywritten)
+ *qtywritten += sum_written;
+
+ return ret;
+}
+\f
+/* Read a hunk of data from FD, up until a terminator. The hunk is
+ limited by whatever the TERMINATOR callback chooses as its
+ terminator. For example, if terminator stops at newline, the hunk
+ will consist of a line of data; if terminator stops at two
+ newlines, it can be used to read the head of an HTTP response.
+ Upon determining the boundary, the function returns the data (up to
+ the terminator) in malloc-allocated storage.
+
+ In case of read error, NULL is returned. In case of EOF and no
+ data read, NULL is returned and errno set to 0. In case of having
+ read some data, but encountering EOF before seeing the terminator,
+ 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);