gopher.c 7.3 KB

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