curltest.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include "curl/curl.h"
  2. #include <stdlib.h>
  3. int GetFtpFile(void)
  4. {
  5. int retVal = 0;
  6. CURL *curl;
  7. CURLcode res;
  8. curl = curl_easy_init();
  9. if(curl)
  10. {
  11. /* Get curl 7.9.2 from sunet.se's FTP site: */
  12. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  13. curl_easy_setopt(curl, CURLOPT_HEADER, 1);
  14. curl_easy_setopt(curl, CURLOPT_URL,
  15. "ftp://public.kitware.com/pub/cmake/cygwin/setup.hint");
  16. res = curl_easy_perform(curl);
  17. if ( res != 0 )
  18. {
  19. printf("Error fetching: ftp://public.kitware.com/pub/cmake/cygwin/setup.hint\n");
  20. retVal = 1;
  21. }
  22. /* always cleanup */
  23. curl_easy_cleanup(curl);
  24. }
  25. else
  26. {
  27. printf("Cannot create curl object\n");
  28. retVal = 1;
  29. }
  30. return retVal;
  31. }
  32. int GetWebFile(void)
  33. {
  34. int retVal = 0;
  35. CURL *curl;
  36. CURLcode res;
  37. char proxy[1024];
  38. int proxy_type = 0;
  39. if ( getenv("HTTP_PROXY") )
  40. {
  41. proxy_type = 1;
  42. if (getenv("HTTP_PROXY_PORT") )
  43. {
  44. sprintf(proxy, "%s:%s", getenv("HTTP_PROXY"), getenv("HTTP_PROXY_PORT"));
  45. }
  46. else
  47. {
  48. sprintf(proxy, "%s", getenv("HTTP_PROXY"));
  49. }
  50. if ( getenv("HTTP_PROXY_TYPE") )
  51. {
  52. /* HTTP/SOCKS4/SOCKS5 */
  53. if ( strcmp(getenv("HTTP_PROXY_TYPE"), "HTTP") == 0 )
  54. {
  55. proxy_type = 1;
  56. }
  57. else if ( strcmp(getenv("HTTP_PROXY_TYPE"), "SOCKS4") == 0 )
  58. {
  59. proxy_type = 2;
  60. }
  61. else if ( strcmp(getenv("HTTP_PROXY_TYPE"), "SOCKS5") == 0 )
  62. {
  63. proxy_type = 3;
  64. }
  65. }
  66. }
  67. curl = curl_easy_init();
  68. if(curl)
  69. {
  70. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  71. curl_easy_setopt(curl, CURLOPT_HEADER, 1);
  72. /* Using proxy */
  73. if ( proxy_type > 0 )
  74. {
  75. curl_easy_setopt(curl, CURLOPT_PROXY, proxy);
  76. switch (proxy_type)
  77. {
  78. case 2:
  79. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
  80. break;
  81. case 3:
  82. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
  83. break;
  84. default:
  85. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  86. }
  87. }
  88. /* get the first document */
  89. curl_easy_setopt(curl, CURLOPT_URL, "http://www.cmake.org/page1.html");
  90. res = curl_easy_perform(curl);
  91. if ( res != 0 )
  92. {
  93. printf("Error fetching: http://www.cmake.org/page1.html\n");
  94. retVal = 1;
  95. }
  96. /* get another document from the same server using the same
  97. connection */
  98. /*
  99. curl_easy_setopt(curl, CURLOPT_URL, "http://www.cmake.org/page2.html");
  100. res = curl_easy_perform(curl);
  101. if ( res != 0 )
  102. {
  103. printf("Error fetching: http://www.cmake.org/page2.html\n");
  104. retVal = 1;
  105. }
  106. */
  107. /* always cleanup */
  108. curl_easy_cleanup(curl);
  109. }
  110. else
  111. {
  112. printf("Cannot create curl object\n");
  113. retVal = 1;
  114. }
  115. return retVal;
  116. }
  117. int main(int argc, char **argv)
  118. {
  119. int retVal = 0;
  120. curl_global_init(CURL_GLOBAL_DEFAULT);
  121. retVal += GetWebFile();
  122. /* Do not check the output of FTP socks5 cannot handle FTP yet */
  123. GetFtpFile();
  124. curl_global_cleanup();
  125. return retVal;
  126. }