Through some sheer bad luck, I am now working on restructuring a system written in classic ASP. There’s a json-rpc service point on a separate domain that the app could consume, and I wanted it to consume it rather than reinventing the functionality again on the system. As much as I hate VBscript, ASP and IIS, I went googling and figure out a way forward.
Basically, it’s difficult to integrate JSON-RPC into a classic ASP system. However, it is very easy to integrate it into jQuery. Problem is, the service existed on an external domain, so what I needed is a proxy asp controller that will forward json-rpc request & response back and forth between the local client and the external json-rpc point.
One word of warning though: I haven’t tested this code extensively so use it at your own risk. – anyway here’s the controller script:
<%@ LANGUAGE=VBScript %> <% '# ========================================================================================================== '# '# JSON-RPC Proxy ASP [classic asp] '# '# This asp page/component will forward the json-rpc request to an existing json-rpc service '# and transparently forward the result back to the caller via the HTTP response. '# '# @author Zen Sugiarto '# ========================================================================================================== '# configuration '# ------------- '# This URl points to the json-rpc service point. '# if your system uses a different configuration mechanism, please adjust it accordingly '# Dim servicePointUrl servicePointUrl = "http://url-to-json-rpc/point" '# main body of execution '# ---------------------- '# this function allows for msg to be logged into a log file so we can trace / analyze the traffic. Sub logInfo (message) logFile = Server.MapPath("event.log") Const ForWriting = 2 Const ForAppending = 8 Set fs = CreateObject("Scripting.FileSystemObject") If fs.FileExists(logFile) Then Set logFile = fs.OpenTextFile(logFile, ForAppending) Else Set logFile = fs.CreateTextFile(logFile, True) End If logFile.WriteLine(Now() & vbTab & message) logFile.Close Set logFile = nothing Set fs = nothing End Sub ' we read all of the POST data, json-rpc supports get, but this component only does POST for now. ' the safe array read from .BinaryRead is then converted into string, which is then sent into the ' forwarding HTTP request object to the bus. dim safeArray, totalBytes totalBytes=Request.TotalBytes safeArray=Request.BinaryRead(totalBytes) jsonBody = "" For n = 1 To LenB(safeArray) jsonBody = jsonBody & Chr(AscB(MidB(safeArray,n,1))) Next ' create the HTTP request object to be forwarded into the service bus, ' inject the json body in. Dim httpreq Set httpreq = Server.CreateObject("MSXML2.XMLHTTP") httpreq.open "POST",servicePointUrl, False httpreq.setRequestHeader "Content-Type","application/json-rpc" 'httpreq.setRequestHeader "Cookie",Request.Cookies ' Send out the request On Error Resume Next httpreq.send jsonBody logInfo("REQUEST={ queryString=" & Request.QueryString & ", cookies="& Request.Cookies &", totalBytes=" & Request.TotalBytes & ", jsonBody="&jsonBody) ' TODO: implement error handling ' get response text, set mime type to application/json-rpc and write the bus response to the response stream Response.ContentType="application/json-rpc" Response.Write(httpreq.responseText) %> |
And now I can skip the whole shitty ASP architecture altogether and just go straight to web2.0 style programming. The controller will also log and output the request into a file called event.log. I haven’t finished writing a proper log rotator and whatnot with it. Just comment out the logInfo part if you’re not using it.
