2

I'm trying to upload a file to a remote server using php's fsockopen. If my request looks like:

$httpContent = [
        "POST /index.php HTTP/1.1", 
        "Host: The IP address of my remote server", 
        "Connection: Close", 
        "User-Agent: My client"
];

the server responses with 200 OK. However if I add

"Content-Type: multipart/mixed; boundary=".md5('myBoundary'), 
"--".md5('myBoundary'), 
"Content-Type: text/plain", 
"value", 
"--".md5('myBoundary')."--"

The server returns "400 Bad Request". Everything seems fine to me, but somehow it doesn't work. What am I doing wrong?

Any help is greatly appreciated!

Edit (@Rei advised to post a dump of the request):

this is what echoes in client's terminal

POST /index.php HTTP/1.1
Host: The IP address of my remote server
Connection: Close
User-Agent: My client
Content-Type: multipart/mixed; boundary=be0850c82dd4983ddc49a51a797dce49
--be0850c82dd4983ddc49a51a797dce49
Content-Type: text/plain
value
--be0850c82dd4983ddc49a51a797dce49--

And this is what the server gets (caught with tcpdump):

POST /index.php HTTP/1.1
Host: The IP address of my remote server
Connection: Close
User-Agent: My client
Content-Type: multipart/mixed; boundary=be0850c82dd4983ddc49a51a797dce49
--be0850c82dd4983ddc49a51a797dce49
Content-Type: text/plain
value
--be0850c82dd4983ddc49a51a797dce49--

I personally don't see any difference between the two and also don't see anything wrong with the request. Yet, the server returns "400 Bad Request".

*Note: The "The IP address of my remote server" is a real IP address, but I'm not posting it due to security concerns.

**Note: Didn't mention that the PHP script is run in a CLI environment and is not used as a web-application back-end (the requests are NOT prepared by a web browser).

4
  • The error message indicates that there is a problem with the request, so you need to show your actual HTTP request. The problem should be obvious then.
    – Rei
    Jul 29, 2018 at 0:16
  • How to get the whole request? Jul 29, 2018 at 2:51
  • Combine everything you're fwrite()-ing into a variable and echo it in the end. Alternatively, every time you call fwrite(), pass the same string to echo. Make sure that you echo nothing else or you'll confuse yourself.
    – Rei
    Jul 29, 2018 at 4:08
  • Question edited with the details provided. Jul 29, 2018 at 4:30

1 Answer 1

6

Just as I suspected, the HTTP request you're sending does not follow HTTP specification. See RFC7230 Section 3. The problem: there are missing CRLFs.

There should be an extra CRLF after the last HTTP header, and one after the last MIME header, too.

POST /index.php HTTP/1.1
Host: The IP address of my remote server
Connection: Close
User-Agent: My client
Content-Type: multipart/mixed; boundary=be0850c82dd4983ddc49a51a797dce49

--be0850c82dd4983ddc49a51a797dce49
Content-Type: text/plain

value
--be0850c82dd4983ddc49a51a797dce49--

That will fix the "Bad Request" problem.

Although the cause of the problem is not the Content-Type: multipart/mixed header, you should know that it is deprecated. Use Content-Type: multipart/form-data instead. See related answer here and RFC7578.

Example of correct request

The content type multipart/form-data can be used to send values and files. The difference is only the attribute filename.

Modifying your HTTP request as little as possible, here is an example that sends two fields (one file and one value):

POST /index.php HTTP/1.1
Host: The IP address of my remote server
Connection: Close
User-Agent: My client
Content-Type: multipart/form-data; boundary=be0850c82dd4983ddc49a51a797dce49
Content-Length: 234

--be0850c82dd4983ddc49a51a797dce49
Content-Disposition: form-data; name="one"; filename="example.txt"

foo
--be0850c82dd4983ddc49a51a797dce49
Content-Disposition: form-data; name="two"

bar
--be0850c82dd4983ddc49a51a797dce49--

For each field, you need a Content-Disposition header to be able to name the field and optionally give it a filename.

Content-Type is optional although you may want to add one if the value comes garbled.

Modify as necessary if you only need to upload one file.

Testing the request

Here's a neat little script that I use to test HTTP requests:

<?php
header('Content-Type: application/json');
echo json_encode([
    'method' => $_SERVER['REQUEST_METHOD'],
    'uri' => $_SERVER['REQUEST_URI'],
    'body' => file_get_contents('php://input'),
    'GET' => $_GET,
    'POST' => $_POST,
    'FILES' => $_FILES,
    'headers' => getallheaders(),
], JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);

With that script at the receiving end, I sent my example request without modification to my localhost server and I got this result:

HTTP/1.1 200 OK
Date: Tue, 31 Jul 2018 11:33:57 GMT
Server: Apache/2.4.9 (Win32)
Connection: close
Transfer-Encoding: chunked
Content-Type: application/json

254
{
    "method": "POST",
    "uri": "/index.php",
    "body": "",
    "GET": [],
    "POST": {
        "two": "bar"
    },
    "FILES": {
        "one": {
            "name": "example.txt",
            "type": "",
            "tmp_name": "C:\\wamp\\tmp\\phpD6B5.tmp",
            "error": 0,
            "size": 3
        }
    },
    "headers": {
        "Host": "The IP address of my remote server",
        "Connection": "Close",
        "User-Agent": "My client",
        "Content-Type": "multipart/form-data; boundary=be0850c82dd4983ddc49a51a797dce49",
        "Content-Length": "234"
    }
}
0

As you can see, field "one" goes to $_FILES and field "two" goes to $_POST.

First, send the example request without modification. Keep trying until you get the correct result. Then modify as needed, making sure every step of the way that the request still follows HTTP specification.

10
  • 1
    As far as I get it CRLF is some kind of an empty line (\r\n) that goes somewhere before the actual message. In the code you posted, I see no change from what I'm doing. Also I am using fputs($socket, join("\r\n", $httpContent)."\r\n\r\n") to write to the fsockopen handle. Jul 29, 2018 at 12:53
  • Sorry! My bad. I saw the difference: the empty row after the Content-type: multipart/mixed header. I added \r\n to this array element BEFORE joining the array using `join("\r\n", $httpContent). Thank you! Jul 29, 2018 at 15:51
  • OK. Third Comment :) How to get value? Right after the begining of the boundary I have Content-Disposition: form-data; name="file" and then value. No Content-Type. When I try to var_dump($_POST) in index.php on the remote server it returns array(0){}. When I try to print_r(getallheaders()) I get all headers before --{boundary}. Jul 29, 2018 at 20:24
  • @EmilAvramov Answer updated with a working example of HTTP request.
    – Rei
    Jul 30, 2018 at 0:57
  • How to get foo or bar in index.php on the server? Jul 30, 2018 at 8:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.