static da_result_t __cancel_download_with_slot_id(int slot_id) { da_result_t ret = DA_RESULT_OK; download_state_t download_state; stage_info *stage = DA_NULL; DA_LOG_FUNC_START(Default); _da_thread_mutex_lock (&mutex_download_state[slot_id]); download_state = GET_DL_STATE_ON_ID(slot_id); DA_LOG(Default, "download_state = %d", GET_DL_STATE_ON_ID(slot_id)); if (download_state == DOWNLOAD_STATE_FINISH || download_state == DOWNLOAD_STATE_CANCELED) { DA_LOG_CRITICAL(Default, "Already download is finished. Do not send cancel request"); _da_thread_mutex_unlock (&mutex_download_state[slot_id]); return ret; } _da_thread_mutex_unlock (&mutex_download_state[slot_id]); stage = GET_DL_CURRENT_STAGE(slot_id); if (!stage) return DA_RESULT_OK; ret = request_to_cancel_http_download(stage); if (ret != DA_RESULT_OK) goto ERR; DA_LOG(Default, "Download cancel Successful for download id - %d", slot_id); ERR: return ret; }
da_result_t send_client_da_state (int download_id, da_state state, int err) { client_noti_t *client_noti = DA_NULL; user_notify_info_t *send_state_info = DA_NULL; da_state cur_da_state; DA_LOG_FUNC_START(ClientNoti); DA_LOG_VERBOSE(ClientNoti, "da_state[%s], download_id[%d]", print_dl_state(state), download_id); if (!is_valid_dl_ID(download_id)) { DA_LOG_ERR(ClientNoti, "Download ID is not valid"); /* Returning DA_RESULT_OK if download_id is not valid, * because sending da_state should not effect to download flow. */ return DA_RESULT_OK; } if (state_watcher_need_redirect_Q(download_id)) { state_watcher_redirect_state(download_id, state, err); return DA_RESULT_OK; } cur_da_state = GET_DL_DA_STATE(download_id); if ((DA_STATE_SUSPENDED != state) && (cur_da_state == state)) { DA_LOG(ClientNoti, "inserting da_state is same with current da_state! Not inserting! inserting: %d, cur : %d", state, cur_da_state); return DA_RESULT_OK; } GET_DL_DA_STATE(download_id) = state; DA_LOG_VERBOSE(ClientNoti, "change da_state to %d", state); client_noti = (client_noti_t *)calloc(1, sizeof(client_noti_t)); if (!client_noti) { DA_LOG_ERR(ClientNoti, "calloc fail"); return DA_ERR_FAIL_TO_MEMALLOC; } client_noti->download_id = download_id; client_noti->user_data = GET_DL_USER_DATA(download_id); client_noti->noti_type = Q_CLIENT_NOTI_TYPE_SEND_STATE; client_noti->next = DA_NULL; send_state_info = (user_notify_info_t *)&(client_noti->type.da_state_info); send_state_info->da_dl_req_id = GET_DL_REQ_ID(download_id); send_state_info->state = state; send_state_info->err = err; DA_LOG(ClientNoti, "pushing da_state=%d, download_id=%d, err=%d, dl_req_id=%d", state, download_id, err, GET_DL_REQ_ID(download_id)); push_client_noti(client_noti); return DA_RESULT_OK; }
da_result_t check_enough_storage(stage_info *stage) { da_result_t ret = DA_RESULT_OK; unsigned long cont_size = 0; da_storage_size_t avail_memory = {0, }; da_storage_type_t type = DA_STORAGE_PHONE; DA_LOG_FUNC_START(Default); /* check enough storage here because of multiple download */ ret = get_storage_type(&type); if (DA_RESULT_OK != ret) return ret; cont_size = GET_CONTENT_STORE_CURRENT_FILE_SIZE(GET_STAGE_CONTENT_STORE_INFO(stage)); DA_LOG(Default,"cont_size [%d]", cont_size); if (cont_size) { ret = get_available_memory(DA_STORAGE_PHONE, &avail_memory); if (DA_RESULT_OK == ret && (avail_memory.b_available < ((cont_size + SAVE_FILE_BUFFERING_SIZE_5MB) / avail_memory.b_size))) /* 5MB buffering */ { DA_LOG_ERR(Default,"There is no space for content in Phone memory"); return DA_ERR_DISK_FULL; } else if (DA_RESULT_OK != ret) { return ret; } /* If default memeory type is mmc, * it need to check phone memroy for temproary folder * and mmc memory for actual install folder */ if (type == DA_STORAGE_MMC) { DA_LOG(Default,"Storage type == DA_STORAGE_MMC\n"); ret = get_available_memory(type, &avail_memory); if (DA_RESULT_OK == ret && (avail_memory.b_available < ((cont_size + SAVE_FILE_BUFFERING_SIZE_50KB) / avail_memory.b_size))) /* 50KB buffering */ { DA_LOG_ERR(Default,"There is no space for content in MMC memory"); return DA_ERR_DISK_FULL; } else if (DA_RESULT_OK != ret) { return ret; } } } return ret; }
da_result_t requesting_download(stage_info *stage) { da_result_t ret = DA_RESULT_OK; req_dl_info *request_session = DA_NULL; DA_LOG_FUNC_START(Default); if (!stage) { DA_LOG_ERR(Default, "stage is null.."); ret = DA_ERR_INVALID_ARGUMENT; goto ERR; } ret = make_req_dl_info_http(stage, GET_STAGE_TRANSACTION_INFO(stage)); if (ret != DA_RESULT_OK) goto ERR; request_session = GET_STAGE_TRANSACTION_INFO(stage); ret = request_http_download(stage); if (DA_RESULT_OK == ret) { DA_LOG(Default, "Http download is complete."); } else { DA_LOG_ERR(Default, "Http download is failed. ret = %d", ret); goto ERR; } ERR: return ret; }
bool WorkThreads::addFd(int fd) { if (-1 == MyUtil::set_non_blocking(fd)) { DA_LOG("gateway", Elephants::WARNING, "[%s: %d] set_non_blocking failed", __FILE__, __LINE__); return false; } struct epoll_event event; event.events = EPOLLIN | EPOLLOUT | EPOLLET; event.data.fd = fd; if (0 != epoll_ctl(m_epfd, EPOLL_CTL_ADD, fd, &event)) { DA_LOG("gateway", Elephants::WARNING, "[%s: %d] epoll_ctl add client failed", __FILE__, __LINE__); return false; } return true; }
da_result_t send_client_update_dl_info ( int download_id, int dl_req_id, char *file_type, unsigned long int file_size, char *tmp_saved_path, char *http_response_header, char *http_chunked_data ) { client_noti_t *client_noti = DA_NULL; user_download_info_t *update_dl_info = DA_NULL; DA_LOG_FUNC_START(ClientNoti); if (!is_valid_dl_ID(download_id)) { DA_LOG_ERR(ClientNoti, "Download ID is not valid"); return DA_ERR_INVALID_DL_REQ_ID; } client_noti = (client_noti_t *)calloc(1, sizeof(client_noti_t)); if (!client_noti) { DA_LOG_ERR(ClientNoti, "calloc fail"); return DA_ERR_FAIL_TO_MEMALLOC; } client_noti->download_id = download_id; client_noti->user_data = GET_DL_USER_DATA(download_id); client_noti->noti_type = Q_CLIENT_NOTI_TYPE_UPDATE_DL_INFO; client_noti->next = DA_NULL; update_dl_info = (user_download_info_t *)&(client_noti->type.update_dl_info); update_dl_info->da_dl_req_id = dl_req_id; update_dl_info->file_size = file_size; /* These strings MUST be copied to detach __thread_for_client_noti from download_info */ if (file_type) update_dl_info->file_type = strdup(file_type); if (tmp_saved_path) update_dl_info->tmp_saved_path = strdup(tmp_saved_path); if (http_response_header) { update_dl_info->http_response_header = strdup(http_response_header); } if (http_chunked_data) { update_dl_info->http_chunked_data = calloc (1, file_size); if (update_dl_info->http_chunked_data) memcpy(update_dl_info->http_chunked_data, http_chunked_data, file_size); } DA_LOG(ClientNoti, "pushing file_size=%lu, download_id=%d, dl_req_id=%d", file_size, download_id, dl_req_id); push_client_noti(client_noti); return DA_RESULT_OK; }
da_result_t send_user_noti_and_finish_download_flow( int slot_id, char *installed_path, char *etag) { da_result_t ret = DA_RESULT_OK; download_state_t download_state = DA_NULL; da_bool_t need_destroy_download_info = DA_FALSE; DA_LOG_FUNC_START(Default); _da_thread_mutex_lock (&mutex_download_state[slot_id]); download_state = GET_DL_STATE_ON_ID(slot_id); DA_LOG(Default, "state = %d", download_state); _da_thread_mutex_unlock (&mutex_download_state[slot_id]); switch (download_state) { case DOWNLOAD_STATE_FINISH: send_client_finished_info(slot_id, GET_DL_ID(slot_id), installed_path, DA_NULL, DA_RESULT_OK, get_http_status(slot_id)); need_destroy_download_info = DA_TRUE; break; case DOWNLOAD_STATE_CANCELED: send_client_finished_info(slot_id, GET_DL_ID(slot_id), installed_path, etag, DA_RESULT_USER_CANCELED, get_http_status(slot_id)); need_destroy_download_info = DA_TRUE; break; #ifdef PAUSE_EXIT case DOWNLOAD_STATE_PAUSED: need_destroy_download_info = DA_TRUE; break; #endif default: DA_LOG(Default, "download state = %d", download_state); break; } if (need_destroy_download_info == DA_TRUE) { destroy_download_info(slot_id); } else { DA_LOG_CRITICAL(Default, "download info is not destroyed"); } return ret; }
da_result_t get_available_memory( da_storage_type_t storage_type, da_storage_size_t *avail_memory) { int fs_ret = 0; struct statfs filesys_info = {0, }; DA_LOG_FUNC_START(Default); if (!avail_memory) return DA_ERR_INVALID_ARGUMENT; if (storage_type == DA_STORAGE_PHONE) { fs_ret = statfs(DA_DEFAULT_TMP_FILE_DIR_PATH, &filesys_info); } else if (storage_type == DA_STORAGE_MMC) { char *default_install_dir = NULL; default_install_dir = PI_get_default_install_dir(); if (default_install_dir) { fs_ret = statfs(default_install_dir, &filesys_info); } else { return DA_ERR_FAIL_TO_ACCESS_STORAGE; } } else { DA_LOG_ERR(Default,"Invalid storage type"); return DA_ERR_INVALID_ARGUMENT; } if (fs_ret != 0) { DA_LOG_ERR(Default,"Phone file path :statfs error - [%d]", errno); return DA_ERR_FAIL_TO_ACCESS_FILE; } avail_memory->b_available = filesys_info.f_bavail; avail_memory->b_size = filesys_info.f_bsize; DA_LOG(Default, "Memory type : %d", storage_type); DA_LOG(Default, "Available Memory(f_bavail) : %lu", filesys_info.f_bavail); DA_LOG(Default, "Available Memory(f_bsize) : %d", filesys_info.f_bsize); DA_LOG(Default, "Available Memory(kbytes) : %lu", (filesys_info.f_bavail/1024)*filesys_info.f_bsize); return DA_RESULT_OK; }
void remove_file(const char *file_path) { DA_LOG_FUNC_START(FileManager); if (file_path) { DA_LOG(FileManager,"remove file [%s]", file_path); if (unlink(file_path) < 0) { DA_LOG_ERR(FileManager,"file removing failed."); } } }
static da_result_t __resume_download_with_slot_id(int slot_id) { da_result_t ret = DA_RESULT_OK; download_state_t download_state; stage_info *stage = DA_NULL; DA_LOG_FUNC_START(Default); _da_thread_mutex_lock (&mutex_download_state[slot_id]); download_state = GET_DL_STATE_ON_ID(slot_id); DA_LOG(Default, "download_state = %d", GET_DL_STATE_ON_ID(slot_id)); _da_thread_mutex_unlock (&mutex_download_state[slot_id]); stage = GET_DL_CURRENT_STAGE(slot_id); ret = request_to_resume_http_download(stage); if (ret != DA_RESULT_OK) goto ERR; DA_LOG(Default, "Download Resume Successful for download id-%d", slot_id); ERR: return ret; }
da_result_t get_extension_from_mime_type(char *mime_type, char **extension) { da_result_t ret = DA_RESULT_OK; char *ext = DA_NULL; DA_LOG_FUNC_START(Default); if (DA_NULL == mime_type || DA_NULL == extension) { DA_LOG_ERR(Default,"received mime_type is null"); ret = DA_ERR_INVALID_ARGUMENT; goto ERR; } DA_LOG(Default,"input mime type = %s", mime_type); if (DA_RESULT_OK != (ret = da_mime_get_ext_name(mime_type, &ext))) { DA_LOG_ERR(Default,"can't find proper extension!"); goto ERR; } *extension = ext; DA_LOG(Default,"found extension = %s", *extension); ERR: return ret; }
std::string BrokerUtil::converDate(const std::string& logtime) { if (logtime.length() < 6) { DA_LOG("process", Elephants::ERR, "Error[%s][%d]: invalid time[%s] 6", __FUNCTION__, __LINE__, logtime.c_str()); return logtime; } std::string year = logtime.substr(0, 4); std::string mon = logtime.substr(4,2); std::string day = logtime.substr(6,2); return year + "-" + mon + "-" + day; }
da_result_t __launch_client_thread(void) { pthread_t thread_id = DA_NULL; DA_LOG_FUNC_START(Thread); if (pthread_create(&thread_id,DA_NULL,__thread_for_client_noti,DA_NULL) < 0) { DA_LOG_ERR(Thread, "making thread failed.."); return DA_ERR_FAIL_TO_CREATE_THREAD; } DA_LOG(Thread, "client mgr thread id[%d]", thread_id); client_app_mgr.thread_id = thread_id; return DA_RESULT_OK; }
da_bool_t da_get_extension_name_from_url(char *url, char **ext) { da_bool_t ret = DA_TRUE; char *buff = DA_NULL; char *temp_str = DA_NULL; int buf_len = 0; DA_LOG_FUNC_START(Default); if (DA_NULL == url || DA_NULL == ext) { ret = DA_FALSE; DA_LOG_ERR(Default,"Invalid Argument"); return ret; } if ((temp_str = strrchr(url,'/'))) { if ((buff = strrchr(temp_str,'.'))) { char *q = DA_NULL; buff++; /* check to exist "?" after extension name */ q = strrchr(buff,'?'); if (q) { buf_len = strlen(buff) - strlen(q); } else { buf_len = strlen(buff); } *ext = (char*) calloc(1, buf_len + 1) ; if (DA_NULL == *ext) { ret = DA_FALSE; DA_LOG_ERR(Default,"Memory Fail"); goto ERR; } strncpy(*ext,buff,buf_len); DA_LOG(Default,"extention name[%s]",*ext); return ret; } } ERR: if (*ext) { free(*ext); *ext = DA_NULL; } return ret; }
da_bool_t is_ambiguous_MIME_Type(const char *in_mime_type) { // DA_LOG_FUNC_START(Default); if (!in_mime_type) return DA_FALSE; int index = 0; int list_size = sizeof(ambiguous_MIME_Type_list) / sizeof(const char *); for (index = 0 ; index < list_size ; index++) { if (0 == strncmp(in_mime_type, ambiguous_MIME_Type_list[index], strlen(ambiguous_MIME_Type_list[index]))) { DA_LOG(Default,"It is ambiguous! [%s]", ambiguous_MIME_Type_list[index]); return DA_TRUE; } } return DA_FALSE; }
da_result_t send_client_update_downloading_info ( int download_id, int dl_req_id, unsigned long int total_received_size, char *saved_path ) { client_noti_t *client_noti = DA_NULL; user_downloading_info_t *downloading_info = DA_NULL; DA_LOG_FUNC_START(ClientNoti); if (!is_valid_dl_ID(download_id)) { DA_LOG_ERR(ClientNoti, "Download ID is not valid"); return DA_ERR_INVALID_DL_REQ_ID; } client_noti = (client_noti_t *)calloc(1, sizeof(client_noti_t)); if (!client_noti) { DA_LOG_ERR(ClientNoti, "calloc fail"); return DA_ERR_FAIL_TO_MEMALLOC; } client_noti->download_id = download_id; client_noti->user_data = GET_DL_USER_DATA(download_id); client_noti->noti_type = Q_CLIENT_NOTI_TYPE_UPDATE_DOWNLOADING_INFO; client_noti->next = DA_NULL; downloading_info = (user_downloading_info_t *)&(client_noti->type.update_downloading_info); downloading_info->da_dl_req_id = dl_req_id; downloading_info->total_received_size = total_received_size; /* These strings MUST be copied to detach __thread_for_client_noti from download_info */ if (saved_path) downloading_info->saved_path = strdup(saved_path); DA_LOG(ClientNoti, "pushing received_size=%lu, download_id=%d, dl_req_id=%d", total_received_size, download_id, dl_req_id); push_client_noti(client_noti); return DA_RESULT_OK; }
std::string BrokerUtil::convertDealTime(const std::string& logtime) { if (logtime.length() < 14) { DA_LOG("process", Elephants::ERR, "Error[%s][%d]: error LogTime[%s]", logtime.c_str()); return logtime; } std::string val = logtime.substr(0, 14); std::tm timeinfo; std::memset(&timeinfo,0, sizeof(timeinfo)); sscanf(val.c_str(),"%4d%2d%2d%2d%2d%2d",&timeinfo.tm_year,&timeinfo.tm_mon,&timeinfo.tm_mday,&timeinfo.tm_hour,&timeinfo.tm_min,&timeinfo.tm_sec); timeinfo.tm_year -= 1900; timeinfo.tm_mon--; std::time_t clock = mktime(&timeinfo); return boost::lexical_cast<std::string>(clock); }
da_result_t handle_after_download(stage_info *stage) { da_result_t ret = DA_RESULT_OK; da_mime_type_id_t mime_type = DA_MIME_TYPE_NONE; DA_LOG_FUNC_START(Default); mime_type = get_mime_type_id( GET_CONTENT_STORE_CONTENT_TYPE(GET_STAGE_CONTENT_STORE_INFO(stage))); switch (mime_type) { case DA_MIME_TYPE_NONE: DA_LOG(Default, "DA_MIME_TYPE_NONE"); ret = DA_ERR_MISMATCH_CONTENT_TYPE; break; default: CHANGE_DOWNLOAD_STATE(DOWNLOAD_STATE_FINISH, stage); break; } /* end of switch */ return ret; }
static void *__thread_for_client_noti(void *data) { da_result_t ret = DA_RESULT_OK; da_bool_t need_wait = DA_TRUE; client_queue_t *queue = DA_NULL; client_noti_t *client_noti = DA_NULL; DA_LOG_FUNC_START(Thread); _da_thread_mutex_lock(&(client_app_mgr.mutex_client_mgr)); client_app_mgr.is_thread_init = DA_TRUE; _da_thread_mutex_unlock(&(client_app_mgr.mutex_client_mgr)); queue = &(client_app_mgr.client_queue); DA_LOG(ClientNoti, "client queue = %p", queue); pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, DA_NULL); pthread_cleanup_push(__thread_clean_up_handler_for_client_thread, (void *)DA_NULL); do { _da_thread_mutex_lock(&(queue->mutex_client_queue)); if (DA_FALSE == IS_CLIENT_Q_HAVING_DATA(queue)) { DA_LOG(Thread, "Sleep @ thread_for_client_noti!"); __client_q_goto_sleep_without_lock(); DA_LOG(Thread, "Woke up @ thread_for_client_noti"); } _da_thread_mutex_unlock(&(queue->mutex_client_queue)); do { __pop_client_noti(&client_noti); if (client_noti == DA_NULL) { DA_LOG_ERR(ClientNoti, "There is no data on client queue!"); ret = DA_ERR_INVALID_STATE; need_wait = DA_FALSE; } else { switch (client_noti->noti_type) { case Q_CLIENT_NOTI_TYPE_UPDATE_DL_INFO: { user_download_info_t *update_dl_info = DA_NULL;; update_dl_info = (user_download_info_t*)(&(client_noti->type.update_dl_info)); if (client_app_mgr.client_app_info.client_callback.update_dl_info_cb) { client_app_mgr.client_app_info.client_callback.update_dl_info_cb(update_dl_info, client_noti->user_data); DA_LOG(ClientNoti, "Update download info for download_id=%d, dl_req_id=%d, received size=%lu- DONE", client_noti->download_id, update_dl_info->da_dl_req_id, update_dl_info->file_size ); } } break; case Q_CLIENT_NOTI_TYPE_UPDATE_DOWNLOADING_INFO: { user_downloading_info_t *downloading_info = DA_NULL;; downloading_info = (user_downloading_info_t*)(&(client_noti->type.update_downloading_info)); if (client_app_mgr.client_app_info.client_callback.update_progress_info_cb) { client_app_mgr.client_app_info.client_callback.update_progress_info_cb(downloading_info, client_noti->user_data); DA_LOG(ClientNoti, "Update downloading info for download_id=%d, dl_req_id=%d, received size=%lu - DONE", client_noti->download_id, downloading_info->da_dl_req_id, downloading_info->total_received_size); } } break; case Q_CLIENT_NOTI_TYPE_SEND_STATE: { user_notify_info_t *da_state_info = DA_NULL; da_state_info = (user_notify_info_t *)(&(client_noti->type.da_state_info)); if (client_app_mgr.client_app_info.client_callback.user_noti_cb) { DA_LOG(ClientNoti, "User Noti info for download_id=%d, dl_req_id=%d, da_state=%d, err=%d", client_noti->download_id, da_state_info->da_dl_req_id, da_state_info->state, da_state_info->err); client_app_mgr.client_app_info.client_callback.user_noti_cb(da_state_info, client_noti->user_data); DA_LOG(ClientNoti, "User Noti info for download_id=%d, dl_req_id=%d, da_state=%d, err=%d - DONE", client_noti->download_id, da_state_info->da_dl_req_id, da_state_info->state, da_state_info->err); } } break; case Q_CLIENT_NOTI_TYPE_TERMINATE: DA_LOG_CRITICAL(ClientNoti, "Q_CLIENT_NOTI_TYPE_TERMINATE"); need_wait = DA_FALSE; break; } destroy_client_noti(client_noti); } if(DA_TRUE == need_wait) { _da_thread_mutex_lock(&(queue->mutex_client_queue)); if (DA_FALSE == IS_CLIENT_Q_HAVING_DATA(queue)) { _da_thread_mutex_unlock (&(queue->mutex_client_queue)); break; } else { _da_thread_mutex_unlock (&(queue->mutex_client_queue)); } } else { break; } } while (1); } while (DA_TRUE == need_wait); _da_thread_mutex_destroy(&(queue->mutex_client_queue)); _da_thread_cond_destroy(&(queue->cond_client_queue)); pthread_cleanup_pop(0); DA_LOG_CRITICAL(Thread, "=====thread_for_client_noti- EXIT====="); pthread_exit((void *)NULL); return DA_NULL; }
da_result_t da_mime_get_ext_name(char *mime, char **ext) { da_result_t ret = DA_RESULT_OK; const char **extlist = DA_NULL; const char *unaliased_mimetype = DA_NULL; char ext_temp[DA_MAX_STR_LEN] = {0,}; char *temp = NULL; DA_LOG_FUNC_START(Default); if (DA_NULL == mime || DA_NULL == ext) { ret = DA_ERR_INVALID_ARGUMENT; DA_LOG_ERR(Default,"Invalid mime type"); goto ERR; } DA_LOG_VERBOSE(Default,"mime str[%s]ptr[%p]len[%d]",mime,mime,strlen(mime)); /* unaliased_mimetype means representative mime among similar types */ unaliased_mimetype = xdg_mime_unalias_mime_type(mime); if (unaliased_mimetype == DA_NULL) { ret = DA_ERR_INVALID_MIME_TYPE; DA_LOG_ERR(Default,"Invalid mime type : No unsaliased mime type"); goto ERR; } DA_LOG(Default,"unaliased_mimetype[%s]\n",unaliased_mimetype); /* Get extension name from shared-mime-info */ extlist = xdg_mime_get_file_names_from_mime_type(unaliased_mimetype); if (extlist == DA_NULL || *extlist == DA_NULL) { int i = 0; ret = DA_ERR_INVALID_MIME_TYPE; DA_LOG(Default,"No extension list"); #ifdef _SAMSUNG_MIME_POLICY for (i = 0; i < MAX_SEC_MIME_TABLE_INDEX; i++) { if (strncmp(sec_mime_table[i].mime, mime, strlen(mime)) == 0) { strncpy(ext_temp, sec_mime_table[i].ext, DA_MAX_STR_LEN-1); ret = DA_RESULT_OK; break; } } #endif } else { /* For drm case, this else statement is needed */ DA_LOG(Default,"extlist[%s]\n",*extlist); strncpy(ext_temp, *extlist, DA_MAX_STR_LEN); /* If only one extension name is existed, don't enter here */ while (*extlist != NULL) { int i = 0; /* If there are existed many extension names, * try to search common extension name from table * with first mime type at extension list*/ for (i = 0; i < MAX_EXT_TABLE_INDEX; i++) { if (strncmp(ext_trans_table[i].standard,*extlist, strlen(*extlist)) == 0) { memset(ext_temp, 0x00, DA_MAX_STR_LEN); strncpy(ext_temp,ext_trans_table[i].normal, DA_MAX_STR_LEN-1); break; } } DA_LOG(Default,"index[%d]\n",i); /* If there is a mime at extension transform table */ if (i < MAX_EXT_TABLE_INDEX) { break; } DA_LOG(Default,"extlist[%s]\n",*extlist); extlist++; } DA_LOG(Default,"extension from shared mime info[%s]",ext_temp); } if (strlen(ext_temp) < 1) { /* If there is no mime string for OMD descriptor mime type */ if (strncmp(DD_MIME_STR,mime,strlen(DD_MIME_STR)) == 0) { strncpy(ext_temp, DD_EXT_STR, DA_MAX_STR_LEN-1); ret = DA_RESULT_OK; /* If there is no extension name for "applicaion/vnd.oma.drm.messeages" * at shared-mime-info*/ } else if (strncmp(DRM_MIME_STR,mime,strlen(DD_MIME_STR)) == 0) { strncpy(ext_temp, DRM_EXT_STR, DA_MAX_STR_LEN-1); /* If there is extension name at extlist, the return value can have an error.*/ ret = DA_RESULT_OK; } else { ret = DA_ERR_INVALID_MIME_TYPE; DA_LOG_ERR(Default,"Invalid mime type : no extension name at list"); } } if (ret != DA_RESULT_OK) goto ERR; temp = strchr(ext_temp,'.'); if (temp == NULL) temp = ext_temp; else temp++; DA_LOG(Default,"final extension name:[%s]",temp); *ext = (char*)calloc(1, strlen(temp) + 1); if (*ext != DA_NULL) { strncpy(*ext, temp,strlen(temp)); } else { ret = DA_ERR_FAIL_TO_MEMALLOC ; goto ERR ; } ERR: return ret; }
da_result_t _parsing_base64_encoded_str(const char *in_encoded_str, char **out_charset_type, char *out_encoding_type, char **out_raw_encoded_str) { da_result_t ret = DA_RESULT_OK; const char *org_str = DA_NULL; // e.g. =?UTF-8?B?7Jew7JWE7JmA7IKs7J6QLmpwZw==?= char *charset_type = NULL; // e.g. UTF-8 char encoding_type = '\0'; // e.g. B (means Base64) char *raw_encoded_str = NULL; // e.g. 7Jew7JWE7JmA7IKs7J6QLmpwZw== char *haystack = DA_NULL; char needle[8] = {0,}; char *wanted_str = DA_NULL; int wanted_str_len = 0; char *wanted_str_start = DA_NULL; char *wanted_str_end = DA_NULL; org_str = in_encoded_str; if (!org_str) { DA_LOG_ERR(Default, "Input string is NULL"); ret = DA_ERR_INVALID_ARGUMENT; goto ERR; } // strip "=?" haystack = (char*)org_str; snprintf(needle, sizeof(needle), "=?"); wanted_str_end = strstr(haystack, needle); if (!wanted_str_end) { DA_LOG_ERR(Default, "DA_ERR_INVALID_ARGUMENT"); ret = DA_ERR_INVALID_ARGUMENT; goto ERR; } else { wanted_str = wanted_str_end + strlen(needle); DA_LOG(Default, "strip [%s]", wanted_str); } // for charset haystack = wanted_str_start = wanted_str; needle[0] = '?'; wanted_str_end = strchr(haystack, needle[0]); if (!wanted_str_end) { DA_LOG_ERR(Default, "DA_ERR_INVALID_ARGUMENT"); ret = DA_ERR_INVALID_ARGUMENT; goto ERR; } else { wanted_str_len = wanted_str_end - wanted_str_start + 1; wanted_str = (char*)calloc(1, wanted_str_len+1); if (!wanted_str) { DA_LOG_ERR(Default, "DA_ERR_FAIL_TO_MEMALLOC"); ret = DA_ERR_FAIL_TO_MEMALLOC; goto ERR; } else { snprintf(wanted_str, wanted_str_len+1, "%s", wanted_str_start); charset_type = wanted_str; wanted_str = DA_NULL; } DA_LOG(Default, "charset [%s]", charset_type); } // for encoding encoding_type = *(++wanted_str_end); DA_LOG(Default, "encoding [%c]", encoding_type); // for raw encoded str haystack = wanted_str_start = wanted_str_end + 1; snprintf(needle, sizeof(needle), "?="); wanted_str_end = strstr(haystack, needle); if (!wanted_str_end) { DA_LOG_ERR(Default, "DA_ERR_INVALID_ARGUMENT"); ret = DA_ERR_INVALID_ARGUMENT; goto ERR; } else { wanted_str_len = wanted_str_end - wanted_str_start + 1; wanted_str = (char*)calloc(1, wanted_str_len+1); if (!wanted_str) { DA_LOG_ERR(Default, "DA_ERR_FAIL_TO_MEMALLOC"); ret = DA_ERR_FAIL_TO_MEMALLOC; goto ERR; } else { snprintf(wanted_str, wanted_str_len+1, "%s", wanted_str_start); raw_encoded_str = wanted_str; wanted_str = NULL; } DA_LOG(Default, "raw encoded str [%s]", raw_encoded_str); } ERR: if (ret != DA_RESULT_OK) { if (charset_type) { free(charset_type); charset_type = NULL; } } *out_charset_type = charset_type; *out_encoding_type = encoding_type; *out_raw_encoded_str = raw_encoded_str; return ret; }
da_result_t decode_base64_encoded_str(const char *in_encoded_str, char **out_decoded_ascii_str) { da_result_t ret = DA_RESULT_OK; const char *org_str = DA_NULL; char *charset_type = NULL; char encoding_type = '\0'; char *raw_encoded_str = NULL; char *decoded_str = NULL; const gchar *g_encoded_text = NULL; guchar *g_decoded_text = NULL; gsize g_decoded_text_len = 0; DA_LOG(Default, "input str = [%s]", in_encoded_str); org_str = in_encoded_str; if(!org_str) { DA_LOG_ERR(Default, "Input string is NULL"); ret = DA_ERR_INVALID_ARGUMENT; goto ERR; } ret = _parsing_base64_encoded_str(org_str, &charset_type, &encoding_type, &raw_encoded_str); if(ret != DA_RESULT_OK) { goto ERR; } // DA_LOG(Default, "charset = [%s], encoding = [%c], raw = [%s]", charset_type, encoding_type, raw_encoded_str); if(encoding_type != 'B') { DA_LOG_ERR(Default, "Encoded Word is not encoded with Base64, but %c. We can only handle Base64.", encoding_type); ret = DA_ERR_INVALID_ARGUMENT; goto ERR; } /* * on glib/gtype.h * typedef char gchar; * typedef unsigned char guchar; * */ g_encoded_text = (const gchar*)raw_encoded_str; g_decoded_text = g_base64_decode(g_encoded_text, &g_decoded_text_len); if(g_decoded_text) { DA_LOG(Default, "g_decoded_text = [%s]", g_decoded_text); decoded_str = (char*)calloc(1, g_decoded_text_len+1); if(!decoded_str) { DA_LOG_ERR(Default, "DA_ERR_FAIL_TO_MEMALLOC"); ret = DA_ERR_FAIL_TO_MEMALLOC; goto ERR; } else { memcpy(decoded_str, g_decoded_text, g_decoded_text_len); } } DA_LOG(Default, "decoded_str = [%s]", decoded_str); ERR: *out_decoded_ascii_str = decoded_str; if(charset_type) { free(charset_type); charset_type = NULL; } if(raw_encoded_str) { free(raw_encoded_str); raw_encoded_str = NULL; } if(g_decoded_text) { g_free(g_decoded_text); } return ret; }
/* FIXME move this function to another file */ da_bool_t da_get_file_name_from_url(char* url, char** name) { da_bool_t ret = DA_TRUE; char *buff = DA_NULL; char *Start = NULL; char *End = NULL; char c = 0; int i = 0; int j = 0; int len_name = 0; char name_buff[DA_MAX_FILE_PATH_LEN] = {0,}; DA_LOG_FUNC_START(Default); if (DA_NULL == url || DA_NULL == name) { ret = DA_FALSE; DA_LOG_ERR(Default,"Invalid Argument"); goto ERR; } *name = DA_NULL; if (!strstr(url, "http") && !strstr(url, "https")) { ret = DA_FALSE; DA_LOG_ERR(Default,"Invalid Argument"); goto ERR; } buff = (char*) calloc(1, strlen(url) +1); if(DA_NULL == buff) { ret = DA_FALSE; DA_LOG_ERR(Default,"Memory Fail"); goto ERR; } while((c = url[i++]) != 0) { if(c == '%') { char buffer[3] = {0,}; buffer[0] = url[i++]; buffer[1] = url[i++]; buff[j++] = (char)strtol(buffer,NULL,16); } else { buff[j++] = c; } } End = strstr(buff, "?"); if (DA_NULL != End) { Start = End -1; while(*(Start) != '/') { Start--; } if ((*(Start) == '/') && ((len_name = (End - Start)) > 1)) { Start++; if (DA_MAX_FILE_PATH_LEN <= len_name) { strncpy(name_buff, Start, DA_MAX_FILE_PATH_LEN-1); name_buff[DA_MAX_FILE_PATH_LEN-1] = '\0'; } else { strncpy(name_buff, Start, len_name); name_buff[len_name] = '\0'; } } else { ret = DA_FALSE; goto ERR ; /*Name not found*/ } } else { int urlLen = strlen (buff); int Start_pos = 0; Start_pos = urlLen - 1; while(Start_pos > 0) { if(buff[Start_pos] == '/') break; Start_pos--; } Start_pos++; if (Start_pos == 0 || urlLen - Start_pos <= 0) { ret = DA_FALSE; goto ERR; } while(Start_pos < urlLen) { name_buff[len_name++] = buff[Start_pos++]; if (DA_MAX_FILE_PATH_LEN <= len_name) { name_buff[DA_MAX_FILE_PATH_LEN-1] ='\0'; break; } } } if (len_name) { End = strrchr(name_buff, '.'); if (End != NULL) { *End = '\0'; } DA_LOG(Default,"file name BEFORE removing prohibited character = %s", name_buff); delete_prohibited_char(name_buff, strlen(name_buff)); DA_LOG(Default,"file name AFTER removing prohibited character = %s", name_buff); len_name = strlen(name_buff); *name = (char*) calloc(1, len_name + 1); if (*name) { strncpy(*name, name_buff,len_name); } } DA_LOG(Default,"Extracted file name : %s", *name); ERR: if (buff) { free (buff); buff = DA_NULL; } return ret; }
void WorkThreads::run() { m_runing = true; struct epoll_event* events = (struct epoll_event*)calloc(EPOLL_SIZE, sizeof(struct epoll_event)); int num = 0; while (m_runing) { num = epoll_wait(m_epfd, events, EPOLL_SIZE, 500); for (int i = 0; i < num; ++i) { try { if (events[i].events & EPOLLERR || events[i].events & EPOLLHUP || (!events[i].events & EPOLLIN && !events[i].events & EPOLLOUT)) { this->shutdownfd(events[i].data.fd); continue; } else if (events[i].events & EPOLLIN) { std::map<int, ServerBufferPtr>::iterator it = m_recvBuffer.find(events[i].data.fd); if (it == m_recvBuffer.end()) { m_recvBuffer[events[i].data.fd] = ServerBufferPtr(new (std::nothrow) ServerBuffer(4096)); } ServerBuffer& rcv = *(m_recvBuffer[events[i].data.fd]); // 扩大buffer大小 if (0 == rcv.getAvailableSize() && rcv.moveData() && 0 == rcv.getAvailableSize()) { rcv.doubleSize(); } // 读取数据 int done = 0; std::size_t rs = read(events[i].data.fd, rcv.buffer, rcv.getAvailableSize()); if (-1 == rs) { DA_LOG("gateway", Elephants::NOTICE, "fd[%d] error", events[i].data.fd); done = 1; } else if (0 == rs) { DA_LOG("gateway", Elephants::NOTICE, "fd[%d] has been closed", events[i].data.fd); } if (1 == done) { this->shutdownfd(events[i].data.fd); } // 检查拆分数据包 uint32_t package = *((uint32_t*)((char*)rcv.buffer + rcv.start)); if (package + sizeof(uint16_t) + sizeof(uint32_t) <= rcv.getDataSize()) { // 获取服务ID uint16_t service = *((uint16_t*)((char*)rcv.buffer + rcv.start + sizeof(uint32_t))); // 打包application请求数据包 std::size_t sz = sizeof(uint32_t) + sizeof(uint64_t) + sizeof(uint16_t) + package; ServerBufferPtr dptr(new (std::nothrow) ServerBuffer(sz)); // 1, length uint32_t* pLength = (uint32_t*)((char*)(dptr->buffer)); *pLength = package; // 2, sequence id uint64_t* pSeq = (uint64_t*)((char*)(dptr->buffer) + sizeof(uint32_t)); *pSeq = SequenceInstance::instance().getSequence(); // 3, version uint16_t* pVersion = (uint16_t*)((char*)(dptr->buffer) + sizeof(uint32_t) + sizeof(uint64_t)); *pVersion = ConfigSingleton::instance().getValue<uint16_t>(boost::lexical_cast<std::string>(service), std::string("version")); // 4, body数据 void* dst = (void*)((char*)(dptr->buffer) + sizeof(uint32_t) + sizeof(uint64_t) + sizeof(uint16_t)); void* src = (void*)((char*)rcv.buffer + rcv.start + sizeof(uint32_t) + sizeof(uint16_t)); memcpy(dst, src, package); // call application client } } else if (events[i].events & EPOLLOUT) { DA_LOG("gateway", Elephants::DEBUG, "write event"); } } catch (...) { this->shutdownfd(events[i].data.fd); } } } }