Bluehost.com Web Hosting $6.95

HTTP problem, wrong characters sent (HTTP pro's needed!)

This is a discussion on HTTP problem, wrong characters sent (HTTP pro's needed!) within the PHP Language forums, part of the PHP Programming Forums category; Hey, I am writing a file that reads in an external file in the web and prints it out including ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-27-2006
webEater
 
Posts: n/a
Default HTTP problem, wrong characters sent (HTTP pro's needed!)

Hey,

I am writing a file that reads in an external file in the web and
prints it out including the response header of the http protocol. I do
this to enable cross domain XMLHttpRequests.
I implemented it via fsockopen, like this:

<?
$url = $_REQUEST['uri']; // take the param as $uri
//... more ...
if ($c = fsockopen($host, $port, $errorNo, $errorStr, 5)) { //
connection
$headers = getallheaders(); // request headers
$h = ($headers['Content-Type'] ==
'application/x-www-form-urlencoded') ? 'POST' : 'GET'; // request
method
$h .= " $path HTTP/1.1\r\nHost: $host\r\n";
foreach ($headers as $name => $content) {
// don't forward user's (and your) cookies to external server!
if ($name == 'Accept')
$h .= 'Accept:
text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*;q=0.5'."\r\n";
elseif ($name == 'Accept-Encoding')
$h .= 'Accept-Encoding: '."\r\n";
elseif ($name != 'Host' && $name != 'Connection' && $name !=
'Cookie')
$h .= $name.': '.$content."\r\n";
}
$h .= 'Connection: closed'."\r\n";
$h .= "\r\n";
// header sent

// post content
if ($_POST) {
$posts = array();
foreach ($_POST as $key => $value)
$posts[] = urlencode($key).'='.urlencode($value);
$h .= implode('&', $posts);
}
fwrite($c, $h);

// read and output results
$header = true;
while (!feof($c)) {
$p = fgets($c);
if ($p == "\r\n")
$header = false;
if ($header) {
if (strpos($p, 'Content-Type') !== false && $html)
header(str_replace('xml', 'html', $p));
else
header(trim($p));
}
else
echo $p;
}
}
fclose($c);
}
else
echo $errorNo.' '.$errorStr;
}
?>

^ Thats the code, test it in Firefox and IE:

http://aka-fotos.de/research/uniajax...esponseXml.php

It means that http.php reads the file
http://aka-fotos.de/research/uniajax/responseXml, a simple XML file
that looks like this:

<?xml version="1.0" encoding="utf-8"?><response>hello</response>

Firefox shows the correct source code, exactly the same as above ^, but
IE shows an error (cannot find the page). To get to the bottom auf
things I used Rex Swains HTTP viewer that shows me all headers of a
http request including the body of the page:
http://rexswain.com/httpview.html. If you paste in my adress -
http://aka-fotos.de/research/uniajax...esponseXml.php
- and submit the form, you will see that the response body looks like
this:

(CR)(LF)
26·(CR)(LF)
<?xml·version="1.0"·encoding="utf-8"?>(CR)(LF)
1b·(CR)(LF)
<response>hello</response>(LF)
(CR)(LF)
0(CR)(LF)
(CR)(LF)

(CR) and (LF) are control characters, forget them, but I see some
charakters "26" and "1b". This result differs from what Firefox shows,
can somebody say me whats going on there?

I hope somebody can help me!
Thanks,

Andi

Reply With Quote
  #2 (permalink)  
Old 09-27-2006
Andy Hassall
 
Posts: n/a
Default Re: HTTP problem, wrong characters sent (HTTP pro's needed!)

On 27 Sep 2006 08:56:56 -0700, "webEater" <andreaskalsch@gmx.de> wrote:

>I am writing a file that reads in an external file in the web and
>prints it out including the response header of the http protocol. I do
>this to enable cross domain XMLHttpRequests.
>I implemented it via fsockopen, like this:
>
><?
>$url = $_REQUEST['uri']; // take the param as $uri
>//... more ...
> if ($c = fsockopen($host, $port, $errorNo, $errorStr, 5)) { //
>connection
> $headers = getallheaders(); // request headers
> $h = ($headers['Content-Type'] ==
>'application/x-www-form-urlencoded') ? 'POST' : 'GET'; // request
>method
> $h .= " $path HTTP/1.1\r\nHost: $host\r\n";


*klaxxon noises*

You are attempting to write an HTTP client yourself. You have made an HTTP/1.1
request, which means you _must_ implement some features as specified in the
specification to be able to decode the response, else it'll bite you.

> $h .= 'Connection: closed'."\r\n";


Not a valid value, you mean "close":
http://www.w3.org/Protocols/rfc2616/....html#sec14.10

> // read and output results
> $header = true;
> while (!feof($c)) {
> $p = fgets($c);
> if ($p == "\r\n")
> $header = false;
> if ($header) {
> if (strpos($p, 'Content-Type') !== false && $html)
> header(str_replace('xml', 'html', $p));
> else
> header(trim($p));
> }
> else
> echo $p;
> }
> }
>
>^ Thats the code, test it in Firefox and IE:
>
>http://aka-fotos.de/research/uniajax...esponseXml.php
>
>It means that http.php reads the file
>http://aka-fotos.de/research/uniajax/responseXml, a simple XML file
>that looks like this:
>
><?xml version="1.0" encoding="utf-8"?><response>hello</response>
>
>Firefox shows the correct source code, exactly the same as above ^, but
>IE shows an error (cannot find the page). To get to the bottom auf
>things I used Rex Swains HTTP viewer that shows me all headers of a
>http request including the body of the page:
>http://rexswain.com/httpview.html. If you paste in my adress -
>http://aka-fotos.de/research/uniajax...esponseXml.php
>- and submit the form, you will see that the response body looks like
>this:
>
>(CR)(LF)
>26·(CR)(LF)
><?xml·version="1.0"·encoding="utf-8"?>(CR)(LF)
>1b·(CR)(LF)
><response>hello</response>(LF)
>(CR)(LF)
>0(CR)(LF)
>(CR)(LF)
>
>(CR) and (LF) are control characters, forget them, but I see some
>charakters "26" and "1b". This result differs from what Firefox shows,
>can somebody say me whats going on there?


26 and 1b are chunk sizes.

You haven't implemented HTTP/1.1 Content-transfer-encoding: chunked, which is
mandatory and very commonly used in HTTP/1.1 server replies.

http://www.w3.org/Protocols/rfc2616/....html#sec3.6.1

"All HTTP/1.1 applications MUST be able to receive and decode the "chunked"
transfer-coding, and MUST ignore chunk-extension extensions they do not
understand."

Some choices:

(1) Make HTTP 1.0 requests instead of 1.1.
(2) Implement chunked transfer-coding.
(3) Use an HTTP client library that understands it, for example cURL.

--
Andy Hassall :: andy@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Reply With Quote
  #3 (permalink)  
Old 09-27-2006
webEater
 
Posts: n/a
Default Re: HTTP problem, wrong characters sent (HTTP pro's needed!)

Hi,

thank you, your notes helped me very much!

> You haven't implemented HTTP/1.1 Content-transfer-encoding: chunked, which is
> mandatory and very commonly used in HTTP/1.1 server replies.
>
> http://www.w3.org/Protocols/rfc2616/....html#sec3.6.1
>
> "All HTTP/1.1 applications MUST be able to receive and decode the "chunked"
> transfer-coding, and MUST ignore chunk-extension extensions they do not
> understand."
>
> Some choices:
>
> (1) Make HTTP 1.0 requests instead of 1.1.
> (2) Implement chunked transfer-coding.
> (3) Use an HTTP client library that understands it, for example cURL.


I took the first choice - taking v1.0 - it's simpler. Now it works
properly ,)

Reply With Quote
  #4 (permalink)  
Old 09-28-2006
Manuel Lemos
 
Posts: n/a
Default Re: HTTP problem, wrong characters sent (HTTP pro's needed!)

Hello,

on 09/27/2006 12:56 PM webEater said the following:
> I am writing a file that reads in an external file in the web and
> prints it out including the response header of the http protocol. I do
> this to enable cross domain XMLHttpRequests.
> I implemented it via fsockopen, like this:


I think you should not send headers that your HTTP client is not capable
of understanding.

Anyway, instead of reinventing the wheel, you may want to try this
proven HTTP client class:

http://www.phpclasses.org/httpclient


--

Regards,
Manuel Lemos

Metastorage - Data object relational mapping layer generator
http://www.metastorage.net/

PHP Classes - Free ready to use OOP components written in PHP
http://www.phpclasses.org/
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 12:23 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0