Click to See Complete Forum and Search --> : How do browsers pack two pages into a single response ?


Tycho_Brahe
10-13-2008, 04:13 PM
This question is conceptual to some degree and has to do with how browsers send and receive data from the web server. Specifically, when the browser asks for one page I get back two ( one internal to the browser and one external page to be opened in adobe reader ). Please correct anything you see wrong and set me straight. I just want to learn how this works.

Here's what happened:

1. I have an aspx page that presents to the user a UI with some drop-down boxes and a button which says "Print...".
2. User clicks the Print button, the server does some calculations then using a third party PDF writer class it draws on a 'canvas' then writes that canvas into the Response.
3. The user's browser receives the response and displays a PDF in the browser.
4. User has to hit the back button to get back to the UI page ( step 1 )

So, we noticed that there was a Boolean parameter for the method in the PDF class called IsInline which if we changed it to false altered the behavior. Now the steps changed to:

1. I have an aspx page that presents to the user a UI with some drop-down boxes and a button which says "Print...".
2. User clicks the Print button, the server does some calculations then using a third party PDF writer class it draws on a 'canvas' then writes that canvas into the Response (?).
3. The user's browser blinks and displays the UI ( see step 1 ) in the original window, fixing the back button problem.
4. Simultaneous to step 3, the browser displays a dialog that says "Do you want to open or save this file ?" with OPEN, SAVE and CANCEL buttons. Note: While the dialog is present, the animated IE or firefox gif spins as if the page isn't finished loading even though the UI page looks to be completely drawn.
5. If the user clicks OPEN, the PDF is opened external to the browser ( in the stand-alone Adobe Reader ).

My question:

What information is sent back to the browser to tell it that in addition to the UI page, there is also a PDF page that needs to be opened ? I've looked in the browser's View Source for the served source code and can't find anything relating to the PDF file and I couldn't find anything relating to any type of javascript that might be doing a window.open to open the PDF. The page type when it comes back still seems to be text/html. I'm assuming for the PDF file the page type would be application/pdf ?

So, the browser has asked ( or posted and expects ) for one page back from the server and we get back the original UI page and are prompted to open the PDF ( externally ). As far as the browser is concerned, where is the request ? In other words, where can I see it ? Is it in the header that comes with each page response ? If so, I'd like to know what tool I can use to view this response. If not, can anyone explain how this works ?

Let me know if this doesn't make sense and I'll try to explain it better.

Thanks !

Eric Gooden

Tycho_Brahe
10-14-2008, 10:57 AM
Perhaps I've figured this out. Someone mentioned to me the concept of content-disposition and that was a huge help. I used the Fiddler tool to view the headers. Here's what I found:


My page 1 ( with the UI controls ) made a post to the server.
The server rendered the PDF and sent the response to the browser.
The browser leaves the original page 1 untouched but brings up a "Do you want to open or save this file ?" dialog to which I click OPEN. The end result is I have my original Page 1 open in the browser just as it was prior to the post and I also have a PDF document in the stand-alone Adobe Reader with the proper contents for the PDF.
From the browser using Fiddler I can see that the header indicates "Content-Type: application/x-msdownload" and "Content-Disposition: attachment;filename=Payment Statement.pdf"
Also, I can see that the Content-Length is 5425 bytes.
Using Fiddler, I cannot find any trace of the original html ( page 1 ) in the response, even though page 1 still looks like it did before.
If I save the PDF from the stand-alone Adobe Reader and check its file properties it is 5425 bytes in size.


So correct me if I'm wrong, but what is happening is that the server is serving up only the PDF content for the response. The server is adding the content-disposition to the header as well. Once the browser gets the response it sees that it's an attachement and a PDF and spawns an external program to open the content. Also, it appears that the normal behavior in this scenario is for the browser to not re-render the original page ( page 1 ) - which is why I don't see any trace of Page 1's contents anywhere in the response to IE ( only see the PDF contents in the response ).

The hardest part for me to grasp was why the contents of Page 1 wasn't being served to the browser as part of the response. In normal POST operations after the post is done the page you are viewing in the browser would be a representation of what was sent from the server as the response. I suppose it makes sense why it doesn't do this if it's an attachment ( you'd have to have 2 areas in the response, one for the first disposition and content and another for the second disposition and content ).

Are my assumptions close to being correct ?

Eric

toicontien
10-14-2008, 04:22 PM
Why not send the HTML page normally, then include a JavaScript on the page that opens a new window and sets its location to the URL of a PDF file? Also provide a link the user can click in case JavaScript is not available. Maybe use a META tag redirect.

Tycho_Brahe
10-14-2008, 04:35 PM
Why not send the HTML page normally, then include a JavaScript on the page that opens a new window and sets its location to the URL of a PDF file? Also provide a link the user can click in case JavaScript is not available. Maybe use a META tag redirect.

Sure, that would work. Yeah, I realize there are much better ways to do this. I just was wondering how it was doing what it is doing currently.

Thanks,

Eric