Unable to Upload File Via POST From Qt to PHP Script - php

I'm trying to upload a file via POST from a Qt (C++) application to my webserver, running a LAMP stack.
All seems to go well, but for some reason, the script's $_POST and $_FILES arrays are completely empty. I've tried uploading the same file with a plain HTML form, and it works.
Here's how I construct the request inside my Qt program:
request.setUrl(ServerAddress);
request.setHeader(request.ContentTypeHeader, "application/x-www-form-urlencoded");
reply = nam.post(request, file);
And the PHP:
<?php
var_dump($_POST);
var_dump($_FILES);
?>
Where request is a QNetworkRequest, reply is a QNetworkReply, nam is a QNetworkAccessManager, and file is the open QFile for my file.
The request sends fine, and I've even tried to see if somehow the request got messed up by using Wireshark. Here's the output:
POST /upload.php HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Content-Length: 8
Connection: Keep-Alive
Accept-Encoding: gzip, deflate
Accept-Language: en-US,*
User-Agent: Mozilla/5.0
Host: mysite.com
My data!HTTP/1.1 200 OK
Date: Wed, 03 Jul 2013 15:16:37 GMT
Server: Apache/2.2.22 (Ubuntu)
X-Powered-By: PHP/5.4.17RC1
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 36
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html
array(0) {
}
array(0) {
}
It's a very small text file, and only contains "'My data!". I just don't get why the PHP script isn't seeing the file. Any ideas where I'm going wrong?

Related

How to know/explore the correct/should-be content of CURLOPT_HTTPHEADER option array to get the content of a specific url by php-curl extension? [duplicate]

When I browse to a page with Firefox and click a download link, the following headers are shown when I inspect the request in network inspector:
Connection: keep-alive
Content-Disposition: attachment; filename="example_file.mp3"
Content-Length: 35181829
Content-Transfer-Encoding: binary
Content-Type: audio/mpeg
Date: Fri, 19 Aug 2016 18:19:02 GMT
Keep-Alive: timeout=60
Server: nginx
X-Powered-By: PHP/5.4.45
However, when I use cURL to visit the same address, I get this:
Connection: keep-alive
Content-Length: 1918
Content-Type: text/html; charset=UTF-8
Date: Fri, 19 Aug 2016 20:46:23 GMT
Keep-Alive: timeout=60
Server: nginx
X-Powered-By: PHP/5.4.45
How can I form a request with cURL that gives me the same response as Firefox?
In Firefox, open up the Net tab in the developer options(F12) and open the URL of the page you need.
Take note of all the Request Headers in the request sent to the server:
Example:
Accept
text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding
gzip, deflate
Accept-Language
nl,en-US;q=0.7,en;q=0.3
Connection
keep-alive
Cookie
_ga=GA1.2.598213448.1471644637; _gat=1
Host
mariannesdelights.be
User-Agent
Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0
Put all the headers in an array in this way
$headers = array('HeaderName:HeaderValue','HeaderName2:HeaderValue2');
Use the php function curl_setoption() to set the headers in the request:
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
That should produce the exact same HTTP-Response headers.

php - Output Compression problems

I'm facing a problem trying to use php output compression , I've been searching for many hours and I still have no clues...
Lets see a simple script :
<?php
$response = "abcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefgh";
if(function_exists('ob_gzhandler'))
{
ob_start('ob_gzhandler');
}
else {
ob_start();
}
echo $response;
ob_end_flush();
This is a method I've found all over the internet, and it used to work for me ... but not any more (and I've got no idea why) .
If i look at the http headers when I call this script :
Request :
Host: 192.168.51.191
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
Cache-Control: max-age=0
Response :
Connection: Keep-Alive
Content-Type: text/html
Date: Tue, 26 Jan 2016 15:19:07 GMT
Keep-Alive: timeout=5, max=100
Server: Apache/2.4.9 (Win64) OpenSSL/1.0.1g PHP/5.5.12
Transfer-Encoding: chunked
Vary: Accept-Encoding
X-Powered-By: PHP/5.5.12
You can see that the response is NOT zipped (Firebird gives me a 0.06ko response), and the server sends the responses using chunked encoding.
I tried an alternate method to send zipped responses :
<?php
$response = "abcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefgh";
$replyBody = gzencode($response, 9, FORCE_GZIP);
header("Content-Encoding: gzip");
echo $replyBody;
And the response headers are as follow (the request headers are always the same):
Response :
Connection: Keep-Alive
Content-Type: text/html
Date: Tue, 26 Jan 2016 15:29:01 GMT
Keep-Alive: timeout=5, max=100
Server: Apache/2.4.9 (Win64) OpenSSL/1.0.1g PHP/5.5.12
Transfer-Encoding: chunked
X-Powered-By: PHP/5.5.12
As you can see, this is basically the same behavior as in the first method.
Then if I try this :
<?php
$response = "abcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefgh";
$replyBody = gzencode($response, 9, FORCE_GZIP);
echo $replyBody;
I receive something that looks like a zipped response (random characters), and the output size is 0.03ko .
Here a the corresponding response http headers :
Connection: Keep-Alive
Content-Length: 31
Content-Type: text/html
Date: Tue, 26 Jan 2016 15:32:46 GMT
Keep-Alive: timeout=5, max=100
Server: Apache/2.4.9 (Win64) OpenSSL/1.0.1g PHP/5.5.12
X-Powered-By: PHP/5.5.12
It lets me think that the zipping part is working correctly because the output size has been reduced (it is obviously not readable because the browser can't know that it is zipped content).
That's where I'm lost ...
If I understand it right, when I manually send zipped data (using gzencode) , If I set the header"Content-Encoding: gzip" , the webserver/php seems to UNZIP it before sending it to the browser ??? How is it possible ?
And Why is it sending it as "chunked" data instead of setting the Content-Length header ?
I tried to set the Content-Length manually in the response; it doesn't change anything (it won't appear in the response headers, I'll still have a "chunked" response.
I've seen somewhere that I have to write the "Content-Length" header before sending other data or header to avoid the "chunked" response, I tried it and still had the same results.
I thought it could be a problem with BOM characters at the beginning of my php test script, but it is saved in UTF-8 without BOM encoding so I don't think it 's the problem.
I have this problem on my dev computer (using wampserver) AND in the production environment (IIS), previously it was working on both servers.
I have this problem using several browsers, and I checked the response sizes I wrote previously with fiddler.
Does anyone see where the problem could be ?
Thanks in advance
I'd go through the following checklist if I were you.
1. Check zlib extension is installed or not.
ob_gzhandler needs the zlib extension to work. Without which it just silently falls back to default settings.
2. Verify that you don't have zlib.output_compression enabled in your php.ini.
As explained here, even though zlib.output_compression is preferred over ob_gzhandler(), you can not use both simultaneously. So your code becomes..
if (extension_loaded('zlib') && !ini_get('zlib.output_compression')){
ob_start('ob_gzhandler');
}
3. Check if headers have already been sent eg. something got output-ted before the
ob_start(ob_gzhandler) This will prevent the compressed output from being detected as such. eg. Having some character before <?php or an echo somewhere up in the code.
4. Make sure you aren't using all of the above in addition to the gzipping in apache(mod_deflate).
This will only cause the output to be double gzipped which most probably will confuse the browser.

How can i log response code from server

i need to log the response header/code from the server?
How can i do this? With NginX or with PHP/Curl?
An example:
(This i the request from the client)
----------------Request-------------------------
GET /download.php?request=abc123xyz&link=http%3A%2F%2Fyoutube.com%2Fwatch?v=pRPOztxXWlQ HTTP/1.1
Host: api.example.com
Accept-Encoding: gzip,deflate
Cache-Control: no-cache
Pragma: no-cache
Accept-Language: de, en-gb;q=0.9, en;q=0.8
Accept: application/json
User-Agent: JDownloader
Answer from my Server (Response Code/Header)
----------------Response Information------------
Connection-Time: keep-Alive
----------------Response------------------------
HTTP/1.1 200 OK
Server: nginx
Date: Fri, 28 Nov 2014 12:03:32 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
Strict-Transport-Security: max-age=63072000; includeSubDomains
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
------------------------------------------------
I need this, because i have sometimes problems with the download of different files. My service downloads videos from some streamhoster like youtube. The output of the response code helps me to know if this a problem with my server or with the external downloader (JDownloader, Load!, ...)
Add header content in your access log with $http_<my_header> variables where <my_header> is the desired header name in lower can with dashes replaced by underscores. For instance X-Frame-Options could be logged with $http_x_frame_options added to access logformat.

Android Media Player send a myterious request

I test an Android application which plays videos.
The problem is the video play fail with an unknown error:
MediaPlayer(20533): Error (1,-2147483648)
The application code :
// Place the video view.
mVideoView = (VideoView) findViewById(R.id.surface_view);
// Retrieve the video path.
path = getIntent().getStringExtra(PATH);
Log.i(TAG, "Play the file : " + path);
// Set the video to play.
mVideoView.setVideoPath(path);
mVideoView.setMediaController(new MediaController(this));
mVideoView.requestFocus();
// Start the video play.
mVideoView.start();
The web server which serves the video is an Apache2 with PHP 5.
HTTP requests:
The first:
GET /.../hsf6bfs9/android.mp4 HTTP/1.1
Host: site.com
Connection: keep-alive
User-Agent: stagefright/1.2 (Linux;Android 4.2.2)
Accept-Encoding: gzip,deflate
HTTP/1.1 200 OK
Date: Tue, 04 Jun 2013 16:57:42 GMT
Server: Apache
Cache-Control: public
Content-Transfer-Encoding: binary
Content-Disposition: inline; filename="android.mp4"
Accept-Ranges: bytes
Content-Range: bytes 0-5245532/5245533
Content-Length: 5245533
Keep-Alive: timeout=30, max=100
Connection: Keep-Alive
Content-Type: video/mp4
The second:
GET /.../hsf6bfs9/android.mp4 HTTP/1.1
Host: site.com
Connection: keep-alive
User-Agent: stagefright/1.2 (Linux;Android 4.2.2)
Accept-Encoding: gzip,deflate
Range: bytes=4927056-
HTTP/1.1 206 Partial Content
Date: Tue, 04 Jun 2013 16:57:45 GMT
Server: Apache
X-Mod-H264-Streaming: version=2.2.7
Content-Length: 318511
Last-Modified: Tue, 04 Jun 2013 16:57:43 GMT
ETag: "36bc00b-500a5d-4de56facaefc0;5245567"
Accept-Ranges: bytes
Content-Range: bytes 4927056-5245566/5245567
Keep-Alive: timeout=30, max=100
Connection: Keep-Alive
Content-Type: video/mp4
And the final one before the fail:
GET /.../hsf6bfs9/android.mp4 HTTP/1.1
Host: site.com
Connection: keep-alive
User-Agent: stagefright/1.2 (Linux;Android 4.2.2)
Accept-Encoding: gzip,deflate
Range: bytes=3591808430-
HTTP/1.1 416 Requested Range Not Satisfiable
Date: Tue, 04 Jun 2013 16:57:47 GMT
Server: Apache
Keep-Alive: timeout=30, max=99
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>416 Requested Range Not Satisfiable</title>
</head><body>
<h1>Requested Range Not Satisfiable</h1>
<p>None of the range-specifier values in the Range
request-header field overlap the current extent
of the selected resource.</p>
</body></html>
Logs found:
06-05 10:23:06.577: I/NuCachedSource2(161): new range: offset= 4927056
06-05 10:23:07.618: I/NuCachedSource2(161): ERROR_END_OF_STREAM
06-05 10:23:07.618: I/NuCachedSource2(161): new range: offset= 3591808430
06-05 10:23:07.618: I/ChromiumHTTPDataSource(161): Reconnecting...
06-05 10:23:07.638: D/overlay(158): Unset pipe=RGB1 dpy=0;
06-05 10:23:07.788: I/ChromiumHTTPDataSourceSupport(161): We requested a content range, but server didn't support that. (responded with 416)
06-05 10:23:07.788: I/ChromiumHTTPDataSource(161): Reconnect failed w/ err 0xffffffe0
06-05 10:23:07.788: E/MediaPlayer(19641): error (1, -2147483648)
Do you have an idea of the problem ?
Thanks!
According to the doc, this error is thrown with the constant USE_DEFAULT_STREAM_TYPE.
Suggests using the default stream type. This may not be used in all
places a stream type is needed.
Constant Value: -2147483648 (0x80000000)
May this answer help. Maybe the mediaPlayer didn't have the right permissions to play this file so you could check this.
Finally you could check this thread which suggests that the video encoding isn't supported by Android.
Hope this helps =)

PHP + jQuery + MySQL async fetching data, script ends unexpected

I'm trying to use jQuery's $.ajax() to GET a PHP page, on a based interval, which prints the latest enteries from a MySQL table. If there are new enteries, they are printed on the page, if not, "NaN" gets printed.
This is my PHP function which checks for new enteries: http://pastebin.com/R6NHDU3t
This is the HTML + jQuery which checks for response from the above function(got it from some answer here on Stack): http://pastebin.com/myT7evAx
The idea is this: I access the HTML page, the jQuery inside is GET-ing the PHP page every 3 seconds, the PHP checkes for new enteries inside MySQL; if there are new enteries, they are printed on the PHP page and get fetched by jQuery, then appended into #messages, if not, "NaN" is printed(because of JSON, doesen't matter).
This works fine if no new enteries are available: every 3 seconds "NaN" gets printed on the page; some Live HTTP Headers:
http://localhost/handler.class.php?Q=comments&_=1348715334458
Host: localhost
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1
Accept: */*
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
X-Requested-With: XMLHttpRequest
Referer: http://localhost/live.html
HTTP/1.1 200 OK
Date: Thu, 27 Sep 2012 03:08:54 GMT
Server: Apache/2.2.22 (Win32) PHP/5.3.13
X-Powered-By: PHP/5.3.13
Content-Length: 15 <---- the "NaN", no new data
Keep-Alive: timeout=5, max=97
Connection: Keep-Alive
Content-Type: text/html
If new enteries are made, the script stopps appending "NaN"(doh, new data), and it hould append my new enteries, but the script stopps, no new data is appended and it stopps making requests. Here is the latest Live HTTP Headers log, showing the last request:
http://localhost/handler.class.php?Q=comments&_=1348715337473
GET /handler.class.php?Q=comments&_=1348715337473 HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1
Accept: */*
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
X-Requested-With: XMLHttpRequest
Referer: http://localhost/live.html
HTTP/1.1 200 OK
Date: Thu, 27 Sep 2012 03:08:57 GMT
Server: Apache/2.2.22 (Win32) PHP/5.3.13
X-Powered-By: PHP/5.3.13
Content-Length: 3257 <-- new data, so it got the response, but stopped here. no appending, no more requests.
Keep-Alive: timeout=5, max=96
Connection: Keep-Alive
Content-Type: text/html
Why does it stop when handler.class.php?Q=comments returns new data?

Categories