Apex

Life in the cloud.

How to Determine HTTP or HTTPS in Salesforce Apex

While playing around with some code tonight I needed to be able to find out if the url on a Visual Force page in Sites was using HTTP or HTTPS.  There doesn’t seem to be a specific server side variable available.  After outputting all the header variables and comparing them, it appears that the ‘CipherSuite‘ variable only appears in HTTPS.

To capture the variables I used this:

public String debugInfo {get; set;} {
	debugInfo = '';
 	// All Incoming Headers
	debugInfo += '<h1>ALL INCOMING HEADERS</h1>';
	for (string key: ApexPages.currentPage().getHeaders().keySet()) {
		debugInfo += key + ' = ' + ApexPages.currentPage().getHeaders().get(key) + '<br />';
	}
}

Then just output the variable {!debugInfo} on your Visual Force page.

<apex:outputText escape="false" value="{!debugInfo}"></apex:outputText>

Change HTTP to HTTPS on the VF Page and you will notice the CipherSuite variable appear.  In the class where you need to detect it, do something like this:

public Boolean hasSSL{ get; set; }
hasSSL = FALSE;
   if (ApexPages.currentPage().getHeaders().get('CipherSuite') != null) {
      hasSSL = TRUE;
   }

I will be following up with another post shortly on why I needed to do this and how you can create a custom login screen using Visual Force and Sites.