gopher.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2018, 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.haxx.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 "curl_setup.h"
  23. #ifndef CURL_DISABLE_GOPHER
  24. #include "urldata.h"
  25. #include <curl/curl.h>
  26. #include "transfer.h"
  27. #include "sendf.h"
  28. #include "progress.h"
  29. #include "gopher.h"
  30. #include "select.h"
  31. #include "url.h"
  32. #include "escape.h"
  33. #include "warnless.h"
  34. #include "curl_memory.h"
  35. /* The last #include file should be: */
  36. #include "memdebug.h"
  37. /*
  38. * Forward declarations.
  39. */
  40. static CURLcode gopher_do(struct connectdata *conn, bool *done);
  41. /*
  42. * Gopher protocol handler.
  43. * This is also a nice simple template to build off for simple
  44. * connect-command-download protocols.
  45. */
  46. const struct Curl_handler Curl_handler_gopher = {
  47. "GOPHER", /* scheme */
  48. ZERO_NULL, /* setup_connection */
  49. gopher_do, /* do_it */
  50. ZERO_NULL, /* done */
  51. ZERO_NULL, /* do_more */
  52. ZERO_NULL, /* connect_it */
  53. ZERO_NULL, /* connecting */
  54. ZERO_NULL, /* doing */
  55. ZERO_NULL, /* proto_getsock */
  56. ZERO_NULL, /* doing_getsock */
  57. ZERO_NULL, /* domore_getsock */
  58. ZERO_NULL, /* perform_getsock */
  59. ZERO_NULL, /* disconnect */
  60. ZERO_NULL, /* readwrite */
  61. ZERO_NULL, /* connection_check */
  62. PORT_GOPHER, /* defport */
  63. CURLPROTO_GOPHER, /* protocol */
  64. PROTOPT_NONE /* flags */
  65. };
  66. static CURLcode gopher_do(struct connectdata *conn, bool *done)
  67. {
  68. CURLcode result = CURLE_OK;
  69. struct Curl_easy *data = conn->data;
  70. curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
  71. curl_off_t *bytecount = &data->req.bytecount;
  72. char *path = data->state.path;
  73. char *sel = NULL;
  74. char *sel_org = NULL;
  75. ssize_t amount, k;
  76. size_t len;
  77. *done = TRUE; /* unconditionally */
  78. /* Create selector. Degenerate cases: / and /1 => convert to "" */
  79. if(strlen(path) <= 2) {
  80. sel = (char *)"";
  81. len = strlen(sel);
  82. }
  83. else {
  84. char *newp;
  85. /* Otherwise, drop / and the first character (i.e., item type) ... */
  86. newp = path;
  87. newp += 2;
  88. /* ... and finally unescape */
  89. result = Curl_urldecode(data, newp, 0, &sel, &len, FALSE);
  90. if(result)
  91. return result;
  92. sel_org = sel;
  93. }
  94. /* We use Curl_write instead of Curl_sendf to make sure the entire buffer is
  95. sent, which could be sizeable with long selectors. */
  96. k = curlx_uztosz(len);
  97. for(;;) {
  98. result = Curl_write(conn, sockfd, sel, k, &amount);
  99. if(!result) { /* Which may not have written it all! */
  100. result = Curl_client_write(conn, CLIENTWRITE_HEADER, sel, amount);
  101. if(result)
  102. break;
  103. k -= amount;
  104. sel += amount;
  105. if(k < 1)
  106. break; /* but it did write it all */
  107. }
  108. else
  109. break;
  110. /* Don't busyloop. The entire loop thing is a work-around as it causes a
  111. BLOCKING behavior which is a NO-NO. This function should rather be
  112. split up in a do and a doing piece where the pieces that aren't
  113. possible to send now will be sent in the doing function repeatedly
  114. until the entire request is sent.
  115. Wait a while for the socket to be writable. Note that this doesn't
  116. acknowledge the timeout.
  117. */
  118. if(SOCKET_WRITABLE(sockfd, 100) < 0) {
  119. result = CURLE_SEND_ERROR;
  120. break;
  121. }
  122. }
  123. free(sel_org);
  124. if(!result)
  125. /* We can use Curl_sendf to send the terminal \r\n relatively safely and
  126. save allocing another string/doing another _write loop. */
  127. result = Curl_sendf(sockfd, conn, "\r\n");
  128. if(result) {
  129. failf(data, "Failed sending Gopher request");
  130. return result;
  131. }
  132. result = Curl_client_write(conn, CLIENTWRITE_HEADER, (char *)"\r\n", 2);
  133. if(result)
  134. return result;
  135. Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
  136. -1, NULL); /* no upload */
  137. return CURLE_OK;
  138. }
  139. #endif /*CURL_DISABLE_GOPHER*/