From a4049d7c4126126ca3abd12b1aca8715e7006d44 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 24 Dec 2008 07:26:59 +0000 Subject: [PATCH] Rename data to content in Message 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 --- source/client.cpp | 10 +++++++--- source/client.h | 11 +++++------ source/message.cpp | 24 ++++++++++++++---------- source/message.h | 30 +++++++++++++++++++----------- source/request.h | 7 ++++--- source/response.cpp | 2 +- source/response.h | 8 ++++---- source/status.cpp | 12 ++++++------ source/status.h | 12 ++++++------ source/{misc.cpp => version.cpp} | 2 +- source/{misc.h => version.h} | 2 -- 11 files changed, 67 insertions(+), 53 deletions(-) rename source/{misc.cpp => version.cpp} (97%) rename source/{misc.h => version.h} (84%) diff --git a/source/client.cpp b/source/client.cpp index fde7c8d..a4bc342 100644 --- a/source/client.cpp +++ b/source/client.cpp @@ -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); } diff --git a/source/client.h b/source/client.h index d72cbad..2554ac4 100644 --- a/source/client.h +++ b/source/client.h @@ -21,6 +21,10 @@ class Response; class Client { +public: + sigc::signal signal_response_complete; + sigc::signal 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 signal_response_complete; - sigc::signal 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(); diff --git a/source/message.cpp b/source/message.cpp index a8851fa..e0a5e7e 100644 --- a/source/message.cpp +++ b/source/message.cpp @@ -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(i->second)-data.size(); + unsigned needed=lexical_cast(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(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; } diff --git a/source/message.h b/source/message.h index c9dc618..1ccc976 100644 --- a/source/message.h +++ b/source/message.h @@ -8,32 +8,40 @@ Distributed under the LGPL #ifndef MSP_HTTP_MESSAGE_H_ #define MSP_HTTP_MESSAGE_H_ +#include #include -#include "misc.h" +#include +#include "version.h" namespace Msp { namespace Http { class Message { +protected: + typedef std::map 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/request.h b/source/request.h index 6f713ab..26d668f 100644 --- a/source/request.h +++ b/source/request.h @@ -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 diff --git a/source/response.cpp b/source/response.cpp index 86d06ba..0defebd 100644 --- a/source/response.cpp +++ b/source/response.cpp @@ -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; } diff --git a/source/response.h b/source/response.h index 20e144e..abd370e 100644 --- a/source/response.h +++ b/source/response.h @@ -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 diff --git a/source/status.cpp b/source/status.cpp index 0eb25f3..9971ae2 100644 --- a/source/status.cpp +++ b/source/status.cpp @@ -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; } diff --git a/source/status.h b/source/status.h index 5d2b196..0ea001d 100644 --- a/source/status.h +++ b/source/status.h @@ -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/misc.cpp b/source/version.cpp similarity index 97% rename from source/misc.cpp rename to source/version.cpp index 6ac7c5d..2966fe8 100644 --- a/source/misc.cpp +++ b/source/version.cpp @@ -8,7 +8,7 @@ Distributed under the LGPL #include #include #include -#include "misc.h" +#include "version.h" using namespace std; diff --git a/source/misc.h b/source/version.h similarity index 84% rename from source/misc.h rename to source/version.h index 8e7ed79..974db10 100644 --- a/source/misc.h +++ b/source/version.h @@ -8,13 +8,11 @@ Distributed under the LGPL #ifndef MSP_HTTP_MISC_H_ #define MSP_HTTP_MISC_H_ -#include #include namespace Msp { namespace Http { -typedef std::map HeaderMap; typedef unsigned Version; Version parse_version(const std::string &); -- 2.43.0