gopher.c 5.6 KB

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