WvStreams
wvhttpex.cc
00001 /*
00002  * A WvHttpStream example.
00003  *
00004  * This program downloads a file via http.
00005  * The expected result is:
00006  * http<Info>: Now in state 0
00007  * http<Info>: Now in state 1
00008  * http<Info>: [     0]
00009  * http<Info>: Now in state 2
00010  * http<Info>: [     0]
00011  * http<Info>: Now in state 3
00012  * http<Info>: [     0][     0][     0][     0][     0][     0][     0][     0][     0]
00013  * http<Info>: Now in state 4
00014  * http<Info>: [   751][   922][     0]
00015  *
00016  */
00017 #include "wvhttp.h"
00018 #include "wvistreamlist.h"
00019 #include "wvlog.h"
00020 #include "wvfile.h"
00021 
00022 
00023 int main(int argc, char **argv)
00024 {
00025     WvLog log("http", WvLog::Info);
00026     WvURL url("http://www.net-itech.com/");
00027     WvHTTPStream http(url);
00028     WvFile out("http.out", O_WRONLY | O_TRUNC | O_CREAT);
00029     WvHTTPStream::State last_state = WvHTTPStream::Done;
00030     static char buf[10240];
00031     size_t len;
00032 
00033     WvIStreamList l;
00034     l.add_after(l.tail, &http, false);
00035 
00036     while (http.isok() && out.isok())
00037     {
00038         if (last_state != http.state)
00039         {
00040             log("\nNow in state %s\n", http.state);
00041             last_state = http.state;
00042         }
00043 
00044         if (l.select(100))
00045             l.callback();
00046 
00047         if (http.select(0))
00048         {
00049             len = http.read(buf, sizeof(buf));
00050             out.write(buf, len);
00051             log("[%6s]", len);
00052         }
00053     }
00054 
00055     if (!http.isok() && http.geterr())
00056         log("http: %s\n", http.errstr());
00057 
00058     return 0;
00059 }