cute HTTP functions.
Andre Garzia
soapdog at mac.com
Wed Jul 26 02:09:53 EDT 2006
Hi Folks,
I built some cute HTTP functions that I think someone here might
like. They are very handy and are making me more productive so I
think they are some good candidates for sharing. I'll glue tem here
and explain below what they do and why should one use it.
---- Start Cute Functions ----
function http.headers pServer, pResource
put format("HEAD %s HTTP/1.1\nHost: %s\nUser-agent: Revolution",
pResource, pServer) into tHeaders
libUrlSetCustomHTTPHeaders tHeaders
get URL ("http://" & pServer)
put libUrlLastRHHeaders() into tResp
IF "200" is in line 1 of tResp THEN
replace ": " with numToChar(4) in tResp
delete line 1 of tResp
split tResp by cr AND numToChar(4)
return tResp
ELSE
return "Error" && word 2 to -1 of line 1 of tResp
END IF
END http.headers
function http.header pServer, pResource, pHeader
put http.headers(pServer, pResource) into tTempA
IF the keys of tTempA is empty THEN
return tTempA
ELSE
return tTempA[pHeader]
END IF
END http.header
function http.lastModificationDate pServer, pResource
return http.header(pServer, pResource, "Last-Modified")
END http.lastModificationDate
function http.type pServer, pResource
return http.header(pServer, pResource, "Content-type")
END http.type
function http.length pServer, pResource
return http.header(pServer, pResource, "Content-length")
END http.length
---- End Cute Functions ----
I am using dot notation on the function names to avoid collisions
with other people functions. Feel free to remove my dot notations.
The first function http.headers() accept two parameters a server, a
resource. Why I splited in two params when I could have used an URL,
well, this way you can use constants, you can declare a constant like
kServer = "andregarzia.com" and replace the file being asked without
changing the server. It's easier. This function returns an array with
the headers returned by the server or an error if the server returned
an error. This function is called by the ones below.
Function http.header() This function accepts three parameters, the
first two ones are server and resource and are passed to http.headers
() then the third one is an specific array element you're looking
for. This is another example of modular coding making functions that
build upon previous functions like a onion skin and thus moving the
functions to higher levels. For example, I want to know the ETag for
my index file on my server to check if I need to update my apps
cache, just call http.header("andregarzia.com", "index.html", "ETag").
There are headers that are always used, the most used headers in my
humble opinion are: Last-Modified, Type and Length. I use Last-
Modified to mantain my resources sync'd among different clients and
thus ensuring everything is always up to date. I use type check what
the server is sending me and length to see if I should use a chunked
connection or not.
This functions execute very fast because they are using HEAD method
instead of GET method, meaning they are not loading the resource they
are just checking the HTTP Headers the server would send case you
used GET. Suppose your app is needs to check if it should download
some file, first it would call http.lastModificationDate() to see if
it is up to date or not, if it is not, it might call http.length() to
check the size of the file before download, if the file is to big it
might download it using som asynchronous call instead of a blocking
call.
Anyway, I have use for this functions, someone on this list might
have too, so I decided to share. Those functions are just too simple
to be shared as a stack right now so I used copy & paste.
Happy HTTP Hacking
Andre Garzia
More information about the use-livecode
mailing list