node-curl/lib/Curl.toffee
2013-05-25 16:00:35 +08:00

133 lines
3 KiB
Text

try
{Curl} = require __dirname + '/../build/Release/node-curl'
catch e
{Curl} = require __dirname + '/../build/default/node-curl'
Curl::setopt_user_ = (option_id, value) ->
@options[option_id] = value
Curl::setopt_httppost = (rows) ->
# convert object-rows to array-rows
# format
# [
# [OPTION_ID, VALUE]
# ]
@httppost = for row in rows
cols = []
for own k, v of row
k = k.toUpperCase()
if (option_id = Curl.httppost_options[k])?
cols.push option_id
unless v instanceof Buffer
v = new Buffer(v.toString())
cols.push v
else
throw new Error("invalid http post option #{k}")
cols
@setopt_httppost_(@httppost)
@
Curl::setopt = (option_name, value) ->
option = option_name.toUpperCase()
# slist must be at the top of condition
# the option exists in string_options too
if (option_id = Curl.user_options[option])?
if (option == 'MULTIPART')
@setopt_httppost value
else
@setopt_user_ option_id, value
else if (option_id = Curl.slist_options[option])?
@setopt_slist_ option_id, value
else if (option_id = Curl.integer_options[option])?
@setopt_int_ option_id, value >> 0
else if (option_id = Curl.string_options[option])?
if !value?
throw new Error("Cannot set option #{option_name} to #{value}.")
@setopt_str_ option_id, value.toString()
else
throw new Error("unsupported option #{option}")
@
Curl::getinfo = (oinfo) ->
info = oinfo.toUpperCase()
if (info_id = Curl.slist_infos[info])?
@getinfo_slist_(info_id)
else if (info_id = Curl.integer_infos[info])?
@getinfo_int_(info_id)
else if (info_id = Curl.string_infos[info])?
@getinfo_str_(info_id)
else if (info_id = Curl.double_infos[info])?
@getinfo_double_(info_id)
else
throw new Error("unsupported info #{oinfo}")
Curl.user_options =
RAW: 'RAW'
DEBUG: 'DEBUG'
MULTIPART: 'MULTIPART'
id = 0
curls = {}
# on 'data' must be returns the chunk length
Curl::on = (event, callback) ->
switch event
when 'data'
# (Buffer chunk) ->
@on_write = (chunk) =>
callback.call @, chunk
when 'header'
@on_header = (chunk) =>
callback.call @, chunk
when 'error'
# (Error error) ->
@on_error = (e) =>
delete curls[@id]
callback.call @, e
when 'end'
# () ->
@on_end = =>
delete curls[@id]
callback.call @
else
throw new Error("invalid event type #{event}")
@
Curl::close = () ->
delete curls[@id]
@close_()
Curl::perform = ->
@id = ++id
curls[@id] = @
@perform_()
Curl.process()
@
m = 0
p = console.log
Curl.process = ->
if Curl.in_process
return
do once = ->
n = Curl.process_()
if n > 0
Curl.in_process = true
if n > 8192 && m < 10
++m
process.nextTick once
else
m = 0
w = (8192 - n) * 80 / 8192 >> 0
if w < 0
w = 0
setTimeout once, w
else
Curl.in_process = false
module.exports = Curl
# vim: sw=2 ts=2 sts=2 expandtab :