gopher.c 5.2 KB

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