Reactos
at master 93 lines 2.9 kB view raw
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 WinInet version of the interface to the "testman" Web Service 5 * COPYRIGHT: Copyright 2009-2015 Colin Finck <colin@reactos.org> 6 * Copyright 2025 Mark Jansen <mark.jansen@reactos.org> 7 */ 8 9#include "precomp.h" 10 11/** 12 * Constructs a CWebService object and immediately establishes a connection to the "testman" Web Service. 13 */ 14CWebServiceWinInet::CWebServiceWinInet() 15{ 16 /* Zero-initialize variables */ 17 m_hHTTP = NULL; 18 m_hHTTPRequest = NULL; 19 20 /* Establish an internet connection to the "testman" server */ 21 m_hInet = InternetOpenW(L"rosautotest", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); 22 23 if(!m_hInet) 24 FATAL("InternetOpenW failed\n"); 25} 26 27/** 28 * Destructs a CWebService object and closes all connections to the Web Service. 29 */ 30CWebServiceWinInet::~CWebServiceWinInet() 31{ 32 if(m_hInet) 33 InternetCloseHandle(m_hInet); 34 35 if(m_hHTTP) 36 InternetCloseHandle(m_hHTTP); 37 38 if(m_hHTTPRequest) 39 InternetCloseHandle(m_hHTTPRequest); 40} 41 42/** 43 * Sends data to the Web Service. 44 * 45 * @param InputData 46 * A std::string containing all the data, which is going to be submitted as HTTP POST data. 47 * 48 * @return 49 * Returns a pointer to a char array containing the data received from the Web Service. 50 * The caller needs to free that pointer. 51 */ 52PCHAR 53CWebServiceWinInet::DoRequest(const char *Hostname, INTERNET_PORT Port, const char *ServerFile, const string &InputData) 54{ 55 const WCHAR szHeaders[] = L"Content-Type: application/x-www-form-urlencoded"; 56 57 auto_array_ptr<char> Data; 58 DWORD DataLength; 59 60 if (!m_hHTTP) 61 { 62 m_hHTTP = InternetConnectA(m_hInet, Hostname, Port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0); 63 64 if (!m_hHTTP) 65 FATAL("InternetConnectW failed\n"); 66 67 } 68 69 /* Post our test results to the web service */ 70 m_hHTTPRequest = HttpOpenRequestA(m_hHTTP, "POST", ServerFile, NULL, NULL, NULL, INTERNET_FLAG_SECURE | INTERNET_FLAG_NO_COOKIES | INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE, 0); 71 72 if(!m_hHTTPRequest) 73 FATAL("HttpOpenRequestW failed\n"); 74 75 Data.reset(new char[InputData.size() + 1]); 76 strcpy(Data, InputData.c_str()); 77 78 if(!HttpSendRequestW(m_hHTTPRequest, szHeaders, lstrlenW(szHeaders), Data, (DWORD)InputData.size())) 79 FATAL("HttpSendRequestW failed\n"); 80 81 /* Get the response */ 82 if(!InternetQueryDataAvailable(m_hHTTPRequest, &DataLength, 0, 0)) 83 FATAL("InternetQueryDataAvailable failed\n"); 84 85 Data.reset(new char[DataLength + 1]); 86 87 if(!InternetReadFile(m_hHTTPRequest, Data, DataLength, &DataLength)) 88 FATAL("InternetReadFile failed\n"); 89 90 Data[DataLength] = 0; 91 92 return Data.release(); 93}