function mgnetrequest_callback, status, progress, request
compile_opt strictarr
request->getProperty, debug=debug
if (debug) then begin
if (strpos(status, 'Verbose: Header Out: ') ne -1L) then begin
print, strmid(status, 22)
endif
endif
return, 1
end
pro mgnetrequest::_printf, lun, s
compile_opt strictarr
printf, lun, s
if (self.debug) then print, s
end
pro mgnetrequest::_sendHeaders, lun
compile_opt strictarr
headerKeys = self.headers->keys(count=nHeaders)
headerValues = self.headers->values()
hostname = self.headers->get('Host')
self->_printf, lun, 'Host: ' + hostname
for h = 0L, nHeaders - 1L do begin
if (headerKeys[h] eq 'Host') then continue
self->_printf, lun, headerKeys[h] + ': ' + headerValues[h]
endfor
end
pro mgnetrequest::_sendHeadersNetUrl, netUrl
compile_opt strictarr
headerKeys = self.headers->keys(count=nHeaders)
headerValues = self.headers->values()
for h = 0L, nHeaders - 1L do begin
if (headerKeys[h] eq 'Host') then continue
netUrl->setProperty, headers=headerKeys[h] + ': ' + headerValues[h]
endfor
end
pro mgnetrequest::_initializeHeaders
compile_opt strictarr
urlparts = parse_url(self.url)
self.headers->put, 'Host', urlparts.host
self.headers->put, 'User-agent', 'IDL-MGnetRequest/' + self.version
end
function mgnetrequest::_sendNetUrl, method, data, $
response_header=responseHeaderArray
compile_opt strictarr
on_error, 2
urlparts = parse_url(self.url)
netUrl = obj_new('IDLnetURL')
netUrl->setProperty, /verbose, $
callback_function='mgnetrequest_callback', $
callback_data=self
self->_sendHeadersNetUrl, netUrl
if (self.debug) then begin
print, urlparts.host, self.port, format='(%"connect: (%s, %d)\n")'
print, 'send: '''
endif
case strupcase(method) of
'GET': content = netUrl->get(url=self.url, /string_array)
'PUT': content = netUrl->put(data, url=self.url, /buffer)
'POST': content = netUrl->post(data, url=self.url, /post, /buffer)
else: message, 'method not supported'
endcase
if (self.debug) then begin
print, ''''
print
endif
netUrl->getProperty, response_header=responseHeaderArray
if (self.debug) then begin
print, 'response header: '''
print, responseHeaderArray
print, ''''
endif
obj_destroy, netUrl
return, content
end
function mgnetrequest::_send, method, data, response_header=responseHeaderArray
compile_opt strictarr
urlparts = parse_url(self.url)
socket, lun, urlparts.host, self.port, /get_lun
if (self.debug) then print, urlparts.host, self.port, format='(%"connect: (%s, %d)\n")'
if (self.debug) then print, 'send: '''
queryLocation = urlparts.path + (urlparts.query eq '' ? '' : '?') + urlparts.query
requestMethod = method + ' /' + queryLocation + ' HTTP/1.0'
self->_printf, lun, requestMethod
self->_sendHeaders, lun
if (self.debug) then print, ''''
self->_printf, lun, ''
responseHeader = obj_new('MGcoArrayList', type=7)
response = obj_new('MGcoArrayList', type=7)
inContent = 0B
if (self.debug) then print, 'response header: '''
line = ''
while (~eof(lun)) do begin
readf, lun, line
if (~inContent && line eq '') then begin
inContent = 1B
continue
endif
if (inContent) then begin
response->add, line
endif else begin
if (self.debug) then print, line
responseHeader->add, line
endelse
endwhile
if (self.debug) then print, ''''
free_lun, lun
responseArray = response->get(/all)
responseHeaderArray = responseHeader->get(/all)
obj_destroy, [responseHeader, response]
return, responseArray
end
function mgnetrequest::get, response_header=responseHeaderArray
compile_opt strictarr
on_error, 2
case strlowcase(self.scheme) of
'http': return, self->_send('GET', response_header=responseHeaderArray)
'https': return, self->_sendNetUrl('GET', response_header=responseHeaderArray)
else: message, 'scheme not implemented yet'
endcase
end
function mgnetrequest::head, response_header=responseHeaderArray
compile_opt strictarr
return, self->_send('HEAD', response_header=responseHeaderArray)
end
function mgnetrequest::put, data, response_header=responseHeaderArray
compile_opt strictarr
on_error, 2
case strlowcase(self.scheme) of
'http': return, self->_send('PUT', data, response_header=responseHeaderArray)
'https': return, self->_sendNetUrl('PUT', response_header=responseHeaderArray)
else: message, 'scheme not implemented yet'
endcase
end
function mgnetrequest::post, data, response_header=responseHeaderArray
compile_opt strictarr
on_error, 2
case strlowcase(self.scheme) of
'http': return, self->_send('POST', data, response_header=responseHeaderArray)
'https': return, self->_sendNetUrl('POST', response_header=responseHeaderArray)
else: message, 'scheme not implemented yet'
endcase
end
function mgnetrequest::delete, response_header=responseHeaderArray
compile_opt strictarr
on_error, 2
case strlowcase(self.scheme) of
'http': return, self->_send('DELETE', data, response_header=responseHeaderArray)
else: message, 'scheme not implemented yet'
endcase
end
function mgnetrequest::options, response_header=responseHeaderArray
compile_opt strictarr
on_error, 2
case strlowcase(self.scheme) of
'http': return, self->_send('OPTIONS', data, response_header=responseHeaderArray)
else: message, 'scheme not implemented yet'
endcase
end
function mgnetrequest::trace, response_header=responseHeaderArray
compile_opt strictarr
on_error, 2
case strlowcase(self.scheme) of
'http': return, self->_send('TRACE', data, response_header=responseHeaderArray)
else: message, 'scheme not implemented yet'
endcase
end
function mgnetrequest::connect, response_header=responseHeaderArray
compile_opt strictarr
on_error, 2
case strlowcase(self.scheme) of
'http': return, self->_send('CONNECT', data, response_header=responseHeaderArray)
else: message, 'scheme not implemented yet'
endcase
end
pro mgnetrequest::addHeader, key, value
compile_opt strictarr
self.headers->put, key, value
end
pro mgnetrequest::getProperty, debug=debug
compile_opt strictarr
if (arg_present(debug)) then debug = self.debug
end
pro mgnetrequest::setProperty, debug=debug
compile_opt strictarr
if (n_elements(debug) gt 0L) then self.debug = keyword_set(debug)
end
pro mgnetrequest::cleanup
compile_opt strictarr
obj_destroy, self.headers
end
function mgnetrequest::init, url_param, url=url, debug=debug
compile_opt strictarr
on_error, 2
self.version = '1.0'
self.url = n_params() gt 0L ? url_param : url
urlparts = parse_url(self.url)
if (urlparts.scheme eq '') then self.url = 'http://' + self.url
urlparts = parse_url(self.url)
self.scheme = urlparts.scheme
if (urlparts.port eq 80L) then begin
case urlparts.scheme of
'http': self.port = 80L
'https': self.port = 443L
else: message, 'unsupported scheme'
endcase
endif else begin
self.port = urlparts.port
endelse
self.debug = keyword_set(debug)
self.headers = obj_new('MGcoHashTable', key_type=7, value_type=7)
self->_initializeHeaders
return, 1
end
pro mgnetrequest__define
compile_opt strictarr
define = { MGnetRequest, $
version: '', $
url: '', $
port: 0L, $
scheme: '', $
headers: obj_new(), $
debug: 0B $
}
end
bk = obj_new('MGnetRequest', 'brightkite.com/people/mgalloy.xml')
bk->setProperty, debug=1
bk_response = bk->get(response_header=bk_header)
obj_destroy, bk
username = ''
read, username, prompt='Username: '
password = ''
read, password, prompt='Password: '
del = obj_new('MGnetRequest', 'https://api.del.icio.us/v1/posts/recent')
del->setProperty, debug=1
del->addHeader, 'Authorization', $
'Basic ' + mg_base64encode(username + ':' + password)
del_response = del->get(response_header=del_header)
obj_destroy, del
end