How can PHP code determine that Apache decides to return HTTP status 400?

314 views Asked by At

I have a Javascript client which is using JQuery to send Ajax POST requests to my PHP code on a shared Hostgator server. The post data is a few hundred KB of JSON data which is read using file_get_contents('php://input'). The PHP post data limit is reported as 64 MB, and normally everything works fine.

But every few days the PHP code is called with an illegal JSON string, and the client receives a status of 400. Presumably this status comes automatically from Apache, as my code is not doing anything special to send it. Yesterday I put code in place to log the post data, and it is truncated JSON of length 196,269...suspiciously just under 3 * 64KB.

It seems like the request is being truncated; since I see this from only one of many clients, I can imagine that there is some intermittent network connectivity issue. If this is a bad conclusion, I'd be happy for someone to correct me, but please note that this is not what I'm asking about.

What I don't understand is why the PHP code is being called in this case. Shouldn't Apache not call when a malformed request is received? It's not possible that Apache starts the PHP execution before the full request, including post data, is received, and doesn't learn that there's a problem until after the PHP code is running, is it? If this is the expected behavior, how can my PHP code detect that this is a malformed request? Currently I'm relying on checking for errors from json_decode, but this seems imperfect, e.g. if the data was something other than JSON. I'm putting in code to check http_response_code(), but my reading of the PHP documentation makes me suspect that this will return 200:

If response_code is not provided, then the current status code will be returned. Both of these values will default to a 200 status code if used in a web server environment.

I'd be happy just to be pointed at Apache/PHP documentation that describes how error cases such as this are handled; my searches of the online documentation haven't been successful.

Thanks much for any help.

EDIT: I was asked to show I am sending the data; it is as shown below. But again, by question is not about how to AVOID the error (I am reasonably sure that it is a connectivity problem), but what happens in the PRESENCE of the error.

  $.ajax({
    url: myurl,
    xhrFields: {'withCredentials': true}, // Send credentials to the server to allow CORS
    dataType: 'json',
    contentType: 'application/json; charset=utf-8',
    type: 'POST', // Means that this changes server state (i.e. not cachable) and needed to send large amounts of data
    data: JSON.stringify(dataToSend),
    timeout: 120000,
    async: true,
    success: syncStepSucc,
    error: syncAjaxErr
  } );

EDIT: PLEASE NOTE that I am trying to understand the interaction between Apache and PHP when garbled/truncated data is received by Apache, and Apache decides to return HTTP status 400. The post data limit is 64 MB, well over the 196 KB that was received.

0

There are 0 answers