Add iron_http_request for linux
This commit is contained in:
+2
-1
@@ -72,11 +72,13 @@ if (platform == "windows") {
|
||||
}
|
||||
else if (platform == "linux") {
|
||||
add_sys_backend("linux");
|
||||
add_net_backend("posix");
|
||||
add_thread_backend("posix");
|
||||
add_gpu_backend("vulkan");
|
||||
project.add_define("IRON_VULKAN");
|
||||
project.add_define("_POSIX_C_SOURCE=200809L");
|
||||
project.add_lib("dl");
|
||||
project.add_lib("ssl");
|
||||
project.add_lib("vulkan");
|
||||
if (flags.with_audio) {
|
||||
project.add_lib("asound");
|
||||
@@ -106,7 +108,6 @@ else if (platform == "ios") {
|
||||
}
|
||||
else if (platform == "android") {
|
||||
add_sys_backend("android");
|
||||
add_net_backend("posix");
|
||||
add_thread_backend("posix");
|
||||
add_gpu_backend("vulkan");
|
||||
project.add_cfiles("sources/backends/android_file_dialog.c");
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
#include <iron_net.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <netdb.h>
|
||||
#include <sys/socket.h>
|
||||
#include <openssl/ssl.h>
|
||||
|
||||
static SSL_CTX *ctx = NULL;
|
||||
static char *buf = NULL;
|
||||
static int buf_len = 1024 * 1024;
|
||||
|
||||
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) {
|
||||
|
||||
int sock_fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
struct hostent *server = gethostbyname(url);
|
||||
struct sockaddr_in server_addr = {0};
|
||||
server_addr.sin_family = AF_INET;
|
||||
memmove(&server_addr.sin_addr.s_addr, server->h_addr_list[0], server->h_length);
|
||||
server_addr.sin_port = htons(port);
|
||||
|
||||
if (connect(sock_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
|
||||
close(sock_fd);
|
||||
callback(1, 0, NULL, callbackdata);
|
||||
return;
|
||||
}
|
||||
|
||||
if (ctx == NULL) {
|
||||
ctx = SSL_CTX_new(TLS_client_method());
|
||||
}
|
||||
|
||||
SSL *ssl = SSL_new(ctx);
|
||||
SSL_set_fd(ssl, sock_fd);
|
||||
|
||||
if (SSL_connect(ssl) <= 0) {
|
||||
SSL_free(ssl);
|
||||
close(sock_fd);
|
||||
callback(1, 0, NULL, callbackdata);
|
||||
return;
|
||||
}
|
||||
|
||||
// For HTTP/1.1, implement "transfer-encoding: chunked"
|
||||
char request[1024];
|
||||
int request_len = snprintf(request, sizeof(request), "GET /%s HTTP/1.0\r\nHost: %s:%d\r\nConnection: close\r\n\r\n", path, url, port);
|
||||
SSL_write(ssl, request, request_len);
|
||||
|
||||
// Read
|
||||
if (buf == NULL) {
|
||||
buf = malloc(buf_len);
|
||||
}
|
||||
int pos = 0;
|
||||
int n;
|
||||
while ((n = SSL_read(ssl, buf + pos, buf_len - pos - 1)) > 0) {
|
||||
pos += n;
|
||||
if (pos == buf_len - 1) {
|
||||
buf_len *= 2;
|
||||
buf = realloc(buf, buf_len);
|
||||
}
|
||||
}
|
||||
|
||||
// Parse
|
||||
int status_code = 0;
|
||||
const char *body_ptr = "";
|
||||
char *headers_end = strstr(buf, "\r\n\r\n");
|
||||
body_ptr = headers_end + 4;
|
||||
callback(0, status_code, body_ptr, callbackdata);
|
||||
|
||||
SSL_free(ssl);
|
||||
close(sock_fd);
|
||||
}
|
||||
@@ -1415,8 +1415,6 @@ void _iron_http_request(string_t *url, i32 size, void (*callback)(char *, buffer
|
||||
url_path[j] = 0;
|
||||
#ifdef IRON_ANDROID
|
||||
iron_http_request(curl, url_path, NULL, 443, true, 0, NULL, &_http_callback, cbd);
|
||||
#elif defined(IRON_LINUX)
|
||||
// TODO
|
||||
#else
|
||||
iron_http_request(url_base, url_path, NULL, 443, true, 0, NULL, &_http_callback, cbd);
|
||||
#endif
|
||||
|
||||
@@ -95,7 +95,6 @@ function file_start(path: string) {
|
||||
}
|
||||
|
||||
function file_download(url: string, dst_path: string, done: (url: string)=>void, size: i32 = 0) {
|
||||
///if (arm_windows || arm_macos || arm_ios || arm_android)
|
||||
let fdd: file_download_data_t = { dst_path: dst_path, done: done };
|
||||
map_set(_file_download_map, url, fdd);
|
||||
_iron_http_request(url, size, function (url: string, ab: buffer_t) {
|
||||
@@ -105,11 +104,6 @@ function file_download(url: string, dst_path: string, done: (url: string)=>void,
|
||||
}
|
||||
fdd.done(url);
|
||||
});
|
||||
///else
|
||||
iron_sys_command("wget -O \"" + dst_path + "\" \"" + url + "\"");
|
||||
// iron_sys_command("curl -L " + url + " -o \"" + dst_path + "\"");
|
||||
done(url);
|
||||
///end
|
||||
}
|
||||
|
||||
function file_download_bytes(url: string, done: (url: string, ab: buffer_t)=>void) {
|
||||
@@ -199,11 +193,7 @@ function file_init_cloud_bytes(done: ()=>void, append: string = "") {
|
||||
if (buffer == null) {
|
||||
let empty: string[] = [];
|
||||
map_set(file_cloud, "cloud", empty);
|
||||
///if arm_linux
|
||||
console_error(strings_ensure_wget_is_installed());
|
||||
///else
|
||||
console_error(strings_check_internet_connection());
|
||||
///end
|
||||
return;
|
||||
}
|
||||
let files: string[] = [];
|
||||
|
||||
@@ -19,10 +19,6 @@ function strings_check_internet_connection(): string {
|
||||
return tr("Error: Check internet connection to access the cloud");
|
||||
}
|
||||
|
||||
function strings_ensure_wget_is_installed(): string {
|
||||
return tr("Error: Ensure wget is installed");
|
||||
}
|
||||
|
||||
function strings_asset_already_imported(): string {
|
||||
return tr("Info: Asset already imported");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user