From 073301327d758bd2d88fca2ef4f64525d90b2f8f Mon Sep 17 00:00:00 2001 From: cxl Date: Mon, 1 Nov 2010 18:59:06 +0000 Subject: [PATCH] .XmlRpc: Improved HTTP compliance git-svn-id: svn://ultimatepp.org/upp/trunk@2829 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- uppsrc/Core/Xmlize.cpp | 2 ++ uppsrc/Web/httpcli.cpp | 4 +-- uppsrc/Web/util.cpp | 13 ++++++++ uppsrc/Web/util.h | 1 + uppsrc/XmlRpc/Server.cpp | 67 ++++++++++++++++++++++++---------------- 5 files changed, 58 insertions(+), 29 deletions(-) diff --git a/uppsrc/Core/Xmlize.cpp b/uppsrc/Core/Xmlize.cpp index 21208c06c..7e7fceb4e 100644 --- a/uppsrc/Core/Xmlize.cpp +++ b/uppsrc/Core/Xmlize.cpp @@ -255,6 +255,8 @@ REGISTER_VALUE_XMLIZE(Rect); REGISTER_VALUE_XMLIZE(Rect64); REGISTER_VALUE_XMLIZE(Rectf); REGISTER_VALUE_XMLIZE(Color); +REGISTER_VALUE_XMLIZE(ValueArray); +REGISTER_VALUE_XMLIZE(ValueMap); static String s_binary("serialized_binary"); diff --git a/uppsrc/Web/httpcli.cpp b/uppsrc/Web/httpcli.cpp index f2967c6af..8df726152 100644 --- a/uppsrc/Web/httpcli.cpp +++ b/uppsrc/Web/httpcli.cpp @@ -4,8 +4,8 @@ NAMESPACE_UPP bool HttpClient_Trace__; -#define LLOG(x) if(HttpClient_Trace__) RLOG(x); else; -#define LLOGBLOCK(x) // DLOGBLOCK(x) +#define LLOG(x) // DLOG(x) //if(HttpClient_Trace__) RLOG(x); else; +#define LLOGBLOCK(x) // DLOG(x) void HttpClient::Trace(bool b) { diff --git a/uppsrc/Web/util.cpp b/uppsrc/Web/util.cpp index e9d7a4032..22f08a52e 100644 --- a/uppsrc/Web/util.cpp +++ b/uppsrc/Web/util.cpp @@ -2,6 +2,19 @@ NAMESPACE_UPP +String WwwFormat(Time tm) +{ + static const char *dayofweek[] = + { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; + static const char *month[] = + { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; + return String().Cat() + << dayofweek[DayOfWeek(tm)] << ", " + << (int)tm.day << ' ' << month[tm.month - 1] + << ' ' << (int)tm.year + << ' ' << Sprintf("%2d:%02d:%02d +0100", tm.hour, tm.minute, tm.second); +} + bool IsSameTextFile(const char *p, const char *q) { for(;;) diff --git a/uppsrc/Web/util.h b/uppsrc/Web/util.h index 4492d0006..d6cbc9216 100644 --- a/uppsrc/Web/util.h +++ b/uppsrc/Web/util.h @@ -1,6 +1,7 @@ #ifndef __tweb_util__ #define __tweb_util__ +String WwwFormat(Time tm); bool IsSameTextFile(const char *p, const char *q); String StringSample(const char *s, int limit); String FormatIP(dword _ip); diff --git a/uppsrc/XmlRpc/Server.cpp b/uppsrc/XmlRpc/Server.cpp index 59b7c2fe3..9047a8a56 100644 --- a/uppsrc/XmlRpc/Server.cpp +++ b/uppsrc/XmlRpc/Server.cpp @@ -1,7 +1,7 @@ #include "XmlRpc.h" #include "XmlRpc.h" -#define LLOG(x) // LOG(x) +#define LLOG(x) // DLOG(x) typedef void (*XmlRpcFnPtr)(XmlRpcData&); @@ -117,33 +117,46 @@ String ReadLine(Socket& s) bool XmlRpcPerform(Socket& http, const char *group) { LLOG("=== Accepted connection ==================================================="); - String request = ReadLine(http); - if(http.IsError()) return false; - VectorMap hdr; - for(;;) { - String s = ReadLine(http); - if(s.IsEmpty()) break; - LLOG(s); - int q = s.Find(':'); - if(q >= 0) - hdr.GetAdd(s.Mid(0, q)) = TrimLeft(s.Mid(q + 1)); + String request = ToUpper(ReadLine(http)); + LLOG(request); + if(http.IsError() || request.GetCount() == 0) + return false; + if(request.Find("POST") >= 0 || request.Find("HTTP") >= 0) { + VectorMap hdr; + for(;;) { + String s = ReadLine(http); + if(s.IsEmpty()) break; + LLOG(s); + int q = s.Find(':'); + if(q >= 0) + hdr.GetAdd(s.Mid(0, q)) = TrimLeft(s.Mid(q + 1)); + } + if(!http.IsError()) { + int len = atoi(hdr.Get("Content-Length", "")); + String r; + if(len > 0 && len < 1024 * 1024 * 1024) { + r = XmlRpcExecute(http.ReadCount(len, 90000), + group, http.GetPeerAddr()); + LLOG("--------- Server response:\n" << r << "============="); + String response; + String ts = WwwFormat(GetUtcTime()); + response << + "HTTP/1.0 200 OK\r\n" + "Date: " << ts << "\r\n" + "Server: U++\r\n" + "Content-Length: " << r.GetCount() << "\r\n" + "Connection: close\r\n" + "Content-Type: text/xml\r\n\r\n" + << r; + LLOG(response); + http.Write(response); + return true; + } + } } - if(http.IsError()) return false; - String r = XmlRpcExecute(http.ReadCount(atoi(hdr.Get("Content-Length", "")), 90000), - group, http.GetPeerAddr()); - LLOG("--------- Server response:\n" << r << "============="); - String response; - response << - "HTTP/1.1 200 OK\r\n" - "Date: Mon, 23 May 2005 22:38:34 GMT\r\n" - "Server: U++\r\n" - "Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT\r\n" - "Accept-Ranges: bytes\r\n" - "Content-Length: " << r.GetCount() << "\r\n" - "Connection: close\r\n" - "Content-Type: application/soap+xml; charset=utf-8\r\n\r\n" << r; - http.Write(response); - return true; + http.Write("HTTP/1.0 400 Bad request\r\n" + "Server: U++\r\n\r\n"); + return false; } bool XmlRpcServer(int port, const char *group)