Mar 20

Written by: Dylan Barber
3/20/2009 8:10 AM  RssIcon

Where I work, yes I sometimes work, full time we have run without ssl for a long time. In some ways it was a lack of need in others it was a lack of understanding of why we need it, not on my part but sometimes management still thinks like its 1970. Finally this last week we have gone through the painful process of putting security on all our public facing websites. Doing so always brings out legacy problems and the little quirks in your code and sometimes in other third party services. One of those quirks turned out to be Google Charts.

Google Charts doesn’t work over ssl it simply redirects to Google.com. We needed a solution to avoid panicking our users with the dreaded "some content came from an unsecured page" warning in the browser. After some research we wrote this little http handler to grab the chart and stream it to the page under ssl.

In order to use the HttpHandler we simply added a class to the App_Code folder but you could easily compile it to its own assembly if you wanted.

Add a verb to you web.config like so, of course the path could be almost anything you want.

   1: "GET" path="GoogleGraphicsSecure.ashx" type="GoogleSecureGraphs" />

Add this class to your App_Code folder or make an assembly out of it and add it to your project.

   1: Imports System
   2: Imports System.Web
   3: Imports System.Net
   4: Imports System.IO
   5:  
   6: Public Class GoogleSecureGraphs
   7:     Implements IHttpHandler
   8:  
   9:     Public ReadOnly Property IsReusable() As Boolean Implements System.Web.IHttpHandler.IsReusable
  10:         Get
  11:             Return False
  12:         End Get
  13:     End Property
  14:  
  15:     Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest
  16:  
  17:         Const BufferLength As Integer = 1000
  18:         Const Chartx As String = "http://chart.apis.google.com/chart?"
  19:         If (context Is Nothing) Then
  20:             Throw New ArgumentNullException("context")
  21:         End If
  22:  
  23:         If Not IsNothing(context.Request.QueryString) Then
  24:             Dim xURL As String = Null.NullString
  25:  
  26:             xURL = context.Request.RawUrl.Substring(context.Request.RawUrl.IndexOf("?") + 1)
  27:  
  28:             Dim MyRequest As HttpWebRequest = CType(WebRequest.Create(Chartx & xURL), HttpWebRequest)
  29:             MyRequest.Timeout = 10 * 1000
  30:  
  31:             ' Get the web response
  32:             Dim MyResponse As HttpWebResponse = CType(MyRequest.GetResponse(), HttpWebResponse)
  33:             Dim s As Stream = MyResponse.GetResponseStream
  34:  
  35:             ' Make sure the response is valid
  36:             If HttpStatusCode.OK = MyResponse.StatusCode Then
  37:  
  38:                 Dim ms As MemoryStream = New MemoryStream(MyResponse.ContentLength)
  39:                 Dim b As Byte() = New Byte(BufferLength) {}
  40:                 Dim cnt As Integer = 0
  41:  
  42:                 Do
  43:  
  44:                     cnt = s.Read(b, 0, BufferLength)
  45:                     ms.Write(b, 0, cnt)
  46:  
  47:                 Loop Until cnt < 1
  48:  
  49:                 context.Response.ContentType = MyResponse.ContentType
  50:                 context.Response.Cache.SetCacheability(HttpCacheability.NoCache)
  51:  
  52:                 context.Response.BinaryWrite(ms.ToArray)
  53:                 MyResponse.Close()
  54:  
  55:             End If
  56:         End If
  57:  
  58:         context.Response.End()
  59:  
  60:  
  61:     End Sub
  62:  
  63: End Class

Now anywhere you use http://chart.apis.google.com/chart? you should be able to replace it with https://yourdomain/GoogleGraphicsSecure.ashx? and keep the query string the same and get the same graph.

2 comment(s) so far...


Gravatar

Re: Google Charts with ssl

Hi,

Interesting article.

Would you have a working sample I can dissect.

I am having a problem getting this to work on my side.

thanks

By Ian on   4/6/2010 8:53 AM
Gravatar

Re: Google Charts with ssl

What part are you having problems with ? - i'll see if i can get an example together

By Dylan Barber on   4/6/2010 8:53 AM

Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
Enter the code shown above in the box below
Add Comment   Cancel