user2641103
user2641103

Reputation: 702

How to handle HTTP Expect header in PHP

I am using the curl command to send a file through a POST request. I'm not using the PHP curl library. However, the curl command is too polite and sends an Expect header. How should I handle it on the server side? I know that the server must return a 100-continue response, but how to it will continue receiving the file? The $_FILES array is always empty.

This is what I sent to the server:

/usr/bin/curl -sS -X POST -T "/tmp/phpuPfIDd" http://localhost/stats/rgateway/index.php/data 2>&1

And the server-side code would be something like this:

<?php
session_start();

switch($_SERVER['REQUEST_METHOD']) {
    case 'GET':
        # Do something if it is a GET request   
        break;

    case 'POST':    
        $headers = apache_request_headers();
        if (array_key_exists('Expect', $headers)) {
                 # What should I do here in order to receive uploaded the file?
        }   
        break;
}

Upvotes: 0

Views: 1102

Answers (1)

miken32
miken32

Reputation: 42695

On the server side, this should be handled by your web server, with no need for PHP to worry about such low-level things.

Relevant sections from man curl:

   -0, --http1.0
          (HTTP) Tells curl to use HTTP version 1.0 instead of using its
          internally preferred: HTTP 1.1.

   --expect100-timeout <seconds>
          (HTTP)  Maximum  time in seconds that you allow curl to wait for a
          100-continue response when curl emits an Expects: 100-continue
          header in its request. By default curl will wait one second. This 
          option accepts decimal values! When curl stops  waiting, it will
          continue as if the response has been received.

          (Added in 7.47.0)

So, if you have a new-enough version it appears that cURL will "continue as if the response has been received". If you don't have a new enough version, Expect is an HTTP 1.1 header so sending the request as HTTP 1.0 should resolve the problem.

Upvotes: 2

Related Questions