DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
HTTP Client For Vim
// HTTP Client for vim
" require vimproc( http://tokyo.cool.ne.jp/hopper2/ )"
let g:HTTP = {}
function g:HTTP.new(host, ...)
let self.host = a:host
if a:0 >= 1
let self.port = a:1
else
let self.port = 80
endif
let self.headers = {'Host': self.host}
let self.query = {}
return deepcopy(self)
endfunction
function g:HTTP.get(path)
return self.access(a:path, 'GET')
endfunction
function g:HTTP.head(path)
return self.access(a:path, 'HEAD')
endfunction
function g:HTTP.access(path, method)
call g:vimproc.load()
let sock = g:vimproc.socket_open(self.host, self.port)
call sock.write(self.make_header(a:path, a:method))
let re = ""
while !sock.eof
let re .= sock.read()
endwhile
call g:vimproc.unload()
return g:HTTP.Response.new(re)
endfunction
function g:HTTP.make_header(path, method)
let hds = []
call add(hds, a:method . " " . a:path . " HTTP/1.0")
for key in keys(self.headers)
call add(hds, key . ": " . self.headers[key])
endfor
return join(hds, "\r\n") . "\r\n\r\n"
endfunction
let g:HTTP.Response = {}
function g:HTTP.Response.new(str)
call self.parse(a:str)
return deepcopy(self)
endfunction
function g:HTTP.Response.parse(str)
let lists = split(a:str, "\r\n\r\n")
let header_lists = split(lists[0], "\r\n")
let first = remove(header_lists, 0)
let self.code = matchstr(first, '[1-5]\d\d')
let self.headers = {}
for header in header_lists
let h = split(header, ': ')
let self.headers[h[0]] = join(h[1:], ': ')
endfor
let self.body = join(lists[1:], "\r\n\r\n")
endfunction
let h = HTTP.new('www.bigbold.com')
let res = h.get('/snippets/')
echo res.headers
if res.code < 400
echo res.body
else
echo 'error ' . res.code
endif





