Friday, 4 September 2009

Coldfusion File Streaming & Stream Proxying

Hot on the heels of yesterdays post about Java Streaming Proxy Servlets comes even more streaming goodness with CF.

The first bit of code opens a file, captures the underlying servlet output stream and then streams out the contents of the file.

streamFile.cfm

<cfsilent>
  <cfif ReFind("[^a-zA-Z0-9_\-\.]", URL.filename)>
    <cfthrow message="not permitted">
  </cfif>

  <cfscript>
    filename = REQUEST.fileDirectory & "/" & URL.filename;
    inputFile = createObject("java", "java.io.File").init(filename);
    inputStream = createObject("java", "java.io.FileInputStream").init(inputFile);
    outputStream = getPageContext().getResponse().getResponse().getOutputStream();

    while(true) {
      b = inputStream.read();

      if(b EQ -1) {
        break;
      }

      outputStream.write(b);
    }
    outputStream.flush();

    inputStream.close();
    outputStream.close();   
  </cfscript>
</cfsilent>

Streaming the contents of a file to a browser is all good because you didn't have to load the whole thing in memory before sending it down the wire. But what if you want to stream a file to a browers that is held on another server, not accessible from the internet. In that case you'll want to stream the file through a proxy to the client.

streamProxy.cfm

<cfsilent>
  <cfheader name="Content-Disposition" value="attachment; filename=#URL.userFilename#">
  <cfcontent type="text/plain">

  <cfif ReFind("[^a-zA-Z0-9_\-\.]", URL.filename)>
    <cfthrow message="not permitted">
  <cfelseif ReFind("[^a-zA-Z0-9_\-\.]", URL.userFilename)>
    <cfthrow message="not permitted">
  </cfif>

  <cfscript>   
    proxy = createObject("java", "org.apache.commons.httpclient.HttpClient").init();

    if(len(CGI.QUERY_STRING) GT 0) {
      query = "?#CGI.QUERY_STRING#";
    } else {
      query = "";
    }

    uri = REQUEST.streamUrl & query;

    proxyMethod = createObject("java", "org.apache.commons.httpclient.methods.GetMethod").init(uri); 
    proxy.executeMethod(proxyMethod); 
    inputStream = proxyMethod.getResponseBodyAsStream(); 
    outputStream = getPageContext().getResponse().getResponse().getOutputStream();
  
    while(true) {
      b = inputStream.read();

      if(b EQ -1) {
        break;
      }

      outputStream.write(b);
    }

    outputStream.flush();

    inputStream.close();
    outputStream.close();
  </cfscript>
</cfsilent>

The proxy uses the Jakarta Commons HttpClient which comes bundled with ColdFusion to call the target stream, passing any URL query parameters supplied.

The response from target tream URL is streamed back to the client via the underlying servlet output stream, just like the file streaming code.

An example project is available for download, make sure to customise the REQUEST scope variables in the Application.cfm file before testing.

Source Code