gopher.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #ifndef CURL_DISABLE_GOPHER
  26. #include "urldata.h"
  27. #include <curl/curl.h>
  28. #include "transfer.h"
  29. #include "sendf.h"
  30. #include "connect.h"
  31. #include "progress.h"
  32. #include "gopher.h"
  33. #include "select.h"
  34. #include "strdup.h"
  35. #include "vtls/vtls.h"
  36. #include "url.h"
  37. #include "escape.h"
  38. #include "warnless.h"
  39. #include "curl_printf.h"
  40. #include "curl_memory.h"
  41. /* The last #include file should be: */
  42. #include "memdebug.h"
  43. /*
  44. * Forward declarations.
  45. */
  46. static CURLcode gopher_do(struct Curl_easy *data, bool *done);
  47. #ifdef USE_SSL
  48. static CURLcode gopher_connect(struct Curl_easy *data, bool *done);
  49. static CURLcode gopher_connecting(struct Curl_easy *data, bool *done);
  50. #endif
  51. /*
  52. * Gopher protocol handler.
  53. * This is also a nice simple template to build off for simple
  54. * connect-command-download protocols.
  55. */
  56. const struct Curl_handler Curl_handler_gopher = {
  57. "GOPHER", /* scheme */
  58. ZERO_NULL, /* setup_connection */
  59. gopher_do, /* do_it */
  60. ZERO_NULL, /* done */
  61. ZERO_NULL, /* do_more */
  62. ZERO_NULL, /* connect_it */
  63. ZERO_NULL, /* connecting */
  64. ZERO_NULL, /* doing */
  65. ZERO_NULL, /* proto_getsock */
  66. ZERO_NULL, /* doing_getsock */
  67. ZERO_NULL, /* domore_getsock */
  68. ZERO_NULL, /* perform_getsock */
  69. ZERO_NULL, /* disconnect */
  70. ZERO_NULL, /* readwrite */
  71. ZERO_NULL, /* connection_check */
  72. ZERO_NULL, /* attach connection */
  73. PORT_GOPHER, /* defport */
  74. CURLPROTO_GOPHER, /* protocol */
  75. CURLPROTO_GOPHER, /* family */
  76. PROTOPT_NONE /* flags */
  77. };
  78. #ifdef USE_SSL
  79. const struct Curl_handler Curl_handler_gophers = {
  80. "GOPHERS", /* scheme */
  81. ZERO_NULL, /* setup_connection */
  82. gopher_do, /* do_it */
  83. ZERO_NULL, /* done */
  84. ZERO_NULL, /* do_more */
  85. gopher_connect, /* connect_it */
  86. gopher_connecting, /* connecting */
  87. ZERO_NULL, /* doing */
  88. ZERO_NULL, /* proto_getsock */
  89. ZERO_NULL, /* doing_getsock */
  90. ZERO_NULL, /* domore_getsock */
  91. ZERO_NULL, /* perform_getsock */
  92. ZERO_NULL, /* disconnect */
  93. ZERO_NULL, /* readwrite */
  94. ZERO_NULL, /* connection_check */
  95. ZERO_NULL, /* attach connection */
  96. PORT_GOPHER, /* defport */
  97. CURLPROTO_GOPHERS, /* protocol */
  98. CURLPROTO_GOPHER, /* family */
  99. PROTOPT_SSL /* flags */
  100. };
  101. static CURLcode gopher_connect(struct Curl_easy *data, bool *done)
  102. {
  103. (void)data;
  104. (void)done;
  105. return CURLE_OK;
  106. }
  107. static CURLcode gopher_connecting(struct Curl_easy *data, bool *done)
  108. {
  109. struct connectdata *conn = data->conn;
  110. CURLcode result = Curl_ssl_connect(data, conn, FIRSTSOCKET);
  111. if(result)
  112. connclose(conn, "Failed TLS connection");
  113. *done = TRUE;
  114. return result;
  115. }
  116. #endif
  117. static CURLcode gopher_do(struct Curl_easy *data, bool *done)
  118. {
  119. CURLcode result = CURLE_OK;
  120. struct connectdata *conn = data->conn;
  121. curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
  122. char *gopherpath;
  123. char *path = data->state.up.path;
  124. char *query = data->state.up.query;
  125. char *sel = NULL;
  126. char *sel_org = NULL;
  127. timediff_t timeout_ms;
  128. ssize_t amount, k;
  129. size_t len;
  130. int what;
  131. *done = TRUE; /* unconditionally */
  132. /* path is guaranteed non-NULL */
  133. DEBUGASSERT(path);
  134. if(query)
  135. gopherpath = aprintf("%s?%s", path, query);
  136. else
  137. gopherpath = strdup(path);
  138. if(!gopherpath)
  139. return CURLE_OUT_OF_MEMORY;
  140. /* Create selector. Degenerate cases: / and /1 => convert to "" */
  141. if(strlen(gopherpath) <= 2) {
  142. sel = (char *)"";
  143. len = strlen(sel);
  144. free(gopherpath);
  145. }
  146. else {
  147. char *newp;
  148. /* Otherwise, drop / and the first character (i.e., item type) ... */
  149. newp = gopherpath;
  150. newp += 2;
  151. /* ... and finally unescape */
  152. result = Curl_urldecode(newp, 0, &sel, &len, REJECT_ZERO);
  153. free(gopherpath);
  154. if(result)
  155. return result;
  156. sel_org = sel;
  157. }
  158. k = curlx_uztosz(len);
  159. for(;;) {
  160. /* Break out of the loop if the selector is empty because OpenSSL and/or
  161. LibreSSL fail with errno 0 if this is the case. */
  162. if(strlen(sel) < 1)
  163. break;
  164. result = Curl_write(data, sockfd, sel, k, &amount);
  165. if(!result) { /* Which may not have written it all! */
  166. result = Curl_client_write(data, CLIENTWRITE_HEADER, sel, amount);
  167. if(result)
  168. break;
  169. k -= amount;
  170. sel += amount;
  171. if(k < 1)
  172. break; /* but it did write it all */
  173. }
  174. else
  175. break;
  176. timeout_ms = Curl_timeleft(data, NULL, FALSE);
  177. if(timeout_ms < 0) {
  178. result = CURLE_OPERATION_TIMEDOUT;
  179. break;
  180. }
  181. if(!timeout_ms)
  182. timeout_ms = TIMEDIFF_T_MAX;
  183. /* Don't busyloop. The entire loop thing is a work-around as it causes a
  184. BLOCKING behavior which is a NO-NO. This function should rather be
  185. split up in a do and a doing piece where the pieces that aren't
  186. possible to send now will be sent in the doing function repeatedly
  187. until the entire request is sent.
  188. */
  189. what = SOCKET_WRITABLE(sockfd, timeout_ms);
  190. if(what < 0) {
  191. result = CURLE_SEND_ERROR;
  192. break;
  193. }
  194. else if(!what) {
  195. result = CURLE_OPERATION_TIMEDOUT;
  196. break;
  197. }
  198. }
  199. free(sel_org);
  200. if(!result)
  201. result = Curl_write(data, sockfd, "\r\n", 2, &amount);
  202. if(result) {
  203. failf(data, "Failed sending Gopher request");
  204. return result;
  205. }
  206. result = Curl_client_write(data, CLIENTWRITE_HEADER, (char *)"\r\n", 2);
  207. if(result)
  208. return result;
  209. Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1);
  210. return CURLE_OK;
  211. }
  212. #endif /* CURL_DISABLE_GOPHER */