static std::string get_next_url(std::string src_page) { try { std::string content = http_get_content(website_next_url(src_page)); std::stringstream ss(content); std::string file_path; if( std::getline(ss,file_path) ) { return file_path; } } catch (std::string e) { WARN << "get_next_url : " << e; } return ""; }
static void *execute(void *param) { struct settings_t *p = (struct settings_t *)param; char *data = NULL, url[1024], *contype = NULL, *post = NULL; char typebuf[255], *tp = typebuf, *successcode, *token; int ret = 0, size = 0, meth = 0; unsigned int match = 0; if(strcmp(p->method, "POST") == 0) { contype = MALLOC(40); if(!contype) { fprintf(stderr, "out of memory\n"); } free_contype = 1; strcpy(contype, "application/x-www-form-urlencoded"); //strcpy(contype, "text/plain"); post = MALLOC(BUFFER_SIZE); if(!post) { fprintf(stderr, "out of memory\n"); exit(EXIT_FAILURE); } free_post = 1; meth = 1; } if(p->laststate == 0) { //was off, so try to switch on if(meth == 0) { sprintf(url, "%s?%s", p->on_uri, p->on_query); } else { strcpy(url, p->on_uri); strcpy(post, p->on_query); } successcode = MALLOC(strlen(p->on_success) + 1); if(!successcode) { fprintf(stderr, "out of memory\n"); exit(EXIT_FAILURE); } strcpy(successcode, p->on_success); free_successcode = 1; } else { //was on, so try to switch off if(meth == 0) { //GET method sprintf(url, "%s?%s", p->off_uri, p->off_query); } else { //POST method strcpy(url, p->off_uri); strcpy(post, p->off_query); } successcode = MALLOC(strlen(p->off_success) + 1); if(!successcode) { fprintf(stderr, "out of memory\n"); exit(EXIT_FAILURE); } strcpy(successcode, p->off_success); free_successcode = 1; } logprintf(LOG_DEBUG, "Webswitch: Calling %s", url); data = NULL; if (meth == 0) { data = http_get_content(url, &tp, &ret, &size); } else { data = http_post_content(url, &tp, &ret, &size, contype, post); } if(data != NULL) { logprintf(LOG_DEBUG, "Webswitch: Returncode: %i, Type: %s, Data: %s, Size: %i", ret, tp, data, size); } else { logprintf(LOG_DEBUG, "Webswitch: Returncode: %i, Data: NULL", ret); } if((p->response = MALLOC(BUFFER_SIZE)) == NULL) { fprintf(stderr, "out of memory\n"); exit(EXIT_FAILURE); } if(ret == 200) { if(data == NULL || strlen(data) == 0) { logprintf(LOG_NOTICE, "Webswitch: %s didn't return success code. Expected \"%s\", got nothing", url, successcode); if (data == NULL) { strcpy(p->response, "*NULL*"); } else { strcpy(p->response, "*EMPTY*"); } } else { if (size > BUFFER_SIZE) { data[BUFFER_SIZE - 1] = '\0'; logprintf(LOG_NOTICE, "Response size %i is too big, truncated to %i", size, BUFFER_SIZE - 1); } //data = json_encode_string(data); match = 0; strcpy(p->response, data); token = strtok(successcode, "&"); if(token != NULL && strlen(token) > 0) { if(strstr(data, token) != NULL) { match = 1; while ((token = strtok(NULL, "&")) && match == 1) { if(strlen(token) > 0) { if(strstr(data, token) == NULL) { match = 0; } } } } else { //one or more parameters didn't match logprintf(LOG_NOTICE, "Webswitch: %s didn't return success code. Expected \"%s\", got \"%s\"", url, successcode, data); } } else { //response doesn't contain parameter list, so compare full response with expected response if(strstr(data, successcode) == NULL) { // No parameterlist, no match logprintf(LOG_NOTICE, "Webswitch: %s didn't return success code. Expected \"%s\", got \"%s\"", url, successcode, data); } else { // No parameter list, entire expected response matched match = 1; } } } } else { if(p->err_response != NULL) { strcpy(p->response, p->err_response); } else { strcpy(p->response, DFLT_ERR_RSP); } } if (match == 1) { //full match so switch to new state if(p->laststate == 0) { p->newstate = 1; } else { p->newstate = 0; } } else { //no match, so return to last state p->newstate=p->laststate; } if(free_contype) { FREE(contype); } if(free_post) { FREE(post); } if(free_successcode) { FREE(successcode); } p->wait = 0; memset(&p->pth, '\0', sizeof(pthread_t)); p->hasthread = 0; p->laststate = -1; pthread_mutex_unlock(&p->thread->mutex); pthread_cond_signal(&p->thread->cond); return NULL; }