1
0

curltest.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 GetWebFile(void)
  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, "http://www.cmake.org/page1.html");
  93. res = curl_easy_perform(curl);
  94. if ( res != 0 )
  95. {
  96. printf("Error fetching: http://www.cmake.org/page1.html\n");
  97. retVal = 1;
  98. }
  99. /* get another document from the same server using the same
  100. connection */
  101. /*
  102. curl_easy_setopt(curl, CURLOPT_URL, "http://www.cmake.org/page2.html");
  103. res = curl_easy_perform(curl);
  104. if ( res != 0 )
  105. {
  106. printf("Error fetching: http://www.cmake.org/page2.html\n");
  107. retVal = 1;
  108. }
  109. */
  110. /* always cleanup */
  111. curl_easy_cleanup(curl);
  112. }
  113. else
  114. {
  115. printf("Cannot create curl object\n");
  116. retVal = 1;
  117. }
  118. return retVal;
  119. }
  120. int main(/*int argc, char **argv*/)
  121. {
  122. int retVal = 0;
  123. curl_global_init(CURL_GLOBAL_DEFAULT);
  124. retVal += GetWebFile();
  125. /* Do not check the output of FTP socks5 cannot handle FTP yet */
  126. GetFtpFile();
  127. curl_global_cleanup();
  128. return retVal;
  129. }