2
0

lib677.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "test.h"
  25. #include "testutil.h"
  26. #include "warnless.h"
  27. #include "memdebug.h"
  28. static const char testcmd[] = "A1 IDLE\r\n";
  29. static char testbuf[1024];
  30. CURLcode test(char *URL)
  31. {
  32. CURLM *mcurl;
  33. CURL *curl = NULL;
  34. int mrun;
  35. curl_socket_t sock = CURL_SOCKET_BAD;
  36. time_t start = time(NULL);
  37. int state = 0;
  38. ssize_t pos = 0;
  39. CURLcode res = CURLE_OK;
  40. global_init(CURL_GLOBAL_DEFAULT);
  41. multi_init(mcurl);
  42. easy_init(curl);
  43. easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  44. easy_setopt(curl, CURLOPT_URL, URL);
  45. easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
  46. if(curl_multi_add_handle(mcurl, curl))
  47. goto test_cleanup;
  48. while(time(NULL) - start < 5) {
  49. struct curl_waitfd waitfd;
  50. multi_perform(mcurl, &mrun);
  51. for(;;) {
  52. int i;
  53. struct CURLMsg *m = curl_multi_info_read(mcurl, &i);
  54. if(!m)
  55. break;
  56. if(m->msg == CURLMSG_DONE && m->easy_handle == curl) {
  57. curl_easy_getinfo(curl, CURLINFO_ACTIVESOCKET, &sock);
  58. if(sock == CURL_SOCKET_BAD)
  59. goto test_cleanup;
  60. printf("Connected fine, extracted socket. Moving on\n");
  61. }
  62. }
  63. if(sock != CURL_SOCKET_BAD) {
  64. waitfd.events = state ? CURL_WAIT_POLLIN : CURL_WAIT_POLLOUT;
  65. waitfd.revents = 0;
  66. curl_easy_getinfo(curl, CURLINFO_ACTIVESOCKET, &sock);
  67. waitfd.fd = sock;
  68. }
  69. curl_multi_wait(mcurl, &waitfd, sock == CURL_SOCKET_BAD ? 0 : 1, 50,
  70. &mrun);
  71. if((sock != CURL_SOCKET_BAD) && (waitfd.revents & waitfd.events)) {
  72. size_t len = 0;
  73. if(!state) {
  74. CURLcode ec;
  75. ec = curl_easy_send(curl, testcmd + pos,
  76. sizeof(testcmd) - 1 - pos, &len);
  77. if(ec == CURLE_AGAIN) {
  78. continue;
  79. }
  80. else if(ec) {
  81. fprintf(stderr, "curl_easy_send() failed, with code %d (%s)\n",
  82. (int)ec, curl_easy_strerror(ec));
  83. res = ec;
  84. goto test_cleanup;
  85. }
  86. if(len > 0)
  87. pos += len;
  88. else
  89. pos = 0;
  90. if(pos == sizeof(testcmd) - 1) {
  91. state++;
  92. pos = 0;
  93. }
  94. }
  95. else if(pos < (ssize_t)sizeof(testbuf)) {
  96. CURLcode ec;
  97. ec = curl_easy_recv(curl, testbuf + pos, sizeof(testbuf) - pos, &len);
  98. if(ec == CURLE_AGAIN) {
  99. continue;
  100. }
  101. else if(ec) {
  102. fprintf(stderr, "curl_easy_recv() failed, with code %d (%s)\n",
  103. (int)ec, curl_easy_strerror(ec));
  104. res = ec;
  105. goto test_cleanup;
  106. }
  107. if(len > 0)
  108. pos += len;
  109. }
  110. if(len <= 0)
  111. sock = CURL_SOCKET_BAD;
  112. }
  113. }
  114. if(state) {
  115. fwrite(testbuf, pos, 1, stdout);
  116. putchar('\n');
  117. }
  118. curl_multi_remove_handle(mcurl, curl);
  119. test_cleanup:
  120. curl_easy_cleanup(curl);
  121. curl_multi_cleanup(mcurl);
  122. curl_global_cleanup();
  123. return res;
  124. }