httpx 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +191 -0
  3. data/README.md +119 -0
  4. data/lib/httpx.rb +50 -0
  5. data/lib/httpx/buffer.rb +34 -0
  6. data/lib/httpx/callbacks.rb +32 -0
  7. data/lib/httpx/chainable.rb +51 -0
  8. data/lib/httpx/channel.rb +222 -0
  9. data/lib/httpx/channel/http1.rb +220 -0
  10. data/lib/httpx/channel/http2.rb +224 -0
  11. data/lib/httpx/client.rb +173 -0
  12. data/lib/httpx/connection.rb +74 -0
  13. data/lib/httpx/errors.rb +7 -0
  14. data/lib/httpx/extensions.rb +52 -0
  15. data/lib/httpx/headers.rb +152 -0
  16. data/lib/httpx/io.rb +240 -0
  17. data/lib/httpx/loggable.rb +11 -0
  18. data/lib/httpx/options.rb +138 -0
  19. data/lib/httpx/plugins/authentication.rb +14 -0
  20. data/lib/httpx/plugins/basic_authentication.rb +20 -0
  21. data/lib/httpx/plugins/compression.rb +123 -0
  22. data/lib/httpx/plugins/compression/brotli.rb +55 -0
  23. data/lib/httpx/plugins/compression/deflate.rb +50 -0
  24. data/lib/httpx/plugins/compression/gzip.rb +59 -0
  25. data/lib/httpx/plugins/cookies.rb +63 -0
  26. data/lib/httpx/plugins/digest_authentication.rb +141 -0
  27. data/lib/httpx/plugins/follow_redirects.rb +72 -0
  28. data/lib/httpx/plugins/h2c.rb +85 -0
  29. data/lib/httpx/plugins/proxy.rb +108 -0
  30. data/lib/httpx/plugins/proxy/http.rb +115 -0
  31. data/lib/httpx/plugins/proxy/socks4.rb +110 -0
  32. data/lib/httpx/plugins/proxy/socks5.rb +152 -0
  33. data/lib/httpx/plugins/push_promise.rb +67 -0
  34. data/lib/httpx/plugins/stream.rb +33 -0
  35. data/lib/httpx/registry.rb +88 -0
  36. data/lib/httpx/request.rb +222 -0
  37. data/lib/httpx/response.rb +225 -0
  38. data/lib/httpx/selector.rb +155 -0
  39. data/lib/httpx/timeout.rb +68 -0
  40. data/lib/httpx/transcoder.rb +12 -0
  41. data/lib/httpx/transcoder/body.rb +56 -0
  42. data/lib/httpx/transcoder/chunker.rb +38 -0
  43. data/lib/httpx/transcoder/form.rb +41 -0
  44. data/lib/httpx/transcoder/json.rb +36 -0
  45. data/lib/httpx/version.rb +5 -0
  46. metadata +150 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 76ac74f383584bdbf20b60f5a969ceb2827379fb9bd5d58bda52474b4ce22cdb
4
+ data.tar.gz: 045ae543497d77f74dfc7aea2e317fa5521b3b9bc31d4ca0733298d1555e773c
5
+ SHA512:
6
+ metadata.gz: f3986c2e0e756b2e41904f33eb3116067557c3eb82852d1d2c222a0d2afdf4281766a331216e3d95e69113cffbe812974b18793b90344dfc97e942d8b6a2fc33
7
+ data.tar.gz: 97f6ee0db636710c51850d359e82302a49a33c7f05d7584492e7a43e33fb7b04d7fc34e9e8209b4b763044362ef75326aa8b482140e9beda2113748b60a8e729
@@ -0,0 +1,191 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ https://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ Copyright 2013-2017 Docker, Inc.
180
+
181
+ Licensed under the Apache License, Version 2.0 (the "License");
182
+ you may not use this file except in compliance with the License.
183
+ You may obtain a copy of the License at
184
+
185
+ https://www.apache.org/licenses/LICENSE-2.0
186
+
187
+ Unless required by applicable law or agreed to in writing, software
188
+ distributed under the License is distributed on an "AS IS" BASIS,
189
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
190
+ See the License for the specific language governing permissions and
191
+ limitations under the License.
@@ -0,0 +1,119 @@
1
+ # HTTPX: A Ruby HTTP HTTPX for tomorrow... and beyond!
2
+
3
+ [![pipeline status](https://gitlab.com/honeyryderchuck/httpx/badges/master/pipeline.svg)](https://gitlab.com/honeyryderchuck/httpx/commits/master)
4
+ [![coverage report](https://gitlab.com/honeyryderchuck/httpx/badges/master/coverage.svg)](https://honeyryderchuck.gitlab.io/httpx/coverage/#_AllFiles)
5
+
6
+ HTTPX is an HTTP client library for the Ruby programming language.
7
+
8
+ Among its features, it supports:
9
+
10
+ * HTTP/2 and HTTP/1.x protocol versions
11
+ * Concurrent requests by default
12
+ * Simple and chainable API (based on HTTP.rb, itself based on Python Requests)
13
+ * Proxy Support (HTTP(S), Socks4/4a/5)
14
+ * Simple Timeout System
15
+ * Lightweight (explicit feature loading)
16
+
17
+ And among others
18
+
19
+ * Compression (gzip, deflate, brotli)
20
+ * Authentication (Basic Auth, Digest Auth)
21
+ * Cookies
22
+ * HTTP/2 Server Push
23
+ * H2C Upgrade
24
+ * Redirect following
25
+
26
+ ## How
27
+
28
+ Here are some simple examples:
29
+
30
+ ```ruby
31
+ HTTPX.get("https://nghttp2.org").to_s #=> "<!DOCT...."
32
+ ```
33
+
34
+ And that's the simplest one there is.
35
+
36
+ If you want to do some more things with the response, you can get an `HTTPX::Response`:
37
+
38
+ ```ruby
39
+ response = HTTPX.get("https://nghttp2.org")
40
+ puts response.status #=> 200
41
+ body = response.body
42
+ puts body #=> #<HTTPX::Response ...
43
+ ```
44
+
45
+ You can also send as many requests as you want simultaneously:
46
+
47
+ ```ruby
48
+ page1, page2, page3 = HTTPX.get("https://news.ycombinator.com/news", "https://news.ycombinator.com/news?p=2", "https://news.ycombinator.com/news?p=3")
49
+ ```
50
+
51
+ ## Installation
52
+
53
+ Add this line to your Gemfile:
54
+
55
+ ```ruby
56
+ gem "httpx"
57
+ ```
58
+
59
+ or install it in your system:
60
+
61
+ ```
62
+ > gem install httpx
63
+ ```
64
+
65
+ and then just require it in your program:
66
+
67
+ ```ruby
68
+ require "httpx"
69
+ ```
70
+
71
+ ## Why Should I care?
72
+
73
+ In Ruby, HTTP client implementations are a known cheap commodity. Why this one?
74
+
75
+ ### Concurrency
76
+
77
+ This library supports HTTP/2 seamlessly (which means, if the request is secure, and the server support ALPN negotiation AND HTTP/2, the request will be made through HTTP/2). If you pass multiple URIs, and they can utilize the same connection, they will run concurrently in it.
78
+
79
+ However if the server supports HTTP/1.1, it will try to use HTTP pipelining, falling back to 1 request at a time if the server doesn't support it (if the server support Keep-Alive connections, it will reuse the same connection).
80
+
81
+ ### Clean API
82
+
83
+ `HTTPX` acknowledges the ease-of-use of the [http gem](https://github.com/httprb/http) API (itself inspired by python [requests](http://docs.python-requests.org/en/latest/) library). It therefore aims at reusing the same facade, extending it for the use cases which the http gem doesn't support.
84
+
85
+ ### Lightweight
86
+
87
+ It ships with a plugin system similar to the ones used by [sequel](https://github.com/jeremyevans/sequel), [roda](https://github.com/jeremyevans/roda) or [shrine](https://github.com/janko-m/shrine).
88
+
89
+ It means that it loads the bare minimum to perform requests, and the user has to explicitly load the plugins, in order to get the features he/she needs.
90
+
91
+ It also means that it ships with the minimum amount of dependencies.
92
+
93
+ ## Supported Rubies
94
+
95
+ All Rubies greater or equal to 2.1, and always latest JRuby.
96
+
97
+ ## Resources
98
+
99
+ | Website | https://honeyryderchuck.gitlab.io/httpx/ |
100
+ | Documentation | https://honeyryderchuck.gitlab.io/httpx/rdoc/ |
101
+ | Wiki | https://gitlab.com/honeyryderchuck/httpx/wikis/home |
102
+ | CI | https://gitlab.com/honeyryderchuck/httpx/pipelines |
103
+
104
+ ## Caveats
105
+
106
+ `HTTPS` TLS backend is ruby's own `openssl` gem.
107
+
108
+ If your requirement is to run requests over HTTP/2 and TLS, make sure you run a version of the gem which compiles OpenSSL 1.0.2 (Ruby 2.3 and higher are guaranteed to).
109
+
110
+ JRuby's `openssl` is based on Bouncy Castle, which is massively outdated and still doesn't implement ALPN. So HTTP/2 over TLS/ALPN negotiation is off until JRuby figures this out.
111
+
112
+ ## Contributing
113
+
114
+ * Discuss your contribution in an issue
115
+ * Fork it
116
+ * Make your changes, add some test
117
+ * Ensure all tests pass (`bundle exec rake test`)
118
+ * Open a Merge Request (that's Pull Request in Github-ish)
119
+ * Wait for feedback
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "httpx/version"
4
+
5
+ require "httpx/extensions"
6
+
7
+ require "httpx/errors"
8
+ require "httpx/callbacks"
9
+ require "httpx/loggable"
10
+ require "httpx/registry"
11
+ require "httpx/transcoder"
12
+ require "httpx/options"
13
+ require "httpx/timeout"
14
+ require "httpx/connection"
15
+ require "httpx/headers"
16
+ require "httpx/request"
17
+ require "httpx/response"
18
+ require "httpx/chainable"
19
+ require "httpx/client"
20
+
21
+ # Top-Level Namespace
22
+ #
23
+ module HTTPX
24
+ # All plugins should be stored under this module/namespace. Can register and load
25
+ # plugins.
26
+ #
27
+ module Plugins
28
+ @plugins = {}
29
+
30
+ # Loads a plugin based on a name. If the plugin hasn't been loaded, tries to load
31
+ # it from the load path under "httpx/plugins/" directory.
32
+ #
33
+ def self.load_plugin(name)
34
+ h = @plugins
35
+ unless (plugin = h[name])
36
+ require "httpx/plugins/#{name}"
37
+ raise "Plugin #{name} hasn't been registered" unless (plugin = h[name])
38
+ end
39
+ plugin
40
+ end
41
+
42
+ # Registers a plugin (+mod+) in the central store indexed by +name+.
43
+ #
44
+ def self.register_plugin(name, mod)
45
+ @plugins[name] = mod
46
+ end
47
+ end
48
+
49
+ extend Chainable
50
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "forwardable"
4
+
5
+ module HTTPX
6
+ class Buffer
7
+ extend Forwardable
8
+
9
+ def_delegator :@buffer, :<<
10
+
11
+ def_delegator :@buffer, :to_s
12
+
13
+ def_delegator :@buffer, :to_str
14
+
15
+ def_delegator :@buffer, :empty?
16
+
17
+ def_delegator :@buffer, :bytesize
18
+
19
+ def_delegator :@buffer, :slice!
20
+
21
+ def_delegator :@buffer, :clear
22
+
23
+ def_delegator :@buffer, :replace
24
+
25
+ def initialize(limit)
26
+ @buffer = "".b
27
+ @limit = limit
28
+ end
29
+
30
+ def full?
31
+ @buffer.bytesize >= @limit
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HTTPX
4
+ module Callbacks
5
+ def on(type, &action)
6
+ callbacks(type) << action
7
+ end
8
+
9
+ def once(event, &block)
10
+ on(event) do |*args, &callback|
11
+ block.call(*args, &callback)
12
+ :delete
13
+ end
14
+ end
15
+
16
+ def emit(type, *args)
17
+ callbacks(type).delete_if { |pr| pr[*args] == :delete }
18
+ end
19
+
20
+ protected
21
+
22
+ def inherit_callbacks(callbackable)
23
+ @callbacks = callbackable.callbacks
24
+ end
25
+
26
+ def callbacks(type = nil)
27
+ return @callbacks unless type
28
+ @callbacks ||= Hash.new { |h, k| h[k] = [] }
29
+ @callbacks[type]
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HTTPX
4
+ module Chainable
5
+ %i[head get post put delete trace options connect patch].each do |meth|
6
+ define_method meth do |*uri, **options|
7
+ request(meth, uri, **options)
8
+ end
9
+ end
10
+
11
+ def request(verb, uri, **options)
12
+ branch(default_options).request(verb, uri, **options)
13
+ end
14
+
15
+ def timeout(**args)
16
+ branch(timeout: args)
17
+ end
18
+
19
+ def headers(headers)
20
+ branch(default_options.with_headers(headers))
21
+ end
22
+
23
+ def accept(type)
24
+ headers("accept" => String(type))
25
+ end
26
+
27
+ def plugin(*plugins)
28
+ klass = is_a?(Client) ? self.class : Client
29
+ klass = Class.new(klass)
30
+ klass.instance_variable_set(:@default_options, klass.default_options.merge(default_options))
31
+ klass.plugins(plugins).new
32
+ end
33
+ alias_method :plugins, :plugin
34
+
35
+ def with(options)
36
+ branch(default_options.merge(options))
37
+ end
38
+
39
+ private
40
+
41
+ def default_options
42
+ @options || Options.new
43
+ end
44
+
45
+ # :nodoc:
46
+ def branch(options)
47
+ return self.class.new(options) if is_a?(Client)
48
+ Client.new(options)
49
+ end
50
+ end
51
+ end