gopher.c 5.7 KB

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