Check https website with authentication and check for a given content string
In custom.conf:
[content_check] type=boolean command=cscript "c:\content_check.js"
In content_check.js:
/*
* This is basic auth http content check that:
*
* 1. Establishes a HTTPS connection to a given URL
* 2. Provides basic HTTP authentication with given username and password
* 3. Loads the page and does a search for the given content string
* 4. Reports success(0) or failure(1).
*
* Usage:
*
* cscript content_check.js
*
*/
// User defined variables
var strURL = "https://";
var strName = "user";
var strPassword = "pass";
// regular expression to rearch (JScript search() function is used)
var strRegex = "Hello, World";
// Perform check
var res = checkContent(strURL, strName, strPassword, strRegex);
// Exit with the result code
WScript.Quit(res);
// Content check routine body
function checkContent(strURL, strName, strPassword, strRegex)
{
try
{
// 1. First check if we get a authorization request
// Create the WinHTTPRequest ActiveX Object.
var WinHttpReq = new ActiveXObject("WinHttp.WinHttpRequest.5.1");
// Set timeouts in milliseconds
WinHttpReq.SetTimeouts(
60000, // Resolve timeout
60000, // Connect timeout
30000, // Send timeout
30000 // Receive timeout
);
// Set option to ignore all certificate errors
// See descriptions at http://msdn.microsoft.com/en-us/library/Aa384108
WinHttpReq.Option(4) = 0x3300;
// Create an HTTP request.
WinHttpReq.Open("GET", strURL, false);
// Send the HTTP request.
WinHttpReq.Send();
// Check whether password request form appeared
if (WinHttpReq.Status != 401) {
throw new Error(WinHttpReq.Status, "No authentication form received");
}
// 2. Try to login using the username and password
// Create an HTTP request with login and password
WinHttpReq.Open("GET", strURL, false);
WinHttpReq.SetCredentials(strName, strPassword, 0);
// Send the HTTP request.
WinHttpReq.Send();
// Check whether login succeded and page loaded
if (WinHttpReq.Status != 200) {
throw new Error(WinHttpReq.Status, WinHttpReq.StatusText);
}
// 3. Do a search for the given content string (regexp is allowed)
if (WinHttpReq.ResponseText.search(strRegex) == -1) {
throw new Error(1, "Couldn't find match to given expression: " + strRegex);
}
// return "Success" code
return 0;
} catch (objError) {
// Error handling
var strError = "Content checker returned error: ";
strError += (objError.number & 0xFFFF).toString() + "\n\n";
strError += objError.description;
WScript.Echo(strError);
// return "Failure" code
return 1;
}
}