HTTP response content parsing [duplicate] - php

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]

Related

PHP's HTTP_IF_NONE_MATCH is always empty

PHP's $_SERVER['HTTP_IF_NONE_MATCH'] is always empty however Firefox's Web Developer Network Requests tab shows the request header as being If-Modified-Since: "Tue, 27 Jun 2017 09:08:23 GMT". Cache is not disabled and my .htaccess file contains the following:
RewriteEngine on
RewriteRule .* - [E=HTTP_IF_NONE_MATCH:%{HTTP:If-None-Match}]
How do I get PHP's $_SERVER['HTTP_IF_NONE_MATCH'] to return the request header properly or how do I access that request header in an alternative manner? I came across getenv('HTTP_IF_MODIFIED_SINCE') though that didn't return anything either. Is it possible there is something in the php.ini file that needs to be changed? It is a fairly fresh server setup.
You should add a If-None-Match http header to your server. If you do so the HTTP_IF_NONE_MATCH server variable should contain the value of the set If-None-Match header.
curl http://my/endpoint --header 'If-None-Match: "my-custom-etag-value"'

how to use gzip decompression with AFNetworking API call?

I am using AFNetworking library in my projects. recently I heard about gzip data compression which is given by defalut in NSURLConnection Class and reducing the time and loading time of large json response, hence AFNetworking might have that feature as it is working on top of NSURLConnection.
but I do not know how to get gzip compressed json response from php API through AFNetworking.
I need this technique when Json response file size is more that 100kb+.
If server supports gzip it could require client to ask server to respond with gzip enabled. To ask server to use gzip you add specific "Accept-Encoding" header to your requests. You can do this, for example, with this lines of code:
// Get one that serializes your requests,
// for example from your AFHTTPSessionManager subclass
AFHTTPRequestSerializer <AFURLRequestSerialization> *requestSerializer = ...
[requestSerializer setValue:#"gzip, identity" forHTTPHeaderField:#"Accept-Encoding"];
"Accept-Encoding" header must contain gzip while, probably, identity is not required.
If you are using NSURLSession based API in AFNetworking, it would be included automatically as said here. Thus you don't need to do anything.
Please be noted that you may not see the header value in the app log for request, but you may check the header in response to confirm this behaviour if the server supports gzip.

PHP MAMP and server conflicts

I have a page that calls a php script. On MAMP everything works fine but when I upload it to a server I get the following error:
Call Request failed! Status code: 4000
Reason - Caught an HttpRequestValidationException due to some bad characters in the request. Make sure your post request is encoded as xml, preferable as UTF-8 ('Content-Type: text/xml; charset=utf-8'). Exception: A potentially dangerous Request.Form value was detected from the client (<?xml version="..."utf-8"?> <uclassify xmlns="ht...").
Has anyone seen anything like that?
you can check it yourself here just place a word like php or ios
It looks like your server is validating based on the content-type header. It seems to want text/xml, whereas you are sending application/x-www-form-urlencoded (which is the default for $.ajax).
Try explicitly setting the content type to text/xml in your $.ajax call. (reference)
try changing charset=utf-8 to charset=UTF-8

server sometimes returns chunked transfer encoding

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]

socket program post file to php

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.

Categories