lib556.c 2.6 KB

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