2025-02-26 08:47:09 +01:00
|
|
|
#include <iron_net.h>
|
2024-11-10 22:11:29 +01:00
|
|
|
|
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
|
|
2025-03-09 17:02:12 +01:00
|
|
|
void iron_http_request(const char *url, const char *path, const char *data, int port, bool secure, int method, const char *header,
|
|
|
|
|
iron_http_callback_t callback, void *callbackdata) {
|
2024-11-10 22:11:29 +01:00
|
|
|
NSString *urlstring = secure ? @"https://" : @"http://";
|
|
|
|
|
urlstring = [urlstring stringByAppendingString:[NSString stringWithUTF8String:url]];
|
|
|
|
|
urlstring = [urlstring stringByAppendingString:@":"];
|
|
|
|
|
urlstring = [urlstring stringByAppendingString:[[NSNumber numberWithInt:port] stringValue]];
|
|
|
|
|
urlstring = [urlstring stringByAppendingString:@"/"];
|
|
|
|
|
urlstring = [urlstring stringByAppendingString:[NSString stringWithUTF8String:path]];
|
|
|
|
|
|
|
|
|
|
NSURL *aUrl = [NSURL URLWithString:urlstring];
|
|
|
|
|
|
|
|
|
|
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
|
|
|
|
|
sessionConfiguration.HTTPAdditionalHeaders = @{@"Content-Type" : @"application/json"};
|
|
|
|
|
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration];
|
|
|
|
|
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl];
|
|
|
|
|
if (data != 0) {
|
|
|
|
|
// printf("Sending %s\n\n", data);
|
|
|
|
|
NSString *datastring = [NSString stringWithUTF8String:data];
|
|
|
|
|
request.HTTPBody = [datastring dataUsingEncoding:NSUTF8StringEncoding];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (method) {
|
2025-03-09 17:02:12 +01:00
|
|
|
case IRON_HTTP_GET:
|
2024-11-10 22:11:29 +01:00
|
|
|
request.HTTPMethod = @"GET";
|
|
|
|
|
break;
|
2025-03-09 17:02:12 +01:00
|
|
|
case IRON_HTTP_POST:
|
2024-11-10 22:11:29 +01:00
|
|
|
request.HTTPMethod = @"POST";
|
|
|
|
|
break;
|
2025-03-09 17:02:12 +01:00
|
|
|
case IRON_HTTP_PUT:
|
2024-11-10 22:11:29 +01:00
|
|
|
request.HTTPMethod = @"PUT";
|
|
|
|
|
break;
|
2025-03-09 17:02:12 +01:00
|
|
|
case IRON_HTTP_DELETE:
|
2024-11-10 22:11:29 +01:00
|
|
|
request.HTTPMethod = @"DELETE";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
|
|
|
|
|
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
|
|
|
|
|
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
|
|
|
|
|
int statusCode = (int)[httpResponse statusCode];
|
|
|
|
|
|
|
|
|
|
NSMutableData *responseData = [[NSMutableData alloc] init];
|
|
|
|
|
[responseData appendData:data];
|
|
|
|
|
[responseData appendBytes:"\0" length:1];
|
|
|
|
|
|
|
|
|
|
callback(error == nil ? 0 : 1, statusCode, (const char *)[responseData bytes], callbackdata);
|
|
|
|
|
}];
|
|
|
|
|
[dataTask resume];
|
|
|
|
|
}
|