HTTP Progressive Upload

General discussions related to php

Moderators: egami, macek, gesf

Post Reply
Pablomagno
New php-forum User
New php-forum User
Posts: 2
Joined: Thu Apr 05, 2018 7:00 am

Thu Apr 05, 2018 10:09 am

I'll start to explain what are my need, that could show clearer my idea:

I'm developing a client application, which takes sound (voice of the user). Then, it sends the voice to my server as text encoded (LAMP - Linux, Apache, MySQL, PHP) via a HTTP POST. The Apache receives it and I can take it from:

Code: Select all

 $body = file_get_contents('php://input'); 
But now I want to make the upload via "streaming" over HTTP (Progressive upload), in order to stablish a continuous stream flow to APIs of voice recognition, reducing the delay (replacing the classic store and forward method).

I have experience in the other way, doing "progressive download". I could send the response flushing the buffer and "chunked-encoding" header to a client. But now, I developed a client which sends the data over HTTP POST and progressive (chunked) but the issue goes about the Apache/PHP system. The stream is correctly received but the PHP script is instanced when the HTTP session is closed. So I can't do anything with the chunks at the moment they arrive, I should wait to the end of the transmission, which makes meaningless the use of chunks.

So my question is about a way of deal with the chunks in server side, when the request is chunked. I want to read chunk by chunk as they are received.

Any idea is welcome.

Thanks in advance,

Pablo.

chorn
php-forum Fan User
php-forum Fan User
Posts: 537
Joined: Fri Apr 01, 2016 2:18 am

Fri Apr 06, 2018 6:26 am

i don't really get the problem - if your client sends the data chunk by chunk, every request will be a new php process in that you can process the data for each chunk separately. if you need to wait until the end, you must save the chunks anywhere and trigger a script after the upload is complete.

Pablomagno
New php-forum User
New php-forum User
Posts: 2
Joined: Thu Apr 05, 2018 7:00 am

Fri Apr 06, 2018 10:59 am

The chunks are sent in just one HTTP POST. The connection persists. If you capture de traffic you will see a persistent TCP connection, with no differences between a normal request.

But I'm using chunked encoding, supported in HTTP which is used to read properly the body as the chunks come. The body is splited in chunks and the receiver can read the body progessively, instroducing less delay time than the classic way which waits the close of the HTTP connection to start to read the body.

When the body is fully received, the destination answers with just one 200 OK.

In my example, we have a client which sends "hi", waits 2 seconds and then sends "there". The HTTP connection is just one, and is closed when the second chunk is received. The problem is that the PHP script in sever side, starts when the last chunk "there" is received. In my PHP script, I have:

Code: Select all

<?php

	header('Content-type: application/json; charset=utf-8');	
	ob_end_flush(); 

	$file_reader = fopen('php://input', "r");
	$body = fgets($file_reader,20);  

	echo $body. '<br />';
 	flush();

 	sleep(2);

 	echo "bija". '<br />';
 	flush();

		

?>
So, because the script instance starts when the whole body is received in the first echo+flush() in the body is sent "hithere". Then, we have a sleep(2) which waits 2 seconds, and then the server sends "bija" as a second chunk, closing the HTTP connection a little more than 4 secs from the begining (2 secs sleep in client + 2 secs sleep in server + little processing and traffic). The chunked header is automatically generated.

As you can see, I'm sending a chunked request and a chunked response. There aren't issue about protocol/communication. The problem is that the PHP script is executed when the full request is received. I can send "hithere" and "bija" as separated chunks, or "hi" and "there" too. But the problem is not about "what", the problem is about "when". I cannot start sending my answer when the first chunk arrives, I must wait to the POST request is received completely. But I don't want to send the response in chunks, my need is easier, I just want to start processing the body, chunk by chunk for do another action.

For example: I receive one chunk, and then I open another chunked HTTP connection, stablishing a continuous flux end to end. If I retransmit the same message I reveice, I'm doing an end to end streaming flux with my server in the middle.

I'm attaching some of my Wireshark captures as images, showing that.

Thanks!

Pablo.
Attachments
http post.png
http post.png (74.16 KiB) Viewed 182 times
chunk2.png
chunk2.png (101.68 KiB) Viewed 182 times
chunk1.png
chunk1.png (81.29 KiB) Viewed 182 times

Post Reply