Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 945347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:44:14+00:00 2026-05-15T22:44:14+00:00

I have two websites: one with Lighttpd with PHP and second with Apache and

  • 0

I have two websites: one with Lighttpd with PHP and second with Apache and neither of this handle chunked transfer encoding properly.

I send this request from my mobile, J2ME and there is no possibility to change this transfer type to any other.

So my only way to go is to handle chunked transfer enconcoded requests in other way. Any solution will be good as long as I can enable it on my CentOS server where I can install and change everything that will be necessary.

So my question is: how to handle chunked encoding request properly on server side?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T22:44:15+00:00Added an answer on May 15, 2026 at 10:44 pm

    EDIT:
    Which version of PHP/Apache/LightHTTP are you running? As there was this bug in PHP before, but it seems to be gone with 5.2.13 and 5.3.2.

    If the link above doesn’t help, I’d like to know exactly what PHP is seeing, can you put this in your api and post the results? (edited down of course).

    $input = file_get_contents('php://input');
    $stdin = file_get_contents('php://stdin');
    
    print "FILES: ";
    print_r($_FILES);
    
    print("<br>POST: ");
    print_r($_POST);
    
    print("<br>input: ".$input);
    print("<br>stdin: ".$stdin);
    die;
    

    This way we can see what PHP is seeing, and if it’s NOT decoding chunked encoding, then we CAN manually decode it.

    End EDIT. (leaving the below in case someone else finds it useful)

    I assume this is a followup to your previous question. And I’m assuming you’re reading the stream from PHP?

    I wrote this a few years back, it reads from a chunked encoded stream, you can then do whatever you want with the output. If it’s a large file don’t read it into a string, instead write to a file.

    <?php
    define('CRLF', "\r\n");
    define('BUFFER_LENGTH', 8192);
    $headers = '';
    $body = '';
    $length = 0;
    
    $fp = fsockopen($host, $port, $errno, $errstr, $timeout);
    
    // get headers FIRST
    do
    {
        // use fgets() not fread(), fgets stops reading at first newline
        // or buffer which ever one is reached first
        $data = fgets($fp, BUFFER_LENGTH);
        // a sincle CRLF indicates end of headers
        if ($data === false || $data == CRLF || feof($fp)) {
            // break BEFORE OUTPUT
            break;
        }
        $headers .= $data;
    }
    while (true);
    // end of headers
    
    // read from chunked stream
    // loop though the stream
    do
    {
        // NOTE: for chunked encoding to work properly make sure
        // there is NOTHING (besides newlines) before the first hexlength
    
        // get the line which has the length of this chunk (use fgets here)
        $line = fgets($fp, BUFFER_LENGTH);
    
        // if it's only a newline this normally means it's read
        // the total amount of data requested minus the newline
        // continue to next loop to make sure we're done
        if ($line == CRLF) {
            continue;
        }
    
        // the length of the block is sent in hex decode it then loop through
        // that much data get the length
        // NOTE: hexdec() ignores all non hexadecimal chars it finds
        $length = hexdec($line);
    
        if (!is_int($length)) {
            trigger_error('Most likely not chunked encoding', E_USER_ERROR);
        }
    
        // zero is sent when at the end of the chunks
        // or the end of the stream or error
        if ($line === false || $length < 1 || feof($fp)) {
            // break out of the streams loop
            break;
        }
    
        // loop though the chunk
        do
        {
            // read $length amount of data
            // (use fread here)
            $data = fread($fp, $length);
    
            // remove the amount received from the total length on the next loop
            // it'll attempt to read that much less data
            $length -= strlen($data);
    
            // PRINT out directly
            #print $data;
            #flush();
            // you could also save it directly to a file here
    
            // store in string for later use
            $body .= $data;
    
            // zero or less or end of connection break
            if ($length <= 0 || feof($fp))
            {
                // break out of the chunk loop
                break;
            }
        }
        while (true);
        // end of chunk loop
    }
    while (true);
    // end of stream loop
    
    // $body and $headers should contain your stream data
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a single Magento install running two different websites. One sells ebooks and
We have two websites using the same MqSQL database. One in English the other
I have two separate websites on the same server. One site is a forum
I have two websites (one asp classic and the other asp.net) which we would
My scenario is this - I have two ASP.net websites. Both sites run on
I have two servers with apache httpd on it. One of them is accessible
I've never experienced this problem. I have two hosting accounts, one with godaddy, and
I have two websites.. (MVC applications) www.WebsiteA.com (has two tabs in it) like this..
There are two websites, more are probably coming. One is a simple PHP site
I have two separate websites running locally on my IIS server at work. One

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.