www.webdeveloper.com
Results 1 to 2 of 2

Thread: [RESOLVED] Forward an image through IIS

  1. #1
    Join Date
    Feb 2008
    Location
    Göteborg, Sweden
    Posts
    61

    resolved [RESOLVED] Forward an image through IIS

    I'm writing a "wrapper" script in Jscript for temporary usage on a clients server. The wrapper should be able to forward any request to another server and deliver the response back to the browser, transparantly. All page components on the other server are accessed through one file with different query strings, i.e. CSS through example.com?css, javascript through example.com?js, images through example.com?image=123, etc.

    By now, everything works well with markup, css and js, but image data doesn't come through and I can't figure out why. This is the essentials of my code:
    Code:
    <%@language=JScript%>
    <%
    var server = "example.com"; // the other server
    var path = "/clientfiles";
    
    var method = new String(Request.ServerVariables('REQUEST_METHOD'));
    var query = new String(Request.ServerVariables('QUERY_STRING'));
    if (query) {
        query = '?' + query;
    }
    
    // Get the request headers to forward to the other server
    // ('Host' header is treated specially)
    var headersEnum = new Enumerator(Request.ServerVariables);
    var headers = new Array();
    while (!headersEnum.atEnd()) {
        var match = headersEnum.item().match(/^HTTP_(\w+)$/);
        if ((typeof match).toLowerCase() == 'array') {
            var key = match[1].replace('_','-');
            var val = key.toLowerCase() == 'host' ? server : Request.ServerVariables(headersEnum.item());
            headers.push({'key': key, 'val': val});
        }
        headersEnum.moveNext();
    }
    
    
    // Make request to the other server, forwarding headers, get- and post data
    var http = Server.CreateObject("Msxml2.ServerXMLHTTP.3.0");
    http.open(method, 'http://'+server+path+query, false);
    for (var i=0; i<headers.length; i++) {
        http.setRequestHeader(headers[i].key, headers[i].val);
    }
    http.send();
    
    // Send server response headers to the browser
    var headersReturned = new String(http.getAllResponseHeaders());
    var headerLines = headersReturned.split(/(\r\n|\n)+/);
    for (var i=0; i<headerLines.length; i++) {
        var match = headerLines[i].match(/^([^:]+): (.+)$/);
        var key = match[1];
        var val = match[2];
        switch (key.toLowerCase()) {
            case 'transfer-encoding':
            // Sending transfer encoding to browser causes IIS to silently crash
            break;
            case 'cache-control':
            Response.CacheControl = val;
            break;
            case 'charset':
            Response.Charset = val;
            break;
            case 'content-type':
            Response.ContentType = val;
            break;
            case 'expires':
            var curDate = new Date();
            var expDate = new Date(val);
            Response.Expires = Math.ceil((expDate.getTime()-curDate.getTime())/(1000*60));
            break;
            default:
            Response.AddHeader(key, val);
        }
    }
    
    // Send the server response body to the browser
    Response.Write(http.responseText);
    %>
    Probably you can tell from the code above, but I'm quite a novice in ASP (even though experienced PHP and Javascript developer). I've made a similar wrapper in PHP that works for both text and images, so I guess I'm just missing something here.

    Image data that is forwarded through the script above end up in the browser as the four bytes 0xffd8ffe0, regardless of which image I try to retrieve...

    Any help appreiated!

  2. #2
    Join Date
    Feb 2008
    Location
    Göteborg, Sweden
    Posts
    61
    This thing was solved by using Response.BinaryWrite(http.responseBody) as the last line.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles