gopher.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2019, 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.haxx.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 "progress.h"
  29. #include "gopher.h"
  30. #include "select.h"
  31. #include "strdup.h"
  32. #include "url.h"
  33. #include "escape.h"
  34. #include "warnless.h"
  35. #include "curl_printf.h"
  36. #include "curl_memory.h"
  37. /* The last #include file should be: */
  38. #include "memdebug.h"
  39. /*
  40. * Forward declarations.
  41. */
  42. static CURLcode gopher_do(struct connectdata *conn, bool *done);
  43. /*
  44. * Gopher protocol handler.
  45. * This is also a nice simple template to build off for simple
  46. * connect-command-download protocols.
  47. */
  48. const struct Curl_handler Curl_handler_gopher = {
  49. "GOPHER", /* scheme */
  50. ZERO_NULL, /* setup_connection */
  51. gopher_do, /* do_it */
  52. ZERO_NULL, /* done */
  53. ZERO_NULL, /* do_more */
  54. ZERO_NULL, /* connect_it */
  55. ZERO_NULL, /* connecting */
  56. ZERO_NULL, /* doing */
  57. ZERO_NULL, /* proto_getsock */
  58. ZERO_NULL, /* doing_getsock */
  59. ZERO_NULL, /* domore_getsock */
  60. ZERO_NULL, /* perform_getsock */
  61. ZERO_NULL, /* disconnect */
  62. ZERO_NULL, /* readwrite */
  63. ZERO_NULL, /* connection_check */
  64. PORT_GOPHER, /* defport */
  65. CURLPROTO_GOPHER, /* protocol */
  66. PROTOPT_NONE /* flags */
  67. };
  68. static CURLcode gopher_do(struct connectdata *conn, bool *done)
  69. {
  70. CURLcode result = CURLE_OK;
  71. struct Curl_easy *data = conn->data;
  72. curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
  73. char *gopherpath;
  74. char *path = data->state.up.path;
  75. char *query = data->state.up.query;
  76. char *sel = NULL;
  77. char *sel_org = NULL;
  78. ssize_t amount, k;
  79. size_t len;
  80. *done = TRUE; /* unconditionally */
  81. /* path is guaranteed non-NULL */
  82. DEBUGASSERT(path);
  83. if(query)
  84. gopherpath = aprintf("%s?%s", path, query);
  85. else
  86. gopherpath = strdup(path);
  87. if(!gopherpath)
  88. return CURLE_OUT_OF_MEMORY;
  89. /* Create selector. Degenerate cases: / and /1 => convert to "" */
  90. if(strlen(gopherpath) <= 2) {
  91. sel = (char *)"";
  92. len = strlen(sel);
  93. free(gopherpath);
  94. }
  95. else {
  96. char *newp;
  97. /* Otherwise, drop / and the first character (i.e., item type) ... */
  98. newp = gopherpath;
  99. newp += 2;
  100. /* ... and finally unescape */
  101. result = Curl_urldecode(data, newp, 0, &sel, &len, FALSE);
  102. free(gopherpath);
  103. if(result)
  104. return result;
  105. sel_org = sel;
  106. }
  107. /* We use Curl_write instead of Curl_sendf to make sure the entire buffer is
  108. sent, which could be sizeable with long selectors. */
  109. k = curlx_uztosz(len);
  110. for(;;) {
  111. result = Curl_write(conn, sockfd, sel, k, &amount);
  112. if(!result) { /* Which may not have written it all! */
  113. result = Curl_client_write(conn, CLIENTWRITE_HEADER, sel, amount);
  114. if(result)
  115. break;
  116. k -= amount;
  117. sel += amount;
  118. if(k < 1)
  119. break; /* but it did write it all */
  120. }
  121. else
  122. break;
  123. /* Don't busyloop. The entire loop thing is a work-around as it causes a
  124. BLOCKING behavior which is a NO-NO. This function should rather be
  125. split up in a do and a doing piece where the pieces that aren't
  126. possible to send now will be sent in the doing function repeatedly
  127. until the entire request is sent.
  128. Wait a while for the socket to be writable. Note that this doesn't
  129. acknowledge the timeout.
  130. */
  131. if(SOCKET_WRITABLE(sockfd, 100) < 0) {
  132. result = CURLE_SEND_ERROR;
  133. break;
  134. }
  135. }
  136. free(sel_org);
  137. if(!result)
  138. /* We can use Curl_sendf to send the terminal \r\n relatively safely and
  139. save allocing another string/doing another _write loop. */
  140. result = Curl_sendf(sockfd, conn, "\r\n");
  141. if(result) {
  142. failf(data, "Failed sending Gopher request");
  143. return result;
  144. }
  145. result = Curl_client_write(conn, CLIENTWRITE_HEADER, (char *)"\r\n", 2);
  146. if(result)
  147. return result;
  148. Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1);
  149. return CURLE_OK;
  150. }
  151. #endif /*CURL_DISABLE_GOPHER*/