2
0

lib556.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. int 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. size_t iolen = 0;
  61. res = curl_easy_send(curl, request, strlen(request), &iolen);
  62. if(!res) {
  63. /* we assume that sending always work */
  64. do {
  65. char buf[1024];
  66. /* busy-read like crazy */
  67. res = curl_easy_recv(curl, buf, sizeof(buf), &iolen);
  68. if(iolen) {
  69. /* send received stuff to stdout */
  70. if(!write(STDOUT_FILENO, buf, iolen))
  71. break;
  72. }
  73. } while((res == CURLE_OK && iolen) || (res == CURLE_AGAIN));
  74. }
  75. if(iolen)
  76. res = (CURLcode)TEST_ERR_FAILURE;
  77. }
  78. test_cleanup:
  79. curl_easy_cleanup(curl);
  80. curl_global_cleanup();
  81. return (int)res;
  82. }