]> git.tdb.fi Git - libs/net.git/commitdiff
Rename data to content in Message
authorMikko Rasa <tdb@tdb.fi>
Wed, 24 Dec 2008 07:26:59 +0000 (07:26 +0000)
committerMikko Rasa <tdb@tdb.fi>
Wed, 24 Dec 2008 07:26:59 +0000 (07:26 +0000)
Make Client::use_event_dispatcher more robust
Make the Status enum values all uppercase
Move the typedef of HeaderMap inside Message
Rename misc.* to version.*
Add a user_data field to Message and copy it rom request to response in Client
Update class member ordering

13 files changed:
source/client.cpp
source/client.h
source/message.cpp
source/message.h
source/misc.cpp [deleted file]
source/misc.h [deleted file]
source/request.h
source/response.cpp
source/response.h
source/status.cpp
source/status.h
source/version.cpp [new file with mode: 0644]
source/version.h [new file with mode: 0644]

index fde7c8d7bd20f48d10ea1f51dda748bb092249a9..a4bc342f38c2738c27047f6a80818196fbd17396 100644 (file)
@@ -36,8 +36,10 @@ Client::~Client()
 
 void Client::use_event_dispatcher(IO::EventDispatcher *ed)
 {
+       if(event_disp && sock)
+               event_disp->remove(*sock);
        event_disp=ed;
-       if(sock)
+       if(event_disp && sock)
                event_disp->add(*sock);
 }
 
@@ -66,10 +68,11 @@ void Client::start_request(const Request &r)
        in_buf.clear();
 }
 
-void Client::get_url(const std::string &url)
+const Response *Client::get_url(const std::string &url)
 {
        start_request(Request::from_url(url));
        wait_response();
+       return response;
 }
 
 void Client::tick()
@@ -130,12 +133,13 @@ void Client::data_available()
                if(in_buf.find("\r\n\r\n")!=string::npos || in_buf.find("\n\n")!=string::npos)
                {
                        response=new Response(Response::parse(in_buf));
+                       response->set_user_data(request->get_user_data());
                        in_buf=string();
                }
        }
        else
        {
-               len=response->parse_data(in_buf);
+               len=response->parse_content(in_buf);
                in_buf.erase(0, len);
        }
 
index d72cbadf8333a486adc201696a78ed323738def4..2554ac432c7cbed367313f170313fc559afd0956 100644 (file)
@@ -21,6 +21,10 @@ class Response;
 
 class Client
 {
+public:
+       sigc::signal<void, const Response &> signal_response_complete;
+       sigc::signal<void, int> signal_socket_error;
+
 private:
        Net::StreamSocket *sock;
        IO::EventDispatcher *event_disp;
@@ -28,11 +32,6 @@ private:
        Response *response;
        std::string in_buf;
 
-public:
-       sigc::signal<void, const Response &> signal_response_complete;
-       sigc::signal<void, int> signal_socket_error;
-
-private:
        Client(const Client &);
        Client &operator=(const Client &);
 public:
@@ -41,7 +40,7 @@ public:
 
        void use_event_dispatcher(IO::EventDispatcher *);
        void start_request(const Request &);
-       void get_url(const std::string &);
+       const Response *get_url(const std::string &);
        void tick();
        void wait_response();
        void abort();
index a8851fa31c4b5b86f9b7df6ff2c0ec60d457ba04..e0a5e7e171bf8afad01cf8932e4ca01acfb720b1 100644 (file)
@@ -31,15 +31,20 @@ const string &Message::get_header(const string &hdr) const
        return i->second;
 }
 
-void Message::add_data(const string &d)
+void Message::add_content(const string &d)
 {
-       data+=d;
+       content+=d;
        if(headers.count("content-type")==0)
                set_header("content-type", "text/plain");
-       set_header("content-length", lexical_cast(data.size()));
+       set_header("content-length", lexical_cast(content.size()));
 }
 
-unsigned Message::parse_data(const string &d)
+void Message::set_user_data(const Variant &d)
+{
+       user_data=d;
+}
+
+unsigned Message::parse_content(const string &d)
 {
        if(complete)
                return 0;
@@ -47,10 +52,10 @@ unsigned Message::parse_data(const string &d)
        HeaderMap::const_iterator i=headers.find("content-length");
        if(i!=headers.end())
        {
-               unsigned needed=lexical_cast<unsigned>(i->second)-data.size();
+               unsigned needed=lexical_cast<unsigned>(i->second)-content.size();
                unsigned len=min(needed, d.size());
                
-               data.append(d, 0, len);
+               content.append(d, 0, len);
 
                if(len==needed)
                        complete=true;
@@ -69,8 +74,7 @@ unsigned Message::parse_data(const string &d)
                                unsigned lf=d.find('\n', pos);
                                if(lf==string::npos)
                                        return pos;
-                               // XXX strtoul
-                               chunk_length=strtoul(strip(d.substr(pos, lf-pos)).c_str(), 0, 16);
+                               chunk_length=lexical_cast<unsigned>(strip(d.substr(pos, lf-pos)), "x");
                                if(chunk_length==0)
                                        complete=true;
                                pos=lf+1;
@@ -78,7 +82,7 @@ unsigned Message::parse_data(const string &d)
                        else
                        {
                                unsigned len=min(chunk_length, d.size()-pos);
-                               data.append(d, pos, len);
+                               content.append(d, pos, len);
                                chunk_length-=len;
                                if((pos=d.find('\n', pos+len))!=string::npos)
                                        ++pos;
@@ -104,7 +108,7 @@ string Message::str_common() const
        for(HeaderMap::const_iterator i=headers.begin(); i!=headers.end(); ++i)
                result+=format("%s: %s\r\n", i->first, i->second);
        result+="\r\n";
-       result+=data;
+       result+=content;
 
        return result;
 }
index c9dc618a61eb31ce05e972a89f09f49d38ce792c..1ccc976ac71c76c3a8517e181a79ac2ab54f27c6 100644 (file)
@@ -8,32 +8,40 @@ Distributed under the LGPL
 #ifndef MSP_HTTP_MESSAGE_H_
 #define MSP_HTTP_MESSAGE_H_
 
+#include <map>
 #include <string>
-#include "misc.h"
+#include <msp/core/variant.h>
+#include "version.h"
 
 namespace Msp {
 namespace Http {
 
 class Message
 {
+protected:
+       typedef std::map<std::string, std::string> HeaderMap;
+
+       Version http_version;
+       HeaderMap headers;
+       std::string content;
+       unsigned chunk_length;
+       bool complete;
+       Variant user_data;
+
+       Message();
 public:
        virtual ~Message() { }
 
        void set_header(const std::string &, const std::string &);
        const std::string &get_header(const std::string &) const;
-       const std::string &get_data() const { return data; }
+       void add_content(const std::string &);
+       const std::string &get_content() const { return content; }
+       void set_user_data(const Variant &);
+       const Variant &get_user_data() const { return user_data; }
        bool get_complete() const { return complete; }
-       void add_data(const std::string &);
-       unsigned parse_data(const std::string &);
+       unsigned parse_content(const std::string &);
        virtual std::string str() const =0;
 protected:
-       Version http_version;
-       HeaderMap headers;
-       std::string data;
-       unsigned chunk_length;
-       bool complete;
-
-       Message();
        std::string str_common() const;
 };
 
diff --git a/source/misc.cpp b/source/misc.cpp
deleted file mode 100644 (file)
index 6ac7c5d..0000000
+++ /dev/null
@@ -1,32 +0,0 @@
-/* $Id$
-
-This file is part of libmsphttp
-Copyright © 2008  Mikkosoft Productions, Mikko Rasa
-Distributed under the LGPL
-*/
-
-#include <msp/strings/formatter.h>
-#include <msp/strings/lexicalcast.h>
-#include <msp/strings/regex.h>
-#include "misc.h"
-
-using namespace std;
-
-namespace Msp {
-namespace Http {
-
-Version parse_version(const std::string &ver)
-{
-       if(RegMatch match=Regex("^HTTP/([0-9]+).([0-9]+)$").match(ver))
-               return lexical_cast<unsigned>(match[1].str)<<4 | lexical_cast<unsigned>(match[2].str);
-       else
-               throw InvalidParameterValue("Invalid HTTP version");
-}
-
-string version_str(Version ver)
-{
-       return format("HTTP/%u.%u", (ver>>4)&0xF, ver&0xF);
-}
-
-} // namespace Http
-} // namespace Msp
diff --git a/source/misc.h b/source/misc.h
deleted file mode 100644 (file)
index 8e7ed79..0000000
+++ /dev/null
@@ -1,26 +0,0 @@
-/* $Id$
-
-This file is part of libmsphttp
-Copyright © 2008  Mikkosoft Productions, Mikko Rasa
-Distributed under the LGPL
-*/
-
-#ifndef MSP_HTTP_MISC_H_
-#define MSP_HTTP_MISC_H_
-
-#include <map>
-#include <string>
-
-namespace Msp {
-namespace Http {
-
-typedef std::map<std::string, std::string> HeaderMap;
-typedef unsigned Version;
-
-Version parse_version(const std::string &);
-std::string version_str(Version);
-
-} // namespace Http
-} // namespace Msp
-
-#endif
index 6f713abf5b526f6a402c5c11bcede10adeb607d9..26d668f95f5d931f9a8b36a1b7f78722d6f35b59 100644 (file)
@@ -16,15 +16,16 @@ namespace Http {
 
 class Request: public Message
 {
+private:
+       std::string method;
+       std::string path;
+
 public:
        Request(const std::string &, const std::string &);
        virtual std::string str() const;
 
        static Request parse(const std::string &);
        static Request from_url(const std::string &);
-private:
-       std::string method;
-       std::string path;
 };
 
 } // namespace Http
index 86d06baf07f42c99097ea3733d0dce6f6d3935e4..0defebd5afb12a7fe3297649bd65b428e5bef22a 100644 (file)
@@ -56,7 +56,7 @@ Response Response::parse(const string &str)
                start=lf+1;
        }
 
-       result.parse_data(str.substr(lf+1));
+       result.parse_content(str.substr(lf+1));
 
        return result;
 }
index 20e144e8afa6d0936084f7b693c4dabde04289fb..abd370ec3825503d1ef684889e5770f8ac044621 100644 (file)
@@ -16,16 +16,16 @@ namespace Http {
 
 class Response: public Message
 {
+private:
+       Status status;
+
+       Response() { }
 public:
        Response(Status);
        Status get_status() const { return status; }
        virtual std::string str() const;
 
        static Response parse(const std::string &);
-private:
-       Status status;
-
-       Response() { }
 };
 
 } // namespace Http
index 0eb25f39dafdc03f27e5c8e066b6679a9c11c819..9971ae263d630d5c16f7cd47d87cf441a8dd76f7 100644 (file)
@@ -16,13 +16,13 @@ ostream &operator<<(ostream &out, Status status)
 {
        switch(status)
        {
-       case None: out<<"None"; break;
+       case NONE: out<<"None"; break;
        case OK: out<<"OK"; break;
-       case BadRequest: out<<"Bad Request"; break;
-       case Forbidden: out<<"Forbidden"; break;
-       case NotFound: out<<"Not Found"; break;
-       case InternalError: out<<"Internal Error"; break;
-       case NotImplemented: out<<"Not Implemented"; break;
+       case BAD_REQUEST: out<<"Bad Request"; break;
+       case FORBIDDEN: out<<"Forbidden"; break;
+       case NOT_FOUND: out<<"Not Found"; break;
+       case INTERNAL_ERROR: out<<"Internal Error"; break;
+       case NOT_IMPLEMENTED: out<<"Not Implemented"; break;
        default: out<<"Unknown Status"; break;
        }
 
index 5d2b19686075bb2a51b5bb1d29cf20e7a3dff94f..0ea001d43e3487fe0324b475586c442592848e5c 100644 (file)
@@ -15,13 +15,13 @@ namespace Http {
 
 enum Status
 {
-       None=0,
+       NONE=0,
        OK=200,
-       BadRequest=400,
-       Forbidden=403,
-       NotFound=404,
-       InternalError=500,
-       NotImplemented=501
+       BAD_REQUEST=400,
+       FORBIDDEN=403,
+       NOT_FOUND=404,
+       INTERNAL_ERROR=500,
+       NOT_IMPLEMENTED=501
 };
 
 extern std::ostream &operator<<(std::ostream &, Status);
diff --git a/source/version.cpp b/source/version.cpp
new file mode 100644 (file)
index 0000000..2966fe8
--- /dev/null
@@ -0,0 +1,32 @@
+/* $Id$
+
+This file is part of libmsphttp
+Copyright © 2008  Mikkosoft Productions, Mikko Rasa
+Distributed under the LGPL
+*/
+
+#include <msp/strings/formatter.h>
+#include <msp/strings/lexicalcast.h>
+#include <msp/strings/regex.h>
+#include "version.h"
+
+using namespace std;
+
+namespace Msp {
+namespace Http {
+
+Version parse_version(const std::string &ver)
+{
+       if(RegMatch match=Regex("^HTTP/([0-9]+).([0-9]+)$").match(ver))
+               return lexical_cast<unsigned>(match[1].str)<<4 | lexical_cast<unsigned>(match[2].str);
+       else
+               throw InvalidParameterValue("Invalid HTTP version");
+}
+
+string version_str(Version ver)
+{
+       return format("HTTP/%u.%u", (ver>>4)&0xF, ver&0xF);
+}
+
+} // namespace Http
+} // namespace Msp
diff --git a/source/version.h b/source/version.h
new file mode 100644 (file)
index 0000000..974db10
--- /dev/null
@@ -0,0 +1,24 @@
+/* $Id$
+
+This file is part of libmsphttp
+Copyright © 2008  Mikkosoft Productions, Mikko Rasa
+Distributed under the LGPL
+*/
+
+#ifndef MSP_HTTP_MISC_H_
+#define MSP_HTTP_MISC_H_
+
+#include <string>
+
+namespace Msp {
+namespace Http {
+
+typedef unsigned Version;
+
+Version parse_version(const std::string &);
+std::string version_str(Version);
+
+} // namespace Http
+} // namespace Msp
+
+#endif