2
0

lib556.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 "warnless.h"
  26. #include "memdebug.h"
  27. /* For Windows, mainly (may be moved in a config file?) */
  28. #ifndef STDIN_FILENO
  29. #define STDIN_FILENO 0
  30. #endif
  31. #ifndef STDOUT_FILENO
  32. #define STDOUT_FILENO 1
  33. #endif
  34. #ifndef STDERR_FILENO
  35. #define STDERR_FILENO 2
  36. #endif
  37. CURLcode test(char *URL)
  38. {
  39. CURLcode res;
  40. CURL *curl;
  41. if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  42. fprintf(stderr, "curl_global_init() failed\n");
  43. return TEST_ERR_MAJOR_BAD;
  44. }
  45. curl = curl_easy_init();
  46. if(!curl) {
  47. fprintf(stderr, "curl_easy_init() failed\n");
  48. curl_global_cleanup();
  49. return TEST_ERR_MAJOR_BAD;
  50. }
  51. test_setopt(curl, CURLOPT_URL, URL);
  52. test_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
  53. test_setopt(curl, CURLOPT_VERBOSE, 1L);
  54. res = curl_easy_perform(curl);
  55. if(!res) {
  56. /* we are connected, now get an HTTP document the raw way */
  57. const char *request =
  58. "GET /556 HTTP/1.1\r\n"
  59. "Host: ninja\r\n\r\n";
  60. const char *sbuf = request;
  61. size_t sblen = strlen(request);
  62. size_t nwritten = 0, nread = 0;
  63. do {
  64. char buf[1024];
  65. if(sblen) {
  66. res = curl_easy_send(curl, sbuf, sblen, &nwritten);
  67. if(res && res != CURLE_AGAIN)
  68. break;
  69. if(nwritten > 0) {
  70. sbuf += nwritten;
  71. sblen -= nwritten;
  72. }
  73. }
  74. /* busy-read like crazy */
  75. res = curl_easy_recv(curl, buf, sizeof(buf), &nread);
  76. if(nread) {
  77. /* send received stuff to stdout */
  78. if(!write(STDOUT_FILENO, buf, nread))
  79. break;
  80. }
  81. } while((res == CURLE_OK && nread) || (res == CURLE_AGAIN));
  82. if(res && res != CURLE_AGAIN)
  83. res = TEST_ERR_FAILURE;
  84. }
  85. test_cleanup:
  86. curl_easy_cleanup(curl);
  87. curl_global_cleanup();
  88. return res;
  89. }