2
0

lib677.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 cmd[] = "A1 IDLE\r\n";
  29. static char buf[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, 500,
  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, cmd + pos, sizeof(cmd) - 1 - pos, &len);
  76. if(ec != CURLE_OK) {
  77. fprintf(stderr, "curl_easy_send() failed, with code %d (%s)\n",
  78. (int)ec, curl_easy_strerror(ec));
  79. res = ec;
  80. goto test_cleanup;
  81. }
  82. if(len > 0)
  83. pos += len;
  84. else
  85. pos = 0;
  86. if(pos == sizeof(cmd) - 1) {
  87. state++;
  88. pos = 0;
  89. }
  90. }
  91. else if(pos < (ssize_t)sizeof(buf)) {
  92. CURLcode ec;
  93. ec = curl_easy_recv(curl, buf + pos, sizeof(buf) - pos, &len);
  94. if(ec != CURLE_OK) {
  95. fprintf(stderr, "curl_easy_recv() failed, with code %d (%s)\n",
  96. (int)ec, curl_easy_strerror(ec));
  97. res = ec;
  98. goto test_cleanup;
  99. }
  100. if(len > 0)
  101. pos += len;
  102. }
  103. if(len <= 0)
  104. sock = CURL_SOCKET_BAD;
  105. }
  106. }
  107. if(state) {
  108. fwrite(buf, pos, 1, stdout);
  109. putchar('\n');
  110. }
  111. curl_multi_remove_handle(mcurl, curl);
  112. test_cleanup:
  113. curl_easy_cleanup(curl);
  114. curl_multi_cleanup(mcurl);
  115. curl_global_cleanup();
  116. return res;
  117. }