gopher.c 7.2 KB

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