tags:

views:

159

answers:

2

Hi,

I was wondering if somebody could shed some light on this browser behaviour:

I have a form with a textarea that is submitted to to the server either via XHR (using jQuery, I've also tried with plain XMLHttpRequest just to rule jQuery out and the result is the same) or the "old fashioned" way via form submit. In both cases method="POST" is used. Both ways submit to the same script on the server.

Now the funny part: if you submit via XHR new line characters are transferred as "%0A" (or \n if I am not mistaken), and if you submit the regular way they are transferred as "%0D%0A" (or \r\n). This, off course, causes some problems on the server side, but that is not the question here. I'd just like to know why this difference? Shouldn't new lines be transferred the same no matter what method of submitting you use? What other differences are there (if any)?

+1  A: 

In part 3.7.1 of RFC2616(HTTP1.1), it allows either \r\n,\r,\n to represent newline.

HTTP relaxes this requirement and allows the transport of text media with plain CR or LF alone representing a line break when it is done consistently for an entire entity-body. HTTP applications MUST accept CRLF, bare CR, and bare LF as being representative of a line break in text media received via HTTP.

But this does not apply to control structures:

This flexibility regarding line breaks applies only to text media in the entity-body; a bare CR or LF MUST NOT be substituted for CRLF within any of the HTTP control structures (such as header fields and multipart boundaries).

CookieOfFortune
I understand that. The question is why the difference in the same browser?
Jan Hancic
Probably just differences in implementation, maybe they took the the XHR code from somewhere else?
CookieOfFortune
@CookieOfFprtune: I think you've misunderstood this section of the HTTP spec. No where in the spec is there any suggestion that a POST of an entity body would replace CRLF sequences with LF sequences. All it is saying is that whereas amongst the HTTP headers a header line is terminated strictly with CRLF, lines in a text body may not be, they __may__ contain only LF or CR or CRLF.
AnthonyWJones
That could be the cause yes. I'm talking about Firefox here (I checked the headers using LiveHeaders). I would love to test this in other browsers if a tool like this would be available for them.
Jan Hancic
@Jan: For IE use http://www.fiddler2.com/ (I use it for Firefox as well).
AnthonyWJones
+2  A: 

XMLHttpRequest will when sending XML strip the CR characters from the stream. This is in accord with the XML specification which indicates that CRLF be normalised to simple LF.

Hence if you package your content as XML and send it via XHR you will lose the CRs.

AnthonyWJones
I'm not sending XML, just plain text directly from a textarea.
Jan Hancic
+1, XHR spec is a better answer.
CookieOfFortune
@Jan: But you are using the XHR? What browser are you using? Can you show some code? In all probablity it will be this CRLF to LF normalisation that is tripping you up even if it shouldn't be.
AnthonyWJones
Yes I'm using XHR (I've tested both with jQuery and plain XMLHttpRequest object) in Firefox.I just don't understand why basically the same operation in one case uses CRLF and LF in the other. That is my only question here :) I know how to handle this "mess" on the server side :)
Jan Hancic
Oh, code is irrelevant here as it's just a simple $.post () nothing more.
Jan Hancic
Then perhaps you could show us the single $.post() line you are using?
AnthonyWJones
$.post ( 'path/to/script', { content: $('#txtContent').val () );
Jan Hancic