void* task_push_http_transfer(const char *url, bool mute, const char *type, retro_task_callback_t cb, void *user_data) { struct http_connection_t *conn; conn = net_http_connection_new(url, "GET", NULL); return task_push_http_transfer_generic(conn, url, mute, type, cb, user_data); }
/** * rarch_main_data_http_iterate_poll: * * Polls HTTP message queue to see if any new URLs * are pending. * * If handle is freed, will set up a new http handle. * The transfer will be started on the next frame. * * Returns: 0 when an URL has been pulled and we will * begin transferring on the next frame. Returns -1 if * no HTTP URL has been pulled. Do nothing in that case. **/ static int rarch_main_data_http_iterate_poll(http_handle_t *http) { char elem0[PATH_MAX_LENGTH]; struct string_list *str_list = NULL; const char *url = msg_queue_pull(http->msg_queue); if (!url) return -1; /* Can only deal with one HTTP transfer at a time for now */ if (http->handle) return -1; str_list = string_split(url, "|"); if (!str_list || (str_list->size < 1)) goto error; strlcpy(elem0, str_list->elems[0].data, sizeof(elem0)); http->connection.handle = net_http_connection_new(elem0); if (!http->connection.handle) return -1; http->connection.cb = &cb_http_conn_default; if (str_list->size > 1) { strlcpy(http->connection.elem1, str_list->elems[1].data, sizeof(http->connection.elem1)); } string_list_free(str_list); return 0; error: if (str_list) string_list_free(str_list); return -1; }
bool rarch_task_push_http_transfer(const char *url, const char *type, rarch_task_callback_t cb, void *user_data) { char tmp[PATH_MAX_LENGTH]; task_finder_data_t find_data; struct http_connection_t *conn = NULL; rarch_task_t *t = NULL; http_handle_t *http = NULL; if (string_is_empty(url)) return false; find_data.func = rarch_task_http_finder; find_data.userdata = (void*)url; /* Concurrent download of the same file is not allowed */ if (task_ctl(TASK_CTL_FIND, &find_data)) { RARCH_LOG("[http] '%s'' is already being downloaded.\n", url); return false; } conn = net_http_connection_new(url); if (!conn) return false; http = (http_handle_t*)calloc(1, sizeof(*http)); if (!http) goto error; http->connection.handle = conn; http->connection.cb = &cb_http_conn_default; if (type) strlcpy(http->connection.elem1, type, sizeof(http->connection.elem1)); http->status = HTTP_STATUS_CONNECTION_TRANSFER; t = (rarch_task_t*)calloc(1, sizeof(*t)); if (!t) goto error; t->handler = rarch_task_http_transfer_handler; t->state = http; t->callback = cb; t->user_data = user_data; t->progress = -1; snprintf(tmp, sizeof(tmp), "%s '%s'", msg_hash_to_str(MSG_DOWNLOADING), path_basename(url)); t->title = strdup(tmp); task_ctl(TASK_CTL_PUSH, t); return true; error: if (conn) net_http_connection_free(conn); if (t) free(t); if (http) free(http); return false; }
int net_http_get(const char **result, size_t *size, const char *url, retro_time_t *timeout) { size_t length; uint8_t* data = NULL; char* res = NULL; int ret = NET_HTTP_GET_OK; struct http_t* http = NULL; retro_time_t t0 = cpu_features_get_time_usec(); struct http_connection_t *conn = net_http_connection_new(url); *result = NULL; /* Error creating the connection descriptor. */ if (!conn) goto error; /* Don't bother with timeouts here, it's just a string scan. */ while (!net_http_connection_iterate(conn)) {} /* Error finishing the connection descriptor. */ if (!net_http_connection_done(conn)) { ret = NET_HTTP_GET_MALFORMED_URL; goto error; } http = net_http_new(conn); /* Error connecting to the endpoint. */ if (!http) { ret = NET_HTTP_GET_CONNECT_ERROR; goto error; } while (!net_http_update(http, NULL, NULL)) { /* Timeout error. */ if (timeout && (cpu_features_get_time_usec() - t0) > *timeout) { ret = NET_HTTP_GET_TIMEOUT; goto error; } } data = net_http_data(http, &length, false); if (data) { res = (char*)malloc(length + 1); /* Allocation error. */ if ( !res ) goto error; memcpy((void*)res, (void*)data, length); free(data); res[length] = 0; *result = res; } else { length = 0; *result = NULL; } if (size) *size = length; error: if ( http ) net_http_delete( http ); if ( conn ) net_http_connection_free( conn ); if (timeout) { t0 = cpu_features_get_time_usec() - t0; if (t0 < *timeout) *timeout -= t0; else *timeout = 0; } return ret; }