libfetch: Fix PROXY_HOST bug, socket leak, cache-crash

As described in FreeBSD PR kern/139751, NO_PROXY=localhost doesn't work
due to bad for loop count.

As described in FreeBSD PR kern/153748, an ftp-cached connection will
close the connection too early when trying to send a request.

There is a socket leak in ftp_request that occurs after a connection is
established.  Make sure socket is disconnected before exiting.

Taken-From: FreeBSD SVN 198339 (21 OCT 2009)
Taken-From: FreeBSD SVN 199801 (25 NOV 2009)
Taken-From: FreeBSD SVN 217505 (17 JAN 2011)
libfetch: Fix hang due to SSL server closing before read completes

If the server sends a close notification before before a SSL read
operation is complete, fetch will hang.  Fix this by reworking
fetch_read() to use non-blocking sockets.

Taken-From: FreeBSD SVN 210568 (28 JUL 2010)
Taken-From: FreeBSD SVN 214256 (23 OCT 2010)
libfetch: Mark all socket and file descriptors close-on-exec

This allows libfetch to close cached sockets.
Also add cast that helped FreeBSD move warns to 6

Taken-from: FreeBSD SVN 221822 (12 May 2011)
Taken-from: FreeBSD SVN 221830 (13 May 2011)
libfetch: Fix failure to return to caller on error

As described in FreeBSD PR kern/171187, several error conditions were
missing return statements.

Taken-from: FreeBSD SVN 240595
libfetch: Support percent-encoded user and password

RFC 1738 specifies that any ":", "@", or "/" within a user name or
password in a URL is percent-encoded, to avoid ambiguity with the use
of those characters as URL component separators.

Taken-From: FreeBSD SVN 234138 (12 APR 2012)
libfetch: Change maximum redirect from 5 to 20

Libfetch gives up a little too soon.  Chrome and Firefox allow 20
redirects with IE stops after 8.

Taken-from: FreeBSD SVN 241839 (21 OCT 2012)
fetch: Fix infinite loop on display progress

As reported by FreeBSD PR bin/153240, fetch can loop forever if it is
interrupted by a signal at just the right time.

Verbatim from FreeBSD SVN 230307 (18 JAN 2012):
Fix two issues related to the use of SIGINFO in fetch(1) to display
progress information.  The first is that fetch_read() (used in the HTTP
code but not the FTP code) can enter an infinite loop if it has previously
been interrupted by a signal.  The second is that when it is interrupted,
fetch_read() will discard any data it may have read up to that point.
Luckily, both bugs are extremely timing-sensitive and therefore difficult

to trigger.
Implement SO_NOSIGPIPE

The SO_NOSIGPIPE socket option allows a user process to mark a socket so
that the socket does not generate SIGPIPE, only EPIPE, when a write is
attempted after socket shutdown.

Regression test added: tools/regression/sockets/sigpipe
libfetch: Avoid SIGPIPE on network connections

To avoid unexpected process termination from SIGPIPE when writing to a
closed network connection, enable SO_NOSIGPIPE on all network connections.

The POSIX standard MSG_NOSIGNAL is not used since it requires modifying all
send calls to add this flag. This is particularly nasty for SSL connections.

Taken-from: FreeBSD SVN 236193 (28 May 2012)
usb4bsd - Change M_NOWAIT to M_WAITOK

* M_NOWAIT really needs to be M_WAITOK in these places.  It is unclear
  why FreeBSD invites random device failures by using M_NOWAIT all over
  the place.
Merge branch 'master' into gsocdrm_alter
libfetch: what happened to ftp-cached commit?

I thought I committed this fix last night.  I don't know how it
disappeared.  It's the fix documented in FreeBSD PR Kern/153748
where a ftp-cached connection will close the connection too early
when sending a request.
libfetch: Change FTP default to passive mode

1. The caller can select active mode using "P" flag.
2. If the "p" passive flag is also present, it takes precedence over "P"
   so the connection will be attempted in passive mode.

Taken-from: FreeBSD SVN 225810 (27 SEP 2011)
Taken-from: FreeBSD SVN 225812 (27 SEP 2011)
libfetch: Use select(2) as a consequence of switching to non-blocking

Since the socket is non-blocking, it is necessary to use select(2) even
when there is no timeout, because read(2) will return immediately if there
is no data waiting in the TCP buffer, causing fetch_read() to busy-loop on
slow connections.

Taken-from: FreeBSD SVN 234837 (30 APR 2012)
libfetch: Implement HTTP digest authentication, HTTP 305, and HTTP 308

Implement HTTP status 305: Use proxy
Implement HTTP status 308: Permanent redirect
Implement HTTP Digest Authentication

Taken-from:
FreeBSD SVN 202613 (19 JAN 2010)
FreeBSD SVN 202623 (19 JAN 2010)
FreeBSD SVN 203028 (26 JAN 2010)
FreeBSD SVN 209632 (01 JUL 2010)
FreeBSD SVN 210563 (28 JUL 2010)
FreeBSD SVN 221820 (12 MAY 2011)
FreeBSD SVN 221821 (12 MAY 2011)
FreeBSD SVN 221822 (12 MAY 2011)
FreeBSD SVN 221823 (12 MAY 2011)
FreeBSD SVN 234838 (30 APR 2012)
FreeBSD SVN 240496 (14 SEP 2012)
FreeBSD SVN 241840 (21 OCT 2012)
FreeBSD SVN 241841 (21 OCT 2012)
Merge branch 'master' of ssh://crater.dragonflybsd.org/repository/git/dragonfly
kernel - Force word-writes in syscons code (Intel hw character mode emulation)

* Observed on the Intel D2500HN.  The system console in character map mode
  fails utterly when a normal bcopy() is used to move stuff around.  Employ
  a specialized bcopy that uses 16-bit writes to work around the problem.

* Just do this generally, we don't care about syscons performance.

* This is possibly due to a hardware/text-mode emulation in the Intel D2500HN
  and it will probably show up in the future as well.  This could be related
  to complaints about UEFI BIOSs (which the D2500HN mobo from Intel supports)
  not handling text mode, but it handles it just fine if we only use 16-bit
  writes.
Merge branch 'master' into gsocdrm_merger