gopher.c 7.4 KB

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