Plugin http.spf

File: http.spf
Author: Nicholas Nemtsev
Description: Additional words allowing to use HTTP protocol in order to monitor changes in remote files, retrieve the date of last modification and even download these files to a local computer.

New Words:


HTTP-CHANGED: <URL> ( -- ?)

This word checks if a file located at the specified URL has changed since the last time when word HTTP-CHANGED: was executed. To perform this check, this word uses HTTP header Last-Modified. Word HTTP-CHANGED: will only work correctly if the remote HTTP server returns the Last-Modified header.

HTTP server responce code is written to user variable HTTP-RESULT.

When used for the first time, word HTTP-CHANGED: records the time of last modification in file etc\htime.txt, located in nnCron's home directory, and later compares the recorded time with those contained in HTTP header Last-Modified, returned by the remote server. If the times are different, word HTTP-CHANGED: returns TRUE (-1). Otherwise, it returns FALSE (0).

Please note that word HTTP-CHANGED: returns a flag. That means that it should be used with an IF ELSE THEN construct, or the value returned by FILE-CHANGE: should be explicitly cleared from the stack.

Example:

#( help_ru-notify
AsLoggedUser
OnceAHour
: help_ru S" http://www.nncron.ru/download/help_ru.zip" ;
Rule:
    ONLINE? DUP
    IF
       DROP
       HTTP-CHANGED: %help_ru% 
    THEN
Action:
    QUERY: "%help_ru% is changed.%crlf%Download it?"
    IF
        S" help_ru.zip" DELETE-FILE DROP
        ShowMinimized
        START-APP: wget %help_ru%
    THEN
)#

There also exists a postfix version of HTTP-CHANGED:

S" http://www.nncron.ru/index.html" HTTP-CHANGED

HTTP-GET: <URL> ( -- a u ior)

Returns error code of an error (if there is one) and a string with contents of a specified URL, in other words, downloads the file to the local computer. HTTP-GET: is mainly used to download html-pages in order to further process them (e.g. to search for some keywords). HTTP-GET: does not support resuming downloads, so it is not recommended to use it for downloading data in binary format (the best way to download binary files is to use utilities specifically designed for that purpose). However, it is still possible to use word HTTP-GET: to download a binary file if such a necessity arises. HTTP server responce code is written to user variable HTTP-RESULT.

Example:

Action:
     S" http://www.nncron.ru/download/help_ru.zip" HTTP-GET 0=
     IF
        S" help_ru.zip" FWRITE
     ELSE 2DROP THEN

There also exists a postfix version of HTTP-GET:

S" http://www.nncron.ru/index.html" HTTP-GET

HTTP-LM: <URL> ( -- a u ior)

Returns error code of an error (if there is one) and a string containing HTTP header Last-Modified from a remote HTTP server, i.e. the time of last last modification of a remote file. HTTP server responce code is written to user variable HTTP-RESULT.

Example:

#( test_http_lm
NoActive
Action:
    HTTP-LM: "http://www.nncron.ru/download/help_ru.zip" 0=
    IF
        2DUP
        \ printing date of last modification to console
        TYPE CR
        \ displaying a message box with the same date
        MsgBox
    ELSE
        2DROP
        MSG: "Error in HTTP-response"
    THEN
)#

There also exists a postfix version of HTTP-LM:

S" http://www.nncron.ru/index.html" HTTP-LM

HTTP-RESULT

Contains HTTP server responce code (after HTTP-CHANGED:, HTTP-GET: and HTTP-LM:).

Here are the most useful codes:

200 - HTTP server reports that operation was successful
404 - HTTP server can not find a specified URL
etc... (you can find the entire list of HTTP server responce codes in your HTTP server documentation).

Response code 0 means, that the problem occured is not related to HTTP server. There are good chances that you are experiencing network problems. When receiving response code 0, it's a good idea to take a look at HTTP-GET: and HTTP-LM: own error codes.

Example:

#( test_http-result
NoActive
Action:
    HTTP-CHANGED: "http://www.nncron.ru/download/help.zip"
    HTTP-RESULT 200 <>
    IF
        DROP
        MSG: "There was an error: %HTTP-RESULT%"
    ELSE
        IF
            MSG: "help.zip was changed"
        THEN
    THEN
)#

HTTPProxy: "proxy_server"

Specifies the name of the proxy-server to use with the words, defined in http.spf plugin. See also: HTTPProxyPort:. Note, please: this word defines a global variable, thus you should use it in nncron.ini.

Пример:

HTTPProxy: "proxy.provider.ru"

HTTPProxyPort: <port_number>

Specifies the number of the proxy-server port (see HTTPProxy:). Default value: 3128. Note, please: this word defines a global variable, thus you should use it in nncron.ini.

Пример:

HTTPProxyPort: 3100

HTTPProxy-Authorization: username:password

Specifies username and password for authorization on the proxy-server (see HTTPProxy:). Note, please: this word defines a global variable, thus you should use it in nncron.ini.

Пример:

HTTPProxy-Authorization: user:password

URL is a string containing the full address (URL) of a remote file in compliance with HTTP protocol.

Examples:

HTTP-CHANGED: "http://www.nncron.ru/index.html"
HTTP-LM: "http://www.nncron.ru/download/help_ru.zip"

Don't omit placing a slash at the URL's end if you are addressing not a single file, but an entire directory or a default page, such as index.htm, index.html etc.:

HTTP-CHANGED: "http://www.nncron.ru/"
HTTP-CHANGED: "http://www.nbk.orc.ru/nncron/"