Advertisement
Guest User

Untitled

a guest
May 29th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. curl_easy_setopt(ctx,CURLOPT_NOBODY ,1 );
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <curl/curl.h>
  6.  
  7. /* Auxiliary function that waits on the socket. */
  8. static int wait_on_socket(curl_socket_t sockfd, int for_recv, long timeout_ms)
  9. {
  10. struct timeval tv;
  11. fd_set infd, outfd, errfd;
  12. int res;
  13.  
  14. tv.tv_sec = timeout_ms / 1000;
  15. tv.tv_usec= (timeout_ms % 1000) * 1000;
  16.  
  17. FD_ZERO(&infd);
  18. FD_ZERO(&outfd);
  19. FD_ZERO(&errfd);
  20.  
  21. FD_SET(sockfd, &errfd); /* always check for error */
  22.  
  23. if(for_recv)
  24. {
  25. FD_SET(sockfd, &infd);
  26. }
  27. else
  28. {
  29. FD_SET(sockfd, &outfd);
  30. }
  31.  
  32. /* select() returns the number of signalled sockets or -1 */
  33. res = select(sockfd + 1, &infd, &outfd, &errfd, &tv);
  34. return res;
  35. }
  36.  
  37. int main(void)
  38. {
  39. CURL *curl;
  40. CURLcode res;
  41. /* Minimalistic http request */
  42. const char *request = "GET / HTTP/1.0rnHost: m0g.netrnrn";
  43. curl_socket_t sockfd; /* socket */
  44. long sockextr;
  45. size_t iolen;
  46. curl_off_t nread;
  47.  
  48. curl = curl_easy_init();
  49. if(curl) {
  50. curl_easy_setopt(curl, CURLOPT_URL, "http://m0g.net");
  51. /* Do not do the transfer - only connect to host */
  52. curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
  53. res = curl_easy_perform(curl);
  54.  
  55. if(CURLE_OK != res)
  56. {
  57. printf("Error: %sn", strerror(res));
  58. return 1;
  59. }
  60.  
  61. /* Extract the socket from the curl handle - we'll need it for waiting.
  62. * Note that this API takes a pointer to a 'long' while we use
  63. * curl_socket_t for sockets otherwise.
  64. */
  65. res = curl_easy_getinfo(curl, CURLINFO_LASTSOCKET, &sockextr);
  66.  
  67. if(CURLE_OK != res)
  68. {
  69. printf("Error: %sn", curl_easy_strerror(res));
  70. return 1;
  71. }
  72.  
  73. sockfd = sockextr;
  74.  
  75. /* wait for the socket to become ready for sending */
  76. if(!wait_on_socket(sockfd, 0, 60000L))
  77. {
  78. printf("Error: timeout.n");
  79. return 1;
  80. }
  81.  
  82. puts("Sending request.");
  83. /* Send the request. Real applications should check the iolen
  84. * to see if all the request has been sent */
  85. res = curl_easy_send(curl, request, strlen(request), &iolen);
  86.  
  87. if(CURLE_OK != res)
  88. {
  89. printf("Error: %sn", curl_easy_strerror(res));
  90. return 1;
  91. }
  92. puts("Reading response.");
  93. char data[2048];
  94. int idxread=0;
  95.  
  96. /* read the response */
  97. for(;;)
  98. {
  99. char buf[1024];
  100.  
  101. wait_on_socket(sockfd, 1, 60000L);
  102. res = curl_easy_recv(curl, buf, 32, &iolen);
  103.  
  104. if(CURLE_OK != res)
  105. break;
  106.  
  107. if (nread+idxread > 2048)
  108. break;
  109.  
  110. strncpy(data+idxread,buf,nread);
  111. idxread+=nread;
  112.  
  113. if (strstr(data,"rnrn") != NULL) {
  114. if (strstr(data,"Content-Type: text/html") == NULL) {
  115. printf("not an html document.");
  116. return 2;
  117. }
  118. }
  119.  
  120. nread = (curl_off_t)iolen;
  121.  
  122. printf("Received %" CURL_FORMAT_CURL_OFF_T " bytes.n", nread);
  123. }
  124. printf("'''%s'''n", data);
  125.  
  126. /* always cleanup */
  127. curl_easy_cleanup(curl);
  128. }
  129. return 0;
  130. }
  131.  
  132. #include <curl/curl.h>
  133. int main(int argc, char *argv[])
  134. {
  135. CURLcode ret;
  136. CURL *hnd = curl_easy_init();
  137. curl_easy_setopt(hnd, CURLOPT_URL, "http://www.haxx.se");
  138. curl_easy_setopt(hnd, CURLOPT_HEADER, 1);
  139. curl_easy_setopt(hnd, CURLOPT_NOBODY, 1);
  140. ret = curl_easy_perform(hnd);
  141. curl_easy_cleanup(hnd);
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement