rendered paste body#include <stdio.h>#include <stdlib.h>#include <string.h>#include <ctype.h>#include <curl/curl.h>#include <curl/types.h>#include <curl/easy.h>#include <libxml/parser.h>#include <libxml/tree.h>#include <openssl/md5.h>#include "lfm_helper.h"int string2MD5(const char *string, char *buffer){ char hash[MD5_DIGEST_LENGTH]; int i; char *cptr; MD5((const unsigned char *)string,strlen(string),(unsigned char *)hash); cptr = buffer; for(i=0;i<MD5_DIGEST_LENGTH;i++){ sprintf(cptr,"%02x",(unsigned char)hash[i]); cptr+=2; } buffer[(MD5_DIGEST_LENGTH*2)]=0; return 0;}void *myrealloc(void *ptr, unsigned int size){ /* There might be a realloc() out there that doesn't like reallocing NULL pointers, so we take care of it here */ if(ptr) return realloc(ptr, size); else return malloc(size);}size_t WriteWebDataCallback(void *ptr, size_t size, size_t nmemb, void *data){ unsigned int realsize = size * nmemb; WebData *mem = data;// printf("WriteBack ptr=%x, size=%u, nmemb=%u\n",ptr,size,nmemb); mem->page = myrealloc(mem->page, mem->size + realsize + 1); if (mem->page) { memcpy(&(mem->page[mem->size]), ptr, realsize);// memcpy(mem->page,ptr,realsize); mem->size += realsize; mem->page[mem->size] = 0; }// mem->page[realsize-1]=0; return realsize;}/*xmlNode *_lfm_helper_get_xml_element(xmlNode *start_node, const char *node, const char *property, const char *value){ xmlNode *cur_node = NULL; xmlNode *found_node = NULL; char *temp=NULL; for(cur_node = start_node; cur_node; cur_node= cur_node->next){ if (cur_node->type == XML_ELEMENT_NODE) { if(strcmp((const char *)cur_node->name,node)==0){ if(property){ temp = (char *) xmlGetProp(cur_node,(xmlChar *)property); if(temp && value){ if(strcmp(temp,value) == 0){ free(temp); //printf("get_xml name = %s\n",cur_node->name); return cur_node; } free(temp); }else if(temp){ free(temp); //printf("get_xml name = %s\n",cur_node->name); return cur_node; } }else { //printf("get_xml name = %s\n",cur_node->name); return cur_node; } } } // Recursive call to look for children if(cur_node->children){ found_node = _lfm_helper_get_xml_element(cur_node->children, node,property,value); if(found_node)return found_node; } } return NULL;}*/xmlNode *_lfm_helper_get_xml_element(xmlNode *start_node,xmlNode **skip_node, const char *node, const char *property, const char *value){ xmlNode *cur_node = NULL; xmlNode *found_node = NULL; char *temp=NULL; for(cur_node = start_node; cur_node; cur_node= cur_node->next){ if(skip_node){ if(*skip_node){ if(cur_node != *skip_node){ // forget about it }else { *skip_node = NULL; } goto forget; } } if (cur_node->type == XML_ELEMENT_NODE) { if(strcmp((const char *)cur_node->name,node)==0){ if(property){ temp = (char *) xmlGetProp(cur_node,(xmlChar *)property); if(temp && value){ if(strcmp(temp,value) == 0){ free(temp); //printf("get_xml name = %s\n",cur_node->name); return cur_node; } free(temp); }else if(temp){ free(temp); //printf("get_xml name = %s\n",cur_node->name); return cur_node; } }else { //printf("get_xml name = %s\n",cur_node->name); return cur_node; } } } // Recursive call to look for children forget: if(cur_node->children){ found_node = _lfm_helper_get_xml_element(cur_node->children, skip_node,node,property,value); if(found_node)return found_node; } //found_node; } return NULL;}/* Returns 0 if not entirely white space */int strisspace(const char *string){ char *i; if(string == NULL)return 1; for(i=(char *)string;*i;i++){ if(!isspace(*i))return 0; } return 1;} int lfm_helper_get_xml_element_content(const WebData *xml, const char *element,const char *property, const char *value, char **buffer){ xmlNode *element_node; xmlNode *root_node; xmlDoc *doc; doc = xmlReadMemory(xml->page,xml->size,"noname.xml",NULL,0); if (doc == NULL) { fprintf(stderr, "Failed to parse document\n"); xmlCleanupParser(); return 1; } root_node = xmlDocGetRootElement(doc); element_node = _lfm_helper_get_xml_element(root_node,NULL,element,property,value); if(!element_node){ xmlFreeDoc(doc); xmlCleanupParser(); return 1; } if(!element_node->children){ printf("lfm_helper_get_xml_element_content: no children\n"); // xmlFreeDoc(doc); // xmlCleanupParser(); // return 1; }else{ element_node = element_node->children; }// if(element_node->type == XML_TEXT_NODE){ if(strisspace((const char *)element_node->content))return 1; *buffer = malloc(1+strlen((char *)element_node->content)); strcpy(*buffer,(char *)element_node->content);// }else {// printf("lfm_helper_get_xml_element_content: not text node\n");// xmlFreeDoc(doc);// xmlCleanupParser();// return 1;// } xmlFreeDoc(doc); xmlCleanupParser(); return 0;}int lfm_helper_get_xml_element_property(const WebData *xml, const char *element,const char *property, const char *value, char **buffer){ xmlDoc *doc = NULL; xmlNode *root_node = NULL; xmlNode *found_node = NULL; doc = xmlReadMemory(xml->page,xml->size,"noname.xml",NULL,0); if (doc == NULL) { fprintf(stderr, "Failed to parse document\n"); return 1; } root_node = xmlDocGetRootElement(doc); found_node = _lfm_helper_get_xml_element(root_node,NULL,element,property,value);// temp = (char *)xmlGetProp(found_node,(xmlChar *) property);// strcpy(buffer,temp); *buffer= (char *)xmlGetProp(found_node,(xmlChar *) property); /* Recursive free, frees nodes too */ xmlFreeDoc(doc); xmlCleanupParser(); return 0;} int lfm_helper_get_all_xml_element_content(const WebData *xml, const char **element_path,const char *element, char ***buffer){ xmlDoc *doc = NULL; xmlNode *root_node = NULL; xmlNode *element_node = NULL; xmlNode *skip_node = NULL; xmlNode *found_node = NULL; int i; doc = xmlReadMemory(xml->page,xml->size,"noname.xml",NULL,0); if (doc == NULL) { fprintf(stderr, "Failed to parse document\n"); return 1; } root_node = xmlDocGetRootElement(doc); if(element_path){ for(i=0;element_path[i];i++){ root_node = _lfm_helper_get_xml_element(root_node,NULL,element_path[i],NULL,NULL); } } found_node = _lfm_helper_get_xml_element(root_node,NULL,element,NULL,NULL); i=1; /* Use *buffer directly and realloc messes up */ char **tptr=NULL; while(found_node){ if(!found_node->children){ printf("lfm_helper_get_xml_element_content: no children\n"); }else{ element_node = found_node->children; } tptr = myrealloc(tptr,(sizeof(char *))*i); if(tptr==NULL){ printf("realloc!!!!!\n"); }else { tptr[i-1] = malloc(strlen((const char *)element_node->content)+1); strcpy(tptr[i-1],(char *)element_node->content); i++; } skip_node = found_node; found_node = _lfm_helper_get_xml_element(root_node,&skip_node,element,NULL,NULL); } tptr = realloc(tptr,(sizeof(char *))*i); tptr[i-1] = NULL; *buffer = tptr; /* if(strisspace((const char *)element_node->content))return 1; *buffer = malloc(1+strlen((char *)element_node->content)); strcpy(*buffer,(char *)element_node->content); */ xmlFreeDoc(doc); xmlCleanupParser(); return 0;} /* Size must be a multiple of 2, ie there are (size/2) params in 'args'; * Each param is at N * Each value is at N+1 */WebData *lfm_helper_post_page(CURL *curl, curl_progress_callback cb, void *cb_data, const char *url,const char **args, int size){ WebData *wpage; struct curl_httppost *formpost=NULL; struct curl_httppost *lastptr=NULL; struct curl_slist *header = NULL; int i; int own_curl=0; CURLcode result; /* Did we RTFM ? */ if(size%2)return NULL; printf("Posting to URL|%s|\n",url); wpage = malloc(sizeof(WebData)); wpage->size=0; wpage->page=NULL; // Realloc needs this /* Are we using our own handle ? */ if(!curl){ own_curl=1; curl = curl_easy_init(); curl_easy_setopt(curl, CURLOPT_POST , 1 ) ; /* send all data to this function */ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteWebDataCallback); /* Allow redirection */ curl_easy_setopt(curl ,CURLOPT_FOLLOWLOCATION, 1); } /* we pass our 'WebData' struct to the callback function */ curl_easy_setopt(curl, CURLOPT_WRITEDATA, NULL); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)wpage); curl_easy_setopt(curl, CURLOPT_URL,url); for(i=0;i<size;i+=2){ curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME,args[i], CURLFORM_COPYCONTENTS,args[i+1], CURLFORM_END); } header = curl_slist_append(header,"Expect:"); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header); curl_easy_setopt(curl,CURLOPT_HTTPPOST,formpost); result = curl_easy_perform(curl); printf("Curl result = %i\n",result); if(!wpage)return NULL; /* Did we use our curl, and was the result not bad */ if(own_curl){ curl_easy_cleanup(curl); } if(result){ if(wpage)lfm_helper_free_page(wpage); wpage = NULL; } curl_slist_free_all(header); curl_formfree(formpost); return wpage;} int lfm_helper_free_page(WebData *wpage){ if(!wpage)return 1; if(wpage->page) free(wpage->page); free(wpage); return 0;} //WebData *lfm_helper_get_page(const char *url)WebData *lfm_helper_get_page(const char *url, curl_progress_callback cb, void *cb_data){ /* libcurl handle */ CURL *curl_handle; WebData *chunk; printf("Getting URL|%s|\n",url); chunk = malloc(sizeof(WebData)); chunk->page = NULL; /* we expect realloc(NULL, size) to work */ chunk->size = 0; /* no data at this point */ curl_global_init(CURL_GLOBAL_ALL); /* init the curl session */ curl_handle = curl_easy_init(); /* specify URL to get */ curl_easy_setopt(curl_handle, CURLOPT_URL, url); /* send all data to this function */ curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteWebDataCallback); /* we pass our 'chunk' struct to the callback function */ curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)chunk); /* some servers don't like requests that are made without a user-agent * field, so we provide one */ /* Just in case somone out there doesnt like us */ //curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0"); curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; U; Linux 2.4.2-2 i586; en-US; m18) Gecko/20010131 Netscape6/6.01"); /* progress bar */ curl_easy_setopt(curl_handle,CURLOPT_NOPROGRESS,0); curl_easy_setopt(curl_handle,CURLOPT_PROGRESSFUNCTION,cb); curl_easy_setopt(curl_handle,CURLOPT_PROGRESSDATA,cb_data); /* get it! */ curl_easy_perform(curl_handle); /* cleanup curl stuff */ curl_easy_cleanup(curl_handle); /* we're done with libcurl, so clean it up */ curl_global_cleanup();// printf("%s\n",chunk->page); return chunk;}