Compare commits

...

84 Commits
0.4.0 ... 0.7.0

Author SHA1 Message Date
9d2ac5d8ad 0.7.0 2013-09-24 20:07:48 +02:00
3e4e1c72a4 Merge branch 'master' of github.com:jkbr/httpie 2013-09-24 19:51:06 +02:00
29f6b6a2a9 Improved Content-Disposition parsing for --download mode
Closes #168.
2013-09-24 19:50:37 +02:00
26b2d408e7 Merge pull request #167 from matt-hickford/master
Fix plugins ImportError
2013-09-23 02:13:14 -07:00
b5f180a5ee Fix plugins ImportError described at https://github.com/jkbr/httpie/issues/166#issuecomment-24905910 2013-09-23 09:54:06 +01:00
354aaa94bd Improved .netrc example formatting. 2013-09-22 15:20:50 +02:00
2ad4059f92 Improved .netrc example formatting. 2013-09-22 15:19:59 +02:00
5a6b65ecc6 Added link to httpie-oauth. 2013-09-22 15:10:50 +02:00
2acb303552 Added support for auth plugins. 2013-09-21 23:46:15 +02:00
f7b703b4bf Added --ignore-stdin
Closes #150
2013-08-23 10:57:17 +02:00
00de49f4c3 Cleanup 2013-08-18 00:59:10 +02:00
67496162fa Improved --help output. 2013-08-10 11:56:19 +02:00
8378ad3624 Try to import argparse before adding it to reqs. 2013-08-01 09:07:33 +02:00
f87884dd8d README 2013-08-01 08:46:37 +02:00
b671ee35e7 Merge pull request #153 from lorin/patch-1
Augment cookie example in README for multiple cookies
2013-07-31 07:52:22 -07:00
69247066dc Augment cookie example in README for multiple cookies
This change updates the README to show how to pass multiple cookies.
2013-07-31 10:29:38 -04:00
383dba524a Print error when download is interrupted by server
Close #147
2013-07-07 17:00:03 +02:00
60f09776a5 httpless outputs also response headers by default 2013-06-03 12:28:04 +02:00
48719aa70e README 2013-06-03 12:22:34 +02:00
809a461a26 v0.6.0 2013-06-03 12:19:43 +02:00
c3d550e930 Fixed headers tests; Require requests>=1.2.3. 2013-06-02 20:47:29 +02:00
172df162b3 Added XML formatting to CHANGELOG. 2013-06-02 20:27:58 +02:00
1bad62ab0e Handle unicode when formatting XML. 2013-06-02 20:25:36 +02:00
8d302f91f9 Merge branch 'master' of git://github.com/jargonjustin/httpie into jargonjustin-master 2013-06-02 20:14:51 +02:00
63b61bc811 Add custom Host example. 2013-05-20 15:31:02 +02:00
5af88756a6 Fixed download ETA for Python 2.6. 2013-05-14 12:49:29 +02:00
7f624e61b5 Use Thread instead of Timer for progress reporting. 2013-05-14 12:49:03 +02:00
6e848b3203 cleanup 2013-05-14 12:14:08 +02:00
8e112a6948 test_download_no_Content_Length 2013-05-13 15:35:12 +02:00
87c59ae561 Added anonymous sessions (--session=/file/path.json). 2013-05-13 14:47:44 +02:00
76eebeac2a 0.6.0-dev 2013-05-13 12:42:16 +02:00
5b9cbcb530 v0.5.1 2013-05-13 12:40:25 +02:00
8ad33d5f6a Changelog 2013-05-13 12:20:54 +02:00
86ac4cdb7b Changelog 2013-05-13 12:20:28 +02:00
e09b74021c Ignore Content-* and If-* request headers.
Those headers are not stored in sessions anymore.

Closes #141.
2013-05-13 11:54:49 +02:00
71e7061014 v0.5.0 2013-04-27 12:03:38 -03:00
bc756cb6a2 Cleanup 2013-04-27 11:57:13 -03:00
63ed4d32a7 Merge remote-tracking branch 'origin/master' 2013-04-17 13:52:02 -03:00
d1b91bfa9c Merge pull request #142 from capncodewash/netrc-example
Added example for .netrc usage (closes #139)
2013-04-17 09:46:50 -07:00
dac79a8efc Added example for .netrc usage (see issue #139 in upstream. 2013-04-17 16:32:55 +01:00
1fc8396c4b Stop the progres reporter thread on error. 2013-04-16 04:55:45 -03:00
6c3b983c18 Tests 2013-04-15 00:56:47 -03:00
cfa7199f0b Added a simple download test. 2013-04-13 15:34:31 -03:00
5a1177d57e Fixed downloads with no Content-Length. 2013-04-13 14:50:46 -03:00
c63a92f9b7 Cleanup 2013-04-12 22:02:34 -03:00
d17e02792b Fixed length progress bar. 2013-04-12 21:49:27 -03:00
fc4f70a900 Colorize stderr on Windows. 2013-04-12 17:15:21 -03:00
1681a4ddd0 TODOs 2013-04-12 15:27:26 -03:00
289e9b844e Fixed Content-Type retrieval for Python 3. 2013-04-12 14:07:21 -03:00
72cf7c2cb7 Fixed tests for Python 2.6. 2013-04-12 13:42:34 -03:00
4d84d77851 Cleanup 2013-04-12 13:09:57 -03:00
1b98505537 Validate download options before setting up streams. 2013-04-12 11:59:23 -03:00
d32acfe2fa Only use Range when already have a partial download. 2013-04-12 11:56:05 -03:00
e8d79c4d8c Docs fix. 2013-04-12 11:37:58 -03:00
38206e9e92 Cleanup 2013-04-12 11:26:42 -03:00
55d5e78324 --download docs (#104). 2013-04-12 11:06:03 -03:00
341272db1e Added support for output redirection with --download (#104). 2013-04-12 11:04:14 -03:00
464b7a36da Tests 2013-04-12 10:20:01 -03:00
9d043eb745 Used Content-Disposition filename (#104). 2013-04-12 10:19:49 -03:00
40bd8f65af Handle KeyboardInterrupt while --download'ing (#104). 2013-04-12 09:08:19 -03:00
347653b369 Performance and progress bar improvements.
#104
2013-04-12 08:59:33 -03:00
ebfce6fb93 Improved progress bar (#104). 2013-04-11 18:51:21 -03:00
674acfe2c2 Cleanup 2013-04-11 16:23:15 -03:00
7ccdece39f Cleanup 2013-04-11 04:00:41 -03:00
e53dcba03e Added Content-Range parsing tests.
#104
2013-04-11 03:49:01 -03:00
486657afa3 Improved Content-Range parsing.
#104
2013-04-11 03:24:59 -03:00
599bc0519f Download resume improvements.
- Set correct Range
- Validate respnse status
- Validate Content-Range

 #104
2013-04-11 02:29:10 -03:00
21613faa5a Progress bar update 2013-04-10 13:07:05 -03:00
36bc64e02f Cleanup. 2013-04-10 12:53:25 -03:00
6e5c696ac9 --json with no data sets Content-Type as well
Closes #137
2013-04-02 11:07:14 -03:00
9b2a293e6e Progress on --download. 2013-03-24 11:23:18 -03:00
b0dd463687 Corrected session info in the README. 2013-03-22 16:26:51 -03:00
bffaee13ff Formatting 2013-03-20 12:07:23 -03:00
30afcea72d Merge pull request #135 from Scorpil/master
Fixed PyPy cookie updating issue

Closes #132
2013-03-20 08:05:23 -07:00
631c54b711 Fixed PyPy cookie updating issue 2013-03-20 11:45:56 +02:00
99f82bbd32 Handle downloads with no Content-Length. 2013-03-07 13:32:48 -03:00
6f64b437b7 Fixed streaming (closes #133) 2013-03-07 12:42:29 -03:00
7774eac3df Fixed unique suffix placement for URLs with a file extension. 2013-03-03 22:35:01 -03:00
8e6c765be2 Initial --download implementation (#104).
Closes #127
2013-03-03 22:17:09 -03:00
f0c42cd089 v0.4.1 2013-02-26 14:37:09 +01:00
5c6cea79a1 Removed a reference to the removed httpie command
Closes #131
2013-02-26 14:31:52 +01:00
2bed81059a Updated README. 2013-02-22 14:04:27 +01:00
be0b2f21d2 v0.4.0 2013-02-22 13:52:50 +01:00
2e57c080fd Pretty print XML 2012-12-17 13:21:38 -08:00
23 changed files with 1895 additions and 611 deletions

View File

@ -28,3 +28,4 @@ Patches and ideas
* `Tomek Wójcik <https://github.com/tomekwojcik>`_ * `Tomek Wójcik <https://github.com/tomekwojcik>`_
* `Davey Shafik <https://github.com/dshafik>`_ * `Davey Shafik <https://github.com/dshafik>`_
* `cido <https://github.com/cido>`_ * `cido <https://github.com/cido>`_
* `Justin Bonnar <https://github.com/jargonjustin>`_

View File

@ -2,13 +2,6 @@
HTTPie: a CLI, cURL-like tool for humans HTTPie: a CLI, cURL-like tool for humans
**************************************** ****************************************
.. image:: https://raw.github.com/claudiatd/httpie-artwork/master/images/httpie_logo_simple.png
:alt: HTTPie logo
:width: 469
:height: 331
:align: center
v0.4.0-alpha (`stable version`_)
HTTPie is a **command line HTTP client**. Its goal is to make CLI interaction HTTPie is a **command line HTTP client**. Its goal is to make CLI interaction
with web services as **human-friendly** as possible. It provides a with web services as **human-friendly** as possible. It provides a
@ -24,6 +17,13 @@ for **testing, debugging**, and generally **interacting** with HTTP servers.
:align: center :align: center
------
.. image:: https://raw.github.com/claudiatd/httpie-artwork/master/images/httpie_logo_simple.png
:alt: HTTPie logo
:align: center
HTTPie is written in Python, and under the hood it uses the excellent HTTPie is written in Python, and under the hood it uses the excellent
`Requests`_ and `Pygments`_ libraries. `Requests`_ and `Pygments`_ libraries.
@ -37,7 +37,6 @@ HTTPie is written in Python, and under the hood it uses the excellent
:backlinks: none :backlinks: none
============= =============
Main Features Main Features
============= =============
@ -50,6 +49,7 @@ Main Features
* Arbitrary request data * Arbitrary request data
* Custom headers * Custom headers
* Persistent sessions * Persistent sessions
* Wget-like downloads
* Python 2.6, 2.7 and 3.x support * Python 2.6, 2.7 and 3.x support
* Linux, Mac OS X and Windows support * Linux, Mac OS X and Windows support
* Documentation * Documentation
@ -143,7 +143,9 @@ See the request that is being sent using one of the `output options`_:
$ http -v example.org $ http -v example.org
Use `Github API`_ to post a comment on an issue with `authentication`_: Use `Github API`_ to post a comment on an
`issue <https://github.com/jkbr/httpie/issues/83>`_
with `authentication`_:
.. code-block:: bash .. code-block:: bash
@ -163,6 +165,13 @@ Download a file and save it via `redirected output`_:
$ http example.org/file > file $ http example.org/file > file
Download a file ``wget`` style:
.. code-block:: bash
$ http --download example.org/file
Use named `sessions`_ to make certain aspects or the communication persistent Use named `sessions`_ to make certain aspects or the communication persistent
between requests to the same host: between requests to the same host:
@ -172,6 +181,13 @@ between requests to the same host:
$ http --session=logged-in httpbin.org/headers $ http --session=logged-in httpbin.org/headers
Set a custom ``Host`` header to work around missing DNS records:
.. code-block:: bash
$ http localhost:8000 Host:example.com
.. ..
-------- --------
@ -250,8 +266,8 @@ their type is distinguished only by the separator used:
| | The ``==`` separator is used | | | The ``==`` separator is used |
+-----------------------+-----------------------------------------------------+ +-----------------------+-----------------------------------------------------+
| Data Fields | Request data fields to be serialized as a JSON | | Data Fields | Request data fields to be serialized as a JSON |
| ``field=value`` | object (default), or to be form encoded (``--form`` | | ``field=value`` | object (default), or to be form encoded |
| | / ``-f``). | | | (``--form, -f``). |
+-----------------------+-----------------------------------------------------+ +-----------------------+-----------------------------------------------------+
| Raw JSON fields | Useful when sending JSON and one or | | Raw JSON fields | Useful when sending JSON and one or |
| ``field:=json`` | more fields need to be a ``Boolean``, ``Number``, | | ``field:=json`` | more fields need to be a ``Boolean``, ``Number``, |
@ -259,7 +275,7 @@ their type is distinguished only by the separator used:
| | ``meals:='["ham","spam"]'`` or ``pies:=[1,2,3]`` | | | ``meals:='["ham","spam"]'`` or ``pies:=[1,2,3]`` |
| | (note the quotes). | | | (note the quotes). |
+-----------------------+-----------------------------------------------------+ +-----------------------+-----------------------------------------------------+
| Files | Only available with ``-f`` / ``--form``. | | Files | Only available with ``--form, -f``. |
| ``field@/dir/file`` | For example ``screenshot@~/Pictures/img.png``. | | ``field@/dir/file`` | For example ``screenshot@~/Pictures/img.png``. |
| | The presence of a file field results | | | The presence of a file field results |
| | in a ``multipart/form-data`` request. | | | in a ``multipart/form-data`` request. |
@ -290,7 +306,7 @@ both of which can be overwritten:
``Accept`` ``application/json`` ``Accept`` ``application/json``
================ ======================================= ================ =======================================
You can use ``--json`` / ``-j`` to explicitly set ``Accept`` You can use ``--json, -j`` to explicitly set ``Accept``
to ``application/json`` regardless of whether you are sending data to ``application/json`` regardless of whether you are sending data
(it's a shortcut for setting the header via the usual header notation (it's a shortcut for setting the header via the usual header notation
``http url Accept:application/json``). ``http url Accept:application/json``).
@ -308,7 +324,6 @@ Simple example:
Accept-Encoding: identity, deflate, compress, gzip Accept-Encoding: identity, deflate, compress, gzip
Content-Type: application/json; charset=utf-8 Content-Type: application/json; charset=utf-8
Host: example.org Host: example.org
User-Agent: HTTPie/0.2.7dev
{ {
"name": "John", "name": "John",
@ -330,7 +345,6 @@ into the resulting object:
Accept: application/json Accept: application/json
Content-Type: application/json; charset=utf-8 Content-Type: application/json; charset=utf-8
Host: api.example.com Host: api.example.com
User-Agent: HTTPie/0.2.7dev
{ {
"age": 29, "age": 29,
@ -355,7 +369,7 @@ Forms
===== =====
Submitting forms is very similar to sending `JSON`_ requests. Often the only Submitting forms is very similar to sending `JSON`_ requests. Often the only
difference is in adding the ``--form`` / ``-f`` option, which ensures that difference is in adding the ``--form, -f`` option, which ensures that
data fields are serialized as, and ``Content-Type`` is set to, data fields are serialized as, and ``Content-Type`` is set to,
``application/x-www-form-urlencoded; charset=utf-8``. ``application/x-www-form-urlencoded; charset=utf-8``.
@ -375,7 +389,6 @@ Regular Forms
.. code-block:: http .. code-block:: http
POST /person/1 HTTP/1.1 POST /person/1 HTTP/1.1
User-Agent: HTTPie/0.2.7dev
Content-Type: application/x-www-form-urlencoded; charset=utf-8 Content-Type: application/x-www-form-urlencoded; charset=utf-8
name=John+Smith&email=john%40example.org name=John+Smith&email=john%40example.org
@ -412,7 +425,7 @@ To set custom headers you can use the ``Header:Value`` notation:
.. code-block:: bash .. code-block:: bash
$ http example.org User-Agent:Bacon/1.0 Cookie:valued-visitor=yes X-Foo:Bar Referer:http://httpie.org/ $ http example.org User-Agent:Bacon/1.0 'Cookie:valued-visitor=yes;foo=bar' X-Foo:Bar Referer:http://httpie.org/
.. code-block:: http .. code-block:: http
@ -420,7 +433,7 @@ To set custom headers you can use the ``Header:Value`` notation:
GET / HTTP/1.1 GET / HTTP/1.1
Accept: */* Accept: */*
Accept-Encoding: identity, deflate, compress, gzip Accept-Encoding: identity, deflate, compress, gzip
Cookie: valued-visitor=yes Cookie: valued-visitor=yes;foo=bar
Host: example.org Host: example.org
Referer: http://httpie.org/ Referer: http://httpie.org/
User-Agent: Bacon/1.0 User-Agent: Bacon/1.0
@ -445,8 +458,8 @@ Any of the default headers can be overwritten.
Authentication Authentication
============== ==============
The currently supported authentication schemes are Basic and Digest (more to The currently supported authentication schemes are Basic and Digest
come). There are two flags that control authentication: (see `auth plugins`_ for more). There are two flags that control authentication:
=================== ====================================================== =================== ======================================================
``--auth, -a`` Pass a ``username:password`` pair as ``--auth, -a`` Pass a ``username:password`` pair as
@ -463,7 +476,7 @@ come). There are two flags that control authentication:
``basic`` so it can often be omitted. ``basic`` so it can often be omitted.
=================== ====================================================== =================== ======================================================
Authorization information from ``.netrc`` is honored as well.
Basic auth: Basic auth:
@ -488,15 +501,38 @@ With password prompt:
$ http -a username example.org $ http -a username example.org
Authorization information from your ``~/.netrc`` file is honored as well:
.. code-block:: bash
$ cat ~/.netrc
machine httpbin.org
login httpie
password test
$ http httpbin.org/basic-auth/httpie/test
HTTP/1.1 200 OK
[...]
------------
Auth Plugins
------------
* `httpie-ntlm <https://github.com/jkbr/httpie-ntlm>`_
* `httpie-oauth <https://github.com/jkbr/httpie-oauth>`_
======= =======
Proxies Proxies
======= =======
You can specify proxies to be used through the ``--proxy`` argument: You can specify proxies to be used through the ``--proxy`` argument for each
protocol (which is included in the value in case of redirects across protocols):
.. code-block:: bash .. code-block:: bash
$ http --proxy=http:10.10.1.10:3128 --https:10.10.1.10:1080 example.org $ http --proxy=http:10.10.1.10:3128 --proxy=https:10.10.1.10:1080 example.org
With Basic authentication: With Basic authentication:
@ -575,7 +611,7 @@ documentation examples:
} }
All the other options are just a shortcut for ``--print`` / ``-p``. All the other options are just a shortcut for ``--print, -p``.
It accepts a string of characters each of which represents a specific part of It accepts a string of characters each of which represents a specific part of
the HTTP exchange: the HTTP exchange:
@ -682,7 +718,16 @@ On OS X, you can send the contents of the clipboard with ``pbpaste``:
Passing data through ``stdin`` cannot be combined with data fields specified Passing data through ``stdin`` cannot be combined with data fields specified
on the command line. on the command line:
.. code-block:: bash
$ echo 'data' | http POST example.org more=data # This is invalid
To prevent HTTPie from reading ``stdin`` data you can use the
``--ignore-stdin`` option.
------------------------- -------------------------
@ -724,6 +769,7 @@ Also, the following formatting is applied:
* HTTP headers are sorted by name. * HTTP headers are sorted by name.
* JSON data is indented, sorted by keys, and unicode escapes are converted * JSON data is indented, sorted by keys, and unicode escapes are converted
to the characters they represent. to the characters they represent.
* XML data is indented for better readability.
One of these options can be used to control output processing: One of these options can be used to control output processing:
@ -812,10 +858,70 @@ by adding the following to your ``~/.bash_profile``:
function httpless { function httpless {
# `httpless example.org' # `httpless example.org'
http --pretty=all "$@" | less -R; http --pretty=all --print=hb "$@" | less -R;
} }
=============
Download Mode
=============
HTTPie features a download mode in which it acts similarly to ``wget``.
When enabled using the ``--download, -d`` flag, response headers are printed to
the terminal (``stderr``), and a progress bar is shown while the response body
is being saved to a file.
.. code-block:: bash
$ http --download https://github.com/jkbr/httpie/tarball/master
.. code-block:: http
HTTP/1.1 200 OK
Connection: keep-alive
Content-Disposition: attachment; filename=jkbr-httpie-0.4.1-33-gfc4f70a.tar.gz
Content-Length: 505530
Content-Type: application/x-gzip
Server: GitHub.com
Vary: Accept-Encoding
Downloading 494.89 kB to "jkbr-httpie-0.4.1-33-gfc4f70a.tar.gz"
/ 21.01% 104.00 kB 47.55 kB/s 0:00:08 ETA
If not provided via ``--output, -o``, the output filename will be determined
from ``Content-Disposition`` (if available), or from the URL and
``Content-Type``. If the guessed filename already exists, HTTPie adds a unique
suffix to it.
You can also redirect the response body to another program while the response
headers and progress are still shown in the terminal:
.. code-block:: bash
$ http -d https://github.com/jkbr/httpie/tarball/master | tar zxf -
If ``--output, -o`` is specified, you can resume a partial download using the
``--continue, -c`` option. This only works with servers that support
``Range`` requests and ``206 Partial Content`` responses. If the server doesn't
support that, the whole file will simply be downloaded:
.. code-block:: bash
$ http -dco file.zip example.org/file
Other notes:
* The ``--download`` option only changes how the response body is treated.
* You can still set custom headers, use sessions, ``--verbose, -v``, etc.
* ``--download`` always implies ``--follow`` (redirects are followed).
* HTTPie exits with status code ``1`` (error) if the body hasn't been fully
downloaded.
* ``Accept-Encoding`` cannot be set with ``--download``.
================== ==================
Streamed Responses Streamed Responses
================== ==================
@ -853,18 +959,21 @@ Streamed output by small chunks alá ``tail -f``:
$ http --stream -f -a YOUR-TWITTER-NAME https://stream.twitter.com/1/statuses/filter.json track=Apple \ $ http --stream -f -a YOUR-TWITTER-NAME https://stream.twitter.com/1/statuses/filter.json track=Apple \
| while read tweet; do echo "$tweet" | http POST example.org/tweets ; done | while read tweet; do echo "$tweet" | http POST example.org/tweets ; done
======== ========
Sessions Sessions
======== ========
By default, every request is completely independent of the previous ones. By default, every request is completely independent of any previous ones.
HTTPie also supports persistent sessions, where custom headers, authorization, HTTPie also supports persistent sessions, where custom headers (except for the
and cookies (manually specified or sent by the server) persist between ones starting with ``Content-`` or ``If-``), authorization, and cookies
requests to the same host. (manually specified or sent by the server) persist between requests
to the same host.
Create a new session named ``user1``: --------------
Named Sessions
--------------
Create a new session named ``user1`` for ``example.org``:
.. code-block:: bash .. code-block:: bash
@ -887,19 +996,31 @@ To use a session without updating it from the request/response exchange
once it is created, specify the session name via once it is created, specify the session name via
``--session-read-only=SESSION_NAME`` instead. ``--session-read-only=SESSION_NAME`` instead.
Session data are stored in JSON files in the directory Named sessions' data is stored in JSON files in the directory
``~/.httpie/sessions/<host>/<name>.json`` ``~/.httpie/sessions/<host>/<name>.json``
(``%APPDATA%\httpie\sessions\<host>\<name>.json`` on Windows). (``%APPDATA%\httpie\sessions\<host>\<name>.json`` on Windows).
**Warning:** All session data, including credentials, cookie data,
and custom headers are stored in plain text.
Session files can also be created or edited with a text editor. ------------------
Anonymous Sessions
------------------
Instead of a name, you can also directly specify a path to a session file. This
allows for sessions to be re-used across multiple hosts:
.. code-block:: bash .. code-block:: bash
$ httpie session edit example.org user1 $ http --session=/tmp/session.json example.org
$ http --session=/tmp/session.json admin.example.org
$ http --session=~/.httpie/sessions/another.example.org/test.json example.org
$ http --session-read-only=/tmp/session.json example.org
**Warning:** All session data, including credentials, cookie data,
and custom headers are stored in plain text.
Note that session files can also be created and edited manually in a text
editor; they are plain JSON.
See also `Config`_. See also `Config`_.
@ -952,14 +1073,18 @@ When using HTTPie from **shell scripts**, it can be handy to set the
``--check-status`` flag. It instructs HTTPie to exit with an error if the ``--check-status`` flag. It instructs HTTPie to exit with an error if the
HTTP status is one of ``3xx``, ``4xx``, or ``5xx``. The exit status will HTTP status is one of ``3xx``, ``4xx``, or ``5xx``. The exit status will
be ``3`` (unless ``--follow`` is set), ``4``, or ``5``, be ``3`` (unless ``--follow`` is set), ``4``, or ``5``,
respectively. Also, the ``--timeout`` option allows to overwrite the default respectively.
30s timeout:
The ``--ignore-stdin`` option prevents HTTPie from reading data from ``stdin``,
which is usually not desirable during non-interactive invocations.
Also, the ``--timeout`` option allows to overwrite the default 30s timeout:
.. code-block:: bash .. code-block:: bash
#!/bin/bash #!/bin/bash
if http --timeout=2.5 --check-status HEAD example.org/health &> /dev/null; then if http --check-status --ignore-stdin --timeout=2.5 HEAD example.org/health &> /dev/null; then
echo 'OK!' echo 'OK!'
else else
case $? in case $? in
@ -1075,7 +1200,6 @@ Authors
`Jakub Roztocil`_ (`@jakubroztocil`_) created HTTPie and `these fine people`_ `Jakub Roztocil`_ (`@jakubroztocil`_) created HTTPie and `these fine people`_
have contributed. have contributed.
======= =======
Licence Licence
======= =======
@ -1089,9 +1213,26 @@ Changelog
*You can click a version name to see a diff with the previous one.* *You can click a version name to see a diff with the previous one.*
* `0.4.0-alpha`_ * `0.8.0-dev`_
* `0.7.0`_ (2013-09-24)
* Added ``--ignore-stdin``.
* Added support for auth plugins.
* Improved ``Content-Disposition`` parsing for ``--download`` mode.
* `0.6.0`_ (2013-06-03)
* XML data is now formatted.
* ``--session`` and ``--session-read-only`` now also accept paths to
session files (eg. ``http --session=/tmp/session.json example.org``).
* `0.5.1`_ (2013-05-13)
* ``Content-*`` and ``If-*`` request headers are not stored in sessions
anymore as they are request-specific.
* `0.5.0`_ (2013-04-27)
* Added a `download mode`_ via ``--download``.
* Bugfixes.
* `0.4.1`_ (2013-02-26)
* Fixed ``setup.py``.
* `0.4.0`_ (2013-02-22)
* Python 3.3 compatibility. * Python 3.3 compatibility.
* Requests v1.0.4 compatibility. * Requests >= v1.0.4 compatibility.
* Added support for credentials in URL. * Added support for credentials in URL.
* Added ``--no-option`` for every ``--option`` to be config-friendly. * Added ``--no-option`` for every ``--option`` to be config-friendly.
* Mutually exclusive arguments can be specified multiple times. The * Mutually exclusive arguments can be specified multiple times. The
@ -1111,7 +1252,7 @@ Changelog
``--ugly`` has bee removed in favor of ``--pretty=none``. ``--ugly`` has bee removed in favor of ``--pretty=none``.
* `0.2.7`_ (2012-08-07) * `0.2.7`_ (2012-08-07)
* Compatibility with Requests 0.13.6. * Compatibility with Requests 0.13.6.
* Streamed terminal output. ``--stream`` / ``-S`` can be used to enable * Streamed terminal output. ``--stream, -S`` can be used to enable
streaming also with ``--pretty`` and to ensure a more frequent output streaming also with ``--pretty`` and to ensure a more frequent output
flushing. flushing.
* Support for efficient large file downloads. * Support for efficient large file downloads.
@ -1190,8 +1331,12 @@ Changelog
.. _0.2.6: https://github.com/jkbr/httpie/compare/0.2.5...0.2.6 .. _0.2.6: https://github.com/jkbr/httpie/compare/0.2.5...0.2.6
.. _0.2.7: https://github.com/jkbr/httpie/compare/0.2.5...0.2.7 .. _0.2.7: https://github.com/jkbr/httpie/compare/0.2.5...0.2.7
.. _0.3.0: https://github.com/jkbr/httpie/compare/0.2.7...0.3.0 .. _0.3.0: https://github.com/jkbr/httpie/compare/0.2.7...0.3.0
.. _0.4.0-alpha: https://github.com/jkbr/httpie/compare/0.3.0...master .. _0.4.0: https://github.com/jkbr/httpie/compare/0.3.0...0.4.0
.. _stable version: https://github.com/jkbr/httpie/tree/0.3.0#readme .. _0.4.1: https://github.com/jkbr/httpie/compare/0.4.0...0.4.1
.. _0.5.0: https://github.com/jkbr/httpie/compare/0.4.1...0.5.0
.. _0.5.1: https://github.com/jkbr/httpie/compare/0.5.0...0.5.1
.. _0.6.0: https://github.com/jkbr/httpie/compare/0.5.1...0.6.0
.. _0.7.0: https://github.com/jkbr/httpie/compare/0.6.0...0.7.0
.. _0.8.0-dev: https://github.com/jkbr/httpie/compare/0.7.0...master
.. _AUTHORS.rst: https://github.com/jkbr/httpie/blob/master/AUTHORS.rst .. _AUTHORS.rst: https://github.com/jkbr/httpie/blob/master/AUTHORS.rst
.. _LICENSE: https://github.com/jkbr/httpie/blob/master/LICENSE .. _LICENSE: https://github.com/jkbr/httpie/blob/master/LICENSE

View File

@ -3,7 +3,7 @@ HTTPie - a CLI, cURL-like tool for humans.
""" """
__author__ = 'Jakub Roztocil' __author__ = 'Jakub Roztocil'
__version__ = '0.4.0-alpha' __version__ = '0.7.0'
__licence__ = 'BSD' __licence__ = 'BSD'

View File

@ -1,88 +1,136 @@
"""CLI arguments definition. """CLI arguments definition.
NOTE: the CLI interface may change before reaching v1.0. NOTE: the CLI interface may change before reaching v1.0.
TODO: make the options config friendly, i.e., no mutually exclusive groups to
allow options overwriting.
""" """
from argparse import FileType, OPTIONAL, ZERO_OR_MORE, SUPPRESS from textwrap import dedent, wrap
#noinspection PyCompatibility
from argparse import (RawDescriptionHelpFormatter, FileType,
OPTIONAL, ZERO_OR_MORE, SUPPRESS)
from . import __doc__ from . import __doc__
from . import __version__ from . import __version__
from .compat import is_windows from .plugins.builtin import BuiltinAuthPlugin
from .sessions import DEFAULT_SESSIONS_DIR, Session from .plugins import plugin_manager
from .sessions import DEFAULT_SESSIONS_DIR
from .output import AVAILABLE_STYLES, DEFAULT_STYLE from .output import AVAILABLE_STYLES, DEFAULT_STYLE
from .input import (Parser, AuthCredentialsArgType, KeyValueArgType, from .input import (Parser, AuthCredentialsArgType, KeyValueArgType,
SEP_PROXY, SEP_CREDENTIALS, SEP_GROUP_ITEMS, SEP_PROXY, SEP_CREDENTIALS, SEP_GROUP_ITEMS,
OUT_REQ_HEAD, OUT_REQ_BODY, OUT_RESP_HEAD, OUT_REQ_HEAD, OUT_REQ_BODY, OUT_RESP_HEAD,
OUT_RESP_BODY, OUTPUT_OPTIONS, OUT_RESP_BODY, OUTPUT_OPTIONS, OUTPUT_OPTIONS_DEFAULT,
PRETTY_MAP, PRETTY_STDOUT_TTY_ONLY, RegexValidator) PRETTY_MAP, PRETTY_STDOUT_TTY_ONLY, SessionNameValidator)
def _(text): class HTTPieHelpFormatter(RawDescriptionHelpFormatter):
"""Normalize whitespace.""" """A nicer help formatter.
return ' '.join(text.strip().split())
Help for arguments can be indented and contain new lines.
It will be de-dented and arguments in the help
will be separated by a blank line for better readability.
"""
def __init__(self, max_help_position=6, *args, **kwargs):
# A smaller indent for args help.
kwargs['max_help_position'] = max_help_position
super(HTTPieHelpFormatter, self).__init__(*args, **kwargs)
def _split_lines(self, text, width):
text = dedent(text).strip() + '\n\n'
return text.splitlines()
parser = Parser( parser = Parser(
formatter_class=HTTPieHelpFormatter,
description='%s <http://httpie.org>' % __doc__.strip(), description='%s <http://httpie.org>' % __doc__.strip(),
epilog='For every --option there is a --no-option' epilog=dedent("""
' that reverts the option to its default value.\n\n' For every --OPTION there is also a --no-OPTION that reverts OPTION
'Suggestions and bug reports are greatly appreciated:\n' to its default value.
'https://github.com/jkbr/httpie/issues'
Suggestions and bug reports are greatly appreciated:
https://github.com/jkbr/httpie/issues
""")
) )
############################################################################### #######################################################################
# Positional arguments. # Positional arguments.
############################################################################### #######################################################################
positional = parser.add_argument_group( positional = parser.add_argument_group(
title='Positional arguments', title='Positional arguments',
description=_(''' description=dedent("""
These arguments come after any flags and in the These arguments come after any flags and in the order they are listed here.
order they are listed here. Only URL is required. Only URL is required.
''')
""")
) )
positional.add_argument( positional.add_argument(
'method', metavar='METHOD', 'method',
metavar='METHOD',
nargs=OPTIONAL, nargs=OPTIONAL,
default=None, default=None,
help=_(''' help="""
The HTTP method to be used for the request The HTTP method to be used for the request (GET, POST, PUT, DELETE, ...).
(GET, POST, PUT, DELETE, PATCH, ...).
If this argument is omitted, then HTTPie This argument can be omitted in which case HTTPie will use POST if there
will guess the HTTP method. If there is some is some data to be sent, otherwise GET:
data to be sent, then it will be POST, otherwise GET.
''') $ http example.org # => GET
$ http example.org hello=world # => POST
"""
) )
positional.add_argument( positional.add_argument(
'url', metavar='URL', 'url',
help=_(''' metavar='URL',
The protocol defaults to http:// if the help="""
URL does not include one. The scheme defaults to 'http://' if the URL does not include one.
''')
"""
) )
positional.add_argument( positional.add_argument(
'items', metavar='REQUEST ITEM', 'items',
metavar='REQUEST ITEM',
nargs=ZERO_OR_MORE, nargs=ZERO_OR_MORE,
type=KeyValueArgType(*SEP_GROUP_ITEMS), type=KeyValueArgType(*SEP_GROUP_ITEMS),
help=_(''' help=r"""
A key-value pair whose type is defined by the Optional key-value pairs to be included in the request. The separator used
separator used. It can be an HTTP header (header:value), determines the type:
a data field to be used in the request body (field_name=value),
a raw JSON data field (field_name:=value), ':' HTTP headers:
a query parameter (name==value),
or a file field (field_name@/path/to/file). Referer:http://httpie.org Cookie:foo=bar User-Agent:bacon/1.0
You can use a backslash to escape a colliding
separator in the field name. '==' URL parameters to be appended to the request URI:
''')
search==httpie
'=' Data fields to be serialized into a JSON object (with --json, -j)
or form data (with --form, -f):
name=HTTPie language=Python description='CLI HTTP client'
'@' Form file fields (only with --form, -f):
cs@~/Documents/CV.pdf
':=' Non-string JSON data fields (only with --json, -j):
awesome:=true amount:=42 colors:='["red", "green", "blue"]'
You can use a backslash to escape a colliding separator in the field name:
field-name-with\:colon=value
"""
) )
############################################################################### #######################################################################
# Content type. # Content type.
############################################################################### #######################################################################
content_type = parser.add_argument_group( content_type = parser.add_argument_group(
title='Predefined content types', title='Predefined content types',
@ -90,117 +138,144 @@ content_type = parser.add_argument_group(
) )
content_type.add_argument( content_type.add_argument(
'--json', '-j', action='store_true', '--json', '-j',
help=_(''' action='store_true',
(default) Data items from the command help="""
line are serialized as a JSON object. (default) Data items from the command line are serialized as a JSON object.
The Content-Type and Accept headers The Content-Type and Accept headers are set to application/json
are set to application/json (if not specified). (if not specified).
''')
"""
) )
content_type.add_argument( content_type.add_argument(
'--form', '-f', action='store_true', '--form', '-f',
help=_(''' action='store_true',
Data items from the command line are serialized as form fields. help="""
The Content-Type is set to application/x-www-form-urlencoded Data items from the command line are serialized as form fields.
(if not specified).
The presence of any file fields results The Content-Type is set to application/x-www-form-urlencoded (if not
in a multipart/form-data request. specified). The presence of any file fields results in a
''') multipart/form-data request.
"""
) )
############################################################################### #######################################################################
# Output processing # Output processing
############################################################################### #######################################################################
output_processing = parser.add_argument_group(title='Output processing') output_processing = parser.add_argument_group(title='Output processing')
output_processing.add_argument( output_processing.add_argument(
'--output', '-o', type=FileType('w+b'), '--pretty',
metavar='FILE', dest='prettify',
help=SUPPRESS if not is_windows else _( default=PRETTY_STDOUT_TTY_ONLY,
''' choices=sorted(PRETTY_MAP.keys()),
Save output to FILE. help="""
This option is a replacement for piping output to FILE, Controls output processing. The value can be "none" to not prettify
which would on Windows result in corrupted data the output (default for redirected output), "all" to apply both colors
being saved. and formatting (default for terminal output), "colors", or "format".
''' """
)
output_processing.add_argument(
'--style', '-s',
dest='style',
metavar='STYLE',
default=DEFAULT_STYLE,
choices=AVAILABLE_STYLES,
help="""
Output coloring style (default is "{default}"). On of:
{available}
For this option to work properly, please make sure that the $TERM
environment variable is set to "xterm-256color" or similar
(e.g., via `export TERM=xterm-256color' in your ~/.bashrc).
"""
.format(
default=DEFAULT_STYLE,
available='\n'.join(
'{0: >20}'.format(line.strip())
for line in
wrap(' '.join(sorted(AVAILABLE_STYLES)), 60)
),
) )
) )
output_processing.add_argument(
'--pretty', dest='prettify', default=PRETTY_STDOUT_TTY_ONLY,
choices=sorted(PRETTY_MAP.keys()),
help=_('''
Controls output processing. The value can be "none" to not prettify
the output (default for redirected output), "all" to apply both colors
and formatting
(default for terminal output), "colors", or "format".
''')
)
output_processing.add_argument(
'--style', '-s', dest='style', default=DEFAULT_STYLE, metavar='STYLE',
choices=AVAILABLE_STYLES,
help=_('''
Output coloring style. One of %s. Defaults to "%s".
For this option to work properly, please make sure that the
$TERM environment variable is set to "xterm-256color" or similar
(e.g., via `export TERM=xterm-256color' in your ~/.bashrc).
''') % (', '.join(sorted(AVAILABLE_STYLES)), DEFAULT_STYLE)
)
############################################################################### #######################################################################
# Output options # Output options
############################################################################### #######################################################################
output_options = parser.add_argument_group(title='Output options') output_options = parser.add_argument_group(title='Output options')
output_options.add_argument( output_options.add_argument(
'--print', '-p', dest='output_options', metavar='WHAT', '--print', '-p',
help=_(''' dest='output_options',
String specifying what the output should contain: metavar='WHAT',
"{request_headers}" stands for the request headers, and help="""
"{request_body}" for the request body. String specifying what the output should contain:
"{response_headers}" stands for the response headers and
"{response_body}" for response the body. '{req_head}' request headers
The default behaviour is "hb" (i.e., the response '{req_body}' request body
headers and body is printed), if standard output is not redirected. '{res_head}' response headers
If the output is piped to another program or to a file, '{res_body}' response body
then only the body is printed by default.
'''.format(request_headers=OUT_REQ_HEAD, The default behaviour is '{default}' (i.e., the response headers and body
request_body=OUT_REQ_BODY, is printed), if standard output is not redirected. If the output is piped
response_headers=OUT_RESP_HEAD, to another program or to a file, then only the response body is printed
response_body=OUT_RESP_BODY,)) by default.
"""
.format(
req_head=OUT_REQ_HEAD,
req_body=OUT_REQ_BODY,
res_head=OUT_RESP_HEAD,
res_body=OUT_RESP_BODY,
default=OUTPUT_OPTIONS_DEFAULT,
)
) )
output_options.add_argument( output_options.add_argument(
'--verbose', '-v', dest='output_options', '--verbose', '-v',
action='store_const', const=''.join(OUTPUT_OPTIONS), dest='output_options',
help=_(''' action='store_const',
Print the whole request as well as the response. const=''.join(OUTPUT_OPTIONS),
Shortcut for --print={0}. help="""
'''.format(''.join(OUTPUT_OPTIONS))) Print the whole request as well as the response. Shortcut for --print={0}.
"""
.format(''.join(OUTPUT_OPTIONS))
) )
output_options.add_argument( output_options.add_argument(
'--headers', '-h', dest='output_options', '--headers', '-h',
action='store_const', const=OUT_RESP_HEAD, dest='output_options',
help=_(''' action='store_const',
Print only the response headers. const=OUT_RESP_HEAD,
Shortcut for --print={0}. help="""
'''.format(OUT_RESP_HEAD)) Print only the response headers. Shortcut for --print={0}.
"""
.format(OUT_RESP_HEAD)
) )
output_options.add_argument( output_options.add_argument(
'--body', '-b', dest='output_options', '--body', '-b',
action='store_const', const=OUT_RESP_BODY, dest='output_options',
help=_(''' action='store_const',
Print only the response body. const=OUT_RESP_BODY,
Shortcut for --print={0}. help="""
'''.format(OUT_RESP_BODY)) Print only the response body. Shortcut for --print={0}.
"""
.format(OUT_RESP_BODY)
) )
output_options.add_argument( output_options.add_argument(
'--stream', '-S', action='store_true', default=False, '--stream', '-S',
help=_(''' action='store_true',
default=False,
help="""
Always stream the output by line, i.e., behave like `tail -f'. Always stream the output by line, i.e., behave like `tail -f'.
Without --stream and with --pretty (either set or implied), Without --stream and with --pretty (either set or implied),
@ -212,141 +287,248 @@ output_options.add_argument(
It is useful also without --pretty: It ensures that the output is flushed It is useful also without --pretty: It ensures that the output is flushed
more often and in smaller chunks. more often and in smaller chunks.
''') """
)
output_options.add_argument(
'--output', '-o',
type=FileType('a+b'),
dest='output_file',
metavar='FILE',
help="""
Save output to FILE. If --download is set, then only the response body is
saved to the file. Other parts of the HTTP exchange are printed to stderr.
"""
)
output_options.add_argument(
'--download', '-d',
action='store_true',
default=False,
help="""
Do not print the response body to stdout. Rather, download it and store it
in a file. The filename is guessed unless specified with --output
[filename]. This action is similar to the default behaviour of wget.
"""
)
output_options.add_argument(
'--continue', '-c',
dest='download_resume',
action='store_true',
default=False,
help="""
Resume an interrupted download. Note that the --output option needs to be
specified as well.
"""
) )
############################################################################### #######################################################################
# Sessions # Sessions
############################################################################### #######################################################################
sessions = parser.add_argument_group(title='Sessions')\ sessions = parser.add_argument_group(title='Sessions')\
.add_mutually_exclusive_group(required=False) .add_mutually_exclusive_group(required=False)
session_name_validator = SessionNameValidator(
'Session name contains invalid characters.'
)
sessions.add_argument( sessions.add_argument(
'--session', metavar='SESSION_NAME', type=RegexValidator( '--session',
Session.VALID_NAME_PATTERN, metavar='SESSION_NAME_OR_PATH',
'Session name contains invalid characters.' type=session_name_validator,
), help="""
help=_(''' Create, or reuse and update a session. Within a session, custom headers,
Create, or reuse and update a session. auth credential, as well as any cookies sent by the server persist between
Within a session, custom headers, auth credential, as well as any requests.
cookies sent by the server persist between requests.
Session files are stored in %s/<HOST>/<SESSION_NAME>.json. Session files are stored in:
''' % DEFAULT_SESSIONS_DIR)
{session_dir}/<HOST>/<SESSION_NAME>.json.
"""
.format(session_dir=DEFAULT_SESSIONS_DIR)
) )
sessions.add_argument( sessions.add_argument(
'--session-read-only', metavar='SESSION_NAME', '--session-read-only',
help=_(''' metavar='SESSION_NAME_OR_PATH',
Create or read a session without updating it form the type=session_name_validator,
request/response exchange. help="""
''') Create or read a session without updating it form the request/response
exchange.
"""
) )
############################################################################### #######################################################################
# Authentication # Authentication
############################################################################### #######################################################################
# ``requests.request`` keyword arguments. # ``requests.request`` keyword arguments.
auth = parser.add_argument_group(title='Authentication') auth = parser.add_argument_group(title='Authentication')
auth.add_argument( auth.add_argument(
'--auth', '-a', metavar='USER[:PASS]', '--auth', '-a',
metavar='USER[:PASS]',
type=AuthCredentialsArgType(SEP_CREDENTIALS), type=AuthCredentialsArgType(SEP_CREDENTIALS),
help=_(''' help="""
If only the username is provided (-a username), If only the username is provided (-a username), HTTPie will prompt
HTTPie will prompt for the password. for the password.
'''),
""",
) )
_auth_plugins = plugin_manager.get_auth_plugins()
auth.add_argument( auth.add_argument(
'--auth-type', choices=['basic', 'digest'], default='basic', '--auth-type',
help=_(''' choices=[plugin.auth_type for plugin in _auth_plugins],
The authentication mechanism to be used. default=_auth_plugins[0].auth_type,
Defaults to "basic". help="""
''') The authentication mechanism to be used. Defaults to "{default}".
{types}
"""
.format(default=_auth_plugins[0].auth_type, types='\n '.join(
'"{type}": {name}{package}{description}'.format(
type=plugin.auth_type,
name=plugin.name,
package=(
'' if issubclass(plugin, BuiltinAuthPlugin)
else ' (provided by %s)' % plugin.package_name
),
description=(
'' if not plugin.description else
'\n ' + ('\n '.join(wrap(plugin.description)))
)
)
for plugin in _auth_plugins
)),
) )
#######################################################################
# Network # Network
############################################# #######################################################################
network = parser.add_argument_group(title='Network') network = parser.add_argument_group(title='Network')
network.add_argument( network.add_argument(
'--proxy', default=[], action='append', metavar='PROTOCOL:HOST', '--proxy',
default=[],
action='append',
metavar='PROTOCOL:HOST',
type=KeyValueArgType(SEP_PROXY), type=KeyValueArgType(SEP_PROXY),
help=_(''' help="""
String mapping protocol to the URL of the proxy String mapping protocol to the URL of the proxy (e.g. http:foo.bar:3128).
(e.g. http:foo.bar:3128). You can specify multiple You can specify multiple proxies with different protocols.
proxies with different protocols.
''') """
) )
network.add_argument( network.add_argument(
'--follow', default=False, action='store_true', '--follow',
help=_(''' default=False,
Set this flag if full redirects are allowed action='store_true',
(e.g. re-POST-ing of data at new ``Location``) help="""
''') Set this flag if full redirects are allowed (e.g. re-POST-ing of data at
new Location).
"""
) )
network.add_argument( network.add_argument(
'--verify', default='yes', '--verify',
help=_(''' default='yes',
Set to "no" to skip checking the host\'s SSL certificate. help="""
You can also pass the path to a CA_BUNDLE Set to "no" to skip checking the host's SSL certificate. You can also pass
file for private certs. You can also set the path to a CA_BUNDLE file for private certs. You can also set the
the REQUESTS_CA_BUNDLE environment variable. REQUESTS_CA_BUNDLE environment variable. Defaults to "yes".
Defaults to "yes".
''') """
) )
network.add_argument( network.add_argument(
'--timeout', type=float, default=30, metavar='SECONDS', '--timeout',
help=_(''' type=float,
The connection timeout of the request in seconds. default=30,
The default value is 30 seconds. metavar='SECONDS',
''') help="""
The connection timeout of the request in seconds. The default value is
30 seconds.
"""
) )
network.add_argument( network.add_argument(
'--check-status', default=False, action='store_true', '--check-status',
help=_(''' default=False,
By default, HTTPie exits with 0 when no network or other fatal action='store_true',
errors occur. help="""
By default, HTTPie exits with 0 when no network or other fatal errors
occur. This flag instructs HTTPie to also check the HTTP status code and
exit with an error if the status indicates one.
This flag instructs HTTPie to also check the HTTP status code and When the server replies with a 4xx (Client Error) or 5xx (Server Error)
exit with an error if the status indicates one. status code, HTTPie exits with 4 or 5 respectively. If the response is a
3xx (Redirect) and --follow hasn't been set, then the exit status is 3.
Also an error message is written to stderr if stdout is redirected.
When the server replies with a 4xx (Client Error) or 5xx """
(Server Error) status code, HTTPie exits with 4 or 5 respectively.
If the response is a 3xx (Redirect) and --follow
hasn't been set, then the exit status is 3.
Also an error message is written to stderr if stdout is redirected.
''')
) )
############################################################################### #######################################################################
# Troubleshooting # Troubleshooting
############################################################################### #######################################################################
troubleshooting = parser.add_argument_group(title='Troubleshooting') troubleshooting = parser.add_argument_group(title='Troubleshooting')
troubleshooting.add_argument(
'--ignore-stdin',
action='store_true',
default=False,
help="""
Do not attempt to read stdin.
"""
)
troubleshooting.add_argument( troubleshooting.add_argument(
'--help', '--help',
action='help', default=SUPPRESS, action='help',
help='Show this help message and exit' default=SUPPRESS,
help="""
Show this help message and exit.
"""
) )
troubleshooting.add_argument( troubleshooting.add_argument(
'--version', action='version', version=__version__) '--version',
troubleshooting.add_argument( action='version',
'--traceback', action='store_true', default=False, version=__version__,
help='Prints exception traceback should one occur.' help="""
Show version and exit.
"""
) )
troubleshooting.add_argument( troubleshooting.add_argument(
'--debug', action='store_true', default=False, '--traceback',
help=_(''' action='store_true',
Prints exception traceback should one occur, and also other default=False,
information that is useful for debugging HTTPie itself and help="""
for bug reports. Prints exception traceback should one occur.
''')
"""
)
troubleshooting.add_argument(
'--debug',
action='store_true',
default=False,
help="""
Prints exception traceback should one occur, and also other information
that is useful for debugging HTTPie itself and for reporting bugs.
"""
) )

View File

@ -7,6 +7,7 @@ import requests.auth
from . import sessions from . import sessions
from . import __version__ from . import __version__
from .plugins import plugin_manager
FORM = 'application/x-www-form-urlencoded; charset=utf-8' FORM = 'application/x-www-form-urlencoded; charset=utf-8'
@ -14,6 +15,12 @@ JSON = 'application/json; charset=utf-8'
DEFAULT_UA = 'HTTPie/%s' % __version__ DEFAULT_UA = 'HTTPie/%s' % __version__
class HTTPie(object):
def __init__(self, env, plugin_manager):
pass
def get_response(args, config_dir): def get_response(args, config_dir):
"""Send the request and return a `request.Response`.""" """Send the request and return a `request.Response`."""
@ -27,9 +34,10 @@ def get_response(args, config_dir):
response = requests.request(**requests_kwargs) response = requests.request(**requests_kwargs)
else: else:
response = sessions.get_response( response = sessions.get_response(
args=args,
config_dir=config_dir, config_dir=config_dir,
name=args.session or args.session_read_only, session_name=args.session or args.session_read_only,
request_kwargs=requests_kwargs, requests_kwargs=requests_kwargs,
read_only=bool(args.session_read_only), read_only=bool(args.session_read_only),
) )
@ -44,9 +52,10 @@ def get_requests_kwargs(args):
} }
auto_json = args.data and not args.form auto_json = args.data and not args.form
# FIXME: Accept is set to JSON with `http url @./file.txt`.
if args.json or auto_json: if args.json or auto_json:
implicit_headers['Accept'] = 'application/json' implicit_headers['Accept'] = 'application/json'
if args.data: if args.json or (auto_json and args.data):
implicit_headers['Content-Type'] = JSON implicit_headers['Content-Type'] = JSON
if isinstance(args.data, dict): if isinstance(args.data, dict):
@ -68,10 +77,8 @@ def get_requests_kwargs(args):
credentials = None credentials = None
if args.auth: if args.auth:
credentials = { auth_plugin = plugin_manager.get_auth_plugin(args.auth_type)()
'basic': requests.auth.HTTPBasicAuth, credentials = auth_plugin.get_auth(args.auth.key, args.auth.value)
'digest': requests.auth.HTTPDigestAuth,
}[args.auth_type](args.auth.key, args.auth.value)
kwargs = { kwargs = {
'stream': True, 'stream': True,

View File

@ -12,7 +12,8 @@ from requests.compat import (
) )
try: try:
#noinspection PyUnresolvedReferences,PyCompatibility
from urllib.parse import urlsplit from urllib.parse import urlsplit
except ImportError: except ImportError:
#noinspection PyUnresolvedReferences,PyCompatibility
from urlparse import urlsplit from urlparse import urlsplit

View File

@ -16,9 +16,8 @@ DEFAULT_CONFIG_DIR = os.environ.get(
class BaseConfigDict(dict): class BaseConfigDict(dict):
name = None name = None
help = None helpurl = None
about = None about = None
directory = DEFAULT_CONFIG_DIR directory = DEFAULT_CONFIG_DIR
def __init__(self, directory=None, *args, **kwargs): def __init__(self, directory=None, *args, **kwargs):
@ -29,18 +28,24 @@ class BaseConfigDict(dict):
def __getattr__(self, item): def __getattr__(self, item):
return self[item] return self[item]
@property def _get_path(self):
def path(self): """Return the config file path without side-effects."""
try:
os.makedirs(self.directory, mode=0o700)
except OSError as e:
if e.errno != errno.EEXIST:
raise
return os.path.join(self.directory, self.name + '.json') return os.path.join(self.directory, self.name + '.json')
@property
def path(self):
"""Return the config file path creating basedir, if needed."""
path = self._get_path()
try:
os.makedirs(os.path.dirname(path), mode=0o700)
except OSError as e:
if e.errno != errno.EEXIST:
raise
return path
@property @property
def is_new(self): def is_new(self):
return not os.path.exists(self.path) return not os.path.exists(self._get_path())
def load(self): def load(self):
try: try:
@ -61,8 +66,8 @@ class BaseConfigDict(dict):
self['__meta__'] = { self['__meta__'] = {
'httpie': __version__ 'httpie': __version__
} }
if self.help: if self.helpurl:
self['__meta__']['help'] = self.help self['__meta__']['help'] = self.helpurl
if self.about: if self.about:
self['__meta__']['about'] = self.about self['__meta__']['about'] = self.about
@ -82,7 +87,7 @@ class BaseConfigDict(dict):
class Config(BaseConfigDict): class Config(BaseConfigDict):
name = 'config' name = 'config'
help = 'https://github.com/jkbr/httpie#config' helpurl = 'https://github.com/jkbr/httpie#config'
about = 'HTTPie configuration file' about = 'HTTPie configuration file'
DEFAULTS = { DEFAULTS = {

View File

@ -18,12 +18,13 @@ from httpie import __version__ as httpie_version
from requests import __version__ as requests_version from requests import __version__ as requests_version
from pygments import __version__ as pygments_version from pygments import __version__ as pygments_version
from .cli import parser
from .compat import str, is_py3 from .compat import str, is_py3
from .client import get_response from .client import get_response
from .downloads import Download
from .models import Environment from .models import Environment
from .output import build_output_stream, write, write_with_colors_win_p3k from .output import build_output_stream, write, write_with_colors_win_py3
from . import ExitStatus from . import ExitStatus
from .plugins import plugin_manager
def get_exit_status(http_status, follow=False): def get_exit_status(http_status, follow=False):
@ -57,13 +58,16 @@ def main(args=sys.argv[1:], env=Environment()):
Return exit status code. Return exit status code.
""" """
plugin_manager.load_installed_plugins()
from .cli import parser
if env.config.default_options: if env.config.default_options:
args = env.config.default_options + args args = env.config.default_options + args
def error(msg, *args, **kwargs): def error(msg, *args, **kwargs):
msg = msg % args msg = msg % args
level = kwargs.get('level', 'error') level = kwargs.get('level', 'error')
env.stderr.write('http: %s: %s\n' % (level, msg)) env.stderr.write('\nhttp: %s: %s\n' % (level, msg))
debug = '--debug' in args debug = '--debug' in args
traceback = debug or '--traceback' in args traceback = debug or '--traceback' in args
@ -74,13 +78,28 @@ def main(args=sys.argv[1:], env=Environment()):
if args == ['--debug']: if args == ['--debug']:
return exit_status return exit_status
download = None
try: try:
args = parser.parse_args(args=args, env=env) args = parser.parse_args(args=args, env=env)
if args.download:
args.follow = True # --download implies --follow.
download = Download(
output_file=args.output_file,
progress_file=env.stderr,
resume=args.download_resume
)
download.pre_request(args.headers)
response = get_response(args, config_dir=env.config.directory) response = get_response(args, config_dir=env.config.directory)
if args.check_status: if args.check_status or download:
exit_status = get_exit_status(response.status_code, args.follow)
exit_status = get_exit_status(
http_status=response.status_code,
follow=args.follow
)
if not env.stdout_isatty and exit_status != ExitStatus.OK: if not env.stdout_isatty and exit_status != ExitStatus.OK:
error('HTTP %s %s', error('HTTP %s %s',
@ -89,25 +108,44 @@ def main(args=sys.argv[1:], env=Environment()):
level='warning') level='warning')
write_kwargs = { write_kwargs = {
'stream': build_output_stream(args, env, 'stream': build_output_stream(
response.request, args, env, response.request, response),
response),
# This will in fact be `stderr` with `--download`
'outfile': env.stdout, 'outfile': env.stdout,
'flush': env.stdout_isatty or args.stream 'flush': env.stdout_isatty or args.stream
} }
try: try:
if env.is_windows and is_py3 and 'colors' in args.prettify: if env.is_windows and is_py3 and 'colors' in args.prettify:
write_with_colors_win_p3k(**write_kwargs) write_with_colors_win_py3(**write_kwargs)
else: else:
write(**write_kwargs) write(**write_kwargs)
if download and exit_status == ExitStatus.OK:
# Response body download.
download_stream, download_to = download.start(response)
write(
stream=download_stream,
outfile=download_to,
flush=False,
)
download.finish()
if download.interrupted:
exit_status = ExitStatus.ERROR
error('Incomplete download: size=%d; downloaded=%d' % (
download.status.total_size,
download.status.downloaded
))
except IOError as e: except IOError as e:
if not traceback and e.errno == errno.EPIPE: if not traceback and e.errno == errno.EPIPE:
# Ignore broken pipes unless --traceback. # Ignore broken pipes unless --traceback.
env.stderr.write('\n') env.stderr.write('\n')
else: else:
raise raise
except (KeyboardInterrupt, SystemExit): except (KeyboardInterrupt, SystemExit):
if traceback: if traceback:
raise raise
@ -126,4 +164,8 @@ def main(args=sys.argv[1:], env=Environment()):
error('%s: %s', type(e).__name__, str(e)) error('%s: %s', type(e).__name__, str(e))
exit_status = ExitStatus.ERROR exit_status = ExitStatus.ERROR
finally:
if download and not download.finished:
download.failed()
return exit_status return exit_status

427
httpie/downloads.py Normal file
View File

@ -0,0 +1,427 @@
# coding=utf-8
"""
Download mode implementation.
"""
from __future__ import division
import os
import re
import sys
import mimetypes
import threading
from time import sleep, time
from mailbox import Message
from .output import RawStream
from .models import HTTPResponse
from .utils import humanize_bytes
from .compat import urlsplit
PARTIAL_CONTENT = 206
CLEAR_LINE = '\r\033[K'
PROGRESS = (
'{percentage: 6.2f} %'
' {downloaded: >10}'
' {speed: >10}/s'
' {eta: >8} ETA'
)
PROGRESS_NO_CONTENT_LENGTH = '{downloaded: >10} {speed: >10}/s'
SUMMARY = 'Done. {downloaded} in {time:0.5f}s ({speed}/s)\n'
SPINNER = '|/-\\'
class ContentRangeError(ValueError):
pass
def parse_content_range(content_range, resumed_from):
"""
Parse and validate Content-Range header.
<http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html>
:param content_range: the value of a Content-Range response header
eg. "bytes 21010-47021/47022"
:param resumed_from: first byte pos. from the Range request header
:return: total size of the response body when fully downloaded.
"""
if content_range is None:
raise ContentRangeError('Missing Content-Range')
pattern = (
'^bytes (?P<first_byte_pos>\d+)-(?P<last_byte_pos>\d+)'
'/(\*|(?P<instance_length>\d+))$'
)
match = re.match(pattern, content_range)
if not match:
raise ContentRangeError(
'Invalid Content-Range format %r' % content_range)
content_range_dict = match.groupdict()
first_byte_pos = int(content_range_dict['first_byte_pos'])
last_byte_pos = int(content_range_dict['last_byte_pos'])
instance_length = (
int(content_range_dict['instance_length'])
if content_range_dict['instance_length']
else None
)
# "A byte-content-range-spec with a byte-range-resp-spec whose
# last- byte-pos value is less than its first-byte-pos value,
# or whose instance-length value is less than or equal to its
# last-byte-pos value, is invalid. The recipient of an invalid
# byte-content-range- spec MUST ignore it and any content
# transferred along with it."
if (first_byte_pos >= last_byte_pos
or (instance_length is not None
and instance_length <= last_byte_pos)):
raise ContentRangeError(
'Invalid Content-Range returned: %r' % content_range)
if (first_byte_pos != resumed_from
or (instance_length is not None
and last_byte_pos + 1 != instance_length)):
# Not what we asked for.
raise ContentRangeError(
'Unexpected Content-Range returned (%r)'
' for the requested Range ("bytes=%d-")'
% (content_range, resumed_from)
)
return last_byte_pos + 1
def filename_from_content_disposition(content_disposition):
"""
Extract and validate filename from a Content-Disposition header.
:param content_disposition: Content-Disposition value
:return: the filename if present and valid, otherwise `None`
"""
# attachment; filename=jkbr-httpie-0.4.1-20-g40bd8f6.tar.gz
msg = Message('Content-Disposition: %s' % content_disposition)
filename = msg.get_filename()
if filename:
# Basic sanitation.
filename = os.path.basename(filename).lstrip('.').strip()
if filename:
return filename
def filename_from_url(url, content_type):
fn = urlsplit(url).path.rstrip('/')
fn = os.path.basename(fn) if fn else 'index'
if '.' not in fn and content_type:
content_type = content_type.split(';')[0]
if content_type == 'text/plain':
# mimetypes returns '.ksh'
ext = '.txt'
else:
ext = mimetypes.guess_extension(content_type)
if ext == '.htm': # Python 3
ext = '.html'
if ext:
fn += ext
return fn
def get_unique_filename(fn, exists=os.path.exists):
attempt = 0
while True:
suffix = '-' + str(attempt) if attempt > 0 else ''
if not exists(fn + suffix):
return fn + suffix
attempt += 1
class Download(object):
def __init__(self, output_file=None,
resume=False, progress_file=sys.stderr):
"""
:param resume: Should the download resume if partial download
already exists.
:type resume: bool
:param output_file: The file to store response body in. If not
provided, it will be guessed from the response.
:type output_file: file
:param progress_file: Where to report download progress.
:type progress_file: file
"""
self._output_file = output_file
self._resume = resume
self._resumed_from = 0
self.finished = False
self.status = Status()
self._progress_reporter = ProgressReporterThread(
status=self.status,
output=progress_file
)
def pre_request(self, request_headers):
"""Called just before the HTTP request is sent.
Might alter `request_headers`.
:type request_headers: dict
"""
# Disable content encoding so that we can resume, etc.
request_headers['Accept-Encoding'] = None
if self._resume:
bytes_have = os.path.getsize(self._output_file.name)
if bytes_have:
# Set ``Range`` header to resume the download
# TODO: Use "If-Range: mtime" to make sure it's fresh?
request_headers['Range'] = 'bytes=%d-' % bytes_have
self._resumed_from = bytes_have
def start(self, response):
"""
Initiate and return a stream for `response` body with progress
callback attached. Can be called only once.
:param response: Initiated response object with headers already fetched
:type response: requests.models.Response
:return: RawStream, output_file
"""
assert not self.status.time_started
try:
total_size = int(response.headers['Content-Length'])
except (KeyError, ValueError, TypeError):
total_size = None
if self._output_file:
if self._resume and response.status_code == PARTIAL_CONTENT:
total_size = parse_content_range(
response.headers.get('Content-Range'),
self._resumed_from
)
else:
self._resumed_from = 0
try:
self._output_file.seek(0)
self._output_file.truncate()
except IOError:
pass # stdout
else:
# TODO: Should the filename be taken from response.history[0].url?
# Output file not specified. Pick a name that doesn't exist yet.
fn = None
if 'Content-Disposition' in response.headers:
fn = filename_from_content_disposition(
response.headers['Content-Disposition'])
if not fn:
fn = filename_from_url(
url=response.url,
content_type=response.headers.get('Content-Type'),
)
self._output_file = open(get_unique_filename(fn), mode='a+b')
self.status.started(
resumed_from=self._resumed_from,
total_size=total_size
)
stream = RawStream(
msg=HTTPResponse(response),
with_headers=False,
with_body=True,
on_body_chunk_downloaded=self.chunk_downloaded,
chunk_size=1024 * 8
)
self._progress_reporter.output.write(
'Downloading %sto "%s"\n' % (
(humanize_bytes(total_size) + ' '
if total_size is not None
else ''),
self._output_file.name
)
)
self._progress_reporter.start()
return stream, self._output_file
def finish(self):
assert not self.finished
self.finished = True
self.status.finished()
def failed(self):
self._progress_reporter.stop()
@property
def interrupted(self):
return (
self.finished
and self.status.total_size
and self.status.total_size != self.status.downloaded
)
def chunk_downloaded(self, chunk):
"""
A download progress callback.
:param chunk: A chunk of response body data that has just
been downloaded and written to the output.
:type chunk: bytes
"""
self.status.chunk_downloaded(len(chunk))
class Status(object):
"""Holds details about the downland status."""
def __init__(self):
self.downloaded = 0
self.total_size = None
self.resumed_from = 0
self.time_started = None
self.time_finished = None
def started(self, resumed_from=0, total_size=None):
assert self.time_started is None
if total_size is not None:
self.total_size = total_size
self.downloaded = self.resumed_from = resumed_from
self.time_started = time()
def chunk_downloaded(self, size):
assert self.time_finished is None
self.downloaded += size
@property
def has_finished(self):
return self.time_finished is not None
def finished(self):
assert self.time_started is not None
assert self.time_finished is None
self.time_finished = time()
class ProgressReporterThread(threading.Thread):
"""
Reports download progress based on its status.
Uses threading to periodically update the status (speed, ETA, etc.).
"""
def __init__(self, status, output, tick=.1, update_interval=1):
"""
:type status: Status
:type output: file
"""
super(ProgressReporterThread, self).__init__()
self.status = status
self.output = output
self._tick = tick
self._update_interval = update_interval
self._spinner_pos = 0
self._status_line = ''
self._prev_bytes = 0
self._prev_time = time()
self._should_stop = threading.Event()
def stop(self):
"""Stop reporting on next tick."""
self._should_stop.set()
def run(self):
while not self._should_stop.is_set():
if self.status.has_finished:
self.sum_up()
break
self.report_speed()
sleep(self._tick)
def report_speed(self):
now = time()
if now - self._prev_time >= self._update_interval:
downloaded = self.status.downloaded
try:
speed = ((downloaded - self._prev_bytes)
/ (now - self._prev_time))
except ZeroDivisionError:
speed = 0
if not self.status.total_size:
self._status_line = PROGRESS_NO_CONTENT_LENGTH.format(
downloaded=humanize_bytes(downloaded),
speed=humanize_bytes(speed),
)
else:
try:
percentage = downloaded / self.status.total_size * 100
except ZeroDivisionError:
percentage = 0
if not speed:
eta = '-:--:--'
else:
s = int((self.status.total_size - downloaded) / speed)
h, s = divmod(s, 60 * 60)
m, s = divmod(s, 60)
eta = '{0}:{1:0>2}:{2:0>2}'.format(h, m, s)
self._status_line = PROGRESS.format(
percentage=percentage,
downloaded=humanize_bytes(downloaded),
speed=humanize_bytes(speed),
eta=eta,
)
self._prev_time = now
self._prev_bytes = downloaded
self.output.write(
CLEAR_LINE
+ ' '
+ SPINNER[self._spinner_pos]
+ ' '
+ self._status_line
)
self.output.flush()
self._spinner_pos = (self._spinner_pos + 1
if self._spinner_pos + 1 != len(SPINNER)
else 0)
def sum_up(self):
actually_downloaded = (self.status.downloaded
- self.status.resumed_from)
time_taken = self.status.time_finished - self.status.time_started
self.output.write(CLEAR_LINE)
self.output.write(SUMMARY.format(
downloaded=humanize_bytes(actually_downloaded),
total=(self.status.total_size
and humanize_bytes(self.status.total_size)),
speed=humanize_bytes(actually_downloaded / time_taken),
time=time_taken,
))
self.output.flush()

View File

@ -6,8 +6,9 @@ import sys
import re import re
import json import json
import mimetypes import mimetypes
import getpass from getpass import getpass
from io import BytesIO from io import BytesIO
#noinspection PyCompatibility
from argparse import ArgumentParser, ArgumentTypeError, ArgumentError from argparse import ArgumentParser, ArgumentTypeError, ArgumentError
try: try:
@ -20,6 +21,7 @@ except ImportError:
from requests.structures import CaseInsensitiveDict from requests.structures import CaseInsensitiveDict
from .compat import urlsplit, str from .compat import urlsplit, str
from .sessions import VALID_SESSION_NAME_PATTERN
HTTP_POST = 'POST' HTTP_POST = 'POST'
@ -98,57 +100,106 @@ class Parser(ArgumentParser):
def parse_args(self, env, args=None, namespace=None): def parse_args(self, env, args=None, namespace=None):
self.env = env self.env = env
self.args, no_options = super(Parser, self)\
.parse_known_args(args, namespace)
args, no_options = super(Parser, self).parse_known_args(args, if self.args.debug:
namespace) self.args.traceback = True
self._apply_no_options(args, no_options) # Arguments processing and environment setup.
self._apply_no_options(no_options)
self._apply_config()
self._validate_download_options()
self._setup_standard_streams()
self._process_output_options()
self._process_pretty_options()
self._guess_method()
self._parse_items()
if not self.args.ignore_stdin and not env.stdin_isatty:
self._body_from_file(self.env.stdin)
if not (self.args.url.startswith((HTTP, HTTPS))):
# Default to 'https://' if invoked as `https args`.
scheme = HTTPS if self.env.progname == 'https' else HTTP
self.args.url = scheme + self.args.url
self._process_auth()
if not args.json and env.config.implicit_content_type == 'form': return self.args
args.form = True
if args.debug: # noinspection PyShadowingBuiltins
args.traceback = True def _print_message(self, message, file=None):
# Sneak in our stderr/stdout.
file = {
sys.stdout: self.env.stdout,
sys.stderr: self.env.stderr,
None: self.env.stderr
}.get(file, file)
if args.output: super(Parser, self)._print_message(message, file)
env.stdout = args.output
env.stdout_isatty = False
self._process_output_options(args, env) def _setup_standard_streams(self):
self._process_pretty_options(args, env) """
self._guess_method(args, env) Modify `env.stdout` and `env.stdout_isatty` based on args, if needed.
self._parse_items(args)
if not env.stdin_isatty: """
self._body_from_file(args, env.stdin) if not self.env.stdout_isatty and self.args.output_file:
self.error('Cannot use --output, -o with redirected output.')
if not (args.url.startswith(HTTP) or args.url.startswith(HTTPS)): # FIXME: Come up with a cleaner solution.
scheme = HTTPS if env.progname == 'https' else HTTP if self.args.download:
args.url = scheme + args.url
self._process_auth(args) if not self.env.stdout_isatty:
# Use stdout as tge download output file.
self.args.output_file = self.env.stdout
return args # With `--download`, we write everything that would normally go to
# `stdout` to `stderr` instead. Let's replace the stream so that
# we don't have to use many `if`s throughout the codebase.
# The response body will be treated separately.
self.env.stdout = self.env.stderr
self.env.stdout_isatty = self.env.stderr_isatty
def _process_auth(self, args): elif self.args.output_file:
url = urlsplit(args.url) # When not `--download`ing, then `--output` simply replaces
# `stdout`. The file is opened for appending, which isn't what
# we want in this case.
self.args.output_file.seek(0)
self.args.output_file.truncate()
if args.auth: self.env.stdout = self.args.output_file
if not args.auth.has_password(): self.env.stdout_isatty = False
def _apply_config(self):
if (not self.args.json
and self.env.config.implicit_content_type == 'form'):
self.args.form = True
def _process_auth(self):
"""
If only a username provided via --auth, then ask for a password.
Or, take credentials from the URL, if provided.
"""
url = urlsplit(self.args.url)
if self.args.auth:
if not self.args.auth.has_password():
# Stdin already read (if not a tty) so it's save to prompt. # Stdin already read (if not a tty) so it's save to prompt.
args.auth.prompt_password(url.netloc) if self.args.ignore_stdin:
self.error('Unable to prompt for passwords because'
' --ignore-stdin is set.')
self.args.auth.prompt_password(url.netloc)
elif url.username is not None: elif url.username is not None:
# Handle http://username:password@hostname/ # Handle http://username:password@hostname/
username, password = url.username, url.password username, password = url.username, url.password
args.auth = AuthCredentials( self.args.auth = AuthCredentials(
key=username, key=username,
value=password, value=password,
sep=SEP_CREDENTIALS, sep=SEP_CREDENTIALS,
orig=SEP_CREDENTIALS.join([username, password]) orig=SEP_CREDENTIALS.join([username, password])
) )
def _apply_no_options(self, args, no_options): def _apply_no_options(self, no_options):
"""For every `--no-OPTION` in `no_options`, set `args.OPTION` to """For every `--no-OPTION` in `no_options`, set `args.OPTION` to
its default value. This allows for un-setting of options, e.g., its default value. This allows for un-setting of options, e.g.,
specified in config. specified in config.
@ -165,7 +216,7 @@ class Parser(ArgumentParser):
inverted = '--' + option[5:] inverted = '--' + option[5:]
for action in self._actions: for action in self._actions:
if inverted in action.option_strings: if inverted in action.option_strings:
setattr(args, action.dest, action.default) setattr(self.args, action.dest, action.default)
break break
else: else:
invalid.append(option) invalid.append(option)
@ -174,123 +225,143 @@ class Parser(ArgumentParser):
msg = 'unrecognized arguments: %s' msg = 'unrecognized arguments: %s'
self.error(msg % ' '.join(invalid)) self.error(msg % ' '.join(invalid))
def _print_message(self, message, file=None): def _body_from_file(self, fd):
# Sneak in our stderr/stdout.
file = {
sys.stdout: self.env.stdout,
sys.stderr: self.env.stderr,
None: self.env.stderr
}.get(file, file)
super(Parser, self)._print_message(message, file)
def _body_from_file(self, args, fd):
"""There can only be one source of request data. """There can only be one source of request data.
Bytes are always read. Bytes are always read.
""" """
if args.data: if self.args.data:
self.error('Request body (from stdin or a file) and request ' self.error('Request body (from stdin or a file) and request '
'data (key=value) cannot be mixed.') 'data (key=value) cannot be mixed.')
args.data = getattr(fd, 'buffer', fd).read() self.args.data = getattr(fd, 'buffer', fd).read()
def _guess_method(self, args, env): def _guess_method(self):
"""Set `args.method` if not specified to either POST or GET """Set `args.method` if not specified to either POST or GET
based on whether the request has data or not. based on whether the request has data or not.
""" """
if args.method is None: if self.args.method is None:
# Invoked as `http URL'. # Invoked as `http URL'.
assert not args.items assert not self.args.items
if not env.stdin_isatty: if not self.args.ignore_stdin and not self.env.stdin_isatty:
args.method = HTTP_POST self.args.method = HTTP_POST
else: else:
args.method = HTTP_GET self.args.method = HTTP_GET
# FIXME: False positive, e.g., "localhost" matches but is a valid URL. # FIXME: False positive, e.g., "localhost" matches but is a valid URL.
elif not re.match('^[a-zA-Z]+$', args.method): elif not re.match('^[a-zA-Z]+$', self.args.method):
# Invoked as `http URL item+'. The URL is now in `args.method` # Invoked as `http URL item+'. The URL is now in `args.method`
# and the first ITEM is now incorrectly in `args.url`. # and the first ITEM is now incorrectly in `args.url`.
try: try:
# Parse the URL as an ITEM and store it as the first ITEM arg. # Parse the URL as an ITEM and store it as the first ITEM arg.
args.items.insert( self.args.items.insert(
0, KeyValueArgType(*SEP_GROUP_ITEMS).__call__(args.url)) 0,
KeyValueArgType(*SEP_GROUP_ITEMS).__call__(self.args.url)
)
except ArgumentTypeError as e: except ArgumentTypeError as e:
if args.traceback: if self.args.traceback:
raise raise
self.error(e.message) self.error(e.message)
else: else:
# Set the URL correctly # Set the URL correctly
args.url = args.method self.args.url = self.args.method
# Infer the method # Infer the method
has_data = not env.stdin_isatty or any( has_data = (
item.sep in SEP_GROUP_DATA_ITEMS for item in args.items) (not self.args.ignore_stdin and
args.method = HTTP_POST if has_data else HTTP_GET not self.env.stdin_isatty) or any(
item.sep in SEP_GROUP_DATA_ITEMS
for item in self.args.items
)
)
self.args.method = HTTP_POST if has_data else HTTP_GET
def _parse_items(self, args): def _parse_items(self):
"""Parse `args.items` into `args.headers`, `args.data`, """Parse `args.items` into `args.headers`, `args.data`, `args.params`,
`args.`, and `args.files`. and `args.files`.
""" """
args.headers = CaseInsensitiveDict() self.args.headers = CaseInsensitiveDict()
args.data = ParamDict() if args.form else OrderedDict() self.args.data = ParamDict() if self.args.form else OrderedDict()
args.files = OrderedDict() self.args.files = OrderedDict()
args.params = ParamDict() self.args.params = ParamDict()
try: try:
parse_items(items=args.items, parse_items(items=self.args.items,
headers=args.headers, headers=self.args.headers,
data=args.data, data=self.args.data,
files=args.files, files=self.args.files,
params=args.params) params=self.args.params)
except ParseError as e: except ParseError as e:
if args.traceback: if self.args.traceback:
raise raise
self.error(e.message) self.error(e.message)
if args.files and not args.form: if self.args.files and not self.args.form:
# `http url @/path/to/file` # `http url @/path/to/file`
file_fields = list(args.files.keys()) file_fields = list(self.args.files.keys())
if file_fields != ['']: if file_fields != ['']:
self.error( self.error(
'Invalid file fields (perhaps you meant --form?): %s' 'Invalid file fields (perhaps you meant --form?): %s'
% ','.join(file_fields)) % ','.join(file_fields))
fn, fd = args.files[''] fn, fd = self.args.files['']
args.files = {} self.args.files = {}
self._body_from_file(args, fd)
if 'Content-Type' not in args.headers: self._body_from_file(fd)
if 'Content-Type' not in self.args.headers:
mime, encoding = mimetypes.guess_type(fn, strict=False) mime, encoding = mimetypes.guess_type(fn, strict=False)
if mime: if mime:
content_type = mime content_type = mime
if encoding: if encoding:
content_type = '%s; charset=%s' % (mime, encoding) content_type = '%s; charset=%s' % (mime, encoding)
args.headers['Content-Type'] = content_type self.args.headers['Content-Type'] = content_type
def _process_output_options(self, args, env): def _process_output_options(self):
"""Apply defaults to output options or validate the provided ones. """Apply defaults to output options, or validate the provided ones.
The default output options are stdout-type-sensitive. The default output options are stdout-type-sensitive.
""" """
if not args.output_options: if not self.args.output_options:
args.output_options = (OUTPUT_OPTIONS_DEFAULT if env.stdout_isatty self.args.output_options = (
else OUTPUT_OPTIONS_DEFAULT_STDOUT_REDIRECTED) OUTPUT_OPTIONS_DEFAULT
if self.env.stdout_isatty
else OUTPUT_OPTIONS_DEFAULT_STDOUT_REDIRECTED
)
unknown = set(args.output_options) - OUTPUT_OPTIONS unknown_output_options = set(self.args.output_options) - OUTPUT_OPTIONS
if unknown: if unknown_output_options:
self.error('Unknown output options: %s' % ','.join(unknown)) self.error(
'Unknown output options: %s' % ','.join(unknown_output_options)
)
def _process_pretty_options(self, args, env): if self.args.download and OUT_RESP_BODY in self.args.output_options:
if args.prettify == PRETTY_STDOUT_TTY_ONLY: # Response body is always downloaded with --download and it goes
args.prettify = PRETTY_MAP['all' if env.stdout_isatty else 'none'] # through a different routine, so we remove it.
elif args.prettify and env.is_windows: self.args.output_options = str(
set(self.args.output_options) - set(OUT_RESP_BODY))
def _process_pretty_options(self):
if self.args.prettify == PRETTY_STDOUT_TTY_ONLY:
self.args.prettify = PRETTY_MAP[
'all' if self.env.stdout_isatty else 'none']
elif self.args.prettify and self.env.is_windows:
self.error('Only terminal output can be colorized on Windows.') self.error('Only terminal output can be colorized on Windows.')
else: else:
args.prettify = PRETTY_MAP[args.prettify] # noinspection PyTypeChecker
self.args.prettify = PRETTY_MAP[self.args.prettify]
def _validate_download_options(self):
if not self.args.download:
if self.args.download_resume:
self.error('--continue only works with --download')
if self.args.download_resume and not (
self.args.download and self.args.output_file):
self.error('--continue requires --output to be specified')
class ParseError(Exception): class ParseError(Exception):
@ -310,34 +381,15 @@ class KeyValue(object):
return self.__dict__ == other.__dict__ return self.__dict__ == other.__dict__
def session_name_arg_type(name): class SessionNameValidator(object):
from .sessions import Session
if not Session.is_valid_name(name):
raise ArgumentTypeError(
'special characters and spaces are not'
' allowed in session names: "%s"'
% name)
return name
def __init__(self, error_message):
def host_name_arg_type(name):
from .sessions import Host
if not Host.is_valid_name(name):
raise ArgumentTypeError(
'special characters and spaces are not'
' allowed in host names: "%s"'
% name)
return name
class RegexValidator(object):
def __init__(self, pattern, error_message):
self.pattern = re.compile(pattern)
self.error_message = error_message self.error_message = error_message
def __call__(self, value): def __call__(self, value):
if not self.pattern.search(value): # Session name can be a path or just a name.
if (os.path.sep not in value
and not VALID_SESSION_NAME_PATTERN.search(value)):
raise ArgumentError(None, self.error_message) raise ArgumentError(None, self.error_message)
return value return value
@ -372,8 +424,8 @@ class KeyValueArgType(object):
"""Tokenize `s`. There are only two token types - strings """Tokenize `s`. There are only two token types - strings
and escaped characters: and escaped characters:
>>> tokenize(r'foo\=bar\\baz') tokenize(r'foo\=bar\\baz')
['foo', Escaped('='), 'bar', Escaped('\\'), 'baz'] => ['foo', Escaped('='), 'bar', Escaped('\\'), 'baz']
""" """
tokens = [''] tokens = ['']

View File

@ -19,23 +19,27 @@ class Environment(object):
if progname not in ['http', 'https']: if progname not in ['http', 'https']:
progname = 'http' progname = 'http'
stdin_isatty = sys.stdin.isatty()
stdin = sys.stdin
stdout_isatty = sys.stdout.isatty()
config_dir = DEFAULT_CONFIG_DIR config_dir = DEFAULT_CONFIG_DIR
if stdout_isatty and is_windows:
from colorama.initialise import wrap_stream
stdout = wrap_stream(sys.stdout, convert=None,
strip=None, autoreset=True, wrap=True)
else:
stdout = sys.stdout
stderr = sys.stderr
# Can be set to 0 to disable colors completely. # Can be set to 0 to disable colors completely.
colors = 256 if '256color' in os.environ.get('TERM', '') else 88 colors = 256 if '256color' in os.environ.get('TERM', '') else 88
stdin = sys.stdin
stdin_isatty = sys.stdin.isatty()
stdout_isatty = sys.stdout.isatty()
stderr_isatty = sys.stderr.isatty()
if is_windows:
# noinspection PyUnresolvedReferences
from colorama.initialise import wrap_stream
stdout = wrap_stream(sys.stdout, convert=None,
strip=None, autoreset=True, wrap=True)
stderr = wrap_stream(sys.stderr, convert=None,
strip=None, autoreset=True, wrap=True)
else:
stdout = sys.stdout
stderr = sys.stderr
def __init__(self, **kwargs): def __init__(self, **kwargs):
assert all(hasattr(type(self), attr) assert all(hasattr(type(self), attr)
for attr in kwargs.keys()) for attr in kwargs.keys())
@ -84,7 +88,13 @@ class HTTPMessage(object):
@property @property
def content_type(self): def content_type(self):
"""Return the message content type.""" """Return the message content type."""
ct = self._orig.headers.get('Content-Type', '') ct = self._orig.headers.get(
b'Content-Type',
self._orig.headers.get(
'Content-Type',
''
)
)
if isinstance(ct, bytes): if isinstance(ct, bytes):
ct = ct.decode() ct = ct.decode()
return ct return ct
@ -99,6 +109,7 @@ class HTTPResponse(HTTPMessage):
def iter_lines(self, chunk_size): def iter_lines(self, chunk_size):
return ((line, b'\n') for line in self._orig.iter_lines(chunk_size)) return ((line, b'\n') for line in self._orig.iter_lines(chunk_size))
#noinspection PyProtectedMember
@property @property
def headers(self): def headers(self):
original = self._orig.raw._original_response original = self._orig.raw._original_response

View File

@ -2,6 +2,7 @@
""" """
import json import json
import xml.dom.minidom
from functools import partial from functools import partial
from itertools import chain from itertools import chain
@ -20,6 +21,9 @@ from .input import (OUT_REQ_BODY, OUT_REQ_HEAD,
OUT_RESP_HEAD, OUT_RESP_BODY) OUT_RESP_HEAD, OUT_RESP_BODY)
# The default number of spaces to indent when pretty printing
DEFAULT_INDENT = 4
# Colors on Windows via colorama don't look that # Colors on Windows via colorama don't look that
# great and fruity seems to give the best result there. # great and fruity seems to give the best result there.
AVAILABLE_STYLES = set(STYLE_MAP.keys()) AVAILABLE_STYLES = set(STYLE_MAP.keys())
@ -61,7 +65,7 @@ def write(stream, outfile, flush):
outfile.flush() outfile.flush()
def write_with_colors_win_p3k(stream, outfile, flush): def write_with_colors_win_py3(stream, outfile, flush):
"""Like `write`, but colorized chunks are written as text """Like `write`, but colorized chunks are written as text
directly to `outfile` to ensure it gets processed by colorama. directly to `outfile` to ensure it gets processed by colorama.
Applies only to Windows with Python 3 and colorized terminal output. Applies only to Windows with Python 3 and colorized terminal output.
@ -147,7 +151,8 @@ def get_stream_type(env, args):
class BaseStream(object): class BaseStream(object):
"""Base HTTP message output stream class.""" """Base HTTP message output stream class."""
def __init__(self, msg, with_headers=True, with_body=True): def __init__(self, msg, with_headers=True, with_body=True,
on_body_chunk_downloaded=None):
""" """
:param msg: a :class:`models.HTTPMessage` subclass :param msg: a :class:`models.HTTPMessage` subclass
:param with_headers: if `True`, headers will be included :param with_headers: if `True`, headers will be included
@ -158,6 +163,7 @@ class BaseStream(object):
self.msg = msg self.msg = msg
self.with_headers = with_headers self.with_headers = with_headers
self.with_body = with_body self.with_body = with_body
self.on_body_chunk_downloaded = on_body_chunk_downloaded
def _get_headers(self): def _get_headers(self):
"""Return the headers' bytes.""" """Return the headers' bytes."""
@ -177,6 +183,8 @@ class BaseStream(object):
try: try:
for chunk in self._iter_body(): for chunk in self._iter_body():
yield chunk yield chunk
if self.on_body_chunk_downloaded:
self.on_body_chunk_downloaded(chunk)
except BinarySuppressedError as e: except BinarySuppressedError as e:
if self.with_headers: if self.with_headers:
yield b'\n' yield b'\n'
@ -187,7 +195,7 @@ class RawStream(BaseStream):
"""The message is streamed in chunks with no processing.""" """The message is streamed in chunks with no processing."""
CHUNK_SIZE = 1024 * 100 CHUNK_SIZE = 1024 * 100
CHUNK_SIZE_BY_LINE = 1024 * 5 CHUNK_SIZE_BY_LINE = 1
def __init__(self, chunk_size=CHUNK_SIZE, **kwargs): def __init__(self, chunk_size=CHUNK_SIZE, **kwargs):
super(RawStream, self).__init__(**kwargs) super(RawStream, self).__init__(**kwargs)
@ -205,7 +213,7 @@ class EncodedStream(BaseStream):
is suppressed. The body is always streamed by line. is suppressed. The body is always streamed by line.
""" """
CHUNK_SIZE = 1024 * 5 CHUNK_SIZE = 1
def __init__(self, env=Environment(), **kwargs): def __init__(self, env=Environment(), **kwargs):
@ -241,7 +249,7 @@ class PrettyStream(EncodedStream):
""" """
CHUNK_SIZE = 1024 * 5 CHUNK_SIZE = 1
def __init__(self, processor, **kwargs): def __init__(self, processor, **kwargs):
super(PrettyStream, self).__init__(**kwargs) super(PrettyStream, self).__init__(**kwargs)
@ -260,8 +268,9 @@ class PrettyStream(EncodedStream):
def _process_body(self, chunk): def _process_body(self, chunk):
return (self.processor return (self.processor
.process_body( .process_body(
chunk.decode(self.msg.encoding, 'replace'), content=chunk.decode(self.msg.encoding, 'replace'),
self.msg.content_type) content_type=self.msg.content_type,
encoding=self.msg.encoding)
.encode(self.output_encoding, 'replace')) .encode(self.output_encoding, 'replace'))
@ -363,12 +372,13 @@ class BaseProcessor(object):
""" """
return headers return headers
def process_body(self, content, content_type, subtype): def process_body(self, content, content_type, subtype, encoding):
"""Return processed `content`. """Return processed `content`.
:param content: The body content as text :param content: The body content as text
:param content_type: Full content type, e.g., 'application/atom+xml'. :param content_type: Full content type, e.g., 'application/atom+xml'.
:param subtype: E.g. 'xml'. :param subtype: E.g. 'xml'.
:param encoding: The original content encoding.
""" """
return content return content
@ -377,7 +387,7 @@ class BaseProcessor(object):
class JSONProcessor(BaseProcessor): class JSONProcessor(BaseProcessor):
"""JSON body processor.""" """JSON body processor."""
def process_body(self, content, content_type, subtype): def process_body(self, content, content_type, subtype, encoding):
if subtype == 'json': if subtype == 'json':
try: try:
# Indent the JSON data, sort keys by name, and # Indent the JSON data, sort keys by name, and
@ -385,13 +395,29 @@ class JSONProcessor(BaseProcessor):
content = json.dumps(json.loads(content), content = json.dumps(json.loads(content),
sort_keys=True, sort_keys=True,
ensure_ascii=False, ensure_ascii=False,
indent=4) indent=DEFAULT_INDENT)
except ValueError: except ValueError:
# Invalid JSON but we don't care. # Invalid JSON but we don't care.
pass pass
return content return content
class XMLProcessor(BaseProcessor):
"""XML body processor."""
# TODO: tests
def process_body(self, content, content_type, subtype, encoding):
if subtype == 'xml':
try:
# Pretty print the XML
doc = xml.dom.minidom.parseString(content.encode(encoding))
content = doc.toprettyxml(indent=' ' * DEFAULT_INDENT)
except xml.parsers.expat.ExpatError:
# Ignore invalid XML errors (skips attempting to pretty print)
pass
return content
class PygmentsProcessor(BaseProcessor): class PygmentsProcessor(BaseProcessor):
"""A processor that applies syntax-highlighting using Pygments """A processor that applies syntax-highlighting using Pygments
to the headers, and to the body as well if its content type is recognized. to the headers, and to the body as well if its content type is recognized.
@ -423,7 +449,7 @@ class PygmentsProcessor(BaseProcessor):
return pygments.highlight( return pygments.highlight(
headers, HTTPLexer(), self.formatter).strip() headers, HTTPLexer(), self.formatter).strip()
def process_body(self, content, content_type, subtype): def process_body(self, content, content_type, subtype, encoding):
try: try:
lexer = self.lexers_by_type.get(content_type) lexer = self.lexers_by_type.get(content_type)
if not lexer: if not lexer:
@ -456,7 +482,8 @@ class OutputProcessor(object):
installed_processors = { installed_processors = {
'format': [ 'format': [
HeadersProcessor, HeadersProcessor,
JSONProcessor JSONProcessor,
XMLProcessor
], ],
'colors': [ 'colors': [
PygmentsProcessor PygmentsProcessor
@ -482,13 +509,18 @@ class OutputProcessor(object):
headers = processor.process_headers(headers) headers = processor.process_headers(headers)
return headers return headers
def process_body(self, content, content_type): def process_body(self, content, content_type, encoding):
# e.g., 'application/atom+xml' # e.g., 'application/atom+xml'
content_type = content_type.split(';')[0] content_type = content_type.split(';')[0]
# e.g., 'xml' # e.g., 'xml'
subtype = content_type.split('/')[-1].split('+')[-1] subtype = content_type.split('/')[-1].split('+')[-1]
for processor in self.processors: for processor in self.processors:
content = processor.process_body(content, content_type, subtype) content = processor.process_body(
content,
content_type,
subtype,
encoding
)
return content return content

View File

@ -0,0 +1,9 @@
from .base import AuthPlugin
from .manager import PluginManager
from .builtin import BasicAuthPlugin, DigestAuthPlugin
plugin_manager = PluginManager()
plugin_manager.register(BasicAuthPlugin)
plugin_manager.register(DigestAuthPlugin)

28
httpie/plugins/base.py Normal file
View File

@ -0,0 +1,28 @@
class AuthPlugin(object):
"""
Base auth plugin class.
See <https://github.com/jkbr/httpie-ntlm> for an example auth plugin.
"""
# The value that should be passed to --auth-type
# to use this auth plugin. Eg. "my-auth"
auth_type = None
# The name of the plugin, eg. "My auth".
name = None
# Optional short description. Will be be shown in the help
# under --auth-type.
description = None
# This be set automatically once the plugin has been loaded.
package_name = None
def get_auth(self, username, password):
"""
Return a ``requests.auth.AuthBase`` subclass instance.
"""
raise NotImplementedError()

26
httpie/plugins/builtin.py Normal file
View File

@ -0,0 +1,26 @@
import requests.auth
from .base import AuthPlugin
class BuiltinAuthPlugin(AuthPlugin):
package_name = '(builtin)'
class BasicAuthPlugin(BuiltinAuthPlugin):
name = 'Basic HTTP auth'
auth_type = 'basic'
def get_auth(self, username, password):
return requests.auth.HTTPBasicAuth(username, password)
class DigestAuthPlugin(BuiltinAuthPlugin):
name = 'Digest HTTP auth'
auth_type = 'digest'
def get_auth(self, username, password):
return requests.auth.HTTPDigestAuth(username, password)

35
httpie/plugins/manager.py Normal file
View File

@ -0,0 +1,35 @@
from pkg_resources import iter_entry_points
ENTRY_POINT_NAMES = [
'httpie.plugins.auth.v1'
]
class PluginManager(object):
def __init__(self):
self._plugins = []
def __iter__(self):
return iter(self._plugins)
def register(self, plugin):
self._plugins.append(plugin)
def get_auth_plugins(self):
return list(self._plugins)
def get_auth_plugin_mapping(self):
return dict((plugin.auth_type, plugin) for plugin in self)
def get_auth_plugin(self, auth_type):
return self.get_auth_plugin_mapping()[auth_type]
def load_installed_plugins(self):
for entry_point_name in ENTRY_POINT_NAMES:
for entry_point in iter_entry_points(entry_point_name):
plugin = entry_point.load()
plugin.package_name = entry_point.dist.key
self.register(entry_point.load())

View File

@ -3,52 +3,68 @@
""" """
import re import re
import os import os
import glob
import errno
import shutil
import requests import requests
from requests.cookies import RequestsCookieJar, create_cookie from requests.cookies import RequestsCookieJar, create_cookie
from requests.auth import HTTPBasicAuth, HTTPDigestAuth
from .compat import urlsplit from .compat import urlsplit
from .config import BaseConfigDict, DEFAULT_CONFIG_DIR from .config import BaseConfigDict, DEFAULT_CONFIG_DIR
from httpie.plugins import plugin_manager
SESSIONS_DIR_NAME = 'sessions' SESSIONS_DIR_NAME = 'sessions'
DEFAULT_SESSIONS_DIR = os.path.join(DEFAULT_CONFIG_DIR, SESSIONS_DIR_NAME) DEFAULT_SESSIONS_DIR = os.path.join(DEFAULT_CONFIG_DIR, SESSIONS_DIR_NAME)
VALID_SESSION_NAME_PATTERN = re.compile('^[a-zA-Z0-9_.-]+$')
# Request headers starting with these prefixes won't be stored in sessions.
# They are specific to each request.
# http://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Requests
SESSION_IGNORED_HEADER_PREFIXES = ['Content-', 'If-']
def get_response(name, request_kwargs, config_dir, read_only=False): def get_response(session_name, requests_kwargs, config_dir, args,
read_only=False):
"""Like `client.get_response`, but applies permanent """Like `client.get_response`, but applies permanent
aspects of the session to the request. aspects of the session to the request.
""" """
sessions_dir = os.path.join(config_dir, SESSIONS_DIR_NAME) if os.path.sep in session_name:
host = Host( path = os.path.expanduser(session_name)
root_dir=sessions_dir, else:
name=request_kwargs['headers'].get('Host', None) hostname = (
or urlsplit(request_kwargs['url']).netloc.split('@')[-1] requests_kwargs['headers'].get('Host', None)
) or urlsplit(requests_kwargs['url']).netloc.split('@')[-1]
session = Session(host, name) )
assert re.match('^[a-zA-Z0-9_.:-]+$', hostname)
# host:port => host_port
hostname = hostname.replace(':', '_')
path = os.path.join(config_dir,
SESSIONS_DIR_NAME,
hostname,
session_name + '.json')
session = Session(path)
session.load() session.load()
# Update session headers with the request headers. request_headers = requests_kwargs.get('headers', {})
session['headers'].update(request_kwargs.get('headers', {})) requests_kwargs['headers'] = dict(session.headers, **request_headers)
# Use the merged headers for the request session.update_headers(request_headers)
request_kwargs['headers'] = session['headers']
auth = request_kwargs.get('auth', None) if args.auth:
if auth: session.auth = {
session.auth = auth 'type': args.auth_type,
'username': args.auth.key,
'password': args.auth.value,
}
elif session.auth: elif session.auth:
request_kwargs['auth'] = session.auth requests_kwargs['auth'] = session.auth
requests_session = requests.Session() requests_session = requests.Session()
requests_session.cookies = session.cookies requests_session.cookies = session.cookies
try: try:
response = requests_session.request(**request_kwargs) response = requests_session.request(**requests_kwargs)
except Exception: except Exception:
raise raise
else: else:
@ -59,69 +75,13 @@ def get_response(name, request_kwargs, config_dir, read_only=False):
return response return response
class Host(object):
"""A host is a per-host directory on the disk containing sessions files."""
VALID_NAME_PATTERN = re.compile('^[a-zA-Z0-9_.:-]+$')
def __init__(self, name, root_dir=DEFAULT_SESSIONS_DIR):
assert self.VALID_NAME_PATTERN.match(name)
self.name = name
self.root_dir = root_dir
def __iter__(self):
"""Return an iterator yielding `Session` instances."""
for fn in sorted(glob.glob1(self.path, '*.json')):
session_name = os.path.splitext(fn)[0]
yield Session(host=self, name=session_name)
@staticmethod
def _quote_name(name):
"""host:port => host_port"""
return name.replace(':', '_')
@staticmethod
def _unquote_name(name):
"""host_port => host:port"""
return re.sub(r'_(\d+)$', r':\1', name)
@classmethod
def all(cls, root_dir=DEFAULT_SESSIONS_DIR):
"""Return a generator yielding a host at a time."""
for name in sorted(glob.glob1(root_dir, '*')):
if os.path.isdir(os.path.join(root_dir, name)):
yield Host(cls._unquote_name(name), root_dir=root_dir)
@property
def verbose_name(self):
return '%s %s' % (self.name, self.path)
def delete(self):
shutil.rmtree(self.path)
@property
def path(self):
path = os.path.join(self.root_dir, self._quote_name(self.name))
try:
os.makedirs(path, mode=0o700)
except OSError as e:
if e.errno != errno.EEXIST:
raise
return path
class Session(BaseConfigDict): class Session(BaseConfigDict):
helpurl = 'https://github.com/jkbr/httpie#sessions'
help = 'https://github.com/jkbr/httpie#sessions'
about = 'HTTPie session file' about = 'HTTPie session file'
VALID_NAME_PATTERN = re.compile('^[a-zA-Z0-9_.-]+$') def __init__(self, path, *args, **kwargs):
def __init__(self, host, name, *args, **kwargs):
assert self.VALID_NAME_PATTERN.match(name)
super(Session, self).__init__(*args, **kwargs) super(Session, self).__init__(*args, **kwargs)
self.host = host self._path = path
self.name = name
self['headers'] = {} self['headers'] = {}
self['cookies'] = {} self['cookies'] = {}
self['auth'] = { self['auth'] = {
@ -130,13 +90,31 @@ class Session(BaseConfigDict):
'password': None 'password': None
} }
@property def _get_path(self):
def directory(self): return self._path
return self.host.path
def update_headers(self, request_headers):
"""
Update the session headers with the request ones while ignoring
certain name prefixes.
:type request_headers: dict
"""
for name, value in request_headers.items():
if name == 'User-Agent' and value.startswith('HTTPie/'):
continue
for prefix in SESSION_IGNORED_HEADER_PREFIXES:
if name.lower().startswith(prefix.lower()):
break
else:
self['headers'][name] = value
@property @property
def verbose_name(self): def headers(self):
return '%s %s %s' % (self.host.name, self.name, self.path) return self['headers']
@property @property
def cookies(self): def cookies(self):
@ -149,31 +127,27 @@ class Session(BaseConfigDict):
@cookies.setter @cookies.setter
def cookies(self, jar): def cookies(self, jar):
"""
:type jar: CookieJar
"""
# http://docs.python.org/2/library/cookielib.html#cookie-objects # http://docs.python.org/2/library/cookielib.html#cookie-objects
stored_attrs = ['value', 'path', 'secure', 'expires'] stored_attrs = ['value', 'path', 'secure', 'expires']
self['cookies'] = {} self['cookies'] = {}
for host in jar._cookies.values(): for cookie in jar:
for path in host.values(): self['cookies'][cookie.name] = dict(
for name, cookie in path.items(): (attname, getattr(cookie, attname))
self['cookies'][name] = dict( for attname in stored_attrs
(attname, getattr(cookie, attname)) )
for attname in stored_attrs
)
@property @property
def auth(self): def auth(self):
auth = self.get('auth', None) auth = self.get('auth', None)
if not auth or not auth['type']: if not auth or not auth['type']:
return return
Auth = {'basic': HTTPBasicAuth, auth_plugin = plugin_manager.get_auth_plugin(auth['type'])()
'digest': HTTPDigestAuth}[auth['type']] return auth_plugin.get_auth(auth['username'], auth['password'])
return Auth(auth['username'], auth['password'])
@auth.setter @auth.setter
def auth(self, cred): def auth(self, auth):
self['auth'] = { assert set(['type', 'username', 'password']) == set(auth.keys())
'type': {HTTPBasicAuth: 'basic', self['auth'] = auth
HTTPDigestAuth: 'digest'}[type(cred)],
'username': cred.username,
'password': cred.password,
}

46
httpie/utils.py Normal file
View File

@ -0,0 +1,46 @@
from __future__ import division
def humanize_bytes(n, precision=2):
# Author: Doug Latornell
# Licence: MIT
# URL: http://code.activestate.com/recipes/577081/
"""Return a humanized string representation of a number of bytes.
Assumes `from __future__ import division`.
>>> humanize_bytes(1)
'1 byte'
>>> humanize_bytes(1024)
'1.0 kB'
>>> humanize_bytes(1024 * 123)
'123.0 kB'
>>> humanize_bytes(1024 * 12342)
'12.1 MB'
>>> humanize_bytes(1024 * 12342, 2)
'12.05 MB'
>>> humanize_bytes(1024 * 1234, 2)
'1.21 MB'
>>> humanize_bytes(1024 * 1234 * 1111, 2)
'1.31 GB'
>>> humanize_bytes(1024 * 1234 * 1111, 1)
'1.3 GB'
"""
abbrevs = [
(1 << 50, 'PB'),
(1 << 40, 'TB'),
(1 << 30, 'GB'),
(1 << 20, 'MB'),
(1 << 10, 'kB'),
(1, 'B')
]
if n == 1:
return '1 B'
for factor, suffix in abbrevs:
if n >= factor:
break
return '%.*f %s' % (precision, n / factor, suffix)

3
requirements-dev.txt Normal file
View File

@ -0,0 +1,3 @@
tox
git+git://github.com/kennethreitz/httpbin.git@7c96875e87a448f08fb1981e85eb79e77d592d98
docutils

View File

@ -1 +0,0 @@
#

View File

@ -1,6 +1,5 @@
import os import os
import sys import sys
import re
import codecs import codecs
from setuptools import setup from setuptools import setup
import httpie import httpie
@ -12,24 +11,23 @@ if sys.argv[-1] == 'test':
requirements = [ requirements = [
'requests>=1.0.4', 'requests>=1.2.3',
'Pygments>=1.5' 'Pygments>=1.5'
] ]
if sys.version_info[:2] in ((2, 6), (3, 1)): try:
# argparse has been added in Python 3.2 / 2.7 #noinspection PyUnresolvedReferences
import argparse
except ImportError:
requirements.append('argparse>=1.2.1') requirements.append('argparse>=1.2.1')
if 'win32' in str(sys.platform).lower(): if 'win32' in str(sys.platform).lower():
# Terminal colors for Windows # Terminal colors for Windows
requirements.append('colorama>=0.2.4') requirements.append('colorama>=0.2.4')
def long_description(): def long_description():
"""Pre-process the README so that PyPi can render it properly."""
with codecs.open('README.rst', encoding='utf8') as f: with codecs.open('README.rst', encoding='utf8') as f:
rst = f.read() return f.read()
code_block = '(:\n\n)?\.\. code-block::.*'
rst = re.sub(code_block, '::', rst)
return rst
setup( setup(
@ -42,11 +40,10 @@ setup(
author=httpie.__author__, author=httpie.__author__,
author_email='jakub@roztocil.name', author_email='jakub@roztocil.name',
license=httpie.__licence__, license=httpie.__licence__,
packages=['httpie'], packages=['httpie', 'httpie.plugins'],
entry_points={ entry_points={
'console_scripts': [ 'console_scripts': [
'http = httpie.__main__:main', 'http = httpie.__main__:main',
'httpie = httpie.manage:main',
], ],
}, },
install_requires=requirements, install_requires=requirements,

View File

@ -23,20 +23,27 @@ import subprocess
import os import os
import sys import sys
import json import json
#noinspection PyCompatibility
import argparse import argparse
import tempfile import tempfile
import unittest import unittest
import shutil import shutil
import time
from requests.structures import CaseInsensitiveDict
try: try:
#noinspection PyCompatibility
from urllib.request import urlopen from urllib.request import urlopen
except ImportError: except ImportError:
# noinspection PyUnresolvedReferences
#noinspection PyCompatibility
from urllib2 import urlopen from urllib2 import urlopen
try: try:
from unittest import skipIf, skip from unittest import skipIf, skip
except ImportError: except ImportError:
skip = lambda msg: lambda self: None skip = lambda msg: lambda self: None
# noinspection PyUnusedLocal
def skipIf(cond, reason): def skipIf(cond, reason):
def decorator(test_method): def decorator(test_method):
if cond: if cond:
@ -62,6 +69,14 @@ from httpie.core import main
from httpie.output import BINARY_SUPPRESSED_NOTICE from httpie.output import BINARY_SUPPRESSED_NOTICE
from httpie.input import ParseError from httpie.input import ParseError
from httpie.compat import is_windows, is_py26, bytes, str from httpie.compat import is_windows, is_py26, bytes, str
from httpie.downloads import (
parse_content_range,
filename_from_content_disposition,
filename_from_url,
get_unique_filename,
ContentRangeError,
Download,
)
CRLF = '\r\n' CRLF = '\r\n'
@ -98,7 +113,8 @@ with open(BIN_FILE_PATH, 'rb') as f:
def httpbin(path): def httpbin(path):
return HTTPBIN_URL + path url = HTTPBIN_URL + path
return url
def mk_config_dir(): def mk_config_dir():
@ -180,20 +196,25 @@ def http(*args, **kwargs):
if not env: if not env:
env = kwargs['env'] = TestEnvironment() env = kwargs['env'] = TestEnvironment()
stdout = env.stdout
stderr = env.stderr
try: try:
try: try:
exit_status = main(args=['--traceback'] + list(args), **kwargs) exit_status = main(args=['--traceback'] + list(args), **kwargs)
if '--download' in args:
# Let the progress reporter thread finish.
time.sleep(.5)
except Exception: except Exception:
sys.stderr.write(env.stderr.read()) sys.stderr.write(stderr.read())
raise raise
except SystemExit: except SystemExit:
exit_status = ExitStatus.ERROR exit_status = ExitStatus.ERROR
env.stdout.seek(0) stdout.seek(0)
env.stderr.seek(0) stderr.seek(0)
output = env.stdout.read() output = stdout.read()
try: try:
r = StrResponse(output.decode('utf8')) r = StrResponse(output.decode('utf8'))
except UnicodeDecodeError: except UnicodeDecodeError:
@ -215,14 +236,14 @@ def http(*args, **kwargs):
except ValueError: except ValueError:
pass pass
r.stderr = env.stderr.read() r.stderr = stderr.read()
r.exit_status = exit_status r.exit_status = exit_status
return r return r
finally: finally:
env.stdout.close() stdout.close()
env.stderr.close() stderr.close()
class BaseTestCase(unittest.TestCase): class BaseTestCase(unittest.TestCase):
@ -240,11 +261,15 @@ class BaseTestCase(unittest.TestCase):
self.assertEqual(set(d1.keys()), set(d2.keys()), msg) self.assertEqual(set(d1.keys()), set(d2.keys()), msg)
self.assertEqual(sorted(d1.values()), sorted(d2.values()), msg) self.assertEqual(sorted(d1.values()), sorted(d2.values()), msg)
def assertIsNone(self, obj, msg=None):
self.assertEqual(obj, None, msg=msg)
################################################################# #################################################################
# High-level tests using httpbin. # High-level tests using httpbin.
################################################################# #################################################################
class HTTPieTest(BaseTestCase): class HTTPieTest(BaseTestCase):
def test_GET(self): def test_GET(self):
@ -268,7 +293,7 @@ class HTTPieTest(BaseTestCase):
'foo=bar' 'foo=bar'
) )
self.assertIn(OK, r) self.assertIn(OK, r)
self.assertIn('"foo": "bar"', r) self.assertIn(r'\"foo\": \"bar\"', r)
def test_POST_JSON_data(self): def test_POST_JSON_data(self):
r = http( r = http(
@ -277,7 +302,7 @@ class HTTPieTest(BaseTestCase):
'foo=bar' 'foo=bar'
) )
self.assertIn(OK, r) self.assertIn(OK, r)
self.assertIn('"foo": "bar"', r) self.assertIn(r'\"foo\": \"bar\"', r)
def test_POST_form(self): def test_POST_form(self):
r = http( r = http(
@ -436,7 +461,9 @@ class AutoContentTypeAndAcceptHeadersTest(BaseTestCase):
) )
self.assertIn(OK, r) self.assertIn(OK, r)
self.assertEqual(r.json['headers']['Accept'], 'application/json') self.assertEqual(r.json['headers']['Accept'], 'application/json')
self.assertFalse(r.json['headers'].get('Content-Type')) # Make sure Content-Type gets set even with no data.
# https://github.com/jkbr/httpie/issues/137
self.assertIn('application/json', r.json['headers']['Content-Type'])
def test_GET_explicit_JSON_explicit_headers(self): def test_GET_explicit_JSON_explicit_headers(self):
r = http( r = http(
@ -519,7 +546,7 @@ class ImplicitHTTPMethodTest(BaseTestCase):
'hello=world' 'hello=world'
) )
self.assertIn(OK, r) self.assertIn(OK, r)
self.assertIn('"hello": "world"', r) self.assertIn(r'\"hello\": \"world\"', r)
def test_implicit_POST_form(self): def test_implicit_POST_form(self):
r = http( r = http(
@ -658,8 +685,8 @@ class VerboseFlagTest(BaseTestCase):
'baz=bar' 'baz=bar'
) )
self.assertIn(OK, r) self.assertIn(OK, r)
#noinspection PyUnresolvedReferences self.assertIn('"baz": "bar"', r) # request
self.assertEqual(r.count('"baz": "bar"'), 2) self.assertIn(r'\"baz\": \"bar\"', r) # response
class MultipartFormDataFileUploadTest(BaseTestCase): class MultipartFormDataFileUploadTest(BaseTestCase):
@ -782,7 +809,10 @@ class RequestBodyFromFilePathTest(BaseTestCase):
""" """
def test_request_body_from_file_by_path(self): def test_request_body_from_file_by_path(self):
# FIXME: *sometimes* fails on py33, the content-type is form.
# https://github.com/jkbr/httpie/issues/140
r = http( r = http(
'--verbose',
'POST', 'POST',
httpbin('/post'), httpbin('/post'),
'@' + FILE_PATH_ARG '@' + FILE_PATH_ARG
@ -792,6 +822,8 @@ class RequestBodyFromFilePathTest(BaseTestCase):
self.assertIn('"Content-Type": "text/plain"', r) self.assertIn('"Content-Type": "text/plain"', r)
def test_request_body_from_file_by_path_with_explicit_content_type(self): def test_request_body_from_file_by_path_with_explicit_content_type(self):
# FIXME: *sometimes* fails on py33, the content-type is form.
# https://github.com/jkbr/httpie/issues/140
r = http( r = http(
'POST', 'POST',
httpbin('/post'), httpbin('/post'),
@ -845,8 +877,8 @@ class AuthTest(BaseTestCase):
httpbin('/digest-auth/auth/user/password') httpbin('/digest-auth/auth/user/password')
) )
self.assertIn(OK, r) self.assertIn(OK, r)
self.assertIn('"authenticated": true', r) self.assertIn(r'"authenticated": true', r)
self.assertIn('"user": "user"', r) self.assertIn(r'"user": "user"', r)
def test_password_prompt(self): def test_password_prompt(self):
@ -1051,6 +1083,30 @@ class StreamTest(BaseTestCase):
self.assertIn(BIN_FILE_CONTENT, r) self.assertIn(BIN_FILE_CONTENT, r)
class IgnoreStdinTest(BaseTestCase):
def test_ignore_stdin(self):
with open(FILE_PATH) as f:
r = http(
'--ignore-stdin',
'--verbose',
httpbin('/get'),
env=TestEnvironment(stdin=f, stdin_isatty=False)
)
self.assertIn(OK, r)
self.assertIn('GET /get HTTP', r) # Don't default to POST.
self.assertNotIn(FILE_CONTENT, r) # Don't send stdin data.
def test_ignore_stdin_cannot_prompt_password(self):
r = http(
'--ignore-stdin',
'--auth=username-without-password',
httpbin('/get'),
)
self.assertEqual(r.exit_status, ExitStatus.ERROR)
self.assertIn('because --ignore-stdin', r.stderr)
class LineEndingsTest(BaseTestCase): class LineEndingsTest(BaseTestCase):
"""Test that CRLF is properly used in headers and """Test that CRLF is properly used in headers and
as the headers/body separator.""" as the headers/body separator."""
@ -1143,6 +1199,8 @@ class ItemParsingTest(BaseTestCase):
# files # files
self.key_value_type('bar\\@baz@%s' % FILE_PATH_ARG) self.key_value_type('bar\\@baz@%s' % FILE_PATH_ARG)
]) ])
# `requests.structures.CaseInsensitiveDict` => `dict`
headers = dict(headers._store.values())
self.assertDictEqual(headers, { self.assertDictEqual(headers, {
'foo:bar': 'baz', 'foo:bar': 'baz',
'jack@jill': 'hill', 'jack@jill': 'hill',
@ -1172,6 +1230,8 @@ class ItemParsingTest(BaseTestCase):
self.key_value_type('test-file@%s' % FILE_PATH_ARG), self.key_value_type('test-file@%s' % FILE_PATH_ARG),
self.key_value_type('query==value'), self.key_value_type('query==value'),
]) ])
# `requests.structures.CaseInsensitiveDict` => `dict`
headers = dict(headers._store.values())
self.assertDictEqual(headers, { self.assertDictEqual(headers, {
'header': 'value', 'header': 'value',
'eh': '' 'eh': ''
@ -1195,71 +1255,84 @@ class ArgumentParserTestCase(unittest.TestCase):
self.parser = input.Parser() self.parser = input.Parser()
def test_guess_when_method_set_and_valid(self): def test_guess_when_method_set_and_valid(self):
args = argparse.Namespace() self.parser.args = argparse.Namespace()
args.method = 'GET' self.parser.args.method = 'GET'
args.url = 'http://example.com/' self.parser.args.url = 'http://example.com/'
args.items = [] self.parser.args.items = []
self.parser.args.ignore_stdin = False
self.parser._guess_method(args, TestEnvironment()) self.parser.env = TestEnvironment()
self.assertEqual(args.method, 'GET') self.parser._guess_method()
self.assertEqual(args.url, 'http://example.com/')
self.assertEqual(args.items, []) self.assertEqual(self.parser.args.method, 'GET')
self.assertEqual(self.parser.args.url, 'http://example.com/')
self.assertEqual(self.parser.args.items, [])
def test_guess_when_method_not_set(self): def test_guess_when_method_not_set(self):
args = argparse.Namespace()
args.method = None
args.url = 'http://example.com/'
args.items = []
self.parser._guess_method(args, TestEnvironment()) self.parser.args = argparse.Namespace()
self.parser.args.method = None
self.parser.args.url = 'http://example.com/'
self.parser.args.items = []
self.parser.args.ignore_stdin = False
self.parser.env = TestEnvironment()
self.assertEqual(args.method, 'GET') self.parser._guess_method()
self.assertEqual(args.url, 'http://example.com/')
self.assertEqual(args.items, []) self.assertEqual(self.parser.args.method, 'GET')
self.assertEqual(self.parser.args.url, 'http://example.com/')
self.assertEqual(self.parser.args.items, [])
def test_guess_when_method_set_but_invalid_and_data_field(self): def test_guess_when_method_set_but_invalid_and_data_field(self):
args = argparse.Namespace() self.parser.args = argparse.Namespace()
args.method = 'http://example.com/' self.parser.args.method = 'http://example.com/'
args.url = 'data=field' self.parser.args.url = 'data=field'
args.items = [] self.parser.args.items = []
self.parser.args.ignore_stdin = False
self.parser.env = TestEnvironment()
self.parser._guess_method()
self.parser._guess_method(args, TestEnvironment()) self.assertEqual(self.parser.args.method, 'POST')
self.assertEqual(self.parser.args.url, 'http://example.com/')
self.assertEqual(args.method, 'POST')
self.assertEqual(args.url, 'http://example.com/')
self.assertEqual( self.assertEqual(
args.items, self.parser.args.items,
[input.KeyValue( [input.KeyValue(
key='data', value='field', sep='=', orig='data=field')]) key='data', value='field', sep='=', orig='data=field')])
def test_guess_when_method_set_but_invalid_and_header_field(self): def test_guess_when_method_set_but_invalid_and_header_field(self):
args = argparse.Namespace() self.parser.args = argparse.Namespace()
args.method = 'http://example.com/' self.parser.args.method = 'http://example.com/'
args.url = 'test:header' self.parser.args.url = 'test:header'
args.items = [] self.parser.args.items = []
self.parser.args.ignore_stdin = False
self.parser._guess_method(args, TestEnvironment()) self.parser.env = TestEnvironment()
self.assertEqual(args.method, 'GET') self.parser._guess_method()
self.assertEqual(args.url, 'http://example.com/')
self.assertEqual(self.parser.args.method, 'GET')
self.assertEqual(self.parser.args.url, 'http://example.com/')
self.assertEqual( self.assertEqual(
args.items, self.parser.args.items,
[input.KeyValue( [input.KeyValue(
key='test', value='header', sep=':', orig='test:header')]) key='test', value='header', sep=':', orig='test:header')])
def test_guess_when_method_set_but_invalid_and_item_exists(self): def test_guess_when_method_set_but_invalid_and_item_exists(self):
args = argparse.Namespace() self.parser.args = argparse.Namespace()
args.method = 'http://example.com/' self.parser.args.method = 'http://example.com/'
args.url = 'new_item=a' self.parser.args.url = 'new_item=a'
args.items = [ self.parser.args.items = [
input.KeyValue( input.KeyValue(
key='old_item', value='b', sep='=', orig='old_item=b') key='old_item', value='b', sep='=', orig='old_item=b')
] ]
self.parser.args.ignore_stdin = False
self.parser._guess_method(args, TestEnvironment()) self.parser.env = TestEnvironment()
self.assertEqual(args.items, [ self.parser._guess_method()
self.assertEqual(self.parser.args.items, [
input.KeyValue( input.KeyValue(
key='new_item', value='a', sep='=', orig='new_item=a'), key='new_item', value='a', sep='=', orig='new_item=a'),
input.KeyValue( input.KeyValue(
@ -1335,6 +1408,26 @@ class SessionTest(BaseTestCase):
self.assertEqual(r.json['headers']['Cookie'], 'hello=world') self.assertEqual(r.json['headers']['Cookie'], 'hello=world')
self.assertIn('Basic ', r.json['headers']['Authorization']) self.assertIn('Basic ', r.json['headers']['Authorization'])
def test_session_ignored_header_prefixes(self):
r = http(
'--session=test',
'GET',
httpbin('/get'),
'Content-Type: text/plain',
'If-Unmodified-Since: Sat, 29 Oct 1994 19:43:31 GMT',
env=self.env
)
self.assertIn(OK, r)
r2 = http(
'--session=test',
'GET',
httpbin('/get')
)
self.assertIn(OK, r2)
self.assertNotIn('Content-Type', r2.json['headers'])
self.assertNotIn('If-Unmodified-Since', r2.json['headers'])
def test_session_update(self): def test_session_update(self):
# Get a response to a request from the original session. # Get a response to a request from the original session.
r1 = http( r1 = http(
@ -1409,6 +1502,181 @@ class SessionTest(BaseTestCase):
# Should be the same as before r2. # Should be the same as before r2.
self.assertDictEqual(r1.json, r3.json) self.assertDictEqual(r1.json, r3.json)
def test_session_by_path(self):
session_path = os.path.join(self.config_dir, 'session-by-path.json')
r1 = http(
'--session=' + session_path,
'GET',
httpbin('/get'),
'Foo:Bar',
env=self.env
)
self.assertIn(OK, r1)
r2 = http(
'--session=' + session_path,
'GET',
httpbin('/get'),
env=self.env
)
self.assertIn(OK, r2)
self.assertEqual(r2.json['headers']['Foo'], 'Bar')
class DownloadUtilsTest(BaseTestCase):
def test_Content_Range_parsing(self):
parse = parse_content_range
self.assertEqual(parse('bytes 100-199/200', 100), 200)
self.assertEqual(parse('bytes 100-199/*', 100), 200)
# missing
self.assertRaises(ContentRangeError, parse, None, 100)
# syntax error
self.assertRaises(ContentRangeError, parse, 'beers 100-199/*', 100)
# unexpected range
self.assertRaises(ContentRangeError, parse, 'bytes 100-199/*', 99)
# invalid instance-length
self.assertRaises(ContentRangeError, parse, 'bytes 100-199/199', 100)
# invalid byte-range-resp-spec
self.assertRaises(ContentRangeError, parse, 'bytes 100-99/199', 100)
# invalid byte-range-resp-spec
self.assertRaises(ContentRangeError, parse, 'bytes 100-100/*', 100)
def test_Content_Disposition_parsing(self):
parse = filename_from_content_disposition
self.assertEqual(
parse('attachment; filename=hello-WORLD_123.txt'),
'hello-WORLD_123.txt'
)
self.assertEqual(
parse('attachment; filename=".hello-WORLD_123.txt"'),
'hello-WORLD_123.txt'
)
self.assertEqual(
parse('attachment; filename="white space.txt"'),
'white space.txt'
)
self.assertEqual(
parse(r'attachment; filename="\"quotes\".txt"'),
'"quotes".txt'
)
self.assertEqual(parse('attachment; filename=/etc/hosts'), 'hosts')
self.assertIsNone(parse('attachment; filename='))
def test_filename_from_url(self):
self.assertEqual(filename_from_url(
url='http://example.org/foo',
content_type='text/plain'
), 'foo.txt')
self.assertEqual(filename_from_url(
url='http://example.org/foo',
content_type='text/html; charset=utf8'
), 'foo.html')
self.assertEqual(filename_from_url(
url='http://example.org/foo',
content_type=None
), 'foo')
self.assertEqual(filename_from_url(
url='http://example.org/foo',
content_type='x-foo/bar'
), 'foo')
def test_unique_filename(self):
def make_exists(unique_on_attempt=0):
# noinspection PyUnresolvedReferences,PyUnusedLocal
def exists(filename):
if exists.attempt == unique_on_attempt:
return False
exists.attempt += 1
return True
exists.attempt = 0
return exists
self.assertEqual(
get_unique_filename('foo.bar', exists=make_exists()),
'foo.bar'
)
self.assertEqual(
get_unique_filename('foo.bar', exists=make_exists(1)),
'foo.bar-1'
)
self.assertEqual(
get_unique_filename('foo.bar', exists=make_exists(10)),
'foo.bar-10'
)
class Response(object):
# noinspection PyDefaultArgument
def __init__(self, url, headers={}, status_code=200):
self.url = url
self.headers = CaseInsensitiveDict(headers)
self.status_code = status_code
# noinspection PyTypeChecker
class DownloadTest(BaseTestCase):
# TODO: more tests
def test_actual_download(self):
url = httpbin('/robots.txt')
body = urlopen(url).read().decode()
r = http(
'--download',
url,
env=TestEnvironment(
stdin_isatty=True,
stdout_isatty=False
)
)
self.assertIn('Downloading', r.stderr)
self.assertIn('[K', r.stderr)
self.assertIn('Done', r.stderr)
self.assertEqual(body, r)
def test_download_with_Content_Length(self):
download = Download(output_file=open(os.devnull, 'w'))
download.start(Response(
url=httpbin('/'),
headers={'Content-Length': 10}
))
time.sleep(1.1)
download.chunk_downloaded(b'12345')
time.sleep(1.1)
download.chunk_downloaded(b'12345')
download.finish()
self.assertFalse(download.interrupted)
def test_download_no_Content_Length(self):
download = Download(output_file=open(os.devnull, 'w'))
download.start(Response(url=httpbin('/')))
time.sleep(1.1)
download.chunk_downloaded(b'12345')
download.finish()
self.assertFalse(download.interrupted)
def test_download_interrupted(self):
download = Download(output_file=open(os.devnull, 'w'))
download.start(Response(
url=httpbin('/'),
headers={'Content-Length': 5}
))
download.chunk_downloaded(b'1234')
download.finish()
self.assertTrue(download.interrupted)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View File

@ -11,9 +11,3 @@ commands = {envpython} setup.py test
[testenv:py26] [testenv:py26]
deps = argparse deps = argparse
[testenv:py30]
deps = argparse
[testenv:py31]
deps = argparse