2
0

gopher.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2014, 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 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. #define _MPRINTF_REPLACE /* use our functions only */
  36. #include <curl/mprintf.h>
  37. #include "curl_memory.h"
  38. /* The last #include file should be: */
  39. #include "memdebug.h"
  40. /*
  41. * Forward declarations.
  42. */
  43. static CURLcode gopher_do(struct connectdata *conn, bool *done);
  44. /*
  45. * Gopher protocol handler.
  46. * This is also a nice simple template to build off for simple
  47. * connect-command-download protocols.
  48. */
  49. const struct Curl_handler Curl_handler_gopher = {
  50. "GOPHER", /* scheme */
  51. ZERO_NULL, /* setup_connection */
  52. gopher_do, /* do_it */
  53. ZERO_NULL, /* done */
  54. ZERO_NULL, /* do_more */
  55. ZERO_NULL, /* connect_it */
  56. ZERO_NULL, /* connecting */
  57. ZERO_NULL, /* doing */
  58. ZERO_NULL, /* proto_getsock */
  59. ZERO_NULL, /* doing_getsock */
  60. ZERO_NULL, /* domore_getsock */
  61. ZERO_NULL, /* perform_getsock */
  62. ZERO_NULL, /* disconnect */
  63. ZERO_NULL, /* readwrite */
  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 SessionHandle *data=conn->data;
  72. curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
  73. curl_off_t *bytecount = &data->req.bytecount;
  74. char *path = data->state.path;
  75. char *sel;
  76. char *sel_org = NULL;
  77. ssize_t amount, k;
  78. *done = TRUE; /* unconditionally */
  79. /* Create selector. Degenerate cases: / and /1 => convert to "" */
  80. if(strlen(path) <= 2)
  81. sel = (char *)"";
  82. else {
  83. char *newp;
  84. size_t j, i;
  85. int len;
  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. sel = curl_easy_unescape(data, newp, 0, &len);
  96. if(!sel)
  97. return CURLE_OUT_OF_MEMORY;
  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(strlen(sel));
  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. Curl_safefree(sel_org);
  109. return result;
  110. }
  111. k -= amount;
  112. sel += amount;
  113. if(k < 1)
  114. break; /* but it did write it all */
  115. }
  116. else {
  117. failf(data, "Failed sending Gopher request");
  118. Curl_safefree(sel_org);
  119. return result;
  120. }
  121. /* Don't busyloop. The entire loop thing is a work-around as it causes a
  122. BLOCKING behavior which is a NO-NO. This function should rather be
  123. split up in a do and a doing piece where the pieces that aren't
  124. possible to send now will be sent in the doing function repeatedly
  125. until the entire request is sent.
  126. Wait a while for the socket to be writable. Note that this doesn't
  127. acknowledge the timeout.
  128. */
  129. Curl_socket_ready(CURL_SOCKET_BAD, sockfd, 100);
  130. }
  131. Curl_safefree(sel_org);
  132. /* We can use Curl_sendf to send the terminal \r\n relatively safely and
  133. save allocing another string/doing another _write loop. */
  134. result = Curl_sendf(sockfd, conn, "\r\n");
  135. if(result) {
  136. failf(data, "Failed sending Gopher request");
  137. return result;
  138. }
  139. result = Curl_client_write(conn, CLIENTWRITE_HEADER, (char *)"\r\n", 2);
  140. if(result)
  141. return result;
  142. Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
  143. -1, NULL); /* no upload */
  144. return CURLE_OK;
  145. }
  146. #endif /*CURL_DISABLE_GOPHER*/