Detecting 413 (or other HTTP Status) in PHP

193 Views Asked by At

I Send Data to a webserver with POST or PUT method (server is FreeBSD, Apache httpd, php72).

I want to detect a 413 (too large) in my PHP script. But var_dump(http_response_code()); gives 200 even i uploaded data was too large (Apache httpd answers with it's 413 page, and my script gets executed as well).

var_dump($_SERVER); does also not show anything with 413.

How can i detect http status like 413 or any other in my script? and is it possible to let my script output the error-message, not apache?

my php-script looks like this

<?php

header('Content-Type: application/octet-stream');

echo ">---- output from php-script begins here\n";

if (http_response_code() != 200) { # <---- this does not work, always 200, how to detect http status code 413 from apache httpd?
  echo ">---- not ok\n";
}
else {
  echo ">---- ok\n";
}

?>

i use this command to upload a file

curl --head --silent --show-error --upload-file test-big-file.txt "my-domain.com/upload.php"

HTTP/1.1 413 Request Entity Too Large
Date: Thu, 09 Apr 2020 12:08:46 GMT
Server: Apache
Connection: close
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>413 Request Entity Too Large</title>
</head><body>
<h1>Request Entity Too Large</h1>
The requested resource does not allow request data with PUT requests, or the amount of data provided in
the request exceeds the capacity limit.
</body></html>
>---- output from php-script begins here
>---- ok

it does not make much sense to me that apache is prepending some error-message to my php-script-output. either user should get an error message or the output of my script. but not both at the same time.

if i clould detect this 413 in php i could just output some meaningful message and exit the script.

SOLUTION

i found out that if i use https:// for uploading the file apache does not call my php-script if file was too big.

only if i use http:// apache does this strange thing showing the error-document AND executing the php-script. i have absolutly no clue why this is. however... because i want to use https anyways... problem for me is gone.

here is an example output to see the diffrence in http and https in my case (CustomErrorDocument is emtpy, so no error output besides the header is shown here, in the final version, there will be an error-document of course)

curl --head --silent --show-error --upload-file test-big-file.txt "https://my-domain.com/test.php"
HTTP/2 413
date: Thu, 09 Apr 2020 17:22:23 GMT
server: Apache
content-type: text/html; charset=UTF-8



curl --head --silent --show-error --upload-file test-big-file.txt "http://my-domain.com/test.php"
HTTP/1.1 413 Request Entity Too Large
Date: Thu, 09 Apr 2020 17:22:28 GMT
Server: Apache
Upgrade: h2,h2c
Connection: Upgrade, close
Content-Type: text/html; charset=UTF-8
--> here is the output of the script that should not be executed <--

the line "--> here is the output of the script that should not be executed <--" is missing in the https call... it's only present in http call. strange...

notice: use of https gets an HTTP/2 response instead of HTTP/1.1 however, must be a misconfiguration from my web-hoster or a bug in HTTP/1.1 processing in apache. so problem solved by forcing everything to HTTPS and usage of ErrorDocument in .htacces to show own document on error as suggested by @ÁlvaroGonzález fixed it for me (users trying to use http will get an 301 Moved Permanently error with correct https-link). works for me.

thanks

0

There are 0 best solutions below