gopher.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2016, Daniel Stenberg, <[email protected]>, 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. PORT_GOPHER, /* defport */
  62. CURLPROTO_GOPHER, /* protocol */
  63. PROTOPT_NONE /* flags */
  64. };
  65. static CURLcode gopher_do(struct connectdata *conn, bool *done)
  66. {
  67. CURLcode result=CURLE_OK;
  68. struct Curl_easy *data=conn->data;
  69. curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
  70. curl_off_t *bytecount = &data->req.bytecount;
  71. char *path = data->state.path;
  72. char *sel;
  73. char *sel_org = NULL;
  74. ssize_t amount, k;
  75. size_t len;
  76. *done = TRUE; /* unconditionally */
  77. /* Create selector. Degenerate cases: / and /1 => convert to "" */
  78. if(strlen(path) <= 2) {
  79. sel = (char *)"";
  80. len = (int)strlen(sel);
  81. }
  82. else {
  83. char *newp;
  84. size_t j, i;
  85. /* Otherwise, drop / and the first character (i.e., item type) ... */
  86. newp = path;
  87. newp+=2;
  88. /* ... then turn ? into TAB for search servers, Veronica, etc. ... */
  89. j = strlen(newp);
  90. for(i=0; i<j; i++)
  91. if(newp[i] == '?')
  92. newp[i] = '\x09';
  93. /* ... and finally unescape */
  94. result = Curl_urldecode(data, newp, 0, &sel, &len, FALSE);
  95. if(!sel)
  96. return CURLE_OUT_OF_MEMORY;
  97. sel_org = sel;
  98. }
  99. /* We use Curl_write instead of Curl_sendf to make sure the entire buffer is
  100. sent, which could be sizeable with long selectors. */
  101. k = curlx_uztosz(len);
  102. for(;;) {
  103. result = Curl_write(conn, sockfd, sel, k, &amount);
  104. if(!result) { /* Which may not have written it all! */
  105. result = Curl_client_write(conn, CLIENTWRITE_HEADER, sel, amount);
  106. if(result)
  107. break;
  108. k -= amount;
  109. sel += amount;
  110. if(k < 1)
  111. break; /* but it did write it all */
  112. }
  113. else
  114. break;
  115. /* Don't busyloop. The entire loop thing is a work-around as it causes a
  116. BLOCKING behavior which is a NO-NO. This function should rather be
  117. split up in a do and a doing piece where the pieces that aren't
  118. possible to send now will be sent in the doing function repeatedly
  119. until the entire request is sent.
  120. Wait a while for the socket to be writable. Note that this doesn't
  121. acknowledge the timeout.
  122. */
  123. if(SOCKET_WRITABLE(sockfd, 100) < 0) {
  124. result = CURLE_SEND_ERROR;
  125. break;
  126. }
  127. }
  128. free(sel_org);
  129. if(!result)
  130. /* We can use Curl_sendf to send the terminal \r\n relatively safely and
  131. save allocing another string/doing another _write loop. */
  132. result = Curl_sendf(sockfd, conn, "\r\n");
  133. if(result) {
  134. failf(data, "Failed sending Gopher request");
  135. return result;
  136. }
  137. result = Curl_client_write(conn, CLIENTWRITE_HEADER, (char *)"\r\n", 2);
  138. if(result)
  139. return result;
  140. Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
  141. -1, NULL); /* no upload */
  142. return CURLE_OK;
  143. }
  144. #endif /*CURL_DISABLE_GOPHER*/