6 quvi-object - Overview of the libquvi quvi-object
10 'quvi-object' is a collection of libquvi functions provided for the
11 linkman:libquvi-scripts[7]. These functions vary from HTTP functions to
12 cryptographic functions. All of the 'quvi-object' functions are
15 NOTE: The 'quvi-object' should not be confused with the
16 linkman:quvi-modules[7] which are a selection of importable
17 modules implemented in Lua. These modules are intended to be
18 loaded ('require') from the linkman:libquvi-scripts[7].
22 Each of the quvi-object functions may be passed a dictionary defining
23 the additional option properties. The following options are supported by
26 qoo_croak_if_error=<boolean>::
27 By default the library terminates the process if an error occurs. By
28 setting this option to 'false', it will register the error and
29 continues execution, leaving the error handling for the script to
30 determine. See also "RETURN VALUES".
32 The 'qoo' prefix is short for 'quvi object option'. The functions will
33 ignore any unknown options, e.g. the crypto functions would ignore the
36 NOTE: The options have been defined in the 'quvi/const' of
37 the linkman:quvi-modules[7]
42 These options are specific to the quvi.crypto.* functions of the
45 qoo_crypto_algorithm=<value>::
46 Specifies the algorithm that should be used. The 'value' is expected to
47 be a string, e.g. 'sha1' or 'aes256'.
49 qoo_crypto_cipher_flags=<value>::
50 Specifies the cipher flags (bit OR'd, see
51 linkman:quvi-modules-3rdparty[7] for 'bit operations') to be used with
52 the cipher. These values are identical to those defined by libgcrypt.
53 See the 'quvi/const' of the linkman:quvi-modules[7] for a complete
54 list of the available cipher flags.
56 qoo_crypto_cipher_mode=<value>::
57 Specifies the cipher mode to be used. These are identical to the
58 values defined by libgcrypt. See the 'quvi/const' of the
59 linkman:quvi-modules[7] for a complete list of the available cipher
62 qoo_crypto_cipher_key=<value>::
63 Used to specify the cipher key. It should be noted that the 'key' is
64 expected to be passed in hexadecimal form. See 'quvi/hex' of the
65 linkman:quvi-modules[7] for the conversion functions.
67 NOTE: The key derivation is left for the script to do
69 NOTE: The key is not a pass{word,phrase}
72 http://www.di-mgt.com.au/cryptokeys.html#passwordpassphraseandkey
77 These options are specific to the quvi.http.* functions of the
80 qoo_fetch_from_charset=<charset>::
81 Instructs the library to convert from this charset to UTF-8. Using this
82 option may be required with the websites that use a specific (non-UTF8)
85 The purpose of this option is to make sure that the data is encoded to
86 unicode (UTF-8) before any of it is parsed and returned to the
87 application using libquvi.
89 By default, libquvi converts the data which is in the encoding used for
90 the strings by the C runtime in the current locale into UTF-8. IF this
91 fails, and the 'from charset' option is set, the library will then try
92 to convert to UTF-8 using the 'from charset' value.
94 qoo_http_cookie_mode=<value>::
95 Modify the cookie settings for the libcurl session handle. This
96 feature wraps the cookie features provided by
97 linkman:libcurl_easy_setopt[3]. See the 'quvi/const' of the
98 linkman:quvi-modules[7] for a complete list of the available cookie
101 See also linkman:libcurl-tutorial[3] which covers the use of cookies
102 with the library in greater detail.
106 Each quvi-object function will return a dictionary containing the
110 The error message, or an empty value.
113 The code returned by the library. See also 'quvi/const'
114 of linkman:quvi-modules[7].
116 Refer to the function documentation for the information about the additional
117 values returned in the dictionary.
125 quvi.base64.encode(<plaintext>[,qoo])::
126 Encode the 'plaintext' to base64 format. The 'plaintext' is expected
127 to be passed in hexadecimal form. See 'quvi/hex' of the
128 linkman:quvi-modules[7] for the conversion functions.
130 The second argument ('qoo') is expected to be a dictionary of
131 additional 'quvi object options', if defined at all.
133 The function will return the 'base64' value in the dictionary.
135 quvi.base64.decode(<base64>)::
136 Decode the 'base64' value to plaintext.
138 The function will return the 'plaintext' value in the dictionary.
139 The value is returned in hexadecimal form.
144 The crypto facility of the quvi-object wraps the libgcrypt symmetric
145 cryptography and the hash (message digest) functions.
147 NOTE: The availability of the algorithms is determined by libgcrypt, and
150 quvi.crypto.encrypt(<plaintext>, <qoo>)::
151 Encrypt the 'plaintext'. The 'plaintext' is expected to be passed in
152 hexadecimal form. See 'quvi/hex' of the linkman:quvi-modules[7] for
153 the conversion functions.
155 The second argument ('qoo') is expected to be a dictionary containing
158 The function will return the 'ciphertext' value in the dictionary.
159 The value is returned in hexadecimal form.
161 quvi.crypto.decrypt(<ciphertext>, <qoo>)::
162 Decrypt the 'ciphertext'. The 'ciphertext' is expected to be passed in
163 hexadecimal form. See 'quvi/hex' of the linkman:quvi-modules[7] for
164 the conversion functions.
166 The second argument ('qoo') is expected to be a dictionary containing
169 The function will return the 'plaintext' value in the dictionary.
170 This value is returned in hexadecimal form.
172 quvi.crypto.hash(<value>, <qoo>)::
173 Generate a hash from the 'value'. The 'value' is expected to be passed
174 in hexadecimal format. See 'quvi/hex' of the linkman:quvi-modules[7]
175 for the conversion functions.
177 The second argument ('qoo') is expected to be a dictionary containing
178 the hash options, e.g. the algorithm that should be used.
180 The function will return the 'digest' value in the dictionary.
181 The value is returned in hexadecimal form.
186 The HTTP functions will return 'response_code' along the other "RETURN
187 VALUES", and the values specific to the HTTP function.
189 quvi.http.cookie(<VALUE>,<qoo>)::
190 Modify the libcurl session handle cookie settings that libquvi
193 The second argument ('qoo') is expected to be a dictionary containing
194 the cookie options, e.g. the cookie mode that should be used.
196 The complete list of the available cookie modes can be found in the
197 'quvi/const' module of the linkman:quvi-modules[7]. The mode names are
198 named after their equivalent CURLOPT_COOKIE{SESSION,FILE,LIST,JAR}
199 variable names. For a description of each option, see
200 linkman:libcurl_easy_setopt[3].
202 This function will not return any additional values in the dictionary.
204 NOTE: Cookies are always enabled for the current libcurl session handle,
205 meaning that libcurl will parse the received cookies and use them
206 in the subsequent HTTP requests
208 quvi.http.fetch(<URL>[,qoo])::
209 Fetch an URL over an HTTP connection.
211 The second argument ('qoo') is expected to be a dictionary of
212 additional 'quvi object options', if defined at all.
214 The function will return the 'data' value among the values in
215 the returned dictionary.
217 quvi.http.header(<VALUE>[,qoo])::
218 Add, remove or replace internally used libcurl HTTP headers.
220 The second argument ('qoo') is expected to be a dictionary of
221 additional 'quvi object options', if defined at all.
223 This function essentially wraps CURLOPT_HTTPHEADER, and will not
224 return any additional values in the dictionary. See
225 linkman:curl_easy_setopt[3] for the full description of
228 NOTE: To clear the custom headers, pass "" as the VALUE; the custom
229 headers are also cleared automatically when a support script
230 function 'parse' is called
232 quvi.http.metainfo(<URL>[,qoo])::
233 Query the HTTP metainfo for the URL.
235 The second argument ('qoo') is expected to be a dictionary of
236 additional 'quvi object options', if defined at all.
238 The function will return the 'content_length' and the 'content_type'
239 values among the values in the returned dictionary.
241 quvi.http.resolve(<URL>[,qoo])::
242 Resolve an URL redirection.
244 The second argument ('qoo') is expected to be a dictionary of
245 additional 'quvi object options', if defined at all.
247 The function will return the 'resolved_url' among the values in the
248 returned dictionary. If the URL did not redirect to another location,
249 the value of the 'resolved_url' is left empty.
257 * Base64 encode a string:
260 local H = require 'quvi/hex'
261 local s = H.to_hex('foo') -- Pass in hexadecimal form
263 local r = quvi.base64.encode(s)
267 * Reverse the process:
270 local r = quvi.base64.decode(r.base64)
271 local s = H.to_str(r.plaintext)
280 local plaintext = 'f34481ec3cc627bacd5dc3fb08f273e6'
281 local key = '00000000000000000000000000000000'
283 local C = require 'quvi/const'
285 [C.qoo_crypto_cipher_mode] = C.qoco_cipher_mode_cbc,
286 [C.qoo_crypto_algorithm] = 'aes',
287 [C.qoo_crypto_cipher_key = key
290 local r = quvi.crypto.encrypt(plaintext, o)
294 * Reverse the process:
297 local r = quvi.crypto.decrypt(r.ciphertext, o)
301 * Generate a hash (message digest):
304 local H = require 'quvi/hex'
305 local s = H.to_hex('foo') -- Pass in hexadecimal form
307 local C = require 'quvi/const'
308 local o = { [C.qoo_crypto_algorithm] = 'sha1' }
310 local r = quvi.crypto.hash(s, o)
314 * Same as above, but use the shorthand function:
317 local A = require 'quvi/hash'
318 local r = A.sha1sum('foo')
325 * Dump the cookies in the memory to stdout:
328 local C = require 'quvi/const'
329 local o = { [C.qoo_http_cookie_mode] = C.qohco_mode_jar }
330 local r = quvi.http.cookie('-', o)
333 * Identical to the above but use the wrapper module:
336 local K = require 'quvi/http/cookie'
343 local r = quvi.http.fetch('http://example.com')
346 r.data would now hold the contents. If an error occurred, e.g.
347 connection failed, the library would exit the process with an error.
349 * Same as above, but handle the error in the script:
352 local C = require 'quvi/const'
353 local o = { [C.qoo_croak_if_error] = false }
355 local r = quvi.http.fetch('http://example.com', o)
356 if r.quvi_code ~= C.qerr_ok then
358 string.format('quvi.http.fetch: %s (libquvi=%d, http/%d)',
359 r.error_message, r.quvi_code, r.response_code)
364 By setting qoo_croak_if_error to 'false', we tell the library to
365 only register that an error occurred and return the control to
366 the script. Handling of the error is then left for the script to do.
368 NOTE: Typically, the scripts would not need to handle the error
370 * Force conversion from ISO-8859-1 to UTF-8:
373 local C = require 'quvi/const'
374 local o = { [C.qoo_fetch_from_charset] = 'ISO-8859-1' }
375 local r = quvi.http.fetch('http://example.com', o)
378 * Override user-agent header in the HTTP request:
381 local r = quvi.http.header('User-Agent: foo/1.0')
382 r = quvi.http.fetch(...)
385 * Disable an internal header in the HTTP request:
388 local r = quvi.http.header('Accept:')
389 r = quvi.http.fetch(...)
392 * Send a cookie in the HTTP request:
395 local r = quvi.http.header('Cookie: foo=1')
396 r = quvi.http.fetch(...)
399 * Query metainfo for an URL:
402 local r = quvi.http.metainfo('http://is.gd/SKyg8m')
403 print(r.content_length, r.content_type)
406 * Resolve URL redirection:
409 local r = quvi.http.resolve('http://is.gd/SKyg8m')
410 if #r.resolved_url >0 then
411 print('new location:', r.resolved_url)
417 linkman:libquvi-scripts[7], linkman:libquvi[3], linkman:quvi-modules[7],
418 linkman:quvi-modules-3rdparty[7]
420 include::../footer.txt[]