gopher.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2015, 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 http://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 "strequal.h"
  30. #include "gopher.h"
  31. #include "rawstr.h"
  32. #include "select.h"
  33. #include "url.h"
  34. #include "warnless.h"
  35. #include "curl_memory.h"
  36. /* The last #include file should be: */
  37. #include "memdebug.h"
  38. /*
  39. * Forward declarations.
  40. */
  41. static CURLcode gopher_do(struct connectdata *conn, bool *done);
  42. /*
  43. * Gopher protocol handler.
  44. * This is also a nice simple template to build off for simple
  45. * connect-command-download protocols.
  46. */
  47. const struct Curl_handler Curl_handler_gopher = {
  48. "GOPHER", /* scheme */
  49. ZERO_NULL, /* setup_connection */
  50. gopher_do, /* do_it */
  51. ZERO_NULL, /* done */
  52. ZERO_NULL, /* do_more */
  53. ZERO_NULL, /* connect_it */
  54. ZERO_NULL, /* connecting */
  55. ZERO_NULL, /* doing */
  56. ZERO_NULL, /* proto_getsock */
  57. ZERO_NULL, /* doing_getsock */
  58. ZERO_NULL, /* domore_getsock */
  59. ZERO_NULL, /* perform_getsock */
  60. ZERO_NULL, /* disconnect */
  61. ZERO_NULL, /* readwrite */
  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 SessionHandle *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;
  74. char *sel_org = NULL;
  75. ssize_t amount, k;
  76. *done = TRUE; /* unconditionally */
  77. /* Create selector. Degenerate cases: / and /1 => convert to "" */
  78. if(strlen(path) <= 2)
  79. sel = (char *)"";
  80. else {
  81. char *newp;
  82. size_t j, i;
  83. int len;
  84. /* Otherwise, drop / and the first character (i.e., item type) ... */
  85. newp = path;
  86. newp+=2;
  87. /* ... then turn ? into TAB for search servers, Veronica, etc. ... */
  88. j = strlen(newp);
  89. for(i=0; i<j; i++)
  90. if(newp[i] == '?')
  91. newp[i] = '\x09';
  92. /* ... and finally unescape */
  93. sel = curl_easy_unescape(data, newp, 0, &len);
  94. if(!sel)
  95. return CURLE_OUT_OF_MEMORY;
  96. sel_org = sel;
  97. }
  98. /* We use Curl_write instead of Curl_sendf to make sure the entire buffer is
  99. sent, which could be sizeable with long selectors. */
  100. k = curlx_uztosz(strlen(sel));
  101. for(;;) {
  102. result = Curl_write(conn, sockfd, sel, k, &amount);
  103. if(!result) { /* Which may not have written it all! */
  104. result = Curl_client_write(conn, CLIENTWRITE_HEADER, sel, amount);
  105. if(result) {
  106. free(sel_org);
  107. return result;
  108. }
  109. k -= amount;
  110. sel += amount;
  111. if(k < 1)
  112. break; /* but it did write it all */
  113. }
  114. else {
  115. failf(data, "Failed sending Gopher request");
  116. free(sel_org);
  117. return result;
  118. }
  119. /* Don't busyloop. The entire loop thing is a work-around as it causes a
  120. BLOCKING behavior which is a NO-NO. This function should rather be
  121. split up in a do and a doing piece where the pieces that aren't
  122. possible to send now will be sent in the doing function repeatedly
  123. until the entire request is sent.
  124. Wait a while for the socket to be writable. Note that this doesn't
  125. acknowledge the timeout.
  126. */
  127. Curl_socket_ready(CURL_SOCKET_BAD, sockfd, 100);
  128. }
  129. free(sel_org);
  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*/