ReactOS  0.4.14-dev-999-g61c8d34
CWebService.cpp
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Automatic Testing Utility
3  * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4  * PURPOSE: Class implementing the interface to the "testman" Web Service
5  * COPYRIGHT: Copyright 2009-2015 Colin Finck (colin@reactos.org)
6  */
7 
8 #include "precomp.h"
9 
10 static const WCHAR szHostname[] = L"reactos.org";
11 static const WCHAR szServerFile[] = L"testman/webservice/";
12 
17 {
18  /* Zero-initialize variables */
19  m_hHTTP = NULL;
21  m_TestID = NULL;
22 
23  /* Establish an internet connection to the "testman" server */
25 
26  if(!m_hInet)
27  FATAL("InternetOpenW failed\n");
28 
30 
31  if(!m_hHTTP)
32  FATAL("InternetConnectW failed\n");
33 }
34 
39 {
40  if(m_hInet)
42 
43  if(m_hHTTP)
45 
46  if(m_hHTTPRequest)
48 
49  if(m_TestID)
50  delete m_TestID;
51 }
52 
63 PCHAR
64 CWebService::DoRequest(const string& InputData)
65 {
66  const WCHAR szHeaders[] = L"Content-Type: application/x-www-form-urlencoded";
67 
70 
71  /* Post our test results to the web service */
73 
74  if(!m_hHTTPRequest)
75  FATAL("HttpOpenRequestW failed\n");
76 
77  Data.reset(new char[InputData.size() + 1]);
78  strcpy(Data, InputData.c_str());
79 
80  if(!HttpSendRequestW(m_hHTTPRequest, szHeaders, lstrlenW(szHeaders), Data, (DWORD)InputData.size()))
81  FATAL("HttpSendRequestW failed\n");
82 
83  /* Get the response */
85  FATAL("InternetQueryDataAvailable failed\n");
86 
87  Data.reset(new char[DataLength + 1]);
88 
90  FATAL("InternetReadFile failed\n");
91 
92  Data[DataLength] = 0;
93 
94  return Data.release();
95 }
96 
103 void
104 CWebService::Finish(const char* TestType)
105 {
107  string Data;
109 
110  if (!m_TestID)
111  EXCEPTION("CWebService::Finish was called, but not a single result had been submitted!\n");
112 
113  Data = "action=finish";
115  Data += "&testtype=";
116  Data += TestType;
117  Data += "&testid=";
118  Data += m_TestID;
119 
120  Response.reset(DoRequest(Data));
121 
122  if (strcmp(Response, "OK"))
123  {
124  ss << "When finishing the test run, the server responded:" << endl << Response << endl;
125  SSEXCEPTION;
126  }
127 }
128 
135 void
136 CWebService::GetTestID(const char* TestType)
137 {
138  string Data;
139 
140  Data = "action=gettestid";
143  Data += "&testtype=";
144  Data += TestType;
145 
146  if(!Configuration.GetComment().empty())
147  {
148  Data += "&comment=";
150  }
151 
153 
154  /* Verify that this is really a number */
155  if(!IsNumber(m_TestID))
156  {
158 
159  ss << "Expected Test ID, but received:" << endl << m_TestID << endl;
160  SSEXCEPTION;
161  }
162 }
163 
177 PCHAR
178 CWebService::GetSuiteID(const char* TestType, CTestInfo* TestInfo)
179 {
180  auto_array_ptr<char> SuiteID;
181  string Data;
182 
183  Data = "action=getsuiteid";
185  Data += "&testtype=";
186  Data += TestType;
187  Data += "&module=";
188  Data += TestInfo->Module;
189  Data += "&test=";
190  Data += TestInfo->Test;
191 
192  SuiteID.reset(DoRequest(Data));
193 
194  /* Verify that this is really a number */
195  if(!IsNumber(SuiteID))
196  {
198 
199  ss << "Expected Suite ID, but received:" << endl << SuiteID << endl;
200  SSEXCEPTION;
201  }
202 
203  return SuiteID.release();
204 }
205 
215 void
216 CWebService::Submit(const char* TestType, CTestInfo* TestInfo)
217 {
219  auto_array_ptr<char> SuiteID;
220  string Data;
222 
223  if(!m_TestID)
224  GetTestID(TestType);
225 
226  SuiteID.reset(GetSuiteID(TestType, TestInfo));
227 
228  Data = "action=submit";
230  Data += "&testtype=";
231  Data += TestType;
232  Data += "&testid=";
233  Data += m_TestID;
234  Data += "&suiteid=";
235  Data += SuiteID;
236  Data += "&log=";
237  Data += EscapeString(TestInfo->Log);
238 
239  Response.reset(DoRequest(Data));
240 
241  if (strcmp(Response, "OK"))
242  {
243  ss << "When submitting the result, the server responded:" << endl << Response << endl;
244  SSEXCEPTION;
245  }
246 }
signed char * PCHAR
Definition: retypes.h:7
#define FATAL(Message)
Definition: precomp.h:57
#define INTERNET_FLAG_NO_CACHE_WRITE
Definition: wininet.h:66
void Finish(const char *TestType)
void Submit(const char *TestType, CTestInfo *TestInfo)
#define INTERNET_FLAG_SECURE
Definition: wininet.h:71
PCHAR DoRequest(const string &InputData)
Definition: CWebService.cpp:64
#define INTERNET_FLAG_RELOAD
Definition: wininet.h:61
HINTERNET m_hHTTPRequest
Definition: CWebService.h:13
BOOL WINAPI InternetCloseHandle(HINTERNET hInternet)
Definition: internet.c:1436
static const WCHAR szHostname[]
Definition: CWebService.cpp:10
HINTERNET m_hInet
Definition: CWebService.h:11
#define lstrlenW
Definition: compat.h:415
const string & GetAuthenticationRequestString() const
void GetTestID(const char *TestType)
string Log
Definition: CTestInfo.h:14
basic_ostream< _CharT, _Traits > &_STLP_CALL endl(basic_ostream< _CharT, _Traits > &__os)
Definition: _ostream.h:357
Type * release()
smooth NULL
Definition: ftsmooth.c:416
BOOL WINAPI InternetQueryDataAvailable(HINTERNET hFile, LPDWORD lpdwNumberOfBytesAvailable, DWORD dwFlags, DWORD_PTR dwContext)
Definition: internet.c:3842
_In_ LPGUID _In_ PVOID Data
Definition: classpnp.h:778
const string & GetSystemInfoRequestString() const
void reset(Type *Ptr=0)
BOOL WINAPI HttpSendRequestW(HINTERNET hHttpRequest, LPCWSTR lpszHeaders, DWORD dwHeaderLength, LPVOID lpOptional, DWORD dwOptionalLength)
Definition: http.c:5631
#define EXCEPTION(Message)
Definition: precomp.h:56
const string & GetComment() const
__wchar_t WCHAR
Definition: xmlstorage.h:180
string EscapeString(const char *Input)
Definition: tools.cpp:24
unsigned long DWORD
Definition: ntddk_ex.h:95
static const WCHAR szServerFile[]
Definition: CWebService.cpp:11
Definition: ncftp.h:89
string Module
Definition: CTestInfo.h:12
static const WCHAR L[]
Definition: oid.c:1250
CConfiguration Configuration
Definition: main.cpp:11
HINTERNET m_hHTTP
Definition: CWebService.h:12
#define INTERNET_DEFAULT_HTTPS_PORT
Definition: winhttp.h:37
#define SSEXCEPTION
Definition: precomp.h:58
PCHAR m_TestID
Definition: CWebService.h:14
#define INTERNET_OPEN_TYPE_PRECONFIG
Definition: wininet.h:521
BOOL WINAPI InternetReadFile(HINTERNET hFile, LPVOID lpBuffer, DWORD dwNumOfBytesToRead, LPDWORD pdwNumOfBytesRead)
Definition: internet.c:2174
#define INTERNET_FLAG_NO_COOKIES
Definition: wininet.h:75
bool IsNumber(const char *Input)
Definition: tools.cpp:74
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
struct Response Response
PCHAR GetSuiteID(const char *TestType, CTestInfo *TestInfo)
HINTERNET WINAPI HttpOpenRequestW(HINTERNET hHttpSession, LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion, LPCWSTR lpszReferrer, LPCWSTR *lpszAcceptTypes, DWORD dwFlags, DWORD_PTR dwContext)
Definition: http.c:3481
string Test
Definition: CTestInfo.h:13
HINTERNET WINAPI InternetConnectW(HINTERNET hInternet, LPCWSTR lpszServerName, INTERNET_PORT nServerPort, LPCWSTR lpszUserName, LPCWSTR lpszPassword, DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext)
Definition: internet.c:1280
_Must_inspect_result_ _Out_writes_to_ DataLength PHIDP_DATA _Inout_ PULONG DataLength
Definition: hidpi.h:333
#define ss
Definition: i386-dis.c:432
HINTERNET WINAPI InternetOpenW(LPCWSTR lpszAgent, DWORD dwAccessType, LPCWSTR lpszProxy, LPCWSTR lpszProxyBypass, DWORD dwFlags)
Definition: internet.c:1001
#define INTERNET_SERVICE_HTTP
Definition: wininet.h:562