Fix build.
[reactos.git] / rostests / rosautotest / webservice.c
index dcfd79f..852ee3d 100644 (file)
@@ -33,9 +33,10 @@ IntDoRequest(char** Data, PDWORD DataLength)
 {
     const WCHAR Headers[] = L"Content-Type: application/x-www-form-urlencoded";
 
-    HINTERNET hHTTP;
-    HINTERNET hHTTPRequest;
-    HINTERNET hInet;
+    BOOL ReturnValue = FALSE;
+    HINTERNET hHTTP = NULL;
+    HINTERNET hHTTPRequest = NULL;
+    HINTERNET hInet = NULL;
 
     /* Establish an internet connection to the "testman" server */
     hInet = InternetOpenW(L"rosautotest", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
@@ -43,7 +44,7 @@ IntDoRequest(char** Data, PDWORD DataLength)
     if(!hInet)
     {
         StringOut("InternetOpenW failed\n");
-        return FALSE;
+        goto Cleanup;
     }
 
     hHTTP = InternetConnectW(hInet, SERVER_HOSTNAME, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
@@ -51,7 +52,7 @@ IntDoRequest(char** Data, PDWORD DataLength)
     if(!hHTTP)
     {
         StringOut("InternetConnectW failed\n");
-        return FALSE;
+        goto Cleanup;
     }
 
     /* Post our test results to the web service */
@@ -60,22 +61,23 @@ IntDoRequest(char** Data, PDWORD DataLength)
     if(!hHTTPRequest)
     {
         StringOut("HttpOpenRequestW failed\n");
-        return FALSE;
+        goto Cleanup;
     }
 
     if(!HttpSendRequestW(hHTTPRequest, Headers, wcslen(Headers), *Data, *DataLength))
     {
         StringOut("HttpSendRequestW failed\n");
-        return FALSE;
+        goto Cleanup;
     }
 
     HeapFree(hProcessHeap, 0, *Data);
+    *Data = NULL;
 
     /* Get the response */
     if(!InternetQueryDataAvailable(hHTTPRequest, DataLength, 0, 0))
     {
         StringOut("InternetQueryDataAvailable failed\n");
-        return FALSE;
+        goto Cleanup;
     }
 
     *Data = HeapAlloc(hProcessHeap, 0, *DataLength + 1);
@@ -83,16 +85,23 @@ IntDoRequest(char** Data, PDWORD DataLength)
     if(!InternetReadFile(hHTTPRequest, *Data, *DataLength, DataLength))
     {
         StringOut("InternetReadFile failed\n");
-        return FALSE;
+        goto Cleanup;
     }
 
     (*Data)[*DataLength] = 0;
+    ReturnValue = TRUE;
 
-    InternetCloseHandle(hHTTPRequest);
-    InternetCloseHandle(hHTTP);
-    InternetCloseHandle(hInet);
+Cleanup:
+    if(hHTTPRequest)
+        InternetCloseHandle(hHTTPRequest);
 
-    return TRUE;
+    if(hHTTP)
+        InternetCloseHandle(hHTTP);
+
+    if(hInet)
+        InternetCloseHandle(hInet);
+
+    return ReturnValue;
 }
 
 /**
@@ -127,18 +136,25 @@ IsNumber(PCHAR Input)
  *
  * @return
  * Returns the Test ID as a CHAR array if successful or NULL otherwise.
+ * The caller needs to HeapFree the returned pointer in case of success.
  */
 PCHAR
 GetTestID(TESTTYPES TestType)
 {
     const CHAR GetTestIDAction[] = "gettestid";
+    const CHAR CommentProp[] = "&comment=";
 
     DWORD DataLength;
     PCHAR Data;
+    PCHAR ReturnValue = NULL;
 
     /* Build the full request string */
     DataLength  = sizeof(ActionProp) - 1 + sizeof(GetTestIDAction) - 1;
     DataLength += strlen(AuthenticationRequestString) + strlen(SystemInfoRequestString);
+
+    if(AppOptions.Comment)
+        DataLength += sizeof(CommentProp) - 1 + strlen(AppOptions.Comment);
+
     DataLength += sizeof(TestTypeProp) - 1;
 
     switch(TestType)
@@ -153,6 +169,13 @@ GetTestID(TESTTYPES TestType)
     strcat(Data, GetTestIDAction);
     strcat(Data, AuthenticationRequestString);
     strcat(Data, SystemInfoRequestString);
+
+    if(AppOptions.Comment)
+    {
+        strcat(Data, CommentProp);
+        strcat(Data, AppOptions.Comment);
+    }
+
     strcat(Data, TestTypeProp);
 
     switch(TestType)
@@ -163,18 +186,24 @@ GetTestID(TESTTYPES TestType)
     }
 
     if(!IntDoRequest(&Data, &DataLength))
-        return NULL;
+        goto Cleanup;
 
     /* Verify that this is really a number */
     if(!IsNumber(Data))
     {
         StringOut("Expected Test ID, but received:\n");
         StringOut(Data);
-        HeapFree(hProcessHeap, 0, Data);
-        return NULL;
+        StringOut("\n");
+        goto Cleanup;
     }
 
-    return Data;
+    ReturnValue = Data;
+
+Cleanup:
+    if(Data && ReturnValue != Data)
+        HeapFree(hProcessHeap, 0, Data);
+
+    return ReturnValue;
 }
 
 /**
@@ -189,6 +218,7 @@ GetTestID(TESTTYPES TestType)
  *
  * @return
  * Returns the Suite ID as a CHAR array if successful or NULL otherwise.
+ * The caller needs to HeapFree the returned pointer in case of success.
  */
 PCHAR
 GetSuiteID(TESTTYPES TestType, const PVOID TestData)
@@ -199,6 +229,7 @@ GetSuiteID(TESTTYPES TestType, const PVOID TestData)
 
     DWORD DataLength;
     PCHAR Data;
+    PCHAR ReturnValue = NULL;
     PWINE_GETSUITEID_DATA WineData;
 
     DataLength  = sizeof(ActionProp) - 1 + sizeof(GetSuiteIDAction) - 1;
@@ -241,18 +272,24 @@ GetSuiteID(TESTTYPES TestType, const PVOID TestData)
     }
 
     if(!IntDoRequest(&Data, &DataLength))
-        return NULL;
+        goto Cleanup;
 
     /* Verify that this is really a number */
     if(!IsNumber(Data))
     {
         StringOut("Expected Suite ID, but received:\n");
         StringOut(Data);
-        HeapFree(hProcessHeap, 0, Data);
-        return NULL;
+        StringOut("\n");
+        goto Cleanup;
     }
 
-    return Data;
+    ReturnValue = Data;
+
+Cleanup:
+    if(Data && ReturnValue != Data)
+        HeapFree(hProcessHeap, 0, Data);
+
+    return ReturnValue;
 }
 
 /**
@@ -275,6 +312,7 @@ Submit(TESTTYPES TestType, const PVOID TestData)
     const CHAR SuiteIDProp[] = "&suiteid=";
     const CHAR LogProp[] = "&log=";
 
+    BOOL ReturnValue = FALSE;
     DWORD DataLength;
     PCHAR Data;
     PCHAR pData;
@@ -340,7 +378,7 @@ Submit(TESTTYPES TestType, const PVOID TestData)
 
     /* Send all the stuff */
     if(!IntDoRequest(&Data, &DataLength))
-        return FALSE;
+        goto Cleanup;
 
     /* Output the response */
     StringOut("The server responded:\n");
@@ -348,9 +386,13 @@ Submit(TESTTYPES TestType, const PVOID TestData)
     StringOut("\n");
 
     if(!strcmp(Data, "OK"))
-        return TRUE;
+        ReturnValue = TRUE;
+
+Cleanup:
+    if(Data)
+        HeapFree(hProcessHeap, 0, Data);
 
-    return FALSE;
+    return ReturnValue;
 }
 
 /**
@@ -371,6 +413,7 @@ Finish(TESTTYPES TestType, const PVOID TestData)
 {
     const CHAR FinishAction[] = "finish";
 
+    BOOL ReturnValue = FALSE;
     DWORD DataLength;
     PCHAR Data;
     PGENERAL_FINISH_DATA GeneralData;
@@ -408,10 +451,14 @@ Finish(TESTTYPES TestType, const PVOID TestData)
     }
 
     if(!IntDoRequest(&Data, &DataLength))
-        return FALSE;
+        goto Cleanup;
 
     if(!strcmp(Data, "OK"))
-        return TRUE;
+        ReturnValue = TRUE;
+
+Cleanup:
+    if(Data)
+        HeapFree(hProcessHeap, 0, Data);
 
-    return FALSE;
+    return ReturnValue;
 }