From a9edb12c2cc13a74f16c0a61deaeaa8780f13f4e Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Sun, 22 Mar 2015 20:23:42 +0000 Subject: SkypeWeb: refactored HttpRequest git-svn-id: http://svn.miranda-ng.org/main/trunk@12477 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/SkypeWeb/src/http_request.h | 210 ++++++++++++++++++++++++----- protocols/SkypeWeb/src/requests/contacts.h | 38 +++--- protocols/SkypeWeb/src/requests/login.h | 37 ++--- protocols/SkypeWeb/src/requests/profile.h | 9 +- protocols/SkypeWeb/src/skype_request.cpp | 2 +- 5 files changed, 227 insertions(+), 69 deletions(-) (limited to 'protocols/SkypeWeb') diff --git a/protocols/SkypeWeb/src/http_request.h b/protocols/SkypeWeb/src/http_request.h index 40edf6895d..28b34c075d 100644 --- a/protocols/SkypeWeb/src/http_request.h +++ b/protocols/SkypeWeb/src/http_request.h @@ -1,48 +1,198 @@ #ifndef _HTTP_REQUEST_H_ #define _HTTP_REQUEST_H_ +struct VALUE +{ + LPCSTR szName; + __forceinline VALUE(LPCSTR _name) : szName(_name) { } +}; + +struct INT_VALUE : public VALUE +{ + int iValue; + __forceinline INT_VALUE(LPCSTR _name, int _value) + : VALUE(_name), iValue(_value) { } +}; + +struct CHAR_VALUE : public VALUE +{ + LPCSTR szValue; + __forceinline CHAR_VALUE(LPCSTR _name, LPCSTR _value) + : VALUE(_name), szValue(_value) { } +}; + +struct FORMAT_VALUE : public VALUE +{ + CMStringA szValue; + __forceinline FORMAT_VALUE(LPCSTR _name, LPCSTR _valueFormat, ...) + : VALUE(_name) + { + va_list args; + va_start(args, _valueFormat); + szValue.AppendFormatV(_valueFormat, args); + va_end(args); + } +}; + class HttpRequest : protected NETLIBHTTPREQUEST, public MZeroedObject { protected: - CMStringA url; + class HttpRequestUrl + { + friend HttpRequest; + + private: + CMStringA content; + + void AppendSeparator() + { + if (!content.IsEmpty()) + { + if (content.Find("?") == -1) + content.AppendChar('?'); + else + content.AppendChar('&'); + } + } - HttpRequest() + public: + HttpRequestUrl & operator<<(const VALUE ¶m) + { + AppendSeparator(); + content.Append(param.szName); + return *this; + } + + HttpRequestUrl & operator<<(const INT_VALUE ¶m) + { + AppendSeparator(); + content.AppendFormat("%s=%i", param.szName, param.iValue); + return *this; + } + + HttpRequestUrl & operator<<(const CHAR_VALUE ¶m) + { + AppendSeparator(); + content.AppendFormat("%s=%s", param.szName, param.szValue); + return *this; + } + + char * ToString() + { + return content.GetBuffer(); + } + }; + + class HttpRequestHeaders { - cbSize = sizeof(NETLIBHTTPREQUEST); - } + private: + HttpRequest &request; - HttpRequest(int httpMethod, LPCSTR urlFormat, va_list args) + void Add(LPCSTR szName) + { + Add(szName, ""); + } + + void Add(LPCSTR szName, LPCSTR szValue) + { + request.headers = (NETLIBHTTPHEADER*)mir_realloc( + request.headers, sizeof(NETLIBHTTPHEADER) * (request.headersCount + 1)); + request.headers[request.headersCount].szName = mir_strdup(szName); + request.headers[request.headersCount].szValue = mir_strdup(szValue); + request.headersCount++; + } + + public: + HttpRequestHeaders(HttpRequest &request) : request(request) { } + + HttpRequestHeaders & operator<<(const VALUE ¶m) + { + Add(param.szName); + return *this; + } + + HttpRequestHeaders & operator<<(const CHAR_VALUE ¶m) + { + Add(param.szName, param.szValue); + return *this; + } + }; + + class HttpRequestBody { - this->HttpRequest::HttpRequest(); + private: + CMStringA content; - requestType = httpMethod; - flags = NLHRF_HTTP11 | NLHRF_NODUMPSEND | NLHRF_DUMPASTEXT; + void AppendSeparator() + { + if (!content.IsEmpty()) + { + content.AppendChar('&'); + } + } - url.AppendFormatV(urlFormat, args); - szUrl = url.GetBuffer(); - } + public: + HttpRequestBody() { } + + HttpRequestBody & operator<<(const VALUE ¶m) + { + AppendSeparator(); + content.Append(param.szName); + return *this; + } + + HttpRequestBody & operator<<(const INT_VALUE ¶m) + { + AppendSeparator(); + content.AppendFormat("%s=%i", param.szName, param.iValue); + return *this; + } - void AddHeader(LPCSTR szName, LPCSTR szValue) + HttpRequestBody & operator<<(const CHAR_VALUE ¶m) + { + AppendSeparator(); + content.AppendFormat("%s=%s", param.szName, param.szValue); + return *this; + } + + HttpRequestBody & operator<<(const FORMAT_VALUE ¶m) + { + AppendSeparator(); + content.AppendFormat("%s=%s", param.szName, param.szValue); + return *this; + } + + char * ToString() + { + return content.GetBuffer(); + } + }; + + HttpRequest() : Headers(*this) { - headers = (NETLIBHTTPHEADER*)mir_realloc(headers, sizeof(NETLIBHTTPHEADER)*(headersCount + 1)); - headers[headersCount].szName = mir_strdup(szName); - headers[headersCount].szValue = mir_strdup(szValue); - headersCount++; + cbSize = sizeof(NETLIBHTTPREQUEST); } - void SetData(const char *data, size_t size) + HttpRequest(int httpMethod, LPCSTR urlFormat, va_list args) + : Headers(*this) { - if (pData != NULL) - mir_free(pData); + this->HttpRequest::HttpRequest(); + + requestType = httpMethod; + flags = NLHRF_HTTP11 | NLHRF_NODUMPSEND | NLHRF_DUMPASTEXT; - dataLength = (int)size; - pData = (char*)mir_alloc(size + 1); - memcpy(pData, data, size); - pData[size] = 0; + Url.content.AppendFormatV(urlFormat, args); + if (Url.content.Find("://") == -1) + Url.content.Insert(0, flags & NLHRF_SSL ? "https://" : "http://"); } public: + HttpRequestUrl Url; + HttpRequestHeaders Headers; + HttpRequestBody Body; + HttpRequest(int type, LPCSTR urlFormat, ...) + : Headers(*this) { va_list args; va_start(args, urlFormat); @@ -58,19 +208,15 @@ public: mir_free(headers[i].szValue); } mir_free(headers); - mir_free(pData); - } - - void SetCookie(LPCSTR szValue) - { - AddHeader("Set-Cookie", szValue); + //mir_free(pData); } NETLIBHTTPREQUEST * Send(HANDLE hConnection) { - if (url.Find("http", 0) != 0) - url.Insert(0, flags & NLHRF_SSL ? "https://" : "http://"); - szUrl = url.GetBuffer(); + szUrl = Url.ToString(); + + pData = Body.ToString(); + dataLength = mir_strlen(pData); char message[1024]; mir_snprintf(message, SIZEOF(message), "Send request to %s", szUrl); diff --git a/protocols/SkypeWeb/src/requests/contacts.h b/protocols/SkypeWeb/src/requests/contacts.h index 5e4ce2cffe..aff095df65 100644 --- a/protocols/SkypeWeb/src/requests/contacts.h +++ b/protocols/SkypeWeb/src/requests/contacts.h @@ -9,8 +9,12 @@ public: { flags |= NLHRF_SSL; - AddHeader("X-Skypetoken", token); - AddHeader("Accept", "application/json"); + Url << CHAR_VALUE("hideDetails", "true"); + + Headers + << CHAR_VALUE("X-Skypetoken", "Accept") + << CHAR_VALUE("X-Skypetoken", token) + << CHAR_VALUE("Accept", "application/json"); } }; @@ -22,18 +26,15 @@ public: { flags |= NLHRF_SSL; - CMStringA data; + Headers + << CHAR_VALUE("X-Skypetoken", "Accept") + << CHAR_VALUE("X-Skypetoken", token) + << CHAR_VALUE("Accept", "application/json"); + for (size_t i = 0; i < skypenames.getCount(); i++) { - data.AppendFormat("contacts[]=%s&", skypenames[i]); + Body << CHAR_VALUE("contacts[]", skypenames[i]); } - data.Delete(data.GetLength() - 1); - - SetData(data, data.GetLength()); - - AddHeader("X-Skypetoken", token); - AddHeader("Accept", "application/json"); - AddHeader("Content-Type", "application/x-www-form-urlencoded"); } }; @@ -45,8 +46,9 @@ public: { flags |= NLHRF_SSL; - AddHeader("X-Skypetoken", token); - AddHeader("Accept", "application/json"); + Headers + << CHAR_VALUE("X-Skypetoken", token) + << CHAR_VALUE("Accept", "application/json"); } }; @@ -58,8 +60,9 @@ public: { flags |= NLHRF_SSL; - AddHeader("X-Skypetoken", token); - AddHeader("Accept", "application/json"); + Headers + << CHAR_VALUE("X-Skypetoken", token) + << CHAR_VALUE("Accept", "application/json"); } }; @@ -71,8 +74,9 @@ public: { flags |= NLHRF_SSL; - AddHeader("X-Skypetoken", token); - AddHeader("Accept", "application/json"); + Headers + << CHAR_VALUE("X-Skypetoken", token) + << CHAR_VALUE("Accept", "application/json"); } }; diff --git a/protocols/SkypeWeb/src/requests/login.h b/protocols/SkypeWeb/src/requests/login.h index 73003b5046..12f0e9e5cc 100644 --- a/protocols/SkypeWeb/src/requests/login.h +++ b/protocols/SkypeWeb/src/requests/login.h @@ -9,35 +9,38 @@ public: { flags |= NLHRF_SSL; - url.Append("?client_id=578134&redirect_uri=https%3A%2F%2Fweb.skype.com"); + Url + << INT_VALUE("client_id", 578134) + << CHAR_VALUE("redirect_uri", "https%3A%2F%2Fweb.skype.com"); + + Headers + << CHAR_VALUE("Host", "login.skype.com") + << CHAR_VALUE("Referer", "https://web.skype.com/"); } LoginRequest(const char *skypename, const char *password, const char *pie, const char *etm) : HttpRequest(REQUEST_POST, "login.skype.com/login") { - flags |= NLHRF_SSL; + this->LoginRequest::LoginRequest(); - url.Append("?client_id=578134&redirect_uri=https%3A%2F%2Fweb.skype.com"); + Headers + << CHAR_VALUE("Content-Type", "application/x-www-form-urlencoded") + << CHAR_VALUE("Referer", "https://login.skype.com/login?method=skype&client_id=578134&redirect_uri=https%3A%2F%2Fweb.skype.com"); LPTIME_ZONE_INFORMATION tzi = tmi.getTziByContact(NULL); - char sign = tzi->Bias > 0 ? '-' : '+'; int hours = tzi->Bias / -60; int minutes = tzi->Bias % -60; - CMStringA data = ""; - data.AppendFormat("username=%s&", skypename); - data.AppendFormat("password=%s&", password); - data.AppendFormat("pie=%s&", ptrA(mir_urlEncode(pie))); - data.AppendFormat("etm=%s&", ptrA(mir_urlEncode(etm))); - data.AppendFormat("timezone_field=%c%02d|%02d&", sign, hours, minutes); - data.AppendFormat("js_time=%d.00&", time(NULL)); - data.Append("client_id=578134&"); - data.Append("redirect_uri=https%3A%2F%2Fweb.skype.com"); - - SetData(data, data.GetLength()); - - AddHeader("Content-Type", "application/x-www-form-urlencoded"); + Body + << CHAR_VALUE("username", skypename) + << CHAR_VALUE("password", password) + << CHAR_VALUE("pie", ptrA(mir_urlEncode(pie))) + << CHAR_VALUE("etm", ptrA(mir_urlEncode(etm))) + << FORMAT_VALUE("timezone_field", "%c%02d|%02d", sign, hours, minutes) + << FORMAT_VALUE("js_time", "%d.00", time(NULL)) + << INT_VALUE("client_id", 578134) + << CHAR_VALUE("redirect_uri", "https%3A%2F%2Fweb.skype.com"); } }; diff --git a/protocols/SkypeWeb/src/requests/profile.h b/protocols/SkypeWeb/src/requests/profile.h index 8473d19875..2c0b4e8fb6 100644 --- a/protocols/SkypeWeb/src/requests/profile.h +++ b/protocols/SkypeWeb/src/requests/profile.h @@ -9,8 +9,13 @@ public: { flags |= NLHRF_SSL; - AddHeader("X-Skypetoken", token); - AddHeader("Accept", "application/json"); + Headers + << CHAR_VALUE("X-Skypetoken", token) + << CHAR_VALUE("Accept", "application/json"); + + + //AddHeader("Origin", "https://web.skype.com"); + //AddHeader("Referer", "https://web.skype.com/main"); } }; diff --git a/protocols/SkypeWeb/src/skype_request.cpp b/protocols/SkypeWeb/src/skype_request.cpp index 1cd5d654f0..61d9754ec0 100644 --- a/protocols/SkypeWeb/src/skype_request.cpp +++ b/protocols/SkypeWeb/src/skype_request.cpp @@ -28,7 +28,7 @@ void CSkypeProto::PushRequest(HttpRequest *request, SkypeResponseCallback respon CMStringA allCookies; for (std::map::iterator cookie = cookies.begin(); cookie != cookies.end(); ++cookie) allCookies.AppendFormat("%s=%s; ", cookie->first.c_str(), cookie->second.c_str()); - request->SetCookie(allCookies); + request->Headers << CHAR_VALUE("Set-Cookie", allCookies); } if (response == NULL) -- cgit v1.2.3