/* *Delete location feed from cosm. Return EINA_TRUE if success. */ Eina_Bool cosm_location_feed_delete(Location *location) { Ecore_Con_Url *cosm_url = NULL; char *s; //Don't delete if no cosm feedid cosm or null location if(!location || (location_cosm_feedid_get(location) == 0) || !edams_settings_cosm_apikey_get()) return EINA_FALSE; int feedid = location_cosm_feedid_get(location); debug(MSG_COSM, _("Deleting Cosm feed '%s'..."), location_name_get(location)); ecore_event_handler_add(ECORE_CON_EVENT_URL_COMPLETE, _url_feed_delete_complete_cb, (void*)feedid); asprintf(&s, "http://api.cosm.com/v2/feeds/%d", location_cosm_feedid_get(location)); cosm_url = ecore_con_url_custom_new(s, "DELETE"); if (!cosm_url) { debug(MSG_ERROR, _("Can't create Ecore_Con_Url object")); return EINA_FALSE; } //ecore_con_url_verbose_set(cosm_url, edams_settings_debug_get()); ecore_con_url_additional_header_add(cosm_url, "X-ApiKey", edams_settings_cosm_apikey_get()); if(!ecore_con_url_post(cosm_url, (void*)s, strlen(s), NULL)) { debug(MSG_ERROR, _("Can't realize url PUT request")); return EINA_FALSE; } FREE(s); return EINA_TRUE; }/*cosm_location_feed_delete*/
/* *Add location feed to cosm. Return EINA_TRUE if success. */ Eina_Bool cosm_location_feed_add(Location *location) { Ecore_Con_Url *cosm_url = NULL; char *s; //Don't add cosm url feed if already there... if(!location || (location_cosm_feedid_get(location) != 0) || !edams_settings_cosm_apikey_get()) return EINA_FALSE; debug(MSG_COSM, _("Creating cosm feed '%s'..."), location_name_get(location)); ecore_event_handler_add(ECORE_CON_EVENT_URL_COMPLETE, (Ecore_Event_Handler_Cb)_url_feed_add_complete_cb,NULL); cosm_url = ecore_con_url_custom_new("http://api.cosm.com/v2/feeds", "POST"); if (!cosm_url) { debug(MSG_COSM, _("Can't create Ecore_Con_Url object")); return EINA_FALSE; } //ecore_con_url_verbose_set(cosm_url, edams_settings_debug_get()); ecore_con_url_additional_header_add(cosm_url, "X-ApiKey", edams_settings_cosm_apikey_get());; ecore_con_url_data_set(cosm_url, (void*)location); char *locale = setlocale(LC_NUMERIC, NULL); setlocale(LC_NUMERIC, "POSIX"); cJSON *root,*fmt; root=cJSON_CreateObject(); asprintf(&s, "%s sensor.basic", location_name_get(location)); cJSON_AddItemToObject(root, "title", cJSON_CreateString(s)); FREE(s); cJSON_AddItemToObject(root, "version", cJSON_CreateString("1.0.0")); cJSON_AddItemToObject(root, "location", fmt=cJSON_CreateObject()); cJSON_AddStringToObject(fmt, "name", location_name_get(location)); //cJSON_AddStringToObject(fmt, "description", location_description_get(location)); cJSON_AddStringToObject(fmt, "disposition", "fixed"); cJSON_AddStringToObject(fmt, "exposure", "indoor"); cJSON_AddStringToObject(fmt, "domain", "physical"); if(location_latitude_get(location) != -1) cJSON_AddNumberToObject(fmt, "lat", location_latitude_get(location)); if(location_longitude_get(location) != -1) cJSON_AddNumberToObject(fmt, "lon", location_longitude_get(location)); s = cJSON_PrintUnformatted(root); cJSON_Delete(root); setlocale(LC_NUMERIC, locale); if(!ecore_con_url_post(cosm_url, (void*)s, strlen(s), NULL)) { debug(MSG_COSM, _("Can't realize url PUT request")); return EINA_FALSE; } FREE(s); return EINA_TRUE; }/*cosm_location_feed_add*/
/* *Update datastream from cosm with device's data. Return EINA_TRUE if success. */ Eina_Bool cosm_device_datastream_update(Location *location, Widget *widget) { Ecore_Con_Url *cosm_url = NULL; char *s; /*Don't add cosm device datastream if no feed available*/ if(!location || (location_cosm_feedid_get(location) == 0) || !edams_settings_cosm_apikey_get()) return EINA_FALSE; ecore_event_handler_add(ECORE_CON_EVENT_URL_COMPLETE, _url_datastream_update_complete_cb, NULL); asprintf(&s, "http://api.cosm.com/v2/feeds/%d", location_cosm_feedid_get(location)); cosm_url = ecore_con_url_custom_new(s, "PUT"); if (!cosm_url) { debug(MSG_COSM, _("Can't create Ecore_Con_Url object")); return EINA_FALSE; } FREE(s); //ecore_con_url_verbose_set(cosm_url, edams_settings_debug_get()); ecore_con_url_additional_header_add(cosm_url, "X-ApiKey", edams_settings_cosm_apikey_get()); cJSON *root, *datastreams, *fmt,*unit; root=cJSON_CreateObject(); cJSON_AddStringToObject(root,"version", "1.0.0"); cJSON_AddItemToObject(root, "datastreams", datastreams=cJSON_CreateArray()); fmt=cJSON_CreateObject(); cJSON_AddStringToObject(fmt,"current_value", widget_xpl_current_get(widget)); cJSON_AddStringToObject(fmt,"id", widget_xpl_device_get(widget)); if(xpl_type_to_unit_symbol(widget_xpl_type_get(widget))) { cJSON_AddItemToObject(fmt, "unit", unit=cJSON_CreateObject()); cJSON_AddStringToObject(unit,"label", xpl_type_to_units(widget_xpl_type_get(widget))); cJSON_AddStringToObject(unit,"symbol", xpl_type_to_unit_symbol(widget_xpl_type_get(widget))); } cJSON_AddItemToArray(datastreams, fmt); s = cJSON_PrintUnformatted(root); cJSON_Delete(root); if(!ecore_con_url_post(cosm_url, (void*)s, strlen(s), "text/json")) { debug(MSG_COSM, _("Can't realize url PUT request")); return EINA_FALSE; } FREE(s); return EINA_TRUE; }/*cosm_device_datastream_update*/
void sourcedrop_share(Share_Data *sd) { Ecore_Con_Url *request_url = NULL; char post_data[BUFFER_SIZE_MAX]; const char *json_encoded = NULL; char *url_encoded = NULL; EINA_SAFETY_ON_NULL_RETURN(sd); json_encoded = json_encode(sd->name, sd->content); request_url = ecore_con_url_new(SOURCEDROP_URL); url_encoded = url_encode(json_encoded); snprintf(post_data, (sizeof(post_data) - 1), "data=%s", url_encoded); free(url_encoded); ecore_con_url_data_set(request_url, (void*)sd); ecore_con_url_post(request_url, (void*)post_data, sizeof(post_data), "application/x-www-form-urlencoded"); }
END_TEST #endif #ifdef ECORE_CON_HTTP_TEST_URL START_TEST(ecore_con_test_ecore_con_url_post) { Ecore_Con_Url *ec_url; url_test *info; int ret; char link[] = ECORE_CON_HTTP_TEST_URL; char url_data[] = "test"; char *username = NULL, *password = NULL; char url[4096]; ret = eina_init(); fail_if(ret != 1); ret = ecore_con_url_init(); fail_if(ret != 1); fail_unless(_parse_url(link, url, &username, &password, NULL, NULL)); fprintf (stderr, "HTTP: \n url = %s \n username = %s \n password = %s \n", url, username, password); ecore_con_url_pipeline_set(EINA_TRUE); fail_unless (ecore_con_url_pipeline_get()); ec_url = ecore_con_url_custom_new(url, "POST"); fail_unless (ec_url); ecore_con_url_additional_header_add(ec_url, "User-Agent", "blablabla"); ecore_con_url_verbose_set(ec_url, EINA_TRUE); ecore_con_url_httpauth_set(ec_url, username, password, EINA_FALSE); ecore_con_url_time(ec_url, ECORE_CON_URL_TIME_IFMODSINCE, 0); fail_unless(ecore_con_url_post(ec_url, url_data, 4, NULL)); ecore_event_handler_add(ECORE_CON_EVENT_URL_COMPLETE, _url_compl_cb, info); ret = ecore_con_url_shutdown(); fail_if(ret != 0); ret = eina_shutdown(); }
bool FileDownloader::Start() { Utils::logger("downloader") << Priority::DEBUG << "FileDownloader: Start download (" << url << ")" << log4cpp::eol; if (url_con) { Utils::logger("downloader") << Priority::WARN << "A download is already in progress..." << log4cpp::eol; return false; } url_con = ecore_con_url_new(url.c_str()); if (!url_con) { string err = "Failed to create Ecore_Con_Url"; IPC::Instance().SendEvent("downloader::" + Utils::to_string(this), "failed", IPCData(new string(err), new DeletorT<string *>()), true); cb_signal.emit("failed", &err); cb_signal_user.emit("failed", &err, user_data); Utils::logger("downloader") << Priority::ERROR << "Download failed: " << err << log4cpp::eol; return false; } if (dest.empty()) { //Create a temporary file for download int cpt = 0; //Get a temporary filename do { tmpFile = "/tmp/calaos" + Utils::to_string(getpid()) + "_download_tmp_"; tmpFile += Utils::to_string(cpt); cpt++; } while (ecore_file_exists(tmpFile.c_str())); dl_file = fopen(tmpFile.c_str(), "wb"); } else { dl_file = fopen(dest.c_str(), "wb"); } if (!dl_file) { string err = "Failed to open file"; IPC::Instance().SendEvent("downloader::" + Utils::to_string(this), "failed", IPCData(new string(err), new DeletorT<string *>()), true); cb_signal.emit("failed", &err); cb_signal_user.emit("failed", &err, user_data); Utils::logger("downloader") << Priority::ERROR << "Download failed: " << err << log4cpp::eol; ecore_con_url_free(url_con); url_con = NULL; return false; } ecore_con_url_fd_set(url_con, fileno(dl_file)); ecore_con_url_data_set(url_con, this); ecore_con_url_ssl_verify_peer_set(url_con, false); bool ret = false; if (postData.empty()) { ret = ecore_con_url_get(url_con); } else { ret = ecore_con_url_post(url_con, postData.c_str(), postData.length(), postContentType.c_str()); } if (!ret) { string err = "Failed to call GET/POST"; IPC::Instance().SendEvent("downloader::" + Utils::to_string(this), "failed", IPCData(new string(err), new DeletorT<string *>()), true); cb_signal.emit("failed", &err); cb_signal_user.emit("failed", &err, user_data); Utils::logger("downloader") << Priority::ERROR << "Download failed: " << err << log4cpp::eol; ecore_con_url_free(url_con); url_con = NULL; fclose(dl_file); if (dest.empty()) ecore_file_unlink(tmpFile.c_str()); return false; } return true; }
/** * @brief Store given data to store's url. * @param store Store structure. * @param buf Buffer to store. * @param len Length of @buf. * @param done_cb Callback to call when data is stored. * @param error_cb Callback to call if an error occured. * @param data Data to pass to callbacks. * @return EINA_TRUE if we try to store data. * EINA_FALSE if an error occured when creating storing process. */ Eina_Bool store_add(Store *store, const char *buf, size_t len, Store_Done_Cb done_cb, Store_Error_Cb error_cb, const void *data) { Store_Add *sa; Eina_Bool r; sa = calloc(1, sizeof(Store_Add)); if (!sa) { ERR("Failed to allocate Store_Add structure"); return EINA_FALSE; } sa->ec = ecore_con_url_new(store->url); if (!sa->ec) { ERR("Failed to create ecore_con_url object"); goto sa_free; } sa->data.buf = eina_strbuf_new(); if (!sa->data.buf) { ERR("Failed to allocate storage buffer"); goto sa_con_url_free; } sa->ev.ed = ecore_event_handler_add(ECORE_CON_EVENT_URL_DATA, store_event_data, sa); if (!sa->ev.ed) { ERR("Failed to create event handler"); goto sa_buf_free; } sa->ev.ec = ecore_event_handler_add(ECORE_CON_EVENT_URL_COMPLETE, store_event_complete, sa); if (!sa->ev.ec) { ERR("Failed to create event handler"); goto sa_event_handler_free_ed; } sa->store = store; sa->cb.done = done_cb; sa->cb.error = error_cb; sa->cb.data = data; sa->data.sent = strdup(buf); DBG("store[%p] sa[%p] buf[%s]", store, sa, buf); ecore_con_url_data_set(sa->ec, sa); ecore_con_url_timeout_set(sa->ec, 10.0); r = ecore_con_url_post(sa->ec, buf, len, "text/json"); if (!r) { ERR("Failed to issue POST method"); goto sa_event_handler_free_ec; } return EINA_TRUE; sa_event_handler_free_ec: ecore_event_handler_del(sa->ev.ec); sa_event_handler_free_ed: ecore_event_handler_del(sa->ev.ed); sa_buf_free: eina_strbuf_free(sa->data.buf); sa_con_url_free: ecore_con_url_free(sa->ec); sa_free: free(sa); return EINA_FALSE; }