- [github project] (https://github.com/geronime/curburger)
Curburger is configurable instance based User-Agent providing get/post requests using curb.
Configurable features:
- user-agent string settings
- per-instance proxy configuration
- enable cookies per-instance
- disable following of
Location:in HTTP response header - request connection timeout
- request timeout
- number of attempts for each request
- default instance configuration to retry 4XX/5XX responses
- random sleep time before retrying failed request
- per-instance request count per time period limitation
- default instance http authentication
- default instance SSL certificate verification
- default instance option to determine whether to ignore signal-based exceptions
require 'curburger'
c = Curburger.new({:opt => val})
logging- logging viaGLogg(defaulttrue); with logging disabled only errors/warnings are printed toSTDERR-
to completely disable logging leave
logging=trueand configureGLoggverbosity toGLogg::L_NILGLogg.ini(nil, GLogg::L_NIL)
-
user_agent- redefine instanceuser_agentstring (default is set bycurb)http_proxy- set instance proxy url (defaultnil)cookies- enable cookies for this instance (defaultfalse)http_auth- default instance http authentication credentials sent with requests (hash with keysuser,password, default{})follow_loc- followLocation:in HTTP response header (defaulttrue)verify_ssl- whether to verify SSL certificates (defaulttrue)retry_45- whether to retry 4XX/5XX responses (defaultfalse)ignore_kill- how to handle exceptions based on signaling- before 0.2.2 all exceptions were handled generally during requests
- when f.ex. Ctrl-c was pressed during the request, interruption exception
rose, followed by
Curl::Err::MultiBadEasyHandle: Invalid easy handleexceptions on retries - all these failures were uselessly retried as the Curl handle became invalid with no useful result anyway and all following requests with the same instance would fail as well
- when f.ex. Ctrl-c was pressed during the request, interruption exception
rose, followed by
- since 0.2.2 these interruption (and "Invalid easy handle") exceptions
are recognized and handled based on
ignore_killoption:false(default) - do not retry at all and immediately return the result hash with:errorkey settrue- reinitialize Curl handle and keep going, retry the current attempt not counting it
- before 0.2.2 all exceptions were handled generally during requests
req_ctimeout- connection timeout for the requests (default10)- this is the timeout for the connection to be established, not the timeout for the whole request & reply
req_timeout- request timeout (default20)req_attempts- number of attempts for the request (default3)req_retry_wait- maximal count of seconds to sleep before retrying failed request (defalut0, disabled)- e.g.
10= sleep random 1-10 seconds before retrying failed request
- e.g.
req_norecode- do not recode request results (defaultfalse)req_enc_ignore_illegal- ignore illegal bytes during request result recode (defaultfalse)req_limit- limit number of successful requests perreq_time_rangetime period (defaultnil)req_time_range- set requests limit time period in secondsresolve_mode- override resolving mode (default:ipv4)- possible options:
:auto,:ipv4,:ipv6 curldefault:automay generate frequentCurl::Err::HostResolutionErrorerrors for ipv4 only machine therefore Curburger uses:ipv4as default
- possible options:
user_agentuser_agent=- get/set currently configured instance
user_agent
- get/set currently configured instance
http_authhttp_auth=- get/set default authentication credentials (
nilclears the settings)
- get/set default authentication credentials (
Available request methods:
headgetpostputdelete
Request methods return hash with following keys/values:
:content- content of the response- header hash for
headrequest (decoded byheadersmethod) - recoded to UTF-8 if original encoding is successfully guessed
and recoding went without error, byte encoded original otherwise
(for more info refer to
Curburger::Recode.recode)
- header hash for
:ctype- appropriate response HTTP header value (empty string if missing):last_url- last effective url of the request (to recognize redirections):attempts- count of spent request attempts:responses- array[[status, time]]of all attempts:time- total processing time rounded to 6 decimal places:error- defined only in case of error: the last error is stored here
Request methods support following optional parameters:
userpassword- credentials for basic HTTP authentication (override instancehttp_authfor this request, defaultnil)follow_loc- redefine instancefollow_locfor this requestverify_ssl- redefine instanceverify_sslfor this requestretry_45- redefine instanceretry_45for this requestignore_kill- redefine instanceignore_killfor this requestctimeout- redefine instancereq_ctimeoutfor this requesttimeout- redefine instancereq_timeoutfor this requestattempts- redefine instancereq_attemptsfor this requestretry_wait- redefine instancereq_retry_waitfor this requestnorecode- redefine instancereq_norecodefor this requestencoding- force encoding for the response body (defaultnil)enc_ignore_illegal- redefine instancereq_enc_ignore_illegalfor this requestforce_ignorestill working as alias for this parameter
cookies- set additional cookies for the request ( defaultnil)- these are just passed to curl instance, therefore string in format
"name1=content1; name2=content;"
- these are just passed to curl instance, therefore string in format
headers- add custom HTTP headers to the request ( default{})- optional block given:
- relevant only in case of enabled request per time period limitation
- request method yields to execute the block before sleeping if the reqeust limit was reached
result = c.head(url, {opts}) { optional block ... }
result = c.get(url, {opts}) { optional block ... }
- since 0.2.6 the data can be posted in GET request using
opts[:data]opts[:data]is the same asdatafor POST/PUT/DELETE described below- optional
content_typeoption can be used as well
result = c.post(url, data, {opts}) { optional block ... }
result = c.put(url, data, {opts}) { optional block ... }
result = c.delete(url, data=nil, {opts}) { optional block ... }
dataparameter is expected inStringscalar orHashof{parameter => value}- posted direcly in case of
Stringscalar - url-encoded and assembled to scalar in case of
Hash - example:
'param1=value1¶m2=value2'or{:param1=>'value1', 'param2'=>'value2'}
- posted direcly in case of
- optional
content_typeoption overrides defaultapplication/x-www-form-urlencodedContent-Type HTTP POST header
To obtain headers of the last reply parsed into Hash use headers
instance method
headers = c.headers
- the first line (status line) is stored in
Statuskey of the returnedHash - multivalue headers are stored in an
Array
To reinitialize curl instance (cookies are flushed as well):
c.reset
- 0.2.8: migration from iconv to encode,
force_ignorerenamed toenc_ignore_illegalwith instance version of this parameter - 0.2.7: bugfix, curb 0.8 required in gemspec
- 0.2.6: instance/request
norecodeoptions, GET payload - 0.2.5:
resetmethod (reinitializecurlinstance) - 0.2.4: removal of 'Expect' HTTP header by default
- 0.2.3: hide iconv deprecation warning
- 0.2.2: instance/request
ignore_killoptions - 0.2.1: rescue 'ArgumentError: unknown encoding name' in Curburger::Recode
- 0.2.0: request methods return hash
- 0.1.8:
user_agentanduser_agent=get/set methods - 0.1.7: instance/request
retry_45options - 0.1.6: instance/request
verify_ssloptions - 0.1.5: empty string
content_typereturned from requests in case of missingContent-TypeHTTP header - 0.1.4: optional post data in DELETE request, bugfix
- 0.1.3: default instance http authentication
- 0.1.2:
:cookiesoption to set additional cookies for requests - 0.1.1:
:follow_locoption for requests; HEAD, PUT, DELETE requests - 0.1.0:
:headersoption for custom headers in requests - 0.0.9:
:resolve_modeinstance option - 0.0.8: removed "
require 'bundler/setup'" statements - 0.0.7:
headersinstance method - 0.0.6:
last_urlpart in request return array - 0.0.5:
:force_ignoreoption for requests - 0.0.4:
:content_typeoption for POST requests - 0.0.3: request timeout added (previously only connect timeout)
- 0.0.2: option for random sleep time before retrying failed request
- 0.0.1: first revision
Curburger is copyright (c)2011 Jiri Nemecek, and released under the terms of the MIT license. See the LICENSE file for the gory details.