2
0

gopher.c 5.8 KB

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