ReactOS  0.4.15-dev-425-gc40b086
CWebService.cpp
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Automatic Testing Utility
3  * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4  * PURPOSE: Class implementing the interface to the "testman" Web Service
5  * COPYRIGHT: Copyright 2009-2020 Colin Finck (colin@reactos.org)
6  */
7 
8 #include "precomp.h"
9 
10 static const WCHAR szHostname[] = L"reactos.org";
11 static const INTERNET_PORT ServerPort = 8443;
12 static const WCHAR szServerFile[] = L"testman/webservice/";
13 
18 {
19  /* Zero-initialize variables */
20  m_hHTTP = NULL;
22  m_TestID = NULL;
23 
24  /* Establish an internet connection to the "testman" server */
26 
27  if(!m_hInet)
28  FATAL("InternetOpenW failed\n");
29 
31 
32  if(!m_hHTTP)
33  FATAL("InternetConnectW failed\n");
34 }
35 
40 {
41  if(m_hInet)
43 
44  if(m_hHTTP)
46 
47  if(m_hHTTPRequest)
49 
50  if(m_TestID)
51  delete m_TestID;
52 }
53 
64 PCHAR
65 CWebService::DoRequest(const string& InputData)
66 {
67  const WCHAR szHeaders[] = L"Content-Type: application/x-www-form-urlencoded";
68 
71 
72  /* Post our test results to the web service */
74 
75  if(!m_hHTTPRequest)
76  FATAL("HttpOpenRequestW failed\n");
77 
78  Data.reset(new char[InputData.size() + 1]);
79  strcpy(Data, InputData.c_str());
80 
81  if(!HttpSendRequestW(m_hHTTPRequest, szHeaders, lstrlenW(szHeaders), Data, (DWORD)InputData.size()))
82  FATAL("HttpSendRequestW failed\n");
83 
84  /* Get the response */
86  FATAL("InternetQueryDataAvailable failed\n");
87 
88  Data.reset(new char[DataLength + 1]);
89 
91  FATAL("InternetReadFile failed\n");
92 
93  Data[DataLength] = 0;
94 
95  return Data.release();
96 }
97 
104 void
105 CWebService::Finish(const char* TestType)
106 {
108  string Data;
110 
111  if (!m_TestID)
112  EXCEPTION("CWebService::Finish was called, but not a single result had been submitted!\n");
113 
114  Data = "action=finish";
116  Data += "&testtype=";
117  Data += TestType;
118  Data += "&testid=";
119  Data += m_TestID;
120 
121  Response.reset(DoRequest(Data));
122 
123  if (strcmp(Response, "OK"))
124  {
125  ss << "When finishing the test run, the server responded:" << endl << Response << endl;
126  SSEXCEPTION;
127  }
128 }
129 
136 void
137 CWebService::GetTestID(const char* TestType)
138 {
139  string Data;
140 
141  Data = "action=gettestid";
144  Data += "&testtype=";
145  Data += TestType;
146 
147  if(!Configuration.GetComment().empty())
148  {
149  Data += "&comment=";
151  }
152 
154 
155  /* Verify that this is really a number */
156  if(!IsNumber(m_TestID))
157  {
159 
160  ss << "Expected Test ID, but received:" << endl << m_TestID << endl;
161  SSEXCEPTION;
162  }
163 }
164 
178 PCHAR
179 CWebService::GetSuiteID(const char* TestType, CTestInfo* TestInfo)
180 {
181  auto_array_ptr<char> SuiteID;
182  string Data;
183 
184  Data = "action=getsuiteid";
186  Data += "&testtype=";
187  Data += TestType;
188  Data += "&module=";
189  Data += TestInfo->Module;
190  Data += "&test=";
191  Data += TestInfo->Test;
192 
193  SuiteID.reset(DoRequest(Data));
194 
195  /* Verify that this is really a number */
196  if(!IsNumber(SuiteID))
197  {
199 
200  ss << "Expected Suite ID, but received:" << endl << SuiteID << endl;
201  SSEXCEPTION;
202  }
203 
204  return SuiteID.release();
205 }
206 
216 void
217 CWebService::Submit(const char* TestType, CTestInfo* TestInfo)
218 {
220  auto_array_ptr<char> SuiteID;
221  string Data;
223 
224  if(!m_TestID)
225  GetTestID(TestType);
226 
227  SuiteID.reset(GetSuiteID(TestType, TestInfo));
228 
229  Data = "action=submit";
231  Data += "&testtype=";
232  Data += TestType;
233  Data += "&testid=";
234  Data += m_TestID;
235  Data += "&suiteid=";
236  Data += SuiteID;
237  Data += "&log=";
238  Data += EscapeString(TestInfo->Log);
239 
240  Response.reset(DoRequest(Data));
241 
242  if (strcmp(Response, "OK"))
243  {
244  ss << "When submitting the result, the server responded:" << endl << Response << endl;
245  SSEXCEPTION;
246  }
247 }
signed char * PCHAR
Definition: retypes.h:7
#define FATAL(Message)
Definition: precomp.h:57
#define INTERNET_FLAG_NO_CACHE_WRITE
Definition: wininet.h:66
static const INTERNET_PORT ServerPort
Definition: CWebService.cpp:11
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:65
#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:416
const string & GetAuthenticationRequestString() const
void GetTestID(const char *TestType)
WORD INTERNET_PORT
Definition: winhttp.h:38
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:12
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 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