mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-09-15 18:45:23 -04:00
iOS: Add a CFNetwork http backend that barely works
This commit is contained in:
parent
708a4c8495
commit
279c36fcf0
@ -289,6 +289,7 @@
|
||||
<ClCompile Include="Deflate.c" />
|
||||
<ClCompile Include="Model.c" />
|
||||
<ClCompile Include="Menus.c" />
|
||||
<ClCompile Include="Platform_Android.c" />
|
||||
<ClCompile Include="Platform_Posix.c" />
|
||||
<ClCompile Include="Platform_Web.c" />
|
||||
<ClCompile Include="Platform_WinApi.c" />
|
||||
|
@ -584,5 +584,8 @@
|
||||
<ClCompile Include="Graphics_D3D11.c">
|
||||
<Filter>Source Files\Graphics</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Platform_Android.c">
|
||||
<Filter>Source Files\Platform</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
@ -188,14 +188,16 @@ Thus it is **NOT SAFE** to allocate a string on the stack. */
|
||||
#define CC_BUILD_GLMODERN
|
||||
#define CC_BUILD_IOS
|
||||
#define CC_BUILD_TOUCH
|
||||
#define CC_BUILD_CFNETWORK
|
||||
#elif defined __x86_64__ || defined __arm64__
|
||||
#define CC_BUILD_COCOA
|
||||
#define CC_BUILD_MACOS
|
||||
#define CC_BUILD_CURL
|
||||
#else
|
||||
#define CC_BUILD_CARBON
|
||||
#define CC_BUILD_MACOS
|
||||
#endif
|
||||
#define CC_BUILD_CURL
|
||||
#endif
|
||||
#define CC_BUILD_OPENAL
|
||||
#elif defined __sun__
|
||||
#define CC_BUILD_SOLARIS
|
||||
|
@ -745,6 +745,80 @@ static cc_result HttpBackend_Do(struct HttpRequest* req, cc_string* url) {
|
||||
http_curProgress = 100;
|
||||
return res;
|
||||
}
|
||||
#elif defined CC_BUILD_CFNETWORK
|
||||
#include "Errors.h"
|
||||
#include <stddef.h>
|
||||
#include <CFNetwork/CFNetwork.h>
|
||||
|
||||
|
||||
cc_bool Http_DescribeError(cc_result res, cc_string* dst) {
|
||||
return false;
|
||||
}
|
||||
|
||||
static void HttpBackend_Init(void) {
|
||||
|
||||
}
|
||||
|
||||
static void Http_AddHeader(struct HttpRequest* req, const char* key, const cc_string* value) {
|
||||
char tmp[NATIVE_STR_LEN];
|
||||
CFStringRef keyCF, valCF;
|
||||
CFHTTPMessageRef msg = (CFHTTPMessageRef)req->meta;
|
||||
Platform_EncodeUtf8(tmp, value);
|
||||
|
||||
keyCF = CFStringCreateWithCString(NULL, key, kCFStringEncodingUTF8);
|
||||
valCF = CFStringCreateWithCString(NULL, tmp, kCFStringEncodingUTF8);
|
||||
CFHTTPMessageSetHeaderFieldValue(msg, keyCF, valCF);
|
||||
CFRelease(keyCF);
|
||||
CFRelease(valCF);
|
||||
}
|
||||
|
||||
static cc_result HttpBackend_Do(struct HttpRequest* req, cc_string* url) {
|
||||
static CFStringRef verbs[] = { CFSTR("GET"), CFSTR("HEAD"), CFSTR("POST") };
|
||||
char tmp[NATIVE_STR_LEN];
|
||||
CFStringRef urlCF;
|
||||
CFURLRef urlRef;
|
||||
CFHTTPMessageRef msg;
|
||||
|
||||
Platform_EncodeUtf8(tmp, url);
|
||||
urlCF = CFStringCreateWithCString(NULL, tmp, kCFStringEncodingUTF8);
|
||||
urlRef = CFURLCreateWithString(NULL, urlCF, NULL);
|
||||
|
||||
msg = CFHTTPMessageCreateRequest(NULL, verbs[req->requestType], urlRef, kCFHTTPVersion1_1);
|
||||
req->meta = msg;
|
||||
Http_SetRequestHeaders(req);
|
||||
|
||||
if (req->data && req->size) {
|
||||
CFDataRef body = CFDataCreate(NULL, req->data, req->size);
|
||||
CFHTTPMessageSetBody(msg, body);
|
||||
CFRelease(body); /* TODO: ???? */
|
||||
|
||||
req->data = NULL;
|
||||
req->size = 0;
|
||||
Mem_Free(req->data);
|
||||
}
|
||||
|
||||
CFReadStreamRef stream = CFReadStreamCreateForHTTPRequest(NULL, msg);
|
||||
CFReadStreamSetProperty(stream, kCFStreamPropertyHTTPShouldAutoredirect, kCFBooleanTrue);
|
||||
//CFHTTPReadStreamSetRedirectsAutomatically(stream, TRUE);
|
||||
CFReadStreamOpen(stream);
|
||||
CFIndex read = 0;
|
||||
char buf[1024];
|
||||
|
||||
for (;;) {
|
||||
CFIndex read = CFReadStreamRead(stream, buf, sizeof(buf));
|
||||
if (read <= 0) break;
|
||||
|
||||
if (!req->_capacity) Http_BufferInit(req);
|
||||
Http_BufferEnsure(req, read);
|
||||
|
||||
Mem_Copy(&req->data[req->size], buf, read);
|
||||
Http_BufferExpanded(req, read);
|
||||
}
|
||||
|
||||
// error handling? what's that anyways?
|
||||
req->statusCode = 200;
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
static void ClearCurrentRequest(void) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user