# Strange problem with php_stream_read()

- Thread: https://externals.io/message/40306
- Original archive: https://news-web.php.net/php.internals/40306
- Started by Mangol Smith on 2008-09-06T22:19:04+00:00
- Messages: 2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #40306 — Mangol Smith — 2008-09-06T22:19:04+00:00

URL: https://externals.io/message/40306#40306

Hello all,

I got a strange problem. I opened a network stream with my web server
(localhost:80) and wrote a HTTP GET into stream and reading the HTTP
RESPONSE.

buff =  (char *)emalloc(content_size);
php_stream_read(http_stream, buff, content_size);

where content_size is the Content-Length value in Response header. Problem
is that when I return RETURN_STRING(buff,1); to userland. I'm getting a
string with the content (response body) + some crap.

length of string is more than content_size.

for example: in a case content_size was 29; after php_stream_read() line,
the strlen(buff) is returning 41. How is this possible?

somewhere I read this

php_stream_read() differs from other stream read functions in one surprising
way. If the stream in use is not a plain files stream, only one call to the
underlying stream implementation's read function will be made, even if more
data was requested and more is actually available to return. This is a
compromise to let packet-based protocols such as UDP function cleanly
without blocking.

I didn't clearly understand its meaning. Does it has anything to do with my
results? Any help in understanding this behavious of php_stream_read() ?



--
Mangol Smith

---

## #40307 — Scott MacVicar — 2008-09-07T00:41:27+00:00

URL: https://externals.io/message/40306#40307 · In reply to: #40306

buff needs to be null terminated, you can use RETURN_STRINGL(buff,  
content_size, 1); if you know the length.

Scott


> Hello all,
>
> I got a strange problem. I opened a network stream with my web server
> (localhost:80) and wrote a HTTP GET into stream and reading the HTTP
> RESPONSE.
>
> buff =  (char *)emalloc(content_size);
> php_stream_read(http_stream, buff, content_size);
>
> where content_size is the Content-Length value in Response header.  
> Problem
> is that when I return RETURN_STRING(buff,1); to userland. I'm  
> getting a
> string with the content (response body) + some crap.
>
> length of string is more than content_size.
>
> for example: in a case content_size was 29; after php_stream_read()  
> line,
> the strlen(buff) is returning 41. How is this possible?
>
> somewhere I read this
>
> php_stream_read() differs from other stream read functions in one  
> surprising
> way. If the stream in use is not a plain files stream, only one call  
> to the
> underlying stream implementation's read function will be made, even  
> if more
> data was requested and more is actually available to return. This is a
> compromise to let packet-based protocols such as UDP function cleanly
> without blocking.
>
> I didn't clearly understand its meaning. Does it has anything to do  
> with my
> results? Any help in understanding this behavious of  
> php_stream_read() ?
>
>
>
> --
> Mangol Smith
