gopher.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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, /* domore_getsock */
  87. ZERO_NULL, /* perform_getsock */
  88. ZERO_NULL, /* disconnect */
  89. ZERO_NULL, /* readwrite */
  90. PORT_GOPHER, /* defport */
  91. CURLPROTO_GOPHER, /* protocol */
  92. PROTOPT_NONE /* flags */
  93. };
  94. static CURLcode gopher_do(struct connectdata *conn, bool *done)
  95. {
  96. CURLcode result=CURLE_OK;
  97. struct SessionHandle *data=conn->data;
  98. curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
  99. curl_off_t *bytecount = &data->req.bytecount;
  100. char *path = data->state.path;
  101. char *sel;
  102. char *sel_org = NULL;
  103. ssize_t amount, k;
  104. *done = TRUE; /* unconditionally */
  105. /* Create selector. Degenerate cases: / and /1 => convert to "" */
  106. if(strlen(path) <= 2)
  107. sel = (char *)"";
  108. else {
  109. char *newp;
  110. size_t j, i;
  111. int len;
  112. /* Otherwise, drop / and the first character (i.e., item type) ... */
  113. newp = path;
  114. newp+=2;
  115. /* ... then turn ? into TAB for search servers, Veronica, etc. ... */
  116. j = strlen(newp);
  117. for(i=0; i<j; i++)
  118. if(newp[i] == '?')
  119. newp[i] = '\x09';
  120. /* ... and finally unescape */
  121. sel = curl_easy_unescape(data, newp, 0, &len);
  122. if(!sel)
  123. return CURLE_OUT_OF_MEMORY;
  124. sel_org = sel;
  125. }
  126. /* We use Curl_write instead of Curl_sendf to make sure the entire buffer is
  127. sent, which could be sizeable with long selectors. */
  128. k = curlx_uztosz(strlen(sel));
  129. for(;;) {
  130. result = Curl_write(conn, sockfd, sel, k, &amount);
  131. if(CURLE_OK == result) { /* Which may not have written it all! */
  132. result = Curl_client_write(conn, CLIENTWRITE_HEADER, sel, amount);
  133. if(result) {
  134. Curl_safefree(sel_org);
  135. return result;
  136. }
  137. k -= amount;
  138. sel += amount;
  139. if(k < 1)
  140. break; /* but it did write it all */
  141. }
  142. else {
  143. failf(data, "Failed sending Gopher request");
  144. Curl_safefree(sel_org);
  145. return result;
  146. }
  147. /* Don't busyloop. The entire loop thing is a work-around as it causes a
  148. BLOCKING behavior which is a NO-NO. This function should rather be
  149. split up in a do and a doing piece where the pieces that aren't
  150. possible to send now will be sent in the doing function repeatedly
  151. until the entire request is sent.
  152. Wait a while for the socket to be writable. Note that this doesn't
  153. acknowledge the timeout.
  154. */
  155. Curl_socket_ready(CURL_SOCKET_BAD, sockfd, 100);
  156. }
  157. Curl_safefree(sel_org);
  158. /* We can use Curl_sendf to send the terminal \r\n relatively safely and
  159. save allocing another string/doing another _write loop. */
  160. result = Curl_sendf(sockfd, conn, "\r\n");
  161. if(result != CURLE_OK) {
  162. failf(data, "Failed sending Gopher request");
  163. return result;
  164. }
  165. result = Curl_client_write(conn, CLIENTWRITE_HEADER, (char *)"\r\n", 2);
  166. if(result)
  167. return result;
  168. Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
  169. -1, NULL); /* no upload */
  170. return CURLE_OK;
  171. }
  172. #endif /*CURL_DISABLE_GOPHER*/