YAJPT: Headers work for IE, but not Firefox

AFB

Lifer
Jan 10, 2004
10,718
3
0
I have had this problem with other websites. Also, does anyone know how to get the request headers from the client? It always returns and says it has nothing to read.


This is the main class

 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Originally posted by: amdfanboy
I have had this problem with other websites. Also, does anyone know how to get the request headers from the client? It always returns and says it has nothing to read.


This is the main class

This is the ClientConnection class . The place where I build the headers are at the bottom. It currently only will send one file out because I can't get it to read the request for the client.

Thanks for your help :)
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
I must confess I understand very little of that. I haven't delved into any nio stuff. But I do understand the http half. At some point somewhere do you have a tcp Socket that is reading info in from the client? Where are you actually receiving some kind of a connection from the client and reading what it has to say?
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Originally posted by: kamper
I must confess I understand very little of that. I haven't delved into any nio stuff. But I do understand the http half. At some point somewhere do you have a tcp Socket that is reading info in from the client? Where are you actually receiving some kind of a connection from the client and reading what it has to say?

Well, let me sum up the NIO part as this, you read it(the channel) and you get a fancy byte array called a ByteBuffer. I took out that part because it was returning 0 (read nothing). The part that confused me was why it couln't read anything and why the headers only worked sometime.


BTW, I would have posted this @ Java Ranch, but their Sockets & Networking forum is dead.
Output from telnet :
HTTP/1.0 200 OK
Content-Type: text/html
Content-Length: 30

<center><b>Hello!</b></center>

This is the part where the header is encoded into a byte array so that you can send it to the browser.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
So you say:

telnet localhost xxxx

and then the headers come right back immediately? Or have you actually typed some request headers into telnet?
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Originally posted by: kamper
So you say:

telnet localhost xxxx

and then the headers come right back immediately? Or have you actually typed some request headers into telnet?

I don't bother to read the input because it doesn' return anything. That's one of my problems. It currently just makes a header for the file and sends the header then the file. Later it should wait till you give it the request headers, but it doesn't now.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
So you either need to find a method that will block until there is input or you need to loop somehow until you have sufficient input (which isn't cool cuz you'd have to busy-wait). With traditional socket programming (tcp, anyways) you wrap a BufferedReader around a socket (with a few layers in between). Then you either do a readLine (blocks until you get a \n) or you grab character by character until you have everything you want. Either way, you need something that blocks until you have enough input to move forward.
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Originally posted by: kamper
So you either need to find a method that will block until there is input or you need to loop somehow until you have sufficient input (which isn't cool cuz you'd have to busy-wait). With traditional socket programming (tcp, anyways) you wrap a BufferedReader around a socket (with a few layers in between). Then you either do a readLine (blocks until you get a \n) or you grab character by character until you have everything you want. Either way, you need something that blocks until you have enough input to move forward.

Hmm, I guess I could just keep reading it when it has data and concatenating till I get enough. I'll try that, thanks :)
 

stndn

Golden Member
Mar 10, 2001
1,886
0
0
I have no clue with the java part, and i have very little to do with HTTP headers..
but if i remember it correctly, don't HTTP headers have to end with "\n\n" instead of "\r\n"?

That is to say, should it end with something like "Content-type: text/html\n\n"?

If this makes no difference whatsoever, blame java and not me ,p
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Yes, Http headers must end with a complete blank line. But rfc 2616 specifies that line breaks are of the form CRLF (I refer you to pages 35 and 39). I believe that translates to \r\n so the end of the the headers section would actually have to be: \r\n\r\n. I don't know if this is taken as law or if you can get away with using just \n\n but ethereal tells me that both firefox and the ietf servers were using \r\n (and the ietf server is apache running on redhat, unless they were lying). At any rate, if you use some kind of readLine method to retrieve the data java should probably handle it for you.