Results 1 to 2 of 2

Thread: [RESOLVED] Forward an image through IIS

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

    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:
    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});
    // 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);
    // 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
            case 'cache-control':
            Response.CacheControl = val;
            case 'charset':
            Response.Charset = val;
            case 'content-type':
            Response.ContentType = val;
            case 'expires':
            var curDate = new Date();
            var expDate = new Date(val);
            Response.Expires = Math.ceil((expDate.getTime()-curDate.getTime())/(1000*60));
            Response.AddHeader(key, val);
    // Send the server response body to the browser
    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
    Göteborg, Sweden
    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