Guest User

Untitled

a guest
Apr 23rd, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. <tag1>
  2. <tag2>
  3. <Server>
  4. <Host>content1</Host>
  5. <User>content2</User>
  6. <Pass encoding="base64">content3</Pass>
  7. </Server>
  8. <Server>
  9. <Host>content4</Host>
  10. <User>content5</User>
  11. <Pass encoding="base64">content6</Pass>
  12. </Server>
  13. <Server>
  14. <Host>content7</Host>
  15. <User>content8</User>
  16. <Pass encoding="base64">content9</Pass>
  17. </Server>
  18. </tag2>
  19. </tag1>
  20.  
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include <fcntl.h>
  25. #include <sys/stat.h>
  26. #include <curl/curl.h>
  27. #define keyurl "http://www.link.com"
  28. int getKeyVal(char *key);
  29. // return data from the server
  30. struct return_string {
  31. char *ptr;
  32. size_t len;
  33. };
  34. // utility functions
  35. size_t accumulate(void *ptr, size_t size, size_t nmemb, struct return_string *s);
  36. void init_string(struct return_string *s);
  37. /* get the <key> */
  38. int getKeyVal(char *key)
  39. {
  40. CURL *curl;
  41. CURLcode res;
  42. char url[128];
  43. sprintf(url, "%s/k=%s", keyurl, key);
  44. curl = curl_easy_init();
  45. if (curl) {
  46. struct return_string s;
  47. init_string(&s);
  48. curl_easy_setopt(curl, CURLOPT_URL, keyurl);
  49. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  50. curl_easy_setopt(curl, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS);
  51. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, accumulate);
  52. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
  53. res = curl_easy_perform(curl);
  54. printf("result: %sn", s.ptr);
  55. free(s.ptr);
  56. curl_easy_cleanup(curl);
  57. }
  58. else {
  59. res = -99;
  60. }
  61. return(res);
  62. }
  63. /* Initialize the string handler so that it is thread safe */
  64. void init_string(struct return_string *s)
  65. {
  66. s->len = 0;
  67. s->ptr = malloc(s->len + 1);
  68. if (s->ptr == NULL) {
  69. fprintf(stderr, "malloc() failedn");
  70. exit(-1);
  71. }
  72. s->ptr[0] = '';
  73. }
  74. /* Use the "writer" to accumulate text until done */
  75. size_t accumulate(void *ptr, size_t size, size_t nmemb, struct return_string *s)
  76. {
  77. size_t new_len = s->len + size*nmemb;
  78. s->ptr = realloc(s->ptr, new_len + 1);
  79. if (s->ptr == NULL) {
  80. fprintf(stderr, "realloc() failedn");
  81. exit(-1);
  82. }
  83. memcpy(s->ptr + s->len, ptr, size*nmemb);
  84. s->ptr[new_len] = '';
  85. s->len = new_len;
  86. return size*nmemb;
  87. }
  88. int getContent()
  89. {
  90. //constants for reading the XML file
  91. char *buffer = 0;
  92. char finalRequest[200];
  93. long length;
  94. FILE *xmlFile = fopen("C:/Users/IEUser/Desktop/rs.xml", "r");
  95. //constants for parsing XML
  96. const char *HOSTSTART = "<Host>";
  97. const char *HOSTEND = "</Host>";
  98. const char *USERSTART = "<User>";
  99. const char *USEREND = "</User>";
  100. const char *PASSSTART = "<Pass encoding="base64">";
  101. const char *PASSEND = "</Pass>";
  102. char *target_host = NULL, *target_user = NULL, *target_pass = NULL, *start, *end;
  103. //read the xml file
  104. if (xmlFile)
  105. {
  106. fseek(xmlFile, 0, SEEK_END);
  107. length = ftell(xmlFile);
  108. fseek(xmlFile, 0, SEEK_SET);
  109. buffer = malloc(length);
  110. if (buffer)
  111. {
  112. fread(buffer, 1, length, xmlFile);
  113. }
  114. fclose(xmlFile);
  115. if (!buffer) exit(1);
  116. }
  117. start = buffer;
  118. while (1) {
  119. //get the HOST
  120. if (start = strstr(start, HOSTSTART))
  121. {
  122. start += strlen(HOSTSTART);
  123. if (end = strstr(start, HOSTEND))
  124. {
  125. target_host = (char *)malloc(end - start + 1);
  126. memcpy(target_host, start, end - start);
  127. target_host[end - start] = '';
  128. }
  129. }
  130. else {
  131. break;
  132. }
  133. //get the USER
  134. if (start = strstr(start, USERSTART))
  135. {
  136. start += strlen(USERSTART);
  137. if (end = strstr(start, USEREND))
  138. {
  139. target_user = (char *)malloc(end - start + 1);
  140. memcpy(target_user, start, end - start);
  141. target_user[end - start] = '';
  142. }
  143. }
  144. //get the PASS
  145. if (start = strstr(start, PASSSTART))
  146. {
  147. start += strlen(PASSSTART);
  148. if (end = strstr(start, PASSEND))
  149. {
  150. target_pass = (char *)malloc(end - start + 1);
  151. memcpy(target_pass, start, end - start);
  152. target_pass[end - start] = '';
  153. }
  154. }
  155. //send the requested string in the desired format to finalRequest[]
  156. //k=host|user|password
  157. strcpy(finalRequest, target_host);
  158. strcat(finalRequest, "|");
  159. strcat(finalRequest, target_user);
  160. strcat(finalRequest, "|");
  161. strcat(finalRequest, target_pass);
  162. printf("%s", finalRequest);
  163. printf("nn");
  164. getKeyVal(finalRequest);
  165. }
  166. return 0;
  167. }
  168. int main()
  169. {
  170. getContent();
  171. return 0;
  172. }
Add Comment
Please, Sign In to add comment