Yesterday , I asked a question about socket post file data to php page.
Link is here
Upload and POST file to PHP page
When I follow the way , I changed my code.
char *str="POST /path/upload_file.php HTTP/1.0 \n Host: 00.00.00.00 \n Content-Disposition: name=2.jpg;filename=2.jpg\r " ;
write(sockfd, buf, filestat.st_size);
sprintf(send1,"%s%s\r\n",str,buf);
retval= send(sockfd,send1,sizeof(send1),0);
When I execute the Program , can get
result:
501 Method Not Implemented
Method Not Implemented
to /index.html not supported.
Invalid method in request
Apache/1.3.39 Server at localhost Port 80
I guess it's possible :
1. apache can't support something?
(my apache don't support ssl)
2. http protocol is not details?
3. other?
Thanks a lot.
There are multiple issues with your code.
First, the Host header is part of the HTTP 1.1 specification, and does not apply to HTTP 1.0.
Second, the delimiter for separating headers as well as body is \r\n, not \n.
Third, you are using the Content-Disposition in a request, the way it should be used in a response.
Lastly, you need to send the request as multipart/form-data. The answer you've linked to had it right. The request has to respect that format.
I've written a detailed example not so long ago (although in PHP, but the request format stays the same). You can find it here.
Related
I'm not a PHP expert at all so please forgive me if I make incorrect or stupid statements! I recently had my web server uprgaded to PHP 7.4.11 from PHP 5. In addition to running Joomla that server also contains a PHP script that receives a file via POST and moves it to a specified folder. This is sent form a .NET client that crucially does not put the Content-Length header into the post with the content type of ' multipart/form-data; boundary=---------------------8d8cf4b4721703d\n'.
The file is notified as being correctly received by this PHP code:
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile))
On PHP5 this was working just fine, on PHP7 it is not. When testing with Postman, we determined that if the Content-Length header is present the $_FILES['file'] contains the file but if Content-Length is not present it will not contain the file. The call typically comes as a post from a .NET application that we have determined does not send the Content-Length header
How can I get PHP7 to replicate the behaviour of PHP5; note that I cannot change the sender as it is a .NET rich client application installed and I can't get to the client to change it.
Thanks for your help.
Cheers,
Neil
I'm building a REST API.. Sometimes the server returns the response with chunked transfer encoding? Why is that?!
Why can't the server always return the response in the same encoding?
The problem is that I don't know how to read the data when its returned as chunked!?
update
neeed moore downvotes... to breeeath...
Assuming your server is using Apache, this is expected behaviour. You can disable it by putting this line in your .htaccess file:
SetEnv downgrade-1.0
However, you should consider modifying your reading code to just support different content encodings. What library are you using to make the HTTP request? Any reasonable HTTP library can handle chunked requests. If your requesting code is written in PHP, use curl. http://php.net/manual/en/book.curl.php
Taken from Server Fault:
specify the "Content-Length' header, so server knows, what's the size of the response
use HTTP 1.0 at the requester's side
A problem may be that Apache is gzipping your download, taking care of correcting the Content-Length, or in your case, adding the header
Content-Encoding: chunked
You can add a .htaccess RewriteRule to disable gzip:
RewriteRule . - [E=no-gzip:1]
The URL in question : http://www.roblox.com/asset/?id=149996624
When accessed in a browser, it will correctly download a file (which is an XML document). I wanted to get the file in php, and simply display its contents on a page.
$contents = file_get_contents("http://www.roblox.com/asset/?id=149996624");
The above is what I've tried using (as far as I know, the page does not expect any headers). I get a 500 HTTP error. However, in Python, the following code works and I receive the file.
r = requests.get("http://www.roblox.com/asset/?id=147781188")
I'm confused as to what the distinction is between how these two requests are sent. I am almost 100% it is not a header problem. I've also tried the cURL library in PHP to no avail. Nothing I've tried in PHP seems to succeed with the URL (with any valid id parameter); but Python is able to bring success nonchalantly.
Any insight as to why this issue may be happening would be great.
EDIT : I have already tried copying Python's headers into my PHP request.
EDIT2 : It also appears that there are two requests happening upon navigating to the link.
Is this on a linux/mac host by chance? If so you could use ngrep to see the differences on the request themselves on the wire. Something like the following should work
ngrep -t '^(GET) ' 'src host 127.0.0.1 and tcp and dst port 80'
EDIT - The problem is that your server is responding with a 302 and the PHP library is not following it automatically. Cheers!
I am trying to migrate a PHP application that uses Twilio to Google Apps and have run into a bit of a snag. As a simple test, I sent a single text message to my cell phone from within the Google App that I created. It sends fine but I receive the message twice; to confirm it was actually executing twice I sent the epoch time - they're about 1 second apart.
I checked the logs and saw this - "This request caused a new process to be started for your application, and thus caused your application code to be loaded for the first time. This request may thus take longer and use more CPU than a typical request for your application." I tried removing the Twilio usage entirely and replaced it with a simple "Hello World" echo, same message appeared in the log for that request.
How can I avoid this sort of behavior?
UPDATE
Here are the headers from my Requestb.in test using the following code. The bin was hit twice from the same IP address - I only went to the App's page one time.
<?php
$result = file_get_contents('http://requestb.in/BINID');
echo $result;
Headers -
First Request:
User-Agent: AppEngine-Google; (+http://code.google.com/appengine; appid: s~MYAPP)
Connection: close
Accept-Encoding: gzip
X-Request-Id: e7583bda-dfeb-4431-92a5-aa4af0bf06e8
Host: requestb.in
Second request:
User-Agent: AppEngine-Google; (+http://code.google.com/appengine; appid: s~MYAPP)
X-Request-Id: e766375b-bea8-4b79-a869-e2603309bec7
Accept-Encoding: gzip
Host: requestb.in
Connection: close
SECOND UPDATE
I added the epoch time as a GET variable to the requestb.in address, the bin was hit twice with the exact same epoch, two different IP addresses, one second apart. So this tells me that the code was executed one time but somehow accessed the bin twice from two IP addresses. Sometimes it seems to only be one IP address. Really puzzled here.. I even tried from scratch with a new app, same result.
I think you will find this message ""This request caused a new process to be started for your application, " is unrelated.
Unless you use warmup requests, you will always see this message if an instance is started to serve user facing request.
I would look at your code and see how the message sending code could be executed twice.
Try doing some logging around the sending code and see if you get that log message in the same request twice.
I'm building a REST API.. Sometimes the server returns the response with chunked transfer encoding? Why is that?!
Why can't the server always return the response in the same encoding?
The problem is that I don't know how to read the data when its returned as chunked!?
update
neeed moore downvotes... to breeeath...
Assuming your server is using Apache, this is expected behaviour. You can disable it by putting this line in your .htaccess file:
SetEnv downgrade-1.0
However, you should consider modifying your reading code to just support different content encodings. What library are you using to make the HTTP request? Any reasonable HTTP library can handle chunked requests. If your requesting code is written in PHP, use curl. http://php.net/manual/en/book.curl.php
Taken from Server Fault:
specify the "Content-Length' header, so server knows, what's the size of the response
use HTTP 1.0 at the requester's side
A problem may be that Apache is gzipping your download, taking care of correcting the Content-Length, or in your case, adding the header
Content-Encoding: chunked
You can add a .htaccess RewriteRule to disable gzip:
RewriteRule . - [E=no-gzip:1]