curltest.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* Prevent warnings on Visual Studio */
  2. struct _RPC_ASYNC_STATE;
  3. #include "curl/curl.h"
  4. #include <stdlib.h>
  5. #include <string.h>
  6. int GetFtpFile(void)
  7. {
  8. int retVal = 0;
  9. CURL *curl;
  10. CURLcode res;
  11. curl = curl_easy_init();
  12. if(curl)
  13. {
  14. /* Get curl 7.9.2 from sunet.se's FTP site: */
  15. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  16. curl_easy_setopt(curl, CURLOPT_HEADER, 1);
  17. curl_easy_setopt(curl, CURLOPT_URL,
  18. "ftp://public.kitware.com/pub/cmake/cygwin/setup.hint");
  19. res = curl_easy_perform(curl);
  20. if ( res != 0 )
  21. {
  22. printf("Error fetching: ftp://public.kitware.com/pub/cmake/cygwin/setup.hint\n");
  23. retVal = 1;
  24. }
  25. /* always cleanup */
  26. curl_easy_cleanup(curl);
  27. }
  28. else
  29. {
  30. printf("Cannot create curl object\n");
  31. retVal = 1;
  32. }
  33. return retVal;
  34. }
  35. int GetWebFiles(char *url1, char *url2)
  36. {
  37. int retVal = 0;
  38. CURL *curl;
  39. CURLcode res;
  40. char proxy[1024];
  41. int proxy_type = 0;
  42. if ( getenv("HTTP_PROXY") )
  43. {
  44. proxy_type = 1;
  45. if (getenv("HTTP_PROXY_PORT") )
  46. {
  47. sprintf(proxy, "%s:%s", getenv("HTTP_PROXY"), getenv("HTTP_PROXY_PORT"));
  48. }
  49. else
  50. {
  51. sprintf(proxy, "%s", getenv("HTTP_PROXY"));
  52. }
  53. if ( getenv("HTTP_PROXY_TYPE") )
  54. {
  55. /* HTTP/SOCKS4/SOCKS5 */
  56. if ( strcmp(getenv("HTTP_PROXY_TYPE"), "HTTP") == 0 )
  57. {
  58. proxy_type = 1;
  59. }
  60. else if ( strcmp(getenv("HTTP_PROXY_TYPE"), "SOCKS4") == 0 )
  61. {
  62. proxy_type = 2;
  63. }
  64. else if ( strcmp(getenv("HTTP_PROXY_TYPE"), "SOCKS5") == 0 )
  65. {
  66. proxy_type = 3;
  67. }
  68. }
  69. }
  70. curl = curl_easy_init();
  71. if(curl)
  72. {
  73. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  74. curl_easy_setopt(curl, CURLOPT_HEADER, 1);
  75. /* Using proxy */
  76. if ( proxy_type > 0 )
  77. {
  78. curl_easy_setopt(curl, CURLOPT_PROXY, proxy);
  79. switch (proxy_type)
  80. {
  81. case 2:
  82. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
  83. break;
  84. case 3:
  85. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
  86. break;
  87. default:
  88. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  89. }
  90. }
  91. /* get the first document */
  92. curl_easy_setopt(curl, CURLOPT_URL, url1);
  93. res = curl_easy_perform(curl);
  94. if ( res != 0 )
  95. {
  96. printf("Error fetching: %s\n", url1);
  97. retVal = 1;
  98. }
  99. /* get another document from the same server using the same
  100. connection */
  101. /* avoid warnings about url2 since below block is commented out: */
  102. (void) url2;
  103. /*
  104. curl_easy_setopt(curl, CURLOPT_URL, url2);
  105. res = curl_easy_perform(curl);
  106. if ( res != 0 )
  107. {
  108. printf("Error fetching: %s\n", url2);
  109. retVal = 1;
  110. }
  111. */
  112. /* always cleanup */
  113. curl_easy_cleanup(curl);
  114. }
  115. else
  116. {
  117. printf("Cannot create curl object\n");
  118. retVal = 1;
  119. }
  120. return retVal;
  121. }
  122. int main(int argc, char **argv)
  123. {
  124. int retVal = 0;
  125. curl_global_init(CURL_GLOBAL_DEFAULT);
  126. if(argc>1)
  127. {
  128. retVal += GetWebFiles(argv[1], 0);
  129. }
  130. else
  131. {
  132. printf("error: first argument should be a url to download\n");
  133. retVal = 1;
  134. }
  135. /* Do not check the output of FTP socks5 cannot handle FTP yet */
  136. /* GetFtpFile(); */
  137. /* do not test ftp right now because we don't enable that port */
  138. curl_global_cleanup();
  139. return retVal;
  140. }