Respond with HTTP 400 Bad Request on invalid revision number

CouchDB should return HTTP 400 instead of HTTP 500 response
when revision number isn't integer like "foo-bar" as like as it does
for completely invalid revisions like "foo".

COUCHDB-2375
Move query server related stats from couch to couch_mrview
Remove couch_query_servers:map_docs/2

It's not being used since couch_mrview introduction which provides own
couch_mrview_updater:map_docs/2 and actually uses it instead.
Make couch_stream monitor the stream opener

This commit fixes a process leak which would happen when a process
opened a couch_stream and exited with reason normal before closing
the stream.

This patch makes the couch_stream gen_server monitor the process
which opens the stream. When that monitor sends an exit signal
the couch_stream gen_server is stopped and the process dies.

Closes COUCHDB-2365
Fix header for documents with newlines in the name

Properly urlencode the Document-Id in the Location field of the
header

Based on a patch from Sean Bartell <wingedtachikoma@gmail.com>

Added some eunit-tests

COUCHDB-708
Implement two factor authentication

If enabled, require a second factor to acquire a session cookie and
reject basic authentication attempts (as second factor cannot be
presented). Allow previous and next token for clock skew.
extract couch_httpd changes API in its own module
add supports of view changes in the _changes API

Now when the option `seq_indexed=true` is set in the design doc, the
view filter in _changes will use it to retrieve the results. Compared to
the current way, using a view index will be faster to retrieve changes.
It also gives the possibility to filter changes by key or get changes in
a key range. All the view options can be used.

Note 1: if someone is trying to filter a changes with view options when
the views are not indexed by sequence, a 400 error will be returned.
Note 2: The changes will only be returned when the view is updated if
seq_indexed=true
couch_httpd_changes: check removed keys from the view filter

Make sure to only emit deleted document when a deleted key is passed to
the view filter.

Conflicts:
	src/couch_httpd_changes.erl
Add preliminary version of view changes
Add view filtering optimization to changes feeds
Add rev to view changes respose. See couch_mrview for corresponding changes
Open view in changes start function rather than outside
Make include_docs=true work for view changes
Refactor code for sanity and so the view changes handler can reuse code better
Add comment and do minor refactoring
Change return format of _view_changes

This commit makes KV pairs for _view_changes requests be returned in the
format:

    {..."add": [["key", "val"],["key2", "val2"]], "remove": ["oldkey"]}

Note that the "add" field is a list of lists rather than an object
because: 1) there can be multiple values for a given key and 2) keys can
be non-strings (non-string keys are invalid JSON).

Also note that if a view update causes adds or removes some (but not
all) values for a given key, all values associated with that key will be
returned in the _view_changes response.
Add stats counter for validate_doc_update rejections

Counting update rejections helps to answer on following questions:
- What is the origin of so anomaly high 401/403 responses?
  We'll be able to distinguish validation errors from regular auth ones.
- Is someone trying to do things we disallowed and how often?
  High rejection rate is a good signal to start figure out do we have
  too strict validation conditions or to start looking for bad clients.
Track the time was spent to execute validate_doc_update function

Validate document update functions are causes significant slowdown on
request processing time when document gets modified. As more validate
functions we have as higher delay will and the couchdb/request_time
metric couldn't tell us why it values are so high and why our service
is so slow. By tracking down the time spent on running all validate
functions for each document update we could see this overhead and decide
just in time if our validation functions are need in some optimizations.