]> git.tdb.fi Git - libs/net.git/commitdiff
Add example of Http::Client usage
authorMikko Rasa <tdb@tdb.fi>
Wed, 10 Aug 2011 18:44:40 +0000 (21:44 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 10 Aug 2011 18:44:40 +0000 (21:44 +0300)
.gitignore
Build
examples/httpget.cpp [new file with mode: 0644]

index bee94e59390ccc980fccc80e09dee5755da6aa81..d16c915753cb82b919e385791baadfc5de25db73 100644 (file)
@@ -2,6 +2,7 @@
 /.options.*
 /.profile
 /debug
+/httpget
 /libmspnet.a
 /libmspnet.so
 /mspnet.pc
diff --git a/Build b/Build
index 1ed9db85befa7188f494ecf7db832e83285b4347..47032c49d60cceabb380eb2fd5e8dd4d467a72c4 100644 (file)
--- a/Build
+++ b/Build
@@ -36,4 +36,13 @@ package "mspnet"
                        library "mspnet";
                };
        };
+
+       program "httpget"
+       {
+               source "examples/httpget.cpp";
+               build_info
+               {
+                       library "mspnet";
+               };
+       };
 };
diff --git a/examples/httpget.cpp b/examples/httpget.cpp
new file mode 100644 (file)
index 0000000..70942b7
--- /dev/null
@@ -0,0 +1,58 @@
+#include <msp/core/application.h>
+#include <msp/core/getopt.h>
+#include <msp/http/client.h>
+#include <msp/http/request.h>
+#include <msp/http/response.h>
+#include <msp/io/print.h>
+
+using namespace std;
+using namespace Msp;
+
+class HttpGet: public RegisteredApplication<HttpGet>
+{
+private:
+       bool verbose;
+       string url;
+
+public:
+       HttpGet(int, char **);
+
+       virtual int main();
+};
+
+HttpGet::HttpGet(int argc, char **argv):
+       verbose(false)
+{
+       GetOpt getopt;
+       getopt.add_option('v', "verbose", verbose, GetOpt::NO_ARG);
+       getopt(argc, argv);
+
+       const vector<string> &args = getopt.get_args();
+       if(args.empty())
+               throw usage_error("No URL");
+
+       url = args.front();
+}
+
+int HttpGet::main()
+{
+       Http::Client client;
+
+       if(verbose)
+       {
+               IO::print("=== Sending request ===\n");
+               client.start_request(Http::Request::from_url(url));
+               IO::print(client.get_request()->str());
+               client.wait_response();
+               const Http::Response *response = client.get_response();
+               IO::print("=== Got response ===\n");
+               IO::print(response->str());
+       }
+       else
+       {
+               const Http::Response *response = client.get_url(url);
+               IO::print(response->get_content());
+       }
+
+       return 1;
+}