Sending HTTP chunked over iOS

Hello
I’m sending data to a server using WWW class in chunks. Chunked transfer encoding - Wikipedia
This works fine in Unity Standalone Windows/OSX but in iOS it the data just wont go through, meaning i send the WWW and i get no response from the server (tested with several web servers) .
What i do is to add necessary headers for using chunked encoding (Transfer-Encoding: chunked)

    Dictionary<string, string> customHeaders = new Dictionary<string, string>();
    customHeaders.Add("Transfer-Encoding", "chunked");
    WWW www = new WWW(Url, chunk.m_data, customHeaders);

As i said it works on standalone, haven’s tested on Android or WindowsStore/Phone.

Thanks
Gilberto

By sniffing the packets in Windows Standalone Vs iOS.
Windows

POST /post HTTP/1.1
User-Agent: UnityPlayer/4.6.1f1 (http://unity3d.com)
Host: httpbin.org
Accept: */*
Accept-Encoding: identity
Content-Length: 1143
Content-Type: application/x-www-form-urlencoded
Transfer-Encoding: chunked
X-Unity-Version: 4.6.1f1
Expect: 100-continue

iOS

POST /post HTTP/1.1
Host: httpbin.org:80
User-Agent: testApp/1.0 CFNetwork/672.1.14 Darwin/14.0.0
Transfer-Encoding: chunked
Content-Type: application/x-www-form-urlencoded
Accept: */*
Content-Length: 1143
Accept-Language: en-gb
Accept-Encoding: gzip, deflate
Connection: keep-alive
X-Unity-Version: 4.6.1f1

Unity is setting “Content-Length” automatically even when i use chunked transfer where that should not exist. It does that on both Windows and iOS although it works on Windows but not in iOS.

I’m running out of options here so i will try a plugin to handle http.

Thanks
Gilberto

in trampoline there is WWWConnection.mm which handles actual connection (creating requests etc).
https://bitbucket.org/Unity-Technologies/iosnativecodesamples/src/2c85a37958dc008c861ba6d49b26d9c8e8fa790c/NativeIntegration/WWW/CustomConnection/?at=4.5-stable
for a simple sample

1 Like

Thank you very much.
I will look into it and tweak it for my needs. :slight_smile:

For someone chasing this same problem here is the solution.
Basically Unity is setting the “Content-Length” no matter what and that needed to be removed is case of a chunked transfer.

#include "Unity/WWWConnection.h"

@interface UnityWWWCustomConnectionDelegate : UnityWWWConnectionDelegate
{
}
@end

@implementation UnityWWWCustomConnectionDelegate
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response
{
  if(request.HTTPBody && [@"chunked" isEqual: [request valueForHTTPHeaderField:@"Transfer-Encoding"]])
  {
   
  NSMutableURLRequest* requestCopy =    [UnityWWWConnectionDelegate newRequestForHTTPMethod:request.HTTPMethod url:request.URL headers:nil];
  NSMutableDictionary* headers = [request.allHTTPHeaderFields mutableCopy];
  // Chunked transfer shouldn't have "Content-Length" set
  [headers removeObjectForKey:@"Content-Length"];
  [requestCopy setAllHTTPHeaderFields:headers];
  requestCopy.HTTPBodyStream = [NSInputStream inputStreamWithData:request.HTTPBody];
  request = requestCopy;
  }
  return request;
}
@end

IMPL_WWW_DELEGATE_SUBCLASS(UnityWWWCustomConnectionDelegate);