gopher.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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, /* write_resp */
  72. ZERO_NULL, /* write_resp_hd */
  73. ZERO_NULL, /* connection_check */
  74. ZERO_NULL, /* attach connection */
  75. PORT_GOPHER, /* defport */
  76. CURLPROTO_GOPHER, /* protocol */
  77. CURLPROTO_GOPHER, /* family */
  78. PROTOPT_NONE /* flags */
  79. };
  80. #ifdef USE_SSL
  81. const struct Curl_handler Curl_handler_gophers = {
  82. "gophers", /* scheme */
  83. ZERO_NULL, /* setup_connection */
  84. gopher_do, /* do_it */
  85. ZERO_NULL, /* done */
  86. ZERO_NULL, /* do_more */
  87. gopher_connect, /* connect_it */
  88. gopher_connecting, /* connecting */
  89. ZERO_NULL, /* doing */
  90. ZERO_NULL, /* proto_getsock */
  91. ZERO_NULL, /* doing_getsock */
  92. ZERO_NULL, /* domore_getsock */
  93. ZERO_NULL, /* perform_getsock */
  94. ZERO_NULL, /* disconnect */
  95. ZERO_NULL, /* write_resp */
  96. ZERO_NULL, /* write_resp_hd */
  97. ZERO_NULL, /* connection_check */
  98. ZERO_NULL, /* attach connection */
  99. PORT_GOPHER, /* defport */
  100. CURLPROTO_GOPHERS, /* protocol */
  101. CURLPROTO_GOPHER, /* family */
  102. PROTOPT_SSL /* flags */
  103. };
  104. static CURLcode gopher_connect(struct Curl_easy *data, bool *done)
  105. {
  106. (void)data;
  107. (void)done;
  108. return CURLE_OK;
  109. }
  110. static CURLcode gopher_connecting(struct Curl_easy *data, bool *done)
  111. {
  112. struct connectdata *conn = data->conn;
  113. CURLcode result;
  114. result = Curl_conn_connect(data, FIRSTSOCKET, TRUE, done);
  115. if(result)
  116. connclose(conn, "Failed TLS connection");
  117. *done = TRUE;
  118. return result;
  119. }
  120. #endif
  121. static CURLcode gopher_do(struct Curl_easy *data, bool *done)
  122. {
  123. CURLcode result = CURLE_OK;
  124. struct connectdata *conn = data->conn;
  125. curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
  126. char *gopherpath;
  127. char *path = data->state.up.path;
  128. char *query = data->state.up.query;
  129. char *sel = NULL;
  130. char *sel_org = NULL;
  131. timediff_t timeout_ms;
  132. ssize_t k;
  133. size_t amount, len;
  134. int what;
  135. *done = TRUE; /* unconditionally */
  136. /* path is guaranteed non-NULL */
  137. DEBUGASSERT(path);
  138. if(query)
  139. gopherpath = aprintf("%s?%s", path, query);
  140. else
  141. gopherpath = strdup(path);
  142. if(!gopherpath)
  143. return CURLE_OUT_OF_MEMORY;
  144. /* Create selector. Degenerate cases: / and /1 => convert to "" */
  145. if(strlen(gopherpath) <= 2) {
  146. sel = (char *)"";
  147. len = strlen(sel);
  148. free(gopherpath);
  149. }
  150. else {
  151. char *newp;
  152. /* Otherwise, drop / and the first character (i.e., item type) ... */
  153. newp = gopherpath;
  154. newp += 2;
  155. /* ... and finally unescape */
  156. result = Curl_urldecode(newp, 0, &sel, &len, REJECT_ZERO);
  157. free(gopherpath);
  158. if(result)
  159. return result;
  160. sel_org = sel;
  161. }
  162. k = curlx_uztosz(len);
  163. for(;;) {
  164. /* Break out of the loop if the selector is empty because OpenSSL and/or
  165. LibreSSL fail with errno 0 if this is the case. */
  166. if(strlen(sel) < 1)
  167. break;
  168. result = Curl_xfer_send(data, sel, k, FALSE, &amount);
  169. if(!result) { /* Which may not have written it all! */
  170. result = Curl_client_write(data, CLIENTWRITE_HEADER, sel, amount);
  171. if(result)
  172. break;
  173. k -= amount;
  174. sel += amount;
  175. if(k < 1)
  176. break; /* but it did write it all */
  177. }
  178. else
  179. break;
  180. timeout_ms = Curl_timeleft(data, NULL, FALSE);
  181. if(timeout_ms < 0) {
  182. result = CURLE_OPERATION_TIMEDOUT;
  183. break;
  184. }
  185. if(!timeout_ms)
  186. timeout_ms = TIMEDIFF_T_MAX;
  187. /* Do not busyloop. The entire loop thing is a work-around as it causes a
  188. BLOCKING behavior which is a NO-NO. This function should rather be
  189. split up in a do and a doing piece where the pieces that are not
  190. possible to send now will be sent in the doing function repeatedly
  191. until the entire request is sent.
  192. */
  193. what = SOCKET_WRITABLE(sockfd, timeout_ms);
  194. if(what < 0) {
  195. result = CURLE_SEND_ERROR;
  196. break;
  197. }
  198. else if(!what) {
  199. result = CURLE_OPERATION_TIMEDOUT;
  200. break;
  201. }
  202. }
  203. free(sel_org);
  204. if(!result)
  205. result = Curl_xfer_send(data, "\r\n", 2, FALSE, &amount);
  206. if(result) {
  207. failf(data, "Failed sending Gopher request");
  208. return result;
  209. }
  210. result = Curl_client_write(data, CLIENTWRITE_HEADER, (char *)"\r\n", 2);
  211. if(result)
  212. return result;
  213. Curl_xfer_setup1(data, CURL_XFER_RECV, -1, FALSE);
  214. return CURLE_OK;
  215. }
  216. #endif /* CURL_DISABLE_GOPHER */