473,756 Members | 4,256 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,ima ge/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_repl ace('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>h ello</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

Sep 27 '06 #1
3 2152
On 27 Sep 2006 08:56:56 -0700, "webEater" <an***********@ gmx.dewrote:
>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_repl ace('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>h ello</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>hell o</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 :: an**@andyh.co.u k :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Sep 27 '06 #2
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 ,)

Sep 27 '06 #3
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/
Sep 28 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3387
by: Jerry | last post by:
I've found a problem with exec, passthru, shell_exec & system. I'm trying to run the following exec("sort -r -n -k2,2 r1.txt > r2.txt") with r1.txt being a numeric file. The file looks like this: 237 291 845 152 585 3 193 810 173 484 151 3
7
1383
by: Juul | last post by:
Hi, My hostingprovider just updated to PHP 4.4. Since that time I have problems using the mail() function in PHP. I can't send headers anymore and my provider don't know what's wrong. Here's a sample script: if (mail("xxx@xxx.com", "test mail", "This is a <B>test</B>!", "From:
1
1349
by: Shahzad Atta | last post by:
Hi there, My web server needs to get html source of a web page from a remote web server . This html will be rendered to client browser after some processing. I know it can be done via XMLHTTP object but I am looking towards a .Net complaint solution, which is part of default .Net framework.(No third party component please). I am using C# with visual studio 2003 .Net framework 1.1 on windows XP OS.
1
1673
by: Andrea | last post by:
I was trying to deserialize my xml file (I enclose it below ) into a class. So I generated an xsd file given my xml using the MS xsd tool; then I generated the corresponsing C# class (using the MS xsd tool): the UserSecurityData class. When I try to execute my app to deserialize my xml file, my application crashes causing a strange FileNotFoundException when invoking the XmlSerializer constructor (second line of my code fragment). Is there...
8
24277
by: Andreas Klemt | last post by:
Hello, I get this error Message "cannot redirect after http headers have been sent" when I do this response.redirect ("home.aspx") How can I find out with vb.net if already a http header has been sent like If response.IsSentHttpHeader then ....
6
7773
by: Andreas Klemt | last post by:
Hello, I get this error Message "cannot redirect after http headers have been sent" when I do this response.redirect ("home.aspx") How can I find out with vb.net if already a http header has been sent like If response.IsSentHttpHeader then ....
4
1166
by: batista | last post by:
Hi there, My problem is that, i've created a simple hello world web service project, and then, when i run it to test the operation,the invoke button does not open any window. I mean there is no response when i click the invoke button, no window containing the output opens. It just does nothing, no error no exception nothing.
0
3579
by: seven.su | last post by:
I am trying to streaming back a xls file in page render event and this has been working properly in .net 1.1. But now I got 'Server cannot set content type after HTTP headers have been sent' under 2.0. Interestingly,Response.WriteFile works fine in 2.0 but I don't know if 2.0 has fixed the notorious buffer problem in WriteFile function or not. So, is it a bug in 2.0 or there is some other to manipulate Response string in Render event? ---...
5
11528
by: gibble | last post by:
Hi, I am going crazy. We get a hundred or so of these errors each day and while the fix would seem obvious, the error does not include a line number! -------------------- Process information: Process ID: 10084 Process name: w3wp.exe
0
10046
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9886
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9857
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8723
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7259
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6542
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5155
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3369
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2677
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.